]> git.neil.brown.name Git - mdadm.git/blob - super-intel.c
0562a557df91db8fbc664daba6727b750592501c
[mdadm.git] / super-intel.c
1 /*
2  * mdadm - Intel(R) Matrix Storage Manager Support
3  *
4  * Copyright (C) 2002-2008 Intel Corporation
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
20 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "sha1.h"
24 #include "platform-intel.h"
25 #include <values.h>
26 #include <scsi/sg.h>
27 #include <ctype.h>
28 #include <dirent.h>
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"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
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"
41 #define MAX_SIGNATURE_LENGTH  32
42 #define MAX_RAID_SERIAL_LEN   16
43
44 /* supports RAID0 */
45 #define MPB_ATTRIB_RAID0                __cpu_to_le32(0x00000001)
46 /* supports RAID1 */
47 #define MPB_ATTRIB_RAID1                __cpu_to_le32(0x00000002)
48 /* supports RAID10 */
49 #define MPB_ATTRIB_RAID10               __cpu_to_le32(0x00000004)
50 /* supports RAID1E */
51 #define MPB_ATTRIB_RAID1E               __cpu_to_le32(0x00000008)
52 /* supports RAID5 */
53 #define MPB_ATTRIB_RAID5                __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG              __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of  256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE      __cpu_to_le32(0x00000040)
58
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM                  __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK             __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM                  __cpu_to_le32(0x08000000)
65
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2           __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB                  __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE            __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY      __cpu_to_le32(0x80000000)
74
75 /* Define all supported attributes that have to be accepted by mdadm
76  */
77 #define MPB_ATTRIB_SUPPORTED           (MPB_ATTRIB_CHECKSUM_VERIFY | \
78                                         MPB_ATTRIB_2TB             | \
79                                         MPB_ATTRIB_2TB_DISK        | \
80                                         MPB_ATTRIB_RAID0           | \
81                                         MPB_ATTRIB_RAID1           | \
82                                         MPB_ATTRIB_RAID10          | \
83                                         MPB_ATTRIB_RAID5           | \
84                                         MPB_ATTRIB_EXP_STRIPE_SIZE | \
85                                         MPB_ATTRIB_BBM)
86
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED              (MPB_ATTRIB_NEVER_USE)
89
90 #define MPB_SECTOR_CNT 2210
91 #define IMSM_RESERVED_SECTORS 4096
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
95
96 /* Disk configuration info. */
97 #define IMSM_MAX_DEVICES 255
98 struct imsm_disk {
99         __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
100         __u32 total_blocks_lo;           /* 0xE8 - 0xEB total blocks lo */
101         __u32 scsi_id;                   /* 0xEC - 0xEF scsi ID */
102 #define SPARE_DISK      __cpu_to_le32(0x01)  /* Spare */
103 #define CONFIGURED_DISK __cpu_to_le32(0x02)  /* Member of some RaidDev */
104 #define FAILED_DISK     __cpu_to_le32(0x04)  /* Permanent failure */
105         __u32 status;                    /* 0xF0 - 0xF3 */
106         __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
107         __u32 total_blocks_hi;           /* 0xF4 - 0xF5 total blocks hi */
108 #define IMSM_DISK_FILLERS       3
109         __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
110 };
111
112 /* map selector for map managment
113  */
114 #define MAP_0           0
115 #define MAP_1           1
116 #define MAP_X           -1
117
118 /* RAID map configuration infos. */
119 struct imsm_map {
120         __u32 pba_of_lba0_lo;   /* start address of partition */
121         __u32 blocks_per_member_lo;/* blocks per member */
122         __u32 num_data_stripes_lo;      /* number of data stripes */
123         __u16 blocks_per_strip;
124         __u8  map_state;        /* Normal, Uninitialized, Degraded, Failed */
125 #define IMSM_T_STATE_NORMAL 0
126 #define IMSM_T_STATE_UNINITIALIZED 1
127 #define IMSM_T_STATE_DEGRADED 2
128 #define IMSM_T_STATE_FAILED 3
129         __u8  raid_level;
130 #define IMSM_T_RAID0 0
131 #define IMSM_T_RAID1 1
132 #define IMSM_T_RAID5 5          /* since metadata version 1.2.02 ? */
133         __u8  num_members;      /* number of member disks */
134         __u8  num_domains;      /* number of parity domains */
135         __u8  failed_disk_num;  /* valid only when state is degraded */
136         __u8  ddf;
137         __u32 pba_of_lba0_hi;
138         __u32 blocks_per_member_hi;
139         __u32 num_data_stripes_hi;
140         __u32 filler[4];        /* expansion area */
141 #define IMSM_ORD_REBUILD (1 << 24)
142         __u32 disk_ord_tbl[1];  /* disk_ord_tbl[num_members],
143                                  * top byte contains some flags
144                                  */
145 } __attribute__ ((packed));
146
147 struct imsm_vol {
148         __u32 curr_migr_unit;
149         __u32 checkpoint_id;    /* id to access curr_migr_unit */
150         __u8  migr_state;       /* Normal or Migrating */
151 #define MIGR_INIT 0
152 #define MIGR_REBUILD 1
153 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
154 #define MIGR_GEN_MIGR 3
155 #define MIGR_STATE_CHANGE 4
156 #define MIGR_REPAIR 5
157         __u8  migr_type;        /* Initializing, Rebuilding, ... */
158         __u8  dirty;
159         __u8  fs_state;         /* fast-sync state for CnG (0xff == disabled) */
160         __u16 verify_errors;    /* number of mismatches */
161         __u16 bad_blocks;       /* number of bad blocks during verify */
162         __u32 filler[4];
163         struct imsm_map map[1];
164         /* here comes another one if migr_state */
165 } __attribute__ ((packed));
166
167 struct imsm_dev {
168         __u8  volume[MAX_RAID_SERIAL_LEN];
169         __u32 size_low;
170         __u32 size_high;
171 #define DEV_BOOTABLE            __cpu_to_le32(0x01)
172 #define DEV_BOOT_DEVICE         __cpu_to_le32(0x02)
173 #define DEV_READ_COALESCING     __cpu_to_le32(0x04)
174 #define DEV_WRITE_COALESCING    __cpu_to_le32(0x08)
175 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
176 #define DEV_HIDDEN_AT_BOOT      __cpu_to_le32(0x20)
177 #define DEV_CURRENTLY_HIDDEN    __cpu_to_le32(0x40)
178 #define DEV_VERIFY_AND_FIX      __cpu_to_le32(0x80)
179 #define DEV_MAP_STATE_UNINIT    __cpu_to_le32(0x100)
180 #define DEV_NO_AUTO_RECOVERY    __cpu_to_le32(0x200)
181 #define DEV_CLONE_N_GO          __cpu_to_le32(0x400)
182 #define DEV_CLONE_MAN_SYNC      __cpu_to_le32(0x800)
183 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
184         __u32 status;   /* Persistent RaidDev status */
185         __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
186         __u8  migr_priority;
187         __u8  num_sub_vols;
188         __u8  tid;
189         __u8  cng_master_disk;
190         __u16 cache_policy;
191         __u8  cng_state;
192         __u8  cng_sub_state;
193 #define IMSM_DEV_FILLERS 10
194         __u32 filler[IMSM_DEV_FILLERS];
195         struct imsm_vol vol;
196 } __attribute__ ((packed));
197
198 struct imsm_super {
199         __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
200         __u32 check_sum;                /* 0x20 - 0x23 MPB Checksum */
201         __u32 mpb_size;                 /* 0x24 - 0x27 Size of MPB */
202         __u32 family_num;               /* 0x28 - 0x2B Checksum from first time this config was written */
203         __u32 generation_num;           /* 0x2C - 0x2F Incremented each time this array's MPB is written */
204         __u32 error_log_size;           /* 0x30 - 0x33 in bytes */
205         __u32 attributes;               /* 0x34 - 0x37 */
206         __u8 num_disks;                 /* 0x38 Number of configured disks */
207         __u8 num_raid_devs;             /* 0x39 Number of configured volumes */
208         __u8 error_log_pos;             /* 0x3A  */
209         __u8 fill[1];                   /* 0x3B */
210         __u32 cache_size;               /* 0x3c - 0x40 in mb */
211         __u32 orig_family_num;          /* 0x40 - 0x43 original family num */
212         __u32 pwr_cycle_count;          /* 0x44 - 0x47 simulated power cycle count for array */
213         __u32 bbm_log_size;             /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
214 #define IMSM_FILLERS 35
215         __u32 filler[IMSM_FILLERS];     /* 0x4C - 0xD7 RAID_MPB_FILLERS */
216         struct imsm_disk disk[1];       /* 0xD8 diskTbl[numDisks] */
217         /* here comes imsm_dev[num_raid_devs] */
218         /* here comes BBM logs */
219 } __attribute__ ((packed));
220
221 #define BBM_LOG_MAX_ENTRIES 254
222 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256           /* Represents 256 LBAs */
223 #define BBM_LOG_SIGNATURE 0xabadb10c
224
225 struct bbm_log_block_addr {
226         __u16 w1;
227         __u32 dw1;
228 } __attribute__ ((__packed__));
229
230 struct bbm_log_entry {
231         __u8 marked_count;              /* Number of blocks marked - 1 */
232         __u8 disk_ordinal;              /* Disk entry within the imsm_super */
233         struct bbm_log_block_addr defective_block_start;
234 } __attribute__ ((__packed__));
235
236 struct bbm_log {
237         __u32 signature; /* 0xABADB10C */
238         __u32 entry_count;
239         struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
240 } __attribute__ ((__packed__));
241
242 #ifndef MDASSEMBLE
243 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
244 #endif
245
246 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
247
248 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
249
250 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
251 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
252                                * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
253                                */
254
255 #define UNIT_SRC_NORMAL     0   /* Source data for curr_migr_unit must
256                                  *  be recovered using srcMap */
257 #define UNIT_SRC_IN_CP_AREA 1   /* Source data for curr_migr_unit has
258                                  *  already been migrated and must
259                                  *  be recovered from checkpoint area */
260 struct migr_record {
261         __u32 rec_status;           /* Status used to determine how to restart
262                                      * migration in case it aborts
263                                      * in some fashion */
264         __u32 curr_migr_unit;       /* 0..numMigrUnits-1 */
265         __u32 family_num;           /* Family number of MPB
266                                      * containing the RaidDev
267                                      * that is migrating */
268         __u32 ascending_migr;       /* True if migrating in increasing
269                                      * order of lbas */
270         __u32 blocks_per_unit;      /* Num disk blocks per unit of operation */
271         __u32 dest_depth_per_unit;  /* Num member blocks each destMap
272                                      * member disk
273                                      * advances per unit-of-operation */
274         __u32 ckpt_area_pba;        /* Pba of first block of ckpt copy area */
275         __u32 dest_1st_member_lba;  /* First member lba on first
276                                      * stripe of destination */
277         __u32 num_migr_units;       /* Total num migration units-of-op */
278         __u32 post_migr_vol_cap;    /* Size of volume after
279                                      * migration completes */
280         __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
281         __u32 ckpt_read_disk_num;   /* Which member disk in destSubMap[0] the
282                                      * migration ckpt record was read from
283                                      * (for recovered migrations) */
284 } __attribute__ ((__packed__));
285
286 struct md_list {
287         /* usage marker:
288          *  1: load metadata
289          *  2: metadata does not match
290          *  4: already checked
291          */
292         int   used;
293         char  *devname;
294         int   found;
295         int   container;
296         dev_t st_rdev;
297         struct md_list *next;
298 };
299
300 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
301
302 static __u8 migr_type(struct imsm_dev *dev)
303 {
304         if (dev->vol.migr_type == MIGR_VERIFY &&
305             dev->status & DEV_VERIFY_AND_FIX)
306                 return MIGR_REPAIR;
307         else
308                 return dev->vol.migr_type;
309 }
310
311 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
312 {
313         /* for compatibility with older oroms convert MIGR_REPAIR, into
314          * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
315          */
316         if (migr_type == MIGR_REPAIR) {
317                 dev->vol.migr_type = MIGR_VERIFY;
318                 dev->status |= DEV_VERIFY_AND_FIX;
319         } else {
320                 dev->vol.migr_type = migr_type;
321                 dev->status &= ~DEV_VERIFY_AND_FIX;
322         }
323 }
324
325 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
326 {
327         return ROUND_UP(bytes, sector_size) / sector_size;
328 }
329
330 static unsigned int mpb_sectors(struct imsm_super *mpb,
331                                         unsigned int sector_size)
332 {
333         return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
334 }
335
336 struct intel_dev {
337         struct imsm_dev *dev;
338         struct intel_dev *next;
339         unsigned index;
340 };
341
342 struct intel_hba {
343         enum sys_dev_type type;
344         char *path;
345         char *pci_id;
346         struct intel_hba *next;
347 };
348
349 enum action {
350         DISK_REMOVE = 1,
351         DISK_ADD
352 };
353 /* internal representation of IMSM metadata */
354 struct intel_super {
355         union {
356                 void *buf; /* O_DIRECT buffer for reading/writing metadata */
357                 struct imsm_super *anchor; /* immovable parameters */
358         };
359         union {
360                 void *migr_rec_buf; /* buffer for I/O operations */
361                 struct migr_record *migr_rec; /* migration record */
362         };
363         int clean_migration_record_by_mdmon; /* when reshape is switched to next
364                 array, it indicates that mdmon is allowed to clean migration
365                 record */
366         size_t len; /* size of the 'buf' allocation */
367         size_t extra_space; /* extra space in 'buf' that is not used yet */
368         void *next_buf; /* for realloc'ing buf from the manager */
369         size_t next_len;
370         int updates_pending; /* count of pending updates for mdmon */
371         int current_vol; /* index of raid device undergoing creation */
372         unsigned long long create_offset; /* common start for 'current_vol' */
373         __u32 random; /* random data for seeding new family numbers */
374         struct intel_dev *devlist;
375         unsigned int sector_size; /* sector size of used member drives */
376         struct dl {
377                 struct dl *next;
378                 int index;
379                 __u8 serial[MAX_RAID_SERIAL_LEN];
380                 int major, minor;
381                 char *devname;
382                 struct imsm_disk disk;
383                 int fd;
384                 int extent_cnt;
385                 struct extent *e; /* for determining freespace @ create */
386                 int raiddisk; /* slot to fill in autolayout */
387                 enum action action;
388         } *disks, *current_disk;
389         struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
390                                       active */
391         struct dl *missing; /* disks removed while we weren't looking */
392         struct bbm_log *bbm_log;
393         struct intel_hba *hba; /* device path of the raid controller for this metadata */
394         const struct imsm_orom *orom; /* platform firmware support */
395         struct intel_super *next; /* (temp) list for disambiguating family_num */
396 };
397
398 struct intel_disk {
399         struct imsm_disk disk;
400         #define IMSM_UNKNOWN_OWNER (-1)
401         int owner;
402         struct intel_disk *next;
403 };
404
405 struct extent {
406         unsigned long long start, size;
407 };
408
409 /* definitions of reshape process types */
410 enum imsm_reshape_type {
411         CH_TAKEOVER,
412         CH_MIGRATION,
413         CH_ARRAY_SIZE,
414 };
415
416 /* definition of messages passed to imsm_process_update */
417 enum imsm_update_type {
418         update_activate_spare,
419         update_create_array,
420         update_kill_array,
421         update_rename_array,
422         update_add_remove_disk,
423         update_reshape_container_disks,
424         update_reshape_migration,
425         update_takeover,
426         update_general_migration_checkpoint,
427         update_size_change,
428         update_prealloc_badblocks_mem,
429 };
430
431 struct imsm_update_activate_spare {
432         enum imsm_update_type type;
433         struct dl *dl;
434         int slot;
435         int array;
436         struct imsm_update_activate_spare *next;
437 };
438
439 struct geo_params {
440         char devnm[32];
441         char *dev_name;
442         unsigned long long size;
443         int level;
444         int layout;
445         int chunksize;
446         int raid_disks;
447 };
448
449 enum takeover_direction {
450         R10_TO_R0,
451         R0_TO_R10
452 };
453 struct imsm_update_takeover {
454         enum imsm_update_type type;
455         int subarray;
456         enum takeover_direction direction;
457 };
458
459 struct imsm_update_reshape {
460         enum imsm_update_type type;
461         int old_raid_disks;
462         int new_raid_disks;
463
464         int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
465 };
466
467 struct imsm_update_reshape_migration {
468         enum imsm_update_type type;
469         int old_raid_disks;
470         int new_raid_disks;
471         /* fields for array migration changes
472          */
473         int subdev;
474         int new_level;
475         int new_layout;
476         int new_chunksize;
477
478         int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
479 };
480
481 struct imsm_update_size_change {
482         enum imsm_update_type type;
483         int subdev;
484         long long new_size;
485 };
486
487 struct imsm_update_general_migration_checkpoint {
488         enum imsm_update_type type;
489         __u32 curr_migr_unit;
490 };
491
492 struct disk_info {
493         __u8 serial[MAX_RAID_SERIAL_LEN];
494 };
495
496 struct imsm_update_create_array {
497         enum imsm_update_type type;
498         int dev_idx;
499         struct imsm_dev dev;
500 };
501
502 struct imsm_update_kill_array {
503         enum imsm_update_type type;
504         int dev_idx;
505 };
506
507 struct imsm_update_rename_array {
508         enum imsm_update_type type;
509         __u8 name[MAX_RAID_SERIAL_LEN];
510         int dev_idx;
511 };
512
513 struct imsm_update_add_remove_disk {
514         enum imsm_update_type type;
515 };
516
517 struct imsm_update_prealloc_bb_mem {
518         enum imsm_update_type type;
519 };
520
521 static const char *_sys_dev_type[] = {
522         [SYS_DEV_UNKNOWN] = "Unknown",
523         [SYS_DEV_SAS] = "SAS",
524         [SYS_DEV_SATA] = "SATA",
525         [SYS_DEV_NVME] = "NVMe",
526         [SYS_DEV_VMD] = "VMD"
527 };
528
529 const char *get_sys_dev_type(enum sys_dev_type type)
530 {
531         if (type >= SYS_DEV_MAX)
532                 type = SYS_DEV_UNKNOWN;
533
534         return _sys_dev_type[type];
535 }
536
537 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
538 {
539         struct intel_hba *result = xmalloc(sizeof(*result));
540
541         result->type = device->type;
542         result->path = xstrdup(device->path);
543         result->next = NULL;
544         if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
545                 result->pci_id++;
546
547         return result;
548 }
549
550 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
551 {
552         struct intel_hba *result;
553
554         for (result = hba; result; result = result->next) {
555                 if (result->type == device->type && strcmp(result->path, device->path) == 0)
556                         break;
557         }
558         return result;
559 }
560
561 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
562 {
563         struct intel_hba *hba;
564
565         /* check if disk attached to Intel HBA */
566         hba = find_intel_hba(super->hba, device);
567         if (hba != NULL)
568                 return 1;
569         /* Check if HBA is already attached to super */
570         if (super->hba == NULL) {
571                 super->hba = alloc_intel_hba(device);
572                 return 1;
573         }
574
575         hba = super->hba;
576         /* Intel metadata allows for all disks attached to the same type HBA.
577          * Do not support HBA types mixing
578          */
579         if (device->type != hba->type)
580                 return 2;
581
582         /* Multiple same type HBAs can be used if they share the same OROM */
583         const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
584
585         if (device_orom != super->orom)
586                 return 2;
587
588         while (hba->next)
589                 hba = hba->next;
590
591         hba->next = alloc_intel_hba(device);
592         return 1;
593 }
594
595 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
596 {
597         struct sys_dev *list, *elem;
598         char *disk_path;
599
600         if ((list = find_intel_devices()) == NULL)
601                 return 0;
602
603         if (fd < 0)
604                 disk_path  = (char *) devname;
605         else
606                 disk_path = diskfd_to_devpath(fd);
607
608         if (!disk_path)
609                 return 0;
610
611         for (elem = list; elem; elem = elem->next)
612                 if (path_attached_to_hba(disk_path, elem->path))
613                         return elem;
614
615         if (disk_path != devname)
616                 free(disk_path);
617
618         return NULL;
619 }
620
621 static int find_intel_hba_capability(int fd, struct intel_super *super,
622                                      char *devname);
623
624 static struct supertype *match_metadata_desc_imsm(char *arg)
625 {
626         struct supertype *st;
627
628         if (strcmp(arg, "imsm") != 0 &&
629             strcmp(arg, "default") != 0
630                 )
631                 return NULL;
632
633         st = xcalloc(1, sizeof(*st));
634         st->ss = &super_imsm;
635         st->max_devs = IMSM_MAX_DEVICES;
636         st->minor_version = 0;
637         st->sb = NULL;
638         return st;
639 }
640
641 #ifndef MDASSEMBLE
642 static __u8 *get_imsm_version(struct imsm_super *mpb)
643 {
644         return &mpb->sig[MPB_SIG_LEN];
645 }
646 #endif
647
648 /* retrieve a disk directly from the anchor when the anchor is known to be
649  * up-to-date, currently only at load time
650  */
651 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
652 {
653         if (index >= mpb->num_disks)
654                 return NULL;
655         return &mpb->disk[index];
656 }
657
658 /* retrieve the disk description based on a index of the disk
659  * in the sub-array
660  */
661 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
662 {
663         struct dl *d;
664
665         for (d = super->disks; d; d = d->next)
666                 if (d->index == index)
667                         return d;
668
669         return NULL;
670 }
671 /* retrieve a disk from the parsed metadata */
672 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
673 {
674         struct dl *dl;
675
676         dl = get_imsm_dl_disk(super, index);
677         if (dl)
678                 return &dl->disk;
679
680         return NULL;
681 }
682
683 /* generate a checksum directly from the anchor when the anchor is known to be
684  * up-to-date, currently only at load or write_super after coalescing
685  */
686 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
687 {
688         __u32 end = mpb->mpb_size / sizeof(end);
689         __u32 *p = (__u32 *) mpb;
690         __u32 sum = 0;
691
692         while (end--) {
693                 sum += __le32_to_cpu(*p);
694                 p++;
695         }
696
697         return sum - __le32_to_cpu(mpb->check_sum);
698 }
699
700 static size_t sizeof_imsm_map(struct imsm_map *map)
701 {
702         return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
703 }
704
705 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
706 {
707         /* A device can have 2 maps if it is in the middle of a migration.
708          * If second_map is:
709          *    MAP_0 - we return the first map
710          *    MAP_1 - we return the second map if it exists, else NULL
711          *    MAP_X - we return the second map if it exists, else the first
712          */
713         struct imsm_map *map = &dev->vol.map[0];
714         struct imsm_map *map2 = NULL;
715
716         if (dev->vol.migr_state)
717                 map2 = (void *)map + sizeof_imsm_map(map);
718
719         switch (second_map) {
720         case MAP_0:
721                 break;
722         case MAP_1:
723                 map = map2;
724                 break;
725         case MAP_X:
726                 if (map2)
727                         map = map2;
728                 break;
729         default:
730                 map = NULL;
731         }
732         return map;
733
734 }
735
736 /* return the size of the device.
737  * migr_state increases the returned size if map[0] were to be duplicated
738  */
739 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
740 {
741         size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
742                       sizeof_imsm_map(get_imsm_map(dev, MAP_0));
743
744         /* migrating means an additional map */
745         if (dev->vol.migr_state)
746                 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
747         else if (migr_state)
748                 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
749
750         return size;
751 }
752
753 #ifndef MDASSEMBLE
754 /* retrieve disk serial number list from a metadata update */
755 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
756 {
757         void *u = update;
758         struct disk_info *inf;
759
760         inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
761               sizeof_imsm_dev(&update->dev, 0);
762
763         return inf;
764 }
765 #endif
766
767 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
768 {
769         int offset;
770         int i;
771         void *_mpb = mpb;
772
773         if (index >= mpb->num_raid_devs)
774                 return NULL;
775
776         /* devices start after all disks */
777         offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
778
779         for (i = 0; i <= index; i++)
780                 if (i == index)
781                         return _mpb + offset;
782                 else
783                         offset += sizeof_imsm_dev(_mpb + offset, 0);
784
785         return NULL;
786 }
787
788 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
789 {
790         struct intel_dev *dv;
791
792         if (index >= super->anchor->num_raid_devs)
793                 return NULL;
794         for (dv = super->devlist; dv; dv = dv->next)
795                 if (dv->index == index)
796                         return dv->dev;
797         return NULL;
798 }
799
800 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
801                                                *addr)
802 {
803         return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
804                 __le16_to_cpu(addr->w1));
805 }
806
807 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
808 {
809         struct bbm_log_block_addr addr;
810
811         addr.w1 =  __cpu_to_le16((__u16)(sec & 0xffff));
812         addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
813         return addr;
814 }
815
816 #ifndef MDASSEMBLE
817 /* get size of the bbm log */
818 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
819 {
820         if (!log || log->entry_count == 0)
821                 return 0;
822
823         return sizeof(log->signature) +
824                 sizeof(log->entry_count) +
825                 log->entry_count * sizeof(struct bbm_log_entry);
826 }
827
828 /* check if bad block is not partially stored in bbm log */
829 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
830                             long long sector, const int length, __u32 *pos)
831 {
832         __u32 i;
833
834         for (i = *pos; i < log->entry_count; i++) {
835                 struct bbm_log_entry *entry = &log->marked_block_entries[i];
836                 unsigned long long bb_start;
837                 unsigned long long bb_end;
838
839                 bb_start = __le48_to_cpu(&entry->defective_block_start);
840                 bb_end = bb_start + (entry->marked_count + 1);
841
842                 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
843                     (bb_end <= sector + length)) {
844                         *pos = i;
845                         return 1;
846                 }
847         }
848         return 0;
849 }
850
851 /* record new bad block in bbm log */
852 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
853                                long long sector, int length)
854 {
855         int new_bb = 0;
856         __u32 pos = 0;
857         struct bbm_log_entry *entry = NULL;
858
859         while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
860                 struct bbm_log_entry *e = &log->marked_block_entries[pos];
861
862                 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
863                     (__le48_to_cpu(&e->defective_block_start) == sector)) {
864                         sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
865                         length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
866                         pos = pos + 1;
867                         continue;
868                 }
869                 entry = e;
870                 break;
871         }
872
873         if (entry) {
874                 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
875                         BBM_LOG_MAX_LBA_ENTRY_VAL;
876                 entry->defective_block_start = __cpu_to_le48(sector);
877                 entry->marked_count = cnt - 1;
878                 if (cnt == length)
879                         return 1;
880                 sector += cnt;
881                 length -= cnt;
882         }
883
884         new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
885                 BBM_LOG_MAX_LBA_ENTRY_VAL;
886         if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
887                 return 0;
888
889         while (length > 0) {
890                 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
891                         BBM_LOG_MAX_LBA_ENTRY_VAL;
892                 struct bbm_log_entry *entry =
893                         &log->marked_block_entries[log->entry_count];
894
895                 entry->defective_block_start = __cpu_to_le48(sector);
896                 entry->marked_count = cnt - 1;
897                 entry->disk_ordinal = idx;
898
899                 sector += cnt;
900                 length -= cnt;
901
902                 log->entry_count++;
903         }
904
905         return new_bb;
906 }
907
908 /* clear all bad blocks for given disk */
909 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
910 {
911         __u32 i = 0;
912
913         while (i < log->entry_count) {
914                 struct bbm_log_entry *entries = log->marked_block_entries;
915
916                 if (entries[i].disk_ordinal == idx) {
917                         if (i < log->entry_count - 1)
918                                 entries[i] = entries[log->entry_count - 1];
919                         log->entry_count--;
920                 } else {
921                         i++;
922                 }
923         }
924 }
925
926 /* clear given bad block */
927 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
928                           long long sector, const int length) {
929         __u32 i = 0;
930
931         while (i < log->entry_count) {
932                 struct bbm_log_entry *entries = log->marked_block_entries;
933
934                 if ((entries[i].disk_ordinal == idx) &&
935                     (__le48_to_cpu(&entries[i].defective_block_start) ==
936                      sector) && (entries[i].marked_count + 1 == length)) {
937                         if (i < log->entry_count - 1)
938                                 entries[i] = entries[log->entry_count - 1];
939                         log->entry_count--;
940                         break;
941                 }
942                 i++;
943         }
944
945         return 1;
946 }
947 #endif /* MDASSEMBLE */
948
949 /* allocate and load BBM log from metadata */
950 static int load_bbm_log(struct intel_super *super)
951 {
952         struct imsm_super *mpb = super->anchor;
953         __u32 bbm_log_size =  __le32_to_cpu(mpb->bbm_log_size);
954
955         super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
956         if (!super->bbm_log)
957                 return 1;
958
959         if (bbm_log_size) {
960                 struct bbm_log *log = (void *)mpb +
961                         __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
962
963                 __u32 entry_count;
964
965                 if (bbm_log_size < sizeof(log->signature) +
966                     sizeof(log->entry_count))
967                         return 2;
968
969                 entry_count = __le32_to_cpu(log->entry_count);
970                 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
971                     (entry_count > BBM_LOG_MAX_ENTRIES))
972                         return 3;
973
974                 if (bbm_log_size !=
975                     sizeof(log->signature) + sizeof(log->entry_count) +
976                     entry_count * sizeof(struct bbm_log_entry))
977                         return 4;
978
979                 memcpy(super->bbm_log, log, bbm_log_size);
980         } else {
981                 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
982                 super->bbm_log->entry_count = 0;
983         }
984
985         return 0;
986 }
987
988 /* checks if bad block is within volume boundaries */
989 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
990                         const unsigned long long start_sector,
991                         const unsigned long long size)
992 {
993         unsigned long long bb_start;
994         unsigned long long bb_end;
995
996         bb_start = __le48_to_cpu(&entry->defective_block_start);
997         bb_end = bb_start + (entry->marked_count + 1);
998
999         if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1000             ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1001                 return 1;
1002
1003         return 0;
1004 }
1005
1006 /* get list of bad blocks on a drive for a volume */
1007 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1008                         const unsigned long long start_sector,
1009                         const unsigned long long size,
1010                         struct md_bb *bbs)
1011 {
1012         __u32 count = 0;
1013         __u32 i;
1014
1015         for (i = 0; i < log->entry_count; i++) {
1016                 const struct bbm_log_entry *ent =
1017                         &log->marked_block_entries[i];
1018                 struct md_bb_entry *bb;
1019
1020                 if ((ent->disk_ordinal == idx) &&
1021                     is_bad_block_in_volume(ent, start_sector, size)) {
1022
1023                         if (!bbs->entries) {
1024                                 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1025                                                      sizeof(*bb));
1026                                 if (!bbs->entries)
1027                                         break;
1028                         }
1029
1030                         bb = &bbs->entries[count++];
1031                         bb->sector = __le48_to_cpu(&ent->defective_block_start);
1032                         bb->length = ent->marked_count + 1;
1033                 }
1034         }
1035         bbs->count = count;
1036 }
1037
1038 /*
1039  * for second_map:
1040  *  == MAP_0 get first map
1041  *  == MAP_1 get second map
1042  *  == MAP_X than get map according to the current migr_state
1043  */
1044 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1045                                   int slot,
1046                                   int second_map)
1047 {
1048         struct imsm_map *map;
1049
1050         map = get_imsm_map(dev, second_map);
1051
1052         /* top byte identifies disk under rebuild */
1053         return __le32_to_cpu(map->disk_ord_tbl[slot]);
1054 }
1055
1056 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1057 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1058 {
1059         __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1060
1061         return ord_to_idx(ord);
1062 }
1063
1064 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1065 {
1066         map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1067 }
1068
1069 static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
1070 {
1071         int slot;
1072         __u32 ord;
1073
1074         for (slot = 0; slot < map->num_members; slot++) {
1075                 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1076                 if (ord_to_idx(ord) == idx)
1077                         return slot;
1078         }
1079
1080         return -1;
1081 }
1082
1083 static int get_imsm_raid_level(struct imsm_map *map)
1084 {
1085         if (map->raid_level == 1) {
1086                 if (map->num_members == 2)
1087                         return 1;
1088                 else
1089                         return 10;
1090         }
1091
1092         return map->raid_level;
1093 }
1094
1095 static int cmp_extent(const void *av, const void *bv)
1096 {
1097         const struct extent *a = av;
1098         const struct extent *b = bv;
1099         if (a->start < b->start)
1100                 return -1;
1101         if (a->start > b->start)
1102                 return 1;
1103         return 0;
1104 }
1105
1106 static int count_memberships(struct dl *dl, struct intel_super *super)
1107 {
1108         int memberships = 0;
1109         int i;
1110
1111         for (i = 0; i < super->anchor->num_raid_devs; i++) {
1112                 struct imsm_dev *dev = get_imsm_dev(super, i);
1113                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1114
1115                 if (get_imsm_disk_slot(map, dl->index) >= 0)
1116                         memberships++;
1117         }
1118
1119         return memberships;
1120 }
1121
1122 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1123
1124 static int split_ull(unsigned long long n, __u32 *lo, __u32 *hi)
1125 {
1126         if (lo == 0 || hi == 0)
1127                 return 1;
1128         *lo = __le32_to_cpu((unsigned)n);
1129         *hi = __le32_to_cpu((unsigned)(n >> 32));
1130         return 0;
1131 }
1132
1133 static unsigned long long join_u32(__u32 lo, __u32 hi)
1134 {
1135         return (unsigned long long)__le32_to_cpu(lo) |
1136                (((unsigned long long)__le32_to_cpu(hi)) << 32);
1137 }
1138
1139 static unsigned long long total_blocks(struct imsm_disk *disk)
1140 {
1141         if (disk == NULL)
1142                 return 0;
1143         return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1144 }
1145
1146 static unsigned long long pba_of_lba0(struct imsm_map *map)
1147 {
1148         if (map == NULL)
1149                 return 0;
1150         return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1151 }
1152
1153 static unsigned long long blocks_per_member(struct imsm_map *map)
1154 {
1155         if (map == NULL)
1156                 return 0;
1157         return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1158 }
1159
1160 static unsigned long long num_data_stripes(struct imsm_map *map)
1161 {
1162         if (map == NULL)
1163                 return 0;
1164         return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1165 }
1166
1167 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1168 {
1169         split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1170 }
1171
1172 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1173 {
1174         split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1175 }
1176
1177 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1178 {
1179         split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1180 }
1181
1182 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1183 {
1184         split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1185 }
1186
1187 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
1188 {
1189         /* find a list of used extents on the given physical device */
1190         struct extent *rv, *e;
1191         int i;
1192         int memberships = count_memberships(dl, super);
1193         __u32 reservation;
1194
1195         /* trim the reserved area for spares, so they can join any array
1196          * regardless of whether the OROM has assigned sectors from the
1197          * IMSM_RESERVED_SECTORS region
1198          */
1199         if (dl->index == -1)
1200                 reservation = imsm_min_reserved_sectors(super);
1201         else
1202                 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1203
1204         rv = xcalloc(sizeof(struct extent), (memberships + 1));
1205         e = rv;
1206
1207         for (i = 0; i < super->anchor->num_raid_devs; i++) {
1208                 struct imsm_dev *dev = get_imsm_dev(super, i);
1209                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1210
1211                 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1212                         e->start = pba_of_lba0(map);
1213                         e->size = blocks_per_member(map);
1214                         e++;
1215                 }
1216         }
1217         qsort(rv, memberships, sizeof(*rv), cmp_extent);
1218
1219         /* determine the start of the metadata
1220          * when no raid devices are defined use the default
1221          * ...otherwise allow the metadata to truncate the value
1222          * as is the case with older versions of imsm
1223          */
1224         if (memberships) {
1225                 struct extent *last = &rv[memberships - 1];
1226                 unsigned long long remainder;
1227
1228                 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1229                 /* round down to 1k block to satisfy precision of the kernel
1230                  * 'size' interface
1231                  */
1232                 remainder &= ~1UL;
1233                 /* make sure remainder is still sane */
1234                 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1235                         remainder = ROUND_UP(super->len, 512) >> 9;
1236                 if (reservation > remainder)
1237                         reservation = remainder;
1238         }
1239         e->start = total_blocks(&dl->disk) - reservation;
1240         e->size = 0;
1241         return rv;
1242 }
1243
1244 /* try to determine how much space is reserved for metadata from
1245  * the last get_extents() entry, otherwise fallback to the
1246  * default
1247  */
1248 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1249 {
1250         struct extent *e;
1251         int i;
1252         __u32 rv;
1253
1254         /* for spares just return a minimal reservation which will grow
1255          * once the spare is picked up by an array
1256          */
1257         if (dl->index == -1)
1258                 return MPB_SECTOR_CNT;
1259
1260         e = get_extents(super, dl);
1261         if (!e)
1262                 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1263
1264         /* scroll to last entry */
1265         for (i = 0; e[i].size; i++)
1266                 continue;
1267
1268         rv = total_blocks(&dl->disk) - e[i].start;
1269
1270         free(e);
1271
1272         return rv;
1273 }
1274
1275 static int is_spare(struct imsm_disk *disk)
1276 {
1277         return (disk->status & SPARE_DISK) == SPARE_DISK;
1278 }
1279
1280 static int is_configured(struct imsm_disk *disk)
1281 {
1282         return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1283 }
1284
1285 static int is_failed(struct imsm_disk *disk)
1286 {
1287         return (disk->status & FAILED_DISK) == FAILED_DISK;
1288 }
1289
1290 /* try to determine how much space is reserved for metadata from
1291  * the last get_extents() entry on the smallest active disk,
1292  * otherwise fallback to the default
1293  */
1294 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1295 {
1296         struct extent *e;
1297         int i;
1298         unsigned long long min_active;
1299         __u32 remainder;
1300         __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1301         struct dl *dl, *dl_min = NULL;
1302
1303         if (!super)
1304                 return rv;
1305
1306         min_active = 0;
1307         for (dl = super->disks; dl; dl = dl->next) {
1308                 if (dl->index < 0)
1309                         continue;
1310                 unsigned long long blocks = total_blocks(&dl->disk);
1311                 if (blocks < min_active || min_active == 0) {
1312                         dl_min = dl;
1313                         min_active = blocks;
1314                 }
1315         }
1316         if (!dl_min)
1317                 return rv;
1318
1319         /* find last lba used by subarrays on the smallest active disk */
1320         e = get_extents(super, dl_min);
1321         if (!e)
1322                 return rv;
1323         for (i = 0; e[i].size; i++)
1324                 continue;
1325
1326         remainder = min_active - e[i].start;
1327         free(e);
1328
1329         /* to give priority to recovery we should not require full
1330            IMSM_RESERVED_SECTORS from the spare */
1331         rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1332
1333         /* if real reservation is smaller use that value */
1334         return  (remainder < rv) ? remainder : rv;
1335 }
1336
1337 /* Return minimum size of a spare that can be used in this array*/
1338 static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st)
1339 {
1340         struct intel_super *super = st->sb;
1341         struct dl *dl;
1342         struct extent *e;
1343         int i;
1344         unsigned long long rv = 0;
1345
1346         if (!super)
1347                 return rv;
1348         /* find first active disk in array */
1349         dl = super->disks;
1350         while (dl && (is_failed(&dl->disk) || dl->index == -1))
1351                 dl = dl->next;
1352         if (!dl)
1353                 return rv;
1354         /* find last lba used by subarrays */
1355         e = get_extents(super, dl);
1356         if (!e)
1357                 return rv;
1358         for (i = 0; e[i].size; i++)
1359                 continue;
1360         if (i > 0)
1361                 rv = e[i-1].start + e[i-1].size;
1362         free(e);
1363
1364         /* add the amount of space needed for metadata */
1365         rv = rv + imsm_min_reserved_sectors(super);
1366
1367         return rv * 512;
1368 }
1369
1370 static int is_gen_migration(struct imsm_dev *dev);
1371
1372 #define IMSM_4K_DIV 8
1373
1374 #ifndef MDASSEMBLE
1375 static __u64 blocks_per_migr_unit(struct intel_super *super,
1376                                   struct imsm_dev *dev);
1377
1378 static void print_imsm_dev(struct intel_super *super,
1379                            struct imsm_dev *dev,
1380                            char *uuid,
1381                            int disk_idx)
1382 {
1383         __u64 sz;
1384         int slot, i;
1385         struct imsm_map *map = get_imsm_map(dev, MAP_0);
1386         struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1387         __u32 ord;
1388
1389         printf("\n");
1390         printf("[%.16s]:\n", dev->volume);
1391         printf("           UUID : %s\n", uuid);
1392         printf("     RAID Level : %d", get_imsm_raid_level(map));
1393         if (map2)
1394                 printf(" <-- %d", get_imsm_raid_level(map2));
1395         printf("\n");
1396         printf("        Members : %d", map->num_members);
1397         if (map2)
1398                 printf(" <-- %d", map2->num_members);
1399         printf("\n");
1400         printf("          Slots : [");
1401         for (i = 0; i < map->num_members; i++) {
1402                 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1403                 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1404         }
1405         printf("]");
1406         if (map2) {
1407                 printf(" <-- [");
1408                 for (i = 0; i < map2->num_members; i++) {
1409                         ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1410                         printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1411                 }
1412                 printf("]");
1413         }
1414         printf("\n");
1415         printf("    Failed disk : ");
1416         if (map->failed_disk_num == 0xff)
1417                 printf("none");
1418         else
1419                 printf("%i", map->failed_disk_num);
1420         printf("\n");
1421         slot = get_imsm_disk_slot(map, disk_idx);
1422         if (slot >= 0) {
1423                 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1424                 printf("      This Slot : %d%s\n", slot,
1425                        ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1426         } else
1427                 printf("      This Slot : ?\n");
1428         sz = __le32_to_cpu(dev->size_high);
1429         sz <<= 32;
1430         sz += __le32_to_cpu(dev->size_low);
1431         printf("     Array Size : %llu%s\n", (unsigned long long)sz,
1432                human_size(sz * 512));
1433         sz = blocks_per_member(map);
1434         printf("   Per Dev Size : %llu%s\n", (unsigned long long)sz,
1435                human_size(sz * 512));
1436         printf("  Sector Offset : %llu\n",
1437                 pba_of_lba0(map));
1438         printf("    Num Stripes : %llu\n",
1439                 num_data_stripes(map));
1440         printf("     Chunk Size : %u KiB",
1441                 __le16_to_cpu(map->blocks_per_strip) / 2);
1442         if (map2)
1443                 printf(" <-- %u KiB",
1444                         __le16_to_cpu(map2->blocks_per_strip) / 2);
1445         printf("\n");
1446         printf("       Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1447         printf("  Migrate State : ");
1448         if (dev->vol.migr_state) {
1449                 if (migr_type(dev) == MIGR_INIT)
1450                         printf("initialize\n");
1451                 else if (migr_type(dev) == MIGR_REBUILD)
1452                         printf("rebuild\n");
1453                 else if (migr_type(dev) == MIGR_VERIFY)
1454                         printf("check\n");
1455                 else if (migr_type(dev) == MIGR_GEN_MIGR)
1456                         printf("general migration\n");
1457                 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1458                         printf("state change\n");
1459                 else if (migr_type(dev) == MIGR_REPAIR)
1460                         printf("repair\n");
1461                 else
1462                         printf("<unknown:%d>\n", migr_type(dev));
1463         } else
1464                 printf("idle\n");
1465         printf("      Map State : %s", map_state_str[map->map_state]);
1466         if (dev->vol.migr_state) {
1467                 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1468
1469                 printf(" <-- %s", map_state_str[map->map_state]);
1470                 printf("\n     Checkpoint : %u ",
1471                            __le32_to_cpu(dev->vol.curr_migr_unit));
1472                 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1473                         printf("(N/A)");
1474                 else
1475                         printf("(%llu)", (unsigned long long)
1476                                    blocks_per_migr_unit(super, dev));
1477         }
1478         printf("\n");
1479         printf("    Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
1480 }
1481
1482 static void print_imsm_disk(struct imsm_disk *disk, int index, __u32 reserved)
1483 {
1484         char str[MAX_RAID_SERIAL_LEN + 1];
1485         __u64 sz;
1486
1487         if (index < -1 || !disk)
1488                 return;
1489
1490         printf("\n");
1491         snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1492         if (index >= 0)
1493                 printf("  Disk%02d Serial : %s\n", index, str);
1494         else
1495                 printf("    Disk Serial : %s\n", str);
1496         printf("          State :%s%s%s\n", is_spare(disk) ? " spare" : "",
1497                                             is_configured(disk) ? " active" : "",
1498                                             is_failed(disk) ? " failed" : "");
1499         printf("             Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1500         sz = total_blocks(disk) - reserved;
1501         printf("    Usable Size : %llu%s\n", (unsigned long long)sz,
1502                human_size(sz * 512));
1503 }
1504
1505 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1506 {
1507         struct migr_record *migr_rec = super->migr_rec;
1508
1509         migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1510         migr_rec->ckpt_area_pba /= IMSM_4K_DIV;
1511         migr_rec->dest_1st_member_lba /= IMSM_4K_DIV;
1512         migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1513         split_ull((join_u32(migr_rec->post_migr_vol_cap,
1514                  migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1515                  &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1516 }
1517
1518 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1519 {
1520         set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1521 }
1522
1523 void convert_to_4k(struct intel_super *super)
1524 {
1525         struct imsm_super *mpb = super->anchor;
1526         struct imsm_disk *disk;
1527         int i;
1528
1529         for (i = 0; i < mpb->num_disks ; i++) {
1530                 disk = __get_imsm_disk(mpb, i);
1531                 /* disk */
1532                 convert_to_4k_imsm_disk(disk);
1533         }
1534         for (i = 0; i < mpb->num_raid_devs; i++) {
1535                 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1536                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1537                 /* dev */
1538                 split_ull((join_u32(dev->size_low, dev->size_high)/IMSM_4K_DIV),
1539                                  &dev->size_low, &dev->size_high);
1540                 dev->vol.curr_migr_unit /= IMSM_4K_DIV;
1541
1542                 /* map0 */
1543                 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1544                 map->blocks_per_strip /= IMSM_4K_DIV;
1545                 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1546
1547                 if (dev->vol.migr_state) {
1548                         /* map1 */
1549                         map = get_imsm_map(dev, MAP_1);
1550                         set_blocks_per_member(map,
1551                             blocks_per_member(map)/IMSM_4K_DIV);
1552                         map->blocks_per_strip /= IMSM_4K_DIV;
1553                         set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1554                 }
1555         }
1556
1557         mpb->check_sum = __gen_imsm_checksum(mpb);
1558 }
1559
1560 void examine_migr_rec_imsm(struct intel_super *super)
1561 {
1562         struct migr_record *migr_rec = super->migr_rec;
1563         struct imsm_super *mpb = super->anchor;
1564         int i;
1565
1566         for (i = 0; i < mpb->num_raid_devs; i++) {
1567                 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1568                 struct imsm_map *map;
1569                 int slot = -1;
1570
1571                 if (is_gen_migration(dev) == 0)
1572                                 continue;
1573
1574                 printf("\nMigration Record Information:");
1575
1576                 /* first map under migration */
1577                 map = get_imsm_map(dev, MAP_0);
1578                 if (map)
1579                         slot = get_imsm_disk_slot(map, super->disks->index);
1580                 if (map == NULL || slot > 1 || slot < 0) {
1581                         printf(" Empty\n                              ");
1582                         printf("Examine one of first two disks in array\n");
1583                         break;
1584                 }
1585                 printf("\n                     Status : ");
1586                 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1587                         printf("Normal\n");
1588                 else
1589                         printf("Contains Data\n");
1590                 printf("               Current Unit : %u\n",
1591                        __le32_to_cpu(migr_rec->curr_migr_unit));
1592                 printf("                     Family : %u\n",
1593                        __le32_to_cpu(migr_rec->family_num));
1594                 printf("                  Ascending : %u\n",
1595                        __le32_to_cpu(migr_rec->ascending_migr));
1596                 printf("            Blocks Per Unit : %u\n",
1597                        __le32_to_cpu(migr_rec->blocks_per_unit));
1598                 printf("       Dest. Depth Per Unit : %u\n",
1599                        __le32_to_cpu(migr_rec->dest_depth_per_unit));
1600                 printf("        Checkpoint Area pba : %u\n",
1601                        __le32_to_cpu(migr_rec->ckpt_area_pba));
1602                 printf("           First member lba : %u\n",
1603                        __le32_to_cpu(migr_rec->dest_1st_member_lba));
1604                 printf("      Total Number of Units : %u\n",
1605                        __le32_to_cpu(migr_rec->num_migr_units));
1606                 printf("             Size of volume : %u\n",
1607                        __le32_to_cpu(migr_rec->post_migr_vol_cap));
1608                 printf("  Expansion space for LBA64 : %u\n",
1609                        __le32_to_cpu(migr_rec->post_migr_vol_cap_hi));
1610                 printf("       Record was read from : %u\n",
1611                        __le32_to_cpu(migr_rec->ckpt_read_disk_num));
1612
1613                 break;
1614         }
1615 }
1616 #endif /* MDASSEMBLE */
1617
1618 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
1619 {
1620         struct migr_record *migr_rec = super->migr_rec;
1621
1622         migr_rec->blocks_per_unit *= IMSM_4K_DIV;
1623         migr_rec->ckpt_area_pba *= IMSM_4K_DIV;
1624         migr_rec->dest_1st_member_lba *= IMSM_4K_DIV;
1625         migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
1626         split_ull((join_u32(migr_rec->post_migr_vol_cap,
1627                  migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
1628                  &migr_rec->post_migr_vol_cap,
1629                  &migr_rec->post_migr_vol_cap_hi);
1630 }
1631
1632 void convert_from_4k(struct intel_super *super)
1633 {
1634         struct imsm_super *mpb = super->anchor;
1635         struct imsm_disk *disk;
1636         int i;
1637
1638         for (i = 0; i < mpb->num_disks ; i++) {
1639                 disk = __get_imsm_disk(mpb, i);
1640                 /* disk */
1641                 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
1642         }
1643
1644         for (i = 0; i < mpb->num_raid_devs; i++) {
1645                 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1646                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1647                 /* dev */
1648                 split_ull((join_u32(dev->size_low, dev->size_high)*IMSM_4K_DIV),
1649                                  &dev->size_low, &dev->size_high);
1650                 dev->vol.curr_migr_unit *= IMSM_4K_DIV;
1651
1652                 /* map0 */
1653                 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
1654                 map->blocks_per_strip *= IMSM_4K_DIV;
1655                 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1656
1657                 if (dev->vol.migr_state) {
1658                         /* map1 */
1659                         map = get_imsm_map(dev, MAP_1);
1660                         set_blocks_per_member(map,
1661                             blocks_per_member(map)*IMSM_4K_DIV);
1662                         map->blocks_per_strip *= IMSM_4K_DIV;
1663                         set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
1664                 }
1665         }
1666
1667         mpb->check_sum = __gen_imsm_checksum(mpb);
1668 }
1669
1670 /*******************************************************************************
1671  * function: imsm_check_attributes
1672  * Description: Function checks if features represented by attributes flags
1673  *              are supported by mdadm.
1674  * Parameters:
1675  *              attributes - Attributes read from metadata
1676  * Returns:
1677  *              0 - passed attributes contains unsupported features flags
1678  *              1 - all features are supported
1679  ******************************************************************************/
1680 static int imsm_check_attributes(__u32 attributes)
1681 {
1682         int ret_val = 1;
1683         __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
1684
1685         not_supported &= ~MPB_ATTRIB_IGNORED;
1686
1687         not_supported &= attributes;
1688         if (not_supported) {
1689                 pr_err("(IMSM): Unsupported attributes : %x\n",
1690                         (unsigned)__le32_to_cpu(not_supported));
1691                 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
1692                         dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1693                         not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
1694                 }
1695                 if (not_supported & MPB_ATTRIB_2TB) {
1696                         dprintf("\t\tMPB_ATTRIB_2TB\n");
1697                         not_supported ^= MPB_ATTRIB_2TB;
1698                 }
1699                 if (not_supported & MPB_ATTRIB_RAID0) {
1700                         dprintf("\t\tMPB_ATTRIB_RAID0\n");
1701                         not_supported ^= MPB_ATTRIB_RAID0;
1702                 }
1703                 if (not_supported & MPB_ATTRIB_RAID1) {
1704                         dprintf("\t\tMPB_ATTRIB_RAID1\n");
1705                         not_supported ^= MPB_ATTRIB_RAID1;
1706                 }
1707                 if (not_supported & MPB_ATTRIB_RAID10) {
1708                         dprintf("\t\tMPB_ATTRIB_RAID10\n");
1709                         not_supported ^= MPB_ATTRIB_RAID10;
1710                 }
1711                 if (not_supported & MPB_ATTRIB_RAID1E) {
1712                         dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1713                         not_supported ^= MPB_ATTRIB_RAID1E;
1714                 }
1715                 if (not_supported & MPB_ATTRIB_RAID5) {
1716                 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1717                         not_supported ^= MPB_ATTRIB_RAID5;
1718                 }
1719                 if (not_supported & MPB_ATTRIB_RAIDCNG) {
1720                         dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1721                         not_supported ^= MPB_ATTRIB_RAIDCNG;
1722                 }
1723                 if (not_supported & MPB_ATTRIB_BBM) {
1724                         dprintf("\t\tMPB_ATTRIB_BBM\n");
1725                 not_supported ^= MPB_ATTRIB_BBM;
1726                 }
1727                 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
1728                         dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1729                         not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
1730                 }
1731                 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
1732                         dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1733                         not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
1734                 }
1735                 if (not_supported & MPB_ATTRIB_2TB_DISK) {
1736                         dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1737                         not_supported ^= MPB_ATTRIB_2TB_DISK;
1738                 }
1739                 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
1740                         dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1741                         not_supported ^= MPB_ATTRIB_NEVER_USE2;
1742                 }
1743                 if (not_supported & MPB_ATTRIB_NEVER_USE) {
1744                         dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1745                         not_supported ^= MPB_ATTRIB_NEVER_USE;
1746                 }
1747
1748                 if (not_supported)
1749                         dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
1750
1751                 ret_val = 0;
1752         }
1753
1754         return ret_val;
1755 }
1756
1757 #ifndef MDASSEMBLE
1758 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
1759
1760 static void examine_super_imsm(struct supertype *st, char *homehost)
1761 {
1762         struct intel_super *super = st->sb;
1763         struct imsm_super *mpb = super->anchor;
1764         char str[MAX_SIGNATURE_LENGTH];
1765         int i;
1766         struct mdinfo info;
1767         char nbuf[64];
1768         __u32 sum;
1769         __u32 reserved = imsm_reserved_sectors(super, super->disks);
1770         struct dl *dl;
1771
1772         snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
1773         printf("          Magic : %s\n", str);
1774         snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
1775         printf("        Version : %s\n", get_imsm_version(mpb));
1776         printf("    Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
1777         printf("         Family : %08x\n", __le32_to_cpu(mpb->family_num));
1778         printf("     Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
1779         printf("     Attributes : ");
1780         if (imsm_check_attributes(mpb->attributes))
1781                 printf("All supported\n");
1782         else
1783                 printf("not supported\n");
1784         getinfo_super_imsm(st, &info, NULL);
1785         fname_from_uuid(st, &info, nbuf, ':');
1786         printf("           UUID : %s\n", nbuf + 5);
1787         sum = __le32_to_cpu(mpb->check_sum);
1788         printf("       Checksum : %08x %s\n", sum,
1789                 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
1790         printf("    MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
1791         printf("          Disks : %d\n", mpb->num_disks);
1792         printf("   RAID Devices : %d\n", mpb->num_raid_devs);
1793         print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), super->disks->index, reserved);
1794         if (get_imsm_bbm_log_size(super->bbm_log)) {
1795                 struct bbm_log *log = super->bbm_log;
1796
1797                 printf("\n");
1798                 printf("Bad Block Management Log:\n");
1799                 printf("       Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
1800                 printf("      Signature : %x\n", __le32_to_cpu(log->signature));
1801                 printf("    Entry Count : %d\n", __le32_to_cpu(log->entry_count));
1802         }
1803         for (i = 0; i < mpb->num_raid_devs; i++) {
1804                 struct mdinfo info;
1805                 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1806
1807                 super->current_vol = i;
1808                 getinfo_super_imsm(st, &info, NULL);
1809                 fname_from_uuid(st, &info, nbuf, ':');
1810                 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
1811         }
1812         for (i = 0; i < mpb->num_disks; i++) {
1813                 if (i == super->disks->index)
1814                         continue;
1815                 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved);
1816         }
1817
1818         for (dl = super->disks; dl; dl = dl->next)
1819                 if (dl->index == -1)
1820                         print_imsm_disk(&dl->disk, -1, reserved);
1821
1822         examine_migr_rec_imsm(super);
1823 }
1824
1825 static void brief_examine_super_imsm(struct supertype *st, int verbose)
1826 {
1827         /* We just write a generic IMSM ARRAY entry */
1828         struct mdinfo info;
1829         char nbuf[64];
1830         struct intel_super *super = st->sb;
1831
1832         if (!super->anchor->num_raid_devs) {
1833                 printf("ARRAY metadata=imsm\n");
1834                 return;
1835         }
1836
1837         getinfo_super_imsm(st, &info, NULL);
1838         fname_from_uuid(st, &info, nbuf, ':');
1839         printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
1840 }
1841
1842 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
1843 {
1844         /* We just write a generic IMSM ARRAY entry */
1845         struct mdinfo info;
1846         char nbuf[64];
1847         char nbuf1[64];
1848         struct intel_super *super = st->sb;
1849         int i;
1850
1851         if (!super->anchor->num_raid_devs)
1852                 return;
1853
1854         getinfo_super_imsm(st, &info, NULL);
1855         fname_from_uuid(st, &info, nbuf, ':');
1856         for (i = 0; i < super->anchor->num_raid_devs; i++) {
1857                 struct imsm_dev *dev = get_imsm_dev(super, i);
1858
1859                 super->current_vol = i;
1860                 getinfo_super_imsm(st, &info, NULL);
1861                 fname_from_uuid(st, &info, nbuf1, ':');
1862                 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1863                        dev->volume, nbuf + 5, i, nbuf1 + 5);
1864         }
1865 }
1866
1867 static void export_examine_super_imsm(struct supertype *st)
1868 {
1869         struct intel_super *super = st->sb;
1870         struct imsm_super *mpb = super->anchor;
1871         struct mdinfo info;
1872         char nbuf[64];
1873
1874         getinfo_super_imsm(st, &info, NULL);
1875         fname_from_uuid(st, &info, nbuf, ':');
1876         printf("MD_METADATA=imsm\n");
1877         printf("MD_LEVEL=container\n");
1878         printf("MD_UUID=%s\n", nbuf+5);
1879         printf("MD_DEVICES=%u\n", mpb->num_disks);
1880 }
1881
1882 static int copy_metadata_imsm(struct supertype *st, int from, int to)
1883 {
1884         /* The second last sector of the device contains
1885          * the "struct imsm_super" metadata.
1886          * This contains mpb_size which is the size in bytes of the
1887          * extended metadata.  This is located immediately before
1888          * the imsm_super.
1889          * We want to read all that, plus the last sector which
1890          * may contain a migration record, and write it all
1891          * to the target.
1892          */
1893         void *buf;
1894         unsigned long long dsize, offset;
1895         int sectors;
1896         struct imsm_super *sb;
1897         struct intel_super *super = st->sb;
1898         unsigned int sector_size = super->sector_size;
1899         unsigned int written = 0;
1900
1901         if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE) != 0)
1902                 return 1;
1903
1904         if (!get_dev_size(from, NULL, &dsize))
1905                 goto err;
1906
1907         if (lseek64(from, dsize-(2*sector_size), 0) < 0)
1908                 goto err;
1909         if (read(from, buf, sector_size) != sector_size)
1910                 goto err;
1911         sb = buf;
1912         if (strncmp((char*)sb->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0)
1913                 goto err;
1914
1915         sectors = mpb_sectors(sb, sector_size) + 2;
1916         offset = dsize - sectors * sector_size;
1917         if (lseek64(from, offset, 0) < 0 ||
1918             lseek64(to, offset, 0) < 0)
1919                 goto err;
1920         while (written < sectors * sector_size) {
1921                 int n = sectors*sector_size - written;
1922                 if (n > 4096)
1923                         n = 4096;
1924                 if (read(from, buf, n) != n)
1925                         goto err;
1926                 if (write(to, buf, n) != n)
1927                         goto err;
1928                 written += n;
1929         }
1930         free(buf);
1931         return 0;
1932 err:
1933         free(buf);
1934         return 1;
1935 }
1936
1937 static void detail_super_imsm(struct supertype *st, char *homehost)
1938 {
1939         struct mdinfo info;
1940         char nbuf[64];
1941
1942         getinfo_super_imsm(st, &info, NULL);
1943         fname_from_uuid(st, &info, nbuf, ':');
1944         printf("\n           UUID : %s\n", nbuf + 5);
1945 }
1946
1947 static void brief_detail_super_imsm(struct supertype *st)
1948 {
1949         struct mdinfo info;
1950         char nbuf[64];
1951         getinfo_super_imsm(st, &info, NULL);
1952         fname_from_uuid(st, &info, nbuf, ':');
1953         printf(" UUID=%s", nbuf + 5);
1954 }
1955
1956 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
1957 static void fd2devname(int fd, char *name);
1958
1959 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
1960 {
1961         /* dump an unsorted list of devices attached to AHCI Intel storage
1962          * controller, as well as non-connected ports
1963          */
1964         int hba_len = strlen(hba_path) + 1;
1965         struct dirent *ent;
1966         DIR *dir;
1967         char *path = NULL;
1968         int err = 0;
1969         unsigned long port_mask = (1 << port_count) - 1;
1970
1971         if (port_count > (int)sizeof(port_mask) * 8) {
1972                 if (verbose > 0)
1973                         pr_err("port_count %d out of range\n", port_count);
1974                 return 2;
1975         }
1976
1977         /* scroll through /sys/dev/block looking for devices attached to
1978          * this hba
1979          */
1980         dir = opendir("/sys/dev/block");
1981         if (!dir)
1982                 return 1;
1983
1984         for (ent = readdir(dir); ent; ent = readdir(dir)) {
1985                 int fd;
1986                 char model[64];
1987                 char vendor[64];
1988                 char buf[1024];
1989                 int major, minor;
1990                 char *device;
1991                 char *c;
1992                 int port;
1993                 int type;
1994
1995                 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
1996                         continue;
1997                 path = devt_to_devpath(makedev(major, minor));
1998                 if (!path)
1999                         continue;
2000                 if (!path_attached_to_hba(path, hba_path)) {
2001                         free(path);
2002                         path = NULL;
2003                         continue;
2004                 }
2005
2006                 /* retrieve the scsi device type */
2007                 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
2008                         if (verbose > 0)
2009                                 pr_err("failed to allocate 'device'\n");
2010                         err = 2;
2011                         break;
2012                 }
2013                 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
2014                 if (load_sys(device, buf, sizeof(buf)) != 0) {
2015                         if (verbose > 0)
2016                                 pr_err("failed to read device type for %s\n",
2017                                         path);
2018                         err = 2;
2019                         free(device);
2020                         break;
2021                 }
2022                 type = strtoul(buf, NULL, 10);
2023
2024                 /* if it's not a disk print the vendor and model */
2025                 if (!(type == 0 || type == 7 || type == 14)) {
2026                         vendor[0] = '\0';
2027                         model[0] = '\0';
2028                         sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
2029                         if (load_sys(device, buf, sizeof(buf)) == 0) {
2030                                 strncpy(vendor, buf, sizeof(vendor));
2031                                 vendor[sizeof(vendor) - 1] = '\0';
2032                                 c = (char *) &vendor[sizeof(vendor) - 1];
2033                                 while (isspace(*c) || *c == '\0')
2034                                         *c-- = '\0';
2035
2036                         }
2037                         sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
2038                         if (load_sys(device, buf, sizeof(buf)) == 0) {
2039                                 strncpy(model, buf, sizeof(model));
2040                                 model[sizeof(model) - 1] = '\0';
2041                                 c = (char *) &model[sizeof(model) - 1];
2042                                 while (isspace(*c) || *c == '\0')
2043                                         *c-- = '\0';
2044                         }
2045
2046                         if (vendor[0] && model[0])
2047                                 sprintf(buf, "%.64s %.64s", vendor, model);
2048                         else
2049                                 switch (type) { /* numbers from hald/linux/device.c */
2050                                 case 1: sprintf(buf, "tape"); break;
2051                                 case 2: sprintf(buf, "printer"); break;
2052                                 case 3: sprintf(buf, "processor"); break;
2053                                 case 4:
2054                                 case 5: sprintf(buf, "cdrom"); break;
2055                                 case 6: sprintf(buf, "scanner"); break;
2056                                 case 8: sprintf(buf, "media_changer"); break;
2057                                 case 9: sprintf(buf, "comm"); break;
2058                                 case 12: sprintf(buf, "raid"); break;
2059                                 default: sprintf(buf, "unknown");
2060                                 }
2061                 } else
2062                         buf[0] = '\0';
2063                 free(device);
2064
2065                 /* chop device path to 'host%d' and calculate the port number */
2066                 c = strchr(&path[hba_len], '/');
2067                 if (!c) {
2068                         if (verbose > 0)
2069                                 pr_err("%s - invalid path name\n", path + hba_len);
2070                         err = 2;
2071                         break;
2072                 }
2073                 *c = '\0';
2074                 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2075                    ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2076                         port -= host_base;
2077                 else {
2078                         if (verbose > 0) {
2079                                 *c = '/'; /* repair the full string */
2080                                 pr_err("failed to determine port number for %s\n",
2081                                         path);
2082                         }
2083                         err = 2;
2084                         break;
2085                 }
2086
2087                 /* mark this port as used */
2088                 port_mask &= ~(1 << port);
2089
2090                 /* print out the device information */
2091                 if (buf[0]) {
2092                         printf("          Port%d : - non-disk device (%s) -\n", port, buf);
2093                         continue;
2094                 }
2095
2096                 fd = dev_open(ent->d_name, O_RDONLY);
2097                 if (fd < 0)
2098                         printf("          Port%d : - disk info unavailable -\n", port);
2099                 else {
2100                         fd2devname(fd, buf);
2101                         printf("          Port%d : %s", port, buf);
2102                         if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
2103                                 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf);
2104                         else
2105                                 printf(" ()\n");
2106                         close(fd);
2107                 }
2108                 free(path);
2109                 path = NULL;
2110         }
2111         if (path)
2112                 free(path);
2113         if (dir)
2114                 closedir(dir);
2115         if (err == 0) {
2116                 int i;
2117
2118                 for (i = 0; i < port_count; i++)
2119                         if (port_mask & (1 << i))
2120                                 printf("          Port%d : - no device attached -\n", i);
2121         }
2122
2123         return err;
2124 }
2125
2126 static int print_vmd_attached_devs(struct sys_dev *hba)
2127 {
2128         struct dirent *ent;
2129         DIR *dir;
2130         char path[292];
2131         char link[256];
2132         char *c, *rp;
2133
2134         if (hba->type != SYS_DEV_VMD)
2135                 return 1;
2136
2137         /* scroll through /sys/dev/block looking for devices attached to
2138          * this hba
2139          */
2140         dir = opendir("/sys/bus/pci/drivers/nvme");
2141         if (!dir)
2142                 return 1;
2143
2144         for (ent = readdir(dir); ent; ent = readdir(dir)) {
2145                 int n;
2146
2147                 /* is 'ent' a device? check that the 'subsystem' link exists and
2148                  * that its target matches 'bus'
2149                  */
2150                 sprintf(path, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2151                         ent->d_name);
2152                 n = readlink(path, link, sizeof(link));
2153                 if (n < 0 || n >= (int)sizeof(link))
2154                         continue;
2155                 link[n] = '\0';
2156                 c = strrchr(link, '/');
2157                 if (!c)
2158                         continue;
2159                 if (strncmp("pci", c+1, strlen("pci")) != 0)
2160                         continue;
2161
2162                 sprintf(path, "/sys/bus/pci/drivers/nvme/%s", ent->d_name);
2163                 /* if not a intel NVMe - skip it*/
2164                 if (devpath_to_vendor(path) != 0x8086)
2165                         continue;
2166
2167                 rp = realpath(path, NULL);
2168                 if (!rp)
2169                         continue;
2170
2171                 if (path_attached_to_hba(rp, hba->path)) {
2172                         printf(" NVMe under VMD : %s\n", rp);
2173                 }
2174                 free(rp);
2175         }
2176
2177         closedir(dir);
2178         return 0;
2179 }
2180
2181 static void print_found_intel_controllers(struct sys_dev *elem)
2182 {
2183         for (; elem; elem = elem->next) {
2184                 pr_err("found Intel(R) ");
2185                 if (elem->type == SYS_DEV_SATA)
2186                         fprintf(stderr, "SATA ");
2187                 else if (elem->type == SYS_DEV_SAS)
2188                         fprintf(stderr, "SAS ");
2189                 else if (elem->type == SYS_DEV_NVME)
2190                         fprintf(stderr, "NVMe ");
2191
2192                 if (elem->type == SYS_DEV_VMD)
2193                         fprintf(stderr, "VMD domain");
2194                 else
2195                         fprintf(stderr, "RAID controller");
2196
2197                 if (elem->pci_id)
2198                         fprintf(stderr, " at %s", elem->pci_id);
2199                 fprintf(stderr, ".\n");
2200         }
2201         fflush(stderr);
2202 }
2203
2204 static int ahci_get_port_count(const char *hba_path, int *port_count)
2205 {
2206         struct dirent *ent;
2207         DIR *dir;
2208         int host_base = -1;
2209
2210         *port_count = 0;
2211         if ((dir = opendir(hba_path)) == NULL)
2212                 return -1;
2213
2214         for (ent = readdir(dir); ent; ent = readdir(dir)) {
2215                 int host;
2216
2217                 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2218                    ((sscanf(ent->d_name, "host%d", &host) != 1)))
2219                         continue;
2220                 if (*port_count == 0)
2221                         host_base = host;
2222                 else if (host < host_base)
2223                         host_base = host;
2224
2225                 if (host + 1 > *port_count + host_base)
2226                         *port_count = host + 1 - host_base;
2227         }
2228         closedir(dir);
2229         return host_base;
2230 }
2231
2232 static void print_imsm_capability(const struct imsm_orom *orom)
2233 {
2234         printf("       Platform : Intel(R) ");
2235         if (orom->capabilities == 0 && orom->driver_features == 0)
2236                 printf("Matrix Storage Manager\n");
2237         else
2238                 printf("Rapid Storage Technology%s\n",
2239                         imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2240         if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2241                 printf("        Version : %d.%d.%d.%d\n", orom->major_ver,
2242                                 orom->minor_ver, orom->hotfix_ver, orom->build);
2243         printf("    RAID Levels :%s%s%s%s%s\n",
2244                imsm_orom_has_raid0(orom) ? " raid0" : "",
2245                imsm_orom_has_raid1(orom) ? " raid1" : "",
2246                imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2247                imsm_orom_has_raid10(orom) ? " raid10" : "",
2248                imsm_orom_has_raid5(orom) ? " raid5" : "");
2249         printf("    Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2250                imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2251                imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2252                imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2253                imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2254                imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2255                imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2256                imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2257                imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2258                imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2259                imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2260                imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2261                imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2262                imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2263                imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2264                imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2265                imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2266         printf("    2TB volumes :%s supported\n",
2267                (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2268         printf("      2TB disks :%s supported\n",
2269                (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2270         printf("      Max Disks : %d\n", orom->tds);
2271         printf("    Max Volumes : %d per array, %d per %s\n",
2272                orom->vpa, orom->vphba,
2273                imsm_orom_is_nvme(orom) ? "platform" : "controller");
2274         return;
2275 }
2276
2277 static void print_imsm_capability_export(const struct imsm_orom *orom)
2278 {
2279         printf("MD_FIRMWARE_TYPE=imsm\n");
2280         if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2281                 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2282                                 orom->hotfix_ver, orom->build);
2283         printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2284                         imsm_orom_has_raid0(orom) ? "raid0 " : "",
2285                         imsm_orom_has_raid1(orom) ? "raid1 " : "",
2286                         imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2287                         imsm_orom_has_raid5(orom) ? "raid10 " : "",
2288                         imsm_orom_has_raid10(orom) ? "raid5 " : "");
2289         printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2290                         imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2291                         imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2292                         imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2293                         imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2294                         imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2295                         imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2296                         imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2297                         imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2298                         imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2299                         imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2300                         imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2301                         imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2302                         imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2303                         imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2304                         imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2305                         imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2306         printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2307         printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2308         printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2309         printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2310         printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2311 }
2312
2313 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2314 {
2315         /* There are two components to imsm platform support, the ahci SATA
2316          * controller and the option-rom.  To find the SATA controller we
2317          * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2318          * controller with the Intel vendor id is present.  This approach
2319          * allows mdadm to leverage the kernel's ahci detection logic, with the
2320          * caveat that if ahci.ko is not loaded mdadm will not be able to
2321          * detect platform raid capabilities.  The option-rom resides in a
2322          * platform "Adapter ROM".  We scan for its signature to retrieve the
2323          * platform capabilities.  If raid support is disabled in the BIOS the
2324          * option-rom capability structure will not be available.
2325          */
2326         struct sys_dev *list, *hba;
2327         int host_base = 0;
2328         int port_count = 0;
2329         int result=1;
2330
2331         if (enumerate_only) {
2332                 if (check_env("IMSM_NO_PLATFORM"))
2333                         return 0;
2334                 list = find_intel_devices();
2335                 if (!list)
2336                         return 2;
2337                 for (hba = list; hba; hba = hba->next) {
2338                         if (find_imsm_capability(hba)) {
2339                                 result = 0;
2340                                 break;
2341                         }
2342                         else
2343                                 result = 2;
2344                 }
2345                 return result;
2346         }
2347
2348         list = find_intel_devices();
2349         if (!list) {
2350                 if (verbose > 0)
2351                         pr_err("no active Intel(R) RAID controller found.\n");
2352                 return 2;
2353         } else if (verbose > 0)
2354                 print_found_intel_controllers(list);
2355
2356         for (hba = list; hba; hba = hba->next) {
2357                 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2358                         continue;
2359                 if (!find_imsm_capability(hba)) {
2360                         char buf[PATH_MAX];
2361                         pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2362                                   hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path,
2363                                   get_sys_dev_type(hba->type));
2364                         continue;
2365                 }
2366                 result = 0;
2367         }
2368
2369         if (controller_path && result == 1) {
2370                 pr_err("no active Intel(R) RAID controller found under %s\n",
2371                                 controller_path);
2372                 return result;
2373         }
2374
2375         const struct orom_entry *entry;
2376
2377         for (entry = orom_entries; entry; entry = entry->next) {
2378                 if (entry->type == SYS_DEV_VMD) {
2379                         print_imsm_capability(&entry->orom);
2380                         for (hba = list; hba; hba = hba->next) {
2381                                 if (hba->type == SYS_DEV_VMD) {
2382                                         char buf[PATH_MAX];
2383                                         printf(" I/O Controller : %s (%s)\n",
2384                                                 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2385                                         if (print_vmd_attached_devs(hba)) {
2386                                                 if (verbose > 0)
2387                                                         pr_err("failed to get devices attached to VMD domain.\n");
2388                                                 result |= 2;
2389                                         }
2390                                 }
2391                         }
2392                         printf("\n");
2393                         continue;
2394                 }
2395
2396                 print_imsm_capability(&entry->orom);
2397                 if (entry->type == SYS_DEV_NVME) {
2398                         for (hba = list; hba; hba = hba->next) {
2399                                 if (hba->type == SYS_DEV_NVME)
2400                                         printf("    NVMe Device : %s\n", hba->path);
2401                         }
2402                         printf("\n");
2403                         continue;
2404                 }
2405
2406                 struct devid_list *devid;
2407                 for (devid = entry->devid_list; devid; devid = devid->next) {
2408                         hba = device_by_id(devid->devid);
2409                         if (!hba)
2410                                 continue;
2411
2412                         printf(" I/O Controller : %s (%s)\n",
2413                                 hba->path, get_sys_dev_type(hba->type));
2414                         if (hba->type == SYS_DEV_SATA) {
2415                                 host_base = ahci_get_port_count(hba->path, &port_count);
2416                                 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2417                                         if (verbose > 0)
2418                                                 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba->pci_id);
2419                                         result |= 2;
2420                                 }
2421                         }
2422                 }
2423                 printf("\n");
2424         }
2425
2426         return result;
2427 }
2428
2429 static int export_detail_platform_imsm(int verbose, char *controller_path)
2430 {
2431         struct sys_dev *list, *hba;
2432         int result=1;
2433
2434         list = find_intel_devices();
2435         if (!list) {
2436                 if (verbose > 0)
2437                         pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2438                 result = 2;
2439                 return result;
2440         }
2441
2442         for (hba = list; hba; hba = hba->next) {
2443                 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2444                         continue;
2445                 if (!find_imsm_capability(hba) && verbose > 0) {
2446                         char buf[PATH_MAX];
2447                         pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2448                         hba->type == SYS_DEV_VMD ? vmd_domain_to_controller(hba, buf) : hba->path);
2449                 }
2450                 else
2451                         result = 0;
2452         }
2453
2454         const struct orom_entry *entry;
2455
2456         for (entry = orom_entries; entry; entry = entry->next) {
2457                 if (entry->type == SYS_DEV_VMD) {
2458                         for (hba = list; hba; hba = hba->next)
2459                                 print_imsm_capability_export(&entry->orom);
2460                         continue;
2461                 }
2462                 print_imsm_capability_export(&entry->orom);
2463         }
2464
2465         return result;
2466 }
2467
2468 #endif
2469
2470 static int match_home_imsm(struct supertype *st, char *homehost)
2471 {
2472         /* the imsm metadata format does not specify any host
2473          * identification information.  We return -1 since we can never
2474          * confirm nor deny whether a given array is "meant" for this
2475          * host.  We rely on compare_super and the 'family_num' fields to
2476          * exclude member disks that do not belong, and we rely on
2477          * mdadm.conf to specify the arrays that should be assembled.
2478          * Auto-assembly may still pick up "foreign" arrays.
2479          */
2480
2481         return -1;
2482 }
2483
2484 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2485 {
2486         /* The uuid returned here is used for:
2487          *  uuid to put into bitmap file (Create, Grow)
2488          *  uuid for backup header when saving critical section (Grow)
2489          *  comparing uuids when re-adding a device into an array
2490          *    In these cases the uuid required is that of the data-array,
2491          *    not the device-set.
2492          *  uuid to recognise same set when adding a missing device back
2493          *    to an array.   This is a uuid for the device-set.
2494          *
2495          * For each of these we can make do with a truncated
2496          * or hashed uuid rather than the original, as long as
2497          * everyone agrees.
2498          * In each case the uuid required is that of the data-array,
2499          * not the device-set.
2500          */
2501         /* imsm does not track uuid's so we synthesis one using sha1 on
2502          * - The signature (Which is constant for all imsm array, but no matter)
2503          * - the orig_family_num of the container
2504          * - the index number of the volume
2505          * - the 'serial' number of the volume.
2506          * Hopefully these are all constant.
2507          */
2508         struct intel_super *super = st->sb;
2509
2510         char buf[20];
2511         struct sha1_ctx ctx;
2512         struct imsm_dev *dev = NULL;
2513         __u32 family_num;
2514
2515         /* some mdadm versions failed to set ->orig_family_num, in which
2516          * case fall back to ->family_num.  orig_family_num will be
2517          * fixed up with the first metadata update.
2518          */
2519         family_num = super->anchor->orig_family_num;
2520         if (family_num == 0)
2521                 family_num = super->anchor->family_num;
2522         sha1_init_ctx(&ctx);
2523         sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2524         sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2525         if (super->current_vol >= 0)
2526                 dev = get_imsm_dev(super, super->current_vol);
2527         if (dev) {
2528                 __u32 vol = super->current_vol;
2529                 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2530                 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2531         }
2532         sha1_finish_ctx(&ctx, buf);
2533         memcpy(uuid, buf, 4*4);
2534 }
2535
2536 #if 0
2537 static void
2538 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
2539 {
2540         __u8 *v = get_imsm_version(mpb);
2541         __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
2542         char major[] = { 0, 0, 0 };
2543         char minor[] = { 0 ,0, 0 };
2544         char patch[] = { 0, 0, 0 };
2545         char *ver_parse[] = { major, minor, patch };
2546         int i, j;
2547
2548         i = j = 0;
2549         while (*v != '\0' && v < end) {
2550                 if (*v != '.' && j < 2)
2551                         ver_parse[i][j++] = *v;
2552                 else {
2553                         i++;
2554                         j = 0;
2555                 }
2556                 v++;
2557         }
2558
2559         *m = strtol(minor, NULL, 0);
2560         *p = strtol(patch, NULL, 0);
2561 }
2562 #endif
2563
2564 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2565 {
2566         /* migr_strip_size when repairing or initializing parity */
2567         struct imsm_map *map = get_imsm_map(dev, MAP_0);
2568         __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2569
2570         switch (get_imsm_raid_level(map)) {
2571         case 5:
2572         case 10:
2573                 return chunk;
2574         default:
2575                 return 128*1024 >> 9;
2576         }
2577 }
2578
2579 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2580 {
2581         /* migr_strip_size when rebuilding a degraded disk, no idea why
2582          * this is different than migr_strip_size_resync(), but it's good
2583          * to be compatible
2584          */
2585         struct imsm_map *map = get_imsm_map(dev, MAP_1);
2586         __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2587
2588         switch (get_imsm_raid_level(map)) {
2589         case 1:
2590         case 10:
2591                 if (map->num_members % map->num_domains == 0)
2592                         return 128*1024 >> 9;
2593                 else
2594                         return chunk;
2595         case 5:
2596                 return max((__u32) 64*1024 >> 9, chunk);
2597         default:
2598                 return 128*1024 >> 9;
2599         }
2600 }
2601
2602 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2603 {
2604         struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2605         struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2606         __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2607         __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2608
2609         return max((__u32) 1, hi_chunk / lo_chunk);
2610 }
2611
2612 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2613 {
2614         struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2615         int level = get_imsm_raid_level(lo);
2616
2617         if (level == 1 || level == 10) {
2618                 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2619
2620                 return hi->num_domains;
2621         } else
2622                 return num_stripes_per_unit_resync(dev);
2623 }
2624
2625 static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map)
2626 {
2627         /* named 'imsm_' because raid0, raid1 and raid10
2628          * counter-intuitively have the same number of data disks
2629          */
2630         struct imsm_map *map = get_imsm_map(dev, second_map);
2631
2632         switch (get_imsm_raid_level(map)) {
2633         case 0:
2634                 return map->num_members;
2635                 break;
2636         case 1:
2637         case 10:
2638                 return map->num_members/2;
2639         case 5:
2640                 return map->num_members - 1;
2641         default:
2642                 dprintf("unsupported raid level\n");
2643                 return 0;
2644         }
2645 }
2646
2647 static __u32 parity_segment_depth(struct imsm_dev *dev)
2648 {
2649         struct imsm_map *map = get_imsm_map(dev, MAP_0);
2650         __u32 chunk =  __le32_to_cpu(map->blocks_per_strip);
2651
2652         switch(get_imsm_raid_level(map)) {
2653         case 1:
2654         case 10:
2655                 return chunk * map->num_domains;
2656         case 5:
2657                 return chunk * map->num_members;
2658         default:
2659                 return chunk;
2660         }
2661 }
2662
2663 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
2664 {
2665         struct imsm_map *map = get_imsm_map(dev, MAP_1);
2666         __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2667         __u32 strip = block / chunk;
2668
2669         switch (get_imsm_raid_level(map)) {
2670         case 1:
2671         case 10: {
2672                 __u32 vol_strip = (strip * map->num_domains) + 1;
2673                 __u32 vol_stripe = vol_strip / map->num_members;
2674
2675                 return vol_stripe * chunk + block % chunk;
2676         } case 5: {
2677                 __u32 stripe = strip / (map->num_members - 1);
2678
2679                 return stripe * chunk + block % chunk;
2680         }
2681         default:
2682                 return 0;
2683         }
2684 }
2685
2686 static __u64 blocks_per_migr_unit(struct intel_super *super,
2687                                   struct imsm_dev *dev)
2688 {
2689         /* calculate the conversion factor between per member 'blocks'
2690          * (md/{resync,rebuild}_start) and imsm migration units, return
2691          * 0 for the 'not migrating' and 'unsupported migration' cases
2692          */
2693         if (!dev->vol.migr_state)
2694                 return 0;
2695
2696         switch (migr_type(dev)) {
2697         case MIGR_GEN_MIGR: {
2698                 struct migr_record *migr_rec = super->migr_rec;
2699                 return __le32_to_cpu(migr_rec->blocks_per_unit);
2700         }
2701         case MIGR_VERIFY:
2702         case MIGR_REPAIR:
2703         case MIGR_INIT: {
2704                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2705                 __u32 stripes_per_unit;
2706                 __u32 blocks_per_unit;
2707                 __u32 parity_depth;
2708                 __u32 migr_chunk;
2709                 __u32 block_map;
2710                 __u32 block_rel;
2711                 __u32 segment;
2712                 __u32 stripe;
2713                 __u8  disks;
2714
2715                 /* yes, this is really the translation of migr_units to
2716                  * per-member blocks in the 'resync' case
2717                  */
2718                 stripes_per_unit = num_stripes_per_unit_resync(dev);
2719                 migr_chunk = migr_strip_blocks_resync(dev);
2720                 disks = imsm_num_data_members(dev, MAP_0);
2721                 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
2722                 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
2723                 segment = blocks_per_unit / stripe;
2724                 block_rel = blocks_per_unit - segment * stripe;
2725                 parity_depth = parity_segment_depth(dev);
2726                 block_map = map_migr_block(dev, block_rel);
2727                 return block_map + parity_depth * segment;
2728         }
2729         case MIGR_REBUILD: {
2730                 __u32 stripes_per_unit;
2731                 __u32 migr_chunk;
2732
2733                 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
2734                 migr_chunk = migr_strip_blocks_rebuild(dev);
2735                 return migr_chunk * stripes_per_unit;
2736         }
2737         case MIGR_STATE_CHANGE:
2738         default:
2739                 return 0;
2740         }
2741 }
2742
2743 static int imsm_level_to_layout(int level)
2744 {
2745         switch (level) {
2746         case 0:
2747         case 1:
2748                 return 0;
2749         case 5:
2750         case 6:
2751                 return ALGORITHM_LEFT_ASYMMETRIC;
2752         case 10:
2753                 return 0x102;
2754         }
2755         return UnSet;
2756 }
2757
2758 /*******************************************************************************
2759  * Function:    read_imsm_migr_rec
2760  * Description: Function reads imsm migration record from last sector of disk
2761  * Parameters:
2762  *      fd      : disk descriptor
2763  *      super   : metadata info
2764  * Returns:
2765  *       0 : success,
2766  *      -1 : fail
2767  ******************************************************************************/
2768 static int read_imsm_migr_rec(int fd, struct intel_super *super)
2769 {
2770         int ret_val = -1;
2771         unsigned int sector_size = super->sector_size;
2772         unsigned long long dsize;
2773
2774         get_dev_size(fd, NULL, &dsize);
2775         if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
2776                    SEEK_SET) < 0) {
2777                 pr_err("Cannot seek to anchor block: %s\n",
2778                        strerror(errno));
2779                 goto out;
2780         }
2781         if (read(fd, super->migr_rec_buf,
2782             MIGR_REC_BUF_SECTORS*sector_size) !=
2783             MIGR_REC_BUF_SECTORS*sector_size) {
2784                 pr_err("Cannot read migr record block: %s\n",
2785                        strerror(errno));
2786                 goto out;
2787         }
2788         ret_val = 0;
2789         if (sector_size == 4096)
2790                 convert_from_4k_imsm_migr_rec(super);
2791
2792 out:
2793         return ret_val;
2794 }
2795
2796 static struct imsm_dev *imsm_get_device_during_migration(
2797         struct intel_super *super)
2798 {
2799
2800         struct intel_dev *dv;
2801
2802         for (dv = super->devlist; dv; dv = dv->next) {
2803                 if (is_gen_migration(dv->dev))
2804                         return dv->dev;
2805         }
2806         return NULL;
2807 }
2808
2809 /*******************************************************************************
2810  * Function:    load_imsm_migr_rec
2811  * Description: Function reads imsm migration record (it is stored at the last
2812  *              sector of disk)
2813  * Parameters:
2814  *      super   : imsm internal array info
2815  *      info    : general array info
2816  * Returns:
2817  *       0 : success
2818  *      -1 : fail
2819  *      -2 : no migration in progress
2820  ******************************************************************************/
2821 static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info)
2822 {
2823         struct mdinfo *sd;
2824         struct dl *dl;
2825         char nm[30];
2826         int retval = -1;
2827         int fd = -1;
2828         struct imsm_dev *dev;
2829         struct imsm_map *map;
2830         int slot = -1;
2831
2832         /* find map under migration */
2833         dev = imsm_get_device_during_migration(super);
2834         /* nothing to load,no migration in progress?
2835         */
2836         if (dev == NULL)
2837                 return -2;
2838
2839         if (info) {
2840                 for (sd = info->devs ; sd ; sd = sd->next) {
2841                         /* read only from one of the first two slots */
2842                         if ((sd->disk.raid_disk < 0) ||
2843                             (sd->disk.raid_disk > 1))
2844                                 continue;
2845
2846                         sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2847                         fd = dev_open(nm, O_RDONLY);
2848                         if (fd >= 0)
2849                                 break;
2850                 }
2851         }
2852         if (fd < 0) {
2853                 map = get_imsm_map(dev, MAP_0);
2854                 for (dl = super->disks; dl; dl = dl->next) {
2855                         /* skip spare and failed disks
2856                         */
2857                         if (dl->index < 0)
2858                                 continue;
2859                         /* read only from one of the first two slots */
2860                         if (map)
2861                                 slot = get_imsm_disk_slot(map, dl->index);
2862                         if (map == NULL || slot > 1 || slot < 0)
2863                                 continue;
2864                         sprintf(nm, "%d:%d", dl->major, dl->minor);
2865                         fd = dev_open(nm, O_RDONLY);
2866                         if (fd >= 0)
2867                                 break;
2868                 }
2869         }
2870         if (fd < 0)
2871                 goto out;
2872         retval = read_imsm_migr_rec(fd, super);
2873
2874 out:
2875         if (fd >= 0)
2876                 close(fd);
2877         return retval;
2878 }
2879
2880 #ifndef MDASSEMBLE
2881 /*******************************************************************************
2882  * function: imsm_create_metadata_checkpoint_update
2883  * Description: It creates update for checkpoint change.
2884  * Parameters:
2885  *      super   : imsm internal array info
2886  *      u       : pointer to prepared update
2887  * Returns:
2888  *      Uptate length.
2889  *      If length is equal to 0, input pointer u contains no update
2890  ******************************************************************************/
2891 static int imsm_create_metadata_checkpoint_update(
2892         struct intel_super *super,
2893         struct imsm_update_general_migration_checkpoint **u)
2894 {
2895
2896         int update_memory_size = 0;
2897
2898         dprintf("(enter)\n");
2899
2900         if (u == NULL)
2901                 return 0;
2902         *u = NULL;
2903
2904         /* size of all update data without anchor */
2905         update_memory_size =
2906                 sizeof(struct imsm_update_general_migration_checkpoint);
2907
2908         *u = xcalloc(1, update_memory_size);
2909         if (*u == NULL) {
2910                 dprintf("error: cannot get memory\n");
2911                 return 0;
2912         }
2913         (*u)->type = update_general_migration_checkpoint;
2914         (*u)->curr_migr_unit = __le32_to_cpu(super->migr_rec->curr_migr_unit);
2915         dprintf("prepared for %u\n", (*u)->curr_migr_unit);
2916
2917         return update_memory_size;
2918 }
2919
2920 static void imsm_update_metadata_locally(struct supertype *st,
2921                                          void *buf, int len);
2922
2923 /*******************************************************************************
2924  * Function:    write_imsm_migr_rec
2925  * Description: Function writes imsm migration record
2926  *              (at the last sector of disk)
2927  * Parameters:
2928  *      super   : imsm internal array info
2929  * Returns:
2930  *       0 : success
2931  *      -1 : if fail
2932  ******************************************************************************/
2933 static int write_imsm_migr_rec(struct supertype *st)
2934 {
2935         struct intel_super *super = st->sb;
2936         unsigned int sector_size = super->sector_size;
2937         unsigned long long dsize;
2938         char nm[30];
2939         int fd = -1;
2940         int retval = -1;
2941         struct dl *sd;
2942         int len;
2943         struct imsm_update_general_migration_checkpoint *u;
2944         struct imsm_dev *dev;
2945         struct imsm_map *map;
2946
2947         /* find map under migration */
2948         dev = imsm_get_device_during_migration(super);
2949         /* if no migration, write buffer anyway to clear migr_record
2950          * on disk based on first available device
2951         */
2952         if (dev == NULL)
2953                 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
2954                                           super->current_vol);
2955
2956         map = get_imsm_map(dev, MAP_0);
2957
2958         if (sector_size == 4096)
2959                 convert_to_4k_imsm_migr_rec(super);
2960         for (sd = super->disks ; sd ; sd = sd->next) {
2961                 int slot = -1;
2962
2963                 /* skip failed and spare devices */
2964                 if (sd->index < 0)
2965                         continue;
2966                 /* write to 2 first slots only */
2967                 if (map)
2968                         slot = get_imsm_disk_slot(map, sd->index);
2969                 if (map == NULL || slot > 1 || slot < 0)
2970                         continue;
2971
2972                 sprintf(nm, "%d:%d", sd->major, sd->minor);
2973                 fd = dev_open(nm, O_RDWR);
2974                 if (fd < 0)
2975                         continue;
2976                 get_dev_size(fd, NULL, &dsize);
2977                 if (lseek64(fd, dsize - (MIGR_REC_SECTOR_POSITION*sector_size),
2978                     SEEK_SET) < 0) {
2979                         pr_err("Cannot seek to anchor block: %s\n",
2980                                strerror(errno));
2981                         goto out;
2982                 }
2983                 if (write(fd, super->migr_rec_buf,
2984                     MIGR_REC_BUF_SECTORS*sector_size) !=
2985                     MIGR_REC_BUF_SECTORS*sector_size) {
2986                         pr_err("Cannot write migr record block: %s\n",
2987                                strerror(errno));
2988                         goto out;
2989                 }
2990                 close(fd);
2991                 fd = -1;
2992         }
2993         if (sector_size == 4096)
2994                 convert_from_4k_imsm_migr_rec(super);
2995         /* update checkpoint information in metadata */
2996         len = imsm_create_metadata_checkpoint_update(super, &u);
2997         if (len <= 0) {
2998                 dprintf("imsm: Cannot prepare update\n");
2999                 goto out;
3000         }
3001         /* update metadata locally */
3002         imsm_update_metadata_locally(st, u, len);
3003         /* and possibly remotely */
3004         if (st->update_tail) {
3005                 append_metadata_update(st, u, len);
3006                 /* during reshape we do all work inside metadata handler
3007                  * manage_reshape(), so metadata update has to be triggered
3008                  * insida it
3009                  */
3010                 flush_metadata_updates(st);
3011                 st->update_tail = &st->updates;
3012         } else
3013                 free(u);
3014
3015         retval = 0;
3016  out:
3017         if (fd >= 0)
3018                 close(fd);
3019         return retval;
3020 }
3021 #endif /* MDASSEMBLE */
3022
3023 /* spare/missing disks activations are not allowe when
3024  * array/container performs reshape operation, because
3025  * all arrays in container works on the same disks set
3026  */
3027 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3028 {
3029         int rv = 0;
3030         struct intel_dev *i_dev;
3031         struct imsm_dev *dev;
3032
3033         /* check whole container
3034          */
3035         for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3036                 dev = i_dev->dev;
3037                 if (is_gen_migration(dev)) {
3038                         /* No repair during any migration in container
3039                          */
3040                         rv = 1;
3041                         break;
3042                 }
3043         }
3044         return rv;
3045 }
3046 static unsigned long long imsm_component_size_aligment_check(int level,
3047                                               int chunk_size,
3048                                               unsigned int sector_size,
3049                                               unsigned long long component_size)
3050 {
3051         unsigned int component_size_alligment;
3052
3053         /* check component size aligment
3054         */
3055         component_size_alligment = component_size % (chunk_size/sector_size);
3056
3057         dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
3058                 level, chunk_size, component_size,
3059                 component_size_alligment);
3060
3061         if (component_size_alligment && (level != 1) && (level != UnSet)) {
3062                 dprintf("imsm: reported component size alligned from %llu ",
3063                         component_size);
3064                 component_size -= component_size_alligment;
3065                 dprintf_cont("to %llu (%i).\n",
3066                         component_size, component_size_alligment);
3067         }
3068
3069         return component_size;
3070 }
3071
3072 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3073 {
3074         struct intel_super *super = st->sb;
3075         struct migr_record *migr_rec = super->migr_rec;
3076         struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3077         struct imsm_map *map = get_imsm_map(dev, MAP_0);
3078         struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3079         struct imsm_map *map_to_analyse = map;
3080         struct dl *dl;
3081         int map_disks = info->array.raid_disks;
3082
3083         memset(info, 0, sizeof(*info));
3084         if (prev_map)
3085                 map_to_analyse = prev_map;
3086
3087         dl = super->current_disk;
3088
3089         info->container_member    = super->current_vol;
3090         info->array.raid_disks    = map->num_members;
3091         info->array.level         = get_imsm_raid_level(map_to_analyse);
3092         info->array.layout        = imsm_level_to_layout(info->array.level);
3093         info->array.md_minor      = -1;
3094         info->array.ctime         = 0;
3095         info->array.utime         = 0;
3096         info->array.chunk_size    =
3097                 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3098         info->array.state         = !dev->vol.dirty;
3099         info->custom_array_size   = __le32_to_cpu(dev->size_high);
3100         info->custom_array_size   <<= 32;
3101         info->custom_array_size   |= __le32_to_cpu(dev->size_low);
3102         info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3103
3104         if (is_gen_migration(dev)) {
3105                 info->reshape_active = 1;
3106                 info->new_level = get_imsm_raid_level(map);
3107                 info->new_layout = imsm_level_to_layout(info->new_level);
3108                 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3109                 info->delta_disks = map->num_members - prev_map->num_members;
3110                 if (info->delta_disks) {
3111                         /* this needs to be applied to every array
3112                          * in the container.
3113                          */
3114                         info->reshape_active = CONTAINER_RESHAPE;
3115                 }
3116                 /* We shape information that we give to md might have to be
3117                  * modify to cope with md's requirement for reshaping arrays.
3118                  * For example, when reshaping a RAID0, md requires it to be
3119                  * presented as a degraded RAID4.
3120                  * Also if a RAID0 is migrating to a RAID5 we need to specify
3121                  * the array as already being RAID5, but the 'before' layout
3122                  * is a RAID4-like layout.
3123                  */
3124                 switch (info->array.level) {
3125                 case 0:
3126                         switch(info->new_level) {
3127                         case 0:
3128                                 /* conversion is happening as RAID4 */
3129                                 info->array.level = 4;
3130                                 info->array.raid_disks += 1;
3131                                 break;
3132                         case 5:
3133                                 /* conversion is happening as RAID5 */
3134                                 info->array.level = 5;
3135                                 info->array.layout = ALGORITHM_PARITY_N;
3136                                 info->delta_disks -= 1;
3137                                 break;
3138                         default:
3139                                 /* FIXME error message */
3140                                 info->array.level = UnSet;
3141                                 break;
3142                         }
3143                         break;
3144                 }
3145         } else {
3146                 info->new_level = UnSet;
3147                 info->new_layout = UnSet;
3148                 info->new_chunk = info->array.chunk_size;
3149                 info->delta_disks = 0;
3150         }
3151
3152         if (dl) {
3153                 info->disk.major = dl->major;
3154                 info->disk.minor = dl->minor;
3155                 info->disk.number = dl->index;
3156                 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3157                                                           dl->index);
3158         }
3159
3160         info->data_offset         = pba_of_lba0(map_to_analyse);
3161
3162         if (info->array.level == 5) {
3163                 info->component_size = num_data_stripes(map_to_analyse) *
3164                                        map_to_analyse->blocks_per_strip;
3165         } else {
3166                 info->component_size = blocks_per_member(map_to_analyse);
3167         }
3168
3169         info->component_size = imsm_component_size_aligment_check(
3170                                                         info->array.level,
3171                                                         info->array.chunk_size,
3172                                                         super->sector_size,
3173                                                         info->component_size);
3174         info->bb.supported = 0;
3175
3176         memset(info->uuid, 0, sizeof(info->uuid));
3177         info->recovery_start = MaxSector;
3178
3179         info->reshape_progress = 0;
3180         info->resync_start = MaxSector;
3181         if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3182             dev->vol.dirty) &&
3183             imsm_reshape_blocks_arrays_changes(super) == 0) {
3184                 info->resync_start = 0;
3185         }
3186         if (dev->vol.migr_state) {
3187                 switch (migr_type(dev)) {
3188                 case MIGR_REPAIR:
3189                 case MIGR_INIT: {
3190                         __u64 blocks_per_unit = blocks_per_migr_unit(super,
3191                                                                      dev);
3192                         __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
3193
3194                         info->resync_start = blocks_per_unit * units;
3195                         break;
3196                 }
3197                 case MIGR_GEN_MIGR: {
3198                         __u64 blocks_per_unit = blocks_per_migr_unit(super,
3199                                                                      dev);
3200                         __u64 units = __le32_to_cpu(migr_rec->curr_migr_unit);
3201                         unsigned long long array_blocks;
3202                         int used_disks;
3203
3204                         if (__le32_to_cpu(migr_rec->ascending_migr) &&
3205                             (units <
3206                                 (__le32_to_cpu(migr_rec->num_migr_units)-1)) &&
3207                             (super->migr_rec->rec_status ==
3208                                         __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3209                                 units++;
3210
3211                         info->reshape_progress = blocks_per_unit * units;
3212
3213                         dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3214                                 (unsigned long long)units,
3215                                 (unsigned long long)blocks_per_unit,
3216                                 info->reshape_progress);
3217
3218                         used_disks = imsm_num_data_members(dev, MAP_1);
3219                         if (used_disks > 0) {
3220                                 array_blocks = blocks_per_member(map) *
3221                                         used_disks;
3222                                 /* round array size down to closest MB
3223                                  */
3224                                 info->custom_array_size = (array_blocks
3225                                                 >> SECT_PER_MB_SHIFT)
3226                                                 << SECT_PER_MB_SHIFT;
3227                         }
3228                 }
3229                 case MIGR_VERIFY:
3230                         /* we could emulate the checkpointing of
3231                          * 'sync_action=check' migrations, but for now
3232                          * we just immediately complete them
3233                          */
3234                 case MIGR_REBUILD:
3235                         /* this is handled by container_content_imsm() */
3236                 case MIGR_STATE_CHANGE:
3237                         /* FIXME handle other migrations */
3238                 default:
3239                         /* we are not dirty, so... */
3240                         info->resync_start = MaxSector;
3241                 }
3242         }
3243
3244         strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3245         info->name[MAX_RAID_SERIAL_LEN] = 0;
3246
3247         info->array.major_version = -1;
3248         info->array.minor_version = -2;
3249         sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3250         info->safe_mode_delay = 4000;  /* 4 secs like the Matrix driver */
3251         uuid_from_super_imsm(st, info->uuid);
3252
3253         if (dmap) {
3254                 int i, j;
3255                 for (i=0; i<map_disks; i++) {
3256                         dmap[i] = 0;
3257                         if (i < info->array.raid_disks) {
3258                                 struct imsm_disk *dsk;
3259                                 j = get_imsm_disk_idx(dev, i, MAP_X);
3260                                 dsk = get_imsm_disk(super, j);
3261                                 if (dsk && (dsk->status & CONFIGURED_DISK))
3262                                         dmap[i] = 1;
3263                         }
3264                 }
3265         }
3266 }
3267
3268 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3269                                 int failed, int look_in_map);
3270
3271 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3272                              int look_in_map);
3273
3274 #ifndef MDASSEMBLE
3275 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3276 {
3277         if (is_gen_migration(dev)) {
3278                 int failed;
3279                 __u8 map_state;
3280                 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3281
3282                 failed = imsm_count_failed(super, dev, MAP_1);
3283                 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3284                 if (map2->map_state != map_state) {
3285                         map2->map_state = map_state;
3286                         super->updates_pending++;
3287                 }
3288         }
3289 }
3290 #endif
3291
3292 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3293 {
3294         struct dl *d;
3295
3296         for (d = super->missing; d; d = d->next)
3297                 if (d->index == index)
3298                         return &d->disk;
3299         return NULL;
3300 }
3301
3302 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3303 {
3304         struct intel_super *super = st->sb;
3305         struct imsm_disk *disk;
3306         int map_disks = info->array.raid_disks;
3307         int max_enough = -1;
3308         int i;
3309         struct imsm_super *mpb;
3310
3311         if (super->current_vol >= 0) {
3312                 getinfo_super_imsm_volume(st, info, map);
3313                 return;
3314         }
3315         memset(info, 0, sizeof(*info));
3316
3317         /* Set raid_disks to zero so that Assemble will always pull in valid
3318          * spares
3319          */
3320         info->array.raid_disks    = 0;
3321         info->array.level         = LEVEL_CONTAINER;
3322         info->array.layout        = 0;
3323         info->array.md_minor      = -1;
3324         info->array.ctime         = 0; /* N/A for imsm */
3325         info->array.utime         = 0;
3326         info->array.chunk_size    = 0;
3327
3328         info->disk.major = 0;
3329         info->disk.minor = 0;
3330         info->disk.raid_disk = -1;
3331         info->reshape_active = 0;
3332         info->array.major_version = -1;
3333         info->array.minor_version = -2;
3334         strcpy(info->text_version, "imsm");
3335         info->safe_mode_delay = 0;
3336         info->disk.number = -1;
3337         info->disk.state = 0;
3338         info->name[0] = 0;
3339         info->recovery_start = MaxSector;
3340         info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3341         info->bb.supported = 0;
3342
3343         /* do we have the all the insync disks that we expect? */
3344         mpb = super->anchor;
3345
3346         for (i = 0; i < mpb->num_raid_devs; i++) {
3347                 struct imsm_dev *dev = get_imsm_dev(super, i);
3348                 int failed, enough, j, missing = 0;
3349                 struct imsm_map *map;
3350                 __u8 state;
3351
3352                 failed = imsm_count_failed(super, dev, MAP_0);
3353                 state = imsm_check_degraded(super, dev, failed, MAP_0);
3354                 map = get_imsm_map(dev, MAP_0);
3355
3356                 /* any newly missing disks?
3357                  * (catches single-degraded vs double-degraded)
3358                  */
3359                 for (j = 0; j < map->num_members; j++) {
3360                         __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3361                         __u32 idx = ord_to_idx(ord);
3362
3363                         if (!(ord & IMSM_ORD_REBUILD) &&
3364                             get_imsm_missing(super, idx)) {
3365                                 missing = 1;
3366                                 break;
3367                         }
3368                 }
3369
3370                 if (state == IMSM_T_STATE_FAILED)
3371                         enough = -1;
3372                 else if (state == IMSM_T_STATE_DEGRADED &&
3373                          (state != map->map_state || missing))
3374                         enough = 0;
3375                 else /* we're normal, or already degraded */
3376                         enough = 1;
3377                 if (is_gen_migration(dev) && missing) {
3378                         /* during general migration we need all disks
3379                          * that process is running on.
3380                          * No new missing disk is allowed.
3381                          */
3382                         max_enough = -1;
3383                         enough = -1;
3384                         /* no more checks necessary
3385                          */
3386                         break;
3387                 }
3388                 /* in the missing/failed disk case check to see
3389                  * if at least one array is runnable
3390                  */
3391                 max_enough = max(max_enough, enough);
3392         }
3393         dprintf("enough: %d\n", max_enough);
3394         info->container_enough = max_enough;
3395
3396         if (super->disks) {
3397                 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3398
3399                 disk = &super->disks->disk;
3400                 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3401                 info->component_size = reserved;
3402                 info->disk.state  = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3403                 /* we don't change info->disk.raid_disk here because
3404                  * this state will be finalized in mdmon after we have
3405                  * found the 'most fresh' version of the metadata
3406                  */
3407                 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3408                 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3409         }
3410
3411         /* only call uuid_from_super_imsm when this disk is part of a populated container,
3412          * ->compare_super may have updated the 'num_raid_devs' field for spares
3413          */
3414         if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3415                 uuid_from_super_imsm(st, info->uuid);
3416         else
3417                 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3418
3419         /* I don't know how to compute 'map' on imsm, so use safe default */
3420         if (map) {
3421                 int i;
3422                 for (i = 0; i < map_disks; i++)
3423                         map[i] = 1;
3424         }
3425
3426 }
3427
3428 /* allocates memory and fills disk in mdinfo structure
3429  * for each disk in array */
3430 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3431 {
3432         struct mdinfo *mddev;
3433         struct intel_super *super = st->sb;
3434         struct imsm_disk *disk;
3435         int count = 0;
3436         struct dl *dl;
3437         if (!super || !super->disks)
3438                 return NULL;
3439         dl = super->disks;
3440         mddev = xcalloc(1, sizeof(*mddev));
3441         while (dl) {
3442                 struct mdinfo *tmp;
3443                 disk = &dl->disk;
3444                 tmp = xcalloc(1, sizeof(*tmp));
3445                 if (mddev->devs)
3446                         tmp->next = mddev->devs;
3447                 mddev->devs = tmp;
3448                 tmp->disk.number = count++;
3449                 tmp->disk.major = dl->major;
3450                 tmp->disk.minor = dl->minor;
3451                 tmp->disk.state = is_configured(disk) ?
3452                                   (1 << MD_DISK_ACTIVE) : 0;
3453                 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3454                 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3455                 tmp->disk.raid_disk = -1;
3456                 dl = dl->next;
3457         }
3458         return mddev;
3459 }
3460
3461 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3462                              char *update, char *devname, int verbose,
3463                              int uuid_set, char *homehost)
3464 {
3465         /* For 'assemble' and 'force' we need to return non-zero if any
3466          * change was made.  For others, the return value is ignored.
3467          * Update options are:
3468          *  force-one : This device looks a bit old but needs to be included,
3469          *        update age info appropriately.
3470          *  assemble: clear any 'faulty' flag to allow this device to
3471          *              be assembled.
3472          *  force-array: Array is degraded but being forced, mark it clean
3473          *         if that will be needed to assemble it.
3474          *
3475          *  newdev:  not used ????
3476          *  grow:  Array has gained a new device - this is currently for
3477          *              linear only
3478          *  resync: mark as dirty so a resync will happen.
3479          *  name:  update the name - preserving the homehost
3480          *  uuid:  Change the uuid of the array to match watch is given
3481          *
3482          * Following are not relevant for this imsm:
3483          *  sparc2.2 : update from old dodgey metadata
3484          *  super-minor: change the preferred_minor number
3485          *  summaries:  update redundant counters.
3486          *  homehost:  update the recorded homehost
3487          *  _reshape_progress: record new reshape_progress position.
3488          */
3489         int rv = 1;
3490         struct intel_super *super = st->sb;
3491         struct imsm_super *mpb;
3492
3493         /* we can only update container info */
3494         if (!super || super->current_vol >= 0 || !super->anchor)
3495                 return 1;
3496
3497         mpb = super->anchor;
3498
3499         if (strcmp(update, "uuid") == 0) {
3500                 /* We take this to mean that the family_num should be updated.
3501                  * However that is much smaller than the uuid so we cannot really
3502                  * allow an explicit uuid to be given.  And it is hard to reliably
3503                  * know if one was.
3504                  * So if !uuid_set we know the current uuid is random and just used
3505                  * the first 'int' and copy it to the other 3 positions.
3506                  * Otherwise we require the 4 'int's to be the same as would be the
3507                  * case if we are using a random uuid.  So an explicit uuid will be
3508                  * accepted as long as all for ints are the same... which shouldn't hurt
3509                  */
3510                 if (!uuid_set) {
3511                         info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3512                         rv = 0;
3513                 } else {
3514                         if (info->uuid[0] != info->uuid[1] ||
3515                             info->uuid[1] != info->uuid[2] ||
3516                             info->uuid[2] != info->uuid[3])
3517                                 rv = -1;
3518                         else
3519                                 rv = 0;
3520                 }
3521                 if (rv == 0)
3522                         mpb->orig_family_num = info->uuid[0];
3523         } else if (strcmp(update, "assemble") == 0)
3524                 rv = 0;
3525         else
3526                 rv = -1;
3527
3528         /* successful update? recompute checksum */
3529         if (rv == 0)
3530                 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3531
3532         return rv;
3533 }
3534
3535 static size_t disks_to_mpb_size(int disks)
3536 {
3537         size_t size;
3538
3539         size = sizeof(struct imsm_super);
3540         size += (disks - 1) * sizeof(struct imsm_disk);
3541         size += 2 * sizeof(struct imsm_dev);
3542         /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3543         size += (4 - 2) * sizeof(struct imsm_map);
3544         /* 4 possible disk_ord_tbl's */
3545         size += 4 * (disks - 1) * sizeof(__u32);
3546         /* maximum bbm log */
3547         size += sizeof(struct bbm_log);
3548
3549         return size;
3550 }
3551
3552 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3553                              unsigned long long data_offset)
3554 {
3555         if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3556                 return 0;
3557
3558         return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
3559 }
3560
3561 static void free_devlist(struct intel_super *super)
3562 {
3563         struct intel_dev *dv;
3564
3565         while (super->devlist) {
3566                 dv = super->devlist->next;
3567                 free(super->devlist->dev);
3568                 free(super->devlist);
3569                 super->devlist = dv;
3570         }
3571 }
3572
3573 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
3574 {
3575         memcpy(dest, src, sizeof_imsm_dev(src, 0));
3576 }
3577
3578 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
3579 {
3580         /*
3581          * return:
3582          *  0 same, or first was empty, and second was copied
3583          *  1 second had wrong number
3584          *  2 wrong uuid
3585          *  3 wrong other info
3586          */
3587         struct intel_super *first = st->sb;
3588         struct intel_super *sec = tst->sb;
3589
3590         if (!first) {
3591                 st->sb = tst->sb;
3592                 tst->sb = NULL;
3593                 return 0;
3594         }
3595         /* in platform dependent environment test if the disks
3596          * use the same Intel hba
3597          * If not on Intel hba at all, allow anything.
3598          */
3599         if (!check_env("IMSM_NO_PLATFORM") && first->hba && sec->hba) {
3600                 if (first->hba->type != sec->hba->type) {
3601                         fprintf(stderr,
3602                                 "HBAs of devices do not match %s != %s\n",
3603                                 get_sys_dev_type(first->hba->type),
3604                                 get_sys_dev_type(sec->hba->type));
3605                         return 3;
3606                 }
3607                 if (first->orom != sec->orom) {
3608                         fprintf(stderr,
3609                                 "HBAs of devices do not match %s != %s\n",
3610                                 first->hba->pci_id, sec->hba->pci_id);
3611                         return 3;
3612                 }
3613         }
3614
3615         /* if an anchor does not have num_raid_devs set then it is a free
3616          * floating spare
3617          */
3618         if (first->anchor->num_raid_devs > 0 &&
3619             sec->anchor->num_raid_devs > 0) {
3620                 /* Determine if these disks might ever have been
3621                  * related.  Further disambiguation can only take place
3622                  * in load_super_imsm_all
3623                  */
3624                 __u32 first_family = first->anchor->orig_family_num;
3625                 __u32 sec_family = sec->anchor->orig_family_num;
3626
3627                 if (memcmp(first->anchor->sig, sec->anchor->sig,
3628                            MAX_SIGNATURE_LENGTH) != 0)
3629                         return 3;
3630
3631                 if (first_family == 0)
3632                         first_family = first->anchor->family_num;
3633                 if (sec_family == 0)
3634                         sec_family = sec->anchor->family_num;
3635
3636                 if (first_family != sec_family)
3637                         return 3;
3638
3639         }
3640
3641         /* if 'first' is a spare promote it to a populated mpb with sec's
3642          * family number
3643          */
3644         if (first->anchor->num_raid_devs == 0 &&
3645             sec->anchor->num_raid_devs > 0) {
3646                 int i;
3647                 struct intel_dev *dv;
3648                 struct imsm_dev *dev;
3649
3650                 /* we need to copy raid device info from sec if an allocation
3651                  * fails here we don't associate the spare
3652                  */
3653                 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
3654                         dv = xmalloc(sizeof(*dv));
3655                         dev = xmalloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
3656                         dv->dev = dev;
3657                         dv->index = i;
3658                         dv->next = first->devlist;
3659                         first->devlist = dv;
3660                 }
3661                 if (i < sec->anchor->num_raid_devs) {
3662                         /* allocation failure */
3663                         free_devlist(first);
3664                         pr_err("imsm: failed to associate spare\n");
3665                         return 3;
3666                 }
3667                 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
3668                 first->anchor->orig_family_num = sec->anchor->orig_family_num;
3669                 first->anchor->family_num = sec->anchor->family_num;
3670                 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
3671                 for (i = 0; i < sec->anchor->num_raid_devs; i++)
3672                         imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
3673         }
3674
3675         return 0;
3676 }
3677
3678 static void fd2devname(int fd, char *name)
3679 {
3680         struct stat st;
3681         char path[256];
3682         char dname[PATH_MAX];
3683         char *nm;
3684         int rv;
3685
3686         name[0] = '\0';
3687         if (fstat(fd, &st) != 0)
3688                 return;
3689         sprintf(path, "/sys/dev/block/%d:%d",
3690                 major(st.st_rdev), minor(st.st_rdev));
3691
3692         rv = readlink(path, dname, sizeof(dname)-1);
3693         if (rv <= 0)
3694                 return;
3695
3696         dname[rv] = '\0';
3697         nm = strrchr(dname, '/');
3698         if (nm) {
3699                 nm++;
3700                 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
3701         }
3702 }
3703
3704 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
3705 {
3706         char path[60];
3707         char *name = fd2kname(fd);
3708
3709         if (!name)
3710                 return 1;
3711
3712         if (strncmp(name, "nvme", 4) != 0)
3713                 return 1;
3714
3715         snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
3716
3717         return load_sys(path, buf, buf_len);
3718 }
3719
3720 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
3721
3722 static int imsm_read_serial(int fd, char *devname,
3723                             __u8 serial[MAX_RAID_SERIAL_LEN])
3724 {
3725         char buf[50];
3726         int rv;
3727         int len;
3728         char *dest;
3729         char *src;
3730         unsigned int i;
3731
3732         memset(buf, 0, sizeof(buf));
3733
3734         rv = nvme_get_serial(fd, buf, sizeof(buf));
3735
3736         if (rv)
3737                 rv = scsi_get_serial(fd, buf, sizeof(buf));
3738
3739         if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
3740                 memset(serial, 0, MAX_RAID_SERIAL_LEN);
3741                 fd2devname(fd, (char *) serial);
3742                 return 0;
3743         }
3744
3745         if (rv != 0) {
3746                 if (devname)
3747                         pr_err("Failed to retrieve serial for %s\n",
3748                                devname);
3749                 return rv;
3750         }
3751
3752         /* trim all whitespace and non-printable characters and convert
3753          * ':' to ';'
3754          */
3755         for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
3756                 src = &buf[i];
3757                 if (*src > 0x20) {
3758                         /* ':' is reserved for use in placeholder serial
3759                          * numbers for missing disks
3760                          */
3761                         if (*src == ':')
3762                                 *dest++ = ';';
3763                         else
3764                                 *dest++ = *src;
3765                 }
3766         }
3767         len = dest - buf;
3768         dest = buf;
3769
3770         /* truncate leading characters */
3771         if (len > MAX_RAID_SERIAL_LEN) {
3772                 dest += len - MAX_RAID_SERIAL_LEN;
3773                 len = MAX_RAID_SERIAL_LEN;
3774         }
3775
3776         memset(serial, 0, MAX_RAID_SERIAL_LEN);
3777         memcpy(serial, dest, len);
3778
3779         return 0;
3780 }
3781
3782 static int serialcmp(__u8 *s1, __u8 *s2)
3783 {
3784         return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
3785 }
3786
3787 static void serialcpy(__u8 *dest, __u8 *src)
3788 {
3789         strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
3790 }
3791
3792 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
3793 {
3794         struct dl *dl;
3795
3796         for (dl = super->disks; dl; dl = dl->next)
3797                 if (serialcmp(dl->serial, serial) == 0)
3798                         break;
3799
3800         return dl;
3801 }
3802
3803 static struct imsm_disk *
3804 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
3805 {
3806         int i;
3807
3808         for (i = 0; i < mpb->num_disks; i++) {
3809                 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
3810
3811                 if (serialcmp(disk->serial, serial) == 0) {
3812                         if (idx)
3813                                 *idx = i;
3814                         return disk;
3815                 }
3816         }
3817
3818         return NULL;
3819 }
3820
3821 static int
3822 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
3823 {
3824         struct imsm_disk *disk;
3825         struct dl *dl;
3826         struct stat stb;
3827         int rv;
3828         char name[40];
3829         __u8 serial[MAX_RAID_SERIAL_LEN];
3830
3831         rv = imsm_read_serial(fd, devname, serial);
3832
3833         if (rv != 0)
3834                 return 2;
3835
3836         dl = xcalloc(1, sizeof(*dl));
3837
3838         fstat(fd, &stb);
3839         dl->major = major(stb.st_rdev);
3840         dl->minor = minor(stb.st_rdev);
3841         dl->next = super->disks;
3842         dl->fd = keep_fd ? fd : -1;
3843         assert(super->disks == NULL);
3844         super->disks = dl;
3845         serialcpy(dl->serial, serial);
3846         dl->index = -2;
3847         dl->e = NULL;
3848         fd2devname(fd, name);
3849         if (devname)
3850                 dl->devname = xstrdup(devname);
3851         else
3852                 dl->devname = xstrdup(name);
3853
3854         /* look up this disk's index in the current anchor */
3855         disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
3856         if (disk) {
3857                 dl->disk = *disk;
3858                 /* only set index on disks that are a member of a
3859                  * populated contianer, i.e. one with raid_devs
3860                  */
3861                 if (is_failed(&dl->disk))
3862                         dl->index = -2;
3863                 else if (is_spare(&dl->disk))
3864                         dl->index = -1;
3865         }
3866
3867         return 0;
3868 }
3869
3870 #ifndef MDASSEMBLE
3871 /* When migrating map0 contains the 'destination' state while map1
3872  * contains the current state.  When not migrating map0 contains the
3873  * current state.  This routine assumes that map[0].map_state is set to
3874  * the current array state before being called.
3875  *
3876  * Migration is indicated by one of the following states
3877  * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3878  * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3879  *    map1state=unitialized)
3880  * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR  map0state=normal
3881  *    map1state=normal)
3882  * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3883  *    map1state=degraded)
3884  * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3885  *    map1state=normal)
3886  */
3887 static void migrate(struct imsm_dev *dev, struct intel_super *super,
3888                     __u8 to_state, int migr_type)
3889 {
3890         struct imsm_map *dest;
3891         struct imsm_map *src = get_imsm_map(dev, MAP_0);
3892
3893         dev->vol.migr_state = 1;
3894         set_migr_type(dev, migr_type);
3895         dev->vol.curr_migr_unit = 0;
3896         dest = get_imsm_map(dev, MAP_1);
3897
3898         /* duplicate and then set the target end state in map[0] */
3899         memcpy(dest, src, sizeof_imsm_map(src));
3900         if (migr_type == MIGR_REBUILD || migr_type ==  MIGR_GEN_MIGR) {
3901                 __u32 ord;
3902                 int i;
3903
3904                 for (i = 0; i < src->num_members; i++) {
3905                         ord = __le32_to_cpu(src->disk_ord_tbl[i]);
3906                         set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
3907                 }
3908         }
3909
3910         if (migr_type == MIGR_GEN_MIGR)
3911                 /* Clear migration record */
3912                 memset(super->migr_rec, 0, sizeof(struct migr_record));
3913
3914         src->map_state = to_state;
3915 }
3916
3917 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
3918                           __u8 map_state)
3919 {
3920         struct imsm_map *map = get_imsm_map(dev, MAP_0);
3921         struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
3922                                                     MAP_0 : MAP_1);
3923         int i, j;
3924
3925         /* merge any IMSM_ORD_REBUILD bits that were not successfully
3926          * completed in the last migration.
3927          *
3928          * FIXME add support for raid-level-migration
3929          */
3930         if (map_state != map->map_state && (is_gen_migration(dev) == 0) &&
3931             prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
3932                 /* when final map state is other than expected
3933                  * merge maps (not for migration)
3934                  */
3935                 int failed;
3936
3937                 for (i = 0; i < prev->num_members; i++)
3938                         for (j = 0; j < map->num_members; j++)
3939                                 /* during online capacity expansion
3940                                  * disks position can be changed
3941                                  * if takeover is used
3942                                  */
3943                                 if (ord_to_idx(map->disk_ord_tbl[j]) ==
3944                                     ord_to_idx(prev->disk_ord_tbl[i])) {
3945                                         map->disk_ord_tbl[j] |=
3946                                                 prev->disk_ord_tbl[i];
3947                                         break;
3948                                 }
3949                 failed = imsm_count_failed(super, dev, MAP_0);
3950                 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
3951         }
3952
3953         dev->vol.migr_state = 0;
3954         set_migr_type(dev, 0);
3955         dev->vol.curr_migr_unit = 0;
3956         map->map_state = map_state;
3957 }
3958 #endif
3959
3960 static int parse_raid_devices(struct intel_super *super)
3961 {
3962         int i;
3963         struct imsm_dev *dev_new;
3964         size_t len, len_migr;
3965         size_t max_len = 0;
3966         size_t space_needed = 0;
3967         struct imsm_super *mpb = super->anchor;
3968
3969         for (i = 0; i < super->anchor->num_raid_devs; i++) {
3970                 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
3971                 struct intel_dev *dv;
3972
3973                 len = sizeof_imsm_dev(dev_iter, 0);
3974                 len_migr = sizeof_imsm_dev(dev_iter, 1);
3975                 if (len_migr > len)
3976                         space_needed += len_migr - len;
3977
3978                 dv = xmalloc(sizeof(*dv));
3979                 if (max_len < len_migr)
3980                         max_len = len_migr;
3981                 if (max_len > len_migr)
3982                         space_needed += max_len - len_migr;
3983                 dev_new = xmalloc(max_len);
3984                 imsm_copy_dev(dev_new, dev_iter);
3985                 dv->dev = dev_new;
3986                 dv->index = i;
3987                 dv->next = super->devlist;
3988                 super->devlist = dv;
3989         }
3990
3991         /* ensure that super->buf is large enough when all raid devices
3992          * are migrating
3993          */
3994         if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
3995                 void *buf;
3996
3997                 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
3998                               super->sector_size);
3999                 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4000                         return 1;
4001
4002                 memcpy(buf, super->buf, super->len);
4003                 memset(buf + super->len, 0, len - super->len);
4004                 free(super->buf);
4005                 super->buf = buf;
4006                 super->len = len;
4007         }
4008
4009         super->extra_space += space_needed;
4010
4011         return 0;
4012 }
4013
4014 /*******************************************************************************
4015  * Function:    check_mpb_migr_compatibility
4016  * Description: Function checks for unsupported migration features:
4017  *              - migration optimization area (pba_of_lba0)
4018  *              - descending reshape (ascending_migr)
4019  * Parameters:
4020  *      super   : imsm metadata information
4021  * Returns:
4022  *       0 : migration is compatible
4023  *      -1 : migration is not compatible
4024  ******************************************************************************/
4025 int check_mpb_migr_compatibility(struct intel_super *super)
4026 {
4027         struct imsm_map *map0, *map1;
4028         struct migr_record *migr_rec = super->migr_rec;
4029         int i;
4030
4031         for (i = 0; i < super->anchor->num_raid_devs; i++) {
4032                 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4033
4034                 if (dev_iter &&
4035                     dev_iter->vol.migr_state == 1 &&
4036                     dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4037                         /* This device is migrating */
4038                         map0 = get_imsm_map(dev_iter, MAP_0);
4039                         map1 = get_imsm_map(dev_iter, MAP_1);
4040                         if (pba_of_lba0(map0) != pba_of_lba0(map1))
4041                                 /* migration optimization area was used */
4042                                 return -1;
4043                         if (migr_rec->ascending_migr == 0
4044                                 && migr_rec->dest_depth_per_unit > 0)
4045                                 /* descending reshape not supported yet */
4046                                 return -1;
4047                 }
4048         }
4049         return 0;
4050 }
4051
4052 static void __free_imsm(struct intel_super *super, int free_disks);
4053
4054 /* load_imsm_mpb - read matrix metadata
4055  * allocates super->mpb to be freed by free_imsm
4056  */
4057 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4058 {
4059         unsigned long long dsize;
4060         unsigned long long sectors;
4061         unsigned int sector_size = super->sector_size;
4062         struct stat;
4063         struct imsm_super *anchor;
4064         __u32 check_sum;
4065
4066         get_dev_size(fd, NULL, &dsize);
4067         if (dsize < 2*sector_size) {
4068                 if (devname)
4069                         pr_err("%s: device to small for imsm\n",
4070                                devname);
4071                 return 1;
4072         }
4073
4074         if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4075                 if (devname)
4076                         pr_err("Cannot seek to anchor block on %s: %s\n",
4077                                devname, strerror(errno));
4078                 return 1;
4079         }
4080
4081         if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4082                 if (devname)
4083                         pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4084                 return 1;
4085         }
4086         if (read(fd, anchor, sector_size) != sector_size) {
4087                 if (devname)
4088                         pr_err("Cannot read anchor block on %s: %s\n",
4089                                devname, strerror(errno));
4090                 free(anchor);
4091                 return 1;
4092         }
4093
4094         if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4095                 if (devname)
4096                         pr_err("no IMSM anchor on %s\n", devname);
4097                 free(anchor);
4098                 return 2;
4099         }
4100
4101         __free_imsm(super, 0);
4102         /*  reload capability and hba */
4103
4104         /* capability and hba must be updated with new super allocation */
4105         find_intel_hba_capability(fd, super, devname);
4106         super->len = ROUND_UP(anchor->mpb_size, sector_size);
4107         if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4108                 if (devname)
4109                         pr_err("unable to allocate %zu byte mpb buffer\n",
4110                                super->len);
4111                 free(anchor);
4112                 return 2;
4113         }
4114         memcpy(super->buf, anchor, sector_size);
4115
4116         sectors = mpb_sectors(anchor, sector_size) - 1;
4117         free(anchor);
4118
4119         if (posix_memalign(&super->migr_rec_buf, sector_size,
4120             MIGR_REC_BUF_SECTORS*sector_size) != 0) {
4121                 pr_err("could not allocate migr_rec buffer\n");
4122                 free(super->buf);
4123                 return 2;
4124         }
4125         super->clean_migration_record_by_mdmon = 0;
4126
4127         if (!sectors) {
4128                 check_sum = __gen_imsm_checksum(super->anchor);
4129                 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4130                         if (devname)
4131                                 pr_err("IMSM checksum %x != %x on %s\n",
4132                                        check_sum,
4133                                        __le32_to_cpu(super->anchor->check_sum),
4134                                        devname);
4135                         return 2;
4136                 }
4137
4138                 return 0;
4139         }
4140
4141         /* read the extended mpb */
4142         if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4143                 if (devname)
4144                         pr_err("Cannot seek to extended mpb on %s: %s\n",
4145                                devname, strerror(errno));
4146                 return 1;
4147         }
4148
4149         if ((unsigned int)read(fd, super->buf + sector_size,
4150                     super->len - sector_size) != super->len - sector_size) {
4151                 if (devname)
4152                         pr_err("Cannot read extended mpb on %s: %s\n",
4153                                devname, strerror(errno));
4154                 return 2;
4155         }
4156
4157         check_sum = __gen_imsm_checksum(super->anchor);
4158         if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4159                 if (devname)
4160                         pr_err("IMSM checksum %x != %x on %s\n",
4161                                check_sum, __le32_to_cpu(super->anchor->check_sum),
4162                                devname);
4163                 return 3;
4164         }
4165
4166         return 0;
4167 }
4168
4169 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4170
4171 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4172 static void clear_hi(struct intel_super *super)
4173 {
4174         struct imsm_super *mpb = super->anchor;
4175         int i, n;
4176         if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4177                 return;
4178         for (i = 0; i < mpb->num_disks; ++i) {
4179                 struct imsm_disk *disk = &mpb->disk[i];
4180                 disk->total_blocks_hi = 0;
4181         }
4182         for (i = 0; i < mpb->num_raid_devs; ++i) {
4183                 struct imsm_dev *dev = get_imsm_dev(super, i);
4184                 if (!dev)
4185                         return;
4186                 for (n = 0; n < 2; ++n) {
4187                         struct imsm_map *map = get_imsm_map(dev, n);
4188                         if (!map)
4189                                 continue;
4190                         map->pba_of_lba0_hi = 0;
4191                         map->blocks_per_member_hi = 0;
4192                         map->num_data_stripes_hi = 0;
4193                 }
4194         }
4195 }
4196
4197 static int
4198 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4199 {
4200         int err;
4201
4202         err = load_imsm_mpb(fd, super, devname);
4203         if (err)
4204                 return err;
4205         if (super->sector_size == 4096)
4206                 convert_from_4k(super);
4207         err = load_imsm_disk(fd, super, devname, keep_fd);
4208         if (err)
4209                 return err;
4210         err = parse_raid_devices(super);
4211         if (err)
4212                 return err;
4213         err = load_bbm_log(super);
4214         clear_hi(super);
4215         return err;
4216 }
4217
4218 static void __free_imsm_disk(struct dl *d)
4219 {
4220         if (d->fd >= 0)
4221                 close(d->fd);
4222         if (d->devname)
4223                 free(d->devname);
4224         if (d->e)
4225                 free(d->e);
4226         free(d);
4227
4228 }
4229
4230 static void free_imsm_disks(struct intel_super *super)
4231 {
4232         struct dl *d;
4233
4234         while (super->disks) {
4235                 d = super->disks;
4236                 super->disks = d->next;
4237                 __free_imsm_disk(d);
4238         }
4239         while (super->disk_mgmt_list) {
4240                 d = super->disk_mgmt_list;
4241                 super->disk_mgmt_list = d->next;
4242                 __free_imsm_disk(d);
4243         }
4244         while (super->missing) {
4245                 d = super->missing;
4246                 super->missing = d->next;
4247                 __free_imsm_disk(d);
4248         }
4249
4250 }
4251
4252 /* free all the pieces hanging off of a super pointer */
4253 static void __free_imsm(struct intel_super *super, int free_disks)
4254 {
4255         struct intel_hba *elem, *next;
4256
4257         if (super->buf) {
4258                 free(super->buf);
4259                 super->buf = NULL;
4260         }
4261         /* unlink capability description */
4262         super->orom = NULL;
4263         if (super->migr_rec_buf) {
4264                 free(super->migr_rec_buf);
4265                 super->migr_rec_buf = NULL;
4266         }
4267         if (free_disks)
4268                 free_imsm_disks(super);
4269         free_devlist(super);
4270         elem = super->hba;
4271         while (elem) {
4272                 if (elem->path)
4273                         free((void *)elem->path);
4274                 next = elem->next;
4275                 free(elem);
4276                 elem = next;
4277         }
4278         if (super->bbm_log)
4279                 free(super->bbm_log);
4280         super->hba = NULL;
4281 }
4282
4283 static void free_imsm(struct intel_super *super)
4284 {
4285         __free_imsm(super, 1);
4286         free(super);
4287 }
4288
4289 static void free_super_imsm(struct supertype *st)
4290 {
4291         struct intel_super *super = st->sb;
4292
4293         if (!super)
4294                 return;
4295
4296         free_imsm(super);
4297         st->sb = NULL;
4298 }
4299
4300 static struct intel_super *alloc_super(void)
4301 {
4302         struct intel_super *super = xcalloc(1, sizeof(*super));
4303
4304         super->current_vol = -1;
4305         super->create_offset = ~((unsigned long long) 0);
4306         return super;
4307 }
4308
4309 /*
4310  * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4311  */
4312 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4313 {
4314         struct sys_dev *hba_name;
4315         int rv = 0;
4316
4317         if (fd < 0 || check_env("IMSM_NO_PLATFORM")) {
4318                 super->orom = NULL;
4319                 super->hba = NULL;
4320                 return 0;
4321         }
4322         hba_name = find_disk_attached_hba(fd, NULL);
4323         if (!hba_name) {
4324                 if (devname)
4325                         pr_err("%s is not attached to Intel(R) RAID controller.\n",
4326                                devname);
4327                 return 1;
4328         }
4329         rv = attach_hba_to_super(super, hba_name);
4330         if (rv == 2) {
4331                 if (devname) {
4332                         struct intel_hba *hba = super->hba;
4333
4334                         pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4335                                 "    but the container is assigned to Intel(R) %s %s (",
4336                                 devname,
4337                                 get_sys_dev_type(hba_name->type),
4338                                 hba_name->type == SYS_DEV_VMD ? "domain" : "RAID controller",
4339                                 hba_name->pci_id ? : "Err!",
4340                                 get_sys_dev_type(super->hba->type),
4341                                 hba->type == SYS_DEV_VMD ? "domain" : "RAID controller");
4342
4343                         while (hba) {
4344                                 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4345                                 if (hba->next)
4346                                         fprintf(stderr, ", ");
4347                                 hba = hba->next;
4348                         }
4349                         fprintf(stderr, ").\n"
4350                                 "    Mixing devices attached to different %s is not allowed.\n",
4351                                 hba_name->type == SYS_DEV_VMD ? "VMD domains" : "controllers");
4352                 }
4353                 return 2;
4354         }
4355         super->orom = find_imsm_capability(hba_name);
4356         if (!super->orom)
4357                 return 3;
4358
4359         return 0;
4360 }
4361
4362 /* find_missing - helper routine for load_super_imsm_all that identifies
4363  * disks that have disappeared from the system.  This routine relies on
4364  * the mpb being uptodate, which it is at load time.
4365  */
4366 static int find_missing(struct intel_super *super)
4367 {
4368         int i;
4369         struct imsm_super *mpb = super->anchor;
4370         struct dl *dl;
4371         struct imsm_disk *disk;
4372
4373         for (i = 0; i < mpb->num_disks; i++) {
4374                 disk = __get_imsm_disk(mpb, i);
4375                 dl = serial_to_dl(disk->serial, super);
4376                 if (dl)
4377                         continue;
4378
4379                 dl = xmalloc(sizeof(*dl));
4380                 dl->major = 0;
4381                 dl->minor = 0;
4382                 dl->fd = -1;
4383                 dl->devname = xstrdup("missing");
4384                 dl->index = i;
4385                 serialcpy(dl->serial, disk->serial);
4386                 dl->disk = *disk;
4387                 dl->e = NULL;
4388                 dl->next = super->missing;
4389                 super->missing = dl;
4390         }
4391
4392         return 0;
4393 }
4394
4395 #ifndef MDASSEMBLE
4396 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4397 {
4398         struct intel_disk *idisk = disk_list;
4399
4400         while (idisk) {
4401                 if (serialcmp(idisk->disk.serial, serial) == 0)
4402                         break;
4403                 idisk = idisk->next;
4404         }
4405
4406         return idisk;
4407 }
4408
4409 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4410                               struct intel_super *super,
4411                               struct intel_disk **disk_list)
4412 {
4413         struct imsm_disk *d = &super->disks->disk;
4414         struct imsm_super *mpb = super->anchor;
4415         int i, j;
4416
4417         for (i = 0; i < tbl_size; i++) {
4418                 struct imsm_super *tbl_mpb = table[i]->anchor;
4419                 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4420
4421                 if (tbl_mpb->family_num == mpb->family_num) {
4422                         if (tbl_mpb->check_sum == mpb->check_sum) {
4423                                 dprintf("mpb from %d:%d matches %d:%d\n",
4424                                         super->disks->major,
4425                                         super->disks->minor,
4426                                         table[i]->disks->major,
4427                                         table[i]->disks->minor);
4428                                 break;
4429                         }
4430
4431                         if (((is_configured(d) && !is_configured(tbl_d)) ||
4432                              is_configured(d) == is_configured(tbl_d)) &&
4433                             tbl_mpb->generation_num < mpb->generation_num) {
4434                                 /* current version of the mpb is a
4435                                  * better candidate than the one in
4436                                  * super_table, but copy over "cross
4437                                  * generational" status
4438                                  */
4439                                 struct intel_disk *idisk;
4440
4441                                 dprintf("mpb from %d:%d replaces %d:%d\n",
4442                                         super->disks->major,
4443                                         super->disks->minor,
4444                                         table[i]->disks->major,
4445                                         table[i]->disks->minor);
4446
4447                                 idisk = disk_list_get(tbl_d->serial, *disk_list);
4448                                 if (idisk && is_failed(&idisk->disk))
4449                                         tbl_d->status |= FAILED_DISK;
4450                                 break;
4451                         } else {
4452                                 struct intel_disk *idisk;
4453                                 struct imsm_disk *disk;
4454
4455                                 /* tbl_mpb is more up to date, but copy
4456                                  * over cross generational status before
4457                                  * returning
4458                                  */
4459                                 disk = __serial_to_disk(d->serial, mpb, NULL);
4460                                 if (disk && is_failed(disk))
4461                                         d->status |= FAILED_DISK;
4462
4463                                 idisk = disk_list_get(d->serial, *disk_list);
4464                                 if (idisk) {
4465                                         idisk->owner = i;
4466                                         if (disk && is_configured(disk))
4467                                                 idisk->disk.status |= CONFIGURED_DISK;
4468                                 }
4469
4470                                 dprintf("mpb from %d:%d prefer %d:%d\n",
4471                                         super->disks->major,
4472                                         super->disks->minor,
4473                                         table[i]->disks->major,
4474                                         table[i]->disks->minor);
4475
4476                                 return tbl_size;
4477                         }
4478                 }
4479         }
4480
4481         if (i >= tbl_size)
4482                 table[tbl_size++] = super;
4483         else
4484                 table[i] = super;
4485
4486         /* update/extend the merged list of imsm_disk records */
4487         for (j = 0; j < mpb->num_disks; j++) {
4488                 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4489                 struct intel_disk *idisk;
4490
4491                 idisk = disk_list_get(disk->serial, *disk_list);
4492                 if (idisk) {
4493                         idisk->disk.status |= disk->status;
4494                         if (is_configured(&idisk->disk) ||
4495                             is_failed(&idisk->disk))
4496                                 idisk->disk.status &= ~(SPARE_DISK);
4497                 } else {
4498                         idisk = xcalloc(1, sizeof(*idisk));
4499                         idisk->owner = IMSM_UNKNOWN_OWNER;
4500                         idisk->disk = *disk;
4501                         idisk->next = *disk_list;
4502                         *disk_list = idisk;
4503                 }
4504
4505                 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4506                         idisk->owner = i;
4507         }
4508
4509         return tbl_size;
4510 }
4511
4512 static struct intel_super *
4513 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4514                  const int owner)
4515 {
4516         struct imsm_super *mpb = super->anchor;
4517         int ok_count = 0;
4518         int i;
4519
4520         for (i = 0; i < mpb->num_disks; i++) {
4521                 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4522                 struct intel_disk *idisk;
4523
4524                 idisk = disk_list_get(disk->serial, disk_list);
4525                 if (idisk) {
4526                         if (idisk->owner == owner ||
4527                             idisk->owner == IMSM_UNKNOWN_OWNER)
4528                                 ok_count++;
4529                         else
4530                                 dprintf("'%.16s' owner %d != %d\n",
4531                                         disk->serial, idisk->owner,
4532                                         owner);
4533                 } else {
4534                         dprintf("unknown disk %x [%d]: %.16s\n",
4535                                 __le32_to_cpu(mpb->family_num), i,
4536                                 disk->serial);
4537                         break;
4538                 }
4539         }
4540
4541         if (ok_count == mpb->num_disks)
4542                 return super;
4543         return NULL;
4544 }
4545
4546 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4547 {
4548         struct intel_super *s;
4549
4550         for (s = super_list; s; s = s->next) {
4551                 if (family_num != s->anchor->family_num)
4552                         continue;
4553                 pr_err("Conflict, offlining family %#x on '%s'\n",
4554                         __le32_to_cpu(family_num), s->disks->devname);
4555         }
4556 }
4557
4558 static struct intel_super *
4559 imsm_thunderdome(struct intel_super **super_list, int len)
4560 {
4561         struct intel_super *super_table[len];
4562         struct intel_disk *disk_list = NULL;
4563         struct intel_super *champion, *spare;
4564         struct intel_super *s, **del;
4565         int tbl_size = 0;
4566         int conflict;
4567         int i;
4568
4569         memset(super_table, 0, sizeof(super_table));
4570         for (s = *super_list; s; s = s->next)
4571                 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4572
4573         for (i = 0; i < tbl_size; i++) {
4574                 struct imsm_disk *d;
4575                 struct intel_disk *idisk;
4576                 struct imsm_super *mpb = super_table[i]->anchor;
4577
4578                 s = super_table[i];
4579                 d = &s->disks->disk;
4580
4581                 /* 'd' must appear in merged disk list for its
4582                  * configuration to be valid
4583                  */
4584                 idisk = disk_list_get(d->serial, disk_list);
4585                 if (idisk && idisk->owner == i)
4586                         s = validate_members(s, disk_list, i);
4587                 else
4588                         s = NULL;
4589
4590                 if (!s)
4591                         dprintf("marking family: %#x from %d:%d offline\n",
4592                                 mpb->family_num,
4593                                 super_table[i]->disks->major,
4594                                 super_table[i]->disks->minor);
4595                 super_table[i] = s;
4596         }
4597
4598         /* This is where the mdadm implementation differs from the Windows
4599          * driver which has no strict concept of a container.  We can only
4600          * assemble one family from a container, so when returning a prodigal
4601          * array member to this system the code will not be able to disambiguate
4602          * the container contents that should be assembled ("foreign" versus
4603          * "local").  It requires user intervention to set the orig_family_num
4604          * to a new value to establish a new container.  The Windows driver in
4605          * this situation fixes up the volume name in place and manages the
4606          * foreign array as an independent entity.
4607          */
4608         s = NULL;
4609         spare = NULL;
4610         conflict = 0;
4611         for (i = 0; i < tbl_size; i++) {
4612                 struct intel_super *tbl_ent = super_table[i];
4613                 int is_spare = 0;
4614
4615                 if (!tbl_ent)
4616                         continue;
4617
4618                 if (tbl_ent->anchor->num_raid_devs == 0) {
4619                         spare = tbl_ent;
4620                         is_spare = 1;
4621                 }
4622
4623                 if (s && !is_spare) {
4624                         show_conflicts(tbl_ent->anchor->family_num, *super_list);
4625                         conflict++;
4626                 } else if (!s && !is_spare)
4627                         s = tbl_ent;
4628         }
4629
4630         if (!s)
4631                 s = spare;
4632         if (!s) {
4633                 champion = NULL;
4634                 goto out;
4635         }
4636         champion = s;
4637
4638         if (conflict)
4639                 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4640                         __le32_to_cpu(s->anchor->family_num), s->disks->devname);
4641
4642         /* collect all dl's onto 'champion', and update them to
4643          * champion's version of the status
4644          */
4645         for (s = *super_list; s; s = s->next) {
4646                 struct imsm_super *mpb = champion->anchor;
4647                 struct dl *dl = s->disks;
4648
4649                 if (s == champion)
4650                         continue;
4651
4652                 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
4653
4654                 for (i = 0; i < mpb->num_disks; i++) {
4655                         struct imsm_disk *disk;
4656
4657                         disk = __serial_to_disk(dl->serial, mpb, &dl->index);
4658                         if (disk) {
4659                                 dl->disk = *disk;
4660                                 /* only set index on disks that are a member of
4661                                  * a populated contianer, i.e. one with
4662                                  * raid_devs
4663                                  */
4664                                 if (is_failed(&dl->disk))
4665                                         dl->index = -2;
4666                                 else if (is_spare(&dl->disk))
4667                                         dl->index = -1;
4668                                 break;
4669                         }
4670                 }
4671
4672                 if (i >= mpb->num_disks) {
4673                         struct intel_disk *idisk;
4674
4675                         idisk = disk_list_get(dl->serial, disk_list);
4676                         if (idisk && is_spare(&idisk->disk) &&
4677                             !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
4678                                 dl->index = -1;
4679                         else {
4680                                 dl->index = -2;
4681                                 continue;
4682                         }
4683                 }
4684
4685                 dl->next = champion->disks;
4686                 champion->disks = dl;
4687                 s->disks = NULL;
4688         }
4689
4690         /* delete 'champion' from super_list */
4691         for (del = super_list; *del; ) {
4692                 if (*del == champion) {
4693                         *del = (*del)->next;
4694                         break;
4695                 } else
4696                         del = &(*del)->next;
4697         }
4698         champion->next = NULL;
4699
4700  out:
4701         while (disk_list) {
4702                 struct intel_disk *idisk = disk_list;
4703
4704                 disk_list = disk_list->next;
4705                 free(idisk);
4706         }
4707
4708         return champion;
4709 }
4710
4711 static int
4712 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
4713 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
4714                            int major, int minor, int keep_fd);
4715 static int
4716 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
4717                         int *max, int keep_fd);
4718
4719 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
4720                                char *devname, struct md_list *devlist,
4721                                int keep_fd)
4722 {
4723         struct intel_super *super_list = NULL;
4724         struct intel_super *super = NULL;
4725         int err = 0;
4726         int i = 0;
4727
4728         if (fd >= 0)
4729                 /* 'fd' is an opened container */
4730                 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
4731         else
4732                 /* get super block from devlist devices */
4733                 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
4734         if (err)
4735                 goto error;
4736         /* all mpbs enter, maybe one leaves */
4737         super = imsm_thunderdome(&super_list, i);
4738         if (!super) {
4739                 err = 1;
4740                 goto error;
4741         }
4742
4743         if (find_missing(super) != 0) {
4744                 free_imsm(super);
4745                 err = 2;
4746                 goto error;
4747         }
4748
4749         /* load migration record */
4750         err = load_imsm_migr_rec(super, NULL);
4751         if (err == -1) {
4752                 /* migration is in progress,
4753                  * but migr_rec cannot be loaded,
4754                  */
4755                 err = 4;
4756                 goto error;
4757         }
4758
4759         /* Check migration compatibility */
4760         if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
4761                 pr_err("Unsupported migration detected");
4762                 if (devname)
4763                         fprintf(stderr, " on %s\n", devname);
4764                 else
4765                         fprintf(stderr, " (IMSM).\n");
4766
4767                 err = 5;
4768                 goto error;
4769         }
4770
4771         err = 0;
4772
4773  error:
4774         while (super_list) {
4775                 struct intel_super *s = super_list;
4776
4777                 super_list = super_list->next;
4778                 free_imsm(s);
4779         }
4780
4781         if (err)
4782                 return err;
4783
4784         *sbp = super;
4785         if (fd >= 0)
4786                 strcpy(st->container_devnm, fd2devnm(fd));
4787         else
4788                 st->container_devnm[0] = 0;
4789         if (err == 0 && st->ss == NULL) {
4790                 st->ss = &super_imsm;
4791                 st->minor_version = 0;
4792                 st->max_devs = IMSM_MAX_DEVICES;
4793         }
4794         return 0;
4795 }
4796
4797 static int
4798 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
4799                         int *max, int keep_fd)
4800 {
4801         struct md_list *tmpdev;
4802         int err = 0;
4803         int i = 0;
4804
4805         for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
4806                 if (tmpdev->used != 1)
4807                         continue;
4808                 if (tmpdev->container == 1) {
4809                         int lmax = 0;
4810                         int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
4811                         if (fd < 0) {
4812                                 pr_err("cannot open device %s: %s\n",
4813                                         tmpdev->devname, strerror(errno));
4814                                 err = 8;
4815                                 goto error;
4816                         }
4817                         err = get_sra_super_block(fd, super_list,
4818                                                   tmpdev->devname, &lmax,
4819                                                   keep_fd);
4820                         i += lmax;
4821                         close(fd);
4822                         if (err) {
4823                                 err = 7;
4824                                 goto error;
4825                         }
4826                 } else {
4827                         int major = major(tmpdev->st_rdev);
4828                         int minor = minor(tmpdev->st_rdev);
4829                         err = get_super_block(super_list,
4830                                               NULL,
4831                                               tmpdev->devname,
4832                                               major, minor,
4833                                               keep_fd);
4834                         i++;
4835                         if (err) {
4836                                 err = 6;
4837                                 goto error;
4838                         }
4839                 }
4840         }
4841  error:
4842         *max = i;
4843         return err;
4844 }
4845
4846 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
4847                            int major, int minor, int keep_fd)
4848 {
4849         struct intel_super *s;
4850         char nm[32];
4851         int dfd = -1;
4852         int err = 0;
4853         int retry;
4854
4855         s = alloc_super();
4856         if (!s) {
4857                 err = 1;
4858                 goto error;
4859         }
4860
4861         sprintf(nm, "%d:%d", major, minor);
4862         dfd = dev_open(nm, O_RDWR);
4863         if (dfd < 0) {
4864                 err = 2;
4865                 goto error;
4866         }
4867
4868         get_dev_sector_size(dfd, NULL, &s->sector_size);
4869         find_intel_hba_capability(dfd, s, devname);
4870         err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
4871
4872         /* retry the load if we might have raced against mdmon */
4873         if (err == 3 && devnm && mdmon_running(devnm))
4874                 for (retry = 0; retry < 3; retry++) {
4875                         usleep(3000);
4876                         err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
4877                         if (err != 3)
4878                                 break;
4879                 }
4880  error:
4881         if (!err) {
4882                 s->next = *super_list;
4883                 *super_list = s;
4884         } else {
4885                 if (s)
4886                         free_imsm(s);
4887                 if (dfd >= 0)
4888                         close(dfd);
4889         }
4890         if (dfd >= 0 && !keep_fd)
4891                 close(dfd);
4892         return err;
4893
4894 }
4895
4896 static int
4897 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
4898 {
4899         struct mdinfo *sra;
4900         char *devnm;
4901         struct mdinfo *sd;
4902         int err = 0;
4903         int i = 0;
4904         sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
4905         if (!sra)
4906                 return 1;
4907
4908         if (sra->array.major_version != -1 ||
4909             sra->array.minor_version != -2 ||
4910             strcmp(sra->text_version, "imsm") != 0) {
4911                 err = 1;
4912                 goto error;
4913         }
4914         /* load all mpbs */
4915         devnm = fd2devnm(fd);
4916         for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
4917                 if (get_super_block(super_list, devnm, devname,
4918                                     sd->disk.major, sd->disk.minor, keep_fd) != 0) {
4919                         err = 7;
4920                         goto error;
4921                 }
4922         }
4923  error:
4924         sysfs_free(sra);
4925         *max = i;
4926         return err;
4927 }
4928
4929 static int load_container_imsm(struct supertype *st, int fd, char *devname)
4930 {
4931         return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
4932 }
4933 #endif
4934
4935 static int load_super_imsm(struct supertype *st, int fd, char *devname)
4936 {
4937         struct intel_super *super;
4938         int rv;
4939         int retry;
4940
4941         if (test_partition(fd))
4942                 /* IMSM not allowed on partitions */
4943                 return 1;
4944
4945         free_super_imsm(st);
4946
4947         super = alloc_super();
4948         get_dev_sector_size(fd, NULL, &super->sector_size);
4949         if (!super)
4950                 return 1;
4951         /* Load hba and capabilities if they exist.
4952          * But do not preclude loading metadata in case capabilities or hba are
4953          * non-compliant and ignore_hw_compat is set.
4954          */
4955         rv = find_intel_hba_capability(fd, super, devname);
4956         /* no orom/efi or non-intel hba of the disk */
4957         if (rv != 0 && st->ignore_hw_compat == 0) {
4958                 if (devname)
4959                         pr_err("No OROM/EFI properties for %s\n", devname);
4960                 free_imsm(super);
4961                 return 2;
4962         }
4963         rv = load_and_parse_mpb(fd, super, devname, 0);
4964
4965         /* retry the load if we might have raced against mdmon */
4966         if (rv == 3) {
4967                 struct mdstat_ent *mdstat = NULL;
4968                 char *name = fd2kname(fd);
4969
4970                 if (name)
4971                         mdstat = mdstat_by_component(name);
4972
4973                 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
4974                         for (retry = 0; retry < 3; retry++) {
4975                                 usleep(3000);
4976                                 rv = load_and_parse_mpb(fd, super, devname, 0);
4977                                 if (rv != 3)
4978                                         break;
4979                         }
4980                 }
4981
4982                 free_mdstat(mdstat);
4983         }
4984
4985         if (rv) {
4986                 if (devname)
4987                         pr_err("Failed to load all information sections on %s\n", devname);
4988                 free_imsm(super);
4989                 return rv;
4990         }
4991
4992         st->sb = super;
4993         if (st->ss == NULL) {
4994                 st->ss = &super_imsm;
4995                 st->minor_version = 0;
4996                 st->max_devs = IMSM_MAX_DEVICES;
4997         }
4998
4999         /* load migration record */
5000         if (load_imsm_migr_rec(super, NULL) == 0) {
5001                 /* Check for unsupported migration features */
5002                 if (check_mpb_migr_compatibility(super) != 0) {
5003                         pr_err("Unsupported migration detected");
5004                         if (devname)
5005                                 fprintf(stderr, " on %s\n", devname);
5006                         else
5007                                 fprintf(stderr, " (IMSM).\n");
5008                         return 3;
5009                 }
5010         }
5011
5012         return 0;
5013 }
5014
5015 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5016 {
5017         if (info->level == 1)
5018                 return 128;
5019         return info->chunk_size >> 9;
5020 }
5021
5022 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5023                                                     unsigned long long size)
5024 {
5025         if (info->level == 1)
5026                 return size * 2;
5027         else
5028                 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5029 }
5030
5031 static void imsm_update_version_info(struct intel_super *super)
5032 {
5033         /* update the version and attributes */
5034         struct imsm_super *mpb = super->anchor;
5035         char *version;
5036         struct imsm_dev *dev;
5037         struct imsm_map *map;
5038         int i;
5039
5040         for (i = 0; i < mpb->num_raid_devs; i++) {
5041                 dev = get_imsm_dev(super, i);
5042                 map = get_imsm_map(dev, MAP_0);
5043                 if (__le32_to_cpu(dev->size_high) > 0)
5044                         mpb->attributes |= MPB_ATTRIB_2TB;
5045
5046                 /* FIXME detect when an array spans a port multiplier */
5047                 #if 0
5048                 mpb->attributes |= MPB_ATTRIB_PM;
5049                 #endif
5050
5051                 if (mpb->num_raid_devs > 1 ||
5052                     mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5053                         version = MPB_VERSION_ATTRIBS;
5054                         switch (get_imsm_raid_level(map)) {
5055                         case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5056                         case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5057                         case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5058                         case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5059                         }
5060                 } else {
5061                         if (map->num_members >= 5)
5062                                 version = MPB_VERSION_5OR6_DISK_ARRAY;
5063                         else if (dev->status == DEV_CLONE_N_GO)
5064                                 version = MPB_VERSION_CNG;
5065                         else if (get_imsm_raid_level(map) == 5)
5066                                 version = MPB_VERSION_RAID5;
5067                         else if (map->num_members >= 3)
5068                                 version = MPB_VERSION_3OR4_DISK_ARRAY;
5069                         else if (get_imsm_raid_level(map) == 1)
5070                                 version = MPB_VERSION_RAID1;
5071                         else
5072                                 version = MPB_VERSION_RAID0;
5073                 }
5074                 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5075         }
5076 }
5077
5078 static int check_name(struct intel_super *super, char *name, int quiet)
5079 {
5080         struct imsm_super *mpb = super->anchor;
5081         char *reason = NULL;
5082         int i;
5083
5084         if (strlen(name) > MAX_RAID_SERIAL_LEN)
5085                 reason = "must be 16 characters or less";
5086
5087         for (i = 0; i < mpb->num_raid_devs; i++) {
5088                 struct imsm_dev *dev = get_imsm_dev(super, i);
5089
5090                 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5091                         reason = "already exists";
5092                         break;
5093                 }
5094         }
5095
5096         if (reason && !quiet)
5097                 pr_err("imsm volume name %s\n", reason);
5098
5099         return !reason;
5100 }
5101
5102 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5103                                   unsigned long long size, char *name,
5104                                   char *homehost, int *uuid,
5105                                   long long data_offset)
5106 {
5107         /* We are creating a volume inside a pre-existing container.
5108          * so st->sb is already set.
5109          */
5110         struct intel_super *super = st->sb;
5111         unsigned int sector_size = super->sector_size;
5112         struct imsm_super *mpb = super->anchor;
5113         struct intel_dev *dv;
5114         struct imsm_dev *dev;
5115         struct imsm_vol *vol;
5116         struct imsm_map *map;
5117         int idx = mpb->num_raid_devs;
5118         int i;
5119         unsigned long long array_blocks;
5120         size_t size_old, size_new;
5121         unsigned long long num_data_stripes;
5122
5123         if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5124                 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5125                 return 0;
5126         }
5127
5128         /* ensure the mpb is large enough for the new data */
5129         size_old = __le32_to_cpu(mpb->mpb_size);
5130         size_new = disks_to_mpb_size(info->nr_disks);
5131         if (size_new > size_old) {
5132                 void *mpb_new;
5133                 size_t size_round = ROUND_UP(size_new, sector_size);
5134
5135                 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5136                         pr_err("could not allocate new mpb\n");
5137                         return 0;
5138                 }
5139                 if (posix_memalign(&super->migr_rec_buf, sector_size,
5140                                    MIGR_REC_BUF_SECTORS*sector_size) != 0) {
5141                         pr_err("could not allocate migr_rec buffer\n");
5142                         free(super->buf);
5143                         free(super);
5144                         free(mpb_new);
5145                         return 0;
5146                 }
5147                 memcpy(mpb_new, mpb, size_old);
5148                 free(mpb);
5149                 mpb = mpb_new;
5150                 super->anchor = mpb_new;
5151                 mpb->mpb_size = __cpu_to_le32(size_new);
5152                 memset(mpb_new + size_old, 0, size_round - size_old);
5153                 super->len = size_round;
5154         }
5155         super->current_vol = idx;
5156
5157         /* handle 'failed_disks' by either:
5158          * a) create dummy disk entries in the table if this the first
5159          *    volume in the array.  We add them here as this is the only
5160          *    opportunity to add them. add_to_super_imsm_volume()
5161          *    handles the non-failed disks and continues incrementing
5162          *    mpb->num_disks.
5163          * b) validate that 'failed_disks' matches the current number
5164          *    of missing disks if the container is populated
5165          */
5166         if (super->current_vol == 0) {
5167                 mpb->num_disks = 0;
5168                 for (i = 0; i < info->failed_disks; i++) {
5169                         struct imsm_disk *disk;
5170
5171                         mpb->num_disks++;
5172                         disk = __get_imsm_disk(mpb, i);
5173                         disk->status = CONFIGURED_DISK | FAILED_DISK;
5174                         disk->scsi_id = __cpu_to_le32(~(__u32)0);
5175                         snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5176                                  "missing:%d", i);
5177                 }
5178                 find_missing(super);
5179         } else {
5180                 int missing = 0;
5181                 struct dl *d;
5182
5183                 for (d = super->missing; d; d = d->next)
5184                         missing++;
5185                 if (info->failed_disks > missing) {
5186                         pr_err("unable to add 'missing' disk to container\n");
5187                         return 0;
5188                 }
5189         }
5190
5191         if (!check_name(super, name, 0))
5192                 return 0;
5193         dv = xmalloc(sizeof(*dv));
5194         dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5195         strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
5196         array_blocks = calc_array_size(info->level, info->raid_disks,
5197                                                info->layout, info->chunk_size,
5198                                                size * 2);
5199         /* round array size down to closest MB */
5200         array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
5201
5202         dev->size_low = __cpu_to_le32((__u32) array_blocks);
5203         dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
5204         dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5205         vol = &dev->vol;
5206         vol->migr_state = 0;
5207         set_migr_type(dev, MIGR_INIT);
5208         vol->dirty = !info->state;
5209         vol->curr_migr_unit = 0;
5210         map = get_imsm_map(dev, MAP_0);
5211         set_pba_of_lba0(map, super->create_offset);
5212         set_blocks_per_member(map, info_to_blocks_per_member(info, size));
5213         map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5214         map->failed_disk_num = ~0;
5215         if (info->level > 0)
5216                 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5217                                   : IMSM_T_STATE_UNINITIALIZED);
5218         else
5219                 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5220                                                       IMSM_T_STATE_NORMAL;
5221         map->ddf = 1;
5222
5223         if (info->level == 1 && info->raid_disks > 2) {
5224                 free(dev);
5225                 free(dv);
5226                 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5227                 return 0;
5228         }
5229
5230         map->raid_level = info->level;
5231         if (info->level == 10) {
5232                 map->raid_level = 1;
5233                 map->num_domains = info->raid_disks / 2;
5234         } else if (info->level == 1)
5235                 map->num_domains = info->raid_disks;
5236         else
5237                 map->num_domains = 1;
5238
5239         /* info->size is only int so use the 'size' parameter instead */
5240         num_data_stripes = (size * 2) / info_to_blocks_per_strip(info);
5241         num_data_stripes /= map->num_domains;
5242         set_num_data_stripes(map, num_data_stripes);
5243
5244         map->num_members = info->raid_disks;
5245         for (i = 0; i < map->num_members; i++) {
5246                 /* initialized in add_to_super */
5247                 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5248         }
5249         mpb->num_raid_devs++;
5250
5251         dv->dev = dev;
5252         dv->index = super->current_vol;
5253         dv->next = super->devlist;
5254         super->devlist = dv;
5255
5256         imsm_update_version_info(super);
5257
5258         return 1;
5259 }
5260
5261 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5262                            unsigned long long size, char *name,
5263                            char *homehost, int *uuid,
5264                            unsigned long long data_offset)
5265 {
5266         /* This is primarily called by Create when creating a new array.
5267          * We will then get add_to_super called for each component, and then
5268          * write_init_super called to write it out to each device.
5269          * For IMSM, Create can create on fresh devices or on a pre-existing
5270          * array.
5271          * To create on a pre-existing array a different method will be called.
5272          * This one is just for fresh drives.
5273          */
5274         struct intel_super *super;
5275         struct imsm_super *mpb;
5276         size_t mpb_size;
5277         char *version;
5278
5279         if (data_offset != INVALID_SECTORS) {
5280                 pr_err("data-offset not supported by imsm\n");
5281                 return 0;
5282         }
5283
5284         if (st->sb)
5285                 return init_super_imsm_volume(st, info, size, name, homehost, uuid,
5286                                               data_offset);
5287
5288         if (info)
5289                 mpb_size = disks_to_mpb_size(info->nr_disks);
5290         else
5291                 mpb_size = MAX_SECTOR_SIZE;
5292
5293         super = alloc_super();
5294         if (super &&
5295             posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5296                 free_imsm(super);
5297                 super = NULL;
5298         }
5299         if (!super) {
5300                 pr_err("could not allocate superblock\n");
5301                 return 0;
5302         }
5303         if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5304             MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5305                 pr_err("could not allocate migr_rec buffer\n");
5306                 free(super->buf);
5307                 free_imsm(super);
5308                 return 0;
5309         }
5310         memset(super->buf, 0, mpb_size);
5311         mpb = super->buf;
5312         mpb->mpb_size = __cpu_to_le32(mpb_size);
5313         st->sb = super;
5314
5315         if (info == NULL) {
5316                 /* zeroing superblock */
5317                 return 0;
5318         }
5319
5320         mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5321
5322         version = (char *) mpb->sig;
5323         strcpy(version, MPB_SIGNATURE);
5324         version += strlen(MPB_SIGNATURE);
5325         strcpy(version, MPB_VERSION_RAID0);
5326
5327         return 1;
5328 }
5329
5330 #ifndef MDASSEMBLE
5331 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5332                                      int fd, char *devname)
5333 {
5334         struct intel_super *super = st->sb;
5335         struct imsm_super *mpb = super->anchor;
5336         struct imsm_disk *_disk;
5337         struct imsm_dev *dev;
5338         struct imsm_map *map;
5339         struct dl *dl, *df;
5340         int slot;
5341
5342         dev = get_imsm_dev(super, super->current_vol);
5343         map = get_imsm_map(dev, MAP_0);
5344
5345         if (! (dk->state & (1<<MD_DISK_SYNC))) {
5346                 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5347                         devname);
5348                 return 1;
5349         }
5350
5351         if (fd == -1) {
5352                 /* we're doing autolayout so grab the pre-marked (in
5353                  * validate_geometry) raid_disk
5354                  */
5355                 for (dl = super->disks; dl; dl = dl->next)
5356                         if (dl->raiddisk == dk->raid_disk)
5357                                 break;
5358         } else {
5359                 for (dl = super->disks; dl ; dl = dl->next)
5360                         if (dl->major == dk->major &&
5361                             dl->minor == dk->minor)
5362                                 break;
5363         }
5364
5365         if (!dl) {
5366                 pr_err("%s is not a member of the same container\n", devname);
5367                 return 1;
5368         }
5369
5370         /* add a pristine spare to the metadata */
5371         if (dl->index < 0) {
5372                 dl->index = super->anchor->num_disks;
5373                 super->anchor->num_disks++;
5374         }
5375         /* Check the device has not already been added */
5376         slot = get_imsm_disk_slot(map, dl->index);
5377         if (slot >= 0 &&
5378             (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5379                 pr_err("%s has been included in this array twice\n",
5380                         devname);
5381                 return 1;
5382         }
5383         set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5384         dl->disk.status = CONFIGURED_DISK;
5385
5386         /* update size of 'missing' disks to be at least as large as the
5387          * largest acitve member (we only have dummy missing disks when
5388          * creating the first volume)
5389          */
5390         if (super->current_vol == 0) {
5391                 for (df = super->missing; df; df = df->next) {
5392                         if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5393                                 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5394                         _disk = __get_imsm_disk(mpb, df->index);
5395                         *_disk = df->disk;
5396                 }
5397         }
5398
5399         /* refresh unset/failed slots to point to valid 'missing' entries */
5400         for (df = super->missing; df; df = df->next)
5401                 for (slot = 0; slot < mpb->num_disks; slot++) {
5402                         __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5403
5404                         if ((ord & IMSM_ORD_REBUILD) == 0)
5405                                 continue;
5406                         set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5407                         if (is_gen_migration(dev)) {
5408                                 struct imsm_map *map2 = get_imsm_map(dev,
5409                                                                      MAP_1);
5410                                 int slot2 = get_imsm_disk_slot(map2, df->index);
5411                                 if (slot2 < map2->num_members && slot2 >= 0) {
5412                                         __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5413                                                                          slot2,
5414                                                                          MAP_1);
5415                                         if ((unsigned)df->index ==
5416                                                                ord_to_idx(ord2))
5417                                                 set_imsm_ord_tbl_ent(map2,
5418                                                         slot2,
5419                                                         df->index |
5420                                                         IMSM_ORD_REBUILD);
5421                                 }
5422                         }
5423                         dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5424                         break;
5425                 }
5426
5427         /* if we are creating the first raid device update the family number */
5428         if (super->current_vol == 0) {
5429                 __u32 sum;
5430                 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5431
5432                 _disk = __get_imsm_disk(mpb, dl->index);
5433                 if (!_dev || !_disk) {
5434                         pr_err("BUG mpb setup error\n");
5435                         return 1;
5436                 }
5437                 *_dev = *dev;
5438                 *_disk = dl->disk;
5439                 sum = random32();
5440                 sum += __gen_imsm_checksum(mpb);
5441                 mpb->family_num = __cpu_to_le32(sum);
5442                 mpb->orig_family_num = mpb->family_num;
5443         }
5444         super->current_disk = dl;
5445         return 0;
5446 }
5447
5448 /* mark_spare()
5449  *   Function marks disk as spare and restores disk serial
5450  *   in case it was previously marked as failed by takeover operation
5451  * reruns:
5452  *   -1 : critical error
5453  *    0 : disk is marked as spare but serial is not set
5454  *    1 : success
5455  */
5456 int mark_spare(struct dl *disk)
5457 {
5458         __u8 serial[MAX_RAID_SERIAL_LEN];
5459         int ret_val = -1;
5460
5461         if (!disk)
5462                 return ret_val;
5463
5464         ret_val = 0;
5465         if (!imsm_read_serial(disk->fd, NULL, serial)) {
5466                 /* Restore disk serial number, because takeover marks disk
5467                  * as failed and adds to serial ':0' before it becomes
5468                  * a spare disk.
5469                  */
5470                 serialcpy(disk->serial, serial);
5471                 serialcpy(disk->disk.serial, serial);
5472                 ret_val = 1;
5473         }
5474         disk->disk.status = SPARE_DISK;
5475         disk->index = -1;
5476
5477         return ret_val;
5478 }
5479
5480 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5481                              int fd, char *devname,
5482                              unsigned long long data_offset)
5483 {
5484         struct intel_super *super = st->sb;
5485         struct dl *dd;
5486         unsigned long long size;
5487         unsigned int member_sector_size;
5488         __u32 id;
5489         int rv;
5490         struct stat stb;
5491
5492         /* If we are on an RAID enabled platform check that the disk is
5493          * attached to the raid controller.
5494          * We do not need to test disks attachment for container based additions,
5495          * they shall be already tested when container was created/assembled.
5496          */
5497         rv = find_intel_hba_capability(fd, super, devname);
5498         /* no orom/efi or non-intel hba of the disk */
5499         if (rv != 0) {
5500                 dprintf("capability: %p fd: %d ret: %d\n",
5501                         super->orom, fd, rv);
5502                 return 1;
5503         }
5504
5505         if (super->current_vol >= 0)
5506                 return add_to_super_imsm_volume(st, dk, fd, devname);
5507
5508         fstat(fd, &stb);
5509         dd = xcalloc(sizeof(*dd), 1);
5510         dd->major = major(stb.st_rdev);
5511         dd->minor = minor(stb.st_rdev);
5512         dd->devname = devname ? xstrdup(devname) : NULL;
5513         dd->fd = fd;
5514         dd->e = NULL;
5515         dd->action = DISK_ADD;
5516         rv = imsm_read_serial(fd, devname, dd->serial);
5517         if (rv) {
5518                 pr_err("failed to retrieve scsi serial, aborting\n");
5519                 if (dd->devname)
5520                         free(dd->devname);
5521                 free(dd);
5522                 abort();
5523         }
5524         if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
5525            (super->hba->type == SYS_DEV_VMD))) {
5526                 int i;
5527                 char *devpath = diskfd_to_devpath(fd);
5528                 char controller_path[PATH_MAX];
5529
5530                 if (!devpath) {
5531                         pr_err("failed to get devpath, aborting\n");
5532                         if (dd->devname)
5533                                 free(dd->devname);
5534                         free(dd);
5535                         return 1;
5536                 }
5537
5538                 snprintf(controller_path, PATH_MAX-1, "%s/device", devpath);
5539                 free(devpath);
5540
5541                 if (devpath_to_vendor(controller_path) == 0x8086) {
5542                         /*
5543                          * If Intel's NVMe drive has serial ended with
5544                          * "-A","-B","-1" or "-2" it means that this is "x8"
5545                          * device (double drive on single PCIe card).
5546                          * User should be warned about potential data loss.
5547                          */
5548                         for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
5549                                 /* Skip empty character at the end */
5550                                 if (dd->serial[i] == 0)
5551                                         continue;
5552
5553                                 if (((dd->serial[i] == 'A') ||
5554                                    (dd->serial[i] == 'B') ||
5555                                    (dd->serial[i] == '1') ||
5556                                    (dd->serial[i] == '2')) &&
5557                                    (dd->serial[i-1] == '-'))
5558                                         pr_err("\tThe action you are about to take may put your data at risk.\n"
5559                                                 "\tPlease note that x8 devices may consist of two separate x4 devices "
5560                                                 "located on a single PCIe port.\n"
5561                                                 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5562                                 break;
5563                         }
5564                 }
5565         }
5566
5567         get_dev_size(fd, NULL, &size);
5568         get_dev_sector_size(fd, NULL, &member_sector_size);
5569
5570         if (super->sector_size == 0) {
5571                 /* this a first device, so sector_size is not set yet */
5572                 super->sector_size = member_sector_size;
5573         } else if (member_sector_size != super->sector_size) {
5574                 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5575                 if (dd->devname)
5576                         free(dd->devname);
5577                 free(dd);
5578                 return 1;
5579         }
5580
5581         /* clear migr_rec when adding disk to container */
5582         memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*super->sector_size);
5583         if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*super->sector_size,
5584             SEEK_SET) >= 0) {
5585                 if (write(fd, super->migr_rec_buf,
5586                     MIGR_REC_BUF_SECTORS*super->sector_size) !=
5587                     MIGR_REC_BUF_SECTORS*super->sector_size)
5588                         perror("Write migr_rec failed");
5589         }
5590
5591         size /= 512;
5592         serialcpy(dd->disk.serial, dd->serial);
5593         set_total_blocks(&dd->disk, size);
5594         if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
5595                 struct imsm_super *mpb = super->anchor;
5596                 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
5597         }
5598         mark_spare(dd);
5599         if (sysfs_disk_to_scsi_id(fd, &id) == 0)
5600                 dd->disk.scsi_id = __cpu_to_le32(id);
5601         else
5602                 dd->disk.scsi_id = __cpu_to_le32(0);
5603
5604         if (st->update_tail) {
5605                 dd->next = super->disk_mgmt_list;
5606                 super->disk_mgmt_list = dd;
5607         } else {
5608                 dd->next = super->disks;
5609                 super->disks = dd;
5610                 super->updates_pending++;
5611         }
5612
5613         return 0;
5614 }
5615
5616 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
5617 {
5618         struct intel_super *super = st->sb;
5619         struct dl *dd;
5620
5621         /* remove from super works only in mdmon - for communication
5622          * manager - monitor. Check if communication memory buffer
5623          * is prepared.
5624          */
5625         if (!st->update_tail) {
5626                 pr_err("shall be used in mdmon context only\n");
5627                 return 1;
5628         }
5629         dd = xcalloc(1, sizeof(*dd));
5630         dd->major = dk->major;
5631         dd->minor = dk->minor;
5632         dd->fd = -1;
5633         mark_spare(dd);
5634         dd->action = DISK_REMOVE;
5635
5636         dd->next = super->disk_mgmt_list;
5637         super->disk_mgmt_list = dd;
5638
5639         return 0;
5640 }
5641
5642 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
5643
5644 static union {
5645         char buf[MAX_SECTOR_SIZE];
5646         struct imsm_super anchor;
5647 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
5648
5649 /* spare records have their own family number and do not have any defined raid
5650  * devices
5651  */
5652 static int write_super_imsm_spares(struct intel_super *super, int doclose)
5653 {
5654         struct imsm_super *mpb = super->anchor;
5655         struct imsm_super *spare = &spare_record.anchor;
5656         __u32 sum;
5657         struct dl *d;
5658
5659         spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
5660         spare->generation_num = __cpu_to_le32(1UL);
5661         spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5662         spare->num_disks = 1;
5663         spare->num_raid_devs = 0;
5664         spare->cache_size = mpb->cache_size;
5665         spare->pwr_cycle_count = __cpu_to_le32(1);
5666
5667         snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
5668                  MPB_SIGNATURE MPB_VERSION_RAID0);
5669
5670         for (d = super->disks; d; d = d->next) {
5671                 if (d->index != -1)
5672                         continue;
5673
5674                 spare->disk[0] = d->disk;
5675                 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
5676                         spare->attributes |= MPB_ATTRIB_2TB_DISK;
5677
5678                 if (super->sector_size == 4096)
5679                         convert_to_4k_imsm_disk(&spare->disk[0]);
5680
5681                 sum = __gen_imsm_checksum(spare);
5682                 spare->family_num = __cpu_to_le32(sum);
5683                 spare->orig_family_num = 0;
5684                 sum = __gen_imsm_checksum(spare);
5685                 spare->check_sum = __cpu_to_le32(sum);
5686
5687                 if (store_imsm_mpb(d->fd, spare)) {
5688                         pr_err("failed for device %d:%d %s\n",
5689                                 d->major, d->minor, strerror(errno));
5690                         return 1;
5691                 }
5692                 if (doclose) {
5693                         close(d->fd);
5694                         d->fd = -1;
5695                 }
5696         }
5697
5698         return 0;
5699 }
5700
5701 static int write_super_imsm(struct supertype *st, int doclose)
5702 {
5703         struct intel_super *super = st->sb;
5704         unsigned int sector_size = super->sector_size;
5705         struct imsm_super *mpb = super->anchor;
5706         struct dl *d;
5707         __u32 generation;
5708         __u32 sum;
5709         int spares = 0;
5710         int i;
5711         __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
5712         int num_disks = 0;
5713         int clear_migration_record = 1;
5714         __u32 bbm_log_size;
5715
5716         /* 'generation' is incremented everytime the metadata is written */
5717         generation = __le32_to_cpu(mpb->generation_num);
5718         generation++;
5719         mpb->generation_num = __cpu_to_le32(generation);
5720
5721         /* fix up cases where previous mdadm releases failed to set
5722          * orig_family_num
5723          */
5724         if (mpb->orig_family_num == 0)
5725                 mpb->orig_family_num = mpb->family_num;
5726
5727         for (d = super->disks; d; d = d->next) {
5728                 if (d->index == -1)
5729                         spares++;
5730                 else {
5731                         mpb->disk[d->index] = d->disk;
5732                         num_disks++;
5733                 }
5734         }
5735         for (d = super->missing; d; d = d->next) {
5736                 mpb->disk[d->index] = d->disk;
5737                 num_disks++;
5738         }
5739         mpb->num_disks = num_disks;
5740         mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
5741
5742         for (i = 0; i < mpb->num_raid_devs; i++) {
5743                 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
5744                 struct imsm_dev *dev2 = get_imsm_dev(super, i);
5745                 if (dev && dev2) {
5746                         imsm_copy_dev(dev, dev2);
5747                         mpb_size += sizeof_imsm_dev(dev, 0);
5748                 }
5749                 if (is_gen_migration(dev2))
5750                         clear_migration_record = 0;
5751         }
5752
5753         bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
5754
5755         if (bbm_log_size) {
5756                 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
5757                 mpb->attributes |= MPB_ATTRIB_BBM;
5758         } else
5759                 mpb->attributes &= ~MPB_ATTRIB_BBM;
5760
5761         super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
5762         mpb_size += bbm_log_size;
5763         mpb->mpb_size = __cpu_to_le32(mpb_size);
5764
5765 #ifdef DEBUG
5766         assert(super->len == 0 || mpb_size <= super->len);
5767 #endif
5768
5769         /* recalculate checksum */
5770         sum = __gen_imsm_checksum(mpb);
5771         mpb->check_sum = __cpu_to_le32(sum);
5772
5773         if (super->clean_migration_record_by_mdmon) {
5774                 clear_migration_record = 1;
5775                 super->clean_migration_record_by_mdmon = 0;
5776         }
5777         if (clear_migration_record)
5778                 memset(super->migr_rec_buf, 0,
5779                     MIGR_REC_BUF_SECTORS*sector_size);
5780
5781         if (sector_size == 4096)
5782                 convert_to_4k(super);
5783
5784         /* write the mpb for disks that compose raid devices */
5785         for (d = super->disks; d ; d = d->next) {
5786                 if (d->index < 0 || is_failed(&d->disk))
5787                         continue;
5788
5789                 if (clear_migration_record) {
5790                         unsigned long long dsize;
5791
5792                         get_dev_size(d->fd, NULL, &dsize);
5793                         if (lseek64(d->fd, dsize - sector_size,
5794                             SEEK_SET) >= 0) {
5795                                 if (write(d->fd, super->migr_rec_buf,
5796                                     MIGR_REC_BUF_SECTORS*sector_size) !=
5797                                     MIGR_REC_BUF_SECTORS*sector_size)
5798                                         perror("Write migr_rec failed");
5799                         }
5800                 }
5801
5802                 if (store_imsm_mpb(d->fd, mpb))
5803                         fprintf(stderr,
5804                                 "failed for device %d:%d (fd: %d)%s\n",
5805                                 d->major, d->minor,
5806                                 d->fd, strerror(errno));
5807
5808                 if (doclose) {
5809                         close(d->fd);
5810                         d->fd = -1;
5811                 }
5812         }
5813
5814         if (spares)
5815                 return write_super_imsm_spares(super, doclose);
5816
5817         return 0;
5818 }
5819
5820 static int create_array(struct supertype *st, int dev_idx)
5821 {
5822         size_t len;
5823         struct imsm_update_create_array *u;
5824         struct intel_super *super = st->sb;
5825         struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
5826         struct imsm_map *map = get_imsm_map(dev, MAP_0);
5827         struct disk_info *inf;
5828         struct imsm_disk *disk;
5829         int i;
5830
5831         len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
5832               sizeof(*inf) * map->num_members;
5833         u = xmalloc(len);
5834         u->type = update_create_array;
5835         u->dev_idx = dev_idx;
5836         imsm_copy_dev(&u->dev, dev);
5837         inf = get_disk_info(u);
5838         for (i = 0; i < map->num_members; i++) {
5839                 int idx = get_imsm_disk_idx(dev, i, MAP_X);
5840
5841                 disk = get_imsm_disk(super, idx);
5842                 if (!disk)
5843                         disk = get_imsm_missing(super, idx);
5844                 serialcpy(inf[i].serial, disk->serial);
5845         }
5846         append_metadata_update(st, u, len);
5847
5848         return 0;
5849 }
5850
5851 static int mgmt_disk(struct supertype *st)
5852 {
5853         struct intel_super *super = st->sb;
5854         size_t len;
5855         struct imsm_update_add_remove_disk *u;
5856
5857         if (!super->disk_mgmt_list)
5858                 return 0;
5859
5860         len = sizeof(*u);
5861         u = xmalloc(len);
5862         u->type = update_add_remove_disk;
5863         append_metadata_update(st, u, len);
5864
5865         return 0;
5866 }
5867
5868 static int write_init_super_imsm(struct supertype *st)
5869 {
5870         struct intel_super *super = st->sb;
5871         int current_vol = super->current_vol;
5872
5873         /* we are done with current_vol reset it to point st at the container */
5874         super->current_vol = -1;
5875
5876         if (st->update_tail) {
5877                 /* queue the recently created array / added disk
5878                  * as a metadata update */
5879                 int rv;
5880
5881                 /* determine if we are creating a volume or adding a disk */
5882                 if (current_vol < 0) {
5883                         /* in the mgmt (add/remove) disk case we are running
5884                          * in mdmon context, so don't close fd's
5885                          */
5886                         return mgmt_disk(st);
5887                 } else
5888                         rv = create_array(st, current_vol);
5889
5890                 return rv;
5891         } else {
5892                 struct dl *d;
5893                 for (d = super->disks; d; d = d->next)
5894                         Kill(d->devname, NULL, 0, -1, 1);
5895                 return write_super_imsm(st, 1);
5896         }
5897 }
5898 #endif
5899
5900 static int store_super_imsm(struct supertype *st, int fd)
5901 {
5902         struct intel_super *super = st->sb;
5903         struct imsm_super *mpb = super ? super->anchor : NULL;
5904
5905         if (!mpb)
5906                 return 1;
5907
5908 #ifndef MDASSEMBLE
5909         if (super->sector_size == 4096)
5910                 convert_to_4k(super);
5911         return store_imsm_mpb(fd, mpb);
5912 #else
5913         return 1;
5914 #endif
5915 }
5916
5917 #ifndef MDASSEMBLE
5918 static int validate_geometry_imsm_container(struct supertype *st, int level,
5919                                             int layout, int raiddisks, int chunk,
5920                                             unsigned long long size,
5921                                             unsigned long long data_offset,
5922                                             char *dev,
5923                                             unsigned long long *freesize,
5924                                             int verbose)
5925 {
5926         int fd;
5927         unsigned long long ldsize;
5928         struct intel_super *super;
5929         int rv = 0;
5930
5931         if (level != LEVEL_CONTAINER)
5932                 return 0;
5933         if (!dev)
5934                 return 1;
5935
5936         fd = open(dev, O_RDONLY|O_EXCL, 0);
5937         if (fd < 0) {
5938                 if (verbose > 0)
5939                         pr_err("imsm: Cannot open %s: %s\n",
5940                                 dev, strerror(errno));
5941                 return 0;
5942         }
5943         if (!get_dev_size(fd, dev, &ldsize)) {
5944                 close(fd);
5945                 return 0;
5946         }
5947
5948         /* capabilities retrieve could be possible
5949          * note that there is no fd for the disks in array.
5950          */
5951         super = alloc_super();
5952         if (!super) {
5953                 close(fd);
5954                 return 0;
5955         }
5956         if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5957                 close(fd);
5958                 free_imsm(super);
5959                 return 0;
5960         }
5961
5962         rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
5963         if (rv != 0) {
5964 #if DEBUG
5965                 char str[256];
5966                 fd2devname(fd, str);
5967                 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5968                         fd, str, super->orom, rv, raiddisks);
5969 #endif
5970                 /* no orom/efi or non-intel hba of the disk */
5971                 close(fd);
5972                 free_imsm(super);
5973                 return 0;
5974         }
5975         close(fd);
5976         if (super->orom) {
5977                 if (raiddisks > super->orom->tds) {
5978                         if (verbose)
5979                                 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5980                                         raiddisks, super->orom->tds);
5981                         free_imsm(super);
5982                         return 0;
5983                 }
5984                 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
5985                     (ldsize >> 9) >> 32 > 0) {
5986                         if (verbose)
5987                                 pr_err("%s exceeds maximum platform supported size\n", dev);
5988                         free_imsm(super);
5989                         return 0;
5990                 }
5991         }
5992
5993         *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
5994         free_imsm(super);
5995
5996         return 1;
5997 }
5998
5999 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6000 {
6001         const unsigned long long base_start = e[*idx].start;
6002         unsigned long long end = base_start + e[*idx].size;
6003         int i;
6004
6005         if (base_start == end)
6006                 return 0;
6007
6008         *idx = *idx + 1;
6009         for (i = *idx; i < num_extents; i++) {
6010                 /* extend overlapping extents */
6011                 if (e[i].start >= base_start &&
6012                     e[i].start <= end) {
6013                         if (e[i].size == 0)
6014                                 return 0;
6015                         if (e[i].start + e[i].size > end)
6016                                 end = e[i].start + e[i].size;
6017                 } else if (e[i].start > end) {
6018                         *idx = i;
6019                         break;
6020                 }
6021         }
6022
6023         return end - base_start;
6024 }
6025
6026 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
6027 {
6028         /* build a composite disk with all known extents and generate a new
6029          * 'maxsize' given the "all disks in an array must share a common start
6030          * offset" constraint
6031          */
6032         struct extent *e = xcalloc(sum_extents, sizeof(*e));
6033         struct dl *dl;
6034         int i, j;
6035         int start_extent;
6036         unsigned long long pos;
6037         unsigned long long start = 0;
6038         unsigned long long maxsize;
6039         unsigned long reserve;
6040
6041         /* coalesce and sort all extents. also, check to see if we need to
6042          * reserve space between member arrays
6043          */
6044         j = 0;
6045         for (dl = super->disks; dl; dl = dl->next) {
6046                 if (!dl->e)
6047                         continue;
6048                 for (i = 0; i < dl->extent_cnt; i++)
6049                         e[j++] = dl->e[i];
6050         }
6051         qsort(e, sum_extents, sizeof(*e), cmp_extent);
6052
6053         /* merge extents */
6054         i = 0;
6055         j = 0;
6056         while (i < sum_extents) {
6057                 e[j].start = e[i].start;
6058                 e[j].size = find_size(e, &i, sum_extents);
6059                 j++;
6060                 if (e[j-1].size == 0)
6061                         break;
6062         }
6063
6064         pos = 0;
6065         maxsize = 0;
6066         start_extent = 0;
6067         i = 0;
6068         do {
6069                 unsigned long long esize;
6070
6071                 esize = e[i].start - pos;
6072                 if (esize >= maxsize) {
6073                         maxsize = esize;
6074                         start = pos;
6075                         start_extent = i;
6076                 }
6077                 pos = e[i].start + e[i].size;
6078                 i++;
6079         } while (e[i-1].size);
6080         free(e);
6081
6082         if (maxsize == 0)
6083                 return 0;
6084
6085         /* FIXME assumes volume at offset 0 is the first volume in a
6086          * container
6087          */
6088         if (start_extent > 0)
6089                 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
6090         else
6091                 reserve = 0;
6092
6093         if (maxsize < reserve)
6094                 return 0;
6095
6096         super->create_offset = ~((unsigned long long) 0);
6097         if (start + reserve > super->create_offset)
6098                 return 0; /* start overflows create_offset */
6099         super->create_offset = start + reserve;
6100
6101         return maxsize - reserve;
6102 }
6103
6104 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6105 {
6106         if (level < 0 || level == 6 || level == 4)
6107                 return 0;
6108
6109         /* if we have an orom prevent invalid raid levels */
6110         if (orom)
6111                 switch (level) {
6112                 case 0: return imsm_orom_has_raid0(orom);
6113                 case 1:
6114                         if (raiddisks > 2)
6115                                 return imsm_orom_has_raid1e(orom);
6116                         return imsm_orom_has_raid1(orom) && raiddisks == 2;
6117                 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6118                 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6119                 }
6120         else
6121                 return 1; /* not on an Intel RAID platform so anything goes */
6122
6123         return 0;
6124 }
6125
6126 static int
6127 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6128                         int dpa, int verbose)
6129 {
6130         struct mdstat_ent *mdstat = mdstat_read(0, 0);
6131         struct mdstat_ent *memb;
6132         int count = 0;
6133         int num = 0;
6134         struct md_list *dv;
6135         int found;
6136
6137         for (memb = mdstat ; memb ; memb = memb->next) {
6138                 if (memb->metadata_version &&
6139                     (strncmp(memb->metadata_version, "external:", 9) == 0)  &&
6140                     (strcmp(&memb->metadata_version[9], name) == 0) &&
6141                     !is_subarray(memb->metadata_version+9) &&
6142                     memb->members) {
6143                         struct dev_member *dev = memb->members;
6144                         int fd = -1;
6145                         while(dev && (fd < 0)) {
6146                                 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6147                                 num = sprintf(path, "%s%s", "/dev/", dev->name);
6148                                 if (num > 0)
6149                                         fd = open(path, O_RDONLY, 0);
6150                                 if (num <= 0 || fd < 0) {
6151                                         pr_vrb("Cannot open %s: %s\n",
6152                                                dev->name, strerror(errno));
6153                                 }
6154                                 free(path);
6155                                 dev = dev->next;
6156                         }
6157                         found = 0;
6158                         if (fd >= 0 && disk_attached_to_hba(fd, hba)) {
6159                                 struct mdstat_ent *vol;
6160                                 for (vol = mdstat ; vol ; vol = vol->next) {
6161                                         if (vol->active > 0 &&
6162                                             vol->metadata_version &&
6163                                             is_container_member(vol, memb->devnm)) {
6164                                                 found++;
6165                                                 count++;
6166                                         }
6167                                 }
6168                                 if (*devlist && (found < dpa)) {
6169                                         dv = xcalloc(1, sizeof(*dv));
6170                                         dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
6171                                         sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
6172                                         dv->found = found;
6173                                         dv->used = 0;
6174                                         dv->next = *devlist;
6175                                         *devlist = dv;
6176                                 }
6177                         }
6178                         if (fd >= 0)
6179                                 close(fd);
6180                 }
6181         }
6182         free_mdstat(mdstat);
6183         return count;
6184 }
6185
6186 #ifdef DEBUG_LOOP
6187 static struct md_list*
6188 get_loop_devices(void)
6189 {
6190         int i;
6191         struct md_list *devlist = NULL;
6192         struct md_list *dv;
6193
6194         for(i = 0; i < 12; i++) {
6195                 dv = xcalloc(1, sizeof(*dv));
6196                 dv->devname = xmalloc(40);
6197                 sprintf(dv->devname, "/dev/loop%d", i);
6198                 dv->next = devlist;
6199                 devlist = dv;
6200         }
6201         return devlist;
6202 }
6203 #endif
6204
6205 static struct md_list*
6206 get_devices(const char *hba_path)
6207 {
6208         struct md_list *devlist = NULL;
6209         struct md_list *dv;
6210         struct dirent *ent;
6211         DIR *dir;
6212         int err = 0;
6213
6214 #if DEBUG_LOOP
6215         devlist = get_loop_devices();
6216         return devlist;
6217 #endif
6218         /* scroll through /sys/dev/block looking for devices attached to
6219          * this hba
6220          */
6221         dir = opendir("/sys/dev/block");
6222         for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
6223                 int fd;
6224                 char buf[1024];
6225                 int major, minor;
6226                 char *path = NULL;
6227                 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
6228                         continue;
6229                 path = devt_to_devpath(makedev(major, minor));
6230                 if (!path)
6231                         continue;
6232                 if (!path_attached_to_hba(path, hba_path)) {
6233                         free(path);
6234                         path = NULL;
6235                         continue;
6236                 }
6237                 free(path);
6238                 path = NULL;
6239                 fd = dev_open(ent->d_name, O_RDONLY);
6240                 if (fd >= 0) {
6241                         fd2devname(fd, buf);
6242                         close(fd);
6243                 } else {
6244                         pr_err("cannot open device: %s\n",
6245                                 ent->d_name);
6246                         continue;
6247                 }
6248
6249                 dv = xcalloc(1, sizeof(*dv));
6250                 dv->devname = xstrdup(buf);
6251                 dv->next = devlist;
6252                 devlist = dv;
6253         }
6254         if (err) {
6255                 while(devlist) {
6256                         dv = devlist;
6257                         devlist = devlist->next;
6258                         free(dv->devname);
6259                         free(dv);
6260                 }
6261         }
6262         closedir(dir);
6263         return devlist;
6264 }
6265
6266 static int
6267 count_volumes_list(struct md_list *devlist, char *homehost,
6268                    int verbose, int *found)
6269 {
6270         struct md_list *tmpdev;
6271         int count = 0;
6272         struct supertype *st;
6273
6274         /* first walk the list of devices to find a consistent set
6275          * that match the criterea, if that is possible.
6276          * We flag the ones we like with 'used'.
6277          */
6278         *found = 0;
6279         st = match_metadata_desc_imsm("imsm");
6280         if (st == NULL) {
6281                 pr_vrb("cannot allocate memory for imsm supertype\n");
6282                 return 0;
6283         }
6284
6285         for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
6286                 char *devname = tmpdev->devname;
6287                 struct stat stb;
6288                 struct supertype *tst;
6289                 int dfd;
6290                 if (tmpdev->used > 1)
6291                         continue;
6292                 tst = dup_super(st);
6293                 if (tst == NULL) {
6294                         pr_vrb("cannot allocate memory for imsm supertype\n");
6295                         goto err_1;
6296                 }
6297                 tmpdev->container = 0;
6298                 dfd = dev_open(devname, O_RDONLY|O_EXCL);
6299                 if (dfd < 0) {
6300                         dprintf("cannot open device %s: %s\n",
6301                                 devname, strerror(errno));
6302                         tmpdev->used = 2;
6303                 } else if (fstat(dfd, &stb)< 0) {
6304                         /* Impossible! */
6305                         dprintf("fstat failed for %s: %s\n",
6306                                 devname, strerror(errno));
6307                         tmpdev->used = 2;
6308                 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
6309                         dprintf("%s is not a block device.\n",
6310                                 devname);
6311                         tmpdev->used = 2;
6312                 } else if (must_be_container(dfd)) {
6313                         struct supertype *cst;
6314                         cst = super_by_fd(dfd, NULL);
6315                         if (cst == NULL) {
6316                                 dprintf("cannot recognize container type %s\n",
6317                                         devname);
6318                                 tmpdev->used = 2;
6319                         } else if (tst->ss != st->ss) {
6320                                 dprintf("non-imsm container - ignore it: %s\n",
6321                                         devname);
6322                                 tmpdev->used = 2;
6323                         } else if (!tst->ss->load_container ||
6324                                    tst->ss->load_container(tst, dfd, NULL))
6325                                 tmpdev->used = 2;
6326                         else {
6327                                 tmpdev->container = 1;
6328                         }
6329                         if (cst)
6330                                 cst->ss->free_super(cst);
6331                 } else {
6332                         tmpdev->st_rdev = stb.st_rdev;
6333                         if (tst->ss->load_super(tst,dfd, NULL)) {
6334                                 dprintf("no RAID superblock on %s\n",
6335                                         devname);
6336                                 tmpdev->used = 2;
6337                         } else if (tst->ss->compare_super == NULL) {
6338                                 dprintf("Cannot assemble %s metadata on %s\n",
6339                                         tst->ss->name, devname);
6340                                 tmpdev->used = 2;
6341                         }
6342                 }
6343                 if (dfd >= 0)
6344                         close(dfd);
6345                 if (tmpdev->used == 2 || tmpdev->used == 4) {
6346                         /* Ignore unrecognised devices during auto-assembly */
6347                         goto loop;
6348                 }
6349                 else {
6350                         struct mdinfo info;
6351                         tst->ss->getinfo_super(tst, &info, NULL);
6352
6353                         if (st->minor_version == -1)
6354                                 st->minor_version = tst->minor_version;
6355
6356                         if (memcmp(info.uuid, uuid_zero,
6357                                    sizeof(int[4])) == 0) {
6358                                 /* this is a floating spare.  It cannot define
6359                                  * an array unless there are no more arrays of
6360                                  * this type to be found.  It can be included
6361                                  * in an array of this type though.
6362                                  */
6363                                 tmpdev->used = 3;
6364                                 goto loop;
6365                         }
6366
6367                         if (st->ss != tst->ss ||
6368                             st->minor_version != tst->minor_version ||
6369                             st->ss->compare_super(st, tst) != 0) {
6370                                 /* Some mismatch. If exactly one array matches this host,
6371                                  * we can resolve on that one.
6372                                  * Or, if we are auto assembling, we just ignore the second
6373                                  * for now.
6374                                  */
6375                                 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6376                                         devname);
6377                                 goto loop;
6378                         }
6379                         tmpdev->used = 1;
6380                         *found = 1;
6381                         dprintf("found: devname: %s\n", devname);
6382                 }
6383         loop:
6384                 if (tst)
6385                         tst->ss->free_super(tst);
6386         }
6387         if (*found != 0) {
6388                 int err;
6389                 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
6390                         struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
6391                         for (iter = head; iter; iter = iter->next) {
6392                                 dprintf("content->text_version: %s vol\n",
6393                                         iter->text_version);
6394                                 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
6395                                         /* do not assemble arrays with unsupported
6396                                            configurations */
6397                                         dprintf("Cannot activate member %s.\n",
6398                                                 iter->text_version);
6399                                 } else
6400                                         count++;
6401                         }
6402                         sysfs_free(head);
6403
6404                 } else {
6405                         dprintf("No valid super block on device list: err: %d %p\n",
6406                                 err, st->sb);
6407                 }
6408         } else {
6409                 dprintf("no more devices to examine\n");
6410         }
6411
6412         for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
6413                 if (tmpdev->used == 1 && tmpdev->found) {
6414                         if (count) {
6415                                 if (count < tmpdev->found)
6416                                         count = 0;
6417                                 else
6418                                         count -= tmpdev->found;
6419                         }
6420                 }
6421                 if (tmpdev->used == 1)
6422                         tmpdev->used = 4;
6423         }
6424         err_1:
6425         if (st)
6426                 st->ss->free_super(st);
6427         return count;
6428 }
6429
6430 static int
6431 count_volumes(struct intel_hba *hba, int dpa, int verbose)
6432 {
6433         struct sys_dev *idev, *intel_devices = find_intel_devices();
6434         int count = 0;
6435         const struct orom_entry *entry;
6436         struct devid_list *dv, *devid_list;
6437
6438         if (!hba || !hba->path)
6439                 return 0;
6440
6441         for (idev = intel_devices; idev; idev = idev->next) {
6442                 if (strstr(idev->path, hba->path))
6443                                 break;
6444         }
6445
6446         if (!idev || !idev->dev_id)
6447                 return 0;
6448
6449         entry = get_orom_entry_by_device_id(idev->dev_id);
6450
6451         if (!entry || !entry->devid_list)
6452                 return 0;
6453
6454         devid_list = entry->devid_list;
6455         for (dv = devid_list; dv; dv = dv->next) {
6456                 struct md_list *devlist;
6457                 struct sys_dev *device = device_by_id(dv->devid);
6458                 char *hba_path;
6459                 int found = 0;
6460
6461                 if (device)
6462                         hba_path = device->path;
6463                 else
6464                         return 0;
6465
6466                 devlist = get_devices(hba_path);
6467                 /* if no intel devices return zero volumes */
6468                 if (devlist == NULL)
6469                         return 0;
6470
6471                 count += active_arrays_by_format("imsm", hba_path, &devlist, dpa, verbose);
6472                 dprintf("path: %s active arrays: %d\n", hba_path, count);
6473                 if (devlist == NULL)
6474                         return 0;
6475                 do  {
6476                         found = 0;
6477                         count += count_volumes_list(devlist,
6478                                                         NULL,
6479                                                         verbose,
6480                                                         &found);
6481                         dprintf("found %d count: %d\n", found, count);
6482                 } while (found);
6483
6484                 dprintf("path: %s total number of volumes: %d\n", hba_path, count);
6485
6486                 while (devlist) {
6487                         struct md_list *dv = devlist;
6488                         devlist = devlist->next;
6489                         free(dv->devname);
6490                         free(dv);
6491                 }
6492         }
6493         return count;
6494 }
6495
6496 static int imsm_default_chunk(const struct imsm_orom *orom)
6497 {
6498         /* up to 512 if the plaform supports it, otherwise the platform max.
6499          * 128 if no platform detected
6500          */
6501         int fs = max(7, orom ? fls(orom->sss) : 0);
6502
6503         return min(512, (1 << fs));
6504 }
6505
6506 static int
6507 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
6508                             int raiddisks, int *chunk, unsigned long long size, int verbose)
6509 {
6510         /* check/set platform and metadata limits/defaults */
6511         if (super->orom && raiddisks > super->orom->dpa) {
6512                 pr_vrb("platform supports a maximum of %d disks per array\n",
6513                        super->orom->dpa);
6514                 return 0;
6515         }
6516
6517         /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6518         if (!is_raid_level_supported(super->orom, level, raiddisks)) {
6519                 pr_vrb("platform does not support raid%d with %d disk%s\n",
6520                         level, raiddisks, raiddisks > 1 ? "s" : "");
6521                 return 0;
6522         }
6523
6524         if (*chunk == 0 || *chunk == UnSet)
6525                 *chunk = imsm_default_chunk(super->orom);
6526
6527         if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
6528                 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
6529                 return 0;
6530         }
6531
6532         if (layout != imsm_level_to_layout(level)) {
6533                 if (level == 5)
6534                         pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6535                 else if (level == 10)
6536                         pr_vrb("imsm raid 10 only supports the n2 layout\n");
6537                 else
6538                         pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6539                                 layout, level);
6540                 return 0;
6541         }
6542
6543         if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
6544                         (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
6545                 pr_vrb("platform does not support a volume size over 2TB\n");
6546                 return 0;
6547         }
6548
6549         return 1;
6550 }
6551
6552 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6553  * FIX ME add ahci details
6554  */
6555 static int validate_geometry_imsm_volume(struct supertype *st, int level,
6556                                          int layout, int raiddisks, int *chunk,
6557                                          unsigned long long size,
6558                                          unsigned long long data_offset,
6559                                          char *dev,
6560                                          unsigned long long *freesize,
6561                                          int verbose)
6562 {
6563         struct stat stb;
6564         struct intel_super *super = st->sb;
6565         struct imsm_super *mpb;
6566         struct dl *dl;
6567         unsigned long long pos = 0;
6568         unsigned long long maxsize;
6569         struct extent *e;
6570         int i;
6571
6572         /* We must have the container info already read in. */
6573         if (!super)
6574                 return 0;
6575
6576         mpb = super->anchor;
6577
6578         if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
6579                 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6580                 return 0;
6581         }
6582         if (!dev) {
6583                 /* General test:  make sure there is space for
6584                  * 'raiddisks' device extents of size 'size' at a given
6585                  * offset
6586                  */
6587                 unsigned long long minsize = size;
6588                 unsigned long long start_offset = MaxSector;
6589                 int dcnt = 0;
6590                 if (minsize == 0)
6591                         minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
6592                 for (dl = super->disks; dl ; dl = dl->next) {
6593                         int found = 0;
6594
6595                         pos = 0;
6596                         i = 0;
6597                         e = get_extents(super, dl);
6598                         if (!e) continue;
6599                         do {
6600                                 unsigned long long esize;
6601                                 esize = e[i].start - pos;
6602                                 if (esize >= minsize)
6603                                         found = 1;
6604                                 if (found && start_offset == MaxSector) {
6605                                         start_offset = pos;
6606                                         break;
6607                                 } else if (found && pos != start_offset) {
6608                                         found = 0;
6609                                         break;
6610                                 }
6611                                 pos = e[i].start + e[i].size;
6612                                 i++;
6613                         } while (e[i-1].size);
6614                         if (found)
6615                                 dcnt++;
6616                         free(e);
6617                 }
6618                 if (dcnt < raiddisks) {
6619                         if (verbose)
6620                                 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6621                                         dcnt, raiddisks);
6622                         return 0;
6623                 }
6624                 return 1;
6625         }
6626
6627         /* This device must be a member of the set */
6628         if (stat(dev, &stb) < 0)
6629                 return 0;
6630         if ((S_IFMT & stb.st_mode) != S_IFBLK)
6631                 return 0;
6632         for (dl = super->disks ; dl ; dl = dl->next) {
6633                 if (dl->major == (int)major(stb.st_rdev) &&
6634                     dl->minor == (int)minor(stb.st_rdev))
6635                         break;
6636         }
6637         if (!dl) {
6638                 if (verbose)
6639                         pr_err("%s is not in the same imsm set\n", dev);
6640                 return 0;
6641         } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
6642                 /* If a volume is present then the current creation attempt
6643                  * cannot incorporate new spares because the orom may not
6644                  * understand this configuration (all member disks must be
6645                  * members of each array in the container).
6646                  */
6647                 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
6648                 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6649                 return 0;
6650         } else if (super->orom && mpb->num_raid_devs > 0 &&
6651                    mpb->num_disks != raiddisks) {
6652                 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6653                 return 0;
6654         }
6655
6656         /* retrieve the largest free space block */
6657         e = get_extents(super, dl);
6658         maxsize = 0;
6659         i = 0;
6660         if (e) {
6661                 do {
6662                         unsigned long long esize;
6663
6664                         esize = e[i].start - pos;
6665                         if (esize >= maxsize)
6666                                 maxsize = esize;
6667                         pos = e[i].start + e[i].size;
6668                         i++;
6669                 } while (e[i-1].size);
6670                 dl->e = e;
6671                 dl->extent_cnt = i;
6672         } else {
6673                 if (verbose)
6674                         pr_err("unable to determine free space for: %s\n",
6675                                 dev);
6676                 return 0;
6677         }
6678         if (maxsize < size) {
6679                 if (verbose)
6680                         pr_err("%s not enough space (%llu < %llu)\n",
6681                                 dev, maxsize, size);
6682                 return 0;
6683         }
6684
6685         /* count total number of extents for merge */
6686         i = 0;
6687         for (dl = super->disks; dl; dl = dl->next)
6688                 if (dl->e)
6689                         i += dl->extent_cnt;
6690
6691         maxsize = merge_extents(super, i);
6692
6693         if (!check_env("IMSM_NO_PLATFORM") &&
6694             mpb->num_raid_devs > 0 && size && size != maxsize) {
6695                 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6696                 return 0;
6697         }
6698
6699         if (maxsize < size || maxsize == 0) {
6700                 if (verbose) {
6701                         if (maxsize == 0)
6702                                 pr_err("no free space left on device. Aborting...\n");
6703                         else
6704                                 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6705                                                 maxsize, size);
6706                 }
6707                 return 0;
6708         }
6709
6710         *freesize = maxsize;
6711
6712         if (super->orom) {
6713                 int count = count_volumes(super->hba,
6714                                       super->orom->dpa, verbose);
6715                 if (super->orom->vphba <= count) {
6716                         pr_vrb("platform does not support more than %d raid volumes.\n",
6717                                super->orom->vphba);
6718                         return 0;
6719                 }
6720         }
6721         return 1;
6722 }
6723
6724 static int imsm_get_free_size(struct supertype *st, int raiddisks,
6725                          unsigned long long size, int chunk,
6726                          unsigned long long *freesize)
6727 {
6728         struct intel_super *super = st->sb;
6729         struct imsm_super *mpb = super->anchor;
6730         struct dl *dl;
6731         int i;
6732         int extent_cnt;
6733         struct extent *e;
6734         unsigned long long maxsize;
6735         unsigned long long minsize;
6736         int cnt;
6737         int used;
6738
6739         /* find the largest common start free region of the possible disks */
6740         used = 0;
6741         extent_cnt = 0;
6742         cnt = 0;
6743         for (dl = super->disks; dl; dl = dl->next) {
6744                 dl->raiddisk = -1;
6745
6746                 if (dl->index >= 0)
6747                         used++;
6748
6749                 /* don't activate new spares if we are orom constrained
6750                  * and there is already a volume active in the container
6751                  */
6752                 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
6753                         continue;
6754
6755                 e = get_extents(super, dl);
6756                 if (!e)
6757                         continue;
6758                 for (i = 1; e[i-1].size; i++)
6759                         ;
6760                 dl->e = e;
6761                 dl->extent_cnt = i;
6762                 extent_cnt += i;
6763                 cnt++;
6764         }
6765
6766         maxsize = merge_extents(super, extent_cnt);
6767         minsize = size;
6768         if (size == 0)
6769                 /* chunk is in K */
6770                 minsize = chunk * 2;
6771
6772         if (cnt < raiddisks ||
6773             (super->orom && used && used != raiddisks) ||
6774             maxsize < minsize ||
6775             maxsize == 0) {
6776                 pr_err("not enough devices with space to create array.\n");
6777                 return 0; /* No enough free spaces large enough */
6778         }
6779
6780         if (size == 0) {
6781                 size = maxsize;
6782                 if (chunk) {
6783                         size /= 2 * chunk;
6784                         size *= 2 * chunk;
6785                 }
6786                 maxsize = size;
6787         }
6788         if (!check_env("IMSM_NO_PLATFORM") &&
6789             mpb->num_raid_devs > 0 && size && size != maxsize) {
6790                 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6791                 return 0;
6792         }
6793         cnt = 0;
6794         for (dl = super->disks; dl; dl = dl->next)
6795                 if (dl->e)
6796                         dl->raiddisk = cnt++;
6797
6798         *freesize = size;
6799
6800         dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
6801
6802         return 1;
6803 }
6804
6805 static int reserve_space(struct supertype *st, int raiddisks,
6806                          unsigned long long size, int chunk,
6807                          unsigned long long *freesize)
6808 {
6809         struct intel_super *super = st->sb;
6810         struct dl *dl;
6811         int cnt;
6812         int rv = 0;
6813
6814         rv = imsm_get_free_size(st, raiddisks, size, chunk, freesize);
6815         if (rv) {
6816                 cnt = 0;
6817                 for (dl = super->disks; dl; dl = dl->next)
6818                         if (dl->e)
6819                                 dl->raiddisk = cnt++;
6820                 rv = 1;
6821         }
6822
6823         return rv;
6824 }
6825
6826 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
6827                                   int raiddisks, int *chunk, unsigned long long size,
6828                                   unsigned long long data_offset,
6829                                   char *dev, unsigned long long *freesize,
6830                                   int verbose)
6831 {
6832         int fd, cfd;
6833         struct mdinfo *sra;
6834         int is_member = 0;
6835
6836         /* load capability
6837          * if given unused devices create a container
6838          * if given given devices in a container create a member volume
6839          */
6840         if (level == LEVEL_CONTAINER) {
6841                 /* Must be a fresh device to add to a container */
6842                 return validate_geometry_imsm_container(st, level, layout,
6843                                                         raiddisks,
6844                                                         *chunk,
6845                                                         size, data_offset,
6846                                                         dev, freesize,
6847                                                         verbose);
6848         }
6849
6850         if (!dev) {
6851                 if (st->sb) {
6852                         struct intel_super *super = st->sb;
6853                         if (!validate_geometry_imsm_orom(st->sb, level, layout,
6854                                                          raiddisks, chunk, size,
6855                                                          verbose))
6856                                 return 0;
6857                         /* we are being asked to automatically layout a
6858                          * new volume based on the current contents of
6859                          * the container.  If the the parameters can be
6860                          * satisfied reserve_space will record the disks,
6861                          * start offset, and size of the volume to be
6862                          * created.  add_to_super and getinfo_super
6863                          * detect when autolayout is in progress.
6864                          */
6865                         /* assuming that freesize is always given when array is
6866                            created */
6867                         if (super->orom && freesize) {
6868                                 int count;
6869                                 count = count_volumes(super->hba,
6870                                                       super->orom->dpa, verbose);
6871                                 if (super->orom->vphba <= count) {
6872                                         pr_vrb("platform does not support more than %d raid volumes.\n",
6873                                                super->orom->vphba);
6874                                         return 0;
6875                                 }
6876                         }
6877                         if (freesize)
6878                                 return reserve_space(st, raiddisks, size,
6879                                                      *chunk, freesize);
6880                 }
6881                 return 1;
6882         }
6883         if (st->sb) {
6884                 /* creating in a given container */
6885                 return validate_geometry_imsm_volume(st, level, layout,
6886                                                      raiddisks, chunk, size,
6887                                                      data_offset,
6888                                                      dev, freesize, verbose);
6889         }
6890
6891         /* This device needs to be a device in an 'imsm' container */
6892         fd = open(dev, O_RDONLY|O_EXCL, 0);
6893         if (fd >= 0) {
6894                 if (verbose)
6895                         pr_err("Cannot create this array on device %s\n",
6896                                dev);
6897                 close(fd);
6898                 return 0;
6899         }
6900         if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
6901                 if (verbose)
6902                         pr_err("Cannot open %s: %s\n",
6903                                 dev, strerror(errno));
6904                 return 0;
6905         }
6906         /* Well, it is in use by someone, maybe an 'imsm' container. */
6907         cfd = open_container(fd);
6908         close(fd);
6909         if (cfd < 0) {
6910                 if (verbose)
6911                         pr_err("Cannot use %s: It is busy\n",
6912                                 dev);
6913                 return 0;
6914         }
6915         sra = sysfs_read(cfd, NULL, GET_VERSION);
6916         if (sra && sra->array.major_version == -1 &&
6917             strcmp(sra->text_version, "imsm") == 0)
6918                 is_member = 1;
6919         sysfs_free(sra);
6920         if (is_member) {
6921                 /* This is a member of a imsm container.  Load the container
6922                  * and try to create a volume
6923                  */
6924                 struct intel_super *super;
6925
6926                 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
6927                         st->sb = super;
6928                         strcpy(st->container_devnm, fd2devnm(cfd));
6929                         close(cfd);
6930                         return validate_geometry_imsm_volume(st, level, layout,
6931                                                              raiddisks, chunk,
6932                                                              size, data_offset, dev,
6933                                                              freesize, 1)
6934                                 ? 1 : -1;
6935                 }
6936         }
6937
6938         if (verbose)
6939                 pr_err("failed container membership check\n");
6940
6941         close(cfd);
6942         return 0;
6943 }
6944
6945 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
6946 {
6947         struct intel_super *super = st->sb;
6948
6949         if (level && *level == UnSet)
6950                 *level = LEVEL_CONTAINER;
6951
6952         if (level && layout && *layout == UnSet)
6953                 *layout = imsm_level_to_layout(*level);
6954
6955         if (chunk && (*chunk == UnSet || *chunk == 0))
6956                 *chunk = imsm_default_chunk(super->orom);
6957 }
6958
6959 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
6960
6961 static int kill_subarray_imsm(struct supertype *st)
6962 {
6963         /* remove the subarray currently referenced by ->current_vol */
6964         __u8 i;
6965         struct intel_dev **dp;
6966         struct intel_super *super = st->sb;
6967         __u8 current_vol = super->current_vol;
6968         struct imsm_super *mpb = super->anchor;
6969
6970         if (super->current_vol < 0)
6971                 return 2;
6972         super->current_vol = -1; /* invalidate subarray cursor */
6973
6974         /* block deletions that would change the uuid of active subarrays
6975          *
6976          * FIXME when immutable ids are available, but note that we'll
6977          * also need to fixup the invalidated/active subarray indexes in
6978          * mdstat
6979          */
6980         for (i = 0; i < mpb->num_raid_devs; i++) {
6981                 char subarray[4];
6982
6983                 if (i < current_vol)
6984                         continue;
6985                 sprintf(subarray, "%u", i);
6986                 if (is_subarray_active(subarray, st->devnm)) {
6987                         pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6988                                current_vol, i);
6989
6990                         return 2;
6991                 }
6992         }
6993
6994         if (st->update_tail) {
6995                 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
6996
6997                 u->type = update_kill_array;
6998                 u->dev_idx = current_vol;
6999                 append_metadata_update(st, u, sizeof(*u));
7000
7001                 return 0;
7002         }
7003
7004         for (dp = &super->devlist; *dp;)
7005                 if ((*dp)->index == current_vol) {
7006                         *dp = (*dp)->next;
7007                 } else {
7008                         handle_missing(super, (*dp)->dev);
7009                         if ((*dp)->index > current_vol)
7010                                 (*dp)->index--;
7011                         dp = &(*dp)->next;
7012                 }
7013
7014         /* no more raid devices, all active components are now spares,
7015          * but of course failed are still failed
7016          */
7017         if (--mpb->num_raid_devs == 0) {
7018                 struct dl *d;
7019
7020                 for (d = super->disks; d; d = d->next)
7021                         if (d->index > -2)
7022                                 mark_spare(d);
7023         }
7024
7025         super->updates_pending++;
7026
7027         return 0;
7028 }
7029
7030 static int update_subarray_imsm(struct supertype *st, char *subarray,
7031                                 char *update, struct mddev_ident *ident)
7032 {
7033         /* update the subarray currently referenced by ->current_vol */
7034         struct intel_super *super = st->sb;
7035         struct imsm_super *mpb = super->anchor;
7036
7037         if (strcmp(update, "name") == 0) {
7038                 char *name = ident->name;
7039                 char *ep;
7040                 int vol;
7041
7042                 if (is_subarray_active(subarray, st->devnm)) {
7043                         pr_err("Unable to update name of active subarray\n");
7044                         return 2;
7045                 }
7046
7047                 if (!check_name(super, name, 0))
7048                         return 2;
7049
7050                 vol = strtoul(subarray, &ep, 10);
7051                 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7052                         return 2;
7053
7054                 if (st->update_tail) {
7055                         struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7056
7057                         u->type = update_rename_array;
7058                         u->dev_idx = vol;
7059                         snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
7060                         append_metadata_update(st, u, sizeof(*u));
7061                 } else {
7062                         struct imsm_dev *dev;
7063                         int i;
7064
7065                         dev = get_imsm_dev(super, vol);
7066                         snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
7067                         for (i = 0; i < mpb->num_raid_devs; i++) {
7068                                 dev = get_imsm_dev(super, i);
7069                                 handle_missing(super, dev);
7070                         }
7071                         super->updates_pending++;
7072                 }
7073         } else
7074                 return 2;
7075
7076         return 0;
7077 }
7078 #endif /* MDASSEMBLE */
7079
7080 static int is_gen_migration(struct imsm_dev *dev)
7081 {
7082         if (dev == NULL)
7083                 return 0;
7084
7085         if (!dev->vol.migr_state)
7086                 return 0;
7087
7088         if (migr_type(dev) == MIGR_GEN_MIGR)
7089                 return 1;
7090
7091         return 0;
7092 }
7093
7094 static int is_rebuilding(struct imsm_dev *dev)
7095 {
7096         struct imsm_map *migr_map;
7097
7098         if (!dev->vol.migr_state)
7099                 return 0;
7100
7101         if (migr_type(dev) != MIGR_REBUILD)
7102                 return 0;
7103
7104         migr_map = get_imsm_map(dev, MAP_1);
7105
7106         if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
7107                 return 1;
7108         else
7109                 return 0;
7110 }
7111
7112 #ifndef MDASSEMBLE
7113 static int is_initializing(struct imsm_dev *dev)
7114 {
7115         struct imsm_map *migr_map;
7116
7117         if (!dev->vol.migr_state)
7118                 return 0;
7119
7120         if (migr_type(dev) != MIGR_INIT)
7121                 return 0;
7122
7123         migr_map = get_imsm_map(dev, MAP_1);
7124
7125         if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
7126                 return 1;
7127
7128         return 0;
7129 }
7130 #endif
7131
7132 static void update_recovery_start(struct intel_super *super,
7133                                         struct imsm_dev *dev,
7134                                         struct mdinfo *array)
7135 {
7136         struct mdinfo *rebuild = NULL;
7137         struct mdinfo *d;
7138         __u32 units;
7139
7140         if (!is_rebuilding(dev))
7141                 return;
7142
7143         /* Find the rebuild target, but punt on the dual rebuild case */
7144         for (d = array->devs; d; d = d->next)
7145                 if (d->recovery_start == 0) {
7146                         if (rebuild)
7147                                 return;
7148                         rebuild = d;
7149                 }
7150
7151         if (!rebuild) {
7152                 /* (?) none of the disks are marked with
7153                  * IMSM_ORD_REBUILD, so assume they are missing and the
7154                  * disk_ord_tbl was not correctly updated
7155                  */
7156                 dprintf("failed to locate out-of-sync disk\n");
7157                 return;
7158         }
7159
7160         units = __le32_to_cpu(dev->vol.curr_migr_unit);
7161         rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
7162 }
7163
7164 #ifndef MDASSEMBLE
7165 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
7166 #endif
7167
7168 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
7169 {
7170         /* Given a container loaded by load_super_imsm_all,
7171          * extract information about all the arrays into
7172          * an mdinfo tree.
7173          * If 'subarray' is given, just extract info about that array.
7174          *
7175          * For each imsm_dev create an mdinfo, fill it in,
7176          *  then look for matching devices in super->disks
7177          *  and create appropriate device mdinfo.
7178          */
7179         struct intel_super *super = st->sb;
7180         struct imsm_super *mpb = super->anchor;
7181         struct mdinfo *rest = NULL;
7182         unsigned int i;
7183         int sb_errors = 0;
7184         struct dl *d;
7185         int spare_disks = 0;
7186
7187         /* do not assemble arrays when not all attributes are supported */
7188         if (imsm_check_attributes(mpb->attributes) == 0) {
7189                 sb_errors = 1;
7190                 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7191         }
7192
7193         /* count spare devices, not used in maps
7194          */
7195         for (d = super->disks; d; d = d->next)
7196                 if (d->index == -1)
7197                         spare_disks++;
7198
7199         for (i = 0; i < mpb->num_raid_devs; i++) {
7200                 struct imsm_dev *dev;
7201                 struct imsm_map *map;
7202                 struct imsm_map *map2;
7203                 struct mdinfo *this;
7204                 int slot;
7205 #ifndef MDASSEMBLE
7206                 int chunk;
7207 #endif
7208                 char *ep;
7209
7210                 if (subarray &&
7211                     (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
7212                         continue;
7213
7214                 dev = get_imsm_dev(super, i);
7215                 map = get_imsm_map(dev, MAP_0);
7216                 map2 = get_imsm_map(dev, MAP_1);
7217
7218                 /* do not publish arrays that are in the middle of an
7219                  * unsupported migration
7220                  */
7221                 if (dev->vol.migr_state &&
7222                     (migr_type(dev) == MIGR_STATE_CHANGE)) {
7223                         pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7224                                 dev->volume);
7225                         continue;
7226                 }
7227                 /* do not publish arrays that are not support by controller's
7228                  * OROM/EFI
7229                  */
7230
7231                 this = xmalloc(sizeof(*this));
7232
7233                 super->current_vol = i;
7234                 getinfo_super_imsm_volume(st, this, NULL);
7235                 this->next = rest;
7236 #ifndef MDASSEMBLE
7237                 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
7238                 /* mdadm does not support all metadata features- set the bit in all arrays state */
7239                 if (!validate_geometry_imsm_orom(super,
7240                                                  get_imsm_raid_level(map), /* RAID level */
7241                                                  imsm_level_to_layout(get_imsm_raid_level(map)),
7242                                                  map->num_members, /* raid disks */
7243                                                  &chunk, join_u32(dev->size_low, dev->size_high),
7244                                                  1 /* verbose */)) {
7245                         pr_err("IMSM RAID geometry validation failed.  Array %s activation is blocked.\n",
7246                                 dev->volume);
7247                         this->array.state |=
7248                           (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
7249                           (1<<MD_SB_BLOCK_VOLUME);
7250                 }
7251 #endif
7252
7253                 /* if array has bad blocks, set suitable bit in all arrays state */
7254                 if (sb_errors)
7255                         this->array.state |=
7256                           (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
7257                           (1<<MD_SB_BLOCK_VOLUME);
7258
7259                 for (slot = 0 ; slot <  map->num_members; slot++) {
7260                         unsigned long long recovery_start;
7261                         struct mdinfo *info_d;
7262                         struct dl *d;
7263                         int idx;
7264                         int skip;
7265                         __u32 ord;
7266
7267                         skip = 0;
7268                         idx = get_imsm_disk_idx(dev, slot, MAP_0);
7269                         ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
7270                         for (d = super->disks; d ; d = d->next)
7271                                 if (d->index == idx)
7272                                         break;
7273
7274                         recovery_start = MaxSector;
7275                         if (d == NULL)
7276                                 skip = 1;
7277                         if (d && is_failed(&d->disk))
7278                                 skip = 1;
7279                         if (ord & IMSM_ORD_REBUILD)
7280                                 recovery_start = 0;
7281
7282                         /*
7283                          * if we skip some disks the array will be assmebled degraded;
7284                          * reset resync start to avoid a dirty-degraded
7285                          * situation when performing the intial sync
7286                          *
7287                          * FIXME handle dirty degraded
7288                          */
7289                         if ((skip || recovery_start == 0) && !dev->vol.dirty)
7290                                 this->resync_start = MaxSector;
7291                         if (skip)
7292                                 continue;
7293
7294                         info_d = xcalloc(1, sizeof(*info_d));
7295                         info_d->next = this->devs;
7296                         this->devs = info_d;
7297
7298                         info_d->disk.number = d->index;
7299                         info_d->disk.major = d->major;
7300                         info_d->disk.minor = d->minor;
7301                         info_d->disk.raid_disk = slot;
7302                         info_d->recovery_start = recovery_start;
7303                         if (map2) {
7304                                 if (slot < map2->num_members)
7305                                         info_d->disk.state = (1 << MD_DISK_ACTIVE);
7306                                 else
7307                                         this->array.spare_disks++;
7308                         } else {
7309                                 if (slot < map->num_members)
7310                                         info_d->disk.state = (1 << MD_DISK_ACTIVE);
7311                                 else
7312                                         this->array.spare_disks++;
7313                         }
7314                         if (info_d->recovery_start == MaxSector)
7315                                 this->array.working_disks++;
7316
7317                         info_d->events = __le32_to_cpu(mpb->generation_num);
7318                         info_d->data_offset = pba_of_lba0(map);
7319
7320                         if (map->raid_level == 5) {
7321                                 info_d->component_size =
7322                                                 num_data_stripes(map) *
7323                                                 map->blocks_per_strip;
7324                         } else {
7325                                 info_d->component_size = blocks_per_member(map);
7326                         }
7327
7328                         info_d->bb.supported = 0;
7329                         get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
7330                                              info_d->data_offset,
7331                                              info_d->component_size,
7332                                              &info_d->bb);
7333                 }
7334                 /* now that the disk list is up-to-date fixup recovery_start */
7335                 update_recovery_start(super, dev, this);
7336                 this->array.spare_disks += spare_disks;
7337
7338 #ifndef MDASSEMBLE
7339                 /* check for reshape */
7340                 if (this->reshape_active == 1)
7341                         recover_backup_imsm(st, this);
7342 #endif
7343                 rest = this;
7344         }
7345
7346         return rest;
7347 }
7348
7349 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
7350                                 int failed, int look_in_map)
7351 {
7352         struct imsm_map *map;
7353
7354         map = get_imsm_map(dev, look_in_map);
7355
7356         if (!failed)
7357                 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
7358                         IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
7359
7360         switch (get_imsm_raid_level(map)) {
7361         case 0:
7362                 return IMSM_T_STATE_FAILED;
7363                 break;
7364         case 1:
7365                 if (failed < map->num_members)
7366                         return IMSM_T_STATE_DEGRADED;
7367                 else
7368                         return IMSM_T_STATE_FAILED;
7369                 break;
7370         case 10:
7371         {
7372                 /**
7373                  * check to see if any mirrors have failed, otherwise we
7374                  * are degraded.  Even numbered slots are mirrored on
7375                  * slot+1
7376                  */
7377                 int i;
7378                 /* gcc -Os complains that this is unused */
7379                 int insync = insync;
7380
7381                 for (i = 0; i < map->num_members; i++) {
7382                         __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
7383                         int idx = ord_to_idx(ord);
7384                         struct imsm_disk *disk;
7385
7386                         /* reset the potential in-sync count on even-numbered
7387                          * slots.  num_copies is always 2 for imsm raid10
7388                          */
7389                         if ((i & 1) == 0)
7390                                 insync = 2;
7391
7392                         disk = get_imsm_disk(super, idx);
7393                         if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
7394                                 insync--;
7395
7396                         /* no in-sync disks left in this mirror the
7397                          * array has failed
7398                          */
7399                         if (insync == 0)
7400                                 return IMSM_T_STATE_FAILED;
7401                 }
7402
7403                 return IMSM_T_STATE_DEGRADED;
7404         }
7405         case 5:
7406                 if (failed < 2)
7407                         return IMSM_T_STATE_DEGRADED;
7408                 else
7409                         return IMSM_T_STATE_FAILED;
7410                 break;
7411         default:
7412                 break;
7413         }
7414
7415         return map->map_state;
7416 }
7417
7418 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
7419                              int look_in_map)
7420 {
7421         int i;
7422         int failed = 0;
7423         struct imsm_disk *disk;
7424         struct imsm_map *map = get_imsm_map(dev, MAP_0);
7425         struct imsm_map *prev = get_imsm_map(dev, MAP_1);
7426         struct imsm_map *map_for_loop;
7427         __u32 ord;
7428         int idx;
7429         int idx_1;
7430
7431         /* at the beginning of migration we set IMSM_ORD_REBUILD on
7432          * disks that are being rebuilt.  New failures are recorded to
7433          * map[0].  So we look through all the disks we started with and
7434          * see if any failures are still present, or if any new ones
7435          * have arrived
7436          */
7437         map_for_loop = map;
7438         if (prev && (map->num_members < prev->num_members))
7439                 map_for_loop = prev;
7440
7441         for (i = 0; i < map_for_loop->num_members; i++) {
7442                 idx_1 = -255;
7443                 /* when MAP_X is passed both maps failures are counted
7444                  */
7445                 if (prev &&
7446                     (look_in_map == MAP_1 || look_in_map == MAP_X) &&
7447                     i < prev->num_members) {
7448                         ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
7449                         idx_1 = ord_to_idx(ord);
7450
7451                         disk = get_imsm_disk(super, idx_1);
7452                         if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
7453                                 failed++;
7454                 }
7455                 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
7456                     i < map->num_members) {
7457                         ord = __le32_to_cpu(map->disk_ord_tbl[i]);
7458                         idx = ord_to_idx(ord);
7459
7460                         if (idx != idx_1) {
7461                                 disk = get_imsm_disk(super, idx);
7462                                 if (!disk || is_failed(disk) ||
7463                                     ord & IMSM_ORD_REBUILD)
7464                                         failed++;
7465                         }
7466                 }
7467         }
7468
7469         return failed;
7470 }
7471
7472 #ifndef MDASSEMBLE
7473 static int imsm_open_new(struct supertype *c, struct active_array *a,
7474                          char *inst)
7475 {
7476         struct intel_super *super = c->sb;
7477         struct imsm_super *mpb = super->anchor;
7478         struct imsm_update_prealloc_bb_mem u;
7479
7480         if (atoi(inst) >= mpb->num_raid_devs) {
7481                 pr_err("subarry index %d, out of range\n", atoi(inst));
7482                 return -ENODEV;
7483         }
7484
7485         dprintf("imsm: open_new %s\n", inst);
7486         a->info.container_member = atoi(inst);
7487
7488         u.type = update_prealloc_badblocks_mem;
7489         imsm_update_metadata_locally(c, &u, sizeof(u));
7490
7491         return 0;
7492 }
7493
7494 static int is_resyncing(struct imsm_dev *dev)
7495 {
7496         struct imsm_map *migr_map;
7497
7498         if (!dev->vol.migr_state)
7499                 return 0;
7500
7501         if (migr_type(dev) == MIGR_INIT ||
7502             migr_type(dev) == MIGR_REPAIR)
7503                 return 1;
7504
7505         if (migr_type(dev) == MIGR_GEN_MIGR)
7506                 return 0;
7507
7508         migr_map = get_imsm_map(dev, MAP_1);
7509
7510         if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
7511             dev->vol.migr_type != MIGR_GEN_MIGR)
7512                 return 1;
7513         else
7514                 return 0;
7515 }
7516
7517 /* return true if we recorded new information */
7518 static int mark_failure(struct intel_super *super,
7519                         struct imsm_dev *dev, struct imsm_disk *disk, int idx)
7520 {
7521         __u32 ord;
7522         int slot;
7523         struct imsm_map *map;
7524         char buf[MAX_RAID_SERIAL_LEN+3];
7525         unsigned int len, shift = 0;
7526
7527         /* new failures are always set in map[0] */
7528         map = get_imsm_map(dev, MAP_0);
7529
7530         slot = get_imsm_disk_slot(map, idx);
7531         if (slot < 0)
7532                 return 0;
7533
7534         ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
7535         if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
7536                 return 0;
7537
7538         memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
7539         buf[MAX_RAID_SERIAL_LEN] = '\000';
7540         strcat(buf, ":0");
7541         if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
7542                 shift = len - MAX_RAID_SERIAL_LEN + 1;
7543         strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN);
7544
7545         disk->status |= FAILED_DISK;
7546         set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
7547         /* mark failures in second map if second map exists and this disk
7548          * in this slot.
7549          * This is valid for migration, initialization and rebuild
7550          */
7551         if (dev->vol.migr_state) {
7552                 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
7553                 int slot2 = get_imsm_disk_slot(map2, idx);
7554
7555                 if (slot2 < map2->num_members && slot2 >= 0)
7556                         set_imsm_ord_tbl_ent(map2, slot2,
7557                                              idx | IMSM_ORD_REBUILD);
7558         }
7559         if (map->failed_disk_num == 0xff)
7560                 map->failed_disk_num = slot;
7561
7562         clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
7563
7564         return 1;
7565 }
7566
7567 static void mark_missing(struct intel_super *super,
7568                          struct imsm_dev *dev, struct imsm_disk *disk, int idx)
7569 {
7570         mark_failure(super, dev, disk, idx);
7571
7572         if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
7573                 return;
7574
7575         disk->scsi_id = __cpu_to_le32(~(__u32)0);
7576         memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
7577 }
7578
7579 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
7580 {
7581         struct dl *dl;
7582
7583         if (!super->missing)
7584                 return;
7585
7586         /* When orom adds replacement for missing disk it does
7587          * not remove entry of missing disk, but just updates map with
7588          * new added disk. So it is not enough just to test if there is
7589          * any missing disk, we have to look if there are any failed disks
7590          * in map to stop migration */
7591
7592         dprintf("imsm: mark missing\n");
7593         /* end process for initialization and rebuild only
7594          */
7595         if (is_gen_migration(dev) == 0) {
7596                 __u8 map_state;
7597                 int failed;
7598
7599                 failed = imsm_count_failed(super, dev, MAP_0);
7600                 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
7601
7602                 if (failed)
7603                         end_migration(dev, super, map_state);
7604         }
7605         for (dl = super->missing; dl; dl = dl->next)
7606                 mark_missing(super, dev, &dl->disk, dl->index);
7607         super->updates_pending++;
7608 }
7609
7610 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
7611                                               long long new_size)
7612 {
7613         int used_disks = imsm_num_data_members(dev, MAP_0);
7614         unsigned long long array_blocks;
7615         struct imsm_map *map;
7616
7617         if (used_disks == 0) {
7618                 /* when problems occures
7619                  * return current array_blocks value
7620                  */
7621                 array_blocks = __le32_to_cpu(dev->size_high);
7622                 array_blocks = array_blocks << 32;
7623                 array_blocks += __le32_to_cpu(dev->size_low);
7624
7625                 return array_blocks;
7626         }
7627
7628         /* set array size in metadata
7629          */
7630         if (new_size <= 0) {
7631                 /* OLCE size change is caused by added disks
7632                  */
7633                 map = get_imsm_map(dev, MAP_0);
7634                 array_blocks = blocks_per_member(map) * used_disks;
7635         } else {
7636                 /* Online Volume Size Change
7637                  * Using  available free space
7638                  */
7639                 array_blocks = new_size;
7640         }
7641
7642         /* round array size down to closest MB
7643          */
7644         array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
7645         dev->size_low = __cpu_to_le32((__u32)array_blocks);
7646         dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32));
7647
7648         return array_blocks;
7649 }
7650
7651 static void imsm_set_disk(struct active_array *a, int n, int state);
7652
7653 static void imsm_progress_container_reshape(struct intel_super *super)
7654 {
7655         /* if no device has a migr_state, but some device has a
7656          * different number of members than the previous device, start
7657          * changing the number of devices in this device to match
7658          * previous.
7659          */
7660         struct imsm_super *mpb = super->anchor;
7661         int prev_disks = -1;
7662         int i;
7663         int copy_map_size;
7664
7665         for (i = 0; i < mpb->num_raid_devs; i++) {
7666                 struct imsm_dev *dev = get_imsm_dev(super, i);
7667                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
7668                 struct imsm_map *map2;
7669                 int prev_num_members;
7670
7671                 if (dev->vol.migr_state)
7672                         return;
7673
7674                 if (prev_disks == -1)
7675                         prev_disks = map->num_members;
7676                 if (prev_disks == map->num_members)
7677                         continue;
7678
7679                 /* OK, this array needs to enter reshape mode.
7680                  * i.e it needs a migr_state
7681                  */
7682
7683                 copy_map_size = sizeof_imsm_map(map);
7684                 prev_num_members = map->num_members;
7685                 map->num_members = prev_disks;
7686                 dev->vol.migr_state = 1;
7687                 dev->vol.curr_migr_unit = 0;
7688                 set_migr_type(dev, MIGR_GEN_MIGR);
7689                 for (i = prev_num_members;
7690                      i < map->num_members; i++)
7691                         set_imsm_ord_tbl_ent(map, i, i);
7692                 map2 = get_imsm_map(dev, MAP_1);
7693                 /* Copy the current map */
7694                 memcpy(map2, map, copy_map_size);
7695                 map2->num_members = prev_num_members;
7696
7697                 imsm_set_array_size(dev, -1);
7698                 super->clean_migration_record_by_mdmon = 1;
7699                 super->updates_pending++;
7700         }
7701 }
7702
7703 /* Handle dirty -> clean transititions, resync and reshape.  Degraded and rebuild
7704  * states are handled in imsm_set_disk() with one exception, when a
7705  * resync is stopped due to a new failure this routine will set the
7706  * 'degraded' state for the array.
7707  */
7708 static int imsm_set_array_state(struct active_array *a, int consistent)
7709 {
7710         int inst = a->info.container_member;
7711         struct intel_super *super = a->container->sb;
7712         struct imsm_dev *dev = get_imsm_dev(super, inst);
7713         struct imsm_map *map = get_imsm_map(dev, MAP_0);
7714         int failed = imsm_count_failed(super, dev, MAP_0);
7715         __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
7716         __u32 blocks_per_unit;
7717
7718         if (dev->vol.migr_state &&
7719             dev->vol.migr_type  == MIGR_GEN_MIGR) {
7720                 /* array state change is blocked due to reshape action
7721                  * We might need to
7722                  * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7723                  * - finish the reshape (if last_checkpoint is big and action != reshape)
7724                  * - update curr_migr_unit
7725                  */
7726                 if (a->curr_action == reshape) {
7727                         /* still reshaping, maybe update curr_migr_unit */
7728                         goto mark_checkpoint;
7729                 } else {
7730                         if (a->last_checkpoint == 0 && a->prev_action == reshape) {
7731                                 /* for some reason we aborted the reshape.
7732                                  *
7733                                  * disable automatic metadata rollback
7734                                  * user action is required to recover process
7735                                  */
7736                                 if (0) {
7737                                         struct imsm_map *map2 =
7738                                                 get_imsm_map(dev, MAP_1);
7739                                         dev->vol.migr_state = 0;
7740                                         set_migr_type(dev, 0);
7741                                         dev->vol.curr_migr_unit = 0;
7742                                         memcpy(map, map2,
7743                                                sizeof_imsm_map(map2));
7744                                         super->updates_pending++;
7745                                 }
7746                         }
7747                         if (a->last_checkpoint >= a->info.component_size) {
7748                                 unsigned long long array_blocks;
7749                                 int used_disks;
7750                                 struct mdinfo *mdi;
7751
7752                                 used_disks = imsm_num_data_members(dev, MAP_0);
7753                                 if (used_disks > 0) {
7754                                         array_blocks =
7755                                                 blocks_per_member(map) *
7756                                                 used_disks;
7757                                         /* round array size down to closest MB
7758                                          */
7759                                         array_blocks = (array_blocks
7760                                                         >> SECT_PER_MB_SHIFT)
7761                                                 << SECT_PER_MB_SHIFT;
7762                                         a->info.custom_array_size = array_blocks;
7763                                         /* encourage manager to update array
7764                                          * size
7765                                          */
7766
7767                                         a->check_reshape = 1;
7768                                 }
7769                                 /* finalize online capacity expansion/reshape */
7770                                 for (mdi = a->info.devs; mdi; mdi = mdi->next)
7771                                         imsm_set_disk(a,
7772                                                       mdi->disk.raid_disk,
7773                                                       mdi->curr_state);
7774
7775                                 imsm_progress_container_reshape(super);
7776                         }
7777                 }
7778         }
7779
7780         /* before we activate this array handle any missing disks */
7781         if (consistent == 2)
7782                 handle_missing(super, dev);
7783
7784         if (consistent == 2 &&
7785             (!is_resync_complete(&a->info) ||
7786              map_state != IMSM_T_STATE_NORMAL ||
7787              dev->vol.migr_state))
7788                 consistent = 0;
7789
7790         if (is_resync_complete(&a->info)) {
7791                 /* complete intialization / resync,
7792                  * recovery and interrupted recovery is completed in
7793                  * ->set_disk
7794                  */
7795                 if (is_resyncing(dev)) {
7796                         dprintf("imsm: mark resync done\n");
7797                         end_migration(dev, super, map_state);
7798                         super->updates_pending++;
7799                         a->last_checkpoint = 0;
7800                 }
7801         } else if ((!is_resyncing(dev) && !failed) &&
7802                    (imsm_reshape_blocks_arrays_changes(super) == 0)) {
7803                 /* mark the start of the init process if nothing is failed */
7804                 dprintf("imsm: mark resync start\n");
7805                 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
7806                         migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
7807                 else
7808                         migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
7809                 super->updates_pending++;
7810         }
7811
7812 mark_checkpoint:
7813         /* skip checkpointing for general migration,
7814          * it is controlled in mdadm
7815          */
7816         if (is_gen_migration(dev))
7817                 goto skip_mark_checkpoint;
7818
7819         /* check if we can update curr_migr_unit from resync_start, recovery_start */
7820         blocks_per_unit = blocks_per_migr_unit(super, dev);
7821         if (blocks_per_unit) {
7822                 __u32 units32;
7823                 __u64 units;
7824
7825                 units = a->last_checkpoint / blocks_per_unit;
7826                 units32 = units;
7827
7828                 /* check that we did not overflow 32-bits, and that
7829                  * curr_migr_unit needs updating
7830                  */
7831                 if (units32 == units &&
7832                     units32 != 0 &&
7833                     __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
7834                         dprintf("imsm: mark checkpoint (%u)\n", units32);
7835                         dev->vol.curr_migr_unit = __cpu_to_le32(units32);
7836                         super->updates_pending++;
7837                 }
7838         }
7839
7840 skip_mark_checkpoint:
7841         /* mark dirty / clean */
7842         if (dev->vol.dirty != !consistent) {
7843                 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
7844                 if (consistent)
7845                         dev->vol.dirty = 0;
7846                 else
7847                         dev->vol.dirty = 1;
7848                 super->updates_pending++;
7849         }
7850
7851         return consistent;
7852 }
7853
7854 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
7855 {
7856         int inst = a->info.container_member;
7857         struct intel_super *super = a->container->sb;
7858         struct imsm_dev *dev = get_imsm_dev(super, inst);
7859         struct imsm_map *map = get_imsm_map(dev, MAP_0);
7860
7861         if (slot > map->num_members) {
7862                 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
7863                        slot, map->num_members - 1);
7864                 return -1;
7865         }
7866
7867         if (slot < 0)
7868                 return -1;
7869
7870         return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
7871 }
7872
7873 static void imsm_set_disk(struct active_array *a, int n, int state)
7874 {
7875         int inst = a->info.container_member;
7876         struct intel_super *super = a->container->sb;
7877         struct imsm_dev *dev = get_imsm_dev(super, inst);
7878         struct imsm_map *map = get_imsm_map(dev, MAP_0);
7879         struct imsm_disk *disk;
7880         struct mdinfo *mdi;
7881         int recovery_not_finished = 0;
7882         int failed;
7883         int ord;
7884         __u8 map_state;
7885
7886         ord = imsm_disk_slot_to_ord(a, n);
7887         if (ord < 0)
7888                 return;
7889
7890         dprintf("imsm: set_disk %d:%x\n", n, state);
7891         disk = get_imsm_disk(super, ord_to_idx(ord));
7892
7893         /* check for new failures */
7894         if (state & DS_FAULTY) {
7895                 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
7896                         super->updates_pending++;
7897         }
7898
7899         /* check if in_sync */
7900         if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
7901                 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
7902
7903                 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
7904                 super->updates_pending++;
7905         }
7906
7907         failed = imsm_count_failed(super, dev, MAP_0);
7908         map_state = imsm_check_degraded(super, dev, failed, MAP_0);
7909
7910         /* check if recovery complete, newly degraded, or failed */
7911         dprintf("imsm: Detected transition to state ");
7912         switch (map_state) {
7913         case IMSM_T_STATE_NORMAL: /* transition to normal state */
7914                 dprintf("normal: ");
7915                 if (is_rebuilding(dev)) {
7916                         dprintf_cont("while rebuilding");
7917                         /* check if recovery is really finished */
7918                         for (mdi = a->info.devs; mdi ; mdi = mdi->next)
7919                                 if (mdi->recovery_start != MaxSector) {
7920                                         recovery_not_finished = 1;
7921                                         break;
7922                                 }
7923                         if (recovery_not_finished) {
7924                                 dprintf_cont("\n");
7925                                 dprintf("Rebuild has not finished yet, state not changed");
7926                                 if (a->last_checkpoint < mdi->recovery_start) {
7927                                         a->last_checkpoint = mdi->recovery_start;
7928                                         super->updates_pending++;
7929                                 }
7930                                 break;
7931                         }
7932                         end_migration(dev, super, map_state);
7933                         map = get_imsm_map(dev, MAP_0);
7934                         map->failed_disk_num = ~0;
7935                         super->updates_pending++;
7936                         a->last_checkpoint = 0;
7937                         break;
7938                 }
7939                 if (is_gen_migration(dev)) {
7940                         dprintf_cont("while general migration");
7941                         if (a->last_checkpoint >= a->info.component_size)
7942                                 end_migration(dev, super, map_state);
7943                         else
7944                                 map->map_state = map_state;
7945                         map = get_imsm_map(dev, MAP_0);
7946                         map->failed_disk_num = ~0;
7947                         super->updates_pending++;
7948                         break;
7949                 }
7950         break;
7951         case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
7952                 dprintf_cont("degraded: ");
7953                 if (map->map_state != map_state && !dev->vol.migr_state) {
7954                         dprintf_cont("mark degraded");
7955                         map->map_state = map_state;
7956                         super->updates_pending++;
7957                         a->last_checkpoint = 0;
7958                         break;
7959                 }
7960                 if (is_rebuilding(dev)) {
7961                         dprintf_cont("while rebuilding.");
7962                         if (map->map_state != map_state)  {
7963                                 dprintf_cont(" Map state change");
7964                                 end_migration(dev, super, map_state);
7965                                 super->updates_pending++;
7966                         }
7967                         break;
7968                 }
7969                 if (is_gen_migration(dev)) {
7970                         dprintf_cont("while general migration");
7971                         if (a->last_checkpoint >= a->info.component_size)
7972                                 end_migration(dev, super, map_state);
7973                         else {
7974                                 map->map_state = map_state;
7975                                 manage_second_map(super, dev);
7976                         }
7977                         super->updates_pending++;
7978                         break;
7979                 }
7980                 if (is_initializing(dev)) {
7981                         dprintf_cont("while initialization.");
7982                         map->map_state = map_state;
7983                         super->updates_pending++;
7984                         break;
7985                 }
7986         break;
7987         case IMSM_T_STATE_FAILED: /* transition to failed state */
7988                 dprintf_cont("failed: ");
7989                 if (is_gen_migration(dev)) {
7990                         dprintf_cont("while general migration");
7991                         map->map_state = map_state;
7992                         super->updates_pending++;
7993                         break;
7994                 }
7995                 if (map->map_state != map_state) {
7996                         dprintf_cont("mark failed");
7997                         end_migration(dev, super, map_state);
7998                         super->updates_pending++;
7999                         a->last_checkpoint = 0;
8000                         break;
8001                 }
8002         break;
8003         default:
8004                 dprintf_cont("state %i\n", map_state);
8005         }
8006         dprintf_cont("\n");
8007 }
8008
8009 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8010 {
8011         void *buf = mpb;
8012         __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8013         unsigned long long dsize;
8014         unsigned long long sectors;
8015         unsigned int sector_size;
8016
8017         get_dev_sector_size(fd, NULL, &sector_size);
8018         get_dev_size(fd, NULL, &dsize);
8019
8020         if (mpb_size > sector_size) {
8021                 /* -1 to account for anchor */
8022                 sectors = mpb_sectors(mpb, sector_size) - 1;
8023
8024                 /* write the extended mpb to the sectors preceeding the anchor */
8025                 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8026                    SEEK_SET) < 0)
8027                         return 1;
8028
8029                 if ((unsigned long long)write(fd, buf + sector_size,
8030                    sector_size * sectors) != sector_size * sectors)
8031                         return 1;
8032         }
8033
8034         /* first block is stored on second to last sector of the disk */
8035         if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8036                 return 1;
8037
8038         if (write(fd, buf, sector_size) != sector_size)
8039                 return 1;
8040
8041         return 0;
8042 }
8043
8044 static void imsm_sync_metadata(struct supertype *container)
8045 {
8046         struct intel_super *super = container->sb;
8047
8048         dprintf("sync metadata: %d\n", super->updates_pending);
8049         if (!super->updates_pending)
8050                 return;
8051
8052         write_super_imsm(container, 0);
8053
8054         super->updates_pending = 0;
8055 }
8056
8057 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
8058 {
8059         struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8060         int i = get_imsm_disk_idx(dev, idx, MAP_X);
8061         struct dl *dl;
8062
8063         for (dl = super->disks; dl; dl = dl->next)
8064                 if (dl->index == i)
8065                         break;
8066
8067         if (dl && is_failed(&dl->disk))
8068                 dl = NULL;
8069
8070         if (dl)
8071                 dprintf("found %x:%x\n", dl->major, dl->minor);
8072
8073         return dl;
8074 }
8075
8076 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8077                                  struct active_array *a, int activate_new,
8078                                  struct mdinfo *additional_test_list)
8079 {
8080         struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
8081         int idx = get_imsm_disk_idx(dev, slot, MAP_X);
8082         struct imsm_super *mpb = super->anchor;
8083         struct imsm_map *map;
8084         unsigned long long pos;
8085         struct mdinfo *d;
8086         struct extent *ex;
8087         int i, j;
8088         int found;
8089         __u32 array_start = 0;
8090         __u32 array_end = 0;
8091         struct dl *dl;
8092         struct mdinfo *test_list;
8093
8094         for (dl = super->disks; dl; dl = dl->next) {
8095                 /* If in this array, skip */
8096                 for (d = a->info.devs ; d ; d = d->next)
8097                         if (d->state_fd >= 0 &&
8098                             d->disk.major == dl->major &&
8099                             d->disk.minor == dl->minor) {
8100                                 dprintf("%x:%x already in array\n",
8101                                         dl->major, dl->minor);
8102                                 break;
8103                         }
8104                 if (d)
8105                         continue;
8106                 test_list = additional_test_list;
8107                 while (test_list) {
8108                         if (test_list->disk.major == dl->major &&
8109                             test_list->disk.minor == dl->minor) {
8110                                 dprintf("%x:%x already in additional test list\n",
8111                                         dl->major, dl->minor);
8112                                 break;
8113                         }
8114                         test_list = test_list->next;
8115                 }
8116                 if (test_list)
8117                         continue;
8118
8119                 /* skip in use or failed drives */
8120                 if (is_failed(&dl->disk) || idx == dl->index ||
8121                     dl->index == -2) {
8122                         dprintf("%x:%x status (failed: %d index: %d)\n",
8123                                 dl->major, dl->minor, is_failed(&dl->disk), idx);
8124                         continue;
8125                 }
8126
8127                 /* skip pure spares when we are looking for partially
8128                  * assimilated drives
8129                  */
8130                 if (dl->index == -1 && !activate_new)
8131                         continue;
8132
8133                 /* Does this unused device have the requisite free space?
8134                  * It needs to be able to cover all member volumes
8135                  */
8136                 ex = get_extents(super, dl);
8137                 if (!ex) {
8138                         dprintf("cannot get extents\n");
8139                         continue;
8140                 }
8141                 for (i = 0; i < mpb->num_raid_devs; i++) {
8142                         dev = get_imsm_dev(super, i);
8143                         map = get_imsm_map(dev, MAP_0);
8144
8145                         /* check if this disk is already a member of
8146                          * this array
8147                          */
8148                         if (get_imsm_disk_slot(map, dl->index) >= 0)
8149                                 continue;
8150
8151                         found = 0;
8152                         j = 0;
8153                         pos = 0;
8154                         array_start = pba_of_lba0(map);
8155                         array_end = array_start +
8156                                     blocks_per_member(map) - 1;
8157
8158                         do {
8159                                 /* check that we can start at pba_of_lba0 with
8160                                  * blocks_per_member of space
8161                                  */
8162                                 if (array_start >= pos && array_end < ex[j].start) {
8163                                         found = 1;
8164                                         break;
8165                                 }
8166                                 pos = ex[j].start + ex[j].size;
8167                                 j++;
8168                         } while (ex[j-1].size);
8169
8170                         if (!found)
8171                                 break;
8172                 }
8173
8174                 free(ex);
8175                 if (i < mpb->num_raid_devs) {
8176                         dprintf("%x:%x does not have %u to %u available\n",
8177                                 dl->major, dl->minor, array_start, array_end);
8178                         /* No room */
8179                         continue;
8180                 }
8181                 return dl;
8182         }
8183
8184         return dl;
8185 }
8186
8187 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
8188 {
8189         struct imsm_dev *dev2;
8190         struct imsm_map *map;
8191         struct dl *idisk;
8192         int slot;
8193         int idx;
8194         __u8 state;
8195
8196         dev2 = get_imsm_dev(cont->sb, dev_idx);
8197         if (dev2) {
8198                 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
8199                 if (state == IMSM_T_STATE_FAILED) {
8200                         map = get_imsm_map(dev2, MAP_0);
8201                         if (!map)
8202                                 return 1;
8203                         for (slot = 0; slot < map->num_members; slot++) {
8204                                 /*
8205                                  * Check if failed disks are deleted from intel
8206                                  * disk list or are marked to be deleted
8207                                  */
8208                                 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
8209                                 idisk = get_imsm_dl_disk(cont->sb, idx);
8210                                 /*
8211                                  * Do not rebuild the array if failed disks
8212                                  * from failed sub-array are not removed from
8213                                  * container.
8214                                  */
8215                                 if (idisk &&
8216                                     is_failed(&idisk->disk) &&
8217                                     (idisk->action != DISK_REMOVE))
8218                                         return 0;
8219                         }
8220                 }
8221         }
8222         return 1;
8223 }
8224
8225 static struct mdinfo *imsm_activate_spare(struct active_array *a,
8226                                           struct metadata_update **updates)
8227 {
8228         /**
8229          * Find a device with unused free space and use it to replace a
8230          * failed/vacant region in an array.  We replace failed regions one a
8231          * array at a time.  The result is that a new spare disk will be added
8232          * to the first failed array and after the monitor has finished
8233          * propagating failures the remainder will be consumed.
8234          *
8235          * FIXME add a capability for mdmon to request spares from another
8236          * container.
8237          */
8238
8239         struct intel_super *super = a->container->sb;
8240         int inst = a->info.container_member;
8241         struct imsm_dev *dev = get_imsm_dev(super, inst);
8242         struct imsm_map *map = get_imsm_map(dev, MAP_0);
8243         int failed = a->info.array.raid_disks;
8244         struct mdinfo *rv = NULL;
8245         struct mdinfo *d;
8246         struct mdinfo *di;
8247         struct metadata_update *mu;
8248         struct dl *dl;
8249         struct imsm_update_activate_spare *u;
8250         int num_spares = 0;
8251         int i;
8252         int allowed;
8253
8254         for (d = a->info.devs ; d ; d = d->next) {
8255                 if ((d->curr_state & DS_FAULTY) &&
8256                         d->state_fd >= 0)
8257                         /* wait for Removal to happen */
8258                         return NULL;
8259                 if (d->state_fd >= 0)
8260                         failed--;
8261         }
8262
8263         dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8264                 inst, failed, a->info.array.raid_disks, a->info.array.level);
8265
8266         if (imsm_reshape_blocks_arrays_changes(super))
8267                         return NULL;
8268
8269         /* Cannot activate another spare if rebuild is in progress already
8270          */
8271         if (is_rebuilding(dev)) {
8272                 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8273                 return NULL;
8274         }
8275
8276         if (a->info.array.level == 4)
8277                 /* No repair for takeovered array
8278                  * imsm doesn't support raid4
8279                  */
8280                 return NULL;
8281
8282         if (imsm_check_degraded(super, dev, failed, MAP_0) !=
8283                         IMSM_T_STATE_DEGRADED)
8284                 return NULL;
8285
8286         if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
8287                 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8288                 return NULL;
8289         }
8290
8291         /*
8292          * If there are any failed disks check state of the other volume.
8293          * Block rebuild if the another one is failed until failed disks
8294          * are removed from container.
8295          */
8296         if (failed) {
8297                 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8298                         MAX_RAID_SERIAL_LEN, dev->volume);
8299                 /* check if states of the other volumes allow for rebuild */
8300                 for (i = 0; i <  super->anchor->num_raid_devs; i++) {
8301                         if (i != inst) {
8302                                 allowed = imsm_rebuild_allowed(a->container,
8303                                                                i, failed);
8304                                 if (!allowed)
8305                                         return NULL;
8306                         }
8307                 }
8308         }
8309
8310         /* For each slot, if it is not working, find a spare */
8311         for (i = 0; i < a->info.array.raid_disks; i++) {
8312                 for (d = a->info.devs ; d ; d = d->next)
8313                         if (d->disk.raid_disk == i)
8314                                 break;
8315                 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
8316                 if (d && (d->state_fd >= 0))
8317                         continue;
8318
8319                 /*
8320                  * OK, this device needs recovery.  Try to re-add the
8321                  * previous occupant of this slot, if this fails see if
8322                  * we can continue the assimilation of a spare that was
8323                  * partially assimilated, finally try to activate a new
8324                  * spare.
8325                  */
8326                 dl = imsm_readd(super, i, a);
8327                 if (!dl)
8328                         dl = imsm_add_spare(super, i, a, 0, rv);
8329                 if (!dl)
8330                         dl = imsm_add_spare(super, i, a, 1, rv);
8331                 if (!dl)
8332                         continue;
8333
8334                 /* found a usable disk with enough space */
8335                 di = xcalloc(1, sizeof(*di));
8336
8337                 /* dl->index will be -1 in the case we are activating a
8338                  * pristine spare.  imsm_process_update() will create a
8339                  * new index in this case.  Once a disk is found to be
8340                  * failed in all member arrays it is kicked from the
8341                  * metadata
8342                  */
8343                 di->disk.number = dl->index;
8344
8345                 /* (ab)use di->devs to store a pointer to the device
8346                  * we chose
8347                  */
8348                 di->devs = (struct mdinfo *) dl;
8349
8350                 di->disk.raid_disk = i;
8351                 di->disk.major = dl->major;
8352                 di->disk.minor = dl->minor;
8353                 di->disk.state = 0;
8354                 di->recovery_start = 0;
8355                 di->data_offset = pba_of_lba0(map);
8356                 di->component_size = a->info.component_size;
8357                 di->container_member = inst;
8358                 di->bb.supported = 0;
8359                 super->random = random32();
8360                 di->next = rv;
8361                 rv = di;
8362                 num_spares++;
8363                 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
8364                         i, di->data_offset);
8365         }
8366
8367         if (!rv)
8368                 /* No spares found */
8369                 return rv;
8370         /* Now 'rv' has a list of devices to return.
8371          * Create a metadata_update record to update the
8372          * disk_ord_tbl for the array
8373          */
8374         mu = xmalloc(sizeof(*mu));
8375         mu->buf = xcalloc(num_spares,
8376                           sizeof(struct imsm_update_activate_spare));
8377         mu->space = NULL;
8378         mu->space_list = NULL;
8379         mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
8380         mu->next = *updates;
8381         u = (struct imsm_update_activate_spare *) mu->buf;
8382
8383         for (di = rv ; di ; di = di->next) {
8384                 u->type = update_activate_spare;
8385                 u->dl = (struct dl *) di->devs;
8386                 di->devs = NULL;
8387                 u->slot = di->disk.raid_disk;
8388                 u->array = inst;
8389                 u->next = u + 1;
8390                 u++;
8391         }
8392         (u-1)->next = NULL;
8393         *updates = mu;
8394
8395         return rv;
8396 }
8397
8398 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8399 {
8400         struct imsm_dev *dev = get_imsm_dev(super, idx);
8401         struct imsm_map *map = get_imsm_map(dev, MAP_0);
8402         struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
8403         struct disk_info *inf = get_disk_info(u);
8404         struct imsm_disk *disk;
8405         int i;
8406         int j;
8407
8408         for (i = 0; i < map->num_members; i++) {
8409                 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
8410                 for (j = 0; j < new_map->num_members; j++)
8411                         if (serialcmp(disk->serial, inf[j].serial) == 0)
8412                                 return 1;
8413         }
8414
8415         return 0;
8416 }
8417
8418 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
8419 {
8420         struct dl *dl;
8421
8422         for (dl = super->disks; dl; dl = dl->next)
8423                 if (dl->major == major &&  dl->minor == minor)
8424                         return dl;
8425         return NULL;
8426 }
8427
8428 static int remove_disk_super(struct intel_super *super, int major, int minor)
8429 {
8430         struct dl *prev;
8431         struct dl *dl;
8432
8433         prev = NULL;
8434         for (dl = super->disks; dl; dl = dl->next) {
8435                 if (dl->major == major && dl->minor == minor) {
8436                         /* remove */
8437                         if (prev)
8438                                 prev->next = dl->next;
8439                         else
8440                                 super->disks = dl->next;
8441                         dl->next = NULL;
8442                         __free_imsm_disk(dl);
8443                         dprintf("removed %x:%x\n", major, minor);
8444                         break;
8445                 }
8446                 prev = dl;
8447         }
8448         return 0;
8449 }
8450
8451 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
8452
8453 static int add_remove_disk_update(struct intel_super *super)
8454 {
8455         int check_degraded = 0;
8456         struct dl *disk;
8457
8458         /* add/remove some spares to/from the metadata/contrainer */
8459         while (super->disk_mgmt_list) {
8460                 struct dl *disk_cfg;
8461
8462                 disk_cfg = super->disk_mgmt_list;
8463                 super->disk_mgmt_list = disk_cfg->next;
8464                 disk_cfg->next = NULL;
8465
8466                 if (disk_cfg->action == DISK_ADD) {
8467                         disk_cfg->next = super->disks;
8468                         super->disks = disk_cfg;
8469                         check_degraded = 1;
8470                         dprintf("added %x:%x\n",
8471                                 disk_cfg->major, disk_cfg->minor);
8472                 } else if (disk_cfg->action == DISK_REMOVE) {
8473                         dprintf("Disk remove action processed: %x.%x\n",
8474                                 disk_cfg->major, disk_cfg->minor);
8475                         disk = get_disk_super(super,
8476                                               disk_cfg->major,
8477                                               disk_cfg->minor);
8478                         if (disk) {
8479                                 /* store action status */
8480                                 disk->action = DISK_REMOVE;
8481                                 /* remove spare disks only */
8482                                 if (disk->index == -1) {
8483                                         remove_disk_super(super,
8484                                                           disk_cfg->major,
8485                                                           disk_cfg->minor);
8486                                 }
8487                         }
8488                         /* release allocate disk structure */
8489                         __free_imsm_disk(disk_cfg);
8490                 }
8491         }
8492         return check_degraded;
8493 }
8494
8495 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
8496                                                 struct intel_super *super,
8497                                                 void ***space_list)
8498 {
8499         struct intel_dev *id;
8500         void **tofree = NULL;
8501         int ret_val = 0;
8502
8503         dprintf("(enter)\n");
8504         if (u->subdev < 0 || u->subdev > 1) {
8505                 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
8506                 return ret_val;
8507         }
8508         if (space_list == NULL || *space_list == NULL) {
8509                 dprintf("imsm: Error: Memory is not allocated\n");
8510                 return ret_val;
8511         }
8512
8513         for (id = super->devlist ; id; id = id->next) {
8514                 if (id->index == (unsigned)u->subdev) {
8515                         struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
8516                         struct imsm_map *map;
8517                         struct imsm_dev *new_dev =
8518                                 (struct imsm_dev *)*space_list;
8519                         struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8520                         int to_state;
8521                         struct dl *new_disk;
8522
8523                         if (new_dev == NULL)
8524                                 return ret_val;
8525                         *space_list = **space_list;
8526                         memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
8527                         map = get_imsm_map(new_dev, MAP_0);
8528                         if (migr_map) {
8529                                 dprintf("imsm: Error: migration in progress");
8530                                 return ret_val;
8531                         }
8532
8533                         to_state = map->map_state;
8534                         if ((u->new_level == 5) && (map->raid_level == 0)) {
8535                                 map->num_members++;
8536                                 /* this should not happen */
8537                                 if (u->new_disks[0] < 0) {
8538                                         map->failed_disk_num =
8539                                                 map->num_members - 1;
8540                                         to_state = IMSM_T_STATE_DEGRADED;
8541                                 } else
8542                                         to_state = IMSM_T_STATE_NORMAL;
8543                         }
8544                         migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
8545                         if (u->new_level > -1)
8546                                 map->raid_level = u->new_level;
8547                         migr_map = get_imsm_map(new_dev, MAP_1);
8548                         if ((u->new_level == 5) &&
8549                             (migr_map->raid_level == 0)) {
8550                                 int ord = map->num_members - 1;
8551                                 migr_map->num_members--;
8552                                 if (u->new_disks[0] < 0)
8553                                         ord |= IMSM_ORD_REBUILD;
8554                                 set_imsm_ord_tbl_ent(map,
8555                                                      map->num_members - 1,
8556                                                      ord);
8557                         }
8558                         id->dev = new_dev;
8559                         tofree = (void **)dev;
8560
8561                         /* update chunk size
8562                          */
8563                         if (u->new_chunksize > 0) {
8564                                 unsigned long long num_data_stripes;
8565                                 int used_disks =
8566                                         imsm_num_data_members(dev, MAP_0);
8567
8568                                 if (used_disks == 0)
8569                                         return ret_val;
8570
8571                                 map->blocks_per_strip =
8572                                         __cpu_to_le16(u->new_chunksize * 2);
8573                                 num_data_stripes =
8574                                         (join_u32(dev->size_low, dev->size_high)
8575                                         / used_disks);
8576                                 num_data_stripes /= map->blocks_per_strip;
8577                                 num_data_stripes /= map->num_domains;
8578                                 set_num_data_stripes(map, num_data_stripes);
8579                         }
8580
8581                         /* add disk
8582                          */
8583                         if (u->new_level != 5 || migr_map->raid_level != 0 ||
8584                             migr_map->raid_level == map->raid_level)
8585                                 goto skip_disk_add;
8586
8587                         if (u->new_disks[0] >= 0) {
8588                                 /* use passes spare
8589                                  */
8590                                 new_disk = get_disk_super(super,
8591                                                         major(u->new_disks[0]),
8592                                                         minor(u->new_disks[0]));
8593                                 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8594                                         major(u->new_disks[0]),
8595                                         minor(u->new_disks[0]),
8596                                         new_disk, new_disk->index);
8597                                 if (new_disk == NULL)
8598                                         goto error_disk_add;
8599
8600                                 new_disk->index = map->num_members - 1;
8601                                 /* slot to fill in autolayout
8602                                  */
8603                                 new_disk->raiddisk = new_disk->index;
8604                                 new_disk->disk.status |= CONFIGURED_DISK;
8605                                 new_disk->disk.status &= ~SPARE_DISK;
8606                         } else
8607                                 goto error_disk_add;
8608
8609 skip_disk_add:
8610                         *tofree = *space_list;
8611                         /* calculate new size
8612                          */
8613                         imsm_set_array_size(new_dev, -1);
8614
8615                         ret_val = 1;
8616                 }
8617         }
8618
8619         if (tofree)
8620                 *space_list = tofree;
8621         return ret_val;
8622
8623 error_disk_add:
8624         dprintf("Error: imsm: Cannot find disk.\n");
8625         return ret_val;
8626 }
8627
8628 static int apply_size_change_update(struct imsm_update_size_change *u,
8629                 struct intel_super *super)
8630 {
8631         struct intel_dev *id;
8632         int ret_val = 0;
8633
8634         dprintf("(enter)\n");
8635         if (u->subdev < 0 || u->subdev > 1) {
8636                 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
8637                 return ret_val;
8638         }
8639
8640         for (id = super->devlist ; id; id = id->next) {
8641                 if (id->index == (unsigned)u->subdev) {
8642                         struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
8643                         struct imsm_map *map = get_imsm_map(dev, MAP_0);
8644                         int used_disks = imsm_num_data_members(dev, MAP_0);
8645                         unsigned long long blocks_per_member;
8646                         unsigned long long num_data_stripes;
8647
8648                         /* calculate new size
8649                          */
8650                         blocks_per_member = u->new_size / used_disks;
8651                         num_data_stripes = blocks_per_member /
8652                                            map->blocks_per_strip;
8653                         num_data_stripes /= map->num_domains;
8654                         dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8655                                 u->new_size, blocks_per_member,
8656                                 num_data_stripes);
8657                         set_blocks_per_member(map, blocks_per_member);
8658                         set_num_data_stripes(map, num_data_stripes);
8659                         imsm_set_array_size(dev, u->new_size);
8660
8661                         ret_val = 1;
8662                         break;
8663                 }
8664         }
8665
8666         return ret_val;
8667 }
8668
8669 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
8670                                        struct intel_super *super,
8671                                        struct active_array *active_array)
8672 {
8673         struct imsm_super *mpb = super->anchor;
8674         struct imsm_dev *dev = get_imsm_dev(super, u->array);
8675         struct imsm_map *map = get_imsm_map(dev, MAP_0);
8676         struct imsm_map *migr_map;
8677         struct active_array *a;
8678         struct imsm_disk *disk;
8679         __u8 to_state;
8680         struct dl *dl;
8681         unsigned int found;
8682         int failed;
8683         int victim;
8684         int i;
8685         int second_map_created = 0;
8686
8687         for (; u; u = u->next) {
8688                 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
8689
8690                 if (victim < 0)
8691                         return 0;
8692
8693                 for (dl = super->disks; dl; dl = dl->next)
8694                         if (dl == u->dl)
8695                                 break;
8696
8697                 if (!dl) {
8698                         pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8699                                 u->dl->index);
8700                         return 0;
8701                 }
8702
8703                 /* count failures (excluding rebuilds and the victim)
8704                  * to determine map[0] state
8705                  */
8706                 failed = 0;
8707                 for (i = 0; i < map->num_members; i++) {
8708                         if (i == u->slot)
8709                                 continue;
8710                         disk = get_imsm_disk(super,
8711                                              get_imsm_disk_idx(dev, i, MAP_X));
8712                         if (!disk || is_failed(disk))
8713                                 failed++;
8714                 }
8715
8716                 /* adding a pristine spare, assign a new index */
8717                 if (dl->index < 0) {
8718                         dl->index = super->anchor->num_disks;
8719                         super->anchor->num_disks++;
8720                 }
8721                 disk = &dl->disk;
8722                 disk->status |= CONFIGURED_DISK;
8723                 disk->status &= ~SPARE_DISK;
8724
8725                 /* mark rebuild */
8726                 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
8727                 if (!second_map_created) {
8728                         second_map_created = 1;
8729                         map->map_state = IMSM_T_STATE_DEGRADED;
8730                         migrate(dev, super, to_state, MIGR_REBUILD);
8731                 } else
8732                         map->map_state = to_state;
8733                 migr_map = get_imsm_map(dev, MAP_1);
8734                 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
8735                 set_imsm_ord_tbl_ent(migr_map, u->slot,
8736                                      dl->index | IMSM_ORD_REBUILD);
8737
8738                 /* update the family_num to mark a new container
8739                  * generation, being careful to record the existing
8740                  * family_num in orig_family_num to clean up after
8741                  * earlier mdadm versions that neglected to set it.
8742                  */
8743                 if (mpb->orig_family_num == 0)
8744                         mpb->orig_family_num = mpb->family_num;
8745                 mpb->family_num += super->random;
8746
8747                 /* count arrays using the victim in the metadata */
8748                 found = 0;
8749                 for (a = active_array; a ; a = a->next) {
8750                         dev = get_imsm_dev(super, a->info.container_member);
8751                         map = get_imsm_map(dev, MAP_0);
8752
8753                         if (get_imsm_disk_slot(map, victim) >= 0)
8754                                 found++;
8755                 }
8756
8757                 /* delete the victim if it is no longer being
8758                  * utilized anywhere
8759                  */
8760                 if (!found) {
8761                         struct dl **dlp;
8762
8763                         /* We know that 'manager' isn't touching anything,
8764                          * so it is safe to delete
8765                          */
8766                         for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
8767                                 if ((*dlp)->index == victim)
8768                                         break;
8769
8770                         /* victim may be on the missing list */
8771                         if (!*dlp)
8772                                 for (dlp = &super->missing; *dlp;
8773                                      dlp = &(*dlp)->next)
8774                                         if ((*dlp)->index == victim)
8775                                                 break;
8776                         imsm_delete(super, dlp, victim);
8777                 }
8778         }
8779
8780         return 1;
8781 }
8782
8783 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
8784                                                 struct intel_super *super,
8785                                                 void ***space_list)
8786 {
8787         struct dl *new_disk;
8788         struct intel_dev *id;
8789         int i;
8790         int delta_disks = u->new_raid_disks - u->old_raid_disks;
8791         int disk_count = u->old_raid_disks;
8792         void **tofree = NULL;
8793         int devices_to_reshape = 1;
8794         struct imsm_super *mpb = super->anchor;
8795         int ret_val = 0;
8796         unsigned int dev_id;
8797
8798         dprintf("(enter)\n");
8799
8800         /* enable spares to use in array */
8801         for (i = 0; i < delta_disks; i++) {
8802                 new_disk = get_disk_super(super,
8803                                           major(u->new_disks[i]),
8804                                           minor(u->new_disks[i]));
8805                 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8806                         major(u->new_disks[i]), minor(u->new_disks[i]),
8807                         new_disk, new_disk->index);
8808                 if (new_disk == NULL ||
8809                     (new_disk->index >= 0 &&
8810                      new_disk->index < u->old_raid_disks))
8811                         goto update_reshape_exit;
8812                 new_disk->index = disk_count++;
8813                 /* slot to fill in autolayout
8814                  */
8815                 new_disk->raiddisk = new_disk->index;
8816                 new_disk->disk.status |=
8817                         CONFIGURED_DISK;
8818                 new_disk->disk.status &= ~SPARE_DISK;
8819         }
8820
8821         dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8822                 mpb->num_raid_devs);
8823         /* manage changes in volume
8824          */
8825         for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
8826                 void **sp = *space_list;
8827                 struct imsm_dev *newdev;
8828                 struct imsm_map *newmap, *oldmap;
8829
8830                 for (id = super->devlist ; id; id = id->next) {
8831                         if (id->index == dev_id)
8832                                 break;
8833                 }
8834                 if (id == NULL)
8835                         break;
8836                 if (!sp)
8837                         continue;
8838                 *space_list = *sp;
8839                 newdev = (void*)sp;
8840                 /* Copy the dev, but not (all of) the map */
8841                 memcpy(newdev, id->dev, sizeof(*newdev));
8842                 oldmap = get_imsm_map(id->dev, MAP_0);
8843                 newmap = get_imsm_map(newdev, MAP_0);
8844                 /* Copy the current map */
8845                 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
8846                 /* update one device only
8847                  */
8848                 if (devices_to_reshape) {
8849                         dprintf("imsm: modifying subdev: %i\n",
8850                                 id->index);
8851                         devices_to_reshape--;
8852                         newdev->vol.migr_state = 1;
8853                         newdev->vol.curr_migr_unit = 0;
8854                         set_migr_type(newdev, MIGR_GEN_MIGR);
8855                         newmap->num_members = u->new_raid_disks;
8856                         for (i = 0; i < delta_disks; i++) {
8857                                 set_imsm_ord_tbl_ent(newmap,
8858                                                      u->old_raid_disks + i,
8859                                                      u->old_raid_disks + i);
8860                         }
8861                         /* New map is correct, now need to save old map
8862                          */
8863                         newmap = get_imsm_map(newdev, MAP_1);
8864                         memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
8865
8866                         imsm_set_array_size(newdev, -1);
8867                 }
8868
8869                 sp = (void **)id->dev;
8870                 id->dev = newdev;
8871                 *sp = tofree;
8872                 tofree = sp;
8873
8874                 /* Clear migration record */
8875                 memset(super->migr_rec, 0, sizeof(struct migr_record));
8876         }
8877         if (tofree)
8878                 *space_list = tofree;
8879         ret_val = 1;
8880
8881 update_reshape_exit:
8882
8883         return ret_val;
8884 }
8885
8886 static int apply_takeover_update(struct imsm_update_takeover *u,
8887                                  struct intel_super *super,
8888                                  void ***space_list)
8889 {
8890         struct imsm_dev *dev = NULL;
8891         struct intel_dev *dv;
8892         struct imsm_dev *dev_new;
8893         struct imsm_map *map;
8894         struct dl *dm, *du;
8895         int i;
8896
8897         for (dv = super->devlist; dv; dv = dv->next)
8898                 if (dv->index == (unsigned int)u->subarray) {
8899                         dev = dv->dev;
8900                         break;
8901                 }
8902
8903         if (dev == NULL)
8904                 return 0;
8905
8906         map = get_imsm_map(dev, MAP_0);
8907
8908         if (u->direction == R10_TO_R0) {
8909                 unsigned long long num_data_stripes;
8910
8911                 map->num_domains = 1;
8912                 num_data_stripes = blocks_per_member(map);
8913                 num_data_stripes /= map->blocks_per_strip;
8914                 num_data_stripes /= map->num_domains;
8915                 set_num_data_stripes(map, num_data_stripes);
8916
8917                 /* Number of failed disks must be half of initial disk number */
8918                 if (imsm_count_failed(super, dev, MAP_0) !=
8919                                 (map->num_members / 2))
8920                         return 0;
8921
8922                 /* iterate through devices to mark removed disks as spare */
8923                 for (dm = super->disks; dm; dm = dm->next) {
8924                         if (dm->disk.status & FAILED_DISK) {
8925                                 int idx = dm->index;
8926                                 /* update indexes on the disk list */
8927 /* FIXME this loop-with-the-loop looks wrong,  I'm not convinced
8928    the index values will end up being correct.... NB */
8929                                 for (du = super->disks; du; du = du->next)
8930                                         if (du->index > idx)
8931                                                 du->index--;
8932                                 /* mark as spare disk */
8933                                 mark_spare(dm);
8934                         }
8935                 }
8936                 /* update map */
8937                 map->num_members = map->num_members / 2;
8938                 map->map_state = IMSM_T_STATE_NORMAL;
8939                 map->num_domains = 1;
8940                 map->raid_level = 0;
8941                 map->failed_disk_num = -1;
8942         }
8943
8944         if (u->direction == R0_TO_R10) {
8945                 void **space;
8946                 /* update slots in current disk list */
8947                 for (dm = super->disks; dm; dm = dm->next) {
8948                         if (dm->index >= 0)
8949                                 dm->index *= 2;
8950                 }
8951                 /* create new *missing* disks */
8952                 for (i = 0; i < map->num_members; i++) {
8953                         space = *space_list;
8954                         if (!space)
8955                                 continue;
8956                         *space_list = *space;
8957                         du = (void *)space;
8958                         memcpy(du, super->disks, sizeof(*du));
8959                         du->fd = -1;
8960                         du->minor = 0;
8961                         du->major = 0;
8962                         du->index = (i * 2) + 1;
8963                         sprintf((char *)du->disk.serial,
8964                                 " MISSING_%d", du->index);
8965                         sprintf((char *)du->serial,
8966                                 "MISSING_%d", du->index);
8967                         du->next = super->missing;
8968                         super->missing = du;
8969                 }
8970                 /* create new dev and map */
8971                 space = *space_list;
8972                 if (!space)
8973                         return 0;
8974                 *space_list = *space;
8975                 dev_new = (void *)space;
8976                 memcpy(dev_new, dev, sizeof(*dev));
8977                 /* update new map */
8978                 map = get_imsm_map(dev_new, MAP_0);
8979                 map->num_members = map->num_members * 2;
8980                 map->map_state = IMSM_T_STATE_DEGRADED;
8981                 map->num_domains = 2;
8982                 map->raid_level = 1;
8983                 /* replace dev<->dev_new */
8984                 dv->dev = dev_new;
8985         }
8986         /* update disk order table */
8987         for (du = super->disks; du; du = du->next)
8988                 if (du->index >= 0)
8989                         set_imsm_ord_tbl_ent(map, du->index, du->index);
8990         for (du = super->missing; du; du = du->next)
8991                 if (du->index >= 0) {
8992                         set_imsm_ord_tbl_ent(map, du->index, du->index);
8993                         mark_missing(super, dv->dev, &du->disk, du->index);
8994                 }
8995
8996         return 1;
8997 }
8998
8999 static void imsm_process_update(struct supertype *st,
9000                                 struct metadata_update *update)
9001 {
9002         /**
9003          * crack open the metadata_update envelope to find the update record
9004          * update can be one of:
9005          *    update_reshape_container_disks - all the arrays in the container
9006          *      are being reshaped to have more devices.  We need to mark
9007          *      the arrays for general migration and convert selected spares
9008          *      into active devices.
9009          *    update_activate_spare - a spare device has replaced a failed
9010          *      device in an array, update the disk_ord_tbl.  If this disk is
9011          *      present in all member arrays then also clear the SPARE_DISK
9012          *      flag
9013          *    update_create_array
9014          *    update_kill_array
9015          *    update_rename_array
9016          *    update_add_remove_disk
9017          */
9018         struct intel_super *super = st->sb;
9019         struct imsm_super *mpb;
9020         enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
9021
9022         /* update requires a larger buf but the allocation failed */
9023         if (super->next_len && !super->next_buf) {
9024                 super->next_len = 0;
9025                 return;
9026         }
9027
9028         if (super->next_buf) {
9029                 memcpy(super->next_buf, super->buf, super->len);
9030                 free(super->buf);
9031                 super->len = super->next_len;
9032                 super->buf = super->next_buf;
9033
9034                 super->next_len = 0;
9035                 super->next_buf = NULL;
9036         }
9037
9038         mpb = super->anchor;
9039
9040         switch (type) {
9041         case update_general_migration_checkpoint: {
9042                 struct intel_dev *id;
9043                 struct imsm_update_general_migration_checkpoint *u =
9044                                                         (void *)update->buf;
9045
9046                 dprintf("called for update_general_migration_checkpoint\n");
9047
9048                 /* find device under general migration */
9049                 for (id = super->devlist ; id; id = id->next) {
9050                         if (is_gen_migration(id->dev)) {
9051                                 id->dev->vol.curr_migr_unit =
9052                                         __cpu_to_le32(u->curr_migr_unit);
9053                                 super->updates_pending++;
9054                         }
9055                 }
9056                 break;
9057         }
9058         case update_takeover: {
9059                 struct imsm_update_takeover *u = (void *)update->buf;
9060                 if (apply_takeover_update(u, super, &update->space_list)) {
9061                         imsm_update_version_info(super);
9062                         super->updates_pending++;
9063                 }
9064                 break;
9065         }
9066
9067         case update_reshape_container_disks: {
9068                 struct imsm_update_reshape *u = (void *)update->buf;
9069                 if (apply_reshape_container_disks_update(
9070                             u, super, &update->space_list))
9071                         super->updates_pending++;
9072                 break;
9073         }
9074         case update_reshape_migration: {
9075                 struct imsm_update_reshape_migration *u = (void *)update->buf;
9076                 if (apply_reshape_migration_update(
9077                             u, super, &update->space_list))
9078                         super->updates_pending++;
9079                 break;
9080         }
9081         case update_size_change: {
9082                 struct imsm_update_size_change *u = (void *)update->buf;
9083                 if (apply_size_change_update(u, super))
9084                         super->updates_pending++;
9085                 break;
9086         }
9087         case update_activate_spare: {
9088                 struct imsm_update_activate_spare *u = (void *) update->buf;
9089                 if (apply_update_activate_spare(u, super, st->arrays))
9090                         super->updates_pending++;
9091                 break;
9092         }
9093         case update_create_array: {
9094                 /* someone wants to create a new array, we need to be aware of
9095                  * a few races/collisions:
9096                  * 1/ 'Create' called by two separate instances of mdadm
9097                  * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9098                  *     devices that have since been assimilated via
9099                  *     activate_spare.
9100                  * In the event this update can not be carried out mdadm will
9101                  * (FIX ME) notice that its update did not take hold.
9102                  */
9103                 struct imsm_update_create_array *u = (void *) update->buf;
9104                 struct intel_dev *dv;
9105                 struct imsm_dev *dev;
9106                 struct imsm_map *map, *new_map;
9107                 unsigned long long start, end;
9108                 unsigned long long new_start, new_end;
9109                 int i;
9110                 struct disk_info *inf;
9111                 struct dl *dl;
9112
9113                 /* handle racing creates: first come first serve */
9114                 if (u->dev_idx < mpb->num_raid_devs) {
9115                         dprintf("subarray %d already defined\n", u->dev_idx);
9116                         goto create_error;
9117                 }
9118
9119                 /* check update is next in sequence */
9120                 if (u->dev_idx != mpb->num_raid_devs) {
9121                         dprintf("can not create array %d expected index %d\n",
9122                                 u->dev_idx, mpb->num_raid_devs);
9123                         goto create_error;
9124                 }
9125
9126                 new_map = get_imsm_map(&u->dev, MAP_0);
9127                 new_start = pba_of_lba0(new_map);
9128                 new_end = new_start + blocks_per_member(new_map);
9129                 inf = get_disk_info(u);
9130
9131                 /* handle activate_spare versus create race:
9132                  * check to make sure that overlapping arrays do not include
9133                  * overalpping disks
9134                  */
9135                 for (i = 0; i < mpb->num_raid_devs; i++) {
9136                         dev = get_imsm_dev(super, i);
9137                         map = get_imsm_map(dev, MAP_0);
9138                         start = pba_of_lba0(map);
9139                         end = start + blocks_per_member(map);
9140                         if ((new_start >= start && new_start <= end) ||
9141                             (start >= new_start && start <= new_end))
9142                                 /* overlap */;
9143                         else
9144                                 continue;
9145
9146                         if (disks_overlap(super, i, u)) {
9147                                 dprintf("arrays overlap\n");
9148                                 goto create_error;
9149                         }
9150                 }
9151
9152                 /* check that prepare update was successful */
9153                 if (!update->space) {
9154                         dprintf("prepare update failed\n");
9155                         goto create_error;
9156                 }
9157
9158                 /* check that all disks are still active before committing
9159                  * changes.  FIXME: could we instead handle this by creating a
9160                  * degraded array?  That's probably not what the user expects,
9161                  * so better to drop this update on the floor.
9162                  */
9163                 for (i = 0; i < new_map->num_members; i++) {
9164                         dl = serial_to_dl(inf[i].serial, super);
9165                         if (!dl) {
9166                                 dprintf("disk disappeared\n");
9167                                 goto create_error;
9168                         }
9169                 }
9170
9171                 super->updates_pending++;
9172
9173                 /* convert spares to members and fixup ord_tbl */
9174                 for (i = 0; i < new_map->num_members; i++) {
9175                         dl = serial_to_dl(inf[i].serial, super);
9176                         if (dl->index == -1) {
9177                                 dl->index = mpb->num_disks;
9178                                 mpb->num_disks++;
9179                                 dl->disk.status |= CONFIGURED_DISK;
9180                                 dl->disk.status &= ~SPARE_DISK;
9181                         }
9182                         set_imsm_ord_tbl_ent(new_map, i, dl->index);
9183                 }
9184
9185                 dv = update->space;
9186                 dev = dv->dev;
9187                 update->space = NULL;
9188                 imsm_copy_dev(dev, &u->dev);
9189                 dv->index = u->dev_idx;
9190                 dv->next = super->devlist;
9191                 super->devlist = dv;
9192                 mpb->num_raid_devs++;
9193
9194                 imsm_update_version_info(super);
9195                 break;
9196  create_error:
9197                 /* mdmon knows how to release update->space, but not
9198                  * ((struct intel_dev *) update->space)->dev
9199                  */
9200                 if (update->space) {
9201                         dv = update->space;
9202                         free(dv->dev);
9203                 }
9204                 break;
9205         }
9206         case update_kill_array: {
9207                 struct imsm_update_kill_array *u = (void *) update->buf;
9208                 int victim = u->dev_idx;
9209                 struct active_array *a;
9210                 struct intel_dev **dp;
9211                 struct imsm_dev *dev;
9212
9213                 /* sanity check that we are not affecting the uuid of
9214                  * active arrays, or deleting an active array
9215                  *
9216                  * FIXME when immutable ids are available, but note that
9217                  * we'll also need to fixup the invalidated/active
9218                  * subarray indexes in mdstat
9219                  */
9220                 for (a = st->arrays; a; a = a->next)
9221                         if (a->info.container_member >= victim)
9222                                 break;
9223                 /* by definition if mdmon is running at least one array
9224                  * is active in the container, so checking
9225                  * mpb->num_raid_devs is just extra paranoia
9226                  */
9227                 dev = get_imsm_dev(super, victim);
9228                 if (a || !dev || mpb->num_raid_devs == 1) {
9229                         dprintf("failed to delete subarray-%d\n", victim);
9230                         break;
9231                 }
9232
9233                 for (dp = &super->devlist; *dp;)
9234                         if ((*dp)->index == (unsigned)super->current_vol) {
9235                                 *dp = (*dp)->next;
9236                         } else {
9237                                 if ((*dp)->index > (unsigned)victim)
9238                                         (*dp)->index--;
9239                                 dp = &(*dp)->next;
9240                         }
9241                 mpb->num_raid_devs--;
9242                 super->updates_pending++;
9243                 break;
9244         }
9245         case update_rename_array: {
9246                 struct imsm_update_rename_array *u = (void *) update->buf;
9247                 char name[MAX_RAID_SERIAL_LEN+1];
9248                 int target = u->dev_idx;
9249                 struct active_array *a;
9250                 struct imsm_dev *dev;
9251
9252                 /* sanity check that we are not affecting the uuid of
9253                  * an active array
9254                  */
9255                 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
9256                 name[MAX_RAID_SERIAL_LEN] = '\0';
9257                 for (a = st->arrays; a; a = a->next)
9258                         if (a->info.container_member == target)
9259                                 break;
9260                 dev = get_imsm_dev(super, u->dev_idx);
9261                 if (a || !dev || !check_name(super, name, 1)) {
9262                         dprintf("failed to rename subarray-%d\n", target);
9263                         break;
9264                 }
9265
9266                 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
9267                 super->updates_pending++;
9268                 break;
9269         }
9270         case update_add_remove_disk: {
9271                 /* we may be able to repair some arrays if disks are
9272                  * being added, check the status of add_remove_disk
9273                  * if discs has been added.
9274                  */
9275                 if (add_remove_disk_update(super)) {
9276                         struct active_array *a;
9277
9278                         super->updates_pending++;
9279                         for (a = st->arrays; a; a = a->next)
9280                                 a->check_degraded = 1;
9281                 }
9282                 break;
9283         }
9284         case update_prealloc_badblocks_mem:
9285                 break;
9286         default:
9287                 pr_err("error: unsuported process update type:(type: %d)\n",    type);
9288         }
9289 }
9290
9291 static struct mdinfo *get_spares_for_grow(struct supertype *st);
9292
9293 static int imsm_prepare_update(struct supertype *st,
9294                                struct metadata_update *update)
9295 {
9296         /**
9297          * Allocate space to hold new disk entries, raid-device entries or a new
9298          * mpb if necessary.  The manager synchronously waits for updates to
9299          * complete in the monitor, so new mpb buffers allocated here can be
9300          * integrated by the monitor thread without worrying about live pointers
9301          * in the manager thread.
9302          */
9303         enum imsm_update_type type;
9304         struct intel_super *super = st->sb;
9305         unsigned int sector_size = super->sector_size;
9306         struct imsm_super *mpb = super->anchor;
9307         size_t buf_len;
9308         size_t len = 0;
9309
9310         if (update->len < (int)sizeof(type))
9311                 return 0;
9312
9313         type = *(enum imsm_update_type *) update->buf;
9314
9315         switch (type) {
9316         case update_general_migration_checkpoint:
9317                 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
9318                         return 0;
9319                 dprintf("called for update_general_migration_checkpoint\n");
9320                 break;
9321         case update_takeover: {
9322                 struct imsm_update_takeover *u = (void *)update->buf;
9323                 if (update->len < (int)sizeof(*u))
9324                         return 0;
9325                 if (u->direction == R0_TO_R10) {
9326                         void **tail = (void **)&update->space_list;
9327                         struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
9328                         struct imsm_map *map = get_imsm_map(dev, MAP_0);
9329                         int num_members = map->num_members;
9330                         void *space;
9331                         int size, i;
9332                         /* allocate memory for added disks */
9333                         for (i = 0; i < num_members; i++) {
9334                                 size = sizeof(struct dl);
9335                                 space = xmalloc(size);
9336                                 *tail = space;
9337                                 tail = space;
9338                                 *tail = NULL;
9339                         }
9340                         /* allocate memory for new device */
9341                         size = sizeof_imsm_dev(super->devlist->dev, 0) +
9342                                 (num_members * sizeof(__u32));
9343                         space = xmalloc(size);
9344                         *tail = space;
9345                         tail = space;
9346                         *tail = NULL;
9347                         len = disks_to_mpb_size(num_members * 2);
9348                 }
9349
9350                 break;
9351         }
9352         case update_reshape_container_disks: {
9353                 /* Every raid device in the container is about to
9354                  * gain some more devices, and we will enter a
9355                  * reconfiguration.
9356                  * So each 'imsm_map' will be bigger, and the imsm_vol
9357                  * will now hold 2 of them.
9358                  * Thus we need new 'struct imsm_dev' allocations sized
9359                  * as sizeof_imsm_dev but with more devices in both maps.
9360                  */
9361                 struct imsm_update_reshape *u = (void *)update->buf;
9362                 struct intel_dev *dl;
9363                 void **space_tail = (void**)&update->space_list;
9364
9365                 if (update->len < (int)sizeof(*u))
9366                         return 0;
9367
9368                 dprintf("for update_reshape\n");
9369
9370                 for (dl = super->devlist; dl; dl = dl->next) {
9371                         int size = sizeof_imsm_dev(dl->dev, 1);
9372                         void *s;
9373                         if (u->new_raid_disks > u->old_raid_disks)
9374                                 size += sizeof(__u32)*2*
9375                                         (u->new_raid_disks - u->old_raid_disks);
9376                         s = xmalloc(size);
9377                         *space_tail = s;
9378                         space_tail = s;
9379                         *space_tail = NULL;
9380                 }
9381
9382                 len = disks_to_mpb_size(u->new_raid_disks);
9383                 dprintf("New anchor length is %llu\n", (unsigned long long)len);
9384                 break;
9385         }
9386         case update_reshape_migration: {
9387                 /* for migration level 0->5 we need to add disks
9388                  * so the same as for container operation we will copy
9389                  * device to the bigger location.
9390                  * in memory prepared device and new disk area are prepared
9391                  * for usage in process update
9392                  */
9393                 struct imsm_update_reshape_migration *u = (void *)update->buf;
9394                 struct intel_dev *id;
9395                 void **space_tail = (void **)&update->space_list;
9396                 int size;
9397                 void *s;
9398                 int current_level = -1;
9399
9400                 if (update->len < (int)sizeof(*u))
9401                         return 0;
9402
9403                 dprintf("for update_reshape\n");
9404
9405                 /* add space for bigger array in update
9406                  */
9407                 for (id = super->devlist; id; id = id->next) {
9408                         if (id->index == (unsigned)u->subdev) {
9409                                 size = sizeof_imsm_dev(id->dev, 1);
9410                                 if (u->new_raid_disks > u->old_raid_disks)
9411                                         size += sizeof(__u32)*2*
9412                                         (u->new_raid_disks - u->old_raid_disks);
9413                                 s = xmalloc(size);
9414                                 *space_tail = s;
9415                                 space_tail = s;
9416                                 *space_tail = NULL;
9417                                 break;
9418                         }
9419                 }
9420                 if (update->space_list == NULL)
9421                         break;
9422
9423                 /* add space for disk in update
9424                  */
9425                 size = sizeof(struct dl);
9426                 s = xmalloc(size);
9427                 *space_tail = s;
9428                 space_tail = s;
9429                 *space_tail = NULL;
9430
9431                 /* add spare device to update
9432                  */
9433                 for (id = super->devlist ; id; id = id->next)
9434                         if (id->index == (unsigned)u->subdev) {
9435                                 struct imsm_dev *dev;
9436                                 struct imsm_map *map;
9437
9438                                 dev = get_imsm_dev(super, u->subdev);
9439                                 map = get_imsm_map(dev, MAP_0);
9440                                 current_level = map->raid_level;
9441                                 break;
9442                         }
9443                 if (u->new_level == 5 && u->new_level != current_level) {
9444                         struct mdinfo *spares;
9445
9446                         spares = get_spares_for_grow(st);
9447                         if (spares) {
9448                                 struct dl *dl;
9449                                 struct mdinfo *dev;
9450
9451                                 dev = spares->devs;
9452                                 if (dev) {
9453                                         u->new_disks[0] =
9454                                                 makedev(dev->disk.major,
9455                                                         dev->disk.minor);
9456                                         dl = get_disk_super(super,
9457                                                             dev->disk.major,
9458                                                             dev->disk.minor);
9459                                         dl->index = u->old_raid_disks;
9460                                         dev = dev->next;
9461                                 }
9462                                 sysfs_free(spares);
9463                         }
9464                 }
9465                 len = disks_to_mpb_size(u->new_raid_disks);
9466                 dprintf("New anchor length is %llu\n", (unsigned long long)len);
9467                 break;
9468         }
9469         case update_size_change: {
9470                 if (update->len < (int)sizeof(struct imsm_update_size_change))
9471                         return 0;
9472                 break;
9473         }
9474         case update_activate_spare: {
9475                 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
9476                         return 0;
9477                 break;
9478         }
9479         case update_create_array: {
9480                 struct imsm_update_create_array *u = (void *) update->buf;
9481                 struct intel_dev *dv;
9482                 struct imsm_dev *dev = &u->dev;
9483                 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9484                 struct dl *dl;
9485                 struct disk_info *inf;
9486                 int i;
9487                 int activate = 0;
9488
9489                 if (update->len < (int)sizeof(*u))
9490                         return 0;
9491
9492                 inf = get_disk_info(u);
9493                 len = sizeof_imsm_dev(dev, 1);
9494                 /* allocate a new super->devlist entry */
9495                 dv = xmalloc(sizeof(*dv));
9496                 dv->dev = xmalloc(len);
9497                 update->space = dv;
9498
9499                 /* count how many spares will be converted to members */
9500                 for (i = 0; i < map->num_members; i++) {
9501                         dl = serial_to_dl(inf[i].serial, super);
9502                         if (!dl) {
9503                                 /* hmm maybe it failed?, nothing we can do about
9504                                  * it here
9505                                  */
9506                                 continue;
9507                         }
9508                         if (count_memberships(dl, super) == 0)
9509                                 activate++;
9510                 }
9511                 len += activate * sizeof(struct imsm_disk);
9512                 break;
9513         }
9514         case update_kill_array: {
9515                 if (update->len < (int)sizeof(struct imsm_update_kill_array))
9516                         return 0;
9517                 break;
9518         }
9519         case update_rename_array: {
9520                 if (update->len < (int)sizeof(struct imsm_update_rename_array))
9521                         return 0;
9522                 break;
9523         }
9524         case update_add_remove_disk:
9525                 /* no update->len needed */
9526                 break;
9527         case update_prealloc_badblocks_mem:
9528                 super->extra_space += sizeof(struct bbm_log) -
9529                         get_imsm_bbm_log_size(super->bbm_log);
9530                 break;
9531         default:
9532                 return 0;
9533         }
9534
9535         /* check if we need a larger metadata buffer */
9536         if (super->next_buf)
9537                 buf_len = super->next_len;
9538         else
9539                 buf_len = super->len;
9540
9541         if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
9542                 /* ok we need a larger buf than what is currently allocated
9543                  * if this allocation fails process_update will notice that
9544                  * ->next_len is set and ->next_buf is NULL
9545                  */
9546                 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
9547                                    super->extra_space + len, sector_size);
9548                 if (super->next_buf)
9549                         free(super->next_buf);
9550
9551                 super->next_len = buf_len;
9552                 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
9553                         memset(super->next_buf, 0, buf_len);
9554                 else
9555                         super->next_buf = NULL;
9556         }
9557         return 1;
9558 }
9559
9560 /* must be called while manager is quiesced */
9561 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
9562 {
9563         struct imsm_super *mpb = super->anchor;
9564         struct dl *iter;
9565         struct imsm_dev *dev;
9566         struct imsm_map *map;
9567         unsigned int i, j, num_members;
9568         __u32 ord;
9569         struct bbm_log *log = super->bbm_log;
9570
9571         dprintf("deleting device[%d] from imsm_super\n", index);
9572
9573         /* shift all indexes down one */
9574         for (iter = super->disks; iter; iter = iter->next)
9575                 if (iter->index > (int)index)
9576                         iter->index--;
9577         for (iter = super->missing; iter; iter = iter->next)
9578                 if (iter->index > (int)index)
9579                         iter->index--;
9580
9581         for (i = 0; i < mpb->num_raid_devs; i++) {
9582                 dev = get_imsm_dev(super, i);
9583                 map = get_imsm_map(dev, MAP_0);
9584                 num_members = map->num_members;
9585                 for (j = 0; j < num_members; j++) {
9586                         /* update ord entries being careful not to propagate
9587                          * ord-flags to the first map
9588                          */
9589                         ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
9590
9591                         if (ord_to_idx(ord) <= index)
9592                                 continue;
9593
9594                         map = get_imsm_map(dev, MAP_0);
9595                         set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
9596                         map = get_imsm_map(dev, MAP_1);
9597                         if (map)
9598                                 set_imsm_ord_tbl_ent(map, j, ord - 1);
9599                 }
9600         }
9601
9602         for (i = 0; i < log->entry_count; i++) {
9603                 struct bbm_log_entry *entry = &log->marked_block_entries[i];
9604
9605                 if (entry->disk_ordinal <= index)
9606                         continue;
9607                 entry->disk_ordinal--;
9608         }
9609
9610         mpb->num_disks--;
9611         super->updates_pending++;
9612         if (*dlp) {
9613                 struct dl *dl = *dlp;
9614
9615                 *dlp = (*dlp)->next;
9616                 __free_imsm_disk(dl);
9617         }
9618 }
9619 #endif /* MDASSEMBLE */
9620
9621 static void close_targets(int *targets, int new_disks)
9622 {
9623         int i;
9624
9625         if (!targets)
9626                 return;
9627
9628         for (i = 0; i < new_disks; i++) {
9629                 if (targets[i] >= 0) {
9630                         close(targets[i]);
9631                         targets[i] = -1;
9632                 }
9633         }
9634 }
9635
9636 static int imsm_get_allowed_degradation(int level, int raid_disks,
9637                                         struct intel_super *super,
9638                                         struct imsm_dev *dev)
9639 {
9640         switch (level) {
9641         case 1:
9642         case 10:{
9643                 int ret_val = 0;
9644                 struct imsm_map *map;
9645                 int i;
9646
9647                 ret_val = raid_disks/2;
9648                 /* check map if all disks pairs not failed
9649                  * in both maps
9650                  */
9651                 map = get_imsm_map(dev, MAP_0);
9652                 for (i = 0; i < ret_val; i++) {
9653                         int degradation = 0;
9654                         if (get_imsm_disk(super, i) == NULL)
9655                                 degradation++;
9656                         if (get_imsm_disk(super, i + 1) == NULL)
9657                                 degradation++;
9658                         if (degradation == 2)
9659                                 return 0;
9660                 }
9661                 map = get_imsm_map(dev, MAP_1);
9662                 /* if there is no second map
9663                  * result can be returned
9664                  */
9665                 if (map == NULL)
9666                         return ret_val;
9667                 /* check degradation in second map
9668                  */
9669                 for (i = 0; i < ret_val; i++) {
9670                         int degradation = 0;
9671                 if (get_imsm_disk(super, i) == NULL)
9672                                 degradation++;
9673                         if (get_imsm_disk(super, i + 1) == NULL)
9674                                 degradation++;
9675                         if (degradation == 2)
9676                                 return 0;
9677                 }
9678                 return ret_val;
9679         }
9680         case 5:
9681                 return 1;
9682         case 6:
9683                 return 2;
9684         default:
9685                 return 0;
9686         }
9687 }
9688
9689 /*******************************************************************************
9690  * Function:    open_backup_targets
9691  * Description: Function opens file descriptors for all devices given in
9692  *              info->devs
9693  * Parameters:
9694  *      info            : general array info
9695  *      raid_disks      : number of disks
9696  *      raid_fds        : table of device's file descriptors
9697  *      super           : intel super for raid10 degradation check
9698  *      dev             : intel device for raid10 degradation check
9699  * Returns:
9700  *       0 : success
9701  *      -1 : fail
9702  ******************************************************************************/
9703 int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds,
9704                         struct intel_super *super, struct imsm_dev *dev)
9705 {
9706         struct mdinfo *sd;
9707         int i;
9708         int opened = 0;
9709
9710         for (i = 0; i < raid_disks; i++)
9711                 raid_fds[i] = -1;
9712
9713         for (sd = info->devs ; sd ; sd = sd->next) {
9714                 char *dn;
9715
9716                 if (sd->disk.state & (1<<MD_DISK_FAULTY)) {
9717                         dprintf("disk is faulty!!\n");
9718                         continue;
9719                 }
9720
9721                 if (sd->disk.raid_disk >= raid_disks || sd->disk.raid_disk < 0)
9722                         continue;
9723
9724                 dn = map_dev(sd->disk.major,
9725                              sd->disk.minor, 1);
9726                 raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR);
9727                 if (raid_fds[sd->disk.raid_disk] < 0) {
9728                         pr_err("cannot open component\n");
9729                         continue;
9730                 }
9731                 opened++;
9732         }
9733         /* check if maximum array degradation level is not exceeded
9734         */
9735         if ((raid_disks - opened) >
9736             imsm_get_allowed_degradation(info->new_level, raid_disks,
9737                                          super, dev)) {
9738                 pr_err("Not enough disks can be opened.\n");
9739                 close_targets(raid_fds, raid_disks);
9740                 return -2;
9741         }
9742         return 0;
9743 }
9744
9745 /*******************************************************************************
9746  * Function:    validate_container_imsm
9747  * Description: This routine validates container after assemble,
9748  *              eg. if devices in container are under the same controller.
9749  *
9750  * Parameters:
9751  *      info    : linked list with info about devices used in array
9752  * Returns:
9753  *      1 : HBA mismatch
9754  *      0 : Success
9755  ******************************************************************************/
9756 int validate_container_imsm(struct mdinfo *info)
9757 {
9758         if (check_env("IMSM_NO_PLATFORM"))
9759                 return 0;
9760
9761         struct sys_dev *idev;
9762         struct sys_dev *hba = NULL;
9763         struct sys_dev *intel_devices = find_intel_devices();
9764         char *dev_path = devt_to_devpath(makedev(info->disk.major,
9765                                                                         info->disk.minor));
9766
9767         for (idev = intel_devices; idev; idev = idev->next) {
9768                 if (dev_path && strstr(dev_path, idev->path)) {
9769                         hba = idev;
9770                         break;
9771                 }
9772         }
9773         if (dev_path)
9774                 free(dev_path);
9775
9776         if (!hba) {
9777                 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9778                                 devid2kname(makedev(info->disk.major, info->disk.minor)));
9779                 return 1;
9780         }
9781
9782         const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
9783         struct mdinfo *dev;
9784
9785         for (dev = info->next; dev; dev = dev->next) {
9786                 dev_path = devt_to_devpath(makedev(dev->disk.major, dev->disk.minor));
9787
9788                 struct sys_dev *hba2 = NULL;
9789                 for (idev = intel_devices; idev; idev = idev->next) {
9790                         if (dev_path && strstr(dev_path, idev->path)) {
9791                                 hba2 = idev;
9792                                 break;
9793                         }
9794                 }
9795                 if (dev_path)
9796                         free(dev_path);
9797
9798                 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
9799                                 get_orom_by_device_id(hba2->dev_id);
9800
9801                 if (hba2 && hba->type != hba2->type) {
9802                         pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9803                                 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
9804                         return 1;
9805                 }
9806
9807                 if (orom != orom2) {
9808                         pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9809                                 "       This operation is not supported and can lead to data loss.\n");
9810                         return 1;
9811                 }
9812
9813                 if (!orom) {
9814                         pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9815                                 "       This operation is not supported and can lead to data loss.\n");
9816                         return 1;
9817                 }
9818         }
9819
9820         return 0;
9821 }
9822 #ifndef MDASSEMBLE
9823 /*******************************************************************************
9824 * Function:   imsm_record_badblock
9825 * Description: This routine stores new bad block record in BBM log
9826 *
9827 * Parameters:
9828 *     a         : array containing a bad block
9829 *     slot      : disk number containing a bad block
9830 *     sector    : bad block sector
9831 *     length    : bad block sectors range
9832 * Returns:
9833 *     1 : Success
9834 *     0 : Error
9835 ******************************************************************************/
9836 static int imsm_record_badblock(struct active_array *a, int slot,
9837                           unsigned long long sector, int length)
9838 {
9839         struct intel_super *super = a->container->sb;
9840         int ord;
9841         int ret;
9842
9843         ord = imsm_disk_slot_to_ord(a, slot);
9844         if (ord < 0)
9845                 return 0;
9846
9847         ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
9848                                    length);
9849         if (ret)
9850                 super->updates_pending++;
9851
9852         return ret;
9853 }
9854 /*******************************************************************************
9855 * Function:   imsm_clear_badblock
9856 * Description: This routine clears bad block record from BBM log
9857 *
9858 * Parameters:
9859 *     a         : array containing a bad block
9860 *     slot      : disk number containing a bad block
9861 *     sector    : bad block sector
9862 *     length    : bad block sectors range
9863 * Returns:
9864 *     1 : Success
9865 *     0 : Error
9866 ******************************************************************************/
9867 static int imsm_clear_badblock(struct active_array *a, int slot,
9868                         unsigned long long sector, int length)
9869 {
9870         struct intel_super *super = a->container->sb;
9871         int ord;
9872         int ret;
9873
9874         ord = imsm_disk_slot_to_ord(a, slot);
9875         if (ord < 0)
9876                 return 0;
9877
9878         ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
9879         if (ret)
9880                 super->updates_pending++;
9881
9882         return ret;
9883 }
9884 /*******************************************************************************
9885  * Function:    init_migr_record_imsm
9886  * Description: Function inits imsm migration record
9887  * Parameters:
9888  *      super   : imsm internal array info
9889  *      dev     : device under migration
9890  *      info    : general array info to find the smallest device
9891  * Returns:
9892  *      none
9893  ******************************************************************************/
9894 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
9895                            struct mdinfo *info)
9896 {
9897         struct intel_super *super = st->sb;
9898         struct migr_record *migr_rec = super->migr_rec;
9899         int new_data_disks;
9900         unsigned long long dsize, dev_sectors;
9901         long long unsigned min_dev_sectors = -1LLU;
9902         struct mdinfo *sd;
9903         char nm[30];
9904         int fd;
9905         struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
9906         struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
9907         unsigned long long num_migr_units;
9908         unsigned long long array_blocks;
9909
9910         memset(migr_rec, 0, sizeof(struct migr_record));
9911         migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
9912
9913         /* only ascending reshape supported now */
9914         migr_rec->ascending_migr = __cpu_to_le32(1);
9915
9916         migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
9917                 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
9918         migr_rec->dest_depth_per_unit *=
9919                 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
9920         new_data_disks = imsm_num_data_members(dev, MAP_0);
9921         migr_rec->blocks_per_unit =
9922                 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
9923         migr_rec->dest_depth_per_unit =
9924                 __cpu_to_le32(migr_rec->dest_depth_per_unit);
9925         array_blocks = info->component_size * new_data_disks;
9926         num_migr_units =
9927                 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
9928
9929         if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
9930                 num_migr_units++;
9931         migr_rec->num_migr_units = __cpu_to_le32(num_migr_units);
9932
9933         migr_rec->post_migr_vol_cap =  dev->size_low;
9934         migr_rec->post_migr_vol_cap_hi = dev->size_high;
9935
9936         /* Find the smallest dev */
9937         for (sd = info->devs ; sd ; sd = sd->next) {
9938                 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
9939                 fd = dev_open(nm, O_RDONLY);
9940                 if (fd < 0)
9941                         continue;
9942                 get_dev_size(fd, NULL, &dsize);
9943                 dev_sectors = dsize / 512;
9944                 if (dev_sectors < min_dev_sectors)
9945                         min_dev_sectors = dev_sectors;
9946                 close(fd);
9947         }
9948         migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors -
9949                                         RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
9950
9951         write_imsm_migr_rec(st);
9952
9953         return;
9954 }
9955
9956 /*******************************************************************************
9957  * Function:    save_backup_imsm
9958  * Description: Function saves critical data stripes to Migration Copy Area
9959  *              and updates the current migration unit status.
9960  *              Use restore_stripes() to form a destination stripe,
9961  *              and to write it to the Copy Area.
9962  * Parameters:
9963  *      st              : supertype information
9964  *      dev             : imsm device that backup is saved for
9965  *      info            : general array info
9966  *      buf             : input buffer
9967  *      length          : length of data to backup (blocks_per_unit)
9968  * Returns:
9969  *       0 : success
9970  *,     -1 : fail
9971  ******************************************************************************/
9972 int save_backup_imsm(struct supertype *st,
9973                      struct imsm_dev *dev,
9974                      struct mdinfo *info,
9975                      void *buf,
9976                      int length)
9977 {
9978         int rv = -1;
9979         struct intel_super *super = st->sb;
9980         unsigned long long *target_offsets;
9981         int *targets;
9982         int i;
9983         struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
9984         int new_disks = map_dest->num_members;
9985         int dest_layout = 0;
9986         int dest_chunk;
9987         unsigned long long start;
9988         int data_disks = imsm_num_data_members(dev, MAP_0);
9989
9990         targets = xmalloc(new_disks * sizeof(int));
9991
9992         for (i = 0; i < new_disks; i++)
9993                 targets[i] = -1;
9994
9995         target_offsets = xcalloc(new_disks, sizeof(unsigned long long));
9996
9997         start = info->reshape_progress * 512;
9998         for (i = 0; i < new_disks; i++) {
9999                 target_offsets[i] = (unsigned long long)
10000                   __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512;
10001                 /* move back copy area adderss, it will be moved forward
10002                  * in restore_stripes() using start input variable
10003                  */
10004                 target_offsets[i] -= start/data_disks;
10005         }
10006
10007         if (open_backup_targets(info, new_disks, targets,
10008                                 super, dev))
10009                 goto abort;
10010
10011         dest_layout = imsm_level_to_layout(map_dest->raid_level);
10012         dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
10013
10014         if (restore_stripes(targets, /* list of dest devices */
10015                             target_offsets, /* migration record offsets */
10016                             new_disks,
10017                             dest_chunk,
10018                             map_dest->raid_level,
10019                             dest_layout,
10020                             -1,    /* source backup file descriptor */
10021                             0,     /* input buf offset
10022                                     * always 0 buf is already offseted */
10023                             start,
10024                             length,
10025                             buf) != 0) {
10026                 pr_err("Error restoring stripes\n");
10027                 goto abort;
10028         }
10029
10030         rv = 0;
10031
10032 abort:
10033         if (targets) {
10034                 close_targets(targets, new_disks);
10035                 free(targets);
10036         }
10037         free(target_offsets);
10038
10039         return rv;
10040 }
10041
10042 /*******************************************************************************
10043  * Function:    save_checkpoint_imsm
10044  * Description: Function called for current unit status update
10045  *              in the migration record. It writes it to disk.
10046  * Parameters:
10047  *      super   : imsm internal array info
10048  *      info    : general array info
10049  * Returns:
10050  *      0: success
10051  *      1: failure
10052  *      2: failure, means no valid migration record
10053  *                 / no general migration in progress /
10054  ******************************************************************************/
10055 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
10056 {
10057         struct intel_super *super = st->sb;
10058         unsigned long long blocks_per_unit;
10059         unsigned long long curr_migr_unit;
10060
10061         if (load_imsm_migr_rec(super, info) != 0) {
10062                 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10063                 return 1;
10064         }
10065
10066         blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
10067         if (blocks_per_unit == 0) {
10068                 dprintf("imsm: no migration in progress.\n");
10069                 return 2;
10070         }
10071         curr_migr_unit = info->reshape_progress / blocks_per_unit;
10072         /* check if array is alligned to copy area
10073          * if it is not alligned, add one to current migration unit value
10074          * this can happend on array reshape finish only
10075          */
10076         if (info->reshape_progress % blocks_per_unit)
10077                 curr_migr_unit++;
10078
10079         super->migr_rec->curr_migr_unit =
10080                 __cpu_to_le32(curr_migr_unit);
10081         super->migr_rec->rec_status = __cpu_to_le32(state);
10082         super->migr_rec->dest_1st_member_lba =
10083                 __cpu_to_le32(curr_migr_unit *
10084                               __le32_to_cpu(super->migr_rec->dest_depth_per_unit));
10085         if (write_imsm_migr_rec(st) < 0) {
10086                 dprintf("imsm: Cannot write migration record outside backup area\n");
10087                 return 1;
10088         }
10089
10090         return 0;
10091 }
10092
10093 /*******************************************************************************
10094  * Function:    recover_backup_imsm
10095  * Description: Function recovers critical data from the Migration Copy Area
10096  *              while assembling an array.
10097  * Parameters:
10098  *      super   : imsm internal array info
10099  *      info    : general array info
10100  * Returns:
10101  *      0 : success (or there is no data to recover)
10102  *      1 : fail
10103  ******************************************************************************/
10104 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
10105 {
10106         struct intel_super *super = st->sb;
10107         struct migr_record *migr_rec = super->migr_rec;
10108         struct imsm_map *map_dest;
10109         struct intel_dev *id = NULL;
10110         unsigned long long read_offset;
10111         unsigned long long write_offset;
10112         unsigned unit_len;
10113         int *targets = NULL;
10114         int new_disks, i, err;
10115         char *buf = NULL;
10116         int retval = 1;
10117         unsigned int sector_size = super->sector_size;
10118         unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit);
10119         unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units);
10120         char buffer[20];
10121         int skipped_disks = 0;
10122
10123         err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
10124         if (err < 1)
10125                 return 1;
10126
10127         /* recover data only during assemblation */
10128         if (strncmp(buffer, "inactive", 8) != 0)
10129                 return 0;
10130         /* no data to recover */
10131         if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
10132                 return 0;
10133         if (curr_migr_unit >= num_migr_units)
10134                 return 1;
10135
10136         /* find device during reshape */
10137         for (id = super->devlist; id; id = id->next)
10138                 if (is_gen_migration(id->dev))
10139                         break;
10140         if (id == NULL)
10141                 return 1;
10142
10143         map_dest = get_imsm_map(id->dev, MAP_0);
10144         new_disks = map_dest->num_members;
10145
10146         read_offset = (unsigned long long)
10147                         __le32_to_cpu(migr_rec->ckpt_area_pba) * 512;
10148
10149         write_offset = ((unsigned long long)
10150                         __le32_to_cpu(migr_rec->dest_1st_member_lba) +
10151                         pba_of_lba0(map_dest)) * 512;
10152
10153         unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
10154         if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
10155                 goto abort;
10156         targets = xcalloc(new_disks, sizeof(int));
10157
10158         if (open_backup_targets(info, new_disks, targets, super, id->dev)) {
10159                 pr_err("Cannot open some devices belonging to array.\n");
10160                 goto abort;
10161         }
10162
10163         for (i = 0; i < new_disks; i++) {
10164                 if (targets[i] < 0) {
10165                         skipped_disks++;
10166                         continue;
10167                 }
10168                 if (lseek64(targets[i], read_offset, SEEK_SET) < 0) {
10169                         pr_err("Cannot seek to block: %s\n",
10170                                strerror(errno));
10171                         skipped_disks++;
10172                         continue;
10173                 }
10174                 if ((unsigned)read(targets[i], buf, unit_len) != unit_len) {
10175                         pr_err("Cannot read copy area block: %s\n",
10176                                strerror(errno));
10177                         skipped_disks++;
10178                         continue;
10179                 }
10180                 if (lseek64(targets[i], write_offset, SEEK_SET) < 0) {
10181                         pr_err("Cannot seek to block: %s\n",
10182                                strerror(errno));
10183                         skipped_disks++;
10184                         continue;
10185                 }
10186                 if ((unsigned)write(targets[i], buf, unit_len) != unit_len) {
10187                         pr_err("Cannot restore block: %s\n",
10188                                strerror(errno));
10189                         skipped_disks++;
10190                         continue;
10191                 }
10192         }
10193
10194         if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
10195                                                          new_disks,
10196                                                          super,
10197                                                          id->dev)) {
10198                 pr_err("Cannot restore data from backup. Too many failed disks\n");
10199                 goto abort;
10200         }
10201
10202         if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
10203                 /* ignore error == 2, this can mean end of reshape here
10204                  */
10205                 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10206         } else
10207                 retval = 0;
10208
10209 abort:
10210         if (targets) {
10211                 for (i = 0; i < new_disks; i++)
10212                         if (targets[i])
10213                                 close(targets[i]);
10214                 free(targets);
10215         }
10216         free(buf);
10217         return retval;
10218 }
10219
10220 static char disk_by_path[] = "/dev/disk/by-path/";
10221
10222 static const char *imsm_get_disk_controller_domain(const char *path)
10223 {
10224         char disk_path[PATH_MAX];
10225         char *drv=NULL;
10226         struct stat st;
10227
10228         strcpy(disk_path, disk_by_path);
10229         strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
10230         if (stat(disk_path, &st) == 0) {
10231                 struct sys_dev* hba;
10232                 char *path;
10233
10234                 path = devt_to_devpath(st.st_rdev);
10235                 if (path == NULL)
10236                         return "unknown";
10237                 hba = find_disk_attached_hba(-1, path);
10238                 if (hba && hba->type == SYS_DEV_SAS)
10239                         drv = "isci";
10240                 else if (hba && hba->type == SYS_DEV_SATA)
10241                         drv = "ahci";
10242                 else
10243                         drv = "unknown";
10244                 dprintf("path: %s hba: %s attached: %s\n",
10245                         path, (hba) ? hba->path : "NULL", drv);
10246                 free(path);
10247         }
10248         return drv;
10249 }
10250
10251 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
10252 {
10253         static char devnm[32];
10254         char subdev_name[20];
10255         struct mdstat_ent *mdstat;
10256
10257         sprintf(subdev_name, "%d", subdev);
10258         mdstat = mdstat_by_subdev(subdev_name, container);
10259         if (!mdstat)
10260                 return NULL;
10261
10262         strcpy(devnm, mdstat->devnm);
10263         free_mdstat(mdstat);
10264         return devnm;
10265 }
10266
10267 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
10268                                                 struct geo_params *geo,
10269                                                 int *old_raid_disks,
10270                                                 int direction)
10271 {
10272         /* currently we only support increasing the number of devices
10273          * for a container.  This increases the number of device for each
10274          * member array.  They must all be RAID0 or RAID5.
10275          */
10276         int ret_val = 0;
10277         struct mdinfo *info, *member;
10278         int devices_that_can_grow = 0;
10279
10280         dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
10281
10282         if (geo->size > 0 ||
10283             geo->level != UnSet ||
10284             geo->layout != UnSet ||
10285             geo->chunksize != 0 ||
10286             geo->raid_disks == UnSet) {
10287                 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10288                 return ret_val;
10289         }
10290
10291         if (direction == ROLLBACK_METADATA_CHANGES) {
10292                 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10293                 return ret_val;
10294         }
10295
10296         info = container_content_imsm(st, NULL);
10297         for (member = info; member; member = member->next) {
10298                 char *result;
10299
10300                 dprintf("imsm: checking device_num: %i\n",
10301                         member->container_member);
10302
10303                 if (geo->raid_disks <= member->array.raid_disks) {
10304                         /* we work on container for Online Capacity Expansion
10305                          * only so raid_disks has to grow
10306                          */
10307                         dprintf("imsm: for container operation raid disks increase is required\n");
10308                         break;
10309                 }
10310
10311                 if (info->array.level != 0 && info->array.level != 5) {
10312                         /* we cannot use this container with other raid level
10313                          */
10314                         dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10315                                 info->array.level);
10316                         break;
10317                 } else {
10318                         /* check for platform support
10319                          * for this raid level configuration
10320                          */
10321                         struct intel_super *super = st->sb;
10322                         if (!is_raid_level_supported(super->orom,
10323                                                      member->array.level,
10324                                                      geo->raid_disks)) {
10325                                 dprintf("platform does not support raid%d with %d disk%s\n",
10326                                          info->array.level,
10327                                          geo->raid_disks,
10328                                          geo->raid_disks > 1 ? "s" : "");
10329                                 break;
10330                         }
10331                         /* check if component size is aligned to chunk size
10332                          */
10333                         if (info->component_size %
10334                             (info->array.chunk_size/512)) {
10335                                 dprintf("Component size is not aligned to chunk size\n");
10336                                 break;
10337                         }
10338                 }
10339
10340                 if (*old_raid_disks &&
10341                     info->array.raid_disks != *old_raid_disks)
10342                         break;
10343                 *old_raid_disks = info->array.raid_disks;
10344
10345                 /* All raid5 and raid0 volumes in container
10346                  * have to be ready for Online Capacity Expansion
10347                  * so they need to be assembled.  We have already
10348                  * checked that no recovery etc is happening.
10349                  */
10350                 result = imsm_find_array_devnm_by_subdev(member->container_member,
10351                                                          st->container_devnm);
10352                 if (result == NULL) {
10353                         dprintf("imsm: cannot find array\n");
10354                         break;
10355                 }
10356                 devices_that_can_grow++;
10357         }
10358         sysfs_free(info);
10359         if (!member && devices_that_can_grow)
10360                 ret_val = 1;
10361
10362         if (ret_val)
10363                 dprintf("Container operation allowed\n");
10364         else
10365                 dprintf("Error: %i\n", ret_val);
10366
10367         return ret_val;
10368 }
10369
10370 /* Function: get_spares_for_grow
10371  * Description: Allocates memory and creates list of spare devices
10372  *              avaliable in container. Checks if spare drive size is acceptable.
10373  * Parameters: Pointer to the supertype structure
10374  * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10375  *              NULL if fail
10376  */
10377 static struct mdinfo *get_spares_for_grow(struct supertype *st)
10378 {
10379         unsigned long long min_size = min_acceptable_spare_size_imsm(st);
10380         return container_choose_spares(st, min_size, NULL, NULL, NULL, 0);
10381 }
10382
10383 /******************************************************************************
10384  * function: imsm_create_metadata_update_for_reshape
10385  * Function creates update for whole IMSM container.
10386  *
10387  ******************************************************************************/
10388 static int imsm_create_metadata_update_for_reshape(
10389         struct supertype *st,
10390         struct geo_params *geo,
10391         int old_raid_disks,
10392         struct imsm_update_reshape **updatep)
10393 {
10394         struct intel_super *super = st->sb;
10395         struct imsm_super *mpb = super->anchor;
10396         int update_memory_size;
10397         struct imsm_update_reshape *u;
10398         struct mdinfo *spares;
10399         int i;
10400         int delta_disks;
10401         struct mdinfo *dev;
10402
10403         dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
10404
10405         delta_disks = geo->raid_disks - old_raid_disks;
10406
10407         /* size of all update data without anchor */
10408         update_memory_size = sizeof(struct imsm_update_reshape);
10409
10410         /* now add space for spare disks that we need to add. */
10411         update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
10412
10413         u = xcalloc(1, update_memory_size);
10414         u->type = update_reshape_container_disks;
10415         u->old_raid_disks = old_raid_disks;
10416         u->new_raid_disks = geo->raid_disks;
10417
10418         /* now get spare disks list
10419          */
10420         spares = get_spares_for_grow(st);
10421
10422         if (spares == NULL
10423             || delta_disks > spares->array.spare_disks) {
10424                 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
10425                 i = -1;
10426                 goto abort;
10427         }
10428
10429         /* we have got spares
10430          * update disk list in imsm_disk list table in anchor
10431          */
10432         dprintf("imsm: %i spares are available.\n\n",
10433                 spares->array.spare_disks);
10434
10435         dev = spares->devs;
10436         for (i = 0; i < delta_disks; i++) {
10437                 struct dl *dl;
10438
10439                 if (dev == NULL)
10440                         break;
10441                 u->new_disks[i] = makedev(dev->disk.major,
10442                                           dev->disk.minor);
10443                 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
10444                 dl->index = mpb->num_disks;
10445                 mpb->num_disks++;
10446                 dev = dev->next;
10447         }
10448
10449 abort:
10450         /* free spares
10451          */
10452         sysfs_free(spares);
10453
10454         dprintf("imsm: reshape update preparation :");
10455         if (i == delta_disks) {
10456                 dprintf_cont(" OK\n");
10457                 *updatep = u;
10458                 return update_memory_size;
10459         }
10460         free(u);
10461         dprintf_cont(" Error\n");
10462
10463         return 0;
10464 }
10465
10466 /******************************************************************************
10467  * function: imsm_create_metadata_update_for_size_change()
10468  *           Creates update for IMSM array for array size change.
10469  *
10470  ******************************************************************************/
10471 static int imsm_create_metadata_update_for_size_change(
10472                                 struct supertype *st,
10473                                 struct geo_params *geo,
10474                                 struct imsm_update_size_change **updatep)
10475 {
10476         struct intel_super *super = st->sb;
10477         int update_memory_size;
10478         struct imsm_update_size_change *u;
10479
10480         dprintf("(enter) New size = %llu\n", geo->size);
10481
10482         /* size of all update data without anchor */
10483         update_memory_size = sizeof(struct imsm_update_size_change);
10484
10485         u = xcalloc(1, update_memory_size);
10486         u->type = update_size_change;
10487         u->subdev = super->current_vol;
10488         u->new_size = geo->size;
10489
10490         dprintf("imsm: reshape update preparation : OK\n");
10491         *updatep = u;
10492
10493         return update_memory_size;
10494 }
10495
10496 /******************************************************************************
10497  * function: imsm_create_metadata_update_for_migration()
10498  *           Creates update for IMSM array.
10499  *
10500  ******************************************************************************/
10501 static int imsm_create_metadata_update_for_migration(
10502                                         struct supertype *st,
10503                                         struct geo_params *geo,
10504                                         struct imsm_update_reshape_migration **updatep)
10505 {
10506         struct intel_super *super = st->sb;
10507         int update_memory_size;
10508         struct imsm_update_reshape_migration *u;
10509         struct imsm_dev *dev;
10510         int previous_level = -1;
10511
10512         dprintf("(enter) New Level = %i\n", geo->level);
10513
10514         /* size of all update data without anchor */
10515         update_memory_size = sizeof(struct imsm_update_reshape_migration);
10516
10517         u = xcalloc(1, update_memory_size);
10518         u->type = update_reshape_migration;
10519         u->subdev = super->current_vol;
10520         u->new_level = geo->level;
10521         u->new_layout = geo->layout;
10522         u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
10523         u->new_disks[0] = -1;
10524         u->new_chunksize = -1;
10525
10526         dev = get_imsm_dev(super, u->subdev);
10527         if (dev) {
10528                 struct imsm_map *map;
10529
10530                 map = get_imsm_map(dev, MAP_0);
10531                 if (map) {
10532                         int current_chunk_size =
10533                                 __le16_to_cpu(map->blocks_per_strip) / 2;
10534
10535                         if (geo->chunksize != current_chunk_size) {
10536                                 u->new_chunksize = geo->chunksize / 1024;
10537                                 dprintf("imsm: chunk size change from %i to %i\n",
10538                                         current_chunk_size, u->new_chunksize);
10539                         }
10540                         previous_level = map->raid_level;
10541                 }
10542         }
10543         if (geo->level == 5 && previous_level == 0) {
10544                 struct mdinfo *spares = NULL;
10545
10546                 u->new_raid_disks++;
10547                 spares = get_spares_for_grow(st);
10548                 if (spares == NULL || spares->array.spare_disks < 1) {
10549                         free(u);
10550                         sysfs_free(spares);
10551                         update_memory_size = 0;
10552                         dprintf("error: cannot get spare device for requested migration");
10553                         return 0;
10554                 }
10555                 sysfs_free(spares);
10556         }
10557         dprintf("imsm: reshape update preparation : OK\n");
10558         *updatep = u;
10559
10560         return update_memory_size;
10561 }
10562
10563 static void imsm_update_metadata_locally(struct supertype *st,
10564                                          void *buf, int len)
10565 {
10566         struct metadata_update mu;
10567
10568         mu.buf = buf;
10569         mu.len = len;
10570         mu.space = NULL;
10571         mu.space_list = NULL;
10572         mu.next = NULL;
10573         if (imsm_prepare_update(st, &mu))
10574                 imsm_process_update(st, &mu);
10575
10576         while (mu.space_list) {
10577                 void **space = mu.space_list;
10578                 mu.space_list = *space;
10579                 free(space);
10580         }
10581 }
10582
10583 /***************************************************************************
10584 * Function:     imsm_analyze_change
10585 * Description:  Function analyze change for single volume
10586 *               and validate if transition is supported
10587 * Parameters:   Geometry parameters, supertype structure,
10588 *               metadata change direction (apply/rollback)
10589 * Returns:      Operation type code on success, -1 if fail
10590 ****************************************************************************/
10591 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
10592                                            struct geo_params *geo,
10593                                            int direction)
10594 {
10595         struct mdinfo info;
10596         int change = -1;
10597         int check_devs = 0;
10598         int chunk;
10599         /* number of added/removed disks in operation result */
10600         int devNumChange = 0;
10601         /* imsm compatible layout value for array geometry verification */
10602         int imsm_layout = -1;
10603         int data_disks;
10604         struct imsm_dev *dev;
10605         struct intel_super *super;
10606         unsigned long long current_size;
10607         unsigned long long free_size;
10608         unsigned long long max_size;
10609         int rv;
10610
10611         getinfo_super_imsm_volume(st, &info, NULL);
10612         if (geo->level != info.array.level && geo->level >= 0 &&
10613             geo->level != UnSet) {
10614                 switch (info.array.level) {
10615                 case 0:
10616                         if (geo->level == 5) {
10617                                 change = CH_MIGRATION;
10618                                 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
10619                                         pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10620                                         change = -1;
10621                                         goto analyse_change_exit;
10622                                 }
10623                                 imsm_layout =  geo->layout;
10624                                 check_devs = 1;
10625                                 devNumChange = 1; /* parity disk added */
10626                         } else if (geo->level == 10) {
10627                                 change = CH_TAKEOVER;
10628                                 check_devs = 1;
10629                                 devNumChange = 2; /* two mirrors added */
10630                                 imsm_layout = 0x102; /* imsm supported layout */
10631                         }
10632                         break;
10633                 case 1:
10634                 case 10:
10635                         if (geo->level == 0) {
10636                                 change = CH_TAKEOVER;
10637                                 check_devs = 1;
10638                                 devNumChange = -(geo->raid_disks/2);
10639                                 imsm_layout = 0; /* imsm raid0 layout */
10640                         }
10641                         break;
10642                 }
10643                 if (change == -1) {
10644                         pr_err("Error. Level Migration from %d to %d not supported!\n",
10645                                info.array.level, geo->level);
10646                         goto analyse_change_exit;
10647                 }
10648         } else
10649                 geo->level = info.array.level;
10650
10651         if (geo->layout != info.array.layout &&
10652             (geo->layout != UnSet && geo->layout != -1)) {
10653                 change = CH_MIGRATION;
10654                 if (info.array.layout == 0 && info.array.level == 5 &&
10655                     geo->layout == 5) {
10656                         /* reshape 5 -> 4 */
10657                 } else if (info.array.layout == 5 && info.array.level == 5 &&
10658                            geo->layout == 0) {
10659                         /* reshape 4 -> 5 */
10660                         geo->layout = 0;
10661                         geo->level = 5;
10662                 } else {
10663                         pr_err("Error. Layout Migration from %d to %d not supported!\n",
10664                                info.array.layout, geo->layout);
10665                         change = -1;
10666                         goto analyse_change_exit;
10667                 }
10668         } else {
10669                 geo->layout = info.array.layout;
10670                 if (imsm_layout == -1)
10671                         imsm_layout = info.array.layout;
10672         }
10673
10674         if (geo->chunksize > 0 && geo->chunksize != UnSet &&
10675             geo->chunksize != info.array.chunk_size) {
10676                 if (info.array.level == 10) {
10677                         pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10678                         change = -1;
10679                         goto analyse_change_exit;
10680                 }
10681                 change = CH_MIGRATION;
10682         } else {
10683                 geo->chunksize = info.array.chunk_size;
10684         }
10685
10686         chunk = geo->chunksize / 1024;
10687
10688         super = st->sb;
10689         dev = get_imsm_dev(super, super->current_vol);
10690         data_disks = imsm_num_data_members(dev , MAP_0);
10691         /* compute current size per disk member
10692          */
10693         current_size = info.custom_array_size / data_disks;
10694
10695         if (geo->size > 0 && geo->size != MAX_SIZE) {
10696                 /* align component size
10697                  */
10698                 geo->size = imsm_component_size_aligment_check(
10699                                     get_imsm_raid_level(dev->vol.map),
10700                                     chunk * 1024, super->sector_size,
10701                                     geo->size * 2);
10702                 if (geo->size == 0) {
10703                         pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10704                                    current_size);
10705                         goto analyse_change_exit;
10706                 }
10707         }
10708
10709         if (current_size != geo->size && geo->size > 0) {
10710                 if (change != -1) {
10711                         pr_err("Error. Size change should be the only one at a time.\n");
10712                         change = -1;
10713                         goto analyse_change_exit;
10714                 }
10715                 if ((super->current_vol + 1) != super->anchor->num_raid_devs) {
10716                         pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10717                                super->current_vol, st->devnm);
10718                         goto analyse_change_exit;
10719                 }
10720                 /* check the maximum available size
10721                  */
10722                 rv =  imsm_get_free_size(st, dev->vol.map->num_members,
10723                                          0, chunk, &free_size);
10724                 if (rv == 0)
10725                         /* Cannot find maximum available space
10726                          */
10727                         max_size = 0;
10728                 else {
10729                         max_size = free_size + current_size;
10730                         /* align component size
10731                          */
10732                         max_size = imsm_component_size_aligment_check(
10733                                         get_imsm_raid_level(dev->vol.map),
10734                                         chunk * 1024, super->sector_size,
10735                                         max_size);
10736                 }
10737                 if (geo->size == MAX_SIZE) {
10738                         /* requested size change to the maximum available size
10739                          */
10740                         if (max_size == 0) {
10741                                 pr_err("Error. Cannot find maximum available space.\n");
10742                                 change = -1;
10743                                 goto analyse_change_exit;
10744                         } else
10745                                 geo->size = max_size;
10746                 }
10747
10748                 if (direction == ROLLBACK_METADATA_CHANGES) {
10749                         /* accept size for rollback only
10750                         */
10751                 } else {
10752                         /* round size due to metadata compatibility
10753                         */
10754                         geo->size = (geo->size >> SECT_PER_MB_SHIFT)
10755                                     << SECT_PER_MB_SHIFT;
10756                         dprintf("Prepare update for size change to %llu\n",
10757                                 geo->size );
10758                         if (current_size >= geo->size) {
10759                                 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10760                                        current_size, geo->size);
10761                                 goto analyse_change_exit;
10762                         }
10763                         if (max_size && geo->size > max_size) {
10764                                 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10765                                        max_size, geo->size);
10766                                 goto analyse_change_exit;
10767                         }
10768                 }
10769                 geo->size *= data_disks;
10770                 geo->raid_disks = dev->vol.map->num_members;
10771                 change = CH_ARRAY_SIZE;
10772         }
10773         if (!validate_geometry_imsm(st,
10774                                     geo->level,
10775                                     imsm_layout,
10776                                     geo->raid_disks + devNumChange,
10777                                     &chunk,
10778                                     geo->size, INVALID_SECTORS,
10779                                     0, 0, 1))
10780                 change = -1;
10781
10782         if (check_devs) {
10783                 struct intel_super *super = st->sb;
10784                 struct imsm_super *mpb = super->anchor;
10785
10786                 if (mpb->num_raid_devs > 1) {
10787                         pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10788                                geo->dev_name);
10789                         change = -1;
10790                 }
10791         }
10792
10793 analyse_change_exit:
10794         if (direction == ROLLBACK_METADATA_CHANGES &&
10795             (change == CH_MIGRATION || change == CH_TAKEOVER)) {
10796                 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10797                 change = -1;
10798         }
10799         return change;
10800 }
10801
10802 int imsm_takeover(struct supertype *st, struct geo_params *geo)
10803 {
10804         struct intel_super *super = st->sb;
10805         struct imsm_update_takeover *u;
10806
10807         u = xmalloc(sizeof(struct imsm_update_takeover));
10808
10809         u->type = update_takeover;
10810         u->subarray = super->current_vol;
10811
10812         /* 10->0 transition */
10813         if (geo->level == 0)
10814                 u->direction = R10_TO_R0;
10815
10816         /* 0->10 transition */
10817         if (geo->level == 10)
10818                 u->direction = R0_TO_R10;
10819
10820         /* update metadata locally */
10821         imsm_update_metadata_locally(st, u,
10822                                         sizeof(struct imsm_update_takeover));
10823         /* and possibly remotely */
10824         if (st->update_tail)
10825                 append_metadata_update(st, u,
10826                                         sizeof(struct imsm_update_takeover));
10827         else
10828                 free(u);
10829
10830         return 0;
10831 }
10832
10833 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
10834                               int level,
10835                               int layout, int chunksize, int raid_disks,
10836                               int delta_disks, char *backup, char *dev,
10837                               int direction, int verbose)
10838 {
10839         int ret_val = 1;
10840         struct geo_params geo;
10841
10842         dprintf("(enter)\n");
10843
10844         memset(&geo, 0, sizeof(struct geo_params));
10845
10846         geo.dev_name = dev;
10847         strcpy(geo.devnm, st->devnm);
10848         geo.size = size;
10849         geo.level = level;
10850         geo.layout = layout;
10851         geo.chunksize = chunksize;
10852         geo.raid_disks = raid_disks;
10853         if (delta_disks != UnSet)
10854                 geo.raid_disks += delta_disks;
10855
10856         dprintf("for level      : %i\n", geo.level);
10857         dprintf("for raid_disks : %i\n", geo.raid_disks);
10858
10859         if (experimental() == 0)
10860                 return ret_val;
10861
10862         if (strcmp(st->container_devnm, st->devnm) == 0) {
10863                 /* On container level we can only increase number of devices. */
10864                 dprintf("imsm: info: Container operation\n");
10865                 int old_raid_disks = 0;
10866
10867                 if (imsm_reshape_is_allowed_on_container(
10868                             st, &geo, &old_raid_disks, direction)) {
10869                         struct imsm_update_reshape *u = NULL;
10870                         int len;
10871
10872                         len = imsm_create_metadata_update_for_reshape(
10873                                 st, &geo, old_raid_disks, &u);
10874
10875                         if (len <= 0) {
10876                                 dprintf("imsm: Cannot prepare update\n");
10877                                 goto exit_imsm_reshape_super;
10878                         }
10879
10880                         ret_val = 0;
10881                         /* update metadata locally */
10882                         imsm_update_metadata_locally(st, u, len);
10883                         /* and possibly remotely */
10884                         if (st->update_tail)
10885                                 append_metadata_update(st, u, len);
10886                         else
10887                                 free(u);
10888
10889                 } else {
10890                         pr_err("(imsm) Operation is not allowed on this container\n");
10891                 }
10892         } else {
10893                 /* On volume level we support following operations
10894                  * - takeover: raid10 -> raid0; raid0 -> raid10
10895                  * - chunk size migration
10896                  * - migration: raid5 -> raid0; raid0 -> raid5
10897                  */
10898                 struct intel_super *super = st->sb;
10899                 struct intel_dev *dev = super->devlist;
10900                 int change;
10901                 dprintf("imsm: info: Volume operation\n");
10902                 /* find requested device */
10903                 while (dev) {
10904                         char *devnm =
10905                                 imsm_find_array_devnm_by_subdev(
10906                                         dev->index, st->container_devnm);
10907                         if (devnm && strcmp(devnm, geo.devnm) == 0)
10908                                 break;
10909                         dev = dev->next;
10910                 }
10911                 if (dev == NULL) {
10912                         pr_err("Cannot find %s (%s) subarray\n",
10913                                 geo.dev_name, geo.devnm);
10914                         goto exit_imsm_reshape_super;
10915                 }
10916                 super->current_vol = dev->index;
10917                 change = imsm_analyze_change(st, &geo, direction);
10918                 switch (change) {
10919                 case CH_TAKEOVER:
10920                         ret_val = imsm_takeover(st, &geo);
10921                         break;
10922                 case CH_MIGRATION: {
10923                         struct imsm_update_reshape_migration *u = NULL;
10924                         int len =
10925                                 imsm_create_metadata_update_for_migration(
10926                                         st, &geo, &u);
10927                         if (len < 1) {
10928                                 dprintf("imsm: Cannot prepare update\n");
10929                                 break;
10930                         }
10931                         ret_val = 0;
10932                         /* update metadata locally */
10933                         imsm_update_metadata_locally(st, u, len);
10934                         /* and possibly remotely */
10935                         if (st->update_tail)
10936                                 append_metadata_update(st, u, len);
10937                         else
10938                                 free(u);
10939                 }
10940                 break;
10941                 case CH_ARRAY_SIZE: {
10942                         struct imsm_update_size_change *u = NULL;
10943                         int len =
10944                                 imsm_create_metadata_update_for_size_change(
10945                                         st, &geo, &u);
10946                         if (len < 1) {
10947                                 dprintf("imsm: Cannot prepare update\n");
10948                                 break;
10949                         }
10950                         ret_val = 0;
10951                         /* update metadata locally */
10952                         imsm_update_metadata_locally(st, u, len);
10953                         /* and possibly remotely */
10954                         if (st->update_tail)
10955                                 append_metadata_update(st, u, len);
10956                         else
10957                                 free(u);
10958                 }
10959                 break;
10960                 default:
10961                         ret_val = 1;
10962                 }
10963         }
10964
10965 exit_imsm_reshape_super:
10966         dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
10967         return ret_val;
10968 }
10969
10970 #define COMPLETED_OK            0
10971 #define COMPLETED_NONE          1
10972 #define COMPLETED_DELAYED       2
10973
10974 static int read_completed(int fd, unsigned long long *val)
10975 {
10976         int ret;
10977         char buf[50];
10978
10979         ret = sysfs_fd_get_str(fd, buf, 50);
10980         if (ret < 0)
10981                 return ret;
10982
10983         ret = COMPLETED_OK;
10984         if (strncmp(buf, "none", 4) == 0) {
10985                 ret = COMPLETED_NONE;
10986         } else if (strncmp(buf, "delayed", 7) == 0) {
10987                 ret = COMPLETED_DELAYED;
10988         } else {
10989                 char *ep;
10990                 *val = strtoull(buf, &ep, 0);
10991                 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
10992                         ret = -1;
10993         }
10994         return ret;
10995 }
10996
10997 /*******************************************************************************
10998  * Function:    wait_for_reshape_imsm
10999  * Description: Function writes new sync_max value and waits until
11000  *              reshape process reach new position
11001  * Parameters:
11002  *      sra             : general array info
11003  *      ndata           : number of disks in new array's layout
11004  * Returns:
11005  *       0 : success,
11006  *       1 : there is no reshape in progress,
11007  *      -1 : fail
11008  ******************************************************************************/
11009 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
11010 {
11011         int fd = sysfs_get_fd(sra, NULL, "sync_completed");
11012         int retry = 3;
11013         unsigned long long completed;
11014         /* to_complete : new sync_max position */
11015         unsigned long long to_complete = sra->reshape_progress;
11016         unsigned long long position_to_set = to_complete / ndata;
11017
11018         if (fd < 0) {
11019                 dprintf("cannot open reshape_position\n");
11020                 return 1;
11021         }
11022
11023         do {
11024                 if (sysfs_fd_get_ll(fd, &completed) < 0) {
11025                         if (!retry) {
11026                                 dprintf("cannot read reshape_position (no reshape in progres)\n");
11027                                 close(fd);
11028                                 return 1;
11029                         }
11030                         usleep(30000);
11031                 } else
11032                         break;
11033         } while (retry--);
11034
11035         if (completed > position_to_set) {
11036                 dprintf("wrong next position to set %llu (%llu)\n",
11037                         to_complete, position_to_set);
11038                 close(fd);
11039                 return -1;
11040         }
11041         dprintf("Position set: %llu\n", position_to_set);
11042         if (sysfs_set_num(sra, NULL, "sync_max",
11043                           position_to_set) != 0) {
11044                 dprintf("cannot set reshape position to %llu\n",
11045                         position_to_set);
11046                 close(fd);
11047                 return -1;
11048         }
11049
11050         do {
11051                 int rc;
11052                 char action[20];
11053                 int timeout = 3000;
11054
11055                 sysfs_wait(fd, &timeout);
11056                 if (sysfs_get_str(sra, NULL, "sync_action",
11057                                   action, 20) > 0 &&
11058                                 strncmp(action, "reshape", 7) != 0) {
11059                         if (strncmp(action, "idle", 4) == 0)
11060                                 break;
11061                         close(fd);
11062                         return -1;
11063                 }
11064
11065                 rc = read_completed(fd, &completed);
11066                 if (rc < 0) {
11067                         dprintf("cannot read reshape_position (in loop)\n");
11068                         close(fd);
11069                         return 1;
11070                 } else if (rc == COMPLETED_NONE)
11071                         break;
11072         } while (completed < position_to_set);
11073
11074         close(fd);
11075         return 0;
11076 }
11077
11078 /*******************************************************************************
11079  * Function:    check_degradation_change
11080  * Description: Check that array hasn't become failed.
11081  * Parameters:
11082  *      info    : for sysfs access
11083  *      sources : source disks descriptors
11084  *      degraded: previous degradation level
11085  * Returns:
11086  *      degradation level
11087  ******************************************************************************/
11088 int check_degradation_change(struct mdinfo *info,
11089                              int *sources,
11090                              int degraded)
11091 {
11092         unsigned long long new_degraded;
11093         int rv;
11094
11095         rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
11096         if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
11097                 /* check each device to ensure it is still working */
11098                 struct mdinfo *sd;
11099                 new_degraded = 0;
11100                 for (sd = info->devs ; sd ; sd = sd->next) {
11101                         if (sd->disk.state & (1<<MD_DISK_FAULTY))
11102                                 continue;
11103                         if (sd->disk.state & (1<<MD_DISK_SYNC)) {
11104                                 char sbuf[100];
11105
11106                                 if (sysfs_get_str(info,
11107                                         sd, "state", sbuf, sizeof(sbuf)) < 0 ||
11108                                         strstr(sbuf, "faulty") ||
11109                                         strstr(sbuf, "in_sync") == NULL) {
11110                                         /* this device is dead */
11111                                         sd->disk.state = (1<<MD_DISK_FAULTY);
11112                                         if (sd->disk.raid_disk >= 0 &&
11113                                             sources[sd->disk.raid_disk] >= 0) {
11114                                                 close(sources[
11115                                                         sd->disk.raid_disk]);
11116                                                 sources[sd->disk.raid_disk] =
11117                                                         -1;
11118                                         }
11119                                         new_degraded++;
11120                                 }
11121                         }
11122                 }
11123         }
11124
11125         return new_degraded;
11126 }
11127
11128 /*******************************************************************************
11129  * Function:    imsm_manage_reshape
11130  * Description: Function finds array under reshape and it manages reshape
11131  *              process. It creates stripes backups (if required) and sets
11132  *              checkpoints.
11133  * Parameters:
11134  *      afd             : Backup handle (nattive) - not used
11135  *      sra             : general array info
11136  *      reshape         : reshape parameters - not used
11137  *      st              : supertype structure
11138  *      blocks          : size of critical section [blocks]
11139  *      fds             : table of source device descriptor
11140  *      offsets         : start of array (offest per devices)
11141  *      dests           : not used
11142  *      destfd          : table of destination device descriptor
11143  *      destoffsets     : table of destination offsets (per device)
11144  * Returns:
11145  *      1 : success, reshape is done
11146  *      0 : fail
11147  ******************************************************************************/
11148 static int imsm_manage_reshape(
11149         int afd, struct mdinfo *sra, struct reshape *reshape,
11150         struct supertype *st, unsigned long backup_blocks,
11151         int *fds, unsigned long long *offsets,
11152         int dests, int *destfd, unsigned long long *destoffsets)
11153 {
11154         int ret_val = 0;
11155         struct intel_super *super = st->sb;
11156         struct intel_dev *dv;
11157         unsigned int sector_size = super->sector_size;
11158         struct imsm_dev *dev = NULL;
11159         struct imsm_map *map_src;
11160         int migr_vol_qan = 0;
11161         int ndata, odata; /* [bytes] */
11162         int chunk; /* [bytes] */
11163         struct migr_record *migr_rec;
11164         char *buf = NULL;
11165         unsigned int buf_size; /* [bytes] */
11166         unsigned long long max_position; /* array size [bytes] */
11167         unsigned long long next_step; /* [blocks]/[bytes] */
11168         unsigned long long old_data_stripe_length;
11169         unsigned long long start_src; /* [bytes] */
11170         unsigned long long start; /* [bytes] */
11171         unsigned long long start_buf_shift; /* [bytes] */
11172         int degraded = 0;
11173         int source_layout = 0;
11174
11175         if (!sra)
11176                 return ret_val;
11177
11178         if (!fds || !offsets)
11179                 goto abort;
11180
11181         /* Find volume during the reshape */
11182         for (dv = super->devlist; dv; dv = dv->next) {
11183                 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR
11184                     && dv->dev->vol.migr_state == 1) {
11185                         dev = dv->dev;
11186                         migr_vol_qan++;
11187                 }
11188         }
11189         /* Only one volume can migrate at the same time */
11190         if (migr_vol_qan != 1) {
11191                 pr_err("%s", migr_vol_qan ?
11192                         "Number of migrating volumes greater than 1\n" :
11193                         "There is no volume during migrationg\n");
11194                 goto abort;
11195         }
11196
11197         map_src = get_imsm_map(dev, MAP_1);
11198         if (map_src == NULL)
11199                 goto abort;
11200
11201         ndata = imsm_num_data_members(dev, MAP_0);
11202         odata = imsm_num_data_members(dev, MAP_1);
11203
11204         chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
11205         old_data_stripe_length = odata * chunk;
11206
11207         migr_rec = super->migr_rec;
11208
11209         /* initialize migration record for start condition */
11210         if (sra->reshape_progress == 0)
11211                 init_migr_record_imsm(st, dev, sra);
11212         else {
11213                 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
11214                         dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11215                         goto abort;
11216                 }
11217                 /* Save checkpoint to update migration record for current
11218                  * reshape position (in md). It can be farther than current
11219                  * reshape position in metadata.
11220                  */
11221                 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
11222                         /* ignore error == 2, this can mean end of reshape here
11223                          */
11224                         dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11225                         goto abort;
11226                 }
11227         }
11228
11229         /* size for data */
11230         buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
11231         /* extend  buffer size for parity disk */
11232         buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11233         /* add space for stripe aligment */
11234         buf_size += old_data_stripe_length;
11235         if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
11236                 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11237                 goto abort;
11238         }
11239
11240         max_position = sra->component_size * ndata;
11241         source_layout = imsm_level_to_layout(map_src->raid_level);
11242
11243         while (__le32_to_cpu(migr_rec->curr_migr_unit) <
11244                __le32_to_cpu(migr_rec->num_migr_units)) {
11245                 /* current reshape position [blocks] */
11246                 unsigned long long current_position =
11247                         __le32_to_cpu(migr_rec->blocks_per_unit)
11248                         * __le32_to_cpu(migr_rec->curr_migr_unit);
11249                 unsigned long long border;
11250
11251                 /* Check that array hasn't become failed.
11252                  */
11253                 degraded = check_degradation_change(sra, fds, degraded);
11254                 if (degraded > 1) {
11255                         dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
11256                         goto abort;
11257                 }
11258
11259                 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
11260
11261                 if ((current_position + next_step) > max_position)
11262                         next_step = max_position - current_position;
11263
11264                 start = current_position * 512;
11265
11266                 /* align reading start to old geometry */
11267                 start_buf_shift = start % old_data_stripe_length;
11268                 start_src = start - start_buf_shift;
11269
11270                 border = (start_src / odata) - (start / ndata);
11271                 border /= 512;
11272                 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
11273                         /* save critical stripes to buf
11274                          * start     - start address of current unit
11275                          *             to backup [bytes]
11276                          * start_src - start address of current unit
11277                          *             to backup alligned to source array
11278                          *             [bytes]
11279                          */
11280                         unsigned long long next_step_filler;
11281                         unsigned long long copy_length = next_step * 512;
11282
11283                         /* allign copy area length to stripe in old geometry */
11284                         next_step_filler = ((copy_length + start_buf_shift)
11285                                             % old_data_stripe_length);
11286                         if (next_step_filler)
11287                                 next_step_filler = (old_data_stripe_length
11288                                                     - next_step_filler);
11289                         dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11290                                 start, start_src, copy_length,
11291                                 start_buf_shift, next_step_filler);
11292
11293                         if (save_stripes(fds, offsets, map_src->num_members,
11294                                          chunk, map_src->raid_level,
11295                                          source_layout, 0, NULL, start_src,
11296                                          copy_length +
11297                                          next_step_filler + start_buf_shift,
11298                                          buf)) {
11299                                 dprintf("imsm: Cannot save stripes to buffer\n");
11300                                 goto abort;
11301                         }
11302                         /* Convert data to destination format and store it
11303                          * in backup general migration area
11304                          */
11305                         if (save_backup_imsm(st, dev, sra,
11306                                 buf + start_buf_shift, copy_length)) {
11307                                 dprintf("imsm: Cannot save stripes to target devices\n");
11308                                 goto abort;
11309                         }
11310                         if (save_checkpoint_imsm(st, sra,
11311                                                  UNIT_SRC_IN_CP_AREA)) {
11312                                 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11313                                 goto abort;
11314                         }
11315                 } else {
11316                         /* set next step to use whole border area */
11317                         border /= next_step;
11318                         if (border > 1)
11319                                 next_step *= border;
11320                 }
11321                 /* When data backed up, checkpoint stored,
11322                  * kick the kernel to reshape unit of data
11323                  */
11324                 next_step = next_step + sra->reshape_progress;
11325                 /* limit next step to array max position */
11326                 if (next_step > max_position)
11327                         next_step = max_position;
11328                 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
11329                 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
11330                 sra->reshape_progress = next_step;
11331
11332                 /* wait until reshape finish */
11333                 if (wait_for_reshape_imsm(sra, ndata)) {
11334                         dprintf("wait_for_reshape_imsm returned error!\n");
11335                         goto abort;
11336                 }
11337                 if (sigterm)
11338                         goto abort;
11339
11340                 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
11341                         /* ignore error == 2, this can mean end of reshape here
11342                          */
11343                         dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11344                         goto abort;
11345                 }
11346
11347         }
11348
11349         /* clear migr_rec on disks after successful migration */
11350         struct dl *d;
11351
11352         memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*sector_size);
11353         for (d = super->disks; d; d = d->next) {
11354                 if (d->index < 0 || is_failed(&d->disk))
11355                         continue;
11356                 unsigned long long dsize;
11357
11358                 get_dev_size(d->fd, NULL, &dsize);
11359                 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
11360                             SEEK_SET) >= 0) {
11361                         if (write(d->fd, super->migr_rec_buf,
11362                             MIGR_REC_BUF_SECTORS*sector_size) !=
11363                             MIGR_REC_BUF_SECTORS*sector_size)
11364                                 perror("Write migr_rec failed");
11365                 }
11366         }
11367
11368         /* return '1' if done */
11369         ret_val = 1;
11370 abort:
11371         free(buf);
11372         /* See Grow.c: abort_reshape() for further explanation */
11373         sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
11374         sysfs_set_num(sra, NULL, "suspend_hi", 0);
11375         sysfs_set_num(sra, NULL, "suspend_lo", 0);
11376
11377         return ret_val;
11378 }
11379
11380 #endif /* MDASSEMBLE */
11381
11382 struct superswitch super_imsm = {
11383 #ifndef MDASSEMBLE
11384         .examine_super  = examine_super_imsm,
11385         .brief_examine_super = brief_examine_super_imsm,
11386         .brief_examine_subarrays = brief_examine_subarrays_imsm,
11387         .export_examine_super = export_examine_super_imsm,
11388         .detail_super   = detail_super_imsm,
11389         .brief_detail_super = brief_detail_super_imsm,
11390         .write_init_super = write_init_super_imsm,
11391         .validate_geometry = validate_geometry_imsm,
11392         .add_to_super   = add_to_super_imsm,
11393         .remove_from_super = remove_from_super_imsm,
11394         .detail_platform = detail_platform_imsm,
11395         .export_detail_platform = export_detail_platform_imsm,
11396         .kill_subarray = kill_subarray_imsm,
11397         .update_subarray = update_subarray_imsm,
11398         .load_container = load_container_imsm,
11399         .default_geometry = default_geometry_imsm,
11400         .get_disk_controller_domain = imsm_get_disk_controller_domain,
11401         .reshape_super  = imsm_reshape_super,
11402         .manage_reshape = imsm_manage_reshape,
11403         .recover_backup = recover_backup_imsm,
11404         .copy_metadata = copy_metadata_imsm,
11405 #endif
11406         .match_home     = match_home_imsm,
11407         .uuid_from_super= uuid_from_super_imsm,
11408         .getinfo_super  = getinfo_super_imsm,
11409         .getinfo_super_disks = getinfo_super_disks_imsm,
11410         .update_super   = update_super_imsm,
11411
11412         .avail_size     = avail_size_imsm,
11413         .min_acceptable_spare_size = min_acceptable_spare_size_imsm,
11414
11415         .compare_super  = compare_super_imsm,
11416
11417         .load_super     = load_super_imsm,
11418         .init_super     = init_super_imsm,
11419         .store_super    = store_super_imsm,
11420         .free_super     = free_super_imsm,
11421         .match_metadata_desc = match_metadata_desc_imsm,
11422         .container_content = container_content_imsm,
11423         .validate_container = validate_container_imsm,
11424
11425         .external       = 1,
11426         .name = "imsm",
11427
11428 #ifndef MDASSEMBLE
11429 /* for mdmon */
11430         .open_new       = imsm_open_new,
11431         .set_array_state= imsm_set_array_state,
11432         .set_disk       = imsm_set_disk,
11433         .sync_metadata  = imsm_sync_metadata,
11434         .activate_spare = imsm_activate_spare,
11435         .process_update = imsm_process_update,
11436         .prepare_update = imsm_prepare_update,
11437         .record_bad_block = imsm_record_badblock,
11438         .clear_bad_block  = imsm_clear_badblock,
11439 #endif /* MDASSEMBLE */
11440 };