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