]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
mdadm: Move pr_vrb define to mdadm.h
[thirdparty/mdadm.git] / super-intel.c
1 /*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2008 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "dlink.h"
24 #include "sha1.h"
25 #include "platform-intel.h"
26 #include <values.h>
27 #include <scsi/sg.h>
28 #include <ctype.h>
29 #include <dirent.h>
30
31 /* MPB == Metadata Parameter Block */
32 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
33 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
34 #define MPB_VERSION_RAID0 "1.0.00"
35 #define MPB_VERSION_RAID1 "1.1.00"
36 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
37 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
38 #define MPB_VERSION_RAID5 "1.2.02"
39 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
40 #define MPB_VERSION_CNG "1.2.06"
41 #define MPB_VERSION_ATTRIBS "1.3.00"
42 #define MAX_SIGNATURE_LENGTH 32
43 #define MAX_RAID_SERIAL_LEN 16
44
45 /* supports RAID0 */
46 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 /* supports RAID1 */
48 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 /* supports RAID10 */
50 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 /* supports RAID1E */
52 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 /* supports RAID5 */
54 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
55 /* supports RAID CNG */
56 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
57 /* supports expanded stripe sizes of 256K, 512K and 1MB */
58 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59
60 /* The OROM Support RST Caching of Volumes */
61 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
62 /* The OROM supports creating disks greater than 2TB */
63 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
64 /* The OROM supports Bad Block Management */
65 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66
67 /* THe OROM Supports NVM Caching of Volumes */
68 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
69 /* The OROM supports creating volumes greater than 2TB */
70 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
71 /* originally for PMP, now it's wasted b/c. Never use this bit! */
72 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
73 /* Verify MPB contents against checksum after reading MPB */
74 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75
76 /* Define all supported attributes that have to be accepted by mdadm
77 */
78 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB | \
80 MPB_ATTRIB_2TB_DISK | \
81 MPB_ATTRIB_RAID0 | \
82 MPB_ATTRIB_RAID1 | \
83 MPB_ATTRIB_RAID10 | \
84 MPB_ATTRIB_RAID5 | \
85 MPB_ATTRIB_EXP_STRIPE_SIZE | \
86 MPB_ATTRIB_BBM)
87
88 /* Define attributes that are unused but not harmful */
89 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
90
91 #define MPB_SECTOR_CNT 2210
92 #define IMSM_RESERVED_SECTORS 8192
93 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2048
94 #define SECT_PER_MB_SHIFT 11
95 #define MAX_SECTOR_SIZE 4096
96 #define MULTIPLE_PPL_AREA_SIZE_IMSM (1024 * 1024) /* Size of the whole
97 * mutliple PPL area
98 */
99
100 /*
101 * Internal Write-intent bitmap is stored in the same area where PPL.
102 * Both features are mutually exclusive, so it is not an issue.
103 * The first 8KiB of the area are reserved and shall not be used.
104 */
105 #define IMSM_BITMAP_AREA_RESERVED_SIZE 8192
106
107 #define IMSM_BITMAP_HEADER_OFFSET (IMSM_BITMAP_AREA_RESERVED_SIZE)
108 #define IMSM_BITMAP_HEADER_SIZE MAX_SECTOR_SIZE
109
110 #define IMSM_BITMAP_START_OFFSET (IMSM_BITMAP_HEADER_OFFSET + IMSM_BITMAP_HEADER_SIZE)
111 #define IMSM_BITMAP_AREA_SIZE (MULTIPLE_PPL_AREA_SIZE_IMSM - IMSM_BITMAP_START_OFFSET)
112 #define IMSM_BITMAP_AND_HEADER_SIZE (IMSM_BITMAP_AREA_SIZE + IMSM_BITMAP_HEADER_SIZE)
113
114 #define IMSM_DEFAULT_BITMAP_CHUNKSIZE (64 * 1024 * 1024)
115 #define IMSM_DEFAULT_BITMAP_DAEMON_SLEEP 5
116
117 /*
118 * This macro let's us ensure that no-one accidentally
119 * changes the size of a struct
120 */
121 #define ASSERT_SIZE(_struct, size) \
122 static inline void __assert_size_##_struct(void) \
123 { \
124 switch (0) { \
125 case 0: break; \
126 case (sizeof(struct _struct) == size): break; \
127 } \
128 }
129
130 /* Disk configuration info. */
131 #define IMSM_MAX_DEVICES 255
132 struct imsm_disk {
133 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
134 __u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
135 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
136 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
137 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
138 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
139 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
140 __u32 status; /* 0xF0 - 0xF3 */
141 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
142 __u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
143 #define IMSM_DISK_FILLERS 3
144 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
145 };
146 ASSERT_SIZE(imsm_disk, 48)
147
148 /* map selector for map managment
149 */
150 #define MAP_0 0
151 #define MAP_1 1
152 #define MAP_X -1
153
154 /* RAID map configuration infos. */
155 struct imsm_map {
156 __u32 pba_of_lba0_lo; /* start address of partition */
157 __u32 blocks_per_member_lo;/* blocks per member */
158 __u32 num_data_stripes_lo; /* number of data stripes */
159 __u16 blocks_per_strip;
160 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
161 #define IMSM_T_STATE_NORMAL 0
162 #define IMSM_T_STATE_UNINITIALIZED 1
163 #define IMSM_T_STATE_DEGRADED 2
164 #define IMSM_T_STATE_FAILED 3
165 __u8 raid_level;
166 #define IMSM_T_RAID0 0
167 #define IMSM_T_RAID1 1
168 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
169 __u8 num_members; /* number of member disks */
170 __u8 num_domains; /* number of parity domains */
171 __u8 failed_disk_num; /* valid only when state is degraded */
172 __u8 ddf;
173 __u32 pba_of_lba0_hi;
174 __u32 blocks_per_member_hi;
175 __u32 num_data_stripes_hi;
176 __u32 filler[4]; /* expansion area */
177 #define IMSM_ORD_REBUILD (1 << 24)
178 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
179 * top byte contains some flags
180 */
181 };
182 ASSERT_SIZE(imsm_map, 52)
183
184 struct imsm_vol {
185 __u32 curr_migr_unit_lo;
186 __u32 checkpoint_id; /* id to access curr_migr_unit */
187 __u8 migr_state; /* Normal or Migrating */
188 #define MIGR_INIT 0
189 #define MIGR_REBUILD 1
190 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
191 #define MIGR_GEN_MIGR 3
192 #define MIGR_STATE_CHANGE 4
193 #define MIGR_REPAIR 5
194 __u8 migr_type; /* Initializing, Rebuilding, ... */
195 #define RAIDVOL_CLEAN 0
196 #define RAIDVOL_DIRTY 1
197 #define RAIDVOL_DSRECORD_VALID 2
198 __u8 dirty;
199 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
200 __u16 verify_errors; /* number of mismatches */
201 __u16 bad_blocks; /* number of bad blocks during verify */
202 __u32 curr_migr_unit_hi;
203 __u32 filler[3];
204 struct imsm_map map[1];
205 /* here comes another one if migr_state */
206 };
207 ASSERT_SIZE(imsm_vol, 84)
208
209 struct imsm_dev {
210 __u8 volume[MAX_RAID_SERIAL_LEN];
211 __u32 size_low;
212 __u32 size_high;
213 #define DEV_BOOTABLE __cpu_to_le32(0x01)
214 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
215 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
216 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
217 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
218 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
219 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
220 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
221 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
222 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
223 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
224 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
225 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
226 __u32 status; /* Persistent RaidDev status */
227 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
228 __u8 migr_priority;
229 __u8 num_sub_vols;
230 __u8 tid;
231 __u8 cng_master_disk;
232 __u16 cache_policy;
233 __u8 cng_state;
234 __u8 cng_sub_state;
235 __u16 my_vol_raid_dev_num; /* Used in Unique volume Id for this RaidDev */
236
237 /* NVM_EN */
238 __u8 nv_cache_mode;
239 __u8 nv_cache_flags;
240
241 /* Unique Volume Id of the NvCache Volume associated with this volume */
242 __u32 nvc_vol_orig_family_num;
243 __u16 nvc_vol_raid_dev_num;
244
245 #define RWH_OFF 0
246 #define RWH_DISTRIBUTED 1
247 #define RWH_JOURNALING_DRIVE 2
248 #define RWH_MULTIPLE_DISTRIBUTED 3
249 #define RWH_MULTIPLE_PPLS_JOURNALING_DRIVE 4
250 #define RWH_MULTIPLE_OFF 5
251 #define RWH_BITMAP 6
252 __u8 rwh_policy; /* Raid Write Hole Policy */
253 __u8 jd_serial[MAX_RAID_SERIAL_LEN]; /* Journal Drive serial number */
254 __u8 filler1;
255
256 #define IMSM_DEV_FILLERS 3
257 __u32 filler[IMSM_DEV_FILLERS];
258 struct imsm_vol vol;
259 };
260 ASSERT_SIZE(imsm_dev, 164)
261
262 struct imsm_super {
263 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
264 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
265 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
266 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
267 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
268 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
269 __u32 attributes; /* 0x34 - 0x37 */
270 __u8 num_disks; /* 0x38 Number of configured disks */
271 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
272 __u8 error_log_pos; /* 0x3A */
273 __u8 fill[1]; /* 0x3B */
274 __u32 cache_size; /* 0x3c - 0x40 in mb */
275 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
276 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
277 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
278 __u16 num_raid_devs_created; /* 0x4C - 0x4D Used for generating unique
279 * volume IDs for raid_dev created in this array
280 * (starts at 1)
281 */
282 __u16 filler1; /* 0x4E - 0x4F */
283 __u64 creation_time; /* 0x50 - 0x57 Array creation time */
284 #define IMSM_FILLERS 32
285 __u32 filler[IMSM_FILLERS]; /* 0x58 - 0xD7 RAID_MPB_FILLERS */
286 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
287 /* here comes imsm_dev[num_raid_devs] */
288 /* here comes BBM logs */
289 };
290 ASSERT_SIZE(imsm_super, 264)
291
292 #define BBM_LOG_MAX_ENTRIES 254
293 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
294 #define BBM_LOG_SIGNATURE 0xabadb10c
295
296 struct bbm_log_block_addr {
297 __u16 w1;
298 __u32 dw1;
299 } __attribute__ ((__packed__));
300
301 struct bbm_log_entry {
302 __u8 marked_count; /* Number of blocks marked - 1 */
303 __u8 disk_ordinal; /* Disk entry within the imsm_super */
304 struct bbm_log_block_addr defective_block_start;
305 } __attribute__ ((__packed__));
306
307 struct bbm_log {
308 __u32 signature; /* 0xABADB10C */
309 __u32 entry_count;
310 struct bbm_log_entry marked_block_entries[BBM_LOG_MAX_ENTRIES];
311 };
312 ASSERT_SIZE(bbm_log, 2040)
313
314 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
315
316 #define BLOCKS_PER_KB (1024/512)
317
318 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
319
320 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
321
322 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
323 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
324 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
325 */
326
327 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
328 * be recovered using srcMap */
329 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
330 * already been migrated and must
331 * be recovered from checkpoint area */
332
333 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of single PPL, without the header */
334
335 struct migr_record {
336 __u32 rec_status; /* Status used to determine how to restart
337 * migration in case it aborts
338 * in some fashion */
339 __u32 curr_migr_unit_lo; /* 0..numMigrUnits-1 */
340 __u32 family_num; /* Family number of MPB
341 * containing the RaidDev
342 * that is migrating */
343 __u32 ascending_migr; /* True if migrating in increasing
344 * order of lbas */
345 __u32 blocks_per_unit; /* Num disk blocks per unit of operation */
346 __u32 dest_depth_per_unit; /* Num member blocks each destMap
347 * member disk
348 * advances per unit-of-operation */
349 __u32 ckpt_area_pba_lo; /* Pba of first block of ckpt copy area */
350 __u32 dest_1st_member_lba_lo; /* First member lba on first
351 * stripe of destination */
352 __u32 num_migr_units_lo; /* Total num migration units-of-op */
353 __u32 post_migr_vol_cap; /* Size of volume after
354 * migration completes */
355 __u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
356 __u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
357 * migration ckpt record was read from
358 * (for recovered migrations) */
359 __u32 curr_migr_unit_hi; /* 0..numMigrUnits-1 high order 32 bits */
360 __u32 ckpt_area_pba_hi; /* Pba of first block of ckpt copy area
361 * high order 32 bits */
362 __u32 dest_1st_member_lba_hi; /* First member lba on first stripe of
363 * destination - high order 32 bits */
364 __u32 num_migr_units_hi; /* Total num migration units-of-op
365 * high order 32 bits */
366 __u32 filler[16];
367 };
368 ASSERT_SIZE(migr_record, 128)
369
370 /**
371 * enum imsm_status - internal IMSM return values representation.
372 * @STATUS_OK: function succeeded.
373 * @STATUS_ERROR: General error ocurred (not specified).
374 *
375 * Typedefed to imsm_status_t.
376 */
377 typedef enum imsm_status {
378 IMSM_STATUS_ERROR = -1,
379 IMSM_STATUS_OK = 0,
380 } imsm_status_t;
381
382 struct md_list {
383 /* usage marker:
384 * 1: load metadata
385 * 2: metadata does not match
386 * 4: already checked
387 */
388 int used;
389 char *devname;
390 int found;
391 int container;
392 dev_t st_rdev;
393 struct md_list *next;
394 };
395
396 static __u8 migr_type(struct imsm_dev *dev)
397 {
398 if (dev->vol.migr_type == MIGR_VERIFY &&
399 dev->status & DEV_VERIFY_AND_FIX)
400 return MIGR_REPAIR;
401 else
402 return dev->vol.migr_type;
403 }
404
405 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
406 {
407 /* for compatibility with older oroms convert MIGR_REPAIR, into
408 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
409 */
410 if (migr_type == MIGR_REPAIR) {
411 dev->vol.migr_type = MIGR_VERIFY;
412 dev->status |= DEV_VERIFY_AND_FIX;
413 } else {
414 dev->vol.migr_type = migr_type;
415 dev->status &= ~DEV_VERIFY_AND_FIX;
416 }
417 }
418
419 static unsigned int sector_count(__u32 bytes, unsigned int sector_size)
420 {
421 return ROUND_UP(bytes, sector_size) / sector_size;
422 }
423
424 static unsigned int mpb_sectors(struct imsm_super *mpb,
425 unsigned int sector_size)
426 {
427 return sector_count(__le32_to_cpu(mpb->mpb_size), sector_size);
428 }
429
430 struct intel_dev {
431 struct imsm_dev *dev;
432 struct intel_dev *next;
433 unsigned index;
434 };
435
436 struct intel_hba {
437 enum sys_dev_type type;
438 char *path;
439 char *pci_id;
440 struct intel_hba *next;
441 };
442
443 enum action {
444 DISK_REMOVE = 1,
445 DISK_ADD
446 };
447 /* internal representation of IMSM metadata */
448 struct intel_super {
449 union {
450 void *buf; /* O_DIRECT buffer for reading/writing metadata */
451 struct imsm_super *anchor; /* immovable parameters */
452 };
453 union {
454 void *migr_rec_buf; /* buffer for I/O operations */
455 struct migr_record *migr_rec; /* migration record */
456 };
457 int clean_migration_record_by_mdmon; /* when reshape is switched to next
458 array, it indicates that mdmon is allowed to clean migration
459 record */
460 size_t len; /* size of the 'buf' allocation */
461 size_t extra_space; /* extra space in 'buf' that is not used yet */
462 void *next_buf; /* for realloc'ing buf from the manager */
463 size_t next_len;
464 int updates_pending; /* count of pending updates for mdmon */
465 int current_vol; /* index of raid device undergoing creation */
466 unsigned long long create_offset; /* common start for 'current_vol' */
467 __u32 random; /* random data for seeding new family numbers */
468 struct intel_dev *devlist;
469 unsigned int sector_size; /* sector size of used member drives */
470 struct dl {
471 struct dl *next;
472 int index;
473 __u8 serial[MAX_RAID_SERIAL_LEN];
474 int major, minor;
475 char *devname;
476 struct imsm_disk disk;
477 int fd;
478 int extent_cnt;
479 struct extent *e; /* for determining freespace @ create */
480 int raiddisk; /* slot to fill in autolayout */
481 enum action action;
482 } *disks, *current_disk;
483 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
484 active */
485 struct dl *missing; /* disks removed while we weren't looking */
486 struct bbm_log *bbm_log;
487 struct intel_hba *hba; /* device path of the raid controller for this metadata */
488 const struct imsm_orom *orom; /* platform firmware support */
489 struct intel_super *next; /* (temp) list for disambiguating family_num */
490 struct md_bb bb; /* memory for get_bad_blocks call */
491 };
492
493 struct intel_disk {
494 struct imsm_disk disk;
495 #define IMSM_UNKNOWN_OWNER (-1)
496 int owner;
497 struct intel_disk *next;
498 };
499
500 /**
501 * struct extent - reserved space details.
502 * @start: start offset.
503 * @size: size of reservation, set to 0 for metadata reservation.
504 * @vol: index of the volume, meaningful if &size is set.
505 */
506 struct extent {
507 unsigned long long start, size;
508 int vol;
509 };
510
511 /* definitions of reshape process types */
512 enum imsm_reshape_type {
513 CH_TAKEOVER,
514 CH_MIGRATION,
515 CH_ARRAY_SIZE,
516 };
517
518 /* definition of messages passed to imsm_process_update */
519 enum imsm_update_type {
520 update_activate_spare,
521 update_create_array,
522 update_kill_array,
523 update_rename_array,
524 update_add_remove_disk,
525 update_reshape_container_disks,
526 update_reshape_migration,
527 update_takeover,
528 update_general_migration_checkpoint,
529 update_size_change,
530 update_prealloc_badblocks_mem,
531 update_rwh_policy,
532 };
533
534 struct imsm_update_activate_spare {
535 enum imsm_update_type type;
536 struct dl *dl;
537 int slot;
538 int array;
539 struct imsm_update_activate_spare *next;
540 };
541
542 struct geo_params {
543 char devnm[32];
544 char *dev_name;
545 unsigned long long size;
546 int level;
547 int layout;
548 int chunksize;
549 int raid_disks;
550 };
551
552 enum takeover_direction {
553 R10_TO_R0,
554 R0_TO_R10
555 };
556 struct imsm_update_takeover {
557 enum imsm_update_type type;
558 int subarray;
559 enum takeover_direction direction;
560 };
561
562 struct imsm_update_reshape {
563 enum imsm_update_type type;
564 int old_raid_disks;
565 int new_raid_disks;
566
567 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
568 };
569
570 struct imsm_update_reshape_migration {
571 enum imsm_update_type type;
572 int old_raid_disks;
573 int new_raid_disks;
574 /* fields for array migration changes
575 */
576 int subdev;
577 int new_level;
578 int new_layout;
579 int new_chunksize;
580
581 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
582 };
583
584 struct imsm_update_size_change {
585 enum imsm_update_type type;
586 int subdev;
587 long long new_size;
588 };
589
590 struct imsm_update_general_migration_checkpoint {
591 enum imsm_update_type type;
592 __u64 curr_migr_unit;
593 };
594
595 struct disk_info {
596 __u8 serial[MAX_RAID_SERIAL_LEN];
597 };
598
599 struct imsm_update_create_array {
600 enum imsm_update_type type;
601 int dev_idx;
602 struct imsm_dev dev;
603 };
604
605 struct imsm_update_kill_array {
606 enum imsm_update_type type;
607 int dev_idx;
608 };
609
610 struct imsm_update_rename_array {
611 enum imsm_update_type type;
612 __u8 name[MAX_RAID_SERIAL_LEN];
613 int dev_idx;
614 };
615
616 struct imsm_update_add_remove_disk {
617 enum imsm_update_type type;
618 };
619
620 struct imsm_update_prealloc_bb_mem {
621 enum imsm_update_type type;
622 };
623
624 struct imsm_update_rwh_policy {
625 enum imsm_update_type type;
626 int new_policy;
627 int dev_idx;
628 };
629
630 static const char *_sys_dev_type[] = {
631 [SYS_DEV_UNKNOWN] = "Unknown",
632 [SYS_DEV_SAS] = "SAS",
633 [SYS_DEV_SATA] = "SATA",
634 [SYS_DEV_NVME] = "NVMe",
635 [SYS_DEV_VMD] = "VMD",
636 [SYS_DEV_SATA_VMD] = "SATA VMD"
637 };
638
639 static int no_platform = -1;
640
641 static int check_no_platform(void)
642 {
643 static const char search[] = "mdadm.imsm.test=1";
644 FILE *fp;
645
646 if (no_platform >= 0)
647 return no_platform;
648
649 if (check_env("IMSM_NO_PLATFORM")) {
650 no_platform = 1;
651 return 1;
652 }
653 fp = fopen("/proc/cmdline", "r");
654 if (fp) {
655 char *l = conf_line(fp);
656 char *w = l;
657
658 if (l == NULL) {
659 fclose(fp);
660 return 0;
661 }
662
663 do {
664 if (strcmp(w, search) == 0)
665 no_platform = 1;
666 w = dl_next(w);
667 } while (w != l);
668 free_line(l);
669 fclose(fp);
670 if (no_platform >= 0)
671 return no_platform;
672 }
673 no_platform = 0;
674 return 0;
675 }
676
677 void imsm_set_no_platform(int v)
678 {
679 no_platform = v;
680 }
681
682 const char *get_sys_dev_type(enum sys_dev_type type)
683 {
684 if (type >= SYS_DEV_MAX)
685 type = SYS_DEV_UNKNOWN;
686
687 return _sys_dev_type[type];
688 }
689
690 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
691 {
692 struct intel_hba *result = xmalloc(sizeof(*result));
693
694 result->type = device->type;
695 result->path = xstrdup(device->path);
696 result->next = NULL;
697 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
698 result->pci_id++;
699
700 return result;
701 }
702
703 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
704 {
705 struct intel_hba *result;
706
707 for (result = hba; result; result = result->next) {
708 if (result->type == device->type && strcmp(result->path, device->path) == 0)
709 break;
710 }
711 return result;
712 }
713
714 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
715 {
716 struct intel_hba *hba;
717
718 /* check if disk attached to Intel HBA */
719 hba = find_intel_hba(super->hba, device);
720 if (hba != NULL)
721 return 1;
722 /* Check if HBA is already attached to super */
723 if (super->hba == NULL) {
724 super->hba = alloc_intel_hba(device);
725 return 1;
726 }
727
728 hba = super->hba;
729 /* Intel metadata allows for all disks attached to the same type HBA.
730 * Do not support HBA types mixing
731 */
732 if (device->type != hba->type)
733 return 2;
734
735 /* Multiple same type HBAs can be used if they share the same OROM */
736 const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
737
738 if (device_orom != super->orom)
739 return 2;
740
741 while (hba->next)
742 hba = hba->next;
743
744 hba->next = alloc_intel_hba(device);
745 return 1;
746 }
747
748 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
749 {
750 struct sys_dev *list, *elem;
751 char *disk_path;
752
753 if ((list = find_intel_devices()) == NULL)
754 return 0;
755
756 if (!is_fd_valid(fd))
757 disk_path = (char *) devname;
758 else
759 disk_path = diskfd_to_devpath(fd, 1, NULL);
760
761 if (!disk_path)
762 return 0;
763
764 for (elem = list; elem; elem = elem->next)
765 if (path_attached_to_hba(disk_path, elem->path))
766 break;
767
768 if (disk_path != devname)
769 free(disk_path);
770
771 return elem;
772 }
773
774 static int find_intel_hba_capability(int fd, struct intel_super *super,
775 char *devname);
776
777 static struct supertype *match_metadata_desc_imsm(char *arg)
778 {
779 struct supertype *st;
780
781 if (strcmp(arg, "imsm") != 0 &&
782 strcmp(arg, "default") != 0
783 )
784 return NULL;
785
786 st = xcalloc(1, sizeof(*st));
787 st->ss = &super_imsm;
788 st->max_devs = IMSM_MAX_DEVICES;
789 st->minor_version = 0;
790 st->sb = NULL;
791 return st;
792 }
793
794 static __u8 *get_imsm_version(struct imsm_super *mpb)
795 {
796 return &mpb->sig[MPB_SIG_LEN];
797 }
798
799 /* retrieve a disk directly from the anchor when the anchor is known to be
800 * up-to-date, currently only at load time
801 */
802 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
803 {
804 if (index >= mpb->num_disks)
805 return NULL;
806 return &mpb->disk[index];
807 }
808
809 /* retrieve the disk description based on a index of the disk
810 * in the sub-array
811 */
812 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
813 {
814 struct dl *d;
815
816 for (d = super->disks; d; d = d->next)
817 if (d->index == index)
818 return d;
819
820 return NULL;
821 }
822 /* retrieve a disk from the parsed metadata */
823 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
824 {
825 struct dl *dl;
826
827 dl = get_imsm_dl_disk(super, index);
828 if (dl)
829 return &dl->disk;
830
831 return NULL;
832 }
833
834 /* generate a checksum directly from the anchor when the anchor is known to be
835 * up-to-date, currently only at load or write_super after coalescing
836 */
837 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
838 {
839 __u32 end = mpb->mpb_size / sizeof(end);
840 __u32 *p = (__u32 *) mpb;
841 __u32 sum = 0;
842
843 while (end--) {
844 sum += __le32_to_cpu(*p);
845 p++;
846 }
847
848 return sum - __le32_to_cpu(mpb->check_sum);
849 }
850
851 static size_t sizeof_imsm_map(struct imsm_map *map)
852 {
853 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
854 }
855
856 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
857 {
858 /* A device can have 2 maps if it is in the middle of a migration.
859 * If second_map is:
860 * MAP_0 - we return the first map
861 * MAP_1 - we return the second map if it exists, else NULL
862 * MAP_X - we return the second map if it exists, else the first
863 */
864 struct imsm_map *map = &dev->vol.map[0];
865 struct imsm_map *map2 = NULL;
866
867 if (dev->vol.migr_state)
868 map2 = (void *)map + sizeof_imsm_map(map);
869
870 switch (second_map) {
871 case MAP_0:
872 break;
873 case MAP_1:
874 map = map2;
875 break;
876 case MAP_X:
877 if (map2)
878 map = map2;
879 break;
880 default:
881 map = NULL;
882 }
883 return map;
884
885 }
886
887 /* return the size of the device.
888 * migr_state increases the returned size if map[0] were to be duplicated
889 */
890 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
891 {
892 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
893 sizeof_imsm_map(get_imsm_map(dev, MAP_0));
894
895 /* migrating means an additional map */
896 if (dev->vol.migr_state)
897 size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
898 else if (migr_state)
899 size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
900
901 return size;
902 }
903
904 /* retrieve disk serial number list from a metadata update */
905 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
906 {
907 void *u = update;
908 struct disk_info *inf;
909
910 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
911 sizeof_imsm_dev(&update->dev, 0);
912
913 return inf;
914 }
915
916 /**
917 * __get_imsm_dev() - Get device with index from imsm_super.
918 * @mpb: &imsm_super pointer, not NULL.
919 * @index: Device index.
920 *
921 * Function works as non-NULL, aborting in such a case,
922 * when NULL would be returned.
923 *
924 * Device index should be in range 0 up to num_raid_devs.
925 * Function assumes the index was already verified.
926 * Index must be valid, otherwise abort() is called.
927 *
928 * Return: Pointer to corresponding imsm_dev.
929 *
930 */
931 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
932 {
933 int offset;
934 int i;
935 void *_mpb = mpb;
936
937 if (index >= mpb->num_raid_devs)
938 goto error;
939
940 /* devices start after all disks */
941 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
942
943 for (i = 0; i <= index; i++, offset += sizeof_imsm_dev(_mpb + offset, 0))
944 if (i == index)
945 return _mpb + offset;
946 error:
947 pr_err("cannot find imsm_dev with index %u in imsm_super\n", index);
948 abort();
949 }
950
951 /**
952 * get_imsm_dev() - Get device with index from intel_super.
953 * @super: &intel_super pointer, not NULL.
954 * @index: Device index.
955 *
956 * Function works as non-NULL, aborting in such a case,
957 * when NULL would be returned.
958 *
959 * Device index should be in range 0 up to num_raid_devs.
960 * Function assumes the index was already verified.
961 * Index must be valid, otherwise abort() is called.
962 *
963 * Return: Pointer to corresponding imsm_dev.
964 *
965 */
966 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
967 {
968 struct intel_dev *dv;
969
970 if (index >= super->anchor->num_raid_devs)
971 goto error;
972
973 for (dv = super->devlist; dv; dv = dv->next)
974 if (dv->index == index)
975 return dv->dev;
976 error:
977 pr_err("cannot find imsm_dev with index %u in intel_super\n", index);
978 abort();
979 }
980
981 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
982 *addr)
983 {
984 return ((((__u64)__le32_to_cpu(addr->dw1)) << 16) |
985 __le16_to_cpu(addr->w1));
986 }
987
988 static inline struct bbm_log_block_addr __cpu_to_le48(unsigned long long sec)
989 {
990 struct bbm_log_block_addr addr;
991
992 addr.w1 = __cpu_to_le16((__u16)(sec & 0xffff));
993 addr.dw1 = __cpu_to_le32((__u32)(sec >> 16) & 0xffffffff);
994 return addr;
995 }
996
997 /* get size of the bbm log */
998 static __u32 get_imsm_bbm_log_size(struct bbm_log *log)
999 {
1000 if (!log || log->entry_count == 0)
1001 return 0;
1002
1003 return sizeof(log->signature) +
1004 sizeof(log->entry_count) +
1005 log->entry_count * sizeof(struct bbm_log_entry);
1006 }
1007
1008 /* check if bad block is not partially stored in bbm log */
1009 static int is_stored_in_bbm(struct bbm_log *log, const __u8 idx, const unsigned
1010 long long sector, const int length, __u32 *pos)
1011 {
1012 __u32 i;
1013
1014 for (i = *pos; i < log->entry_count; i++) {
1015 struct bbm_log_entry *entry = &log->marked_block_entries[i];
1016 unsigned long long bb_start;
1017 unsigned long long bb_end;
1018
1019 bb_start = __le48_to_cpu(&entry->defective_block_start);
1020 bb_end = bb_start + (entry->marked_count + 1);
1021
1022 if ((entry->disk_ordinal == idx) && (bb_start >= sector) &&
1023 (bb_end <= sector + length)) {
1024 *pos = i;
1025 return 1;
1026 }
1027 }
1028 return 0;
1029 }
1030
1031 /* record new bad block in bbm log */
1032 static int record_new_badblock(struct bbm_log *log, const __u8 idx, unsigned
1033 long long sector, int length)
1034 {
1035 int new_bb = 0;
1036 __u32 pos = 0;
1037 struct bbm_log_entry *entry = NULL;
1038
1039 while (is_stored_in_bbm(log, idx, sector, length, &pos)) {
1040 struct bbm_log_entry *e = &log->marked_block_entries[pos];
1041
1042 if ((e->marked_count + 1 == BBM_LOG_MAX_LBA_ENTRY_VAL) &&
1043 (__le48_to_cpu(&e->defective_block_start) == sector)) {
1044 sector += BBM_LOG_MAX_LBA_ENTRY_VAL;
1045 length -= BBM_LOG_MAX_LBA_ENTRY_VAL;
1046 pos = pos + 1;
1047 continue;
1048 }
1049 entry = e;
1050 break;
1051 }
1052
1053 if (entry) {
1054 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1055 BBM_LOG_MAX_LBA_ENTRY_VAL;
1056 entry->defective_block_start = __cpu_to_le48(sector);
1057 entry->marked_count = cnt - 1;
1058 if (cnt == length)
1059 return 1;
1060 sector += cnt;
1061 length -= cnt;
1062 }
1063
1064 new_bb = ROUND_UP(length, BBM_LOG_MAX_LBA_ENTRY_VAL) /
1065 BBM_LOG_MAX_LBA_ENTRY_VAL;
1066 if (log->entry_count + new_bb > BBM_LOG_MAX_ENTRIES)
1067 return 0;
1068
1069 while (length > 0) {
1070 int cnt = (length <= BBM_LOG_MAX_LBA_ENTRY_VAL) ? length :
1071 BBM_LOG_MAX_LBA_ENTRY_VAL;
1072 struct bbm_log_entry *entry =
1073 &log->marked_block_entries[log->entry_count];
1074
1075 entry->defective_block_start = __cpu_to_le48(sector);
1076 entry->marked_count = cnt - 1;
1077 entry->disk_ordinal = idx;
1078
1079 sector += cnt;
1080 length -= cnt;
1081
1082 log->entry_count++;
1083 }
1084
1085 return new_bb;
1086 }
1087
1088 /* clear all bad blocks for given disk */
1089 static void clear_disk_badblocks(struct bbm_log *log, const __u8 idx)
1090 {
1091 __u32 i = 0;
1092
1093 while (i < log->entry_count) {
1094 struct bbm_log_entry *entries = log->marked_block_entries;
1095
1096 if (entries[i].disk_ordinal == idx) {
1097 if (i < log->entry_count - 1)
1098 entries[i] = entries[log->entry_count - 1];
1099 log->entry_count--;
1100 } else {
1101 i++;
1102 }
1103 }
1104 }
1105
1106 /* clear given bad block */
1107 static int clear_badblock(struct bbm_log *log, const __u8 idx, const unsigned
1108 long long sector, const int length) {
1109 __u32 i = 0;
1110
1111 while (i < log->entry_count) {
1112 struct bbm_log_entry *entries = log->marked_block_entries;
1113
1114 if ((entries[i].disk_ordinal == idx) &&
1115 (__le48_to_cpu(&entries[i].defective_block_start) ==
1116 sector) && (entries[i].marked_count + 1 == length)) {
1117 if (i < log->entry_count - 1)
1118 entries[i] = entries[log->entry_count - 1];
1119 log->entry_count--;
1120 break;
1121 }
1122 i++;
1123 }
1124
1125 return 1;
1126 }
1127
1128 /* allocate and load BBM log from metadata */
1129 static int load_bbm_log(struct intel_super *super)
1130 {
1131 struct imsm_super *mpb = super->anchor;
1132 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1133
1134 super->bbm_log = xcalloc(1, sizeof(struct bbm_log));
1135 if (!super->bbm_log)
1136 return 1;
1137
1138 if (bbm_log_size) {
1139 struct bbm_log *log = (void *)mpb +
1140 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1141
1142 __u32 entry_count;
1143
1144 if (bbm_log_size < sizeof(log->signature) +
1145 sizeof(log->entry_count))
1146 return 2;
1147
1148 entry_count = __le32_to_cpu(log->entry_count);
1149 if ((__le32_to_cpu(log->signature) != BBM_LOG_SIGNATURE) ||
1150 (entry_count > BBM_LOG_MAX_ENTRIES))
1151 return 3;
1152
1153 if (bbm_log_size !=
1154 sizeof(log->signature) + sizeof(log->entry_count) +
1155 entry_count * sizeof(struct bbm_log_entry))
1156 return 4;
1157
1158 memcpy(super->bbm_log, log, bbm_log_size);
1159 } else {
1160 super->bbm_log->signature = __cpu_to_le32(BBM_LOG_SIGNATURE);
1161 super->bbm_log->entry_count = 0;
1162 }
1163
1164 return 0;
1165 }
1166
1167 /* checks if bad block is within volume boundaries */
1168 static int is_bad_block_in_volume(const struct bbm_log_entry *entry,
1169 const unsigned long long start_sector,
1170 const unsigned long long size)
1171 {
1172 unsigned long long bb_start;
1173 unsigned long long bb_end;
1174
1175 bb_start = __le48_to_cpu(&entry->defective_block_start);
1176 bb_end = bb_start + (entry->marked_count + 1);
1177
1178 if (((bb_start >= start_sector) && (bb_start < start_sector + size)) ||
1179 ((bb_end >= start_sector) && (bb_end <= start_sector + size)))
1180 return 1;
1181
1182 return 0;
1183 }
1184
1185 /* get list of bad blocks on a drive for a volume */
1186 static void get_volume_badblocks(const struct bbm_log *log, const __u8 idx,
1187 const unsigned long long start_sector,
1188 const unsigned long long size,
1189 struct md_bb *bbs)
1190 {
1191 __u32 count = 0;
1192 __u32 i;
1193
1194 for (i = 0; i < log->entry_count; i++) {
1195 const struct bbm_log_entry *ent =
1196 &log->marked_block_entries[i];
1197 struct md_bb_entry *bb;
1198
1199 if ((ent->disk_ordinal == idx) &&
1200 is_bad_block_in_volume(ent, start_sector, size)) {
1201
1202 if (!bbs->entries) {
1203 bbs->entries = xmalloc(BBM_LOG_MAX_ENTRIES *
1204 sizeof(*bb));
1205 if (!bbs->entries)
1206 break;
1207 }
1208
1209 bb = &bbs->entries[count++];
1210 bb->sector = __le48_to_cpu(&ent->defective_block_start);
1211 bb->length = ent->marked_count + 1;
1212 }
1213 }
1214 bbs->count = count;
1215 }
1216
1217 /*
1218 * for second_map:
1219 * == MAP_0 get first map
1220 * == MAP_1 get second map
1221 * == MAP_X than get map according to the current migr_state
1222 */
1223 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
1224 int slot,
1225 int second_map)
1226 {
1227 struct imsm_map *map;
1228
1229 map = get_imsm_map(dev, second_map);
1230
1231 /* top byte identifies disk under rebuild */
1232 return __le32_to_cpu(map->disk_ord_tbl[slot]);
1233 }
1234
1235 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1236 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
1237 {
1238 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
1239
1240 return ord_to_idx(ord);
1241 }
1242
1243 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
1244 {
1245 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
1246 }
1247
1248 static int get_imsm_disk_slot(struct imsm_map *map, const unsigned int idx)
1249 {
1250 int slot;
1251 __u32 ord;
1252
1253 for (slot = 0; slot < map->num_members; slot++) {
1254 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
1255 if (ord_to_idx(ord) == idx)
1256 return slot;
1257 }
1258
1259 return IMSM_STATUS_ERROR;
1260 }
1261
1262 static int get_imsm_raid_level(struct imsm_map *map)
1263 {
1264 if (map->raid_level == 1) {
1265 if (map->num_members == 2)
1266 return 1;
1267 else
1268 return 10;
1269 }
1270
1271 return map->raid_level;
1272 }
1273
1274 /**
1275 * get_disk_slot_in_dev() - retrieve disk slot from &imsm_dev.
1276 * @super: &intel_super pointer, not NULL.
1277 * @dev_idx: imsm device index.
1278 * @idx: disk index.
1279 *
1280 * Return: Slot on success, IMSM_STATUS_ERROR otherwise.
1281 */
1282 static int get_disk_slot_in_dev(struct intel_super *super, const __u8 dev_idx,
1283 const unsigned int idx)
1284 {
1285 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
1286 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1287
1288 return get_imsm_disk_slot(map, idx);
1289 }
1290
1291 static int cmp_extent(const void *av, const void *bv)
1292 {
1293 const struct extent *a = av;
1294 const struct extent *b = bv;
1295 if (a->start < b->start)
1296 return -1;
1297 if (a->start > b->start)
1298 return 1;
1299 return 0;
1300 }
1301
1302 static int count_memberships(struct dl *dl, struct intel_super *super)
1303 {
1304 int memberships = 0;
1305 int i;
1306
1307 for (i = 0; i < super->anchor->num_raid_devs; i++)
1308 if (get_disk_slot_in_dev(super, i, dl->index) >= 0)
1309 memberships++;
1310
1311 return memberships;
1312 }
1313
1314 static __u32 imsm_min_reserved_sectors(struct intel_super *super);
1315
1316 static int split_ull(unsigned long long n, void *lo, void *hi)
1317 {
1318 if (lo == 0 || hi == 0)
1319 return 1;
1320 __put_unaligned32(__cpu_to_le32((__u32)n), lo);
1321 __put_unaligned32(__cpu_to_le32((n >> 32)), hi);
1322 return 0;
1323 }
1324
1325 static unsigned long long join_u32(__u32 lo, __u32 hi)
1326 {
1327 return (unsigned long long)__le32_to_cpu(lo) |
1328 (((unsigned long long)__le32_to_cpu(hi)) << 32);
1329 }
1330
1331 static unsigned long long total_blocks(struct imsm_disk *disk)
1332 {
1333 if (disk == NULL)
1334 return 0;
1335 return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
1336 }
1337
1338 /**
1339 * imsm_num_data_members() - get data drives count for an array.
1340 * @map: Map to analyze.
1341 *
1342 * num_data_members value represents minimal count of drives for level.
1343 * The name of the property could be misleading for RAID5 with asymmetric layout
1344 * because some data required to be calculated from parity.
1345 * The property is extracted from level and num_members value.
1346 *
1347 * Return: num_data_members value on success, zero otherwise.
1348 */
1349 static __u8 imsm_num_data_members(struct imsm_map *map)
1350 {
1351 switch (get_imsm_raid_level(map)) {
1352 case 0:
1353 return map->num_members;
1354 case 1:
1355 case 10:
1356 return map->num_members / 2;
1357 case 5:
1358 return map->num_members - 1;
1359 default:
1360 dprintf("unsupported raid level\n");
1361 return 0;
1362 }
1363 }
1364
1365 static unsigned long long pba_of_lba0(struct imsm_map *map)
1366 {
1367 if (map == NULL)
1368 return 0;
1369 return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
1370 }
1371
1372 static unsigned long long blocks_per_member(struct imsm_map *map)
1373 {
1374 if (map == NULL)
1375 return 0;
1376 return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
1377 }
1378
1379 static unsigned long long num_data_stripes(struct imsm_map *map)
1380 {
1381 if (map == NULL)
1382 return 0;
1383 return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
1384 }
1385
1386 static unsigned long long vol_curr_migr_unit(struct imsm_dev *dev)
1387 {
1388 if (dev == NULL)
1389 return 0;
1390
1391 return join_u32(dev->vol.curr_migr_unit_lo, dev->vol.curr_migr_unit_hi);
1392 }
1393
1394 static unsigned long long imsm_dev_size(struct imsm_dev *dev)
1395 {
1396 if (dev == NULL)
1397 return 0;
1398 return join_u32(dev->size_low, dev->size_high);
1399 }
1400
1401 static unsigned long long migr_chkp_area_pba(struct migr_record *migr_rec)
1402 {
1403 if (migr_rec == NULL)
1404 return 0;
1405 return join_u32(migr_rec->ckpt_area_pba_lo,
1406 migr_rec->ckpt_area_pba_hi);
1407 }
1408
1409 static unsigned long long current_migr_unit(struct migr_record *migr_rec)
1410 {
1411 if (migr_rec == NULL)
1412 return 0;
1413 return join_u32(migr_rec->curr_migr_unit_lo,
1414 migr_rec->curr_migr_unit_hi);
1415 }
1416
1417 static unsigned long long migr_dest_1st_member_lba(struct migr_record *migr_rec)
1418 {
1419 if (migr_rec == NULL)
1420 return 0;
1421 return join_u32(migr_rec->dest_1st_member_lba_lo,
1422 migr_rec->dest_1st_member_lba_hi);
1423 }
1424
1425 static unsigned long long get_num_migr_units(struct migr_record *migr_rec)
1426 {
1427 if (migr_rec == NULL)
1428 return 0;
1429 return join_u32(migr_rec->num_migr_units_lo,
1430 migr_rec->num_migr_units_hi);
1431 }
1432
1433 static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
1434 {
1435 split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
1436 }
1437
1438 /**
1439 * set_num_domains() - Set number of domains for an array.
1440 * @map: Map to be updated.
1441 *
1442 * num_domains property represents copies count of each data drive, thus make
1443 * it meaningful only for RAID1 and RAID10. IMSM supports two domains for
1444 * raid1 and raid10.
1445 */
1446 static void set_num_domains(struct imsm_map *map)
1447 {
1448 int level = get_imsm_raid_level(map);
1449
1450 if (level == 1 || level == 10)
1451 map->num_domains = 2;
1452 else
1453 map->num_domains = 1;
1454 }
1455
1456 static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
1457 {
1458 split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
1459 }
1460
1461 static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
1462 {
1463 split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
1464 }
1465
1466 static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
1467 {
1468 split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
1469 }
1470
1471 /**
1472 * update_num_data_stripes() - Calculate and update num_data_stripes value.
1473 * @map: map to be updated.
1474 * @dev_size: size of volume.
1475 *
1476 * num_data_stripes value is addictionally divided by num_domains, therefore for
1477 * levels where num_domains is not 1, nds is a part of real value.
1478 */
1479 static void update_num_data_stripes(struct imsm_map *map,
1480 unsigned long long dev_size)
1481 {
1482 unsigned long long nds = dev_size / imsm_num_data_members(map);
1483
1484 nds /= map->num_domains;
1485 nds /= map->blocks_per_strip;
1486 set_num_data_stripes(map, nds);
1487 }
1488
1489 static void set_vol_curr_migr_unit(struct imsm_dev *dev, unsigned long long n)
1490 {
1491 if (dev == NULL)
1492 return;
1493
1494 split_ull(n, &dev->vol.curr_migr_unit_lo, &dev->vol.curr_migr_unit_hi);
1495 }
1496
1497 static void set_imsm_dev_size(struct imsm_dev *dev, unsigned long long n)
1498 {
1499 split_ull(n, &dev->size_low, &dev->size_high);
1500 }
1501
1502 static void set_migr_chkp_area_pba(struct migr_record *migr_rec,
1503 unsigned long long n)
1504 {
1505 split_ull(n, &migr_rec->ckpt_area_pba_lo, &migr_rec->ckpt_area_pba_hi);
1506 }
1507
1508 static void set_current_migr_unit(struct migr_record *migr_rec,
1509 unsigned long long n)
1510 {
1511 split_ull(n, &migr_rec->curr_migr_unit_lo,
1512 &migr_rec->curr_migr_unit_hi);
1513 }
1514
1515 static void set_migr_dest_1st_member_lba(struct migr_record *migr_rec,
1516 unsigned long long n)
1517 {
1518 split_ull(n, &migr_rec->dest_1st_member_lba_lo,
1519 &migr_rec->dest_1st_member_lba_hi);
1520 }
1521
1522 static void set_num_migr_units(struct migr_record *migr_rec,
1523 unsigned long long n)
1524 {
1525 split_ull(n, &migr_rec->num_migr_units_lo,
1526 &migr_rec->num_migr_units_hi);
1527 }
1528
1529 static unsigned long long per_dev_array_size(struct imsm_map *map)
1530 {
1531 unsigned long long array_size = 0;
1532
1533 if (map == NULL)
1534 return array_size;
1535
1536 array_size = num_data_stripes(map) * map->blocks_per_strip;
1537 if (get_imsm_raid_level(map) == 1 || get_imsm_raid_level(map) == 10)
1538 array_size *= 2;
1539
1540 return array_size;
1541 }
1542
1543 static struct extent *get_extents(struct intel_super *super, struct dl *dl,
1544 int get_minimal_reservation)
1545 {
1546 /* find a list of used extents on the given physical device */
1547 int memberships = count_memberships(dl, super);
1548 struct extent *rv = xcalloc(memberships + 1, sizeof(struct extent));
1549 struct extent *e = rv;
1550 int i;
1551 __u32 reservation;
1552
1553 /* trim the reserved area for spares, so they can join any array
1554 * regardless of whether the OROM has assigned sectors from the
1555 * IMSM_RESERVED_SECTORS region
1556 */
1557 if (dl->index == -1 || get_minimal_reservation)
1558 reservation = imsm_min_reserved_sectors(super);
1559 else
1560 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1561
1562 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1563 struct imsm_dev *dev = get_imsm_dev(super, i);
1564 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1565
1566 if (get_imsm_disk_slot(map, dl->index) >= 0) {
1567 e->start = pba_of_lba0(map);
1568 e->size = per_dev_array_size(map);
1569 e->vol = i;
1570 e++;
1571 }
1572 }
1573 qsort(rv, memberships, sizeof(*rv), cmp_extent);
1574
1575 /* determine the start of the metadata
1576 * when no raid devices are defined use the default
1577 * ...otherwise allow the metadata to truncate the value
1578 * as is the case with older versions of imsm
1579 */
1580 if (memberships) {
1581 struct extent *last = &rv[memberships - 1];
1582 unsigned long long remainder;
1583
1584 remainder = total_blocks(&dl->disk) - (last->start + last->size);
1585 /* round down to 1k block to satisfy precision of the kernel
1586 * 'size' interface
1587 */
1588 remainder &= ~1UL;
1589 /* make sure remainder is still sane */
1590 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
1591 remainder = ROUND_UP(super->len, 512) >> 9;
1592 if (reservation > remainder)
1593 reservation = remainder;
1594 }
1595 e->start = total_blocks(&dl->disk) - reservation;
1596 e->size = 0;
1597 return rv;
1598 }
1599
1600 /* try to determine how much space is reserved for metadata from
1601 * the last get_extents() entry, otherwise fallback to the
1602 * default
1603 */
1604 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
1605 {
1606 struct extent *e;
1607 int i;
1608 __u32 rv;
1609
1610 /* for spares just return a minimal reservation which will grow
1611 * once the spare is picked up by an array
1612 */
1613 if (dl->index == -1)
1614 return MPB_SECTOR_CNT;
1615
1616 e = get_extents(super, dl, 0);
1617 if (!e)
1618 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1619
1620 /* scroll to last entry */
1621 for (i = 0; e[i].size; i++)
1622 continue;
1623
1624 rv = total_blocks(&dl->disk) - e[i].start;
1625
1626 free(e);
1627
1628 return rv;
1629 }
1630
1631 static int is_spare(struct imsm_disk *disk)
1632 {
1633 return (disk->status & SPARE_DISK) == SPARE_DISK;
1634 }
1635
1636 static int is_configured(struct imsm_disk *disk)
1637 {
1638 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
1639 }
1640
1641 static int is_failed(struct imsm_disk *disk)
1642 {
1643 return (disk->status & FAILED_DISK) == FAILED_DISK;
1644 }
1645
1646 static int is_journal(struct imsm_disk *disk)
1647 {
1648 return (disk->status & JOURNAL_DISK) == JOURNAL_DISK;
1649 }
1650
1651 /**
1652 * round_member_size_to_mb()- Round given size to closest MiB.
1653 * @size: size to round in sectors.
1654 */
1655 static inline unsigned long long round_member_size_to_mb(unsigned long long size)
1656 {
1657 return (size >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
1658 }
1659
1660 /**
1661 * round_size_to_mb()- Round given size.
1662 * @array_size: size to round in sectors.
1663 * @disk_count: count of data members.
1664 *
1665 * Get size per each data member and round it to closest MiB to ensure that data
1666 * splits evenly between members.
1667 *
1668 * Return: Array size, rounded down.
1669 */
1670 static inline unsigned long long round_size_to_mb(unsigned long long array_size,
1671 unsigned int disk_count)
1672 {
1673 return round_member_size_to_mb(array_size / disk_count) * disk_count;
1674 }
1675
1676 static int able_to_resync(int raid_level, int missing_disks)
1677 {
1678 int max_missing_disks = 0;
1679
1680 switch (raid_level) {
1681 case 10:
1682 max_missing_disks = 1;
1683 break;
1684 default:
1685 max_missing_disks = 0;
1686 }
1687 return missing_disks <= max_missing_disks;
1688 }
1689
1690 /* try to determine how much space is reserved for metadata from
1691 * the last get_extents() entry on the smallest active disk,
1692 * otherwise fallback to the default
1693 */
1694 static __u32 imsm_min_reserved_sectors(struct intel_super *super)
1695 {
1696 struct extent *e;
1697 int i;
1698 unsigned long long min_active;
1699 __u32 remainder;
1700 __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1701 struct dl *dl, *dl_min = NULL;
1702
1703 if (!super)
1704 return rv;
1705
1706 min_active = 0;
1707 for (dl = super->disks; dl; dl = dl->next) {
1708 if (dl->index < 0)
1709 continue;
1710 unsigned long long blocks = total_blocks(&dl->disk);
1711 if (blocks < min_active || min_active == 0) {
1712 dl_min = dl;
1713 min_active = blocks;
1714 }
1715 }
1716 if (!dl_min)
1717 return rv;
1718
1719 /* find last lba used by subarrays on the smallest active disk */
1720 e = get_extents(super, dl_min, 0);
1721 if (!e)
1722 return rv;
1723 for (i = 0; e[i].size; i++)
1724 continue;
1725
1726 remainder = min_active - e[i].start;
1727 free(e);
1728
1729 /* to give priority to recovery we should not require full
1730 IMSM_RESERVED_SECTORS from the spare */
1731 rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
1732
1733 /* if real reservation is smaller use that value */
1734 return (remainder < rv) ? remainder : rv;
1735 }
1736
1737 static bool is_gen_migration(struct imsm_dev *dev);
1738
1739 #define IMSM_4K_DIV 8
1740
1741 static __u64 blocks_per_migr_unit(struct intel_super *super,
1742 struct imsm_dev *dev);
1743
1744 static void print_imsm_dev(struct intel_super *super,
1745 struct imsm_dev *dev,
1746 char *uuid,
1747 int disk_idx)
1748 {
1749 __u64 sz;
1750 int slot, i;
1751 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1752 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
1753 __u32 ord;
1754
1755 printf("\n");
1756 printf("[%.16s]:\n", dev->volume);
1757 printf(" Subarray : %d\n", super->current_vol);
1758 printf(" UUID : %s\n", uuid);
1759 printf(" RAID Level : %d", get_imsm_raid_level(map));
1760 if (map2)
1761 printf(" <-- %d", get_imsm_raid_level(map2));
1762 printf("\n");
1763 printf(" Members : %d", map->num_members);
1764 if (map2)
1765 printf(" <-- %d", map2->num_members);
1766 printf("\n");
1767 printf(" Slots : [");
1768 for (i = 0; i < map->num_members; i++) {
1769 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
1770 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1771 }
1772 printf("]");
1773 if (map2) {
1774 printf(" <-- [");
1775 for (i = 0; i < map2->num_members; i++) {
1776 ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
1777 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
1778 }
1779 printf("]");
1780 }
1781 printf("\n");
1782 printf(" Failed disk : ");
1783 if (map->failed_disk_num == 0xff)
1784 printf(STR_COMMON_NONE);
1785 else
1786 printf("%i", map->failed_disk_num);
1787 printf("\n");
1788 slot = get_imsm_disk_slot(map, disk_idx);
1789 if (slot >= 0) {
1790 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
1791 printf(" This Slot : %d%s\n", slot,
1792 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
1793 } else
1794 printf(" This Slot : ?\n");
1795 printf(" Sector Size : %u\n", super->sector_size);
1796 sz = imsm_dev_size(dev);
1797 printf(" Array Size : %llu%s\n",
1798 (unsigned long long)sz * 512 / super->sector_size,
1799 human_size(sz * 512));
1800 sz = blocks_per_member(map);
1801 printf(" Per Dev Size : %llu%s\n",
1802 (unsigned long long)sz * 512 / super->sector_size,
1803 human_size(sz * 512));
1804 printf(" Sector Offset : %llu\n",
1805 pba_of_lba0(map) * 512 / super->sector_size);
1806 printf(" Num Stripes : %llu\n",
1807 num_data_stripes(map));
1808 printf(" Chunk Size : %u KiB",
1809 __le16_to_cpu(map->blocks_per_strip) / 2);
1810 if (map2)
1811 printf(" <-- %u KiB",
1812 __le16_to_cpu(map2->blocks_per_strip) / 2);
1813 printf("\n");
1814 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
1815 printf(" Migrate State : ");
1816 if (dev->vol.migr_state) {
1817 if (migr_type(dev) == MIGR_INIT)
1818 printf("initialize\n");
1819 else if (migr_type(dev) == MIGR_REBUILD)
1820 printf("rebuild\n");
1821 else if (migr_type(dev) == MIGR_VERIFY)
1822 printf("check\n");
1823 else if (migr_type(dev) == MIGR_GEN_MIGR)
1824 printf("general migration\n");
1825 else if (migr_type(dev) == MIGR_STATE_CHANGE)
1826 printf("state change\n");
1827 else if (migr_type(dev) == MIGR_REPAIR)
1828 printf("repair\n");
1829 else
1830 printf("<unknown:%d>\n", migr_type(dev));
1831 } else
1832 printf("idle\n");
1833 printf(" Map State : %s", map_state_str[map->map_state]);
1834 if (dev->vol.migr_state) {
1835 struct imsm_map *map = get_imsm_map(dev, MAP_1);
1836
1837 printf(" <-- %s", map_state_str[map->map_state]);
1838 printf("\n Checkpoint : %llu ", vol_curr_migr_unit(dev));
1839 if (is_gen_migration(dev) && (slot > 1 || slot < 0))
1840 printf("(N/A)");
1841 else
1842 printf("(%llu)", (unsigned long long)
1843 blocks_per_migr_unit(super, dev));
1844 }
1845 printf("\n");
1846 printf(" Dirty State : %s\n", (dev->vol.dirty & RAIDVOL_DIRTY) ?
1847 "dirty" : "clean");
1848 printf(" RWH Policy : ");
1849 if (dev->rwh_policy == RWH_OFF || dev->rwh_policy == RWH_MULTIPLE_OFF)
1850 printf("off\n");
1851 else if (dev->rwh_policy == RWH_DISTRIBUTED)
1852 printf("PPL distributed\n");
1853 else if (dev->rwh_policy == RWH_JOURNALING_DRIVE)
1854 printf("PPL journaling drive\n");
1855 else if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
1856 printf("Multiple distributed PPLs\n");
1857 else if (dev->rwh_policy == RWH_MULTIPLE_PPLS_JOURNALING_DRIVE)
1858 printf("Multiple PPLs on journaling drive\n");
1859 else if (dev->rwh_policy == RWH_BITMAP)
1860 printf("Write-intent bitmap\n");
1861 else
1862 printf("<unknown:%d>\n", dev->rwh_policy);
1863
1864 printf(" Volume ID : %u\n", dev->my_vol_raid_dev_num);
1865 }
1866
1867 static void print_imsm_disk(struct imsm_disk *disk,
1868 int index,
1869 __u32 reserved,
1870 unsigned int sector_size) {
1871 char str[MAX_RAID_SERIAL_LEN + 1];
1872 __u64 sz;
1873
1874 if (index < -1 || !disk)
1875 return;
1876
1877 printf("\n");
1878 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1879 if (index >= 0)
1880 printf(" Disk%02d Serial : %s\n", index, str);
1881 else
1882 printf(" Disk Serial : %s\n", str);
1883 printf(" State :%s%s%s%s\n", is_spare(disk) ? " spare" : "",
1884 is_configured(disk) ? " active" : "",
1885 is_failed(disk) ? " failed" : "",
1886 is_journal(disk) ? " journal" : "");
1887 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1888 sz = total_blocks(disk) - reserved;
1889 printf(" Usable Size : %llu%s\n",
1890 (unsigned long long)sz * 512 / sector_size,
1891 human_size(sz * 512));
1892 }
1893
1894 void convert_to_4k_imsm_migr_rec(struct intel_super *super)
1895 {
1896 struct migr_record *migr_rec = super->migr_rec;
1897
1898 migr_rec->blocks_per_unit /= IMSM_4K_DIV;
1899 migr_rec->dest_depth_per_unit /= IMSM_4K_DIV;
1900 split_ull((join_u32(migr_rec->post_migr_vol_cap,
1901 migr_rec->post_migr_vol_cap_hi) / IMSM_4K_DIV),
1902 &migr_rec->post_migr_vol_cap, &migr_rec->post_migr_vol_cap_hi);
1903 set_migr_chkp_area_pba(migr_rec,
1904 migr_chkp_area_pba(migr_rec) / IMSM_4K_DIV);
1905 set_migr_dest_1st_member_lba(migr_rec,
1906 migr_dest_1st_member_lba(migr_rec) / IMSM_4K_DIV);
1907 }
1908
1909 void convert_to_4k_imsm_disk(struct imsm_disk *disk)
1910 {
1911 set_total_blocks(disk, (total_blocks(disk)/IMSM_4K_DIV));
1912 }
1913
1914 void convert_to_4k(struct intel_super *super)
1915 {
1916 struct imsm_super *mpb = super->anchor;
1917 struct imsm_disk *disk;
1918 int i;
1919 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
1920
1921 for (i = 0; i < mpb->num_disks ; i++) {
1922 disk = __get_imsm_disk(mpb, i);
1923 /* disk */
1924 convert_to_4k_imsm_disk(disk);
1925 }
1926 for (i = 0; i < mpb->num_raid_devs; i++) {
1927 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1928 struct imsm_map *map = get_imsm_map(dev, MAP_0);
1929 /* dev */
1930 set_imsm_dev_size(dev, imsm_dev_size(dev)/IMSM_4K_DIV);
1931 set_vol_curr_migr_unit(dev,
1932 vol_curr_migr_unit(dev) / IMSM_4K_DIV);
1933
1934 /* map0 */
1935 set_blocks_per_member(map, blocks_per_member(map)/IMSM_4K_DIV);
1936 map->blocks_per_strip /= IMSM_4K_DIV;
1937 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1938
1939 if (dev->vol.migr_state) {
1940 /* map1 */
1941 map = get_imsm_map(dev, MAP_1);
1942 set_blocks_per_member(map,
1943 blocks_per_member(map)/IMSM_4K_DIV);
1944 map->blocks_per_strip /= IMSM_4K_DIV;
1945 set_pba_of_lba0(map, pba_of_lba0(map)/IMSM_4K_DIV);
1946 }
1947 }
1948 if (bbm_log_size) {
1949 struct bbm_log *log = (void *)mpb +
1950 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
1951 __u32 i;
1952
1953 for (i = 0; i < log->entry_count; i++) {
1954 struct bbm_log_entry *entry =
1955 &log->marked_block_entries[i];
1956
1957 __u8 count = entry->marked_count + 1;
1958 unsigned long long sector =
1959 __le48_to_cpu(&entry->defective_block_start);
1960
1961 entry->defective_block_start =
1962 __cpu_to_le48(sector/IMSM_4K_DIV);
1963 entry->marked_count = max(count/IMSM_4K_DIV, 1) - 1;
1964 }
1965 }
1966
1967 mpb->check_sum = __gen_imsm_checksum(mpb);
1968 }
1969
1970 void examine_migr_rec_imsm(struct intel_super *super)
1971 {
1972 struct migr_record *migr_rec = super->migr_rec;
1973 struct imsm_super *mpb = super->anchor;
1974 int i;
1975
1976 for (i = 0; i < mpb->num_raid_devs; i++) {
1977 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1978 struct imsm_map *map;
1979 int slot = -1;
1980
1981 if (is_gen_migration(dev) == false)
1982 continue;
1983
1984 printf("\nMigration Record Information:");
1985
1986 /* first map under migration */
1987 map = get_imsm_map(dev, MAP_0);
1988
1989 if (map)
1990 slot = get_imsm_disk_slot(map, super->disks->index);
1991 if (map == NULL || slot > 1 || slot < 0) {
1992 printf(" Empty\n ");
1993 printf("Examine one of first two disks in array\n");
1994 break;
1995 }
1996 printf("\n Status : ");
1997 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
1998 printf("Normal\n");
1999 else
2000 printf("Contains Data\n");
2001 printf(" Current Unit : %llu\n",
2002 current_migr_unit(migr_rec));
2003 printf(" Family : %u\n",
2004 __le32_to_cpu(migr_rec->family_num));
2005 printf(" Ascending : %u\n",
2006 __le32_to_cpu(migr_rec->ascending_migr));
2007 printf(" Blocks Per Unit : %u\n",
2008 __le32_to_cpu(migr_rec->blocks_per_unit));
2009 printf(" Dest. Depth Per Unit : %u\n",
2010 __le32_to_cpu(migr_rec->dest_depth_per_unit));
2011 printf(" Checkpoint Area pba : %llu\n",
2012 migr_chkp_area_pba(migr_rec));
2013 printf(" First member lba : %llu\n",
2014 migr_dest_1st_member_lba(migr_rec));
2015 printf(" Total Number of Units : %llu\n",
2016 get_num_migr_units(migr_rec));
2017 printf(" Size of volume : %llu\n",
2018 join_u32(migr_rec->post_migr_vol_cap,
2019 migr_rec->post_migr_vol_cap_hi));
2020 printf(" Record was read from : %u\n",
2021 __le32_to_cpu(migr_rec->ckpt_read_disk_num));
2022
2023 break;
2024 }
2025 }
2026
2027 void convert_from_4k_imsm_migr_rec(struct intel_super *super)
2028 {
2029 struct migr_record *migr_rec = super->migr_rec;
2030
2031 migr_rec->blocks_per_unit *= IMSM_4K_DIV;
2032 migr_rec->dest_depth_per_unit *= IMSM_4K_DIV;
2033 split_ull((join_u32(migr_rec->post_migr_vol_cap,
2034 migr_rec->post_migr_vol_cap_hi) * IMSM_4K_DIV),
2035 &migr_rec->post_migr_vol_cap,
2036 &migr_rec->post_migr_vol_cap_hi);
2037 set_migr_chkp_area_pba(migr_rec,
2038 migr_chkp_area_pba(migr_rec) * IMSM_4K_DIV);
2039 set_migr_dest_1st_member_lba(migr_rec,
2040 migr_dest_1st_member_lba(migr_rec) * IMSM_4K_DIV);
2041 }
2042
2043 void convert_from_4k(struct intel_super *super)
2044 {
2045 struct imsm_super *mpb = super->anchor;
2046 struct imsm_disk *disk;
2047 int i;
2048 __u32 bbm_log_size = __le32_to_cpu(mpb->bbm_log_size);
2049
2050 for (i = 0; i < mpb->num_disks ; i++) {
2051 disk = __get_imsm_disk(mpb, i);
2052 /* disk */
2053 set_total_blocks(disk, (total_blocks(disk)*IMSM_4K_DIV));
2054 }
2055
2056 for (i = 0; i < mpb->num_raid_devs; i++) {
2057 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2058 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2059 /* dev */
2060 set_imsm_dev_size(dev, imsm_dev_size(dev)*IMSM_4K_DIV);
2061 set_vol_curr_migr_unit(dev,
2062 vol_curr_migr_unit(dev) * IMSM_4K_DIV);
2063
2064 /* map0 */
2065 set_blocks_per_member(map, blocks_per_member(map)*IMSM_4K_DIV);
2066 map->blocks_per_strip *= IMSM_4K_DIV;
2067 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2068
2069 if (dev->vol.migr_state) {
2070 /* map1 */
2071 map = get_imsm_map(dev, MAP_1);
2072 set_blocks_per_member(map,
2073 blocks_per_member(map)*IMSM_4K_DIV);
2074 map->blocks_per_strip *= IMSM_4K_DIV;
2075 set_pba_of_lba0(map, pba_of_lba0(map)*IMSM_4K_DIV);
2076 }
2077 }
2078 if (bbm_log_size) {
2079 struct bbm_log *log = (void *)mpb +
2080 __le32_to_cpu(mpb->mpb_size) - bbm_log_size;
2081 __u32 i;
2082
2083 for (i = 0; i < log->entry_count; i++) {
2084 struct bbm_log_entry *entry =
2085 &log->marked_block_entries[i];
2086
2087 __u8 count = entry->marked_count + 1;
2088 unsigned long long sector =
2089 __le48_to_cpu(&entry->defective_block_start);
2090
2091 entry->defective_block_start =
2092 __cpu_to_le48(sector*IMSM_4K_DIV);
2093 entry->marked_count = count*IMSM_4K_DIV - 1;
2094 }
2095 }
2096
2097 mpb->check_sum = __gen_imsm_checksum(mpb);
2098 }
2099
2100 /*******************************************************************************
2101 * function: imsm_check_attributes
2102 * Description: Function checks if features represented by attributes flags
2103 * are supported by mdadm.
2104 * Parameters:
2105 * attributes - Attributes read from metadata
2106 * Returns:
2107 * 0 - passed attributes contains unsupported features flags
2108 * 1 - all features are supported
2109 ******************************************************************************/
2110 static int imsm_check_attributes(__u32 attributes)
2111 {
2112 int ret_val = 1;
2113 __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
2114
2115 not_supported &= ~MPB_ATTRIB_IGNORED;
2116
2117 not_supported &= attributes;
2118 if (not_supported) {
2119 pr_err("(IMSM): Unsupported attributes : %x\n",
2120 (unsigned)__le32_to_cpu(not_supported));
2121 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2122 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
2123 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2124 }
2125 if (not_supported & MPB_ATTRIB_2TB) {
2126 dprintf("\t\tMPB_ATTRIB_2TB\n");
2127 not_supported ^= MPB_ATTRIB_2TB;
2128 }
2129 if (not_supported & MPB_ATTRIB_RAID0) {
2130 dprintf("\t\tMPB_ATTRIB_RAID0\n");
2131 not_supported ^= MPB_ATTRIB_RAID0;
2132 }
2133 if (not_supported & MPB_ATTRIB_RAID1) {
2134 dprintf("\t\tMPB_ATTRIB_RAID1\n");
2135 not_supported ^= MPB_ATTRIB_RAID1;
2136 }
2137 if (not_supported & MPB_ATTRIB_RAID10) {
2138 dprintf("\t\tMPB_ATTRIB_RAID10\n");
2139 not_supported ^= MPB_ATTRIB_RAID10;
2140 }
2141 if (not_supported & MPB_ATTRIB_RAID1E) {
2142 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
2143 not_supported ^= MPB_ATTRIB_RAID1E;
2144 }
2145 if (not_supported & MPB_ATTRIB_RAID5) {
2146 dprintf("\t\tMPB_ATTRIB_RAID5\n");
2147 not_supported ^= MPB_ATTRIB_RAID5;
2148 }
2149 if (not_supported & MPB_ATTRIB_RAIDCNG) {
2150 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
2151 not_supported ^= MPB_ATTRIB_RAIDCNG;
2152 }
2153 if (not_supported & MPB_ATTRIB_BBM) {
2154 dprintf("\t\tMPB_ATTRIB_BBM\n");
2155 not_supported ^= MPB_ATTRIB_BBM;
2156 }
2157 if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
2158 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
2159 not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
2160 }
2161 if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
2162 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
2163 not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
2164 }
2165 if (not_supported & MPB_ATTRIB_2TB_DISK) {
2166 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
2167 not_supported ^= MPB_ATTRIB_2TB_DISK;
2168 }
2169 if (not_supported & MPB_ATTRIB_NEVER_USE2) {
2170 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
2171 not_supported ^= MPB_ATTRIB_NEVER_USE2;
2172 }
2173 if (not_supported & MPB_ATTRIB_NEVER_USE) {
2174 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
2175 not_supported ^= MPB_ATTRIB_NEVER_USE;
2176 }
2177
2178 if (not_supported)
2179 dprintf("(IMSM): Unknown attributes : %x\n", not_supported);
2180
2181 ret_val = 0;
2182 }
2183
2184 return ret_val;
2185 }
2186
2187 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
2188
2189 static void examine_super_imsm(struct supertype *st, char *homehost)
2190 {
2191 struct intel_super *super = st->sb;
2192 struct imsm_super *mpb = super->anchor;
2193 char str[MAX_SIGNATURE_LENGTH];
2194 int i;
2195 struct mdinfo info;
2196 char nbuf[64];
2197 __u32 sum;
2198 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2199 struct dl *dl;
2200 time_t creation_time;
2201
2202 strncpy(str, (char *)mpb->sig, MPB_SIG_LEN);
2203 str[MPB_SIG_LEN-1] = '\0';
2204 printf(" Magic : %s\n", str);
2205 printf(" Version : %s\n", get_imsm_version(mpb));
2206 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
2207 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
2208 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
2209 creation_time = __le64_to_cpu(mpb->creation_time);
2210 printf(" Creation Time : %.24s\n",
2211 creation_time ? ctime(&creation_time) : "Unknown");
2212 printf(" Attributes : ");
2213 if (imsm_check_attributes(mpb->attributes))
2214 printf("All supported\n");
2215 else
2216 printf("not supported\n");
2217 getinfo_super_imsm(st, &info, NULL);
2218 fname_from_uuid(&info, nbuf);
2219 printf(" UUID : %s\n", nbuf + 5);
2220 sum = __le32_to_cpu(mpb->check_sum);
2221 printf(" Checksum : %08x %s\n", sum,
2222 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
2223 printf(" MPB Sectors : %d\n", mpb_sectors(mpb, super->sector_size));
2224 printf(" Disks : %d\n", mpb->num_disks);
2225 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
2226 print_imsm_disk(__get_imsm_disk(mpb, super->disks->index),
2227 super->disks->index, reserved, super->sector_size);
2228 if (get_imsm_bbm_log_size(super->bbm_log)) {
2229 struct bbm_log *log = super->bbm_log;
2230
2231 printf("\n");
2232 printf("Bad Block Management Log:\n");
2233 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
2234 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
2235 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
2236 }
2237 for (i = 0; i < mpb->num_raid_devs; i++) {
2238 struct mdinfo info;
2239 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2240
2241 super->current_vol = i;
2242 getinfo_super_imsm(st, &info, NULL);
2243 fname_from_uuid(&info, nbuf);
2244 print_imsm_dev(super, dev, nbuf + 5, super->disks->index);
2245 }
2246 for (i = 0; i < mpb->num_disks; i++) {
2247 if (i == super->disks->index)
2248 continue;
2249 print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved,
2250 super->sector_size);
2251 }
2252
2253 for (dl = super->disks; dl; dl = dl->next)
2254 if (dl->index == -1)
2255 print_imsm_disk(&dl->disk, -1, reserved,
2256 super->sector_size);
2257
2258 examine_migr_rec_imsm(super);
2259 }
2260
2261 static void brief_examine_super_imsm(struct supertype *st, int verbose)
2262 {
2263 /* We just write a generic IMSM ARRAY entry */
2264 struct mdinfo info;
2265 char nbuf[64];
2266
2267 getinfo_super_imsm(st, &info, NULL);
2268 fname_from_uuid(&info, nbuf);
2269 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
2270 }
2271
2272 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
2273 {
2274 /* We just write a generic IMSM ARRAY entry */
2275 struct mdinfo info;
2276 char nbuf[64];
2277 char nbuf1[64];
2278 struct intel_super *super = st->sb;
2279 int i;
2280
2281 if (!super->anchor->num_raid_devs)
2282 return;
2283
2284 getinfo_super_imsm(st, &info, NULL);
2285 fname_from_uuid(&info, nbuf);
2286 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2287 struct imsm_dev *dev = get_imsm_dev(super, i);
2288
2289 super->current_vol = i;
2290 getinfo_super_imsm(st, &info, NULL);
2291 fname_from_uuid(&info, nbuf1);
2292 printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
2293 dev->volume, nbuf + 5, i, nbuf1 + 5);
2294 }
2295 }
2296
2297 static void export_examine_super_imsm(struct supertype *st)
2298 {
2299 struct intel_super *super = st->sb;
2300 struct imsm_super *mpb = super->anchor;
2301 struct mdinfo info;
2302 char nbuf[64];
2303
2304 getinfo_super_imsm(st, &info, NULL);
2305 fname_from_uuid(&info, nbuf);
2306 printf("MD_METADATA=imsm\n");
2307 printf("MD_LEVEL=container\n");
2308 printf("MD_UUID=%s\n", nbuf+5);
2309 printf("MD_DEVICES=%u\n", mpb->num_disks);
2310 printf("MD_CREATION_TIME=%llu\n", __le64_to_cpu(mpb->creation_time));
2311 }
2312
2313 static void detail_super_imsm(struct supertype *st, char *homehost,
2314 char *subarray)
2315 {
2316 struct mdinfo info;
2317 char nbuf[64];
2318 struct intel_super *super = st->sb;
2319 int temp_vol = super->current_vol;
2320
2321 if (subarray)
2322 super->current_vol = strtoul(subarray, NULL, 10);
2323
2324 getinfo_super_imsm(st, &info, NULL);
2325 fname_from_uuid(&info, nbuf);
2326 printf("\n UUID : %s\n", nbuf + 5);
2327
2328 super->current_vol = temp_vol;
2329 }
2330
2331 static void brief_detail_super_imsm(struct supertype *st, char *subarray)
2332 {
2333 struct mdinfo info;
2334 char nbuf[64];
2335 struct intel_super *super = st->sb;
2336 int temp_vol = super->current_vol;
2337
2338 if (subarray)
2339 super->current_vol = strtoul(subarray, NULL, 10);
2340
2341 getinfo_super_imsm(st, &info, NULL);
2342 fname_from_uuid(&info, nbuf);
2343 printf(" UUID=%s", nbuf + 5);
2344
2345 super->current_vol = temp_vol;
2346 }
2347
2348 static int imsm_read_serial(int fd, char *devname, __u8 *serial,
2349 size_t serial_buf_len);
2350 static void fd2devname(int fd, char *name);
2351
2352 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
2353 {
2354 /* dump an unsorted list of devices attached to AHCI Intel storage
2355 * controller, as well as non-connected ports
2356 */
2357 int hba_len = strlen(hba_path) + 1;
2358 struct dirent *ent;
2359 DIR *dir;
2360 char *path = NULL;
2361 int err = 0;
2362 unsigned long port_mask = (1 << port_count) - 1;
2363
2364 if (port_count > (int)sizeof(port_mask) * 8) {
2365 if (verbose > 0)
2366 pr_err("port_count %d out of range\n", port_count);
2367 return 2;
2368 }
2369
2370 /* scroll through /sys/dev/block looking for devices attached to
2371 * this hba
2372 */
2373 dir = opendir("/sys/dev/block");
2374 if (!dir)
2375 return 1;
2376
2377 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2378 int fd;
2379 char model[64];
2380 char vendor[64];
2381 char buf[1024];
2382 int major, minor;
2383 char device[PATH_MAX];
2384 char *c;
2385 int port;
2386 int type;
2387
2388 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
2389 continue;
2390 path = devt_to_devpath(makedev(major, minor), 1, NULL);
2391 if (!path)
2392 continue;
2393 if (!path_attached_to_hba(path, hba_path)) {
2394 free(path);
2395 path = NULL;
2396 continue;
2397 }
2398
2399 /* retrieve the scsi device */
2400 if (!devt_to_devpath(makedev(major, minor), 1, device)) {
2401 if (verbose > 0)
2402 pr_err("failed to get device\n");
2403 err = 2;
2404 break;
2405 }
2406 if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
2407 err = 2;
2408 break;
2409 }
2410 type = strtoul(buf, NULL, 10);
2411
2412 /* if it's not a disk print the vendor and model */
2413 if (!(type == 0 || type == 7 || type == 14)) {
2414 vendor[0] = '\0';
2415 model[0] = '\0';
2416
2417 if (devpath_to_char(device, "vendor", buf,
2418 sizeof(buf), 0) == 0) {
2419 strncpy(vendor, buf, sizeof(vendor));
2420 vendor[sizeof(vendor) - 1] = '\0';
2421 c = (char *) &vendor[sizeof(vendor) - 1];
2422 while (isspace(*c) || *c == '\0')
2423 *c-- = '\0';
2424
2425 }
2426
2427 if (devpath_to_char(device, "model", buf,
2428 sizeof(buf), 0) == 0) {
2429 strncpy(model, buf, sizeof(model));
2430 model[sizeof(model) - 1] = '\0';
2431 c = (char *) &model[sizeof(model) - 1];
2432 while (isspace(*c) || *c == '\0')
2433 *c-- = '\0';
2434 }
2435
2436 if (vendor[0] && model[0])
2437 sprintf(buf, "%.64s %.64s", vendor, model);
2438 else
2439 switch (type) { /* numbers from hald/linux/device.c */
2440 case 1: sprintf(buf, "tape"); break;
2441 case 2: sprintf(buf, "printer"); break;
2442 case 3: sprintf(buf, "processor"); break;
2443 case 4:
2444 case 5: sprintf(buf, "cdrom"); break;
2445 case 6: sprintf(buf, "scanner"); break;
2446 case 8: sprintf(buf, "media_changer"); break;
2447 case 9: sprintf(buf, "comm"); break;
2448 case 12: sprintf(buf, "raid"); break;
2449 default: sprintf(buf, "unknown");
2450 }
2451 } else
2452 buf[0] = '\0';
2453
2454 /* chop device path to 'host%d' and calculate the port number */
2455 c = strchr(&path[hba_len], '/');
2456 if (!c) {
2457 if (verbose > 0)
2458 pr_err("%s - invalid path name\n", path + hba_len);
2459 err = 2;
2460 break;
2461 }
2462 *c = '\0';
2463 if ((sscanf(&path[hba_len], "ata%d", &port) == 1) ||
2464 ((sscanf(&path[hba_len], "host%d", &port) == 1)))
2465 port -= host_base;
2466 else {
2467 if (verbose > 0) {
2468 *c = '/'; /* repair the full string */
2469 pr_err("failed to determine port number for %s\n",
2470 path);
2471 }
2472 err = 2;
2473 break;
2474 }
2475
2476 /* mark this port as used */
2477 port_mask &= ~(1 << port);
2478
2479 /* print out the device information */
2480 if (buf[0]) {
2481 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
2482 continue;
2483 }
2484
2485 fd = dev_open(ent->d_name, O_RDONLY);
2486 if (!is_fd_valid(fd))
2487 printf(" Port%d : - disk info unavailable -\n", port);
2488 else {
2489 fd2devname(fd, buf);
2490 printf(" Port%d : %s", port, buf);
2491 if (imsm_read_serial(fd, NULL, (__u8 *)buf,
2492 sizeof(buf)) == 0)
2493 printf(" (%s)\n", buf);
2494 else
2495 printf(" ()\n");
2496 close(fd);
2497 }
2498 free(path);
2499 path = NULL;
2500 }
2501 if (path)
2502 free(path);
2503 if (dir)
2504 closedir(dir);
2505 if (err == 0) {
2506 int i;
2507
2508 for (i = 0; i < port_count; i++)
2509 if (port_mask & (1 << i))
2510 printf(" Port%d : - no device attached -\n", i);
2511 }
2512
2513 return err;
2514 }
2515
2516 static int print_nvme_info(struct sys_dev *hba)
2517 {
2518 struct dirent *ent;
2519 DIR *dir;
2520
2521 dir = opendir("/sys/block/");
2522 if (!dir)
2523 return 1;
2524
2525 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2526 char ns_path[PATH_MAX];
2527 char cntrl_path[PATH_MAX];
2528 char buf[PATH_MAX];
2529 int fd = -1;
2530
2531 if (!strstr(ent->d_name, "nvme"))
2532 goto skip;
2533
2534 fd = open_dev(ent->d_name);
2535 if (!is_fd_valid(fd))
2536 goto skip;
2537
2538 if (!diskfd_to_devpath(fd, 0, ns_path) ||
2539 !diskfd_to_devpath(fd, 1, cntrl_path))
2540 goto skip;
2541
2542 if (!path_attached_to_hba(cntrl_path, hba->path))
2543 goto skip;
2544
2545 if (!imsm_is_nvme_namespace_supported(fd, 0))
2546 goto skip;
2547
2548 fd2devname(fd, buf);
2549 if (hba->type == SYS_DEV_VMD)
2550 printf(" NVMe under VMD : %s", buf);
2551 else if (hba->type == SYS_DEV_NVME)
2552 printf(" NVMe Device : %s", buf);
2553
2554 if (!imsm_read_serial(fd, NULL, (__u8 *)buf,
2555 sizeof(buf)))
2556 printf(" (%s)\n", buf);
2557 else
2558 printf("()\n");
2559
2560 skip:
2561 close_fd(&fd);
2562 }
2563
2564 closedir(dir);
2565 return 0;
2566 }
2567
2568 static void print_found_intel_controllers(struct sys_dev *elem)
2569 {
2570 for (; elem; elem = elem->next) {
2571 pr_err("found Intel(R) ");
2572 if (elem->type == SYS_DEV_SATA)
2573 fprintf(stderr, "SATA ");
2574 else if (elem->type == SYS_DEV_SAS)
2575 fprintf(stderr, "SAS ");
2576 else if (elem->type == SYS_DEV_NVME)
2577 fprintf(stderr, "NVMe ");
2578
2579 if (elem->type == SYS_DEV_VMD)
2580 fprintf(stderr, "VMD domain");
2581 else if (elem->type == SYS_DEV_SATA_VMD)
2582 fprintf(stderr, "SATA VMD domain");
2583 else
2584 fprintf(stderr, "RAID controller");
2585
2586 if (elem->pci_id)
2587 fprintf(stderr, " at %s", elem->pci_id);
2588 fprintf(stderr, ".\n");
2589 }
2590 fflush(stderr);
2591 }
2592
2593 static int ahci_get_port_count(const char *hba_path, int *port_count)
2594 {
2595 struct dirent *ent;
2596 DIR *dir;
2597 int host_base = -1;
2598
2599 *port_count = 0;
2600 if ((dir = opendir(hba_path)) == NULL)
2601 return -1;
2602
2603 for (ent = readdir(dir); ent; ent = readdir(dir)) {
2604 int host;
2605
2606 if ((sscanf(ent->d_name, "ata%d", &host) != 1) &&
2607 ((sscanf(ent->d_name, "host%d", &host) != 1)))
2608 continue;
2609 if (*port_count == 0)
2610 host_base = host;
2611 else if (host < host_base)
2612 host_base = host;
2613
2614 if (host + 1 > *port_count + host_base)
2615 *port_count = host + 1 - host_base;
2616 }
2617 closedir(dir);
2618 return host_base;
2619 }
2620
2621 static void print_imsm_capability(const struct imsm_orom *orom)
2622 {
2623 printf(" Platform : Intel(R) ");
2624 if (orom->capabilities == 0 && orom->driver_features == 0)
2625 printf("Matrix Storage Manager\n");
2626 else if (imsm_orom_is_enterprise(orom) && orom->major_ver >= 6)
2627 printf("Virtual RAID on CPU\n");
2628 else
2629 printf("Rapid Storage Technology%s\n",
2630 imsm_orom_is_enterprise(orom) ? " enterprise" : "");
2631 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build) {
2632 if (imsm_orom_is_vmd_without_efi(orom))
2633 printf(" Version : %d.%d\n", orom->major_ver,
2634 orom->minor_ver);
2635 else
2636 printf(" Version : %d.%d.%d.%d\n", orom->major_ver,
2637 orom->minor_ver, orom->hotfix_ver, orom->build);
2638 }
2639 printf(" RAID Levels :%s%s%s%s%s\n",
2640 imsm_orom_has_raid0(orom) ? " raid0" : "",
2641 imsm_orom_has_raid1(orom) ? " raid1" : "",
2642 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
2643 imsm_orom_has_raid10(orom) ? " raid10" : "",
2644 imsm_orom_has_raid5(orom) ? " raid5" : "");
2645 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2646 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
2647 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
2648 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
2649 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
2650 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
2651 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
2652 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
2653 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
2654 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
2655 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
2656 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
2657 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
2658 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
2659 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
2660 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
2661 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
2662 printf(" 2TB volumes :%s supported\n",
2663 (orom->attr & IMSM_OROM_ATTR_2TB)?"":" not");
2664 printf(" 2TB disks :%s supported\n",
2665 (orom->attr & IMSM_OROM_ATTR_2TB_DISK)?"":" not");
2666 printf(" Max Disks : %d\n", orom->tds);
2667 printf(" Max Volumes : %d per array, %d per %s\n",
2668 orom->vpa, orom->vphba,
2669 imsm_orom_is_nvme(orom) ? "platform" : "controller");
2670 return;
2671 }
2672
2673 static void print_imsm_capability_export(const struct imsm_orom *orom)
2674 {
2675 printf("MD_FIRMWARE_TYPE=imsm\n");
2676 if (orom->major_ver || orom->minor_ver || orom->hotfix_ver || orom->build)
2677 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
2678 orom->hotfix_ver, orom->build);
2679 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2680 imsm_orom_has_raid0(orom) ? "raid0 " : "",
2681 imsm_orom_has_raid1(orom) ? "raid1 " : "",
2682 imsm_orom_has_raid1e(orom) ? "raid1e " : "",
2683 imsm_orom_has_raid5(orom) ? "raid10 " : "",
2684 imsm_orom_has_raid10(orom) ? "raid5 " : "");
2685 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2686 imsm_orom_has_chunk(orom, 2) ? "2k " : "",
2687 imsm_orom_has_chunk(orom, 4) ? "4k " : "",
2688 imsm_orom_has_chunk(orom, 8) ? "8k " : "",
2689 imsm_orom_has_chunk(orom, 16) ? "16k " : "",
2690 imsm_orom_has_chunk(orom, 32) ? "32k " : "",
2691 imsm_orom_has_chunk(orom, 64) ? "64k " : "",
2692 imsm_orom_has_chunk(orom, 128) ? "128k " : "",
2693 imsm_orom_has_chunk(orom, 256) ? "256k " : "",
2694 imsm_orom_has_chunk(orom, 512) ? "512k " : "",
2695 imsm_orom_has_chunk(orom, 1024*1) ? "1M " : "",
2696 imsm_orom_has_chunk(orom, 1024*2) ? "2M " : "",
2697 imsm_orom_has_chunk(orom, 1024*4) ? "4M " : "",
2698 imsm_orom_has_chunk(orom, 1024*8) ? "8M " : "",
2699 imsm_orom_has_chunk(orom, 1024*16) ? "16M " : "",
2700 imsm_orom_has_chunk(orom, 1024*32) ? "32M " : "",
2701 imsm_orom_has_chunk(orom, 1024*64) ? "64M " : "");
2702 printf("IMSM_2TB_VOLUMES=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB) ? "yes" : "no");
2703 printf("IMSM_2TB_DISKS=%s\n",(orom->attr & IMSM_OROM_ATTR_2TB_DISK) ? "yes" : "no");
2704 printf("IMSM_MAX_DISKS=%d\n",orom->tds);
2705 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom->vpa);
2706 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom->vphba);
2707 }
2708
2709 static int detail_platform_imsm(int verbose, int enumerate_only, char *controller_path)
2710 {
2711 /* There are two components to imsm platform support, the ahci SATA
2712 * controller and the option-rom. To find the SATA controller we
2713 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2714 * controller with the Intel vendor id is present. This approach
2715 * allows mdadm to leverage the kernel's ahci detection logic, with the
2716 * caveat that if ahci.ko is not loaded mdadm will not be able to
2717 * detect platform raid capabilities. The option-rom resides in a
2718 * platform "Adapter ROM". We scan for its signature to retrieve the
2719 * platform capabilities. If raid support is disabled in the BIOS the
2720 * option-rom capability structure will not be available.
2721 */
2722 struct sys_dev *list, *hba;
2723 int host_base = 0;
2724 int port_count = 0;
2725 int result=1;
2726
2727 if (enumerate_only) {
2728 if (check_no_platform())
2729 return 0;
2730 list = find_intel_devices();
2731 if (!list)
2732 return 2;
2733 for (hba = list; hba; hba = hba->next) {
2734 if (find_imsm_capability(hba)) {
2735 result = 0;
2736 break;
2737 }
2738 else
2739 result = 2;
2740 }
2741 return result;
2742 }
2743
2744 list = find_intel_devices();
2745 if (!list) {
2746 if (verbose > 0)
2747 pr_err("no active Intel(R) RAID controller found.\n");
2748 return 2;
2749 } else if (verbose > 0)
2750 print_found_intel_controllers(list);
2751
2752 for (hba = list; hba; hba = hba->next) {
2753 if (controller_path && (compare_paths(hba->path, controller_path) != 0))
2754 continue;
2755 if (!find_imsm_capability(hba)) {
2756 char buf[PATH_MAX];
2757 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2758 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2759 vmd_domain_to_controller(hba, buf) :
2760 hba->path, get_sys_dev_type(hba->type));
2761 continue;
2762 }
2763 result = 0;
2764 }
2765
2766 if (controller_path && result == 1) {
2767 pr_err("no active Intel(R) RAID controller found under %s\n",
2768 controller_path);
2769 return result;
2770 }
2771
2772 const struct orom_entry *entry;
2773
2774 for (entry = orom_entries; entry; entry = entry->next) {
2775 if (entry->type == SYS_DEV_VMD) {
2776 print_imsm_capability(&entry->orom);
2777 printf(" 3rd party NVMe :%s supported\n",
2778 imsm_orom_has_tpv_support(&entry->orom)?"":" not");
2779 for (hba = list; hba; hba = hba->next) {
2780 if (hba->type == SYS_DEV_VMD) {
2781 char buf[PATH_MAX];
2782 printf(" I/O Controller : %s (%s)\n",
2783 vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
2784 if (print_nvme_info(hba)) {
2785 if (verbose > 0)
2786 pr_err("failed to get devices attached to VMD domain.\n");
2787 result |= 2;
2788 }
2789 }
2790 }
2791 printf("\n");
2792 continue;
2793 }
2794
2795 print_imsm_capability(&entry->orom);
2796 if (entry->type == SYS_DEV_NVME) {
2797 for (hba = list; hba; hba = hba->next) {
2798 if (hba->type == SYS_DEV_NVME)
2799 print_nvme_info(hba);
2800 }
2801 printf("\n");
2802 continue;
2803 }
2804
2805 struct devid_list *devid;
2806 for (devid = entry->devid_list; devid; devid = devid->next) {
2807 hba = device_by_id(devid->devid);
2808 if (!hba)
2809 continue;
2810
2811 printf(" I/O Controller : %s (%s)\n",
2812 hba->path, get_sys_dev_type(hba->type));
2813 if (hba->type == SYS_DEV_SATA || hba->type == SYS_DEV_SATA_VMD) {
2814 host_base = ahci_get_port_count(hba->path, &port_count);
2815 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
2816 if (verbose > 0)
2817 pr_err("failed to enumerate ports on %s controller at %s.\n",
2818 get_sys_dev_type(hba->type), hba->pci_id);
2819 result |= 2;
2820 }
2821 }
2822 }
2823 printf("\n");
2824 }
2825
2826 return result;
2827 }
2828
2829 static int export_detail_platform_imsm(int verbose, char *controller_path)
2830 {
2831 struct sys_dev *list, *hba;
2832 int result=1;
2833
2834 list = find_intel_devices();
2835 if (!list) {
2836 if (verbose > 0)
2837 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2838 result = 2;
2839 return result;
2840 }
2841
2842 for (hba = list; hba; hba = hba->next) {
2843 if (controller_path && (compare_paths(hba->path,controller_path) != 0))
2844 continue;
2845 if (!find_imsm_capability(hba) && verbose > 0) {
2846 char buf[PATH_MAX];
2847 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2848 hba->type == SYS_DEV_VMD || hba->type == SYS_DEV_SATA_VMD ?
2849 vmd_domain_to_controller(hba, buf) : hba->path);
2850 }
2851 else
2852 result = 0;
2853 }
2854
2855 const struct orom_entry *entry;
2856
2857 for (entry = orom_entries; entry; entry = entry->next) {
2858 if (entry->type == SYS_DEV_VMD || entry->type == SYS_DEV_SATA_VMD) {
2859 for (hba = list; hba; hba = hba->next)
2860 print_imsm_capability_export(&entry->orom);
2861 continue;
2862 }
2863 print_imsm_capability_export(&entry->orom);
2864 }
2865
2866 return result;
2867 }
2868
2869 static int match_home_imsm(struct supertype *st, char *homehost)
2870 {
2871 /* the imsm metadata format does not specify any host
2872 * identification information. We return -1 since we can never
2873 * confirm nor deny whether a given array is "meant" for this
2874 * host. We rely on compare_super and the 'family_num' fields to
2875 * exclude member disks that do not belong, and we rely on
2876 * mdadm.conf to specify the arrays that should be assembled.
2877 * Auto-assembly may still pick up "foreign" arrays.
2878 */
2879
2880 return -1;
2881 }
2882
2883 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
2884 {
2885 /* The uuid returned here is used for:
2886 * uuid to put into bitmap file (Create, Grow)
2887 * uuid for backup header when saving critical section (Grow)
2888 * comparing uuids when re-adding a device into an array
2889 * In these cases the uuid required is that of the data-array,
2890 * not the device-set.
2891 * uuid to recognise same set when adding a missing device back
2892 * to an array. This is a uuid for the device-set.
2893 *
2894 * For each of these we can make do with a truncated
2895 * or hashed uuid rather than the original, as long as
2896 * everyone agrees.
2897 * In each case the uuid required is that of the data-array,
2898 * not the device-set.
2899 */
2900 /* imsm does not track uuid's so we synthesis one using sha1 on
2901 * - The signature (Which is constant for all imsm array, but no matter)
2902 * - the orig_family_num of the container
2903 * - the index number of the volume
2904 * - the 'serial' number of the volume.
2905 * Hopefully these are all constant.
2906 */
2907 struct intel_super *super = st->sb;
2908
2909 char buf[20];
2910 struct sha1_ctx ctx;
2911 struct imsm_dev *dev = NULL;
2912 __u32 family_num;
2913
2914 /* some mdadm versions failed to set ->orig_family_num, in which
2915 * case fall back to ->family_num. orig_family_num will be
2916 * fixed up with the first metadata update.
2917 */
2918 family_num = super->anchor->orig_family_num;
2919 if (family_num == 0)
2920 family_num = super->anchor->family_num;
2921 sha1_init_ctx(&ctx);
2922 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
2923 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
2924 if (super->current_vol >= 0)
2925 dev = get_imsm_dev(super, super->current_vol);
2926 if (dev) {
2927 __u32 vol = super->current_vol;
2928 sha1_process_bytes(&vol, sizeof(vol), &ctx);
2929 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
2930 }
2931 sha1_finish_ctx(&ctx, buf);
2932 memcpy(uuid, buf, 4*4);
2933 }
2934
2935 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
2936 {
2937 /* migr_strip_size when repairing or initializing parity */
2938 struct imsm_map *map = get_imsm_map(dev, MAP_0);
2939 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2940
2941 switch (get_imsm_raid_level(map)) {
2942 case 5:
2943 case 10:
2944 return chunk;
2945 default:
2946 return 128*1024 >> 9;
2947 }
2948 }
2949
2950 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
2951 {
2952 /* migr_strip_size when rebuilding a degraded disk, no idea why
2953 * this is different than migr_strip_size_resync(), but it's good
2954 * to be compatible
2955 */
2956 struct imsm_map *map = get_imsm_map(dev, MAP_1);
2957 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
2958
2959 switch (get_imsm_raid_level(map)) {
2960 case 1:
2961 case 10:
2962 if (map->num_members % map->num_domains == 0)
2963 return 128*1024 >> 9;
2964 else
2965 return chunk;
2966 case 5:
2967 return max((__u32) 64*1024 >> 9, chunk);
2968 default:
2969 return 128*1024 >> 9;
2970 }
2971 }
2972
2973 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
2974 {
2975 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2976 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2977 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
2978 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
2979
2980 return max((__u32) 1, hi_chunk / lo_chunk);
2981 }
2982
2983 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
2984 {
2985 struct imsm_map *lo = get_imsm_map(dev, MAP_0);
2986 int level = get_imsm_raid_level(lo);
2987
2988 if (level == 1 || level == 10) {
2989 struct imsm_map *hi = get_imsm_map(dev, MAP_1);
2990
2991 return hi->num_domains;
2992 } else
2993 return num_stripes_per_unit_resync(dev);
2994 }
2995
2996 static unsigned long long calc_component_size(struct imsm_map *map,
2997 struct imsm_dev *dev)
2998 {
2999 unsigned long long component_size;
3000 unsigned long long dev_size = imsm_dev_size(dev);
3001 long long calc_dev_size = 0;
3002 unsigned int member_disks = imsm_num_data_members(map);
3003
3004 if (member_disks == 0)
3005 return 0;
3006
3007 component_size = per_dev_array_size(map);
3008 calc_dev_size = component_size * member_disks;
3009
3010 /* Component size is rounded to 1MB so difference between size from
3011 * metadata and size calculated from num_data_stripes equals up to
3012 * 2048 blocks per each device. If the difference is higher it means
3013 * that array size was expanded and num_data_stripes was not updated.
3014 */
3015 if (llabs(calc_dev_size - (long long)dev_size) >
3016 (1 << SECT_PER_MB_SHIFT) * member_disks) {
3017 component_size = dev_size / member_disks;
3018 dprintf("Invalid num_data_stripes in metadata; expected=%llu, found=%llu\n",
3019 component_size / map->blocks_per_strip,
3020 num_data_stripes(map));
3021 }
3022
3023 return component_size;
3024 }
3025
3026 static __u32 parity_segment_depth(struct imsm_dev *dev)
3027 {
3028 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3029 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3030
3031 switch(get_imsm_raid_level(map)) {
3032 case 1:
3033 case 10:
3034 return chunk * map->num_domains;
3035 case 5:
3036 return chunk * map->num_members;
3037 default:
3038 return chunk;
3039 }
3040 }
3041
3042 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
3043 {
3044 struct imsm_map *map = get_imsm_map(dev, MAP_1);
3045 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
3046 __u32 strip = block / chunk;
3047
3048 switch (get_imsm_raid_level(map)) {
3049 case 1:
3050 case 10: {
3051 __u32 vol_strip = (strip * map->num_domains) + 1;
3052 __u32 vol_stripe = vol_strip / map->num_members;
3053
3054 return vol_stripe * chunk + block % chunk;
3055 } case 5: {
3056 __u32 stripe = strip / (map->num_members - 1);
3057
3058 return stripe * chunk + block % chunk;
3059 }
3060 default:
3061 return 0;
3062 }
3063 }
3064
3065 static __u64 blocks_per_migr_unit(struct intel_super *super,
3066 struct imsm_dev *dev)
3067 {
3068 /* calculate the conversion factor between per member 'blocks'
3069 * (md/{resync,rebuild}_start) and imsm migration units, return
3070 * 0 for the 'not migrating' and 'unsupported migration' cases
3071 */
3072 if (!dev->vol.migr_state)
3073 return 0;
3074
3075 switch (migr_type(dev)) {
3076 case MIGR_GEN_MIGR: {
3077 struct migr_record *migr_rec = super->migr_rec;
3078 return __le32_to_cpu(migr_rec->blocks_per_unit);
3079 }
3080 case MIGR_VERIFY:
3081 case MIGR_REPAIR:
3082 case MIGR_INIT: {
3083 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3084 __u32 stripes_per_unit;
3085 __u32 blocks_per_unit;
3086 __u32 parity_depth;
3087 __u32 migr_chunk;
3088 __u32 block_map;
3089 __u32 block_rel;
3090 __u32 segment;
3091 __u32 stripe;
3092 __u8 disks;
3093
3094 /* yes, this is really the translation of migr_units to
3095 * per-member blocks in the 'resync' case
3096 */
3097 stripes_per_unit = num_stripes_per_unit_resync(dev);
3098 migr_chunk = migr_strip_blocks_resync(dev);
3099 disks = imsm_num_data_members(map);
3100 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
3101 stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
3102 segment = blocks_per_unit / stripe;
3103 block_rel = blocks_per_unit - segment * stripe;
3104 parity_depth = parity_segment_depth(dev);
3105 block_map = map_migr_block(dev, block_rel);
3106 return block_map + parity_depth * segment;
3107 }
3108 case MIGR_REBUILD: {
3109 __u32 stripes_per_unit;
3110 __u32 migr_chunk;
3111
3112 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
3113 migr_chunk = migr_strip_blocks_rebuild(dev);
3114 return migr_chunk * stripes_per_unit;
3115 }
3116 case MIGR_STATE_CHANGE:
3117 default:
3118 return 0;
3119 }
3120 }
3121
3122 static int imsm_level_to_layout(int level)
3123 {
3124 switch (level) {
3125 case 0:
3126 case 1:
3127 return 0;
3128 case 5:
3129 case 6:
3130 return ALGORITHM_LEFT_ASYMMETRIC;
3131 case 10:
3132 return 0x102;
3133 }
3134 return UnSet;
3135 }
3136
3137 /*******************************************************************************
3138 * Function: read_imsm_migr_rec
3139 * Description: Function reads imsm migration record from last sector of disk
3140 * Parameters:
3141 * fd : disk descriptor
3142 * super : metadata info
3143 * Returns:
3144 * 0 : success,
3145 * -1 : fail
3146 ******************************************************************************/
3147 static int read_imsm_migr_rec(int fd, struct intel_super *super)
3148 {
3149 int ret_val = -1;
3150 unsigned int sector_size = super->sector_size;
3151 unsigned long long dsize;
3152
3153 get_dev_size(fd, NULL, &dsize);
3154 if (lseek64(fd, dsize - (sector_size*MIGR_REC_SECTOR_POSITION),
3155 SEEK_SET) < 0) {
3156 pr_err("Cannot seek to anchor block: %s\n",
3157 strerror(errno));
3158 goto out;
3159 }
3160 if ((unsigned int)read(fd, super->migr_rec_buf,
3161 MIGR_REC_BUF_SECTORS*sector_size) !=
3162 MIGR_REC_BUF_SECTORS*sector_size) {
3163 pr_err("Cannot read migr record block: %s\n",
3164 strerror(errno));
3165 goto out;
3166 }
3167 ret_val = 0;
3168 if (sector_size == 4096)
3169 convert_from_4k_imsm_migr_rec(super);
3170
3171 out:
3172 return ret_val;
3173 }
3174
3175 static struct imsm_dev *imsm_get_device_during_migration(
3176 struct intel_super *super)
3177 {
3178
3179 struct intel_dev *dv;
3180
3181 for (dv = super->devlist; dv; dv = dv->next) {
3182 if (is_gen_migration(dv->dev))
3183 return dv->dev;
3184 }
3185 return NULL;
3186 }
3187
3188 /*******************************************************************************
3189 * Function: load_imsm_migr_rec
3190 * Description: Function reads imsm migration record (it is stored at the last
3191 * sector of disk)
3192 * Parameters:
3193 * super : imsm internal array info
3194 * Returns:
3195 * 0 : success
3196 * -1 : fail
3197 * -2 : no migration in progress
3198 ******************************************************************************/
3199 static int load_imsm_migr_rec(struct intel_super *super)
3200 {
3201 struct dl *dl;
3202 char nm[30];
3203 int retval = -1;
3204 int fd = -1;
3205 struct imsm_dev *dev;
3206 struct imsm_map *map;
3207 int slot = -1;
3208 int keep_fd = 1;
3209
3210 /* find map under migration */
3211 dev = imsm_get_device_during_migration(super);
3212 /* nothing to load,no migration in progress?
3213 */
3214 if (dev == NULL)
3215 return -2;
3216
3217 map = get_imsm_map(dev, MAP_0);
3218 if (!map)
3219 return -1;
3220
3221 for (dl = super->disks; dl; dl = dl->next) {
3222 /* skip spare and failed disks
3223 */
3224 if (dl->index < 0)
3225 continue;
3226 /* read only from one of the first two slots
3227 */
3228 slot = get_imsm_disk_slot(map, dl->index);
3229 if (slot > 1 || slot < 0)
3230 continue;
3231
3232 if (!is_fd_valid(dl->fd)) {
3233 sprintf(nm, "%d:%d", dl->major, dl->minor);
3234 fd = dev_open(nm, O_RDONLY);
3235
3236 if (is_fd_valid(fd)) {
3237 keep_fd = 0;
3238 break;
3239 }
3240 } else {
3241 fd = dl->fd;
3242 break;
3243 }
3244 }
3245
3246 if (!is_fd_valid(fd))
3247 return retval;
3248 retval = read_imsm_migr_rec(fd, super);
3249 if (!keep_fd)
3250 close(fd);
3251
3252 return retval;
3253 }
3254
3255 /*******************************************************************************
3256 * function: imsm_create_metadata_checkpoint_update
3257 * Description: It creates update for checkpoint change.
3258 * Parameters:
3259 * super : imsm internal array info
3260 * u : pointer to prepared update
3261 * Returns:
3262 * Uptate length.
3263 * If length is equal to 0, input pointer u contains no update
3264 ******************************************************************************/
3265 static int imsm_create_metadata_checkpoint_update(
3266 struct intel_super *super,
3267 struct imsm_update_general_migration_checkpoint **u)
3268 {
3269
3270 int update_memory_size = 0;
3271
3272 dprintf("(enter)\n");
3273
3274 if (u == NULL)
3275 return 0;
3276 *u = NULL;
3277
3278 /* size of all update data without anchor */
3279 update_memory_size =
3280 sizeof(struct imsm_update_general_migration_checkpoint);
3281
3282 *u = xcalloc(1, update_memory_size);
3283 if (*u == NULL) {
3284 dprintf("error: cannot get memory\n");
3285 return 0;
3286 }
3287 (*u)->type = update_general_migration_checkpoint;
3288 (*u)->curr_migr_unit = current_migr_unit(super->migr_rec);
3289 dprintf("prepared for %llu\n", (unsigned long long)(*u)->curr_migr_unit);
3290
3291 return update_memory_size;
3292 }
3293
3294 static void imsm_update_metadata_locally(struct supertype *st,
3295 void *buf, int len);
3296
3297 /*******************************************************************************
3298 * Function: write_imsm_migr_rec
3299 * Description: Function writes imsm migration record
3300 * (at the last sector of disk)
3301 * Parameters:
3302 * super : imsm internal array info
3303 * Returns:
3304 * 0 : success
3305 * -1 : if fail
3306 ******************************************************************************/
3307 static int write_imsm_migr_rec(struct supertype *st)
3308 {
3309 struct intel_super *super = st->sb;
3310 unsigned int sector_size = super->sector_size;
3311 unsigned long long dsize;
3312 int retval = -1;
3313 struct dl *sd;
3314 int len;
3315 struct imsm_update_general_migration_checkpoint *u;
3316 struct imsm_dev *dev;
3317 struct imsm_map *map;
3318
3319 /* find map under migration */
3320 dev = imsm_get_device_during_migration(super);
3321 /* if no migration, write buffer anyway to clear migr_record
3322 * on disk based on first available device
3323 */
3324 if (dev == NULL)
3325 dev = get_imsm_dev(super, super->current_vol < 0 ? 0 :
3326 super->current_vol);
3327
3328 map = get_imsm_map(dev, MAP_0);
3329
3330 if (sector_size == 4096)
3331 convert_to_4k_imsm_migr_rec(super);
3332 for (sd = super->disks ; sd ; sd = sd->next) {
3333 int slot = -1;
3334
3335 /* skip failed and spare devices */
3336 if (sd->index < 0)
3337 continue;
3338 /* write to 2 first slots only */
3339 if (map)
3340 slot = get_imsm_disk_slot(map, sd->index);
3341 if (map == NULL || slot > 1 || slot < 0)
3342 continue;
3343
3344 get_dev_size(sd->fd, NULL, &dsize);
3345 if (lseek64(sd->fd, dsize - (MIGR_REC_SECTOR_POSITION *
3346 sector_size),
3347 SEEK_SET) < 0) {
3348 pr_err("Cannot seek to anchor block: %s\n",
3349 strerror(errno));
3350 goto out;
3351 }
3352 if ((unsigned int)write(sd->fd, super->migr_rec_buf,
3353 MIGR_REC_BUF_SECTORS*sector_size) !=
3354 MIGR_REC_BUF_SECTORS*sector_size) {
3355 pr_err("Cannot write migr record block: %s\n",
3356 strerror(errno));
3357 goto out;
3358 }
3359 }
3360 if (sector_size == 4096)
3361 convert_from_4k_imsm_migr_rec(super);
3362 /* update checkpoint information in metadata */
3363 len = imsm_create_metadata_checkpoint_update(super, &u);
3364 if (len <= 0) {
3365 dprintf("imsm: Cannot prepare update\n");
3366 goto out;
3367 }
3368 /* update metadata locally */
3369 imsm_update_metadata_locally(st, u, len);
3370 /* and possibly remotely */
3371 if (st->update_tail) {
3372 append_metadata_update(st, u, len);
3373 /* during reshape we do all work inside metadata handler
3374 * manage_reshape(), so metadata update has to be triggered
3375 * insida it
3376 */
3377 flush_metadata_updates(st);
3378 st->update_tail = &st->updates;
3379 } else
3380 free(u);
3381
3382 retval = 0;
3383 out:
3384 return retval;
3385 }
3386
3387 /* spare/missing disks activations are not allowe when
3388 * array/container performs reshape operation, because
3389 * all arrays in container works on the same disks set
3390 */
3391 int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
3392 {
3393 int rv = 0;
3394 struct intel_dev *i_dev;
3395 struct imsm_dev *dev;
3396
3397 /* check whole container
3398 */
3399 for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
3400 dev = i_dev->dev;
3401 if (is_gen_migration(dev)) {
3402 /* No repair during any migration in container
3403 */
3404 rv = 1;
3405 break;
3406 }
3407 }
3408 return rv;
3409 }
3410 static unsigned long long imsm_component_size_alignment_check(int level,
3411 int chunk_size,
3412 unsigned int sector_size,
3413 unsigned long long component_size)
3414 {
3415 unsigned int component_size_alignment;
3416
3417 /* check component size alignment
3418 */
3419 component_size_alignment = component_size % (chunk_size/sector_size);
3420
3421 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alignment = %u\n",
3422 level, chunk_size, component_size,
3423 component_size_alignment);
3424
3425 if (component_size_alignment && (level != 1) && (level != UnSet)) {
3426 dprintf("imsm: reported component size aligned from %llu ",
3427 component_size);
3428 component_size -= component_size_alignment;
3429 dprintf_cont("to %llu (%i).\n",
3430 component_size, component_size_alignment);
3431 }
3432
3433 return component_size;
3434 }
3435
3436 /*******************************************************************************
3437 * Function: get_bitmap_header_sector
3438 * Description: Returns the sector where the bitmap header is placed.
3439 * Parameters:
3440 * st : supertype information
3441 * dev_idx : index of the device with bitmap
3442 *
3443 * Returns:
3444 * The sector where the bitmap header is placed
3445 ******************************************************************************/
3446 static unsigned long long get_bitmap_header_sector(struct intel_super *super,
3447 int dev_idx)
3448 {
3449 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3450 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3451
3452 if (!super->sector_size) {
3453 dprintf("sector size is not set\n");
3454 return 0;
3455 }
3456
3457 return pba_of_lba0(map) + calc_component_size(map, dev) +
3458 (IMSM_BITMAP_HEADER_OFFSET / super->sector_size);
3459 }
3460
3461 /*******************************************************************************
3462 * Function: get_bitmap_sector
3463 * Description: Returns the sector where the bitmap is placed.
3464 * Parameters:
3465 * st : supertype information
3466 * dev_idx : index of the device with bitmap
3467 *
3468 * Returns:
3469 * The sector where the bitmap is placed
3470 ******************************************************************************/
3471 static unsigned long long get_bitmap_sector(struct intel_super *super,
3472 int dev_idx)
3473 {
3474 if (!super->sector_size) {
3475 dprintf("sector size is not set\n");
3476 return 0;
3477 }
3478
3479 return get_bitmap_header_sector(super, dev_idx) +
3480 (IMSM_BITMAP_HEADER_SIZE / super->sector_size);
3481 }
3482
3483 static unsigned long long get_ppl_sector(struct intel_super *super, int dev_idx)
3484 {
3485 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3486 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3487
3488 return pba_of_lba0(map) +
3489 (num_data_stripes(map) * map->blocks_per_strip);
3490 }
3491
3492 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
3493 {
3494 struct intel_super *super = st->sb;
3495 struct migr_record *migr_rec = super->migr_rec;
3496 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
3497 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3498 struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
3499 struct imsm_map *map_to_analyse = map;
3500 struct dl *dl;
3501 int map_disks = info->array.raid_disks;
3502
3503 memset(info, 0, sizeof(*info));
3504 if (prev_map)
3505 map_to_analyse = prev_map;
3506
3507 dl = super->current_disk;
3508
3509 info->container_member = super->current_vol;
3510 info->array.raid_disks = map->num_members;
3511 info->array.level = get_imsm_raid_level(map_to_analyse);
3512 info->array.layout = imsm_level_to_layout(info->array.level);
3513 info->array.md_minor = -1;
3514 info->array.ctime = 0;
3515 info->array.utime = 0;
3516 info->array.chunk_size =
3517 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
3518 info->array.state = !(dev->vol.dirty & RAIDVOL_DIRTY);
3519 info->custom_array_size = imsm_dev_size(dev);
3520 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3521
3522 if (is_gen_migration(dev)) {
3523 /*
3524 * device prev_map should be added if it is in the middle
3525 * of migration
3526 */
3527 assert(prev_map);
3528
3529 info->reshape_active = 1;
3530 info->new_level = get_imsm_raid_level(map);
3531 info->new_layout = imsm_level_to_layout(info->new_level);
3532 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
3533 info->delta_disks = map->num_members - prev_map->num_members;
3534 if (info->delta_disks) {
3535 /* this needs to be applied to every array
3536 * in the container.
3537 */
3538 info->reshape_active = CONTAINER_RESHAPE;
3539 }
3540 /* We shape information that we give to md might have to be
3541 * modify to cope with md's requirement for reshaping arrays.
3542 * For example, when reshaping a RAID0, md requires it to be
3543 * presented as a degraded RAID4.
3544 * Also if a RAID0 is migrating to a RAID5 we need to specify
3545 * the array as already being RAID5, but the 'before' layout
3546 * is a RAID4-like layout.
3547 */
3548 switch (info->array.level) {
3549 case 0:
3550 switch(info->new_level) {
3551 case 0:
3552 /* conversion is happening as RAID4 */
3553 info->array.level = 4;
3554 info->array.raid_disks += 1;
3555 break;
3556 case 5:
3557 /* conversion is happening as RAID5 */
3558 info->array.level = 5;
3559 info->array.layout = ALGORITHM_PARITY_N;
3560 info->delta_disks -= 1;
3561 break;
3562 default:
3563 /* FIXME error message */
3564 info->array.level = UnSet;
3565 break;
3566 }
3567 break;
3568 }
3569 } else {
3570 info->new_level = UnSet;
3571 info->new_layout = UnSet;
3572 info->new_chunk = info->array.chunk_size;
3573 info->delta_disks = 0;
3574 }
3575
3576 if (dl) {
3577 info->disk.major = dl->major;
3578 info->disk.minor = dl->minor;
3579 info->disk.number = dl->index;
3580 info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
3581 dl->index);
3582 }
3583
3584 info->data_offset = pba_of_lba0(map_to_analyse);
3585 info->component_size = calc_component_size(map, dev);
3586 info->component_size = imsm_component_size_alignment_check(
3587 info->array.level,
3588 info->array.chunk_size,
3589 super->sector_size,
3590 info->component_size);
3591 info->bb.supported = 1;
3592
3593 memset(info->uuid, 0, sizeof(info->uuid));
3594 info->recovery_start = MaxSector;
3595
3596 if (info->array.level == 5 &&
3597 (dev->rwh_policy == RWH_DISTRIBUTED ||
3598 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)) {
3599 info->consistency_policy = CONSISTENCY_POLICY_PPL;
3600 info->ppl_sector = get_ppl_sector(super, super->current_vol);
3601 if (dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
3602 info->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
3603 else
3604 info->ppl_size = (PPL_HEADER_SIZE + PPL_ENTRY_SPACE)
3605 >> 9;
3606 } else if (info->array.level <= 0) {
3607 info->consistency_policy = CONSISTENCY_POLICY_NONE;
3608 } else {
3609 if (dev->rwh_policy == RWH_BITMAP) {
3610 info->bitmap_offset = get_bitmap_sector(super, super->current_vol);
3611 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
3612 } else {
3613 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
3614 }
3615 }
3616
3617 info->reshape_progress = 0;
3618 info->resync_start = MaxSector;
3619 if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
3620 !(info->array.state & 1)) &&
3621 imsm_reshape_blocks_arrays_changes(super) == 0) {
3622 info->resync_start = 0;
3623 }
3624 if (dev->vol.migr_state) {
3625 switch (migr_type(dev)) {
3626 case MIGR_REPAIR:
3627 case MIGR_INIT: {
3628 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3629 dev);
3630 __u64 units = vol_curr_migr_unit(dev);
3631
3632 info->resync_start = blocks_per_unit * units;
3633 break;
3634 }
3635 case MIGR_GEN_MIGR: {
3636 __u64 blocks_per_unit = blocks_per_migr_unit(super,
3637 dev);
3638 __u64 units = current_migr_unit(migr_rec);
3639 int used_disks;
3640
3641 if (__le32_to_cpu(migr_rec->ascending_migr) &&
3642 (units <
3643 (get_num_migr_units(migr_rec)-1)) &&
3644 (super->migr_rec->rec_status ==
3645 __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
3646 units++;
3647
3648 info->reshape_progress = blocks_per_unit * units;
3649
3650 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3651 (unsigned long long)units,
3652 (unsigned long long)blocks_per_unit,
3653 info->reshape_progress);
3654
3655 used_disks = imsm_num_data_members(prev_map);
3656 if (used_disks > 0) {
3657 info->custom_array_size = per_dev_array_size(map) *
3658 used_disks;
3659 }
3660 }
3661 case MIGR_VERIFY:
3662 /* we could emulate the checkpointing of
3663 * 'sync_action=check' migrations, but for now
3664 * we just immediately complete them
3665 */
3666 case MIGR_REBUILD:
3667 /* this is handled by container_content_imsm() */
3668 case MIGR_STATE_CHANGE:
3669 /* FIXME handle other migrations */
3670 default:
3671 /* we are not dirty, so... */
3672 info->resync_start = MaxSector;
3673 }
3674 }
3675
3676 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
3677 info->name[MAX_RAID_SERIAL_LEN] = 0;
3678
3679 info->array.major_version = -1;
3680 info->array.minor_version = -2;
3681 sprintf(info->text_version, "/%s/%d", st->container_devnm, info->container_member);
3682 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
3683 uuid_from_super_imsm(st, info->uuid);
3684
3685 if (dmap) {
3686 int i, j;
3687 for (i=0; i<map_disks; i++) {
3688 dmap[i] = 0;
3689 if (i < info->array.raid_disks) {
3690 struct imsm_disk *dsk;
3691 j = get_imsm_disk_idx(dev, i, MAP_X);
3692 dsk = get_imsm_disk(super, j);
3693 if (dsk && (dsk->status & CONFIGURED_DISK))
3694 dmap[i] = 1;
3695 }
3696 }
3697 }
3698 }
3699
3700 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
3701 int failed, int look_in_map);
3702
3703 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
3704 int look_in_map);
3705
3706 static void manage_second_map(struct intel_super *super, struct imsm_dev *dev)
3707 {
3708 if (is_gen_migration(dev)) {
3709 int failed;
3710 __u8 map_state;
3711 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
3712
3713 failed = imsm_count_failed(super, dev, MAP_1);
3714 map_state = imsm_check_degraded(super, dev, failed, MAP_1);
3715 if (map2->map_state != map_state) {
3716 map2->map_state = map_state;
3717 super->updates_pending++;
3718 }
3719 }
3720 }
3721
3722 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
3723 {
3724 struct dl *d;
3725
3726 for (d = super->missing; d; d = d->next)
3727 if (d->index == index)
3728 return &d->disk;
3729 return NULL;
3730 }
3731
3732 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
3733 {
3734 struct intel_super *super = st->sb;
3735 struct imsm_disk *disk;
3736 int map_disks = info->array.raid_disks;
3737 int max_enough = -1;
3738 int i;
3739 struct imsm_super *mpb;
3740
3741 if (super->current_vol >= 0) {
3742 getinfo_super_imsm_volume(st, info, map);
3743 return;
3744 }
3745 memset(info, 0, sizeof(*info));
3746
3747 /* Set raid_disks to zero so that Assemble will always pull in valid
3748 * spares
3749 */
3750 info->array.raid_disks = 0;
3751 info->array.level = LEVEL_CONTAINER;
3752 info->array.layout = 0;
3753 info->array.md_minor = -1;
3754 info->array.ctime = 0; /* N/A for imsm */
3755 info->array.utime = 0;
3756 info->array.chunk_size = 0;
3757
3758 info->disk.major = 0;
3759 info->disk.minor = 0;
3760 info->disk.raid_disk = -1;
3761 info->reshape_active = 0;
3762 info->array.major_version = -1;
3763 info->array.minor_version = -2;
3764 strcpy(info->text_version, "imsm");
3765 info->safe_mode_delay = 0;
3766 info->disk.number = -1;
3767 info->disk.state = 0;
3768 info->name[0] = 0;
3769 info->recovery_start = MaxSector;
3770 info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
3771 info->bb.supported = 1;
3772
3773 /* do we have the all the insync disks that we expect? */
3774 mpb = super->anchor;
3775 info->events = __le32_to_cpu(mpb->generation_num);
3776
3777 for (i = 0; i < mpb->num_raid_devs; i++) {
3778 struct imsm_dev *dev = get_imsm_dev(super, i);
3779 int failed, enough, j, missing = 0;
3780 struct imsm_map *map;
3781 __u8 state;
3782
3783 failed = imsm_count_failed(super, dev, MAP_0);
3784 state = imsm_check_degraded(super, dev, failed, MAP_0);
3785 map = get_imsm_map(dev, MAP_0);
3786
3787 /* any newly missing disks?
3788 * (catches single-degraded vs double-degraded)
3789 */
3790 for (j = 0; j < map->num_members; j++) {
3791 __u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
3792 __u32 idx = ord_to_idx(ord);
3793
3794 if (super->disks && super->disks->index == (int)idx)
3795 info->disk.raid_disk = j;
3796
3797 if (!(ord & IMSM_ORD_REBUILD) &&
3798 get_imsm_missing(super, idx)) {
3799 missing = 1;
3800 break;
3801 }
3802 }
3803
3804 if (state == IMSM_T_STATE_FAILED)
3805 enough = -1;
3806 else if (state == IMSM_T_STATE_DEGRADED &&
3807 (state != map->map_state || missing))
3808 enough = 0;
3809 else /* we're normal, or already degraded */
3810 enough = 1;
3811 if (is_gen_migration(dev) && missing) {
3812 /* during general migration we need all disks
3813 * that process is running on.
3814 * No new missing disk is allowed.
3815 */
3816 max_enough = -1;
3817 enough = -1;
3818 /* no more checks necessary
3819 */
3820 break;
3821 }
3822 /* in the missing/failed disk case check to see
3823 * if at least one array is runnable
3824 */
3825 max_enough = max(max_enough, enough);
3826 }
3827
3828 info->container_enough = max_enough;
3829
3830 if (super->disks) {
3831 __u32 reserved = imsm_reserved_sectors(super, super->disks);
3832
3833 disk = &super->disks->disk;
3834 info->data_offset = total_blocks(&super->disks->disk) - reserved;
3835 info->component_size = reserved;
3836 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
3837 /* we don't change info->disk.raid_disk here because
3838 * this state will be finalized in mdmon after we have
3839 * found the 'most fresh' version of the metadata
3840 */
3841 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3842 info->disk.state |= (is_spare(disk) || is_journal(disk)) ?
3843 0 : (1 << MD_DISK_SYNC);
3844 }
3845
3846 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3847 * ->compare_super may have updated the 'num_raid_devs' field for spares
3848 */
3849 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
3850 uuid_from_super_imsm(st, info->uuid);
3851 else
3852 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
3853
3854 /* I don't know how to compute 'map' on imsm, so use safe default */
3855 if (map) {
3856 int i;
3857 for (i = 0; i < map_disks; i++)
3858 map[i] = 1;
3859 }
3860
3861 }
3862
3863 /* allocates memory and fills disk in mdinfo structure
3864 * for each disk in array */
3865 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
3866 {
3867 struct mdinfo *mddev;
3868 struct intel_super *super = st->sb;
3869 struct imsm_disk *disk;
3870 int count = 0;
3871 struct dl *dl;
3872 if (!super || !super->disks)
3873 return NULL;
3874 dl = super->disks;
3875 mddev = xcalloc(1, sizeof(*mddev));
3876 while (dl) {
3877 struct mdinfo *tmp;
3878 disk = &dl->disk;
3879 tmp = xcalloc(1, sizeof(*tmp));
3880 if (mddev->devs)
3881 tmp->next = mddev->devs;
3882 mddev->devs = tmp;
3883 tmp->disk.number = count++;
3884 tmp->disk.major = dl->major;
3885 tmp->disk.minor = dl->minor;
3886 tmp->disk.state = is_configured(disk) ?
3887 (1 << MD_DISK_ACTIVE) : 0;
3888 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
3889 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
3890 tmp->disk.raid_disk = -1;
3891 dl = dl->next;
3892 }
3893 return mddev;
3894 }
3895
3896 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
3897 enum update_opt update, char *devname,
3898 int verbose, int uuid_set, char *homehost)
3899 {
3900 /* For 'assemble' and 'force' we need to return non-zero if any
3901 * change was made. For others, the return value is ignored.
3902 * Update options are:
3903 * force-one : This device looks a bit old but needs to be included,
3904 * update age info appropriately.
3905 * assemble: clear any 'faulty' flag to allow this device to
3906 * be assembled.
3907 * force-array: Array is degraded but being forced, mark it clean
3908 * if that will be needed to assemble it.
3909 *
3910 * newdev: not used ????
3911 * grow: Array has gained a new device - this is currently for
3912 * linear only
3913 * resync: mark as dirty so a resync will happen.
3914 * name: update the name - preserving the homehost
3915 * uuid: Change the uuid of the array to match watch is given
3916 *
3917 * Following are not relevant for this imsm:
3918 * sparc2.2 : update from old dodgey metadata
3919 * super-minor: change the preferred_minor number
3920 * summaries: update redundant counters.
3921 * homehost: update the recorded homehost
3922 * _reshape_progress: record new reshape_progress position.
3923 */
3924 int rv = 1;
3925 struct intel_super *super = st->sb;
3926 struct imsm_super *mpb;
3927
3928 /* we can only update container info */
3929 if (!super || super->current_vol >= 0 || !super->anchor)
3930 return 1;
3931
3932 mpb = super->anchor;
3933
3934 switch (update) {
3935 case UOPT_UUID:
3936 /* We take this to mean that the family_num should be updated.
3937 * However that is much smaller than the uuid so we cannot really
3938 * allow an explicit uuid to be given. And it is hard to reliably
3939 * know if one was.
3940 * So if !uuid_set we know the current uuid is random and just used
3941 * the first 'int' and copy it to the other 3 positions.
3942 * Otherwise we require the 4 'int's to be the same as would be the
3943 * case if we are using a random uuid. So an explicit uuid will be
3944 * accepted as long as all for ints are the same... which shouldn't hurt
3945 */
3946 if (!uuid_set) {
3947 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
3948 rv = 0;
3949 } else {
3950 if (info->uuid[0] != info->uuid[1] ||
3951 info->uuid[1] != info->uuid[2] ||
3952 info->uuid[2] != info->uuid[3])
3953 rv = -1;
3954 else
3955 rv = 0;
3956 }
3957 if (rv == 0)
3958 mpb->orig_family_num = info->uuid[0];
3959 break;
3960 case UOPT_SPEC_ASSEMBLE:
3961 rv = 0;
3962 break;
3963 default:
3964 rv = -1;
3965 break;
3966 }
3967
3968 /* successful update? recompute checksum */
3969 if (rv == 0)
3970 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
3971
3972 return rv;
3973 }
3974
3975 static size_t disks_to_mpb_size(int disks)
3976 {
3977 size_t size;
3978
3979 size = sizeof(struct imsm_super);
3980 size += (disks - 1) * sizeof(struct imsm_disk);
3981 size += 2 * sizeof(struct imsm_dev);
3982 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3983 size += (4 - 2) * sizeof(struct imsm_map);
3984 /* 4 possible disk_ord_tbl's */
3985 size += 4 * (disks - 1) * sizeof(__u32);
3986 /* maximum bbm log */
3987 size += sizeof(struct bbm_log);
3988
3989 return size;
3990 }
3991
3992 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize,
3993 unsigned long long data_offset)
3994 {
3995 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
3996 return 0;
3997
3998 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
3999 }
4000
4001 static void free_devlist(struct intel_super *super)
4002 {
4003 struct intel_dev *dv;
4004
4005 while (super->devlist) {
4006 dv = super->devlist->next;
4007 free(super->devlist->dev);
4008 free(super->devlist);
4009 super->devlist = dv;
4010 }
4011 }
4012
4013 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
4014 {
4015 memcpy(dest, src, sizeof_imsm_dev(src, 0));
4016 }
4017
4018 static int compare_super_imsm(struct supertype *st, struct supertype *tst,
4019 int verbose)
4020 {
4021 /* return:
4022 * 0 same, or first was empty, and second was copied
4023 * 1 sb are different
4024 */
4025 struct intel_super *first = st->sb;
4026 struct intel_super *sec = tst->sb;
4027
4028 if (!first) {
4029 st->sb = tst->sb;
4030 tst->sb = NULL;
4031 return 0;
4032 }
4033
4034 /* in platform dependent environment test if the disks
4035 * use the same Intel hba
4036 * if not on Intel hba at all, allow anything.
4037 * doesn't check HBAs if num_raid_devs is not set, as it means
4038 * it is a free floating spare, and all spares regardless of HBA type
4039 * will fall into separate container during the assembly
4040 */
4041 if (first->hba && sec->hba && first->anchor->num_raid_devs != 0) {
4042 if (first->hba->type != sec->hba->type) {
4043 if (verbose)
4044 pr_err("HBAs of devices do not match %s != %s\n",
4045 get_sys_dev_type(first->hba->type),
4046 get_sys_dev_type(sec->hba->type));
4047 return 1;
4048 }
4049 if (first->orom != sec->orom) {
4050 if (verbose)
4051 pr_err("HBAs of devices do not match %s != %s\n",
4052 first->hba->pci_id, sec->hba->pci_id);
4053 return 1;
4054 }
4055 }
4056
4057 if (first->anchor->num_raid_devs > 0 &&
4058 sec->anchor->num_raid_devs > 0) {
4059 /* Determine if these disks might ever have been
4060 * related. Further disambiguation can only take place
4061 * in load_super_imsm_all
4062 */
4063 __u32 first_family = first->anchor->orig_family_num;
4064 __u32 sec_family = sec->anchor->orig_family_num;
4065
4066 if (memcmp(first->anchor->sig, sec->anchor->sig,
4067 MAX_SIGNATURE_LENGTH) != 0)
4068 return 1;
4069
4070 if (first_family == 0)
4071 first_family = first->anchor->family_num;
4072 if (sec_family == 0)
4073 sec_family = sec->anchor->family_num;
4074
4075 if (first_family != sec_family)
4076 return 1;
4077
4078 }
4079
4080 /* if an anchor does not have num_raid_devs set then it is a free
4081 * floating spare. don't assosiate spare with any array, as during assembly
4082 * spares shall fall into separate container, from which they can be moved
4083 * when necessary
4084 */
4085 if (first->anchor->num_raid_devs ^ sec->anchor->num_raid_devs)
4086 return 1;
4087
4088 return 0;
4089 }
4090
4091 static void fd2devname(int fd, char *name)
4092 {
4093 char *nm;
4094
4095 nm = fd2kname(fd);
4096 if (!nm)
4097 return;
4098
4099 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
4100 }
4101
4102 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
4103 {
4104 char path[PATH_MAX];
4105 char *name = fd2kname(fd);
4106
4107 if (!name)
4108 return 1;
4109
4110 if (strncmp(name, "nvme", 4) != 0)
4111 return 1;
4112
4113 if (!diskfd_to_devpath(fd, 1, path))
4114 return 1;
4115
4116 return devpath_to_char(path, "serial", buf, buf_len, 0);
4117 }
4118
4119 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
4120
4121 static int imsm_read_serial(int fd, char *devname,
4122 __u8 *serial, size_t serial_buf_len)
4123 {
4124 char buf[50];
4125 int rv;
4126 size_t len;
4127 char *dest;
4128 char *src;
4129 unsigned int i;
4130
4131 memset(buf, 0, sizeof(buf));
4132
4133 if (check_env("IMSM_DEVNAME_AS_SERIAL")) {
4134 memset(serial, 0, serial_buf_len);
4135 fd2devname(fd, (char *) serial);
4136 return 0;
4137 }
4138
4139 rv = nvme_get_serial(fd, buf, sizeof(buf));
4140
4141 if (rv)
4142 rv = scsi_get_serial(fd, buf, sizeof(buf));
4143
4144 if (rv != 0) {
4145 if (devname)
4146 pr_err("Failed to retrieve serial for %s\n",
4147 devname);
4148 return rv;
4149 }
4150
4151 /* trim all whitespace and non-printable characters and convert
4152 * ':' to ';'
4153 */
4154 for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
4155 src = &buf[i];
4156 if (*src > 0x20) {
4157 /* ':' is reserved for use in placeholder serial
4158 * numbers for missing disks
4159 */
4160 if (*src == ':')
4161 *dest++ = ';';
4162 else
4163 *dest++ = *src;
4164 }
4165 }
4166 len = dest - buf;
4167 dest = buf;
4168
4169 if (len > serial_buf_len) {
4170 /* truncate leading characters */
4171 dest += len - serial_buf_len;
4172 len = serial_buf_len;
4173 }
4174
4175 memset(serial, 0, serial_buf_len);
4176 memcpy(serial, dest, len);
4177
4178 return 0;
4179 }
4180
4181 static int serialcmp(__u8 *s1, __u8 *s2)
4182 {
4183 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
4184 }
4185
4186 static void serialcpy(__u8 *dest, __u8 *src)
4187 {
4188 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
4189 }
4190
4191 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
4192 {
4193 struct dl *dl;
4194
4195 for (dl = super->disks; dl; dl = dl->next)
4196 if (serialcmp(dl->serial, serial) == 0)
4197 break;
4198
4199 return dl;
4200 }
4201
4202 static struct imsm_disk *
4203 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
4204 {
4205 int i;
4206
4207 for (i = 0; i < mpb->num_disks; i++) {
4208 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4209
4210 if (serialcmp(disk->serial, serial) == 0) {
4211 if (idx)
4212 *idx = i;
4213 return disk;
4214 }
4215 }
4216
4217 return NULL;
4218 }
4219
4220 static int
4221 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
4222 {
4223 struct imsm_disk *disk;
4224 struct dl *dl;
4225 struct stat stb;
4226 int rv;
4227 char name[40];
4228 __u8 serial[MAX_RAID_SERIAL_LEN];
4229
4230 rv = imsm_read_serial(fd, devname, serial, MAX_RAID_SERIAL_LEN);
4231
4232 if (rv != 0)
4233 return 2;
4234
4235 dl = xcalloc(1, sizeof(*dl));
4236
4237 fstat(fd, &stb);
4238 dl->major = major(stb.st_rdev);
4239 dl->minor = minor(stb.st_rdev);
4240 dl->next = super->disks;
4241 dl->fd = keep_fd ? fd : -1;
4242 assert(super->disks == NULL);
4243 super->disks = dl;
4244 serialcpy(dl->serial, serial);
4245 dl->index = -2;
4246 dl->e = NULL;
4247 fd2devname(fd, name);
4248 if (devname)
4249 dl->devname = xstrdup(devname);
4250 else
4251 dl->devname = xstrdup(name);
4252
4253 /* look up this disk's index in the current anchor */
4254 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
4255 if (disk) {
4256 dl->disk = *disk;
4257 /* only set index on disks that are a member of a
4258 * populated contianer, i.e. one with raid_devs
4259 */
4260 if (is_failed(&dl->disk))
4261 dl->index = -2;
4262 else if (is_spare(&dl->disk) || is_journal(&dl->disk))
4263 dl->index = -1;
4264 }
4265
4266 return 0;
4267 }
4268
4269 /* When migrating map0 contains the 'destination' state while map1
4270 * contains the current state. When not migrating map0 contains the
4271 * current state. This routine assumes that map[0].map_state is set to
4272 * the current array state before being called.
4273 *
4274 * Migration is indicated by one of the following states
4275 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4276 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4277 * map1state=unitialized)
4278 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4279 * map1state=normal)
4280 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4281 * map1state=degraded)
4282 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4283 * map1state=normal)
4284 */
4285 static void migrate(struct imsm_dev *dev, struct intel_super *super,
4286 __u8 to_state, int migr_type)
4287 {
4288 struct imsm_map *dest;
4289 struct imsm_map *src = get_imsm_map(dev, MAP_0);
4290
4291 dev->vol.migr_state = 1;
4292 set_migr_type(dev, migr_type);
4293 set_vol_curr_migr_unit(dev, 0);
4294 dest = get_imsm_map(dev, MAP_1);
4295
4296 /* duplicate and then set the target end state in map[0] */
4297 memcpy(dest, src, sizeof_imsm_map(src));
4298 if (migr_type == MIGR_GEN_MIGR) {
4299 __u32 ord;
4300 int i;
4301
4302 for (i = 0; i < src->num_members; i++) {
4303 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
4304 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
4305 }
4306 }
4307
4308 if (migr_type == MIGR_GEN_MIGR)
4309 /* Clear migration record */
4310 memset(super->migr_rec, 0, sizeof(struct migr_record));
4311
4312 src->map_state = to_state;
4313 }
4314
4315 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
4316 __u8 map_state)
4317 {
4318 struct imsm_map *map = get_imsm_map(dev, MAP_0);
4319 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
4320 MAP_0 : MAP_1);
4321 int i, j;
4322
4323 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4324 * completed in the last migration.
4325 *
4326 * FIXME add support for raid-level-migration
4327 */
4328 if (map_state != map->map_state && (is_gen_migration(dev) == false) &&
4329 prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
4330 /* when final map state is other than expected
4331 * merge maps (not for migration)
4332 */
4333 int failed;
4334
4335 for (i = 0; i < prev->num_members; i++)
4336 for (j = 0; j < map->num_members; j++)
4337 /* during online capacity expansion
4338 * disks position can be changed
4339 * if takeover is used
4340 */
4341 if (ord_to_idx(map->disk_ord_tbl[j]) ==
4342 ord_to_idx(prev->disk_ord_tbl[i])) {
4343 map->disk_ord_tbl[j] |=
4344 prev->disk_ord_tbl[i];
4345 break;
4346 }
4347 failed = imsm_count_failed(super, dev, MAP_0);
4348 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
4349 }
4350
4351 dev->vol.migr_state = 0;
4352 set_migr_type(dev, 0);
4353 set_vol_curr_migr_unit(dev, 0);
4354 map->map_state = map_state;
4355 }
4356
4357 static int parse_raid_devices(struct intel_super *super)
4358 {
4359 int i;
4360 struct imsm_dev *dev_new;
4361 size_t len, len_migr;
4362 size_t max_len = 0;
4363 size_t space_needed = 0;
4364 struct imsm_super *mpb = super->anchor;
4365
4366 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4367 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4368 struct intel_dev *dv;
4369
4370 len = sizeof_imsm_dev(dev_iter, 0);
4371 len_migr = sizeof_imsm_dev(dev_iter, 1);
4372 if (len_migr > len)
4373 space_needed += len_migr - len;
4374
4375 dv = xmalloc(sizeof(*dv));
4376 if (max_len < len_migr)
4377 max_len = len_migr;
4378 if (max_len > len_migr)
4379 space_needed += max_len - len_migr;
4380 dev_new = xmalloc(max_len);
4381 imsm_copy_dev(dev_new, dev_iter);
4382 dv->dev = dev_new;
4383 dv->index = i;
4384 dv->next = super->devlist;
4385 super->devlist = dv;
4386 }
4387
4388 /* ensure that super->buf is large enough when all raid devices
4389 * are migrating
4390 */
4391 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
4392 void *buf;
4393
4394 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed,
4395 super->sector_size);
4396 if (posix_memalign(&buf, MAX_SECTOR_SIZE, len) != 0)
4397 return 1;
4398
4399 memcpy(buf, super->buf, super->len);
4400 memset(buf + super->len, 0, len - super->len);
4401 free(super->buf);
4402 super->buf = buf;
4403 super->len = len;
4404 }
4405
4406 super->extra_space += space_needed;
4407
4408 return 0;
4409 }
4410
4411 /*******************************************************************************
4412 * Function: check_mpb_migr_compatibility
4413 * Description: Function checks for unsupported migration features:
4414 * - migration optimization area (pba_of_lba0)
4415 * - descending reshape (ascending_migr)
4416 * Parameters:
4417 * super : imsm metadata information
4418 * Returns:
4419 * 0 : migration is compatible
4420 * -1 : migration is not compatible
4421 ******************************************************************************/
4422 int check_mpb_migr_compatibility(struct intel_super *super)
4423 {
4424 struct imsm_map *map0, *map1;
4425 struct migr_record *migr_rec = super->migr_rec;
4426 int i;
4427
4428 for (i = 0; i < super->anchor->num_raid_devs; i++) {
4429 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
4430
4431 if (dev_iter->vol.migr_state == 1 &&
4432 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
4433 /* This device is migrating */
4434 map0 = get_imsm_map(dev_iter, MAP_0);
4435 map1 = get_imsm_map(dev_iter, MAP_1);
4436 if (pba_of_lba0(map0) != pba_of_lba0(map1))
4437 /* migration optimization area was used */
4438 return -1;
4439 if (migr_rec->ascending_migr == 0 &&
4440 migr_rec->dest_depth_per_unit > 0)
4441 /* descending reshape not supported yet */
4442 return -1;
4443 }
4444 }
4445 return 0;
4446 }
4447
4448 static void __free_imsm(struct intel_super *super, int free_disks);
4449
4450 /* load_imsm_mpb - read matrix metadata
4451 * allocates super->mpb to be freed by free_imsm
4452 */
4453 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
4454 {
4455 unsigned long long dsize;
4456 unsigned long long sectors;
4457 unsigned int sector_size = super->sector_size;
4458 struct stat;
4459 struct imsm_super *anchor;
4460 __u32 check_sum;
4461
4462 get_dev_size(fd, NULL, &dsize);
4463 if (dsize < 2*sector_size) {
4464 if (devname)
4465 pr_err("%s: device to small for imsm\n",
4466 devname);
4467 return 1;
4468 }
4469
4470 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0) {
4471 if (devname)
4472 pr_err("Cannot seek to anchor block on %s: %s\n",
4473 devname, strerror(errno));
4474 return 1;
4475 }
4476
4477 if (posix_memalign((void **)&anchor, sector_size, sector_size) != 0) {
4478 if (devname)
4479 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname);
4480 return 1;
4481 }
4482 if ((unsigned int)read(fd, anchor, sector_size) != sector_size) {
4483 if (devname)
4484 pr_err("Cannot read anchor block on %s: %s\n",
4485 devname, strerror(errno));
4486 free(anchor);
4487 return 1;
4488 }
4489
4490 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
4491 if (devname)
4492 pr_err("no IMSM anchor on %s\n", devname);
4493 free(anchor);
4494 return 2;
4495 }
4496
4497 __free_imsm(super, 0);
4498 /* reload capability and hba */
4499
4500 /* capability and hba must be updated with new super allocation */
4501 find_intel_hba_capability(fd, super, devname);
4502 super->len = ROUND_UP(anchor->mpb_size, sector_size);
4503 if (posix_memalign(&super->buf, MAX_SECTOR_SIZE, super->len) != 0) {
4504 if (devname)
4505 pr_err("unable to allocate %zu byte mpb buffer\n",
4506 super->len);
4507 free(anchor);
4508 return 2;
4509 }
4510 memcpy(super->buf, anchor, sector_size);
4511
4512 sectors = mpb_sectors(anchor, sector_size) - 1;
4513 free(anchor);
4514
4515 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
4516 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
4517 pr_err("could not allocate migr_rec buffer\n");
4518 free(super->buf);
4519 super->buf = NULL;
4520 return 2;
4521 }
4522 super->clean_migration_record_by_mdmon = 0;
4523
4524 if (!sectors) {
4525 check_sum = __gen_imsm_checksum(super->anchor);
4526 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4527 if (devname)
4528 pr_err("IMSM checksum %x != %x on %s\n",
4529 check_sum,
4530 __le32_to_cpu(super->anchor->check_sum),
4531 devname);
4532 return 2;
4533 }
4534
4535 return 0;
4536 }
4537
4538 /* read the extended mpb */
4539 if (lseek64(fd, dsize - (sector_size * (2 + sectors)), SEEK_SET) < 0) {
4540 if (devname)
4541 pr_err("Cannot seek to extended mpb on %s: %s\n",
4542 devname, strerror(errno));
4543 return 1;
4544 }
4545
4546 if ((unsigned int)read(fd, super->buf + sector_size,
4547 super->len - sector_size) != super->len - sector_size) {
4548 if (devname)
4549 pr_err("Cannot read extended mpb on %s: %s\n",
4550 devname, strerror(errno));
4551 return 2;
4552 }
4553
4554 check_sum = __gen_imsm_checksum(super->anchor);
4555 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
4556 if (devname)
4557 pr_err("IMSM checksum %x != %x on %s\n",
4558 check_sum, __le32_to_cpu(super->anchor->check_sum),
4559 devname);
4560 return 3;
4561 }
4562
4563 return 0;
4564 }
4565
4566 static int read_imsm_migr_rec(int fd, struct intel_super *super);
4567
4568 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4569 static void clear_hi(struct intel_super *super)
4570 {
4571 struct imsm_super *mpb = super->anchor;
4572 int i, n;
4573 if (mpb->attributes & MPB_ATTRIB_2TB_DISK)
4574 return;
4575 for (i = 0; i < mpb->num_disks; ++i) {
4576 struct imsm_disk *disk = &mpb->disk[i];
4577 disk->total_blocks_hi = 0;
4578 }
4579 for (i = 0; i < mpb->num_raid_devs; ++i) {
4580 struct imsm_dev *dev = get_imsm_dev(super, i);
4581 for (n = 0; n < 2; ++n) {
4582 struct imsm_map *map = get_imsm_map(dev, n);
4583 if (!map)
4584 continue;
4585 map->pba_of_lba0_hi = 0;
4586 map->blocks_per_member_hi = 0;
4587 map->num_data_stripes_hi = 0;
4588 }
4589 }
4590 }
4591
4592 static int
4593 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
4594 {
4595 int err;
4596
4597 err = load_imsm_mpb(fd, super, devname);
4598 if (err)
4599 return err;
4600 if (super->sector_size == 4096)
4601 convert_from_4k(super);
4602 err = load_imsm_disk(fd, super, devname, keep_fd);
4603 if (err)
4604 return err;
4605 err = parse_raid_devices(super);
4606 if (err)
4607 return err;
4608 err = load_bbm_log(super);
4609 clear_hi(super);
4610 return err;
4611 }
4612
4613 static void __free_imsm_disk(struct dl *d, int do_close)
4614 {
4615 if (do_close)
4616 close_fd(&d->fd);
4617 if (d->devname)
4618 free(d->devname);
4619 if (d->e)
4620 free(d->e);
4621 free(d);
4622
4623 }
4624
4625 static void free_imsm_disks(struct intel_super *super)
4626 {
4627 struct dl *d;
4628
4629 while (super->disks) {
4630 d = super->disks;
4631 super->disks = d->next;
4632 __free_imsm_disk(d, 1);
4633 }
4634 while (super->disk_mgmt_list) {
4635 d = super->disk_mgmt_list;
4636 super->disk_mgmt_list = d->next;
4637 __free_imsm_disk(d, 1);
4638 }
4639 while (super->missing) {
4640 d = super->missing;
4641 super->missing = d->next;
4642 __free_imsm_disk(d, 1);
4643 }
4644
4645 }
4646
4647 /* free all the pieces hanging off of a super pointer */
4648 static void __free_imsm(struct intel_super *super, int free_disks)
4649 {
4650 struct intel_hba *elem, *next;
4651
4652 if (super->buf) {
4653 free(super->buf);
4654 super->buf = NULL;
4655 }
4656 /* unlink capability description */
4657 super->orom = NULL;
4658 if (super->migr_rec_buf) {
4659 free(super->migr_rec_buf);
4660 super->migr_rec_buf = NULL;
4661 }
4662 if (free_disks)
4663 free_imsm_disks(super);
4664 free_devlist(super);
4665 elem = super->hba;
4666 while (elem) {
4667 if (elem->path)
4668 free((void *)elem->path);
4669 next = elem->next;
4670 free(elem);
4671 elem = next;
4672 }
4673 if (super->bbm_log)
4674 free(super->bbm_log);
4675 super->hba = NULL;
4676 }
4677
4678 static void free_imsm(struct intel_super *super)
4679 {
4680 __free_imsm(super, 1);
4681 free(super->bb.entries);
4682 free(super);
4683 }
4684
4685 static void free_super_imsm(struct supertype *st)
4686 {
4687 struct intel_super *super = st->sb;
4688
4689 if (!super)
4690 return;
4691
4692 free_imsm(super);
4693 st->sb = NULL;
4694 }
4695
4696 static struct intel_super *alloc_super(void)
4697 {
4698 struct intel_super *super = xcalloc(1, sizeof(*super));
4699
4700 super->current_vol = -1;
4701 super->create_offset = ~((unsigned long long) 0);
4702
4703 super->bb.entries = xmalloc(BBM_LOG_MAX_ENTRIES *
4704 sizeof(struct md_bb_entry));
4705 if (!super->bb.entries) {
4706 free(super);
4707 return NULL;
4708 }
4709
4710 return super;
4711 }
4712
4713 /*
4714 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4715 */
4716 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
4717 {
4718 struct sys_dev *hba_name;
4719 int rv = 0;
4720
4721 if (is_fd_valid(fd) && test_partition(fd)) {
4722 pr_err("imsm: %s is a partition, cannot be used in IMSM\n",
4723 devname);
4724 return 1;
4725 }
4726 if (!is_fd_valid(fd) || check_no_platform()) {
4727 super->orom = NULL;
4728 super->hba = NULL;
4729 return 0;
4730 }
4731 hba_name = find_disk_attached_hba(fd, NULL);
4732 if (!hba_name) {
4733 if (devname)
4734 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4735 devname);
4736 return 1;
4737 }
4738 rv = attach_hba_to_super(super, hba_name);
4739 if (rv == 2) {
4740 if (devname) {
4741 struct intel_hba *hba = super->hba;
4742
4743 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4744 " but the container is assigned to Intel(R) %s %s (",
4745 devname,
4746 get_sys_dev_type(hba_name->type),
4747 hba_name->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4748 "domain" : "RAID controller",
4749 hba_name->pci_id ? : "Err!",
4750 get_sys_dev_type(super->hba->type),
4751 hba->type == SYS_DEV_VMD || hba_name->type == SYS_DEV_SATA_VMD ?
4752 "domain" : "RAID controller");
4753
4754 while (hba) {
4755 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
4756 if (hba->next)
4757 fprintf(stderr, ", ");
4758 hba = hba->next;
4759 }
4760 fprintf(stderr, ").\n"
4761 " Mixing devices attached to different controllers is not allowed.\n");
4762 }
4763 return 2;
4764 }
4765 super->orom = find_imsm_capability(hba_name);
4766 if (!super->orom)
4767 return 3;
4768
4769 return 0;
4770 }
4771
4772 /* find_missing - helper routine for load_super_imsm_all that identifies
4773 * disks that have disappeared from the system. This routine relies on
4774 * the mpb being uptodate, which it is at load time.
4775 */
4776 static int find_missing(struct intel_super *super)
4777 {
4778 int i;
4779 struct imsm_super *mpb = super->anchor;
4780 struct dl *dl;
4781 struct imsm_disk *disk;
4782
4783 for (i = 0; i < mpb->num_disks; i++) {
4784 disk = __get_imsm_disk(mpb, i);
4785 dl = serial_to_dl(disk->serial, super);
4786 if (dl)
4787 continue;
4788
4789 dl = xmalloc(sizeof(*dl));
4790 dl->major = 0;
4791 dl->minor = 0;
4792 dl->fd = -1;
4793 dl->devname = xstrdup("missing");
4794 dl->index = i;
4795 serialcpy(dl->serial, disk->serial);
4796 dl->disk = *disk;
4797 dl->e = NULL;
4798 dl->next = super->missing;
4799 super->missing = dl;
4800 }
4801
4802 return 0;
4803 }
4804
4805 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
4806 {
4807 struct intel_disk *idisk = disk_list;
4808
4809 while (idisk) {
4810 if (serialcmp(idisk->disk.serial, serial) == 0)
4811 break;
4812 idisk = idisk->next;
4813 }
4814
4815 return idisk;
4816 }
4817
4818 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
4819 struct intel_super *super,
4820 struct intel_disk **disk_list)
4821 {
4822 struct imsm_disk *d = &super->disks->disk;
4823 struct imsm_super *mpb = super->anchor;
4824 int i, j;
4825
4826 for (i = 0; i < tbl_size; i++) {
4827 struct imsm_super *tbl_mpb = table[i]->anchor;
4828 struct imsm_disk *tbl_d = &table[i]->disks->disk;
4829
4830 if (tbl_mpb->family_num == mpb->family_num) {
4831 if (tbl_mpb->check_sum == mpb->check_sum) {
4832 dprintf("mpb from %d:%d matches %d:%d\n",
4833 super->disks->major,
4834 super->disks->minor,
4835 table[i]->disks->major,
4836 table[i]->disks->minor);
4837 break;
4838 }
4839
4840 if (((is_configured(d) && !is_configured(tbl_d)) ||
4841 is_configured(d) == is_configured(tbl_d)) &&
4842 tbl_mpb->generation_num < mpb->generation_num) {
4843 /* current version of the mpb is a
4844 * better candidate than the one in
4845 * super_table, but copy over "cross
4846 * generational" status
4847 */
4848 struct intel_disk *idisk;
4849
4850 dprintf("mpb from %d:%d replaces %d:%d\n",
4851 super->disks->major,
4852 super->disks->minor,
4853 table[i]->disks->major,
4854 table[i]->disks->minor);
4855
4856 idisk = disk_list_get(tbl_d->serial, *disk_list);
4857 if (idisk && is_failed(&idisk->disk))
4858 tbl_d->status |= FAILED_DISK;
4859 break;
4860 } else {
4861 struct intel_disk *idisk;
4862 struct imsm_disk *disk;
4863
4864 /* tbl_mpb is more up to date, but copy
4865 * over cross generational status before
4866 * returning
4867 */
4868 disk = __serial_to_disk(d->serial, mpb, NULL);
4869 if (disk && is_failed(disk))
4870 d->status |= FAILED_DISK;
4871
4872 idisk = disk_list_get(d->serial, *disk_list);
4873 if (idisk) {
4874 idisk->owner = i;
4875 if (disk && is_configured(disk))
4876 idisk->disk.status |= CONFIGURED_DISK;
4877 }
4878
4879 dprintf("mpb from %d:%d prefer %d:%d\n",
4880 super->disks->major,
4881 super->disks->minor,
4882 table[i]->disks->major,
4883 table[i]->disks->minor);
4884
4885 return tbl_size;
4886 }
4887 }
4888 }
4889
4890 if (i >= tbl_size)
4891 table[tbl_size++] = super;
4892 else
4893 table[i] = super;
4894
4895 /* update/extend the merged list of imsm_disk records */
4896 for (j = 0; j < mpb->num_disks; j++) {
4897 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
4898 struct intel_disk *idisk;
4899
4900 idisk = disk_list_get(disk->serial, *disk_list);
4901 if (idisk) {
4902 idisk->disk.status |= disk->status;
4903 if (is_configured(&idisk->disk) ||
4904 is_failed(&idisk->disk))
4905 idisk->disk.status &= ~(SPARE_DISK);
4906 } else {
4907 idisk = xcalloc(1, sizeof(*idisk));
4908 idisk->owner = IMSM_UNKNOWN_OWNER;
4909 idisk->disk = *disk;
4910 idisk->next = *disk_list;
4911 *disk_list = idisk;
4912 }
4913
4914 if (serialcmp(idisk->disk.serial, d->serial) == 0)
4915 idisk->owner = i;
4916 }
4917
4918 return tbl_size;
4919 }
4920
4921 static struct intel_super *
4922 validate_members(struct intel_super *super, struct intel_disk *disk_list,
4923 const int owner)
4924 {
4925 struct imsm_super *mpb = super->anchor;
4926 int ok_count = 0;
4927 int i;
4928
4929 for (i = 0; i < mpb->num_disks; i++) {
4930 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
4931 struct intel_disk *idisk;
4932
4933 idisk = disk_list_get(disk->serial, disk_list);
4934 if (idisk) {
4935 if (idisk->owner == owner ||
4936 idisk->owner == IMSM_UNKNOWN_OWNER)
4937 ok_count++;
4938 else
4939 dprintf("'%.16s' owner %d != %d\n",
4940 disk->serial, idisk->owner,
4941 owner);
4942 } else {
4943 dprintf("unknown disk %x [%d]: %.16s\n",
4944 __le32_to_cpu(mpb->family_num), i,
4945 disk->serial);
4946 break;
4947 }
4948 }
4949
4950 if (ok_count == mpb->num_disks)
4951 return super;
4952 return NULL;
4953 }
4954
4955 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
4956 {
4957 struct intel_super *s;
4958
4959 for (s = super_list; s; s = s->next) {
4960 if (family_num != s->anchor->family_num)
4961 continue;
4962 pr_err("Conflict, offlining family %#x on '%s'\n",
4963 __le32_to_cpu(family_num), s->disks->devname);
4964 }
4965 }
4966
4967 static struct intel_super *
4968 imsm_thunderdome(struct intel_super **super_list, int len)
4969 {
4970 struct intel_super *super_table[len];
4971 struct intel_disk *disk_list = NULL;
4972 struct intel_super *champion, *spare;
4973 struct intel_super *s, **del;
4974 int tbl_size = 0;
4975 int conflict;
4976 int i;
4977
4978 memset(super_table, 0, sizeof(super_table));
4979 for (s = *super_list; s; s = s->next)
4980 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
4981
4982 for (i = 0; i < tbl_size; i++) {
4983 struct imsm_disk *d;
4984 struct intel_disk *idisk;
4985 struct imsm_super *mpb = super_table[i]->anchor;
4986
4987 s = super_table[i];
4988 d = &s->disks->disk;
4989
4990 /* 'd' must appear in merged disk list for its
4991 * configuration to be valid
4992 */
4993 idisk = disk_list_get(d->serial, disk_list);
4994 if (idisk && idisk->owner == i)
4995 s = validate_members(s, disk_list, i);
4996 else
4997 s = NULL;
4998
4999 if (!s)
5000 dprintf("marking family: %#x from %d:%d offline\n",
5001 mpb->family_num,
5002 super_table[i]->disks->major,
5003 super_table[i]->disks->minor);
5004 super_table[i] = s;
5005 }
5006
5007 /* This is where the mdadm implementation differs from the Windows
5008 * driver which has no strict concept of a container. We can only
5009 * assemble one family from a container, so when returning a prodigal
5010 * array member to this system the code will not be able to disambiguate
5011 * the container contents that should be assembled ("foreign" versus
5012 * "local"). It requires user intervention to set the orig_family_num
5013 * to a new value to establish a new container. The Windows driver in
5014 * this situation fixes up the volume name in place and manages the
5015 * foreign array as an independent entity.
5016 */
5017 s = NULL;
5018 spare = NULL;
5019 conflict = 0;
5020 for (i = 0; i < tbl_size; i++) {
5021 struct intel_super *tbl_ent = super_table[i];
5022 int is_spare = 0;
5023
5024 if (!tbl_ent)
5025 continue;
5026
5027 if (tbl_ent->anchor->num_raid_devs == 0) {
5028 spare = tbl_ent;
5029 is_spare = 1;
5030 }
5031
5032 if (s && !is_spare) {
5033 show_conflicts(tbl_ent->anchor->family_num, *super_list);
5034 conflict++;
5035 } else if (!s && !is_spare)
5036 s = tbl_ent;
5037 }
5038
5039 if (!s)
5040 s = spare;
5041 if (!s) {
5042 champion = NULL;
5043 goto out;
5044 }
5045 champion = s;
5046
5047 if (conflict)
5048 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
5049 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
5050
5051 /* collect all dl's onto 'champion', and update them to
5052 * champion's version of the status
5053 */
5054 for (s = *super_list; s; s = s->next) {
5055 struct imsm_super *mpb = champion->anchor;
5056 struct dl *dl = s->disks;
5057
5058 if (s == champion)
5059 continue;
5060
5061 mpb->attributes |= s->anchor->attributes & MPB_ATTRIB_2TB_DISK;
5062
5063 for (i = 0; i < mpb->num_disks; i++) {
5064 struct imsm_disk *disk;
5065
5066 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
5067 if (disk) {
5068 dl->disk = *disk;
5069 /* only set index on disks that are a member of
5070 * a populated contianer, i.e. one with
5071 * raid_devs
5072 */
5073 if (is_failed(&dl->disk))
5074 dl->index = -2;
5075 else if (is_spare(&dl->disk))
5076 dl->index = -1;
5077 break;
5078 }
5079 }
5080
5081 if (i >= mpb->num_disks) {
5082 struct intel_disk *idisk;
5083
5084 idisk = disk_list_get(dl->serial, disk_list);
5085 if (idisk && is_spare(&idisk->disk) &&
5086 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
5087 dl->index = -1;
5088 else {
5089 dl->index = -2;
5090 continue;
5091 }
5092 }
5093
5094 dl->next = champion->disks;
5095 champion->disks = dl;
5096 s->disks = NULL;
5097 }
5098
5099 /* delete 'champion' from super_list */
5100 for (del = super_list; *del; ) {
5101 if (*del == champion) {
5102 *del = (*del)->next;
5103 break;
5104 } else
5105 del = &(*del)->next;
5106 }
5107 champion->next = NULL;
5108
5109 out:
5110 while (disk_list) {
5111 struct intel_disk *idisk = disk_list;
5112
5113 disk_list = disk_list->next;
5114 free(idisk);
5115 }
5116
5117 return champion;
5118 }
5119
5120 static int
5121 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
5122 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5123 int major, int minor, int keep_fd);
5124 static int
5125 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5126 int *max, int keep_fd);
5127
5128 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
5129 char *devname, struct md_list *devlist,
5130 int keep_fd)
5131 {
5132 struct intel_super *super_list = NULL;
5133 struct intel_super *super = NULL;
5134 int err = 0;
5135 int i = 0;
5136
5137 if (is_fd_valid(fd))
5138 /* 'fd' is an opened container */
5139 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
5140 else
5141 /* get super block from devlist devices */
5142 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
5143 if (err)
5144 goto error;
5145 /* all mpbs enter, maybe one leaves */
5146 super = imsm_thunderdome(&super_list, i);
5147 if (!super) {
5148 err = 1;
5149 goto error;
5150 }
5151
5152 if (find_missing(super) != 0) {
5153 free_imsm(super);
5154 err = 2;
5155 goto error;
5156 }
5157
5158 /* load migration record */
5159 err = load_imsm_migr_rec(super);
5160 if (err == -1) {
5161 /* migration is in progress,
5162 * but migr_rec cannot be loaded,
5163 */
5164 err = 4;
5165 goto error;
5166 }
5167
5168 /* Check migration compatibility */
5169 if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
5170 pr_err("Unsupported migration detected");
5171 if (devname)
5172 fprintf(stderr, " on %s\n", devname);
5173 else
5174 fprintf(stderr, " (IMSM).\n");
5175
5176 err = 5;
5177 goto error;
5178 }
5179
5180 err = 0;
5181
5182 error:
5183 while (super_list) {
5184 struct intel_super *s = super_list;
5185
5186 super_list = super_list->next;
5187 free_imsm(s);
5188 }
5189
5190 if (err)
5191 return err;
5192
5193 *sbp = super;
5194 if (is_fd_valid(fd))
5195 strcpy(st->container_devnm, fd2devnm(fd));
5196 else
5197 st->container_devnm[0] = 0;
5198 if (err == 0 && st->ss == NULL) {
5199 st->ss = &super_imsm;
5200 st->minor_version = 0;
5201 st->max_devs = IMSM_MAX_DEVICES;
5202 }
5203 return 0;
5204 }
5205
5206 static int
5207 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
5208 int *max, int keep_fd)
5209 {
5210 struct md_list *tmpdev;
5211 int err = 0;
5212 int i = 0;
5213
5214 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5215 if (tmpdev->used != 1)
5216 continue;
5217 if (tmpdev->container == 1) {
5218 int lmax = 0;
5219 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
5220 if (!is_fd_valid(fd)) {
5221 pr_err("cannot open device %s: %s\n",
5222 tmpdev->devname, strerror(errno));
5223 err = 8;
5224 goto error;
5225 }
5226 err = get_sra_super_block(fd, super_list,
5227 tmpdev->devname, &lmax,
5228 keep_fd);
5229 i += lmax;
5230 close(fd);
5231 if (err) {
5232 err = 7;
5233 goto error;
5234 }
5235 } else {
5236 int major = major(tmpdev->st_rdev);
5237 int minor = minor(tmpdev->st_rdev);
5238 err = get_super_block(super_list,
5239 NULL,
5240 tmpdev->devname,
5241 major, minor,
5242 keep_fd);
5243 i++;
5244 if (err) {
5245 err = 6;
5246 goto error;
5247 }
5248 }
5249 }
5250 error:
5251 *max = i;
5252 return err;
5253 }
5254
5255 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
5256 int major, int minor, int keep_fd)
5257 {
5258 struct intel_super *s;
5259 char nm[32];
5260 int dfd = -1;
5261 int err = 0;
5262 int retry;
5263
5264 s = alloc_super();
5265 if (!s) {
5266 err = 1;
5267 goto error;
5268 }
5269
5270 sprintf(nm, "%d:%d", major, minor);
5271 dfd = dev_open(nm, O_RDWR);
5272 if (!is_fd_valid(dfd)) {
5273 err = 2;
5274 goto error;
5275 }
5276
5277 if (!get_dev_sector_size(dfd, NULL, &s->sector_size)) {
5278 err = 2;
5279 goto error;
5280 }
5281 find_intel_hba_capability(dfd, s, devname);
5282 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5283
5284 /* retry the load if we might have raced against mdmon */
5285 if (err == 3 && devnm && mdmon_running(devnm))
5286 for (retry = 0; retry < 3; retry++) {
5287 sleep_for(0, MSEC_TO_NSEC(3), true);
5288 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
5289 if (err != 3)
5290 break;
5291 }
5292 error:
5293 if (!err) {
5294 s->next = *super_list;
5295 *super_list = s;
5296 } else {
5297 if (s)
5298 free_imsm(s);
5299 close_fd(&dfd);
5300 }
5301 if (!keep_fd)
5302 close_fd(&dfd);
5303 return err;
5304
5305 }
5306
5307 static int
5308 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
5309 {
5310 struct mdinfo *sra;
5311 char *devnm;
5312 struct mdinfo *sd;
5313 int err = 0;
5314 int i = 0;
5315 sra = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
5316 if (!sra)
5317 return 1;
5318
5319 if (sra->array.major_version != -1 ||
5320 sra->array.minor_version != -2 ||
5321 strcmp(sra->text_version, "imsm") != 0) {
5322 err = 1;
5323 goto error;
5324 }
5325 /* load all mpbs */
5326 devnm = fd2devnm(fd);
5327 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
5328 if (get_super_block(super_list, devnm, devname,
5329 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
5330 err = 7;
5331 goto error;
5332 }
5333 }
5334 error:
5335 sysfs_free(sra);
5336 *max = i;
5337 return err;
5338 }
5339
5340 static int load_container_imsm(struct supertype *st, int fd, char *devname)
5341 {
5342 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
5343 }
5344
5345 static int load_super_imsm(struct supertype *st, int fd, char *devname)
5346 {
5347 struct intel_super *super;
5348 int rv;
5349 int retry;
5350
5351 if (test_partition(fd))
5352 /* IMSM not allowed on partitions */
5353 return 1;
5354
5355 free_super_imsm(st);
5356
5357 super = alloc_super();
5358 if (!super)
5359 return 1;
5360
5361 if (!get_dev_sector_size(fd, NULL, &super->sector_size)) {
5362 free_imsm(super);
5363 return 1;
5364 }
5365 /* Load hba and capabilities if they exist.
5366 * But do not preclude loading metadata in case capabilities or hba are
5367 * non-compliant and ignore_hw_compat is set.
5368 */
5369 rv = find_intel_hba_capability(fd, super, devname);
5370 /* no orom/efi or non-intel hba of the disk */
5371 if (rv != 0 && st->ignore_hw_compat == 0) {
5372 if (devname)
5373 pr_err("No OROM/EFI properties for %s\n", devname);
5374 free_imsm(super);
5375 return 2;
5376 }
5377 rv = load_and_parse_mpb(fd, super, devname, 0);
5378
5379 /* retry the load if we might have raced against mdmon */
5380 if (rv == 3) {
5381 struct mdstat_ent *mdstat = NULL;
5382 char *name = fd2kname(fd);
5383
5384 if (name)
5385 mdstat = mdstat_by_component(name);
5386
5387 if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
5388 for (retry = 0; retry < 3; retry++) {
5389 sleep_for(0, MSEC_TO_NSEC(3), true);
5390 rv = load_and_parse_mpb(fd, super, devname, 0);
5391 if (rv != 3)
5392 break;
5393 }
5394 }
5395
5396 free_mdstat(mdstat);
5397 }
5398
5399 if (rv) {
5400 if (devname)
5401 pr_err("Failed to load all information sections on %s\n", devname);
5402 free_imsm(super);
5403 return rv;
5404 }
5405
5406 st->sb = super;
5407 if (st->ss == NULL) {
5408 st->ss = &super_imsm;
5409 st->minor_version = 0;
5410 st->max_devs = IMSM_MAX_DEVICES;
5411 }
5412
5413 /* load migration record */
5414 if (load_imsm_migr_rec(super) == 0) {
5415 /* Check for unsupported migration features */
5416 if (check_mpb_migr_compatibility(super) != 0) {
5417 pr_err("Unsupported migration detected");
5418 if (devname)
5419 fprintf(stderr, " on %s\n", devname);
5420 else
5421 fprintf(stderr, " (IMSM).\n");
5422 return 3;
5423 }
5424 }
5425
5426 return 0;
5427 }
5428
5429 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
5430 {
5431 if (info->level == 1)
5432 return 128;
5433 return info->chunk_size >> 9;
5434 }
5435
5436 static unsigned long long info_to_blocks_per_member(mdu_array_info_t *info,
5437 unsigned long long size)
5438 {
5439 if (info->level == 1)
5440 return size * 2;
5441 else
5442 return (size * 2) & ~(info_to_blocks_per_strip(info) - 1);
5443 }
5444
5445 static void imsm_update_version_info(struct intel_super *super)
5446 {
5447 /* update the version and attributes */
5448 struct imsm_super *mpb = super->anchor;
5449 char *version;
5450 struct imsm_dev *dev;
5451 struct imsm_map *map;
5452 int i;
5453
5454 for (i = 0; i < mpb->num_raid_devs; i++) {
5455 dev = get_imsm_dev(super, i);
5456 map = get_imsm_map(dev, MAP_0);
5457 if (__le32_to_cpu(dev->size_high) > 0)
5458 mpb->attributes |= MPB_ATTRIB_2TB;
5459
5460 /* FIXME detect when an array spans a port multiplier */
5461 #if 0
5462 mpb->attributes |= MPB_ATTRIB_PM;
5463 #endif
5464
5465 if (mpb->num_raid_devs > 1 ||
5466 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
5467 version = MPB_VERSION_ATTRIBS;
5468 switch (get_imsm_raid_level(map)) {
5469 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
5470 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
5471 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
5472 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
5473 }
5474 } else {
5475 if (map->num_members >= 5)
5476 version = MPB_VERSION_5OR6_DISK_ARRAY;
5477 else if (dev->status == DEV_CLONE_N_GO)
5478 version = MPB_VERSION_CNG;
5479 else if (get_imsm_raid_level(map) == 5)
5480 version = MPB_VERSION_RAID5;
5481 else if (map->num_members >= 3)
5482 version = MPB_VERSION_3OR4_DISK_ARRAY;
5483 else if (get_imsm_raid_level(map) == 1)
5484 version = MPB_VERSION_RAID1;
5485 else
5486 version = MPB_VERSION_RAID0;
5487 }
5488 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
5489 }
5490 }
5491
5492 /**
5493 * imsm_check_name() - check imsm naming criteria.
5494 * @super: &intel_super pointer, not NULL.
5495 * @name: name to check.
5496 * @verbose: verbose level.
5497 *
5498 * Name must be no longer than &MAX_RAID_SERIAL_LEN and must be unique across volumes.
5499 *
5500 * Returns: &true if @name matches, &false otherwise.
5501 */
5502 static bool imsm_is_name_allowed(struct intel_super *super, const char * const name,
5503 const int verbose)
5504 {
5505 struct imsm_super *mpb = super->anchor;
5506 int i;
5507
5508 if (is_string_lq(name, MAX_RAID_SERIAL_LEN + 1) == false) {
5509 pr_vrb("imsm: Name \"%s\" is too long\n", name);
5510 return false;
5511 }
5512
5513 for (i = 0; i < mpb->num_raid_devs; i++) {
5514 struct imsm_dev *dev = get_imsm_dev(super, i);
5515
5516 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
5517 pr_vrb("imsm: Name \"%s\" already exists\n", name);
5518 return false;
5519 }
5520 }
5521
5522 return true;
5523 }
5524
5525 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
5526 struct shape *s, char *name,
5527 char *homehost, int *uuid,
5528 long long data_offset)
5529 {
5530 /* We are creating a volume inside a pre-existing container.
5531 * so st->sb is already set.
5532 */
5533 struct intel_super *super = st->sb;
5534 unsigned int sector_size = super->sector_size;
5535 struct imsm_super *mpb = super->anchor;
5536 struct intel_dev *dv;
5537 struct imsm_dev *dev;
5538 struct imsm_vol *vol;
5539 struct imsm_map *map;
5540 int idx = mpb->num_raid_devs;
5541 int i;
5542 int namelen;
5543 unsigned long long array_blocks;
5544 size_t size_old, size_new;
5545 unsigned int data_disks;
5546 unsigned long long size_per_member;
5547
5548 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
5549 pr_err("This imsm-container already has the maximum of %d volumes\n", super->orom->vpa);
5550 return 0;
5551 }
5552
5553 /* ensure the mpb is large enough for the new data */
5554 size_old = __le32_to_cpu(mpb->mpb_size);
5555 size_new = disks_to_mpb_size(info->nr_disks);
5556 if (size_new > size_old) {
5557 void *mpb_new;
5558 size_t size_round = ROUND_UP(size_new, sector_size);
5559
5560 if (posix_memalign(&mpb_new, sector_size, size_round) != 0) {
5561 pr_err("could not allocate new mpb\n");
5562 return 0;
5563 }
5564 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5565 MIGR_REC_BUF_SECTORS*
5566 MAX_SECTOR_SIZE) != 0) {
5567 pr_err("could not allocate migr_rec buffer\n");
5568 free(super->buf);
5569 free(super);
5570 free(mpb_new);
5571 return 0;
5572 }
5573 memcpy(mpb_new, mpb, size_old);
5574 free(mpb);
5575 mpb = mpb_new;
5576 super->anchor = mpb_new;
5577 mpb->mpb_size = __cpu_to_le32(size_new);
5578 memset(mpb_new + size_old, 0, size_round - size_old);
5579 super->len = size_round;
5580 }
5581 super->current_vol = idx;
5582
5583 /* handle 'failed_disks' by either:
5584 * a) create dummy disk entries in the table if this the first
5585 * volume in the array. We add them here as this is the only
5586 * opportunity to add them. add_to_super_imsm_volume()
5587 * handles the non-failed disks and continues incrementing
5588 * mpb->num_disks.
5589 * b) validate that 'failed_disks' matches the current number
5590 * of missing disks if the container is populated
5591 */
5592 if (super->current_vol == 0) {
5593 mpb->num_disks = 0;
5594 for (i = 0; i < info->failed_disks; i++) {
5595 struct imsm_disk *disk;
5596
5597 mpb->num_disks++;
5598 disk = __get_imsm_disk(mpb, i);
5599 disk->status = CONFIGURED_DISK | FAILED_DISK;
5600 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5601 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
5602 "missing:%d", (__u8)i);
5603 }
5604 find_missing(super);
5605 } else {
5606 int missing = 0;
5607 struct dl *d;
5608
5609 for (d = super->missing; d; d = d->next)
5610 missing++;
5611 if (info->failed_disks > missing) {
5612 pr_err("unable to add 'missing' disk to container\n");
5613 return 0;
5614 }
5615 }
5616
5617 if (imsm_is_name_allowed(super, name, 1) == false)
5618 return 0;
5619
5620 dv = xmalloc(sizeof(*dv));
5621 dev = xcalloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
5622 /*
5623 * Explicitly allow truncating to not confuse gcc's
5624 * -Werror=stringop-truncation
5625 */
5626 namelen = min((int) strlen(name), MAX_RAID_SERIAL_LEN);
5627 memcpy(dev->volume, name, namelen);
5628 array_blocks = calc_array_size(info->level, info->raid_disks,
5629 info->layout, info->chunk_size,
5630 s->size * BLOCKS_PER_KB);
5631 data_disks = get_data_disks(info->level, info->layout,
5632 info->raid_disks);
5633 array_blocks = round_size_to_mb(array_blocks, data_disks);
5634 size_per_member = array_blocks / data_disks;
5635
5636 set_imsm_dev_size(dev, array_blocks);
5637 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
5638 vol = &dev->vol;
5639 vol->migr_state = 0;
5640 set_migr_type(dev, MIGR_INIT);
5641 vol->dirty = !info->state;
5642 set_vol_curr_migr_unit(dev, 0);
5643 map = get_imsm_map(dev, MAP_0);
5644 set_pba_of_lba0(map, super->create_offset);
5645 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
5646 map->failed_disk_num = ~0;
5647 if (info->level > 0)
5648 map->map_state = (info->state ? IMSM_T_STATE_NORMAL
5649 : IMSM_T_STATE_UNINITIALIZED);
5650 else
5651 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
5652 IMSM_T_STATE_NORMAL;
5653 map->ddf = 1;
5654
5655 if (info->level == 1 && info->raid_disks > 2) {
5656 free(dev);
5657 free(dv);
5658 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5659 return 0;
5660 }
5661
5662 map->raid_level = info->level;
5663 if (info->level == 10)
5664 map->raid_level = 1;
5665 set_num_domains(map);
5666
5667 size_per_member += NUM_BLOCKS_DIRTY_STRIPE_REGION;
5668 set_blocks_per_member(map, info_to_blocks_per_member(info,
5669 size_per_member /
5670 BLOCKS_PER_KB));
5671
5672 map->num_members = info->raid_disks;
5673 update_num_data_stripes(map, array_blocks);
5674 for (i = 0; i < map->num_members; i++) {
5675 /* initialized in add_to_super */
5676 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
5677 }
5678 mpb->num_raid_devs++;
5679 mpb->num_raid_devs_created++;
5680 dev->my_vol_raid_dev_num = mpb->num_raid_devs_created;
5681
5682 if (s->consistency_policy <= CONSISTENCY_POLICY_RESYNC) {
5683 dev->rwh_policy = RWH_MULTIPLE_OFF;
5684 } else if (s->consistency_policy == CONSISTENCY_POLICY_PPL) {
5685 dev->rwh_policy = RWH_MULTIPLE_DISTRIBUTED;
5686 } else {
5687 free(dev);
5688 free(dv);
5689 pr_err("imsm does not support consistency policy %s\n",
5690 map_num_s(consistency_policies, s->consistency_policy));
5691 return 0;
5692 }
5693
5694 dv->dev = dev;
5695 dv->index = super->current_vol;
5696 dv->next = super->devlist;
5697 super->devlist = dv;
5698
5699 imsm_update_version_info(super);
5700
5701 return 1;
5702 }
5703
5704 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
5705 struct shape *s, char *name,
5706 char *homehost, int *uuid,
5707 unsigned long long data_offset)
5708 {
5709 /* This is primarily called by Create when creating a new array.
5710 * We will then get add_to_super called for each component, and then
5711 * write_init_super called to write it out to each device.
5712 * For IMSM, Create can create on fresh devices or on a pre-existing
5713 * array.
5714 * To create on a pre-existing array a different method will be called.
5715 * This one is just for fresh drives.
5716 */
5717 struct intel_super *super;
5718 struct imsm_super *mpb;
5719 size_t mpb_size;
5720 char *version;
5721
5722 if (data_offset != INVALID_SECTORS) {
5723 pr_err("data-offset not supported by imsm\n");
5724 return 0;
5725 }
5726
5727 if (st->sb)
5728 return init_super_imsm_volume(st, info, s, name, homehost, uuid,
5729 data_offset);
5730
5731 if (info)
5732 mpb_size = disks_to_mpb_size(info->nr_disks);
5733 else
5734 mpb_size = MAX_SECTOR_SIZE;
5735
5736 super = alloc_super();
5737 if (super &&
5738 posix_memalign(&super->buf, MAX_SECTOR_SIZE, mpb_size) != 0) {
5739 free_imsm(super);
5740 super = NULL;
5741 }
5742 if (!super) {
5743 pr_err("could not allocate superblock\n");
5744 return 0;
5745 }
5746 if (posix_memalign(&super->migr_rec_buf, MAX_SECTOR_SIZE,
5747 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE) != 0) {
5748 pr_err("could not allocate migr_rec buffer\n");
5749 free(super->buf);
5750 free_imsm(super);
5751 return 0;
5752 }
5753 memset(super->buf, 0, mpb_size);
5754 mpb = super->buf;
5755 mpb->mpb_size = __cpu_to_le32(mpb_size);
5756 st->sb = super;
5757
5758 if (info == NULL) {
5759 /* zeroing superblock */
5760 return 0;
5761 }
5762
5763 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
5764
5765 version = (char *) mpb->sig;
5766 strcpy(version, MPB_SIGNATURE);
5767 version += strlen(MPB_SIGNATURE);
5768 strcpy(version, MPB_VERSION_RAID0);
5769
5770 return 1;
5771 }
5772
5773 static int drive_validate_sector_size(struct intel_super *super, struct dl *dl)
5774 {
5775 unsigned int member_sector_size;
5776
5777 if (!is_fd_valid(dl->fd)) {
5778 pr_err("Invalid file descriptor for %s\n", dl->devname);
5779 return 0;
5780 }
5781
5782 if (!get_dev_sector_size(dl->fd, dl->devname, &member_sector_size))
5783 return 0;
5784 if (member_sector_size != super->sector_size)
5785 return 0;
5786 return 1;
5787 }
5788
5789 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
5790 int fd, char *devname)
5791 {
5792 struct intel_super *super = st->sb;
5793 struct imsm_super *mpb = super->anchor;
5794 struct imsm_disk *_disk;
5795 struct imsm_dev *dev;
5796 struct imsm_map *map;
5797 struct dl *dl, *df;
5798 int slot;
5799 int autolayout = 0;
5800
5801 if (!is_fd_valid(fd))
5802 autolayout = 1;
5803
5804 dev = get_imsm_dev(super, super->current_vol);
5805 map = get_imsm_map(dev, MAP_0);
5806
5807 if (! (dk->state & (1<<MD_DISK_SYNC))) {
5808 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5809 devname);
5810 return 1;
5811 }
5812
5813 for (dl = super->disks; dl ; dl = dl->next) {
5814 if (autolayout) {
5815 if (dl->raiddisk == dk->raid_disk)
5816 break;
5817 } else if (dl->major == dk->major && dl->minor == dk->minor)
5818 break;
5819 }
5820
5821 if (!dl) {
5822 if (!autolayout)
5823 pr_err("%s is not a member of the same container.\n",
5824 devname);
5825 return 1;
5826 }
5827
5828 if (!autolayout && super->current_vol > 0) {
5829 int _slot = get_disk_slot_in_dev(super, 0, dl->index);
5830
5831 if (_slot != dk->raid_disk) {
5832 pr_err("Member %s is in %d slot for the first volume, but is in %d slot for a new volume.\n",
5833 dl->devname, _slot, dk->raid_disk);
5834 pr_err("Raid members are in different order than for the first volume, aborting.\n");
5835 return 1;
5836 }
5837 }
5838
5839 if (mpb->num_disks == 0)
5840 if (!get_dev_sector_size(dl->fd, dl->devname,
5841 &super->sector_size))
5842 return 1;
5843
5844 if (!drive_validate_sector_size(super, dl)) {
5845 pr_err("Combining drives of different sector size in one volume is not allowed\n");
5846 return 1;
5847 }
5848
5849 /* add a pristine spare to the metadata */
5850 if (dl->index < 0) {
5851 dl->index = super->anchor->num_disks;
5852 super->anchor->num_disks++;
5853 }
5854 /* Check the device has not already been added */
5855 slot = get_imsm_disk_slot(map, dl->index);
5856 if (slot >= 0 &&
5857 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
5858 pr_err("%s has been included in this array twice\n",
5859 devname);
5860 return 1;
5861 }
5862 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
5863 dl->disk.status = CONFIGURED_DISK;
5864
5865 /* update size of 'missing' disks to be at least as large as the
5866 * largest acitve member (we only have dummy missing disks when
5867 * creating the first volume)
5868 */
5869 if (super->current_vol == 0) {
5870 for (df = super->missing; df; df = df->next) {
5871 if (total_blocks(&dl->disk) > total_blocks(&df->disk))
5872 set_total_blocks(&df->disk, total_blocks(&dl->disk));
5873 _disk = __get_imsm_disk(mpb, df->index);
5874 *_disk = df->disk;
5875 }
5876 }
5877
5878 /* refresh unset/failed slots to point to valid 'missing' entries */
5879 for (df = super->missing; df; df = df->next)
5880 for (slot = 0; slot < mpb->num_disks; slot++) {
5881 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
5882
5883 if ((ord & IMSM_ORD_REBUILD) == 0)
5884 continue;
5885 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
5886 if (is_gen_migration(dev)) {
5887 struct imsm_map *map2 = get_imsm_map(dev,
5888 MAP_1);
5889 int slot2 = get_imsm_disk_slot(map2, df->index);
5890 if (slot2 < map2->num_members && slot2 >= 0) {
5891 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
5892 slot2,
5893 MAP_1);
5894 if ((unsigned)df->index ==
5895 ord_to_idx(ord2))
5896 set_imsm_ord_tbl_ent(map2,
5897 slot2,
5898 df->index |
5899 IMSM_ORD_REBUILD);
5900 }
5901 }
5902 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
5903 break;
5904 }
5905
5906 /* if we are creating the first raid device update the family number */
5907 if (super->current_vol == 0) {
5908 __u32 sum;
5909 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
5910
5911 _disk = __get_imsm_disk(mpb, dl->index);
5912 if (!_disk) {
5913 pr_err("BUG mpb setup error\n");
5914 return 1;
5915 }
5916 *_dev = *dev;
5917 *_disk = dl->disk;
5918 sum = random32();
5919 sum += __gen_imsm_checksum(mpb);
5920 mpb->family_num = __cpu_to_le32(sum);
5921 mpb->orig_family_num = mpb->family_num;
5922 mpb->creation_time = __cpu_to_le64((__u64)time(NULL));
5923 }
5924 super->current_disk = dl;
5925 return 0;
5926 }
5927
5928 /* mark_spare()
5929 * Function marks disk as spare and restores disk serial
5930 * in case it was previously marked as failed by takeover operation
5931 * reruns:
5932 * -1 : critical error
5933 * 0 : disk is marked as spare but serial is not set
5934 * 1 : success
5935 */
5936 int mark_spare(struct dl *disk)
5937 {
5938 __u8 serial[MAX_RAID_SERIAL_LEN];
5939 int ret_val = -1;
5940
5941 if (!disk)
5942 return ret_val;
5943
5944 ret_val = 0;
5945 if (!imsm_read_serial(disk->fd, NULL, serial, MAX_RAID_SERIAL_LEN)) {
5946 /* Restore disk serial number, because takeover marks disk
5947 * as failed and adds to serial ':0' before it becomes
5948 * a spare disk.
5949 */
5950 serialcpy(disk->serial, serial);
5951 serialcpy(disk->disk.serial, serial);
5952 ret_val = 1;
5953 }
5954 disk->disk.status = SPARE_DISK;
5955 disk->index = -1;
5956
5957 return ret_val;
5958 }
5959
5960
5961 static int write_super_imsm_spare(struct intel_super *super, struct dl *d);
5962
5963 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
5964 int fd, char *devname,
5965 unsigned long long data_offset)
5966 {
5967 struct intel_super *super = st->sb;
5968 struct dl *dd;
5969 unsigned long long size;
5970 unsigned int member_sector_size;
5971 __u32 id;
5972 int rv;
5973 struct stat stb;
5974
5975 /* If we are on an RAID enabled platform check that the disk is
5976 * attached to the raid controller.
5977 * We do not need to test disks attachment for container based additions,
5978 * they shall be already tested when container was created/assembled.
5979 */
5980 rv = find_intel_hba_capability(fd, super, devname);
5981 /* no orom/efi or non-intel hba of the disk */
5982 if (rv != 0) {
5983 dprintf("capability: %p fd: %d ret: %d\n",
5984 super->orom, fd, rv);
5985 return 1;
5986 }
5987
5988 if (super->current_vol >= 0)
5989 return add_to_super_imsm_volume(st, dk, fd, devname);
5990
5991 fstat(fd, &stb);
5992 dd = xcalloc(sizeof(*dd), 1);
5993 dd->major = major(stb.st_rdev);
5994 dd->minor = minor(stb.st_rdev);
5995 dd->devname = devname ? xstrdup(devname) : NULL;
5996 dd->fd = fd;
5997 dd->e = NULL;
5998 dd->action = DISK_ADD;
5999 rv = imsm_read_serial(fd, devname, dd->serial, MAX_RAID_SERIAL_LEN);
6000 if (rv) {
6001 pr_err("failed to retrieve scsi serial, aborting\n");
6002 __free_imsm_disk(dd, 0);
6003 abort();
6004 }
6005
6006 if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
6007 (super->hba->type == SYS_DEV_VMD))) {
6008 int i;
6009 char cntrl_path[PATH_MAX];
6010 char *cntrl_name;
6011 char pci_dev_path[PATH_MAX];
6012
6013 if (!diskfd_to_devpath(fd, 2, pci_dev_path) ||
6014 !diskfd_to_devpath(fd, 1, cntrl_path)) {
6015 pr_err("failed to get dev paths, aborting\n");
6016 __free_imsm_disk(dd, 0);
6017 return 1;
6018 }
6019
6020 cntrl_name = basename(cntrl_path);
6021 if (is_multipath_nvme(fd))
6022 pr_err("%s controller supports Multi-Path I/O, Intel (R) VROC does not support multipathing\n",
6023 cntrl_name);
6024
6025 if (devpath_to_vendor(pci_dev_path) == 0x8086) {
6026 /*
6027 * If Intel's NVMe drive has serial ended with
6028 * "-A","-B","-1" or "-2" it means that this is "x8"
6029 * device (double drive on single PCIe card).
6030 * User should be warned about potential data loss.
6031 */
6032 for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
6033 /* Skip empty character at the end */
6034 if (dd->serial[i] == 0)
6035 continue;
6036
6037 if (((dd->serial[i] == 'A') ||
6038 (dd->serial[i] == 'B') ||
6039 (dd->serial[i] == '1') ||
6040 (dd->serial[i] == '2')) &&
6041 (dd->serial[i-1] == '-'))
6042 pr_err("\tThe action you are about to take may put your data at risk.\n"
6043 "\tPlease note that x8 devices may consist of two separate x4 devices "
6044 "located on a single PCIe port.\n"
6045 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
6046 break;
6047 }
6048 } else if (super->hba->type == SYS_DEV_VMD && super->orom &&
6049 !imsm_orom_has_tpv_support(super->orom)) {
6050 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
6051 "\tPlease refer to Intel(R) RSTe/VROC user guide.\n");
6052 __free_imsm_disk(dd, 0);
6053 return 1;
6054 }
6055 }
6056
6057 get_dev_size(fd, NULL, &size);
6058 if (!get_dev_sector_size(fd, NULL, &member_sector_size)) {
6059 __free_imsm_disk(dd, 0);
6060 return 1;
6061 }
6062
6063 if (super->sector_size == 0) {
6064 /* this a first device, so sector_size is not set yet */
6065 super->sector_size = member_sector_size;
6066 }
6067
6068 /* clear migr_rec when adding disk to container */
6069 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6070 if (lseek64(fd, size - MIGR_REC_SECTOR_POSITION*member_sector_size,
6071 SEEK_SET) >= 0) {
6072 if ((unsigned int)write(fd, super->migr_rec_buf,
6073 MIGR_REC_BUF_SECTORS*member_sector_size) !=
6074 MIGR_REC_BUF_SECTORS*member_sector_size)
6075 perror("Write migr_rec failed");
6076 }
6077
6078 size /= 512;
6079 serialcpy(dd->disk.serial, dd->serial);
6080 set_total_blocks(&dd->disk, size);
6081 if (__le32_to_cpu(dd->disk.total_blocks_hi) > 0) {
6082 struct imsm_super *mpb = super->anchor;
6083 mpb->attributes |= MPB_ATTRIB_2TB_DISK;
6084 }
6085 mark_spare(dd);
6086 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
6087 dd->disk.scsi_id = __cpu_to_le32(id);
6088 else
6089 dd->disk.scsi_id = __cpu_to_le32(0);
6090
6091 if (st->update_tail) {
6092 dd->next = super->disk_mgmt_list;
6093 super->disk_mgmt_list = dd;
6094 } else {
6095 /* this is called outside of mdmon
6096 * write initial spare metadata
6097 * mdmon will overwrite it.
6098 */
6099 dd->next = super->disks;
6100 super->disks = dd;
6101 write_super_imsm_spare(super, dd);
6102 }
6103
6104 return 0;
6105 }
6106
6107 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
6108 {
6109 struct intel_super *super = st->sb;
6110 struct dl *dd;
6111
6112 /* remove from super works only in mdmon - for communication
6113 * manager - monitor. Check if communication memory buffer
6114 * is prepared.
6115 */
6116 if (!st->update_tail) {
6117 pr_err("shall be used in mdmon context only\n");
6118 return 1;
6119 }
6120 dd = xcalloc(1, sizeof(*dd));
6121 dd->major = dk->major;
6122 dd->minor = dk->minor;
6123 dd->fd = -1;
6124 mark_spare(dd);
6125 dd->action = DISK_REMOVE;
6126
6127 dd->next = super->disk_mgmt_list;
6128 super->disk_mgmt_list = dd;
6129
6130 return 0;
6131 }
6132
6133 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
6134
6135 static union {
6136 char buf[MAX_SECTOR_SIZE];
6137 struct imsm_super anchor;
6138 } spare_record __attribute__ ((aligned(MAX_SECTOR_SIZE)));
6139
6140
6141 static int write_super_imsm_spare(struct intel_super *super, struct dl *d)
6142 {
6143 struct imsm_super *mpb = super->anchor;
6144 struct imsm_super *spare = &spare_record.anchor;
6145 __u32 sum;
6146
6147 if (d->index != -1)
6148 return 1;
6149
6150 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super));
6151 spare->generation_num = __cpu_to_le32(1UL);
6152 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
6153 spare->num_disks = 1;
6154 spare->num_raid_devs = 0;
6155 spare->cache_size = mpb->cache_size;
6156 spare->pwr_cycle_count = __cpu_to_le32(1);
6157
6158 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
6159 MPB_SIGNATURE MPB_VERSION_RAID0);
6160
6161 spare->disk[0] = d->disk;
6162 if (__le32_to_cpu(d->disk.total_blocks_hi) > 0)
6163 spare->attributes |= MPB_ATTRIB_2TB_DISK;
6164
6165 if (super->sector_size == 4096)
6166 convert_to_4k_imsm_disk(&spare->disk[0]);
6167
6168 sum = __gen_imsm_checksum(spare);
6169 spare->family_num = __cpu_to_le32(sum);
6170 spare->orig_family_num = 0;
6171 sum = __gen_imsm_checksum(spare);
6172 spare->check_sum = __cpu_to_le32(sum);
6173
6174 if (store_imsm_mpb(d->fd, spare)) {
6175 pr_err("failed for device %d:%d %s\n",
6176 d->major, d->minor, strerror(errno));
6177 return 1;
6178 }
6179
6180 return 0;
6181 }
6182 /* spare records have their own family number and do not have any defined raid
6183 * devices
6184 */
6185 static int write_super_imsm_spares(struct intel_super *super, int doclose)
6186 {
6187 struct dl *d;
6188
6189 for (d = super->disks; d; d = d->next) {
6190 if (d->index != -1)
6191 continue;
6192
6193 if (write_super_imsm_spare(super, d))
6194 return 1;
6195
6196 if (doclose)
6197 close_fd(&d->fd);
6198 }
6199
6200 return 0;
6201 }
6202
6203 static int write_super_imsm(struct supertype *st, int doclose)
6204 {
6205 struct intel_super *super = st->sb;
6206 unsigned int sector_size = super->sector_size;
6207 struct imsm_super *mpb = super->anchor;
6208 struct dl *d;
6209 __u32 generation;
6210 __u32 sum;
6211 int spares = 0;
6212 int i;
6213 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
6214 int num_disks = 0;
6215 int clear_migration_record = 1;
6216 __u32 bbm_log_size;
6217
6218 /* 'generation' is incremented everytime the metadata is written */
6219 generation = __le32_to_cpu(mpb->generation_num);
6220 generation++;
6221 mpb->generation_num = __cpu_to_le32(generation);
6222
6223 /* fix up cases where previous mdadm releases failed to set
6224 * orig_family_num
6225 */
6226 if (mpb->orig_family_num == 0)
6227 mpb->orig_family_num = mpb->family_num;
6228
6229 for (d = super->disks; d; d = d->next) {
6230 if (d->index == -1)
6231 spares++;
6232 else {
6233 mpb->disk[d->index] = d->disk;
6234 num_disks++;
6235 }
6236 }
6237 for (d = super->missing; d; d = d->next) {
6238 mpb->disk[d->index] = d->disk;
6239 num_disks++;
6240 }
6241 mpb->num_disks = num_disks;
6242 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
6243
6244 for (i = 0; i < mpb->num_raid_devs; i++) {
6245 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
6246 struct imsm_dev *dev2 = get_imsm_dev(super, i);
6247
6248 imsm_copy_dev(dev, dev2);
6249 mpb_size += sizeof_imsm_dev(dev, 0);
6250
6251 if (is_gen_migration(dev2))
6252 clear_migration_record = 0;
6253 }
6254
6255 bbm_log_size = get_imsm_bbm_log_size(super->bbm_log);
6256
6257 if (bbm_log_size) {
6258 memcpy((void *)mpb + mpb_size, super->bbm_log, bbm_log_size);
6259 mpb->attributes |= MPB_ATTRIB_BBM;
6260 } else
6261 mpb->attributes &= ~MPB_ATTRIB_BBM;
6262
6263 super->anchor->bbm_log_size = __cpu_to_le32(bbm_log_size);
6264 mpb_size += bbm_log_size;
6265 mpb->mpb_size = __cpu_to_le32(mpb_size);
6266
6267 #ifdef DEBUG
6268 assert(super->len == 0 || mpb_size <= super->len);
6269 #endif
6270
6271 /* recalculate checksum */
6272 sum = __gen_imsm_checksum(mpb);
6273 mpb->check_sum = __cpu_to_le32(sum);
6274
6275 if (super->clean_migration_record_by_mdmon) {
6276 clear_migration_record = 1;
6277 super->clean_migration_record_by_mdmon = 0;
6278 }
6279 if (clear_migration_record)
6280 memset(super->migr_rec_buf, 0,
6281 MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
6282
6283 if (sector_size == 4096)
6284 convert_to_4k(super);
6285
6286 /* write the mpb for disks that compose raid devices */
6287 for (d = super->disks; d ; d = d->next) {
6288 if (d->index < 0 || is_failed(&d->disk))
6289 continue;
6290
6291 if (clear_migration_record) {
6292 unsigned long long dsize;
6293
6294 get_dev_size(d->fd, NULL, &dsize);
6295 if (lseek64(d->fd, dsize - sector_size,
6296 SEEK_SET) >= 0) {
6297 if ((unsigned int)write(d->fd,
6298 super->migr_rec_buf,
6299 MIGR_REC_BUF_SECTORS*sector_size) !=
6300 MIGR_REC_BUF_SECTORS*sector_size)
6301 perror("Write migr_rec failed");
6302 }
6303 }
6304
6305 if (store_imsm_mpb(d->fd, mpb))
6306 fprintf(stderr,
6307 "failed for device %d:%d (fd: %d)%s\n",
6308 d->major, d->minor,
6309 d->fd, strerror(errno));
6310
6311 if (doclose)
6312 close_fd(&d->fd);
6313 }
6314
6315 if (spares)
6316 return write_super_imsm_spares(super, doclose);
6317
6318 return 0;
6319 }
6320
6321 static int create_array(struct supertype *st, int dev_idx)
6322 {
6323 size_t len;
6324 struct imsm_update_create_array *u;
6325 struct intel_super *super = st->sb;
6326 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
6327 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6328 struct disk_info *inf;
6329 struct imsm_disk *disk;
6330 int i;
6331
6332 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
6333 sizeof(*inf) * map->num_members;
6334 u = xmalloc(len);
6335 u->type = update_create_array;
6336 u->dev_idx = dev_idx;
6337 imsm_copy_dev(&u->dev, dev);
6338 inf = get_disk_info(u);
6339 for (i = 0; i < map->num_members; i++) {
6340 int idx = get_imsm_disk_idx(dev, i, MAP_X);
6341
6342 disk = get_imsm_disk(super, idx);
6343 if (!disk)
6344 disk = get_imsm_missing(super, idx);
6345 serialcpy(inf[i].serial, disk->serial);
6346 }
6347 append_metadata_update(st, u, len);
6348
6349 return 0;
6350 }
6351
6352 static int mgmt_disk(struct supertype *st)
6353 {
6354 struct intel_super *super = st->sb;
6355 size_t len;
6356 struct imsm_update_add_remove_disk *u;
6357
6358 if (!super->disk_mgmt_list)
6359 return 0;
6360
6361 len = sizeof(*u);
6362 u = xmalloc(len);
6363 u->type = update_add_remove_disk;
6364 append_metadata_update(st, u, len);
6365
6366 return 0;
6367 }
6368
6369 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
6370
6371 static int write_ppl_header(unsigned long long ppl_sector, int fd, void *buf)
6372 {
6373 struct ppl_header *ppl_hdr = buf;
6374 int ret;
6375
6376 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
6377
6378 if (lseek64(fd, ppl_sector * 512, SEEK_SET) < 0) {
6379 ret = -errno;
6380 perror("Failed to seek to PPL header location");
6381 return ret;
6382 }
6383
6384 if (write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6385 ret = -errno;
6386 perror("Write PPL header failed");
6387 return ret;
6388 }
6389
6390 fsync(fd);
6391
6392 return 0;
6393 }
6394
6395 static int write_init_ppl_imsm(struct supertype *st, struct mdinfo *info, int fd)
6396 {
6397 struct intel_super *super = st->sb;
6398 void *buf;
6399 struct ppl_header *ppl_hdr;
6400 int ret;
6401
6402 /* first clear entire ppl space */
6403 ret = zero_disk_range(fd, info->ppl_sector, info->ppl_size);
6404 if (ret)
6405 return ret;
6406
6407 ret = posix_memalign(&buf, MAX_SECTOR_SIZE, PPL_HEADER_SIZE);
6408 if (ret) {
6409 pr_err("Failed to allocate PPL header buffer\n");
6410 return -ret;
6411 }
6412
6413 memset(buf, 0, PPL_HEADER_SIZE);
6414 ppl_hdr = buf;
6415 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
6416 ppl_hdr->signature = __cpu_to_le32(super->anchor->orig_family_num);
6417
6418 if (info->mismatch_cnt) {
6419 /*
6420 * We are overwriting an invalid ppl. Make one entry with wrong
6421 * checksum to prevent the kernel from skipping resync.
6422 */
6423 ppl_hdr->entries_count = __cpu_to_le32(1);
6424 ppl_hdr->entries[0].checksum = ~0;
6425 }
6426
6427 ret = write_ppl_header(info->ppl_sector, fd, buf);
6428
6429 free(buf);
6430 return ret;
6431 }
6432
6433 static int is_rebuilding(struct imsm_dev *dev);
6434
6435 static int validate_ppl_imsm(struct supertype *st, struct mdinfo *info,
6436 struct mdinfo *disk)
6437 {
6438 struct intel_super *super = st->sb;
6439 struct dl *d;
6440 void *buf_orig, *buf, *buf_prev = NULL;
6441 int ret = 0;
6442 struct ppl_header *ppl_hdr = NULL;
6443 __u32 crc;
6444 struct imsm_dev *dev;
6445 __u32 idx;
6446 unsigned int i;
6447 unsigned long long ppl_offset = 0;
6448 unsigned long long prev_gen_num = 0;
6449
6450 if (disk->disk.raid_disk < 0)
6451 return 0;
6452
6453 dev = get_imsm_dev(super, info->container_member);
6454 idx = get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_0);
6455 d = get_imsm_dl_disk(super, idx);
6456
6457 if (!d || d->index < 0 || is_failed(&d->disk))
6458 return 0;
6459
6460 if (posix_memalign(&buf_orig, MAX_SECTOR_SIZE, PPL_HEADER_SIZE * 2)) {
6461 pr_err("Failed to allocate PPL header buffer\n");
6462 return -1;
6463 }
6464 buf = buf_orig;
6465
6466 ret = 1;
6467 while (ppl_offset < MULTIPLE_PPL_AREA_SIZE_IMSM) {
6468 void *tmp;
6469
6470 dprintf("Checking potential PPL at offset: %llu\n", ppl_offset);
6471
6472 if (lseek64(d->fd, info->ppl_sector * 512 + ppl_offset,
6473 SEEK_SET) < 0) {
6474 perror("Failed to seek to PPL header location");
6475 ret = -1;
6476 break;
6477 }
6478
6479 if (read(d->fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
6480 perror("Read PPL header failed");
6481 ret = -1;
6482 break;
6483 }
6484
6485 ppl_hdr = buf;
6486
6487 crc = __le32_to_cpu(ppl_hdr->checksum);
6488 ppl_hdr->checksum = 0;
6489
6490 if (crc != ~crc32c_le(~0, buf, PPL_HEADER_SIZE)) {
6491 dprintf("Wrong PPL header checksum on %s\n",
6492 d->devname);
6493 break;
6494 }
6495
6496 if (prev_gen_num > __le64_to_cpu(ppl_hdr->generation)) {
6497 /* previous was newest, it was already checked */
6498 break;
6499 }
6500
6501 if ((__le32_to_cpu(ppl_hdr->signature) !=
6502 super->anchor->orig_family_num)) {
6503 dprintf("Wrong PPL header signature on %s\n",
6504 d->devname);
6505 ret = 1;
6506 break;
6507 }
6508
6509 ret = 0;
6510 prev_gen_num = __le64_to_cpu(ppl_hdr->generation);
6511
6512 ppl_offset += PPL_HEADER_SIZE;
6513 for (i = 0; i < __le32_to_cpu(ppl_hdr->entries_count); i++)
6514 ppl_offset +=
6515 __le32_to_cpu(ppl_hdr->entries[i].pp_size);
6516
6517 if (!buf_prev)
6518 buf_prev = buf + PPL_HEADER_SIZE;
6519 tmp = buf_prev;
6520 buf_prev = buf;
6521 buf = tmp;
6522 }
6523
6524 if (buf_prev) {
6525 buf = buf_prev;
6526 ppl_hdr = buf_prev;
6527 }
6528
6529 /*
6530 * Update metadata to use mutliple PPLs area (1MB).
6531 * This is done once for all RAID members
6532 */
6533 if (info->consistency_policy == CONSISTENCY_POLICY_PPL &&
6534 info->ppl_size != (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9)) {
6535 char subarray[20];
6536 struct mdinfo *member_dev;
6537
6538 sprintf(subarray, "%d", info->container_member);
6539
6540 if (mdmon_running(st->container_devnm))
6541 st->update_tail = &st->updates;
6542
6543 if (st->ss->update_subarray(st, subarray, UOPT_PPL, NULL)) {
6544 pr_err("Failed to update subarray %s\n",
6545 subarray);
6546 } else {
6547 if (st->update_tail)
6548 flush_metadata_updates(st);
6549 else
6550 st->ss->sync_metadata(st);
6551 info->ppl_size = (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6552 for (member_dev = info->devs; member_dev;
6553 member_dev = member_dev->next)
6554 member_dev->ppl_size =
6555 (MULTIPLE_PPL_AREA_SIZE_IMSM >> 9);
6556 }
6557 }
6558
6559 if (ret == 1) {
6560 struct imsm_map *map = get_imsm_map(dev, MAP_X);
6561
6562 if (map->map_state == IMSM_T_STATE_UNINITIALIZED ||
6563 (map->map_state == IMSM_T_STATE_NORMAL &&
6564 !(dev->vol.dirty & RAIDVOL_DIRTY)) ||
6565 (is_rebuilding(dev) &&
6566 vol_curr_migr_unit(dev) == 0 &&
6567 get_imsm_disk_idx(dev, disk->disk.raid_disk, MAP_1) != idx))
6568 ret = st->ss->write_init_ppl(st, info, d->fd);
6569 else
6570 info->mismatch_cnt++;
6571 } else if (ret == 0 &&
6572 ppl_hdr->entries_count == 0 &&
6573 is_rebuilding(dev) &&
6574 info->resync_start == 0) {
6575 /*
6576 * The header has no entries - add a single empty entry and
6577 * rewrite the header to prevent the kernel from going into
6578 * resync after an interrupted rebuild.
6579 */
6580 ppl_hdr->entries_count = __cpu_to_le32(1);
6581 ret = write_ppl_header(info->ppl_sector, d->fd, buf);
6582 }
6583
6584 free(buf_orig);
6585
6586 return ret;
6587 }
6588
6589 static int write_init_ppl_imsm_all(struct supertype *st, struct mdinfo *info)
6590 {
6591 struct intel_super *super = st->sb;
6592 struct dl *d;
6593 int ret = 0;
6594
6595 if (info->consistency_policy != CONSISTENCY_POLICY_PPL ||
6596 info->array.level != 5)
6597 return 0;
6598
6599 for (d = super->disks; d ; d = d->next) {
6600 if (d->index < 0 || is_failed(&d->disk))
6601 continue;
6602
6603 ret = st->ss->write_init_ppl(st, info, d->fd);
6604 if (ret)
6605 break;
6606 }
6607
6608 return ret;
6609 }
6610
6611 /*******************************************************************************
6612 * Function: write_init_bitmap_imsm_vol
6613 * Description: Write a bitmap header and prepares the area for the bitmap.
6614 * Parameters:
6615 * st : supertype information
6616 * vol_idx : the volume index to use
6617 *
6618 * Returns:
6619 * 0 : success
6620 * -1 : fail
6621 ******************************************************************************/
6622 static int write_init_bitmap_imsm_vol(struct supertype *st, int vol_idx)
6623 {
6624 struct intel_super *super = st->sb;
6625 int prev_current_vol = super->current_vol;
6626 struct dl *d;
6627 int ret = 0;
6628
6629 super->current_vol = vol_idx;
6630 for (d = super->disks; d; d = d->next) {
6631 if (d->index < 0 || is_failed(&d->disk))
6632 continue;
6633 ret = st->ss->write_bitmap(st, d->fd, NoUpdate);
6634 if (ret)
6635 break;
6636 }
6637 super->current_vol = prev_current_vol;
6638 return ret;
6639 }
6640
6641 /*******************************************************************************
6642 * Function: write_init_bitmap_imsm_all
6643 * Description: Write a bitmap header and prepares the area for the bitmap.
6644 * Operation is executed for volumes with CONSISTENCY_POLICY_BITMAP.
6645 * Parameters:
6646 * st : supertype information
6647 * info : info about the volume where the bitmap should be written
6648 * vol_idx : the volume index to use
6649 *
6650 * Returns:
6651 * 0 : success
6652 * -1 : fail
6653 ******************************************************************************/
6654 static int write_init_bitmap_imsm_all(struct supertype *st, struct mdinfo *info,
6655 int vol_idx)
6656 {
6657 int ret = 0;
6658
6659 if (info && (info->consistency_policy == CONSISTENCY_POLICY_BITMAP))
6660 ret = write_init_bitmap_imsm_vol(st, vol_idx);
6661
6662 return ret;
6663 }
6664
6665 static int write_init_super_imsm(struct supertype *st)
6666 {
6667 struct intel_super *super = st->sb;
6668 int current_vol = super->current_vol;
6669 int rv = 0;
6670 struct mdinfo info;
6671
6672 getinfo_super_imsm(st, &info, NULL);
6673
6674 /* we are done with current_vol reset it to point st at the container */
6675 super->current_vol = -1;
6676
6677 if (st->update_tail) {
6678 /* queue the recently created array / added disk
6679 * as a metadata update */
6680
6681 /* determine if we are creating a volume or adding a disk */
6682 if (current_vol < 0) {
6683 /* in the mgmt (add/remove) disk case we are running
6684 * in mdmon context, so don't close fd's
6685 */
6686 rv = mgmt_disk(st);
6687 } else {
6688 /* adding the second volume to the array */
6689 rv = write_init_ppl_imsm_all(st, &info);
6690 if (!rv)
6691 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6692 if (!rv)
6693 rv = create_array(st, current_vol);
6694 }
6695 } else {
6696 struct dl *d;
6697 for (d = super->disks; d; d = d->next)
6698 Kill(d->devname, NULL, 0, -1, 1);
6699 if (current_vol >= 0) {
6700 rv = write_init_ppl_imsm_all(st, &info);
6701 if (!rv)
6702 rv = write_init_bitmap_imsm_all(st, &info, current_vol);
6703 }
6704
6705 if (!rv)
6706 rv = write_super_imsm(st, 1);
6707 }
6708
6709 return rv;
6710 }
6711
6712 static int store_super_imsm(struct supertype *st, int fd)
6713 {
6714 struct intel_super *super = st->sb;
6715 struct imsm_super *mpb = super ? super->anchor : NULL;
6716
6717 if (!mpb)
6718 return 1;
6719
6720 if (super->sector_size == 4096)
6721 convert_to_4k(super);
6722 return store_imsm_mpb(fd, mpb);
6723 }
6724
6725 static int validate_geometry_imsm_container(struct supertype *st, int level,
6726 int raiddisks,
6727 unsigned long long data_offset,
6728 char *dev,
6729 unsigned long long *freesize,
6730 int verbose)
6731 {
6732 int fd;
6733 unsigned long long ldsize;
6734 struct intel_super *super = NULL;
6735 int rv = 0;
6736
6737 if (!is_container(level))
6738 return 0;
6739 if (!dev)
6740 return 1;
6741
6742 fd = dev_open(dev, O_RDONLY|O_EXCL);
6743 if (!is_fd_valid(fd)) {
6744 pr_vrb("imsm: Cannot open %s: %s\n", dev, strerror(errno));
6745 return 0;
6746 }
6747 if (!get_dev_size(fd, dev, &ldsize))
6748 goto exit;
6749
6750 /* capabilities retrieve could be possible
6751 * note that there is no fd for the disks in array.
6752 */
6753 super = alloc_super();
6754 if (!super)
6755 goto exit;
6756
6757 if (!get_dev_sector_size(fd, NULL, &super->sector_size))
6758 goto exit;
6759
6760 rv = find_intel_hba_capability(fd, super, verbose > 0 ? dev : NULL);
6761 if (rv != 0) {
6762 #if DEBUG
6763 char str[256];
6764 fd2devname(fd, str);
6765 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6766 fd, str, super->orom, rv, raiddisks);
6767 #endif
6768 /* no orom/efi or non-intel hba of the disk */
6769 rv = 0;
6770 goto exit;
6771 }
6772 if (super->orom) {
6773 if (raiddisks > super->orom->tds) {
6774 if (verbose)
6775 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6776 raiddisks, super->orom->tds);
6777 goto exit;
6778 }
6779 if ((super->orom->attr & IMSM_OROM_ATTR_2TB_DISK) == 0 &&
6780 (ldsize >> 9) >> 32 > 0) {
6781 if (verbose)
6782 pr_err("%s exceeds maximum platform supported size\n", dev);
6783 goto exit;
6784 }
6785
6786 if (super->hba->type == SYS_DEV_VMD ||
6787 super->hba->type == SYS_DEV_NVME) {
6788 if (!imsm_is_nvme_namespace_supported(fd, 1)) {
6789 if (verbose)
6790 pr_err("NVMe namespace %s is not supported by IMSM\n",
6791 basename(dev));
6792 goto exit;
6793 }
6794 }
6795 }
6796 if (freesize)
6797 *freesize = avail_size_imsm(st, ldsize >> 9, data_offset);
6798 rv = 1;
6799 exit:
6800 if (super)
6801 free_imsm(super);
6802 close(fd);
6803
6804 return rv;
6805 }
6806
6807 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
6808 {
6809 const unsigned long long base_start = e[*idx].start;
6810 unsigned long long end = base_start + e[*idx].size;
6811 int i;
6812
6813 if (base_start == end)
6814 return 0;
6815
6816 *idx = *idx + 1;
6817 for (i = *idx; i < num_extents; i++) {
6818 /* extend overlapping extents */
6819 if (e[i].start >= base_start &&
6820 e[i].start <= end) {
6821 if (e[i].size == 0)
6822 return 0;
6823 if (e[i].start + e[i].size > end)
6824 end = e[i].start + e[i].size;
6825 } else if (e[i].start > end) {
6826 *idx = i;
6827 break;
6828 }
6829 }
6830
6831 return end - base_start;
6832 }
6833
6834 /** merge_extents() - analyze extents and get free size.
6835 * @super: Intel metadata, not NULL.
6836 * @expanding: if set, we are expanding &super->current_vol.
6837 *
6838 * Build a composite disk with all known extents and generate a size given the
6839 * "all disks in an array must share a common start offset" constraint.
6840 * If a volume is expanded, then return free space after the volume.
6841 *
6842 * Return: Free space or 0 on failure.
6843 */
6844 static unsigned long long merge_extents(struct intel_super *super, const bool expanding)
6845 {
6846 struct extent *e;
6847 struct dl *dl;
6848 int i, j, pos_vol_idx = -1;
6849 int extent_idx = 0;
6850 int sum_extents = 0;
6851 unsigned long long pos = 0;
6852 unsigned long long start = 0;
6853 unsigned long long free_size = 0;
6854
6855 unsigned long pre_reservation = 0;
6856 unsigned long post_reservation = IMSM_RESERVED_SECTORS;
6857 unsigned long reservation_size;
6858
6859 for (dl = super->disks; dl; dl = dl->next)
6860 if (dl->e)
6861 sum_extents += dl->extent_cnt;
6862 e = xcalloc(sum_extents, sizeof(struct extent));
6863
6864 /* coalesce and sort all extents. also, check to see if we need to
6865 * reserve space between member arrays
6866 */
6867 j = 0;
6868 for (dl = super->disks; dl; dl = dl->next) {
6869 if (!dl->e)
6870 continue;
6871 for (i = 0; i < dl->extent_cnt; i++)
6872 e[j++] = dl->e[i];
6873 }
6874 qsort(e, sum_extents, sizeof(*e), cmp_extent);
6875
6876 /* merge extents */
6877 i = 0;
6878 j = 0;
6879 while (i < sum_extents) {
6880 e[j].start = e[i].start;
6881 e[j].vol = e[i].vol;
6882 e[j].size = find_size(e, &i, sum_extents);
6883 j++;
6884 if (e[j-1].size == 0)
6885 break;
6886 }
6887
6888 i = 0;
6889 do {
6890 unsigned long long esize = e[i].start - pos;
6891
6892 if (expanding ? pos_vol_idx == super->current_vol : esize >= free_size) {
6893 free_size = esize;
6894 start = pos;
6895 extent_idx = i;
6896 }
6897
6898 pos = e[i].start + e[i].size;
6899 pos_vol_idx = e[i].vol;
6900
6901 i++;
6902 } while (e[i-1].size);
6903
6904 if (free_size == 0) {
6905 dprintf("imsm: Cannot find free size.\n");
6906 free(e);
6907 return 0;
6908 }
6909
6910 if (!expanding && extent_idx != 0)
6911 /*
6912 * Not a real first volume in a container is created, pre_reservation is needed.
6913 */
6914 pre_reservation = IMSM_RESERVED_SECTORS;
6915
6916 if (e[extent_idx].size == 0)
6917 /*
6918 * extent_idx points to the metadata, post_reservation is allready done.
6919 */
6920 post_reservation = 0;
6921 free(e);
6922
6923 reservation_size = pre_reservation + post_reservation;
6924
6925 if (free_size < reservation_size) {
6926 dprintf("imsm: Reservation size is greater than free space.\n");
6927 return 0;
6928 }
6929
6930 super->create_offset = start + pre_reservation;
6931 return free_size - reservation_size;
6932 }
6933
6934 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
6935 {
6936 if (level < 0 || level == 6 || level == 4)
6937 return 0;
6938
6939 /* if we have an orom prevent invalid raid levels */
6940 if (orom)
6941 switch (level) {
6942 case 0: return imsm_orom_has_raid0(orom);
6943 case 1:
6944 if (raiddisks > 2)
6945 return imsm_orom_has_raid1e(orom);
6946 return imsm_orom_has_raid1(orom) && raiddisks == 2;
6947 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
6948 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
6949 }
6950 else
6951 return 1; /* not on an Intel RAID platform so anything goes */
6952
6953 return 0;
6954 }
6955
6956 static int
6957 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
6958 int dpa, int verbose)
6959 {
6960 struct mdstat_ent *mdstat = mdstat_read(0, 0);
6961 struct mdstat_ent *memb;
6962 int count = 0;
6963 int num = 0;
6964 struct md_list *dv;
6965 int found;
6966
6967 for (memb = mdstat ; memb ; memb = memb->next) {
6968 if (memb->metadata_version &&
6969 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
6970 (strcmp(&memb->metadata_version[9], name) == 0) &&
6971 !is_subarray(memb->metadata_version+9) &&
6972 memb->members) {
6973 struct dev_member *dev = memb->members;
6974 int fd = -1;
6975 while (dev && !is_fd_valid(fd)) {
6976 char *path = xmalloc(strlen(dev->name) + strlen("/dev/") + 1);
6977 num = snprintf(path, PATH_MAX, "%s%s", "/dev/", dev->name);
6978 if (num > 0)
6979 fd = open(path, O_RDONLY, 0);
6980 if (num <= 0 || !is_fd_valid(fd)) {
6981 pr_vrb("Cannot open %s: %s\n",
6982 dev->name, strerror(errno));
6983 }
6984 free(path);
6985 dev = dev->next;
6986 }
6987 found = 0;
6988 if (is_fd_valid(fd) && disk_attached_to_hba(fd, hba)) {
6989 struct mdstat_ent *vol;
6990 for (vol = mdstat ; vol ; vol = vol->next) {
6991 if (vol->active > 0 &&
6992 vol->metadata_version &&
6993 is_container_member(vol, memb->devnm)) {
6994 found++;
6995 count++;
6996 }
6997 }
6998 if (*devlist && (found < dpa)) {
6999 dv = xcalloc(1, sizeof(*dv));
7000 dv->devname = xmalloc(strlen(memb->devnm) + strlen("/dev/") + 1);
7001 sprintf(dv->devname, "%s%s", "/dev/", memb->devnm);
7002 dv->found = found;
7003 dv->used = 0;
7004 dv->next = *devlist;
7005 *devlist = dv;
7006 }
7007 }
7008 close_fd(&fd);
7009 }
7010 }
7011 free_mdstat(mdstat);
7012 return count;
7013 }
7014
7015 #ifdef DEBUG_LOOP
7016 static struct md_list*
7017 get_loop_devices(void)
7018 {
7019 int i;
7020 struct md_list *devlist = NULL;
7021 struct md_list *dv;
7022
7023 for(i = 0; i < 12; i++) {
7024 dv = xcalloc(1, sizeof(*dv));
7025 dv->devname = xmalloc(40);
7026 sprintf(dv->devname, "/dev/loop%d", i);
7027 dv->next = devlist;
7028 devlist = dv;
7029 }
7030 return devlist;
7031 }
7032 #endif
7033
7034 static struct md_list*
7035 get_devices(const char *hba_path)
7036 {
7037 struct md_list *devlist = NULL;
7038 struct md_list *dv;
7039 struct dirent *ent;
7040 DIR *dir;
7041 int err = 0;
7042
7043 #if DEBUG_LOOP
7044 devlist = get_loop_devices();
7045 return devlist;
7046 #endif
7047 /* scroll through /sys/dev/block looking for devices attached to
7048 * this hba
7049 */
7050 dir = opendir("/sys/dev/block");
7051 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
7052 int fd;
7053 char buf[1024];
7054 int major, minor;
7055 char *path = NULL;
7056 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
7057 continue;
7058 path = devt_to_devpath(makedev(major, minor), 1, NULL);
7059 if (!path)
7060 continue;
7061 if (!path_attached_to_hba(path, hba_path)) {
7062 free(path);
7063 path = NULL;
7064 continue;
7065 }
7066 free(path);
7067 path = NULL;
7068 fd = dev_open(ent->d_name, O_RDONLY);
7069 if (is_fd_valid(fd)) {
7070 fd2devname(fd, buf);
7071 close(fd);
7072 } else {
7073 pr_err("cannot open device: %s\n",
7074 ent->d_name);
7075 continue;
7076 }
7077
7078 dv = xcalloc(1, sizeof(*dv));
7079 dv->devname = xstrdup(buf);
7080 dv->next = devlist;
7081 devlist = dv;
7082 }
7083 if (err) {
7084 while(devlist) {
7085 dv = devlist;
7086 devlist = devlist->next;
7087 free(dv->devname);
7088 free(dv);
7089 }
7090 }
7091 closedir(dir);
7092 return devlist;
7093 }
7094
7095 static int
7096 count_volumes_list(struct md_list *devlist, char *homehost,
7097 int verbose, int *found)
7098 {
7099 struct md_list *tmpdev;
7100 int count = 0;
7101 struct supertype *st;
7102
7103 /* first walk the list of devices to find a consistent set
7104 * that match the criterea, if that is possible.
7105 * We flag the ones we like with 'used'.
7106 */
7107 *found = 0;
7108 st = match_metadata_desc_imsm("imsm");
7109 if (st == NULL) {
7110 pr_vrb("cannot allocate memory for imsm supertype\n");
7111 return 0;
7112 }
7113
7114 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7115 char *devname = tmpdev->devname;
7116 dev_t rdev;
7117 struct supertype *tst;
7118 int dfd;
7119 if (tmpdev->used > 1)
7120 continue;
7121 tst = dup_super(st);
7122 if (tst == NULL) {
7123 pr_vrb("cannot allocate memory for imsm supertype\n");
7124 goto err_1;
7125 }
7126 tmpdev->container = 0;
7127 dfd = dev_open(devname, O_RDONLY|O_EXCL);
7128 if (!is_fd_valid(dfd)) {
7129 dprintf("cannot open device %s: %s\n",
7130 devname, strerror(errno));
7131 tmpdev->used = 2;
7132 } else if (!fstat_is_blkdev(dfd, devname, &rdev)) {
7133 tmpdev->used = 2;
7134 } else if (must_be_container(dfd)) {
7135 struct supertype *cst;
7136 cst = super_by_fd(dfd, NULL);
7137 if (cst == NULL) {
7138 dprintf("cannot recognize container type %s\n",
7139 devname);
7140 tmpdev->used = 2;
7141 } else if (tst->ss != st->ss) {
7142 dprintf("non-imsm container - ignore it: %s\n",
7143 devname);
7144 tmpdev->used = 2;
7145 } else if (!tst->ss->load_container ||
7146 tst->ss->load_container(tst, dfd, NULL))
7147 tmpdev->used = 2;
7148 else {
7149 tmpdev->container = 1;
7150 }
7151 if (cst)
7152 cst->ss->free_super(cst);
7153 } else {
7154 tmpdev->st_rdev = rdev;
7155 if (tst->ss->load_super(tst,dfd, NULL)) {
7156 dprintf("no RAID superblock on %s\n",
7157 devname);
7158 tmpdev->used = 2;
7159 } else if (tst->ss->compare_super == NULL) {
7160 dprintf("Cannot assemble %s metadata on %s\n",
7161 tst->ss->name, devname);
7162 tmpdev->used = 2;
7163 }
7164 }
7165 close_fd(&dfd);
7166
7167 if (tmpdev->used == 2 || tmpdev->used == 4) {
7168 /* Ignore unrecognised devices during auto-assembly */
7169 goto loop;
7170 }
7171 else {
7172 struct mdinfo info;
7173 tst->ss->getinfo_super(tst, &info, NULL);
7174
7175 if (st->minor_version == -1)
7176 st->minor_version = tst->minor_version;
7177
7178 if (memcmp(info.uuid, uuid_zero,
7179 sizeof(int[4])) == 0) {
7180 /* this is a floating spare. It cannot define
7181 * an array unless there are no more arrays of
7182 * this type to be found. It can be included
7183 * in an array of this type though.
7184 */
7185 tmpdev->used = 3;
7186 goto loop;
7187 }
7188
7189 if (st->ss != tst->ss ||
7190 st->minor_version != tst->minor_version ||
7191 st->ss->compare_super(st, tst, 1) != 0) {
7192 /* Some mismatch. If exactly one array matches this host,
7193 * we can resolve on that one.
7194 * Or, if we are auto assembling, we just ignore the second
7195 * for now.
7196 */
7197 dprintf("superblock on %s doesn't match others - assembly aborted\n",
7198 devname);
7199 goto loop;
7200 }
7201 tmpdev->used = 1;
7202 *found = 1;
7203 dprintf("found: devname: %s\n", devname);
7204 }
7205 loop:
7206 if (tst)
7207 tst->ss->free_super(tst);
7208 }
7209 if (*found != 0) {
7210 int err;
7211 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
7212 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
7213 for (iter = head; iter; iter = iter->next) {
7214 dprintf("content->text_version: %s vol\n",
7215 iter->text_version);
7216 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
7217 /* do not assemble arrays with unsupported
7218 configurations */
7219 dprintf("Cannot activate member %s.\n",
7220 iter->text_version);
7221 } else
7222 count++;
7223 }
7224 sysfs_free(head);
7225
7226 } else {
7227 dprintf("No valid super block on device list: err: %d %p\n",
7228 err, st->sb);
7229 }
7230 } else {
7231 dprintf("no more devices to examine\n");
7232 }
7233
7234 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
7235 if (tmpdev->used == 1 && tmpdev->found) {
7236 if (count) {
7237 if (count < tmpdev->found)
7238 count = 0;
7239 else
7240 count -= tmpdev->found;
7241 }
7242 }
7243 if (tmpdev->used == 1)
7244 tmpdev->used = 4;
7245 }
7246 err_1:
7247 if (st)
7248 st->ss->free_super(st);
7249 return count;
7250 }
7251
7252 static int __count_volumes(char *hba_path, int dpa, int verbose,
7253 int cmp_hba_path)
7254 {
7255 struct sys_dev *idev, *intel_devices = find_intel_devices();
7256 int count = 0;
7257 const struct orom_entry *entry;
7258 struct devid_list *dv, *devid_list;
7259
7260 if (!hba_path)
7261 return 0;
7262
7263 for (idev = intel_devices; idev; idev = idev->next) {
7264 if (strstr(idev->path, hba_path))
7265 break;
7266 }
7267
7268 if (!idev || !idev->dev_id)
7269 return 0;
7270
7271 entry = get_orom_entry_by_device_id(idev->dev_id);
7272
7273 if (!entry || !entry->devid_list)
7274 return 0;
7275
7276 devid_list = entry->devid_list;
7277 for (dv = devid_list; dv; dv = dv->next) {
7278 struct md_list *devlist;
7279 struct sys_dev *device = NULL;
7280 char *hpath;
7281 int found = 0;
7282
7283 if (cmp_hba_path)
7284 device = device_by_id_and_path(dv->devid, hba_path);
7285 else
7286 device = device_by_id(dv->devid);
7287
7288 if (device)
7289 hpath = device->path;
7290 else
7291 return 0;
7292
7293 devlist = get_devices(hpath);
7294 /* if no intel devices return zero volumes */
7295 if (devlist == NULL)
7296 return 0;
7297
7298 count += active_arrays_by_format("imsm", hpath, &devlist, dpa,
7299 verbose);
7300 dprintf("path: %s active arrays: %d\n", hpath, count);
7301 if (devlist == NULL)
7302 return 0;
7303 do {
7304 found = 0;
7305 count += count_volumes_list(devlist,
7306 NULL,
7307 verbose,
7308 &found);
7309 dprintf("found %d count: %d\n", found, count);
7310 } while (found);
7311
7312 dprintf("path: %s total number of volumes: %d\n", hpath, count);
7313
7314 while (devlist) {
7315 struct md_list *dv = devlist;
7316 devlist = devlist->next;
7317 free(dv->devname);
7318 free(dv);
7319 }
7320 }
7321 return count;
7322 }
7323
7324 static int count_volumes(struct intel_hba *hba, int dpa, int verbose)
7325 {
7326 if (!hba)
7327 return 0;
7328 if (hba->type == SYS_DEV_VMD) {
7329 struct sys_dev *dev;
7330 int count = 0;
7331
7332 for (dev = find_intel_devices(); dev; dev = dev->next) {
7333 if (dev->type == SYS_DEV_VMD)
7334 count += __count_volumes(dev->path, dpa,
7335 verbose, 1);
7336 }
7337 return count;
7338 }
7339 return __count_volumes(hba->path, dpa, verbose, 0);
7340 }
7341
7342 static int imsm_default_chunk(const struct imsm_orom *orom)
7343 {
7344 /* up to 512 if the plaform supports it, otherwise the platform max.
7345 * 128 if no platform detected
7346 */
7347 int fs = max(7, orom ? fls(orom->sss) : 0);
7348
7349 return min(512, (1 << fs));
7350 }
7351
7352 static int
7353 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
7354 int raiddisks, int *chunk, unsigned long long size, int verbose)
7355 {
7356 /* check/set platform and metadata limits/defaults */
7357 if (super->orom && raiddisks > super->orom->dpa) {
7358 pr_vrb("platform supports a maximum of %d disks per array\n",
7359 super->orom->dpa);
7360 return 0;
7361 }
7362
7363 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
7364 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
7365 pr_vrb("platform does not support raid%d with %d disk%s\n",
7366 level, raiddisks, raiddisks > 1 ? "s" : "");
7367 return 0;
7368 }
7369
7370 if (*chunk == 0 || *chunk == UnSet)
7371 *chunk = imsm_default_chunk(super->orom);
7372
7373 if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
7374 pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
7375 return 0;
7376 }
7377
7378 if (layout != imsm_level_to_layout(level)) {
7379 if (level == 5)
7380 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
7381 else if (level == 10)
7382 pr_vrb("imsm raid 10 only supports the n2 layout\n");
7383 else
7384 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
7385 layout, level);
7386 return 0;
7387 }
7388
7389 if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
7390 (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
7391 pr_vrb("platform does not support a volume size over 2TB\n");
7392 return 0;
7393 }
7394
7395 return 1;
7396 }
7397
7398 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
7399 * FIX ME add ahci details
7400 */
7401 static int validate_geometry_imsm_volume(struct supertype *st, int level,
7402 int layout, int raiddisks, int *chunk,
7403 unsigned long long size,
7404 unsigned long long data_offset,
7405 char *dev,
7406 unsigned long long *freesize,
7407 int verbose)
7408 {
7409 dev_t rdev;
7410 struct intel_super *super = st->sb;
7411 struct imsm_super *mpb;
7412 struct dl *dl;
7413 unsigned long long pos = 0;
7414 unsigned long long maxsize;
7415 struct extent *e;
7416 int i;
7417
7418 /* We must have the container info already read in. */
7419 if (!super)
7420 return 0;
7421
7422 mpb = super->anchor;
7423
7424 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, size, verbose)) {
7425 pr_err("RAID geometry validation failed. Cannot proceed with the action(s).\n");
7426 return 0;
7427 }
7428 if (!dev) {
7429 /* General test: make sure there is space for
7430 * 'raiddisks' device extents of size 'size' at a given
7431 * offset
7432 */
7433 unsigned long long minsize = size;
7434 unsigned long long start_offset = MaxSector;
7435 int dcnt = 0;
7436 if (minsize == 0)
7437 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
7438 for (dl = super->disks; dl ; dl = dl->next) {
7439 int found = 0;
7440
7441 pos = 0;
7442 i = 0;
7443 e = get_extents(super, dl, 0);
7444 if (!e) continue;
7445 do {
7446 unsigned long long esize;
7447 esize = e[i].start - pos;
7448 if (esize >= minsize)
7449 found = 1;
7450 if (found && start_offset == MaxSector) {
7451 start_offset = pos;
7452 break;
7453 } else if (found && pos != start_offset) {
7454 found = 0;
7455 break;
7456 }
7457 pos = e[i].start + e[i].size;
7458 i++;
7459 } while (e[i-1].size);
7460 if (found)
7461 dcnt++;
7462 free(e);
7463 }
7464 if (dcnt < raiddisks) {
7465 if (verbose)
7466 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
7467 dcnt, raiddisks);
7468 return 0;
7469 }
7470 return 1;
7471 }
7472
7473 /* This device must be a member of the set */
7474 if (!stat_is_blkdev(dev, &rdev))
7475 return 0;
7476 for (dl = super->disks ; dl ; dl = dl->next) {
7477 if (dl->major == (int)major(rdev) &&
7478 dl->minor == (int)minor(rdev))
7479 break;
7480 }
7481 if (!dl) {
7482 if (verbose)
7483 pr_err("%s is not in the same imsm set\n", dev);
7484 return 0;
7485 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
7486 /* If a volume is present then the current creation attempt
7487 * cannot incorporate new spares because the orom may not
7488 * understand this configuration (all member disks must be
7489 * members of each array in the container).
7490 */
7491 pr_err("%s is a spare and a volume is already defined for this container\n", dev);
7492 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7493 return 0;
7494 } else if (super->orom && mpb->num_raid_devs > 0 &&
7495 mpb->num_disks != raiddisks) {
7496 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
7497 return 0;
7498 }
7499
7500 /* retrieve the largest free space block */
7501 e = get_extents(super, dl, 0);
7502 maxsize = 0;
7503 i = 0;
7504 if (e) {
7505 do {
7506 unsigned long long esize;
7507
7508 esize = e[i].start - pos;
7509 if (esize >= maxsize)
7510 maxsize = esize;
7511 pos = e[i].start + e[i].size;
7512 i++;
7513 } while (e[i-1].size);
7514 dl->e = e;
7515 dl->extent_cnt = i;
7516 } else {
7517 if (verbose)
7518 pr_err("unable to determine free space for: %s\n",
7519 dev);
7520 return 0;
7521 }
7522 if (maxsize < size) {
7523 if (verbose)
7524 pr_err("%s not enough space (%llu < %llu)\n",
7525 dev, maxsize, size);
7526 return 0;
7527 }
7528
7529 maxsize = merge_extents(super, false);
7530
7531 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7532 pr_err("attempting to create a second volume with size less then remaining space.\n");
7533
7534 if (maxsize < size || maxsize == 0) {
7535 if (verbose) {
7536 if (maxsize == 0)
7537 pr_err("no free space left on device. Aborting...\n");
7538 else
7539 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7540 maxsize, size);
7541 }
7542 return 0;
7543 }
7544
7545 *freesize = maxsize;
7546
7547 if (super->orom) {
7548 int count = count_volumes(super->hba,
7549 super->orom->dpa, verbose);
7550 if (super->orom->vphba <= count) {
7551 pr_vrb("platform does not support more than %d raid volumes.\n",
7552 super->orom->vphba);
7553 return 0;
7554 }
7555 }
7556 return 1;
7557 }
7558
7559 /**
7560 * imsm_get_free_size() - get the biggest, common free space from members.
7561 * @super: &intel_super pointer, not NULL.
7562 * @raiddisks: number of raid disks.
7563 * @size: requested size, could be 0 (means max size).
7564 * @chunk: requested chunk size in KiB.
7565 * @freesize: pointer for returned size value.
7566 *
7567 * Return: &IMSM_STATUS_OK or &IMSM_STATUS_ERROR.
7568 *
7569 * @freesize is set to meaningful value, this can be @size, or calculated
7570 * max free size.
7571 * super->create_offset value is modified and set appropriately in
7572 * merge_extends() for further creation.
7573 */
7574 static imsm_status_t imsm_get_free_size(struct intel_super *super,
7575 const int raiddisks,
7576 unsigned long long size,
7577 const int chunk,
7578 unsigned long long *freesize,
7579 bool expanding)
7580 {
7581 struct imsm_super *mpb = super->anchor;
7582 struct dl *dl;
7583 int i;
7584 struct extent *e;
7585 int cnt = 0;
7586 int used = 0;
7587 unsigned long long maxsize;
7588 unsigned long long minsize = size;
7589
7590 if (minsize == 0)
7591 minsize = chunk * 2;
7592
7593 /* find the largest common start free region of the possible disks */
7594 for (dl = super->disks; dl; dl = dl->next) {
7595 dl->raiddisk = -1;
7596
7597 if (dl->index >= 0)
7598 used++;
7599
7600 /* don't activate new spares if we are orom constrained
7601 * and there is already a volume active in the container
7602 */
7603 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
7604 continue;
7605
7606 e = get_extents(super, dl, 0);
7607 if (!e)
7608 continue;
7609 for (i = 1; e[i-1].size; i++)
7610 ;
7611 dl->e = e;
7612 dl->extent_cnt = i;
7613 cnt++;
7614 }
7615
7616 maxsize = merge_extents(super, expanding);
7617 if (maxsize < minsize) {
7618 pr_err("imsm: Free space is %llu but must be equal or larger than %llu.\n",
7619 maxsize, minsize);
7620 return IMSM_STATUS_ERROR;
7621 }
7622
7623 if (cnt < raiddisks || (super->orom && used && used != raiddisks)) {
7624 pr_err("imsm: Not enough devices with space to create array.\n");
7625 return IMSM_STATUS_ERROR;
7626 }
7627
7628 if (size == 0) {
7629 size = maxsize;
7630 if (chunk) {
7631 size /= 2 * chunk;
7632 size *= 2 * chunk;
7633 }
7634 maxsize = size;
7635 }
7636 if (mpb->num_raid_devs > 0 && size && size != maxsize)
7637 pr_err("attempting to create a second volume with size less then remaining space.\n");
7638 *freesize = size;
7639
7640 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size);
7641
7642 return IMSM_STATUS_OK;
7643 }
7644
7645 /**
7646 * autolayout_imsm() - automatically layout a new volume.
7647 * @super: &intel_super pointer, not NULL.
7648 * @raiddisks: number of raid disks.
7649 * @size: requested size, could be 0 (means max size).
7650 * @chunk: requested chunk.
7651 * @freesize: pointer for returned size value.
7652 *
7653 * We are being asked to automatically layout a new volume based on the current
7654 * contents of the container. If the parameters can be satisfied autolayout_imsm
7655 * will record the disks, start offset, and will return size of the volume to
7656 * be created. See imsm_get_free_size() for details.
7657 * add_to_super() and getinfo_super() detect when autolayout is in progress.
7658 * If first volume exists, slots are set consistently to it.
7659 *
7660 * Return: &IMSM_STATUS_OK on success, &IMSM_STATUS_ERROR otherwise.
7661 *
7662 * Disks are marked for creation via dl->raiddisk.
7663 */
7664 static imsm_status_t autolayout_imsm(struct intel_super *super,
7665 const int raiddisks,
7666 unsigned long long size, const int chunk,
7667 unsigned long long *freesize)
7668 {
7669 int curr_slot = 0;
7670 struct dl *disk;
7671 int vol_cnt = super->anchor->num_raid_devs;
7672 imsm_status_t rv;
7673
7674 rv = imsm_get_free_size(super, raiddisks, size, chunk, freesize, false);
7675 if (rv != IMSM_STATUS_OK)
7676 return IMSM_STATUS_ERROR;
7677
7678 for (disk = super->disks; disk; disk = disk->next) {
7679 if (!disk->e)
7680 continue;
7681
7682 if (curr_slot == raiddisks)
7683 break;
7684
7685 if (vol_cnt == 0) {
7686 disk->raiddisk = curr_slot;
7687 } else {
7688 int _slot = get_disk_slot_in_dev(super, 0, disk->index);
7689
7690 if (_slot == -1) {
7691 pr_err("Disk %s is not used in first volume, aborting\n",
7692 disk->devname);
7693 return IMSM_STATUS_ERROR;
7694 }
7695 disk->raiddisk = _slot;
7696 }
7697 curr_slot++;
7698 }
7699
7700 return IMSM_STATUS_OK;
7701 }
7702
7703 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
7704 int raiddisks, int *chunk, unsigned long long size,
7705 unsigned long long data_offset,
7706 char *dev, unsigned long long *freesize,
7707 int consistency_policy, int verbose)
7708 {
7709 int fd, cfd;
7710 struct mdinfo *sra;
7711 int is_member = 0;
7712
7713 /* load capability
7714 * if given unused devices create a container
7715 * if given given devices in a container create a member volume
7716 */
7717 if (is_container(level))
7718 /* Must be a fresh device to add to a container */
7719 return validate_geometry_imsm_container(st, level, raiddisks,
7720 data_offset, dev,
7721 freesize, verbose);
7722
7723 /*
7724 * Size is given in sectors.
7725 */
7726 if (size && (size < 2048)) {
7727 pr_err("Given size must be greater than 1M.\n");
7728 /* Depends on algorithm in Create.c :
7729 * if container was given (dev == NULL) return -1,
7730 * if block device was given ( dev != NULL) return 0.
7731 */
7732 return dev ? -1 : 0;
7733 }
7734
7735 if (!dev) {
7736 struct intel_super *super = st->sb;
7737
7738 /*
7739 * Autolayout mode, st->sb must be set.
7740 */
7741 if (!super) {
7742 pr_vrb("superblock must be set for autolayout, aborting\n");
7743 return 0;
7744 }
7745
7746 if (!validate_geometry_imsm_orom(st->sb, level, layout,
7747 raiddisks, chunk, size,
7748 verbose))
7749 return 0;
7750
7751 if (super->orom && freesize) {
7752 imsm_status_t rv;
7753 int count = count_volumes(super->hba, super->orom->dpa,
7754 verbose);
7755 if (super->orom->vphba <= count) {
7756 pr_vrb("platform does not support more than %d raid volumes.\n",
7757 super->orom->vphba);
7758 return 0;
7759 }
7760
7761 rv = autolayout_imsm(super, raiddisks, size, *chunk,
7762 freesize);
7763 if (rv != IMSM_STATUS_OK)
7764 return 0;
7765 }
7766 return 1;
7767 }
7768 if (st->sb) {
7769 /* creating in a given container */
7770 return validate_geometry_imsm_volume(st, level, layout,
7771 raiddisks, chunk, size,
7772 data_offset,
7773 dev, freesize, verbose);
7774 }
7775
7776 /* This device needs to be a device in an 'imsm' container */
7777 fd = open(dev, O_RDONLY|O_EXCL, 0);
7778
7779 if (is_fd_valid(fd)) {
7780 pr_vrb("Cannot create this array on device %s\n", dev);
7781 close(fd);
7782 return 0;
7783 }
7784 if (errno == EBUSY)
7785 fd = open(dev, O_RDONLY, 0);
7786
7787 if (!is_fd_valid(fd)) {
7788 pr_vrb("Cannot open %s: %s\n", dev, strerror(errno));
7789 return 0;
7790 }
7791
7792 /* Well, it is in use by someone, maybe an 'imsm' container. */
7793 cfd = open_container(fd);
7794 close_fd(&fd);
7795
7796 if (!is_fd_valid(cfd)) {
7797 pr_vrb("Cannot use %s: It is busy\n", dev);
7798 return 0;
7799 }
7800 sra = sysfs_read(cfd, NULL, GET_VERSION);
7801 if (sra && sra->array.major_version == -1 &&
7802 strcmp(sra->text_version, "imsm") == 0)
7803 is_member = 1;
7804 sysfs_free(sra);
7805 if (is_member) {
7806 /* This is a member of a imsm container. Load the container
7807 * and try to create a volume
7808 */
7809 struct intel_super *super;
7810
7811 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
7812 st->sb = super;
7813 strcpy(st->container_devnm, fd2devnm(cfd));
7814 close(cfd);
7815 return validate_geometry_imsm_volume(st, level, layout,
7816 raiddisks, chunk,
7817 size, data_offset, dev,
7818 freesize, 1)
7819 ? 1 : -1;
7820 }
7821 }
7822
7823 if (verbose)
7824 pr_err("failed container membership check\n");
7825
7826 close(cfd);
7827 return 0;
7828 }
7829
7830 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
7831 {
7832 struct intel_super *super = st->sb;
7833
7834 if (level && *level == UnSet)
7835 *level = LEVEL_CONTAINER;
7836
7837 if (level && layout && *layout == UnSet)
7838 *layout = imsm_level_to_layout(*level);
7839
7840 if (chunk && (*chunk == UnSet || *chunk == 0))
7841 *chunk = imsm_default_chunk(super->orom);
7842 }
7843
7844 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
7845
7846 static int kill_subarray_imsm(struct supertype *st, char *subarray_id)
7847 {
7848 /* remove the subarray currently referenced by subarray_id */
7849 __u8 i;
7850 struct intel_dev **dp;
7851 struct intel_super *super = st->sb;
7852 __u8 current_vol = strtoul(subarray_id, NULL, 10);
7853 struct imsm_super *mpb = super->anchor;
7854
7855 if (mpb->num_raid_devs == 0)
7856 return 2;
7857
7858 /* block deletions that would change the uuid of active subarrays
7859 *
7860 * FIXME when immutable ids are available, but note that we'll
7861 * also need to fixup the invalidated/active subarray indexes in
7862 * mdstat
7863 */
7864 for (i = 0; i < mpb->num_raid_devs; i++) {
7865 char subarray[4];
7866
7867 if (i < current_vol)
7868 continue;
7869 snprintf(subarray, sizeof(subarray), "%u", i);
7870 if (is_subarray_active(subarray, st->devnm)) {
7871 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7872 current_vol, i);
7873
7874 return 2;
7875 }
7876 }
7877
7878 if (st->update_tail) {
7879 struct imsm_update_kill_array *u = xmalloc(sizeof(*u));
7880
7881 u->type = update_kill_array;
7882 u->dev_idx = current_vol;
7883 append_metadata_update(st, u, sizeof(*u));
7884
7885 return 0;
7886 }
7887
7888 for (dp = &super->devlist; *dp;)
7889 if ((*dp)->index == current_vol) {
7890 *dp = (*dp)->next;
7891 } else {
7892 handle_missing(super, (*dp)->dev);
7893 if ((*dp)->index > current_vol)
7894 (*dp)->index--;
7895 dp = &(*dp)->next;
7896 }
7897
7898 /* no more raid devices, all active components are now spares,
7899 * but of course failed are still failed
7900 */
7901 if (--mpb->num_raid_devs == 0) {
7902 struct dl *d;
7903
7904 for (d = super->disks; d; d = d->next)
7905 if (d->index > -2)
7906 mark_spare(d);
7907 }
7908
7909 super->updates_pending++;
7910
7911 return 0;
7912 }
7913
7914 /**
7915 * get_rwh_policy_from_update() - Get the rwh policy for update option.
7916 * @update: Update option.
7917 */
7918 static int get_rwh_policy_from_update(enum update_opt update)
7919 {
7920 switch (update) {
7921 case UOPT_PPL:
7922 return RWH_MULTIPLE_DISTRIBUTED;
7923 case UOPT_NO_PPL:
7924 return RWH_MULTIPLE_OFF;
7925 case UOPT_BITMAP:
7926 return RWH_BITMAP;
7927 case UOPT_NO_BITMAP:
7928 return RWH_OFF;
7929 default:
7930 break;
7931 }
7932 return UOPT_UNDEFINED;
7933 }
7934
7935 static int update_subarray_imsm(struct supertype *st, char *subarray,
7936 enum update_opt update, struct mddev_ident *ident)
7937 {
7938 /* update the subarray currently referenced by ->current_vol */
7939 struct intel_super *super = st->sb;
7940 struct imsm_super *mpb = super->anchor;
7941
7942 if (update == UOPT_NAME) {
7943 char *name = ident->name;
7944 char *ep;
7945 int vol;
7946
7947 if (imsm_is_name_allowed(super, name, 1) == false)
7948 return 2;
7949
7950 vol = strtoul(subarray, &ep, 10);
7951 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7952 return 2;
7953
7954 if (st->update_tail) {
7955 struct imsm_update_rename_array *u = xmalloc(sizeof(*u));
7956
7957 u->type = update_rename_array;
7958 u->dev_idx = vol;
7959 strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN);
7960 u->name[MAX_RAID_SERIAL_LEN-1] = '\0';
7961 append_metadata_update(st, u, sizeof(*u));
7962 } else {
7963 struct imsm_dev *dev;
7964 int i, namelen;
7965
7966 dev = get_imsm_dev(super, vol);
7967 memset(dev->volume, '\0', MAX_RAID_SERIAL_LEN);
7968 namelen = min((int)strlen(name), MAX_RAID_SERIAL_LEN);
7969 memcpy(dev->volume, name, namelen);
7970 for (i = 0; i < mpb->num_raid_devs; i++) {
7971 dev = get_imsm_dev(super, i);
7972 handle_missing(super, dev);
7973 }
7974 super->updates_pending++;
7975 }
7976 } else if (get_rwh_policy_from_update(update) != UOPT_UNDEFINED) {
7977 int new_policy;
7978 char *ep;
7979 int vol = strtoul(subarray, &ep, 10);
7980
7981 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
7982 return 2;
7983
7984 new_policy = get_rwh_policy_from_update(update);
7985
7986 if (st->update_tail) {
7987 struct imsm_update_rwh_policy *u = xmalloc(sizeof(*u));
7988
7989 u->type = update_rwh_policy;
7990 u->dev_idx = vol;
7991 u->new_policy = new_policy;
7992 append_metadata_update(st, u, sizeof(*u));
7993 } else {
7994 struct imsm_dev *dev;
7995
7996 dev = get_imsm_dev(super, vol);
7997 dev->rwh_policy = new_policy;
7998 super->updates_pending++;
7999 }
8000 if (new_policy == RWH_BITMAP)
8001 return write_init_bitmap_imsm_vol(st, vol);
8002 } else
8003 return 2;
8004
8005 return 0;
8006 }
8007
8008 static bool is_gen_migration(struct imsm_dev *dev)
8009 {
8010 if (dev && dev->vol.migr_state &&
8011 migr_type(dev) == MIGR_GEN_MIGR)
8012 return true;
8013
8014 return false;
8015 }
8016
8017 static int is_rebuilding(struct imsm_dev *dev)
8018 {
8019 struct imsm_map *migr_map;
8020
8021 if (!dev->vol.migr_state)
8022 return 0;
8023
8024 if (migr_type(dev) != MIGR_REBUILD)
8025 return 0;
8026
8027 migr_map = get_imsm_map(dev, MAP_1);
8028
8029 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
8030 return 1;
8031 else
8032 return 0;
8033 }
8034
8035 static int is_initializing(struct imsm_dev *dev)
8036 {
8037 struct imsm_map *migr_map;
8038
8039 if (!dev->vol.migr_state)
8040 return 0;
8041
8042 if (migr_type(dev) != MIGR_INIT)
8043 return 0;
8044
8045 migr_map = get_imsm_map(dev, MAP_1);
8046
8047 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
8048 return 1;
8049
8050 return 0;
8051 }
8052
8053 static void update_recovery_start(struct intel_super *super,
8054 struct imsm_dev *dev,
8055 struct mdinfo *array)
8056 {
8057 struct mdinfo *rebuild = NULL;
8058 struct mdinfo *d;
8059 __u32 units;
8060
8061 if (!is_rebuilding(dev))
8062 return;
8063
8064 /* Find the rebuild target, but punt on the dual rebuild case */
8065 for (d = array->devs; d; d = d->next)
8066 if (d->recovery_start == 0) {
8067 if (rebuild)
8068 return;
8069 rebuild = d;
8070 }
8071
8072 if (!rebuild) {
8073 /* (?) none of the disks are marked with
8074 * IMSM_ORD_REBUILD, so assume they are missing and the
8075 * disk_ord_tbl was not correctly updated
8076 */
8077 dprintf("failed to locate out-of-sync disk\n");
8078 return;
8079 }
8080
8081 units = vol_curr_migr_unit(dev);
8082 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
8083 }
8084
8085 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
8086
8087 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
8088 {
8089 /* Given a container loaded by load_super_imsm_all,
8090 * extract information about all the arrays into
8091 * an mdinfo tree.
8092 * If 'subarray' is given, just extract info about that array.
8093 *
8094 * For each imsm_dev create an mdinfo, fill it in,
8095 * then look for matching devices in super->disks
8096 * and create appropriate device mdinfo.
8097 */
8098 struct intel_super *super = st->sb;
8099 struct imsm_super *mpb = super->anchor;
8100 struct mdinfo *rest = NULL;
8101 unsigned int i;
8102 int sb_errors = 0;
8103 struct dl *d;
8104 int spare_disks = 0;
8105 int current_vol = super->current_vol;
8106
8107 /* do not assemble arrays when not all attributes are supported */
8108 if (imsm_check_attributes(mpb->attributes) == 0) {
8109 sb_errors = 1;
8110 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
8111 }
8112
8113 /* count spare devices, not used in maps
8114 */
8115 for (d = super->disks; d; d = d->next)
8116 if (d->index == -1)
8117 spare_disks++;
8118
8119 for (i = 0; i < mpb->num_raid_devs; i++) {
8120 struct imsm_dev *dev;
8121 struct imsm_map *map;
8122 struct imsm_map *map2;
8123 struct mdinfo *this;
8124 int slot;
8125 int chunk;
8126 char *ep;
8127 int level;
8128
8129 if (subarray &&
8130 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
8131 continue;
8132
8133 dev = get_imsm_dev(super, i);
8134 map = get_imsm_map(dev, MAP_0);
8135 map2 = get_imsm_map(dev, MAP_1);
8136 level = get_imsm_raid_level(map);
8137
8138 /* do not publish arrays that are in the middle of an
8139 * unsupported migration
8140 */
8141 if (dev->vol.migr_state &&
8142 (migr_type(dev) == MIGR_STATE_CHANGE)) {
8143 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
8144 dev->volume);
8145 continue;
8146 }
8147 /* do not publish arrays that are not support by controller's
8148 * OROM/EFI
8149 */
8150
8151 this = xmalloc(sizeof(*this));
8152
8153 super->current_vol = i;
8154 getinfo_super_imsm_volume(st, this, NULL);
8155 this->next = rest;
8156 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
8157 /* mdadm does not support all metadata features- set the bit in all arrays state */
8158 if (!validate_geometry_imsm_orom(super,
8159 level, /* RAID level */
8160 imsm_level_to_layout(level),
8161 map->num_members, /* raid disks */
8162 &chunk, imsm_dev_size(dev),
8163 1 /* verbose */)) {
8164 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
8165 dev->volume);
8166 this->array.state |=
8167 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8168 (1<<MD_SB_BLOCK_VOLUME);
8169 }
8170
8171 /* if array has bad blocks, set suitable bit in all arrays state */
8172 if (sb_errors)
8173 this->array.state |=
8174 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
8175 (1<<MD_SB_BLOCK_VOLUME);
8176
8177 for (slot = 0 ; slot < map->num_members; slot++) {
8178 unsigned long long recovery_start;
8179 struct mdinfo *info_d;
8180 struct dl *d;
8181 int idx;
8182 int skip;
8183 __u32 ord;
8184 int missing = 0;
8185
8186 skip = 0;
8187 idx = get_imsm_disk_idx(dev, slot, MAP_0);
8188 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
8189 for (d = super->disks; d ; d = d->next)
8190 if (d->index == idx)
8191 break;
8192
8193 recovery_start = MaxSector;
8194 if (d == NULL)
8195 skip = 1;
8196 if (d && is_failed(&d->disk))
8197 skip = 1;
8198 if (!skip && (ord & IMSM_ORD_REBUILD))
8199 recovery_start = 0;
8200 if (!(ord & IMSM_ORD_REBUILD))
8201 this->array.working_disks++;
8202 /*
8203 * if we skip some disks the array will be assmebled degraded;
8204 * reset resync start to avoid a dirty-degraded
8205 * situation when performing the intial sync
8206 */
8207 if (skip)
8208 missing++;
8209
8210 if (!(dev->vol.dirty & RAIDVOL_DIRTY)) {
8211 if ((!able_to_resync(level, missing) ||
8212 recovery_start == 0))
8213 this->resync_start = MaxSector;
8214 }
8215
8216 if (skip)
8217 continue;
8218
8219 info_d = xcalloc(1, sizeof(*info_d));
8220 info_d->next = this->devs;
8221 this->devs = info_d;
8222
8223 info_d->disk.number = d->index;
8224 info_d->disk.major = d->major;
8225 info_d->disk.minor = d->minor;
8226 info_d->disk.raid_disk = slot;
8227 info_d->recovery_start = recovery_start;
8228 if (map2) {
8229 if (slot < map2->num_members)
8230 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8231 else
8232 this->array.spare_disks++;
8233 } else {
8234 if (slot < map->num_members)
8235 info_d->disk.state = (1 << MD_DISK_ACTIVE);
8236 else
8237 this->array.spare_disks++;
8238 }
8239
8240 info_d->events = __le32_to_cpu(mpb->generation_num);
8241 info_d->data_offset = pba_of_lba0(map);
8242 info_d->component_size = calc_component_size(map, dev);
8243
8244 if (map->raid_level == 5) {
8245 info_d->ppl_sector = this->ppl_sector;
8246 info_d->ppl_size = this->ppl_size;
8247 if (this->consistency_policy == CONSISTENCY_POLICY_PPL &&
8248 recovery_start == 0)
8249 this->resync_start = 0;
8250 }
8251
8252 info_d->bb.supported = 1;
8253 get_volume_badblocks(super->bbm_log, ord_to_idx(ord),
8254 info_d->data_offset,
8255 info_d->component_size,
8256 &info_d->bb);
8257 }
8258 /* now that the disk list is up-to-date fixup recovery_start */
8259 update_recovery_start(super, dev, this);
8260 this->array.spare_disks += spare_disks;
8261
8262 /* check for reshape */
8263 if (this->reshape_active == 1)
8264 recover_backup_imsm(st, this);
8265 rest = this;
8266 }
8267
8268 super->current_vol = current_vol;
8269 return rest;
8270 }
8271
8272 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
8273 int failed, int look_in_map)
8274 {
8275 struct imsm_map *map;
8276
8277 map = get_imsm_map(dev, look_in_map);
8278
8279 if (!failed)
8280 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
8281 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
8282
8283 switch (get_imsm_raid_level(map)) {
8284 case 0:
8285 return IMSM_T_STATE_FAILED;
8286 break;
8287 case 1:
8288 if (failed < map->num_members)
8289 return IMSM_T_STATE_DEGRADED;
8290 else
8291 return IMSM_T_STATE_FAILED;
8292 break;
8293 case 10:
8294 {
8295 /**
8296 * check to see if any mirrors have failed, otherwise we
8297 * are degraded. Even numbered slots are mirrored on
8298 * slot+1
8299 */
8300 int i;
8301 /* gcc -Os complains that this is unused */
8302 int insync = insync;
8303
8304 for (i = 0; i < map->num_members; i++) {
8305 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
8306 int idx = ord_to_idx(ord);
8307 struct imsm_disk *disk;
8308
8309 /* reset the potential in-sync count on even-numbered
8310 * slots. num_copies is always 2 for imsm raid10
8311 */
8312 if ((i & 1) == 0)
8313 insync = 2;
8314
8315 disk = get_imsm_disk(super, idx);
8316 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8317 insync--;
8318
8319 /* no in-sync disks left in this mirror the
8320 * array has failed
8321 */
8322 if (insync == 0)
8323 return IMSM_T_STATE_FAILED;
8324 }
8325
8326 return IMSM_T_STATE_DEGRADED;
8327 }
8328 case 5:
8329 if (failed < 2)
8330 return IMSM_T_STATE_DEGRADED;
8331 else
8332 return IMSM_T_STATE_FAILED;
8333 break;
8334 default:
8335 break;
8336 }
8337
8338 return map->map_state;
8339 }
8340
8341 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
8342 int look_in_map)
8343 {
8344 int i;
8345 int failed = 0;
8346 struct imsm_disk *disk;
8347 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8348 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
8349 struct imsm_map *map_for_loop;
8350 __u32 ord;
8351 int idx;
8352 int idx_1;
8353
8354 /* at the beginning of migration we set IMSM_ORD_REBUILD on
8355 * disks that are being rebuilt. New failures are recorded to
8356 * map[0]. So we look through all the disks we started with and
8357 * see if any failures are still present, or if any new ones
8358 * have arrived
8359 */
8360 map_for_loop = map;
8361 if (prev && (map->num_members < prev->num_members))
8362 map_for_loop = prev;
8363
8364 for (i = 0; i < map_for_loop->num_members; i++) {
8365 idx_1 = -255;
8366 /* when MAP_X is passed both maps failures are counted
8367 */
8368 if (prev &&
8369 (look_in_map == MAP_1 || look_in_map == MAP_X) &&
8370 i < prev->num_members) {
8371 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
8372 idx_1 = ord_to_idx(ord);
8373
8374 disk = get_imsm_disk(super, idx_1);
8375 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
8376 failed++;
8377 }
8378 if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
8379 i < map->num_members) {
8380 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
8381 idx = ord_to_idx(ord);
8382
8383 if (idx != idx_1) {
8384 disk = get_imsm_disk(super, idx);
8385 if (!disk || is_failed(disk) ||
8386 ord & IMSM_ORD_REBUILD)
8387 failed++;
8388 }
8389 }
8390 }
8391
8392 return failed;
8393 }
8394
8395 static int imsm_open_new(struct supertype *c, struct active_array *a,
8396 int inst)
8397 {
8398 struct intel_super *super = c->sb;
8399 struct imsm_super *mpb = super->anchor;
8400 struct imsm_update_prealloc_bb_mem u;
8401
8402 if (inst >= mpb->num_raid_devs) {
8403 pr_err("subarry index %d, out of range\n", inst);
8404 return -ENODEV;
8405 }
8406
8407 dprintf("imsm: open_new %d\n", inst);
8408 a->info.container_member = inst;
8409
8410 u.type = update_prealloc_badblocks_mem;
8411 imsm_update_metadata_locally(c, &u, sizeof(u));
8412
8413 return 0;
8414 }
8415
8416 static int is_resyncing(struct imsm_dev *dev)
8417 {
8418 struct imsm_map *migr_map;
8419
8420 if (!dev->vol.migr_state)
8421 return 0;
8422
8423 if (migr_type(dev) == MIGR_INIT ||
8424 migr_type(dev) == MIGR_REPAIR)
8425 return 1;
8426
8427 if (migr_type(dev) == MIGR_GEN_MIGR)
8428 return 0;
8429
8430 migr_map = get_imsm_map(dev, MAP_1);
8431
8432 if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
8433 dev->vol.migr_type != MIGR_GEN_MIGR)
8434 return 1;
8435 else
8436 return 0;
8437 }
8438
8439 /* return true if we recorded new information */
8440 static int mark_failure(struct intel_super *super,
8441 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8442 {
8443 __u32 ord;
8444 int slot;
8445 struct imsm_map *map;
8446 char buf[MAX_RAID_SERIAL_LEN+3];
8447 unsigned int len, shift = 0;
8448
8449 /* new failures are always set in map[0] */
8450 map = get_imsm_map(dev, MAP_0);
8451
8452 slot = get_imsm_disk_slot(map, idx);
8453 if (slot < 0)
8454 return 0;
8455
8456 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
8457 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
8458 return 0;
8459
8460 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
8461 buf[MAX_RAID_SERIAL_LEN] = '\000';
8462 strcat(buf, ":0");
8463 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
8464 shift = len - MAX_RAID_SERIAL_LEN + 1;
8465 memcpy(disk->serial, &buf[shift], len + 1 - shift);
8466
8467 disk->status |= FAILED_DISK;
8468 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
8469 /* mark failures in second map if second map exists and this disk
8470 * in this slot.
8471 * This is valid for migration, initialization and rebuild
8472 */
8473 if (dev->vol.migr_state) {
8474 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
8475 int slot2 = get_imsm_disk_slot(map2, idx);
8476
8477 if (slot2 < map2->num_members && slot2 >= 0)
8478 set_imsm_ord_tbl_ent(map2, slot2,
8479 idx | IMSM_ORD_REBUILD);
8480 }
8481 if (map->failed_disk_num == 0xff ||
8482 (!is_rebuilding(dev) && map->failed_disk_num > slot))
8483 map->failed_disk_num = slot;
8484
8485 clear_disk_badblocks(super->bbm_log, ord_to_idx(ord));
8486
8487 return 1;
8488 }
8489
8490 static void mark_missing(struct intel_super *super,
8491 struct imsm_dev *dev, struct imsm_disk *disk, int idx)
8492 {
8493 mark_failure(super, dev, disk, idx);
8494
8495 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
8496 return;
8497
8498 disk->scsi_id = __cpu_to_le32(~(__u32)0);
8499 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
8500 }
8501
8502 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
8503 {
8504 struct dl *dl;
8505
8506 if (!super->missing)
8507 return;
8508
8509 /* When orom adds replacement for missing disk it does
8510 * not remove entry of missing disk, but just updates map with
8511 * new added disk. So it is not enough just to test if there is
8512 * any missing disk, we have to look if there are any failed disks
8513 * in map to stop migration */
8514
8515 dprintf("imsm: mark missing\n");
8516 /* end process for initialization and rebuild only
8517 */
8518 if (is_gen_migration(dev) == false) {
8519 int failed = imsm_count_failed(super, dev, MAP_0);
8520
8521 if (failed) {
8522 __u8 map_state;
8523 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8524 struct imsm_map *map1;
8525 int i, ord, ord_map1;
8526 int rebuilt = 1;
8527
8528 for (i = 0; i < map->num_members; i++) {
8529 ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8530 if (!(ord & IMSM_ORD_REBUILD))
8531 continue;
8532
8533 map1 = get_imsm_map(dev, MAP_1);
8534 if (!map1)
8535 continue;
8536
8537 ord_map1 = __le32_to_cpu(map1->disk_ord_tbl[i]);
8538 if (ord_map1 & IMSM_ORD_REBUILD)
8539 rebuilt = 0;
8540 }
8541
8542 if (rebuilt) {
8543 map_state = imsm_check_degraded(super, dev,
8544 failed, MAP_0);
8545 end_migration(dev, super, map_state);
8546 }
8547 }
8548 }
8549 for (dl = super->missing; dl; dl = dl->next)
8550 mark_missing(super, dev, &dl->disk, dl->index);
8551 super->updates_pending++;
8552 }
8553
8554 static unsigned long long imsm_set_array_size(struct imsm_dev *dev,
8555 long long new_size)
8556 {
8557 unsigned long long array_blocks;
8558 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8559 int used_disks = imsm_num_data_members(map);
8560
8561 if (used_disks == 0) {
8562 /* when problems occures
8563 * return current array_blocks value
8564 */
8565 array_blocks = imsm_dev_size(dev);
8566
8567 return array_blocks;
8568 }
8569
8570 /* set array size in metadata
8571 */
8572 if (new_size <= 0)
8573 /* OLCE size change is caused by added disks
8574 */
8575 array_blocks = per_dev_array_size(map) * used_disks;
8576 else
8577 /* Online Volume Size Change
8578 * Using available free space
8579 */
8580 array_blocks = new_size;
8581
8582 array_blocks = round_size_to_mb(array_blocks, used_disks);
8583 set_imsm_dev_size(dev, array_blocks);
8584
8585 return array_blocks;
8586 }
8587
8588 static void imsm_set_disk(struct active_array *a, int n, int state);
8589
8590 static void imsm_progress_container_reshape(struct intel_super *super)
8591 {
8592 /* if no device has a migr_state, but some device has a
8593 * different number of members than the previous device, start
8594 * changing the number of devices in this device to match
8595 * previous.
8596 */
8597 struct imsm_super *mpb = super->anchor;
8598 int prev_disks = -1;
8599 int i;
8600 int copy_map_size;
8601
8602 for (i = 0; i < mpb->num_raid_devs; i++) {
8603 struct imsm_dev *dev = get_imsm_dev(super, i);
8604 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8605 struct imsm_map *map2;
8606 int prev_num_members;
8607
8608 if (dev->vol.migr_state)
8609 return;
8610
8611 if (prev_disks == -1)
8612 prev_disks = map->num_members;
8613 if (prev_disks == map->num_members)
8614 continue;
8615
8616 /* OK, this array needs to enter reshape mode.
8617 * i.e it needs a migr_state
8618 */
8619
8620 copy_map_size = sizeof_imsm_map(map);
8621 prev_num_members = map->num_members;
8622 map->num_members = prev_disks;
8623 dev->vol.migr_state = 1;
8624 set_vol_curr_migr_unit(dev, 0);
8625 set_migr_type(dev, MIGR_GEN_MIGR);
8626 for (i = prev_num_members;
8627 i < map->num_members; i++)
8628 set_imsm_ord_tbl_ent(map, i, i);
8629 map2 = get_imsm_map(dev, MAP_1);
8630 /* Copy the current map */
8631 memcpy(map2, map, copy_map_size);
8632 map2->num_members = prev_num_members;
8633
8634 imsm_set_array_size(dev, -1);
8635 super->clean_migration_record_by_mdmon = 1;
8636 super->updates_pending++;
8637 }
8638 }
8639
8640 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8641 * states are handled in imsm_set_disk() with one exception, when a
8642 * resync is stopped due to a new failure this routine will set the
8643 * 'degraded' state for the array.
8644 */
8645 static int imsm_set_array_state(struct active_array *a, int consistent)
8646 {
8647 int inst = a->info.container_member;
8648 struct intel_super *super = a->container->sb;
8649 struct imsm_dev *dev = get_imsm_dev(super, inst);
8650 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8651 int failed = imsm_count_failed(super, dev, MAP_0);
8652 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8653 __u32 blocks_per_unit;
8654
8655 if (dev->vol.migr_state &&
8656 dev->vol.migr_type == MIGR_GEN_MIGR) {
8657 /* array state change is blocked due to reshape action
8658 * We might need to
8659 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8660 * - finish the reshape (if last_checkpoint is big and action != reshape)
8661 * - update vol_curr_migr_unit
8662 */
8663 if (a->curr_action == reshape) {
8664 /* still reshaping, maybe update vol_curr_migr_unit */
8665 goto mark_checkpoint;
8666 } else {
8667 if (a->last_checkpoint >= a->info.component_size) {
8668 unsigned long long array_blocks;
8669 int used_disks;
8670 struct mdinfo *mdi;
8671
8672 used_disks = imsm_num_data_members(map);
8673 if (used_disks > 0) {
8674 array_blocks =
8675 per_dev_array_size(map) *
8676 used_disks;
8677 array_blocks =
8678 round_size_to_mb(array_blocks,
8679 used_disks);
8680 a->info.custom_array_size = array_blocks;
8681 /* encourage manager to update array
8682 * size
8683 */
8684
8685 a->check_reshape = 1;
8686 }
8687 /* finalize online capacity expansion/reshape */
8688 for (mdi = a->info.devs; mdi; mdi = mdi->next)
8689 imsm_set_disk(a,
8690 mdi->disk.raid_disk,
8691 mdi->curr_state);
8692
8693 imsm_progress_container_reshape(super);
8694 }
8695 }
8696 }
8697
8698 /* before we activate this array handle any missing disks */
8699 if (consistent == 2)
8700 handle_missing(super, dev);
8701
8702 if (consistent == 2 &&
8703 (!is_resync_complete(&a->info) ||
8704 map_state != IMSM_T_STATE_NORMAL ||
8705 dev->vol.migr_state))
8706 consistent = 0;
8707
8708 if (is_resync_complete(&a->info)) {
8709 /* complete intialization / resync,
8710 * recovery and interrupted recovery is completed in
8711 * ->set_disk
8712 */
8713 if (is_resyncing(dev)) {
8714 dprintf("imsm: mark resync done\n");
8715 end_migration(dev, super, map_state);
8716 super->updates_pending++;
8717 a->last_checkpoint = 0;
8718 }
8719 } else if ((!is_resyncing(dev) && !failed) &&
8720 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
8721 /* mark the start of the init process if nothing is failed */
8722 dprintf("imsm: mark resync start\n");
8723 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
8724 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
8725 else
8726 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
8727 super->updates_pending++;
8728 }
8729
8730 if (a->prev_action == idle)
8731 goto skip_mark_checkpoint;
8732
8733 mark_checkpoint:
8734 /* skip checkpointing for general migration,
8735 * it is controlled in mdadm
8736 */
8737 if (is_gen_migration(dev))
8738 goto skip_mark_checkpoint;
8739
8740 /* check if we can update vol_curr_migr_unit from resync_start,
8741 * recovery_start
8742 */
8743 blocks_per_unit = blocks_per_migr_unit(super, dev);
8744 if (blocks_per_unit) {
8745 set_vol_curr_migr_unit(dev,
8746 a->last_checkpoint / blocks_per_unit);
8747 dprintf("imsm: mark checkpoint (%llu)\n",
8748 vol_curr_migr_unit(dev));
8749 super->updates_pending++;
8750 }
8751
8752 skip_mark_checkpoint:
8753 /* mark dirty / clean */
8754 if (((dev->vol.dirty & RAIDVOL_DIRTY) && consistent) ||
8755 (!(dev->vol.dirty & RAIDVOL_DIRTY) && !consistent)) {
8756 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
8757 if (consistent) {
8758 dev->vol.dirty = RAIDVOL_CLEAN;
8759 } else {
8760 dev->vol.dirty = RAIDVOL_DIRTY;
8761 if (dev->rwh_policy == RWH_DISTRIBUTED ||
8762 dev->rwh_policy == RWH_MULTIPLE_DISTRIBUTED)
8763 dev->vol.dirty |= RAIDVOL_DSRECORD_VALID;
8764 }
8765 super->updates_pending++;
8766 }
8767
8768 return consistent;
8769 }
8770
8771 static int imsm_disk_slot_to_ord(struct active_array *a, int slot)
8772 {
8773 int inst = a->info.container_member;
8774 struct intel_super *super = a->container->sb;
8775 struct imsm_dev *dev = get_imsm_dev(super, inst);
8776 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8777
8778 if (slot > map->num_members) {
8779 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8780 slot, map->num_members - 1);
8781 return -1;
8782 }
8783
8784 if (slot < 0)
8785 return -1;
8786
8787 return get_imsm_ord_tbl_ent(dev, slot, MAP_0);
8788 }
8789
8790 static void imsm_set_disk(struct active_array *a, int n, int state)
8791 {
8792 int inst = a->info.container_member;
8793 struct intel_super *super = a->container->sb;
8794 struct imsm_dev *dev = get_imsm_dev(super, inst);
8795 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8796 struct imsm_disk *disk;
8797 struct mdinfo *mdi;
8798 int recovery_not_finished = 0;
8799 int failed;
8800 int ord;
8801 __u8 map_state;
8802 int rebuild_done = 0;
8803 int i;
8804
8805 ord = get_imsm_ord_tbl_ent(dev, n, MAP_X);
8806 if (ord < 0)
8807 return;
8808
8809 dprintf("imsm: set_disk %d:%x\n", n, state);
8810 disk = get_imsm_disk(super, ord_to_idx(ord));
8811
8812 /* check for new failures */
8813 if (disk && (state & DS_FAULTY)) {
8814 if (mark_failure(super, dev, disk, ord_to_idx(ord)))
8815 super->updates_pending++;
8816 }
8817
8818 /* check if in_sync */
8819 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
8820 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
8821
8822 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
8823 rebuild_done = 1;
8824 super->updates_pending++;
8825 }
8826
8827 failed = imsm_count_failed(super, dev, MAP_0);
8828 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
8829
8830 /* check if recovery complete, newly degraded, or failed */
8831 dprintf("imsm: Detected transition to state ");
8832 switch (map_state) {
8833 case IMSM_T_STATE_NORMAL: /* transition to normal state */
8834 dprintf("normal: ");
8835 if (is_rebuilding(dev)) {
8836 dprintf_cont("while rebuilding");
8837 /* check if recovery is really finished */
8838 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8839 if (mdi->recovery_start != MaxSector) {
8840 recovery_not_finished = 1;
8841 break;
8842 }
8843 if (recovery_not_finished) {
8844 dprintf_cont("\n");
8845 dprintf("Rebuild has not finished yet, state not changed");
8846 if (a->last_checkpoint < mdi->recovery_start) {
8847 a->last_checkpoint = mdi->recovery_start;
8848 super->updates_pending++;
8849 }
8850 break;
8851 }
8852 end_migration(dev, super, map_state);
8853 map->failed_disk_num = ~0;
8854 super->updates_pending++;
8855 a->last_checkpoint = 0;
8856 break;
8857 }
8858 if (is_gen_migration(dev)) {
8859 dprintf_cont("while general migration");
8860 if (a->last_checkpoint >= a->info.component_size)
8861 end_migration(dev, super, map_state);
8862 else
8863 map->map_state = map_state;
8864 map->failed_disk_num = ~0;
8865 super->updates_pending++;
8866 break;
8867 }
8868 break;
8869 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
8870 dprintf_cont("degraded: ");
8871 if (map->map_state != map_state && !dev->vol.migr_state) {
8872 dprintf_cont("mark degraded");
8873 map->map_state = map_state;
8874 super->updates_pending++;
8875 a->last_checkpoint = 0;
8876 break;
8877 }
8878 if (is_rebuilding(dev)) {
8879 dprintf_cont("while rebuilding ");
8880 if (state & DS_FAULTY) {
8881 dprintf_cont("removing failed drive ");
8882 if (n == map->failed_disk_num) {
8883 dprintf_cont("end migration");
8884 end_migration(dev, super, map_state);
8885 a->last_checkpoint = 0;
8886 } else {
8887 dprintf_cont("fail detected during rebuild, changing map state");
8888 map->map_state = map_state;
8889 }
8890 super->updates_pending++;
8891 }
8892
8893 if (!rebuild_done)
8894 break;
8895
8896 /* check if recovery is really finished */
8897 for (mdi = a->info.devs; mdi ; mdi = mdi->next)
8898 if (mdi->recovery_start != MaxSector) {
8899 recovery_not_finished = 1;
8900 break;
8901 }
8902 if (recovery_not_finished) {
8903 dprintf_cont("\n");
8904 dprintf_cont("Rebuild has not finished yet");
8905 if (a->last_checkpoint < mdi->recovery_start) {
8906 a->last_checkpoint =
8907 mdi->recovery_start;
8908 super->updates_pending++;
8909 }
8910 break;
8911 }
8912
8913 dprintf_cont(" Rebuild done, still degraded");
8914 end_migration(dev, super, map_state);
8915 a->last_checkpoint = 0;
8916 super->updates_pending++;
8917
8918 for (i = 0; i < map->num_members; i++) {
8919 int idx = get_imsm_ord_tbl_ent(dev, i, MAP_0);
8920
8921 if (idx & IMSM_ORD_REBUILD)
8922 map->failed_disk_num = i;
8923 }
8924 super->updates_pending++;
8925 break;
8926 }
8927 if (is_gen_migration(dev)) {
8928 dprintf_cont("while general migration");
8929 if (a->last_checkpoint >= a->info.component_size)
8930 end_migration(dev, super, map_state);
8931 else {
8932 map->map_state = map_state;
8933 manage_second_map(super, dev);
8934 }
8935 super->updates_pending++;
8936 break;
8937 }
8938 if (is_initializing(dev)) {
8939 dprintf_cont("while initialization.");
8940 map->map_state = map_state;
8941 super->updates_pending++;
8942 break;
8943 }
8944 break;
8945 case IMSM_T_STATE_FAILED: /* transition to failed state */
8946 dprintf_cont("failed: ");
8947 if (is_gen_migration(dev)) {
8948 dprintf_cont("while general migration");
8949 map->map_state = map_state;
8950 super->updates_pending++;
8951 break;
8952 }
8953 if (map->map_state != map_state) {
8954 dprintf_cont("mark failed");
8955 end_migration(dev, super, map_state);
8956 super->updates_pending++;
8957 a->last_checkpoint = 0;
8958 break;
8959 }
8960 break;
8961 default:
8962 dprintf_cont("state %i\n", map_state);
8963 }
8964 dprintf_cont("\n");
8965 }
8966
8967 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
8968 {
8969 void *buf = mpb;
8970 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
8971 unsigned long long dsize;
8972 unsigned long long sectors;
8973 unsigned int sector_size;
8974
8975 if (!get_dev_sector_size(fd, NULL, &sector_size))
8976 return 1;
8977 get_dev_size(fd, NULL, &dsize);
8978
8979 if (mpb_size > sector_size) {
8980 /* -1 to account for anchor */
8981 sectors = mpb_sectors(mpb, sector_size) - 1;
8982
8983 /* write the extended mpb to the sectors preceeding the anchor */
8984 if (lseek64(fd, dsize - (sector_size * (2 + sectors)),
8985 SEEK_SET) < 0)
8986 return 1;
8987
8988 if ((unsigned long long)write(fd, buf + sector_size,
8989 sector_size * sectors) != sector_size * sectors)
8990 return 1;
8991 }
8992
8993 /* first block is stored on second to last sector of the disk */
8994 if (lseek64(fd, dsize - (sector_size * 2), SEEK_SET) < 0)
8995 return 1;
8996
8997 if ((unsigned int)write(fd, buf, sector_size) != sector_size)
8998 return 1;
8999
9000 return 0;
9001 }
9002
9003 static void imsm_sync_metadata(struct supertype *container)
9004 {
9005 struct intel_super *super = container->sb;
9006
9007 dprintf("sync metadata: %d\n", super->updates_pending);
9008 if (!super->updates_pending)
9009 return;
9010
9011 write_super_imsm(container, 0);
9012
9013 super->updates_pending = 0;
9014 }
9015
9016 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
9017 {
9018 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9019 int i = get_imsm_disk_idx(dev, idx, MAP_X);
9020 struct dl *dl;
9021
9022 for (dl = super->disks; dl; dl = dl->next)
9023 if (dl->index == i)
9024 break;
9025
9026 if (dl && is_failed(&dl->disk))
9027 dl = NULL;
9028
9029 if (dl)
9030 dprintf("found %x:%x\n", dl->major, dl->minor);
9031
9032 return dl;
9033 }
9034
9035 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
9036 struct active_array *a, int activate_new,
9037 struct mdinfo *additional_test_list)
9038 {
9039 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
9040 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
9041 struct imsm_super *mpb = super->anchor;
9042 struct imsm_map *map;
9043 unsigned long long pos;
9044 struct mdinfo *d;
9045 struct extent *ex;
9046 int i, j;
9047 int found;
9048 __u32 array_start = 0;
9049 __u32 array_end = 0;
9050 struct dl *dl;
9051 struct mdinfo *test_list;
9052
9053 for (dl = super->disks; dl; dl = dl->next) {
9054 /* If in this array, skip */
9055 for (d = a->info.devs ; d ; d = d->next)
9056 if (is_fd_valid(d->state_fd) &&
9057 d->disk.major == dl->major &&
9058 d->disk.minor == dl->minor) {
9059 dprintf("%x:%x already in array\n",
9060 dl->major, dl->minor);
9061 break;
9062 }
9063 if (d)
9064 continue;
9065 test_list = additional_test_list;
9066 while (test_list) {
9067 if (test_list->disk.major == dl->major &&
9068 test_list->disk.minor == dl->minor) {
9069 dprintf("%x:%x already in additional test list\n",
9070 dl->major, dl->minor);
9071 break;
9072 }
9073 test_list = test_list->next;
9074 }
9075 if (test_list)
9076 continue;
9077
9078 /* skip in use or failed drives */
9079 if (is_failed(&dl->disk) || idx == dl->index ||
9080 dl->index == -2) {
9081 dprintf("%x:%x status (failed: %d index: %d)\n",
9082 dl->major, dl->minor, is_failed(&dl->disk), idx);
9083 continue;
9084 }
9085
9086 /* skip pure spares when we are looking for partially
9087 * assimilated drives
9088 */
9089 if (dl->index == -1 && !activate_new)
9090 continue;
9091
9092 if (!drive_validate_sector_size(super, dl))
9093 continue;
9094
9095 /* Does this unused device have the requisite free space?
9096 * It needs to be able to cover all member volumes
9097 */
9098 ex = get_extents(super, dl, 1);
9099 if (!ex) {
9100 dprintf("cannot get extents\n");
9101 continue;
9102 }
9103 for (i = 0; i < mpb->num_raid_devs; i++) {
9104 dev = get_imsm_dev(super, i);
9105 map = get_imsm_map(dev, MAP_0);
9106
9107 /* check if this disk is already a member of
9108 * this array
9109 */
9110 if (get_imsm_disk_slot(map, dl->index) >= 0)
9111 continue;
9112
9113 found = 0;
9114 j = 0;
9115 pos = 0;
9116 array_start = pba_of_lba0(map);
9117 array_end = array_start +
9118 per_dev_array_size(map) - 1;
9119
9120 do {
9121 /* check that we can start at pba_of_lba0 with
9122 * num_data_stripes*blocks_per_stripe of space
9123 */
9124 if (array_start >= pos && array_end < ex[j].start) {
9125 found = 1;
9126 break;
9127 }
9128 pos = ex[j].start + ex[j].size;
9129 j++;
9130 } while (ex[j-1].size);
9131
9132 if (!found)
9133 break;
9134 }
9135
9136 free(ex);
9137 if (i < mpb->num_raid_devs) {
9138 dprintf("%x:%x does not have %u to %u available\n",
9139 dl->major, dl->minor, array_start, array_end);
9140 /* No room */
9141 continue;
9142 }
9143 return dl;
9144 }
9145
9146 return dl;
9147 }
9148
9149 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
9150 {
9151 struct imsm_dev *dev2;
9152 struct imsm_map *map;
9153 struct dl *idisk;
9154 int slot;
9155 int idx;
9156 __u8 state;
9157
9158 dev2 = get_imsm_dev(cont->sb, dev_idx);
9159
9160 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
9161 if (state == IMSM_T_STATE_FAILED) {
9162 map = get_imsm_map(dev2, MAP_0);
9163 for (slot = 0; slot < map->num_members; slot++) {
9164 /*
9165 * Check if failed disks are deleted from intel
9166 * disk list or are marked to be deleted
9167 */
9168 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
9169 idisk = get_imsm_dl_disk(cont->sb, idx);
9170 /*
9171 * Do not rebuild the array if failed disks
9172 * from failed sub-array are not removed from
9173 * container.
9174 */
9175 if (idisk &&
9176 is_failed(&idisk->disk) &&
9177 (idisk->action != DISK_REMOVE))
9178 return 0;
9179 }
9180 }
9181 return 1;
9182 }
9183
9184 static struct mdinfo *imsm_activate_spare(struct active_array *a,
9185 struct metadata_update **updates)
9186 {
9187 /**
9188 * Find a device with unused free space and use it to replace a
9189 * failed/vacant region in an array. We replace failed regions one a
9190 * array at a time. The result is that a new spare disk will be added
9191 * to the first failed array and after the monitor has finished
9192 * propagating failures the remainder will be consumed.
9193 *
9194 * FIXME add a capability for mdmon to request spares from another
9195 * container.
9196 */
9197
9198 struct intel_super *super = a->container->sb;
9199 int inst = a->info.container_member;
9200 struct imsm_dev *dev = get_imsm_dev(super, inst);
9201 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9202 int failed = a->info.array.raid_disks;
9203 struct mdinfo *rv = NULL;
9204 struct mdinfo *d;
9205 struct mdinfo *di;
9206 struct metadata_update *mu;
9207 struct dl *dl;
9208 struct imsm_update_activate_spare *u;
9209 int num_spares = 0;
9210 int i;
9211 int allowed;
9212
9213 for (d = a->info.devs ; d; d = d->next) {
9214 if (!is_fd_valid(d->state_fd))
9215 continue;
9216
9217 if (d->curr_state & DS_FAULTY)
9218 /* wait for Removal to happen */
9219 return NULL;
9220
9221 failed--;
9222 }
9223
9224 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
9225 inst, failed, a->info.array.raid_disks, a->info.array.level);
9226
9227 if (imsm_reshape_blocks_arrays_changes(super))
9228 return NULL;
9229
9230 /* Cannot activate another spare if rebuild is in progress already
9231 */
9232 if (is_rebuilding(dev)) {
9233 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
9234 return NULL;
9235 }
9236
9237 if (a->info.array.level == 4)
9238 /* No repair for takeovered array
9239 * imsm doesn't support raid4
9240 */
9241 return NULL;
9242
9243 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
9244 IMSM_T_STATE_DEGRADED)
9245 return NULL;
9246
9247 if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
9248 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
9249 return NULL;
9250 }
9251
9252 /*
9253 * If there are any failed disks check state of the other volume.
9254 * Block rebuild if the another one is failed until failed disks
9255 * are removed from container.
9256 */
9257 if (failed) {
9258 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
9259 MAX_RAID_SERIAL_LEN, dev->volume);
9260 /* check if states of the other volumes allow for rebuild */
9261 for (i = 0; i < super->anchor->num_raid_devs; i++) {
9262 if (i != inst) {
9263 allowed = imsm_rebuild_allowed(a->container,
9264 i, failed);
9265 if (!allowed)
9266 return NULL;
9267 }
9268 }
9269 }
9270
9271 /* For each slot, if it is not working, find a spare */
9272 for (i = 0; i < a->info.array.raid_disks; i++) {
9273 for (d = a->info.devs ; d ; d = d->next)
9274 if (d->disk.raid_disk == i)
9275 break;
9276 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
9277 if (d && is_fd_valid(d->state_fd))
9278 continue;
9279
9280 /*
9281 * OK, this device needs recovery. Try to re-add the
9282 * previous occupant of this slot, if this fails see if
9283 * we can continue the assimilation of a spare that was
9284 * partially assimilated, finally try to activate a new
9285 * spare.
9286 */
9287 dl = imsm_readd(super, i, a);
9288 if (!dl)
9289 dl = imsm_add_spare(super, i, a, 0, rv);
9290 if (!dl)
9291 dl = imsm_add_spare(super, i, a, 1, rv);
9292 if (!dl)
9293 continue;
9294
9295 /* found a usable disk with enough space */
9296 di = xcalloc(1, sizeof(*di));
9297
9298 /* dl->index will be -1 in the case we are activating a
9299 * pristine spare. imsm_process_update() will create a
9300 * new index in this case. Once a disk is found to be
9301 * failed in all member arrays it is kicked from the
9302 * metadata
9303 */
9304 di->disk.number = dl->index;
9305
9306 /* (ab)use di->devs to store a pointer to the device
9307 * we chose
9308 */
9309 di->devs = (struct mdinfo *) dl;
9310
9311 di->disk.raid_disk = i;
9312 di->disk.major = dl->major;
9313 di->disk.minor = dl->minor;
9314 di->disk.state = 0;
9315 di->recovery_start = 0;
9316 di->data_offset = pba_of_lba0(map);
9317 di->component_size = a->info.component_size;
9318 di->container_member = inst;
9319 di->bb.supported = 1;
9320 if (a->info.consistency_policy == CONSISTENCY_POLICY_PPL) {
9321 di->ppl_sector = get_ppl_sector(super, inst);
9322 di->ppl_size = MULTIPLE_PPL_AREA_SIZE_IMSM >> 9;
9323 }
9324 super->random = random32();
9325 di->next = rv;
9326 rv = di;
9327 num_spares++;
9328 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
9329 i, di->data_offset);
9330 }
9331
9332 if (!rv)
9333 /* No spares found */
9334 return rv;
9335 /* Now 'rv' has a list of devices to return.
9336 * Create a metadata_update record to update the
9337 * disk_ord_tbl for the array
9338 */
9339 mu = xmalloc(sizeof(*mu));
9340 mu->buf = xcalloc(num_spares,
9341 sizeof(struct imsm_update_activate_spare));
9342 mu->space = NULL;
9343 mu->space_list = NULL;
9344 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
9345 mu->next = *updates;
9346 u = (struct imsm_update_activate_spare *) mu->buf;
9347
9348 for (di = rv ; di ; di = di->next) {
9349 u->type = update_activate_spare;
9350 u->dl = (struct dl *) di->devs;
9351 di->devs = NULL;
9352 u->slot = di->disk.raid_disk;
9353 u->array = inst;
9354 u->next = u + 1;
9355 u++;
9356 }
9357 (u-1)->next = NULL;
9358 *updates = mu;
9359
9360 return rv;
9361 }
9362
9363 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
9364 {
9365 struct imsm_dev *dev = get_imsm_dev(super, idx);
9366 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9367 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
9368 struct disk_info *inf = get_disk_info(u);
9369 struct imsm_disk *disk;
9370 int i;
9371 int j;
9372
9373 for (i = 0; i < map->num_members; i++) {
9374 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
9375 for (j = 0; j < new_map->num_members; j++)
9376 if (serialcmp(disk->serial, inf[j].serial) == 0)
9377 return 1;
9378 }
9379
9380 return 0;
9381 }
9382
9383 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
9384 {
9385 struct dl *dl;
9386
9387 for (dl = super->disks; dl; dl = dl->next)
9388 if (dl->major == major && dl->minor == minor)
9389 return dl;
9390 return NULL;
9391 }
9392
9393 static int remove_disk_super(struct intel_super *super, int major, int minor)
9394 {
9395 struct dl *prev;
9396 struct dl *dl;
9397
9398 prev = NULL;
9399 for (dl = super->disks; dl; dl = dl->next) {
9400 if (dl->major == major && dl->minor == minor) {
9401 /* remove */
9402 if (prev)
9403 prev->next = dl->next;
9404 else
9405 super->disks = dl->next;
9406 dl->next = NULL;
9407 __free_imsm_disk(dl, 1);
9408 dprintf("removed %x:%x\n", major, minor);
9409 break;
9410 }
9411 prev = dl;
9412 }
9413 return 0;
9414 }
9415
9416 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
9417
9418 static int add_remove_disk_update(struct intel_super *super)
9419 {
9420 int check_degraded = 0;
9421 struct dl *disk;
9422
9423 /* add/remove some spares to/from the metadata/contrainer */
9424 while (super->disk_mgmt_list) {
9425 struct dl *disk_cfg;
9426
9427 disk_cfg = super->disk_mgmt_list;
9428 super->disk_mgmt_list = disk_cfg->next;
9429 disk_cfg->next = NULL;
9430
9431 if (disk_cfg->action == DISK_ADD) {
9432 disk_cfg->next = super->disks;
9433 super->disks = disk_cfg;
9434 check_degraded = 1;
9435 dprintf("added %x:%x\n",
9436 disk_cfg->major, disk_cfg->minor);
9437 } else if (disk_cfg->action == DISK_REMOVE) {
9438 dprintf("Disk remove action processed: %x.%x\n",
9439 disk_cfg->major, disk_cfg->minor);
9440 disk = get_disk_super(super,
9441 disk_cfg->major,
9442 disk_cfg->minor);
9443 if (disk) {
9444 /* store action status */
9445 disk->action = DISK_REMOVE;
9446 /* remove spare disks only */
9447 if (disk->index == -1) {
9448 remove_disk_super(super,
9449 disk_cfg->major,
9450 disk_cfg->minor);
9451 } else {
9452 disk_cfg->fd = disk->fd;
9453 disk->fd = -1;
9454 }
9455 }
9456 /* release allocate disk structure */
9457 __free_imsm_disk(disk_cfg, 1);
9458 }
9459 }
9460 return check_degraded;
9461 }
9462
9463 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
9464 struct intel_super *super,
9465 void ***space_list)
9466 {
9467 struct intel_dev *id;
9468 void **tofree = NULL;
9469 int ret_val = 0;
9470
9471 dprintf("(enter)\n");
9472 if (u->subdev < 0 || u->subdev > 1) {
9473 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9474 return ret_val;
9475 }
9476 if (space_list == NULL || *space_list == NULL) {
9477 dprintf("imsm: Error: Memory is not allocated\n");
9478 return ret_val;
9479 }
9480
9481 for (id = super->devlist ; id; id = id->next) {
9482 if (id->index == (unsigned)u->subdev) {
9483 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9484 struct imsm_map *map;
9485 struct imsm_dev *new_dev =
9486 (struct imsm_dev *)*space_list;
9487 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
9488 int to_state;
9489 struct dl *new_disk;
9490
9491 if (new_dev == NULL)
9492 return ret_val;
9493 *space_list = **space_list;
9494 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
9495 map = get_imsm_map(new_dev, MAP_0);
9496 if (migr_map) {
9497 dprintf("imsm: Error: migration in progress");
9498 return ret_val;
9499 }
9500
9501 to_state = map->map_state;
9502 if ((u->new_level == 5) && (map->raid_level == 0)) {
9503 map->num_members++;
9504 /* this should not happen */
9505 if (u->new_disks[0] < 0) {
9506 map->failed_disk_num =
9507 map->num_members - 1;
9508 to_state = IMSM_T_STATE_DEGRADED;
9509 } else
9510 to_state = IMSM_T_STATE_NORMAL;
9511 }
9512 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
9513 if (u->new_level > -1)
9514 map->raid_level = u->new_level;
9515 migr_map = get_imsm_map(new_dev, MAP_1);
9516 if ((u->new_level == 5) &&
9517 (migr_map->raid_level == 0)) {
9518 int ord = map->num_members - 1;
9519 migr_map->num_members--;
9520 if (u->new_disks[0] < 0)
9521 ord |= IMSM_ORD_REBUILD;
9522 set_imsm_ord_tbl_ent(map,
9523 map->num_members - 1,
9524 ord);
9525 }
9526 id->dev = new_dev;
9527 tofree = (void **)dev;
9528
9529 /* update chunk size
9530 */
9531 if (u->new_chunksize > 0) {
9532 struct imsm_map *dest_map =
9533 get_imsm_map(dev, MAP_0);
9534 int used_disks =
9535 imsm_num_data_members(dest_map);
9536
9537 if (used_disks == 0)
9538 return ret_val;
9539
9540 map->blocks_per_strip =
9541 __cpu_to_le16(u->new_chunksize * 2);
9542 update_num_data_stripes(map, imsm_dev_size(dev));
9543 }
9544
9545 /* ensure blocks_per_member has valid value
9546 */
9547 set_blocks_per_member(map,
9548 per_dev_array_size(map) +
9549 NUM_BLOCKS_DIRTY_STRIPE_REGION);
9550
9551 /* add disk
9552 */
9553 if (u->new_level != 5 || migr_map->raid_level != 0 ||
9554 migr_map->raid_level == map->raid_level)
9555 goto skip_disk_add;
9556
9557 if (u->new_disks[0] >= 0) {
9558 /* use passes spare
9559 */
9560 new_disk = get_disk_super(super,
9561 major(u->new_disks[0]),
9562 minor(u->new_disks[0]));
9563 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9564 major(u->new_disks[0]),
9565 minor(u->new_disks[0]),
9566 new_disk, new_disk->index);
9567 if (new_disk == NULL)
9568 goto error_disk_add;
9569
9570 new_disk->index = map->num_members - 1;
9571 /* slot to fill in autolayout
9572 */
9573 new_disk->raiddisk = new_disk->index;
9574 new_disk->disk.status |= CONFIGURED_DISK;
9575 new_disk->disk.status &= ~SPARE_DISK;
9576 } else
9577 goto error_disk_add;
9578
9579 skip_disk_add:
9580 *tofree = *space_list;
9581 /* calculate new size
9582 */
9583 imsm_set_array_size(new_dev, -1);
9584
9585 ret_val = 1;
9586 }
9587 }
9588
9589 if (tofree)
9590 *space_list = tofree;
9591 return ret_val;
9592
9593 error_disk_add:
9594 dprintf("Error: imsm: Cannot find disk.\n");
9595 return ret_val;
9596 }
9597
9598 static int apply_size_change_update(struct imsm_update_size_change *u,
9599 struct intel_super *super)
9600 {
9601 struct intel_dev *id;
9602 int ret_val = 0;
9603
9604 dprintf("(enter)\n");
9605 if (u->subdev < 0 || u->subdev > 1) {
9606 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
9607 return ret_val;
9608 }
9609
9610 for (id = super->devlist ; id; id = id->next) {
9611 if (id->index == (unsigned)u->subdev) {
9612 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
9613 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9614 int used_disks = imsm_num_data_members(map);
9615 unsigned long long blocks_per_member;
9616 unsigned long long new_size_per_disk;
9617
9618 if (used_disks == 0)
9619 return 0;
9620
9621 /* calculate new size
9622 */
9623 new_size_per_disk = u->new_size / used_disks;
9624 blocks_per_member = new_size_per_disk +
9625 NUM_BLOCKS_DIRTY_STRIPE_REGION;
9626
9627 imsm_set_array_size(dev, u->new_size);
9628 set_blocks_per_member(map, blocks_per_member);
9629 update_num_data_stripes(map, u->new_size);
9630 ret_val = 1;
9631 break;
9632 }
9633 }
9634
9635 return ret_val;
9636 }
9637
9638 static int prepare_spare_to_activate(struct supertype *st,
9639 struct imsm_update_activate_spare *u)
9640 {
9641 struct intel_super *super = st->sb;
9642 int prev_current_vol = super->current_vol;
9643 struct active_array *a;
9644 int ret = 1;
9645
9646 for (a = st->arrays; a; a = a->next)
9647 /*
9648 * Additional initialization (adding bitmap header, filling
9649 * the bitmap area with '1's to force initial rebuild for a whole
9650 * data-area) is required when adding the spare to the volume
9651 * with write-intent bitmap.
9652 */
9653 if (a->info.container_member == u->array &&
9654 a->info.consistency_policy == CONSISTENCY_POLICY_BITMAP) {
9655 struct dl *dl;
9656
9657 for (dl = super->disks; dl; dl = dl->next)
9658 if (dl == u->dl)
9659 break;
9660 if (!dl)
9661 break;
9662
9663 super->current_vol = u->array;
9664 if (st->ss->write_bitmap(st, dl->fd, NoUpdate))
9665 ret = 0;
9666 super->current_vol = prev_current_vol;
9667 }
9668 return ret;
9669 }
9670
9671 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
9672 struct intel_super *super,
9673 struct active_array *active_array)
9674 {
9675 struct imsm_super *mpb = super->anchor;
9676 struct imsm_dev *dev = get_imsm_dev(super, u->array);
9677 struct imsm_map *map = get_imsm_map(dev, MAP_0);
9678 struct imsm_map *migr_map;
9679 struct active_array *a;
9680 struct imsm_disk *disk;
9681 __u8 to_state;
9682 struct dl *dl;
9683 unsigned int found;
9684 int failed;
9685 int victim;
9686 int i;
9687 int second_map_created = 0;
9688
9689 for (; u; u = u->next) {
9690 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
9691
9692 if (victim < 0)
9693 return 0;
9694
9695 for (dl = super->disks; dl; dl = dl->next)
9696 if (dl == u->dl)
9697 break;
9698
9699 if (!dl) {
9700 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9701 u->dl->index);
9702 return 0;
9703 }
9704
9705 /* count failures (excluding rebuilds and the victim)
9706 * to determine map[0] state
9707 */
9708 failed = 0;
9709 for (i = 0; i < map->num_members; i++) {
9710 if (i == u->slot)
9711 continue;
9712 disk = get_imsm_disk(super,
9713 get_imsm_disk_idx(dev, i, MAP_X));
9714 if (!disk || is_failed(disk))
9715 failed++;
9716 }
9717
9718 /* adding a pristine spare, assign a new index */
9719 if (dl->index < 0) {
9720 dl->index = super->anchor->num_disks;
9721 super->anchor->num_disks++;
9722 }
9723 disk = &dl->disk;
9724 disk->status |= CONFIGURED_DISK;
9725 disk->status &= ~SPARE_DISK;
9726
9727 /* mark rebuild */
9728 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
9729 if (!second_map_created) {
9730 second_map_created = 1;
9731 map->map_state = IMSM_T_STATE_DEGRADED;
9732 migrate(dev, super, to_state, MIGR_REBUILD);
9733 } else
9734 map->map_state = to_state;
9735 migr_map = get_imsm_map(dev, MAP_1);
9736 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
9737 set_imsm_ord_tbl_ent(migr_map, u->slot,
9738 dl->index | IMSM_ORD_REBUILD);
9739
9740 /* update the family_num to mark a new container
9741 * generation, being careful to record the existing
9742 * family_num in orig_family_num to clean up after
9743 * earlier mdadm versions that neglected to set it.
9744 */
9745 if (mpb->orig_family_num == 0)
9746 mpb->orig_family_num = mpb->family_num;
9747 mpb->family_num += super->random;
9748
9749 /* count arrays using the victim in the metadata */
9750 found = 0;
9751 for (a = active_array; a ; a = a->next) {
9752 int dev_idx = a->info.container_member;
9753
9754 if (get_disk_slot_in_dev(super, dev_idx, victim) >= 0)
9755 found++;
9756 }
9757
9758 /* delete the victim if it is no longer being
9759 * utilized anywhere
9760 */
9761 if (!found) {
9762 struct dl **dlp;
9763
9764 /* We know that 'manager' isn't touching anything,
9765 * so it is safe to delete
9766 */
9767 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
9768 if ((*dlp)->index == victim)
9769 break;
9770
9771 /* victim may be on the missing list */
9772 if (!*dlp)
9773 for (dlp = &super->missing; *dlp;
9774 dlp = &(*dlp)->next)
9775 if ((*dlp)->index == victim)
9776 break;
9777 imsm_delete(super, dlp, victim);
9778 }
9779 }
9780
9781 return 1;
9782 }
9783
9784 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
9785 struct intel_super *super,
9786 void ***space_list)
9787 {
9788 struct dl *new_disk;
9789 struct intel_dev *id;
9790 int i;
9791 int delta_disks = u->new_raid_disks - u->old_raid_disks;
9792 int disk_count = u->old_raid_disks;
9793 void **tofree = NULL;
9794 int devices_to_reshape = 1;
9795 struct imsm_super *mpb = super->anchor;
9796 int ret_val = 0;
9797 unsigned int dev_id;
9798
9799 dprintf("(enter)\n");
9800
9801 /* enable spares to use in array */
9802 for (i = 0; i < delta_disks; i++) {
9803 new_disk = get_disk_super(super,
9804 major(u->new_disks[i]),
9805 minor(u->new_disks[i]));
9806 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9807 major(u->new_disks[i]), minor(u->new_disks[i]),
9808 new_disk, new_disk->index);
9809 if (new_disk == NULL ||
9810 (new_disk->index >= 0 &&
9811 new_disk->index < u->old_raid_disks))
9812 goto update_reshape_exit;
9813 new_disk->index = disk_count++;
9814 /* slot to fill in autolayout
9815 */
9816 new_disk->raiddisk = new_disk->index;
9817 new_disk->disk.status |=
9818 CONFIGURED_DISK;
9819 new_disk->disk.status &= ~SPARE_DISK;
9820 }
9821
9822 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9823 mpb->num_raid_devs);
9824 /* manage changes in volume
9825 */
9826 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
9827 void **sp = *space_list;
9828 struct imsm_dev *newdev;
9829 struct imsm_map *newmap, *oldmap;
9830
9831 for (id = super->devlist ; id; id = id->next) {
9832 if (id->index == dev_id)
9833 break;
9834 }
9835 if (id == NULL)
9836 break;
9837 if (!sp)
9838 continue;
9839 *space_list = *sp;
9840 newdev = (void*)sp;
9841 /* Copy the dev, but not (all of) the map */
9842 memcpy(newdev, id->dev, sizeof(*newdev));
9843 oldmap = get_imsm_map(id->dev, MAP_0);
9844 newmap = get_imsm_map(newdev, MAP_0);
9845 /* Copy the current map */
9846 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9847 /* update one device only
9848 */
9849 if (devices_to_reshape) {
9850 dprintf("imsm: modifying subdev: %i\n",
9851 id->index);
9852 devices_to_reshape--;
9853 newdev->vol.migr_state = 1;
9854 set_vol_curr_migr_unit(newdev, 0);
9855 set_migr_type(newdev, MIGR_GEN_MIGR);
9856 newmap->num_members = u->new_raid_disks;
9857 for (i = 0; i < delta_disks; i++) {
9858 set_imsm_ord_tbl_ent(newmap,
9859 u->old_raid_disks + i,
9860 u->old_raid_disks + i);
9861 }
9862 /* New map is correct, now need to save old map
9863 */
9864 newmap = get_imsm_map(newdev, MAP_1);
9865 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
9866
9867 imsm_set_array_size(newdev, -1);
9868 }
9869
9870 sp = (void **)id->dev;
9871 id->dev = newdev;
9872 *sp = tofree;
9873 tofree = sp;
9874
9875 /* Clear migration record */
9876 memset(super->migr_rec, 0, sizeof(struct migr_record));
9877 }
9878 if (tofree)
9879 *space_list = tofree;
9880 ret_val = 1;
9881
9882 update_reshape_exit:
9883
9884 return ret_val;
9885 }
9886
9887 static int apply_takeover_update(struct imsm_update_takeover *u,
9888 struct intel_super *super,
9889 void ***space_list)
9890 {
9891 struct imsm_dev *dev = NULL;
9892 struct intel_dev *dv;
9893 struct imsm_dev *dev_new;
9894 struct imsm_map *map;
9895 struct dl *dm, *du;
9896 int i;
9897
9898 for (dv = super->devlist; dv; dv = dv->next)
9899 if (dv->index == (unsigned int)u->subarray) {
9900 dev = dv->dev;
9901 break;
9902 }
9903
9904 if (dev == NULL)
9905 return 0;
9906
9907 map = get_imsm_map(dev, MAP_0);
9908
9909 if (u->direction == R10_TO_R0) {
9910 /* Number of failed disks must be half of initial disk number */
9911 if (imsm_count_failed(super, dev, MAP_0) !=
9912 (map->num_members / 2))
9913 return 0;
9914
9915 /* iterate through devices to mark removed disks as spare */
9916 for (dm = super->disks; dm; dm = dm->next) {
9917 if (dm->disk.status & FAILED_DISK) {
9918 int idx = dm->index;
9919 /* update indexes on the disk list */
9920 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9921 the index values will end up being correct.... NB */
9922 for (du = super->disks; du; du = du->next)
9923 if (du->index > idx)
9924 du->index--;
9925 /* mark as spare disk */
9926 mark_spare(dm);
9927 }
9928 }
9929 /* update map */
9930 map->num_members /= map->num_domains;
9931 map->map_state = IMSM_T_STATE_NORMAL;
9932 map->raid_level = 0;
9933 set_num_domains(map);
9934 update_num_data_stripes(map, imsm_dev_size(dev));
9935 map->failed_disk_num = -1;
9936 }
9937
9938 if (u->direction == R0_TO_R10) {
9939 void **space;
9940
9941 /* update slots in current disk list */
9942 for (dm = super->disks; dm; dm = dm->next) {
9943 if (dm->index >= 0)
9944 dm->index *= 2;
9945 }
9946 /* create new *missing* disks */
9947 for (i = 0; i < map->num_members; i++) {
9948 space = *space_list;
9949 if (!space)
9950 continue;
9951 *space_list = *space;
9952 du = (void *)space;
9953 memcpy(du, super->disks, sizeof(*du));
9954 du->fd = -1;
9955 du->minor = 0;
9956 du->major = 0;
9957 du->index = (i * 2) + 1;
9958 sprintf((char *)du->disk.serial,
9959 " MISSING_%d", du->index);
9960 sprintf((char *)du->serial,
9961 "MISSING_%d", du->index);
9962 du->next = super->missing;
9963 super->missing = du;
9964 }
9965 /* create new dev and map */
9966 space = *space_list;
9967 if (!space)
9968 return 0;
9969 *space_list = *space;
9970 dev_new = (void *)space;
9971 memcpy(dev_new, dev, sizeof(*dev));
9972 /* update new map */
9973 map = get_imsm_map(dev_new, MAP_0);
9974
9975 map->map_state = IMSM_T_STATE_DEGRADED;
9976 map->raid_level = 1;
9977 set_num_domains(map);
9978 map->num_members = map->num_members * map->num_domains;
9979 update_num_data_stripes(map, imsm_dev_size(dev));
9980
9981 /* replace dev<->dev_new */
9982 dv->dev = dev_new;
9983 }
9984 /* update disk order table */
9985 for (du = super->disks; du; du = du->next)
9986 if (du->index >= 0)
9987 set_imsm_ord_tbl_ent(map, du->index, du->index);
9988 for (du = super->missing; du; du = du->next)
9989 if (du->index >= 0) {
9990 set_imsm_ord_tbl_ent(map, du->index, du->index);
9991 mark_missing(super, dv->dev, &du->disk, du->index);
9992 }
9993
9994 return 1;
9995 }
9996
9997 static void imsm_process_update(struct supertype *st,
9998 struct metadata_update *update)
9999 {
10000 /**
10001 * crack open the metadata_update envelope to find the update record
10002 * update can be one of:
10003 * update_reshape_container_disks - all the arrays in the container
10004 * are being reshaped to have more devices. We need to mark
10005 * the arrays for general migration and convert selected spares
10006 * into active devices.
10007 * update_activate_spare - a spare device has replaced a failed
10008 * device in an array, update the disk_ord_tbl. If this disk is
10009 * present in all member arrays then also clear the SPARE_DISK
10010 * flag
10011 * update_create_array
10012 * update_kill_array
10013 * update_rename_array
10014 * update_add_remove_disk
10015 */
10016 struct intel_super *super = st->sb;
10017 struct imsm_super *mpb;
10018 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
10019
10020 /* update requires a larger buf but the allocation failed */
10021 if (super->next_len && !super->next_buf) {
10022 super->next_len = 0;
10023 return;
10024 }
10025
10026 if (super->next_buf) {
10027 memcpy(super->next_buf, super->buf, super->len);
10028 free(super->buf);
10029 super->len = super->next_len;
10030 super->buf = super->next_buf;
10031
10032 super->next_len = 0;
10033 super->next_buf = NULL;
10034 }
10035
10036 mpb = super->anchor;
10037
10038 switch (type) {
10039 case update_general_migration_checkpoint: {
10040 struct intel_dev *id;
10041 struct imsm_update_general_migration_checkpoint *u =
10042 (void *)update->buf;
10043
10044 dprintf("called for update_general_migration_checkpoint\n");
10045
10046 /* find device under general migration */
10047 for (id = super->devlist ; id; id = id->next) {
10048 if (is_gen_migration(id->dev)) {
10049 set_vol_curr_migr_unit(id->dev,
10050 u->curr_migr_unit);
10051 super->updates_pending++;
10052 }
10053 }
10054 break;
10055 }
10056 case update_takeover: {
10057 struct imsm_update_takeover *u = (void *)update->buf;
10058 if (apply_takeover_update(u, super, &update->space_list)) {
10059 imsm_update_version_info(super);
10060 super->updates_pending++;
10061 }
10062 break;
10063 }
10064
10065 case update_reshape_container_disks: {
10066 struct imsm_update_reshape *u = (void *)update->buf;
10067 if (apply_reshape_container_disks_update(
10068 u, super, &update->space_list))
10069 super->updates_pending++;
10070 break;
10071 }
10072 case update_reshape_migration: {
10073 struct imsm_update_reshape_migration *u = (void *)update->buf;
10074 if (apply_reshape_migration_update(
10075 u, super, &update->space_list))
10076 super->updates_pending++;
10077 break;
10078 }
10079 case update_size_change: {
10080 struct imsm_update_size_change *u = (void *)update->buf;
10081 if (apply_size_change_update(u, super))
10082 super->updates_pending++;
10083 break;
10084 }
10085 case update_activate_spare: {
10086 struct imsm_update_activate_spare *u = (void *) update->buf;
10087
10088 if (prepare_spare_to_activate(st, u) &&
10089 apply_update_activate_spare(u, super, st->arrays))
10090 super->updates_pending++;
10091 break;
10092 }
10093 case update_create_array: {
10094 /* someone wants to create a new array, we need to be aware of
10095 * a few races/collisions:
10096 * 1/ 'Create' called by two separate instances of mdadm
10097 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
10098 * devices that have since been assimilated via
10099 * activate_spare.
10100 * In the event this update can not be carried out mdadm will
10101 * (FIX ME) notice that its update did not take hold.
10102 */
10103 struct imsm_update_create_array *u = (void *) update->buf;
10104 struct intel_dev *dv;
10105 struct imsm_dev *dev;
10106 struct imsm_map *map, *new_map;
10107 unsigned long long start, end;
10108 unsigned long long new_start, new_end;
10109 int i;
10110 struct disk_info *inf;
10111 struct dl *dl;
10112
10113 /* handle racing creates: first come first serve */
10114 if (u->dev_idx < mpb->num_raid_devs) {
10115 dprintf("subarray %d already defined\n", u->dev_idx);
10116 goto create_error;
10117 }
10118
10119 /* check update is next in sequence */
10120 if (u->dev_idx != mpb->num_raid_devs) {
10121 dprintf("can not create array %d expected index %d\n",
10122 u->dev_idx, mpb->num_raid_devs);
10123 goto create_error;
10124 }
10125
10126 new_map = get_imsm_map(&u->dev, MAP_0);
10127 new_start = pba_of_lba0(new_map);
10128 new_end = new_start + per_dev_array_size(new_map);
10129 inf = get_disk_info(u);
10130
10131 /* handle activate_spare versus create race:
10132 * check to make sure that overlapping arrays do not include
10133 * overalpping disks
10134 */
10135 for (i = 0; i < mpb->num_raid_devs; i++) {
10136 dev = get_imsm_dev(super, i);
10137 map = get_imsm_map(dev, MAP_0);
10138 start = pba_of_lba0(map);
10139 end = start + per_dev_array_size(map);
10140 if ((new_start >= start && new_start <= end) ||
10141 (start >= new_start && start <= new_end))
10142 /* overlap */;
10143 else
10144 continue;
10145
10146 if (disks_overlap(super, i, u)) {
10147 dprintf("arrays overlap\n");
10148 goto create_error;
10149 }
10150 }
10151
10152 /* check that prepare update was successful */
10153 if (!update->space) {
10154 dprintf("prepare update failed\n");
10155 goto create_error;
10156 }
10157
10158 /* check that all disks are still active before committing
10159 * changes. FIXME: could we instead handle this by creating a
10160 * degraded array? That's probably not what the user expects,
10161 * so better to drop this update on the floor.
10162 */
10163 for (i = 0; i < new_map->num_members; i++) {
10164 dl = serial_to_dl(inf[i].serial, super);
10165 if (!dl) {
10166 dprintf("disk disappeared\n");
10167 goto create_error;
10168 }
10169 }
10170
10171 super->updates_pending++;
10172
10173 /* convert spares to members and fixup ord_tbl */
10174 for (i = 0; i < new_map->num_members; i++) {
10175 dl = serial_to_dl(inf[i].serial, super);
10176 if (dl->index == -1) {
10177 dl->index = mpb->num_disks;
10178 mpb->num_disks++;
10179 dl->disk.status |= CONFIGURED_DISK;
10180 dl->disk.status &= ~SPARE_DISK;
10181 }
10182 set_imsm_ord_tbl_ent(new_map, i, dl->index);
10183 }
10184
10185 dv = update->space;
10186 dev = dv->dev;
10187 update->space = NULL;
10188 imsm_copy_dev(dev, &u->dev);
10189 dv->index = u->dev_idx;
10190 dv->next = super->devlist;
10191 super->devlist = dv;
10192 mpb->num_raid_devs++;
10193
10194 imsm_update_version_info(super);
10195 break;
10196 create_error:
10197 /* mdmon knows how to release update->space, but not
10198 * ((struct intel_dev *) update->space)->dev
10199 */
10200 if (update->space) {
10201 dv = update->space;
10202 free(dv->dev);
10203 }
10204 break;
10205 }
10206 case update_kill_array: {
10207 struct imsm_update_kill_array *u = (void *) update->buf;
10208 int victim = u->dev_idx;
10209 struct active_array *a;
10210 struct intel_dev **dp;
10211
10212 /* sanity check that we are not affecting the uuid of
10213 * active arrays, or deleting an active array
10214 *
10215 * FIXME when immutable ids are available, but note that
10216 * we'll also need to fixup the invalidated/active
10217 * subarray indexes in mdstat
10218 */
10219 for (a = st->arrays; a; a = a->next)
10220 if (a->info.container_member >= victim)
10221 break;
10222 /* by definition if mdmon is running at least one array
10223 * is active in the container, so checking
10224 * mpb->num_raid_devs is just extra paranoia
10225 */
10226 if (a || mpb->num_raid_devs == 1 || victim >= super->anchor->num_raid_devs) {
10227 dprintf("failed to delete subarray-%d\n", victim);
10228 break;
10229 }
10230
10231 for (dp = &super->devlist; *dp;)
10232 if ((*dp)->index == (unsigned)super->current_vol) {
10233 *dp = (*dp)->next;
10234 } else {
10235 if ((*dp)->index > (unsigned)victim)
10236 (*dp)->index--;
10237 dp = &(*dp)->next;
10238 }
10239 mpb->num_raid_devs--;
10240 super->updates_pending++;
10241 break;
10242 }
10243 case update_rename_array: {
10244 struct imsm_update_rename_array *u = (void *) update->buf;
10245 char name[MAX_RAID_SERIAL_LEN+1];
10246 int target = u->dev_idx;
10247 struct active_array *a;
10248 struct imsm_dev *dev;
10249
10250 /* sanity check that we are not affecting the uuid of
10251 * an active array
10252 */
10253 memset(name, 0, sizeof(name));
10254 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
10255 name[MAX_RAID_SERIAL_LEN] = '\0';
10256 for (a = st->arrays; a; a = a->next)
10257 if (a->info.container_member == target)
10258 break;
10259 dev = get_imsm_dev(super, u->dev_idx);
10260
10261 if (a || !dev || imsm_is_name_allowed(super, name, 0) == false) {
10262 dprintf("failed to rename subarray-%d\n", target);
10263 break;
10264 }
10265
10266 memcpy(dev->volume, name, MAX_RAID_SERIAL_LEN);
10267 super->updates_pending++;
10268 break;
10269 }
10270 case update_add_remove_disk: {
10271 /* we may be able to repair some arrays if disks are
10272 * being added, check the status of add_remove_disk
10273 * if discs has been added.
10274 */
10275 if (add_remove_disk_update(super)) {
10276 struct active_array *a;
10277
10278 super->updates_pending++;
10279 for (a = st->arrays; a; a = a->next)
10280 a->check_degraded = 1;
10281 }
10282 break;
10283 }
10284 case update_prealloc_badblocks_mem:
10285 break;
10286 case update_rwh_policy: {
10287 struct imsm_update_rwh_policy *u = (void *)update->buf;
10288 int target = u->dev_idx;
10289 struct imsm_dev *dev = get_imsm_dev(super, target);
10290
10291 if (dev->rwh_policy != u->new_policy) {
10292 dev->rwh_policy = u->new_policy;
10293 super->updates_pending++;
10294 }
10295 break;
10296 }
10297 default:
10298 pr_err("error: unsupported process update type:(type: %d)\n", type);
10299 }
10300 }
10301
10302 static struct mdinfo *get_spares_for_grow(struct supertype *st);
10303
10304 static int imsm_prepare_update(struct supertype *st,
10305 struct metadata_update *update)
10306 {
10307 /**
10308 * Allocate space to hold new disk entries, raid-device entries or a new
10309 * mpb if necessary. The manager synchronously waits for updates to
10310 * complete in the monitor, so new mpb buffers allocated here can be
10311 * integrated by the monitor thread without worrying about live pointers
10312 * in the manager thread.
10313 */
10314 enum imsm_update_type type;
10315 struct intel_super *super = st->sb;
10316 unsigned int sector_size = super->sector_size;
10317 struct imsm_super *mpb = super->anchor;
10318 size_t buf_len;
10319 size_t len = 0;
10320
10321 if (update->len < (int)sizeof(type))
10322 return 0;
10323
10324 type = *(enum imsm_update_type *) update->buf;
10325
10326 switch (type) {
10327 case update_general_migration_checkpoint:
10328 if (update->len < (int)sizeof(struct imsm_update_general_migration_checkpoint))
10329 return 0;
10330 dprintf("called for update_general_migration_checkpoint\n");
10331 break;
10332 case update_takeover: {
10333 struct imsm_update_takeover *u = (void *)update->buf;
10334 if (update->len < (int)sizeof(*u))
10335 return 0;
10336 if (u->direction == R0_TO_R10) {
10337 void **tail = (void **)&update->space_list;
10338 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
10339 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10340 int num_members = map->num_members;
10341 void *space;
10342 int size, i;
10343 /* allocate memory for added disks */
10344 for (i = 0; i < num_members; i++) {
10345 size = sizeof(struct dl);
10346 space = xmalloc(size);
10347 *tail = space;
10348 tail = space;
10349 *tail = NULL;
10350 }
10351 /* allocate memory for new device */
10352 size = sizeof_imsm_dev(super->devlist->dev, 0) +
10353 (num_members * sizeof(__u32));
10354 space = xmalloc(size);
10355 *tail = space;
10356 tail = space;
10357 *tail = NULL;
10358 len = disks_to_mpb_size(num_members * 2);
10359 }
10360
10361 break;
10362 }
10363 case update_reshape_container_disks: {
10364 /* Every raid device in the container is about to
10365 * gain some more devices, and we will enter a
10366 * reconfiguration.
10367 * So each 'imsm_map' will be bigger, and the imsm_vol
10368 * will now hold 2 of them.
10369 * Thus we need new 'struct imsm_dev' allocations sized
10370 * as sizeof_imsm_dev but with more devices in both maps.
10371 */
10372 struct imsm_update_reshape *u = (void *)update->buf;
10373 struct intel_dev *dl;
10374 void **space_tail = (void**)&update->space_list;
10375
10376 if (update->len < (int)sizeof(*u))
10377 return 0;
10378
10379 dprintf("for update_reshape\n");
10380
10381 for (dl = super->devlist; dl; dl = dl->next) {
10382 int size = sizeof_imsm_dev(dl->dev, 1);
10383 void *s;
10384 if (u->new_raid_disks > u->old_raid_disks)
10385 size += sizeof(__u32)*2*
10386 (u->new_raid_disks - u->old_raid_disks);
10387 s = xmalloc(size);
10388 *space_tail = s;
10389 space_tail = s;
10390 *space_tail = NULL;
10391 }
10392
10393 len = disks_to_mpb_size(u->new_raid_disks);
10394 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10395 break;
10396 }
10397 case update_reshape_migration: {
10398 /* for migration level 0->5 we need to add disks
10399 * so the same as for container operation we will copy
10400 * device to the bigger location.
10401 * in memory prepared device and new disk area are prepared
10402 * for usage in process update
10403 */
10404 struct imsm_update_reshape_migration *u = (void *)update->buf;
10405 struct intel_dev *id;
10406 void **space_tail = (void **)&update->space_list;
10407 int size;
10408 void *s;
10409 int current_level = -1;
10410
10411 if (update->len < (int)sizeof(*u))
10412 return 0;
10413
10414 dprintf("for update_reshape\n");
10415
10416 /* add space for bigger array in update
10417 */
10418 for (id = super->devlist; id; id = id->next) {
10419 if (id->index == (unsigned)u->subdev) {
10420 size = sizeof_imsm_dev(id->dev, 1);
10421 if (u->new_raid_disks > u->old_raid_disks)
10422 size += sizeof(__u32)*2*
10423 (u->new_raid_disks - u->old_raid_disks);
10424 s = xmalloc(size);
10425 *space_tail = s;
10426 space_tail = s;
10427 *space_tail = NULL;
10428 break;
10429 }
10430 }
10431 if (update->space_list == NULL)
10432 break;
10433
10434 /* add space for disk in update
10435 */
10436 size = sizeof(struct dl);
10437 s = xmalloc(size);
10438 *space_tail = s;
10439 space_tail = s;
10440 *space_tail = NULL;
10441
10442 /* add spare device to update
10443 */
10444 for (id = super->devlist ; id; id = id->next)
10445 if (id->index == (unsigned)u->subdev) {
10446 struct imsm_dev *dev;
10447 struct imsm_map *map;
10448
10449 dev = get_imsm_dev(super, u->subdev);
10450 map = get_imsm_map(dev, MAP_0);
10451 current_level = map->raid_level;
10452 break;
10453 }
10454 if (u->new_level == 5 && u->new_level != current_level) {
10455 struct mdinfo *spares;
10456
10457 spares = get_spares_for_grow(st);
10458 if (spares) {
10459 struct dl *dl;
10460 struct mdinfo *dev;
10461
10462 dev = spares->devs;
10463 if (dev) {
10464 u->new_disks[0] =
10465 makedev(dev->disk.major,
10466 dev->disk.minor);
10467 dl = get_disk_super(super,
10468 dev->disk.major,
10469 dev->disk.minor);
10470 dl->index = u->old_raid_disks;
10471 dev = dev->next;
10472 }
10473 sysfs_free(spares);
10474 }
10475 }
10476 len = disks_to_mpb_size(u->new_raid_disks);
10477 dprintf("New anchor length is %llu\n", (unsigned long long)len);
10478 break;
10479 }
10480 case update_size_change: {
10481 if (update->len < (int)sizeof(struct imsm_update_size_change))
10482 return 0;
10483 break;
10484 }
10485 case update_activate_spare: {
10486 if (update->len < (int)sizeof(struct imsm_update_activate_spare))
10487 return 0;
10488 break;
10489 }
10490 case update_create_array: {
10491 struct imsm_update_create_array *u = (void *) update->buf;
10492 struct intel_dev *dv;
10493 struct imsm_dev *dev = &u->dev;
10494 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10495 struct dl *dl;
10496 struct disk_info *inf;
10497 int i;
10498 int activate = 0;
10499
10500 if (update->len < (int)sizeof(*u))
10501 return 0;
10502
10503 inf = get_disk_info(u);
10504 len = sizeof_imsm_dev(dev, 1);
10505 /* allocate a new super->devlist entry */
10506 dv = xmalloc(sizeof(*dv));
10507 dv->dev = xmalloc(len);
10508 update->space = dv;
10509
10510 /* count how many spares will be converted to members */
10511 for (i = 0; i < map->num_members; i++) {
10512 dl = serial_to_dl(inf[i].serial, super);
10513 if (!dl) {
10514 /* hmm maybe it failed?, nothing we can do about
10515 * it here
10516 */
10517 continue;
10518 }
10519 if (count_memberships(dl, super) == 0)
10520 activate++;
10521 }
10522 len += activate * sizeof(struct imsm_disk);
10523 break;
10524 }
10525 case update_kill_array: {
10526 if (update->len < (int)sizeof(struct imsm_update_kill_array))
10527 return 0;
10528 break;
10529 }
10530 case update_rename_array: {
10531 if (update->len < (int)sizeof(struct imsm_update_rename_array))
10532 return 0;
10533 break;
10534 }
10535 case update_add_remove_disk:
10536 /* no update->len needed */
10537 break;
10538 case update_prealloc_badblocks_mem:
10539 super->extra_space += sizeof(struct bbm_log) -
10540 get_imsm_bbm_log_size(super->bbm_log);
10541 break;
10542 case update_rwh_policy: {
10543 if (update->len < (int)sizeof(struct imsm_update_rwh_policy))
10544 return 0;
10545 break;
10546 }
10547 default:
10548 return 0;
10549 }
10550
10551 /* check if we need a larger metadata buffer */
10552 if (super->next_buf)
10553 buf_len = super->next_len;
10554 else
10555 buf_len = super->len;
10556
10557 if (__le32_to_cpu(mpb->mpb_size) + super->extra_space + len > buf_len) {
10558 /* ok we need a larger buf than what is currently allocated
10559 * if this allocation fails process_update will notice that
10560 * ->next_len is set and ->next_buf is NULL
10561 */
10562 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) +
10563 super->extra_space + len, sector_size);
10564 if (super->next_buf)
10565 free(super->next_buf);
10566
10567 super->next_len = buf_len;
10568 if (posix_memalign(&super->next_buf, sector_size, buf_len) == 0)
10569 memset(super->next_buf, 0, buf_len);
10570 else
10571 super->next_buf = NULL;
10572 }
10573 return 1;
10574 }
10575
10576 /* must be called while manager is quiesced */
10577 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
10578 {
10579 struct imsm_super *mpb = super->anchor;
10580 struct dl *iter;
10581 struct imsm_dev *dev;
10582 struct imsm_map *map;
10583 unsigned int i, j, num_members;
10584 __u32 ord, ord_map0;
10585 struct bbm_log *log = super->bbm_log;
10586
10587 dprintf("deleting device[%d] from imsm_super\n", index);
10588
10589 /* shift all indexes down one */
10590 for (iter = super->disks; iter; iter = iter->next)
10591 if (iter->index > (int)index)
10592 iter->index--;
10593 for (iter = super->missing; iter; iter = iter->next)
10594 if (iter->index > (int)index)
10595 iter->index--;
10596
10597 for (i = 0; i < mpb->num_raid_devs; i++) {
10598 dev = get_imsm_dev(super, i);
10599 map = get_imsm_map(dev, MAP_0);
10600 num_members = map->num_members;
10601 for (j = 0; j < num_members; j++) {
10602 /* update ord entries being careful not to propagate
10603 * ord-flags to the first map
10604 */
10605 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
10606 ord_map0 = get_imsm_ord_tbl_ent(dev, j, MAP_0);
10607
10608 if (ord_to_idx(ord) <= index)
10609 continue;
10610
10611 map = get_imsm_map(dev, MAP_0);
10612 set_imsm_ord_tbl_ent(map, j, ord_map0 - 1);
10613 map = get_imsm_map(dev, MAP_1);
10614 if (map)
10615 set_imsm_ord_tbl_ent(map, j, ord - 1);
10616 }
10617 }
10618
10619 for (i = 0; i < log->entry_count; i++) {
10620 struct bbm_log_entry *entry = &log->marked_block_entries[i];
10621
10622 if (entry->disk_ordinal <= index)
10623 continue;
10624 entry->disk_ordinal--;
10625 }
10626
10627 mpb->num_disks--;
10628 super->updates_pending++;
10629 if (*dlp) {
10630 struct dl *dl = *dlp;
10631
10632 *dlp = (*dlp)->next;
10633 __free_imsm_disk(dl, 1);
10634 }
10635 }
10636
10637 static int imsm_get_allowed_degradation(int level, int raid_disks,
10638 struct intel_super *super,
10639 struct imsm_dev *dev)
10640 {
10641 switch (level) {
10642 case 1:
10643 case 10:{
10644 int ret_val = 0;
10645 struct imsm_map *map;
10646 int i;
10647
10648 ret_val = raid_disks/2;
10649 /* check map if all disks pairs not failed
10650 * in both maps
10651 */
10652 map = get_imsm_map(dev, MAP_0);
10653 for (i = 0; i < ret_val; i++) {
10654 int degradation = 0;
10655 if (get_imsm_disk(super, i) == NULL)
10656 degradation++;
10657 if (get_imsm_disk(super, i + 1) == NULL)
10658 degradation++;
10659 if (degradation == 2)
10660 return 0;
10661 }
10662 map = get_imsm_map(dev, MAP_1);
10663 /* if there is no second map
10664 * result can be returned
10665 */
10666 if (map == NULL)
10667 return ret_val;
10668 /* check degradation in second map
10669 */
10670 for (i = 0; i < ret_val; i++) {
10671 int degradation = 0;
10672 if (get_imsm_disk(super, i) == NULL)
10673 degradation++;
10674 if (get_imsm_disk(super, i + 1) == NULL)
10675 degradation++;
10676 if (degradation == 2)
10677 return 0;
10678 }
10679 return ret_val;
10680 }
10681 case 5:
10682 return 1;
10683 case 6:
10684 return 2;
10685 default:
10686 return 0;
10687 }
10688 }
10689
10690 /*******************************************************************************
10691 * Function: validate_container_imsm
10692 * Description: This routine validates container after assemble,
10693 * eg. if devices in container are under the same controller.
10694 *
10695 * Parameters:
10696 * info : linked list with info about devices used in array
10697 * Returns:
10698 * 1 : HBA mismatch
10699 * 0 : Success
10700 ******************************************************************************/
10701 int validate_container_imsm(struct mdinfo *info)
10702 {
10703 if (check_no_platform())
10704 return 0;
10705
10706 struct sys_dev *idev;
10707 struct sys_dev *hba = NULL;
10708 struct sys_dev *intel_devices = find_intel_devices();
10709 char *dev_path = devt_to_devpath(makedev(info->disk.major,
10710 info->disk.minor), 1, NULL);
10711
10712 for (idev = intel_devices; idev; idev = idev->next) {
10713 if (dev_path && strstr(dev_path, idev->path)) {
10714 hba = idev;
10715 break;
10716 }
10717 }
10718 if (dev_path)
10719 free(dev_path);
10720
10721 if (!hba) {
10722 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10723 devid2kname(makedev(info->disk.major, info->disk.minor)));
10724 return 1;
10725 }
10726
10727 const struct imsm_orom *orom = get_orom_by_device_id(hba->dev_id);
10728 struct mdinfo *dev;
10729
10730 for (dev = info->next; dev; dev = dev->next) {
10731 dev_path = devt_to_devpath(makedev(dev->disk.major,
10732 dev->disk.minor), 1, NULL);
10733
10734 struct sys_dev *hba2 = NULL;
10735 for (idev = intel_devices; idev; idev = idev->next) {
10736 if (dev_path && strstr(dev_path, idev->path)) {
10737 hba2 = idev;
10738 break;
10739 }
10740 }
10741 if (dev_path)
10742 free(dev_path);
10743
10744 const struct imsm_orom *orom2 = hba2 == NULL ? NULL :
10745 get_orom_by_device_id(hba2->dev_id);
10746
10747 if (hba2 && hba->type != hba2->type) {
10748 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10749 get_sys_dev_type(hba->type), get_sys_dev_type(hba2->type));
10750 return 1;
10751 }
10752
10753 if (orom != orom2) {
10754 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10755 " This operation is not supported and can lead to data loss.\n");
10756 return 1;
10757 }
10758
10759 if (!orom) {
10760 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10761 " This operation is not supported and can lead to data loss.\n");
10762 return 1;
10763 }
10764 }
10765
10766 return 0;
10767 }
10768
10769 /*******************************************************************************
10770 * Function: imsm_record_badblock
10771 * Description: This routine stores new bad block record in BBM log
10772 *
10773 * Parameters:
10774 * a : array containing a bad block
10775 * slot : disk number containing a bad block
10776 * sector : bad block sector
10777 * length : bad block sectors range
10778 * Returns:
10779 * 1 : Success
10780 * 0 : Error
10781 ******************************************************************************/
10782 static int imsm_record_badblock(struct active_array *a, int slot,
10783 unsigned long long sector, int length)
10784 {
10785 struct intel_super *super = a->container->sb;
10786 int ord;
10787 int ret;
10788
10789 ord = imsm_disk_slot_to_ord(a, slot);
10790 if (ord < 0)
10791 return 0;
10792
10793 ret = record_new_badblock(super->bbm_log, ord_to_idx(ord), sector,
10794 length);
10795 if (ret)
10796 super->updates_pending++;
10797
10798 return ret;
10799 }
10800 /*******************************************************************************
10801 * Function: imsm_clear_badblock
10802 * Description: This routine clears bad block record from BBM log
10803 *
10804 * Parameters:
10805 * a : array containing a bad block
10806 * slot : disk number containing a bad block
10807 * sector : bad block sector
10808 * length : bad block sectors range
10809 * Returns:
10810 * 1 : Success
10811 * 0 : Error
10812 ******************************************************************************/
10813 static int imsm_clear_badblock(struct active_array *a, int slot,
10814 unsigned long long sector, int length)
10815 {
10816 struct intel_super *super = a->container->sb;
10817 int ord;
10818 int ret;
10819
10820 ord = imsm_disk_slot_to_ord(a, slot);
10821 if (ord < 0)
10822 return 0;
10823
10824 ret = clear_badblock(super->bbm_log, ord_to_idx(ord), sector, length);
10825 if (ret)
10826 super->updates_pending++;
10827
10828 return ret;
10829 }
10830 /*******************************************************************************
10831 * Function: imsm_get_badblocks
10832 * Description: This routine get list of bad blocks for an array
10833 *
10834 * Parameters:
10835 * a : array
10836 * slot : disk number
10837 * Returns:
10838 * bb : structure containing bad blocks
10839 * NULL : error
10840 ******************************************************************************/
10841 static struct md_bb *imsm_get_badblocks(struct active_array *a, int slot)
10842 {
10843 int inst = a->info.container_member;
10844 struct intel_super *super = a->container->sb;
10845 struct imsm_dev *dev = get_imsm_dev(super, inst);
10846 struct imsm_map *map = get_imsm_map(dev, MAP_0);
10847 int ord;
10848
10849 ord = imsm_disk_slot_to_ord(a, slot);
10850 if (ord < 0)
10851 return NULL;
10852
10853 get_volume_badblocks(super->bbm_log, ord_to_idx(ord), pba_of_lba0(map),
10854 per_dev_array_size(map), &super->bb);
10855
10856 return &super->bb;
10857 }
10858 /*******************************************************************************
10859 * Function: examine_badblocks_imsm
10860 * Description: Prints list of bad blocks on a disk to the standard output
10861 *
10862 * Parameters:
10863 * st : metadata handler
10864 * fd : open file descriptor for device
10865 * devname : device name
10866 * Returns:
10867 * 0 : Success
10868 * 1 : Error
10869 ******************************************************************************/
10870 static int examine_badblocks_imsm(struct supertype *st, int fd, char *devname)
10871 {
10872 struct intel_super *super = st->sb;
10873 struct bbm_log *log = super->bbm_log;
10874 struct dl *d = NULL;
10875 int any = 0;
10876
10877 for (d = super->disks; d ; d = d->next) {
10878 if (strcmp(d->devname, devname) == 0)
10879 break;
10880 }
10881
10882 if ((d == NULL) || (d->index < 0)) { /* serial mismatch probably */
10883 pr_err("%s doesn't appear to be part of a raid array\n",
10884 devname);
10885 return 1;
10886 }
10887
10888 if (log != NULL) {
10889 unsigned int i;
10890 struct bbm_log_entry *entry = &log->marked_block_entries[0];
10891
10892 for (i = 0; i < log->entry_count; i++) {
10893 if (entry[i].disk_ordinal == d->index) {
10894 unsigned long long sector = __le48_to_cpu(
10895 &entry[i].defective_block_start);
10896 int cnt = entry[i].marked_count + 1;
10897
10898 if (!any) {
10899 printf("Bad-blocks on %s:\n", devname);
10900 any = 1;
10901 }
10902
10903 printf("%20llu for %d sectors\n", sector, cnt);
10904 }
10905 }
10906 }
10907
10908 if (!any)
10909 printf("No bad-blocks list configured on %s\n", devname);
10910
10911 return 0;
10912 }
10913 /*******************************************************************************
10914 * Function: init_migr_record_imsm
10915 * Description: Function inits imsm migration record
10916 * Parameters:
10917 * super : imsm internal array info
10918 * dev : device under migration
10919 * info : general array info to find the smallest device
10920 * Returns:
10921 * none
10922 ******************************************************************************/
10923 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
10924 struct mdinfo *info)
10925 {
10926 struct intel_super *super = st->sb;
10927 struct migr_record *migr_rec = super->migr_rec;
10928 int new_data_disks;
10929 unsigned long long dsize, dev_sectors;
10930 long long unsigned min_dev_sectors = -1LLU;
10931 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
10932 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
10933 unsigned long long num_migr_units;
10934 unsigned long long array_blocks;
10935 struct dl *dl_disk = NULL;
10936
10937 memset(migr_rec, 0, sizeof(struct migr_record));
10938 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
10939
10940 /* only ascending reshape supported now */
10941 migr_rec->ascending_migr = __cpu_to_le32(1);
10942
10943 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
10944 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10945 migr_rec->dest_depth_per_unit *=
10946 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
10947 new_data_disks = imsm_num_data_members(map_dest);
10948 migr_rec->blocks_per_unit =
10949 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
10950 migr_rec->dest_depth_per_unit =
10951 __cpu_to_le32(migr_rec->dest_depth_per_unit);
10952 array_blocks = info->component_size * new_data_disks;
10953 num_migr_units =
10954 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
10955
10956 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
10957 num_migr_units++;
10958 set_num_migr_units(migr_rec, num_migr_units);
10959
10960 migr_rec->post_migr_vol_cap = dev->size_low;
10961 migr_rec->post_migr_vol_cap_hi = dev->size_high;
10962
10963 /* Find the smallest dev */
10964 for (dl_disk = super->disks; dl_disk ; dl_disk = dl_disk->next) {
10965 /* ignore spares in container */
10966 if (dl_disk->index < 0)
10967 continue;
10968 get_dev_size(dl_disk->fd, NULL, &dsize);
10969 dev_sectors = dsize / 512;
10970 if (dev_sectors < min_dev_sectors)
10971 min_dev_sectors = dev_sectors;
10972 }
10973 set_migr_chkp_area_pba(migr_rec, min_dev_sectors -
10974 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
10975
10976 write_imsm_migr_rec(st);
10977
10978 return;
10979 }
10980
10981 /*******************************************************************************
10982 * Function: save_backup_imsm
10983 * Description: Function saves critical data stripes to Migration Copy Area
10984 * and updates the current migration unit status.
10985 * Use restore_stripes() to form a destination stripe,
10986 * and to write it to the Copy Area.
10987 * Parameters:
10988 * st : supertype information
10989 * dev : imsm device that backup is saved for
10990 * info : general array info
10991 * buf : input buffer
10992 * length : length of data to backup (blocks_per_unit)
10993 * Returns:
10994 * 0 : success
10995 *, -1 : fail
10996 ******************************************************************************/
10997 int save_backup_imsm(struct supertype *st,
10998 struct imsm_dev *dev,
10999 struct mdinfo *info,
11000 void *buf,
11001 int length)
11002 {
11003 int rv = -1;
11004 struct intel_super *super = st->sb;
11005 int i;
11006 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
11007 int new_disks = map_dest->num_members;
11008 int dest_layout = 0;
11009 int dest_chunk, targets[new_disks];
11010 unsigned long long start, target_offsets[new_disks];
11011 int data_disks = imsm_num_data_members(map_dest);
11012
11013 for (i = 0; i < new_disks; i++) {
11014 struct dl *dl_disk = get_imsm_dl_disk(super, i);
11015 if (dl_disk && is_fd_valid(dl_disk->fd))
11016 targets[i] = dl_disk->fd;
11017 else
11018 goto abort;
11019 }
11020
11021 start = info->reshape_progress * 512;
11022 for (i = 0; i < new_disks; i++) {
11023 target_offsets[i] = migr_chkp_area_pba(super->migr_rec) * 512;
11024 /* move back copy area adderss, it will be moved forward
11025 * in restore_stripes() using start input variable
11026 */
11027 target_offsets[i] -= start/data_disks;
11028 }
11029
11030 dest_layout = imsm_level_to_layout(map_dest->raid_level);
11031 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
11032
11033 if (restore_stripes(targets, /* list of dest devices */
11034 target_offsets, /* migration record offsets */
11035 new_disks,
11036 dest_chunk,
11037 map_dest->raid_level,
11038 dest_layout,
11039 -1, /* source backup file descriptor */
11040 0, /* input buf offset
11041 * always 0 buf is already offseted */
11042 start,
11043 length,
11044 buf) != 0) {
11045 pr_err("Error restoring stripes\n");
11046 goto abort;
11047 }
11048
11049 rv = 0;
11050
11051 abort:
11052 return rv;
11053 }
11054
11055 /*******************************************************************************
11056 * Function: save_checkpoint_imsm
11057 * Description: Function called for current unit status update
11058 * in the migration record. It writes it to disk.
11059 * Parameters:
11060 * super : imsm internal array info
11061 * info : general array info
11062 * Returns:
11063 * 0: success
11064 * 1: failure
11065 * 2: failure, means no valid migration record
11066 * / no general migration in progress /
11067 ******************************************************************************/
11068 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
11069 {
11070 struct intel_super *super = st->sb;
11071 unsigned long long blocks_per_unit;
11072 unsigned long long curr_migr_unit;
11073
11074 if (load_imsm_migr_rec(super) != 0) {
11075 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
11076 return 1;
11077 }
11078
11079 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
11080 if (blocks_per_unit == 0) {
11081 dprintf("imsm: no migration in progress.\n");
11082 return 2;
11083 }
11084 curr_migr_unit = info->reshape_progress / blocks_per_unit;
11085 /* check if array is alligned to copy area
11086 * if it is not alligned, add one to current migration unit value
11087 * this can happend on array reshape finish only
11088 */
11089 if (info->reshape_progress % blocks_per_unit)
11090 curr_migr_unit++;
11091
11092 set_current_migr_unit(super->migr_rec, curr_migr_unit);
11093 super->migr_rec->rec_status = __cpu_to_le32(state);
11094 set_migr_dest_1st_member_lba(super->migr_rec,
11095 super->migr_rec->dest_depth_per_unit * curr_migr_unit);
11096
11097 if (write_imsm_migr_rec(st) < 0) {
11098 dprintf("imsm: Cannot write migration record outside backup area\n");
11099 return 1;
11100 }
11101
11102 return 0;
11103 }
11104
11105 /*******************************************************************************
11106 * Function: recover_backup_imsm
11107 * Description: Function recovers critical data from the Migration Copy Area
11108 * while assembling an array.
11109 * Parameters:
11110 * super : imsm internal array info
11111 * info : general array info
11112 * Returns:
11113 * 0 : success (or there is no data to recover)
11114 * 1 : fail
11115 ******************************************************************************/
11116 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
11117 {
11118 struct intel_super *super = st->sb;
11119 struct migr_record *migr_rec = super->migr_rec;
11120 struct imsm_map *map_dest;
11121 struct intel_dev *id = NULL;
11122 unsigned long long read_offset;
11123 unsigned long long write_offset;
11124 unsigned unit_len;
11125 int new_disks, err;
11126 char *buf = NULL;
11127 int retval = 1;
11128 unsigned int sector_size = super->sector_size;
11129 unsigned long long curr_migr_unit = current_migr_unit(migr_rec);
11130 unsigned long long num_migr_units = get_num_migr_units(migr_rec);
11131 char buffer[SYSFS_MAX_BUF_SIZE];
11132 int skipped_disks = 0;
11133 struct dl *dl_disk;
11134
11135 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, sizeof(buffer));
11136 if (err < 1)
11137 return 1;
11138
11139 /* recover data only during assemblation */
11140 if (strncmp(buffer, "inactive", 8) != 0)
11141 return 0;
11142 /* no data to recover */
11143 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
11144 return 0;
11145 if (curr_migr_unit >= num_migr_units)
11146 return 1;
11147
11148 /* find device during reshape */
11149 for (id = super->devlist; id; id = id->next)
11150 if (is_gen_migration(id->dev))
11151 break;
11152 if (id == NULL)
11153 return 1;
11154
11155 map_dest = get_imsm_map(id->dev, MAP_0);
11156 new_disks = map_dest->num_members;
11157
11158 read_offset = migr_chkp_area_pba(migr_rec) * 512;
11159
11160 write_offset = (migr_dest_1st_member_lba(migr_rec) +
11161 pba_of_lba0(map_dest)) * 512;
11162
11163 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
11164 if (posix_memalign((void **)&buf, sector_size, unit_len) != 0)
11165 goto abort;
11166
11167 for (dl_disk = super->disks; dl_disk; dl_disk = dl_disk->next) {
11168 if (dl_disk->index < 0)
11169 continue;
11170
11171 if (!is_fd_valid(dl_disk->fd)) {
11172 skipped_disks++;
11173 continue;
11174 }
11175 if (lseek64(dl_disk->fd, read_offset, SEEK_SET) < 0) {
11176 pr_err("Cannot seek to block: %s\n",
11177 strerror(errno));
11178 skipped_disks++;
11179 continue;
11180 }
11181 if (read(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11182 pr_err("Cannot read copy area block: %s\n",
11183 strerror(errno));
11184 skipped_disks++;
11185 continue;
11186 }
11187 if (lseek64(dl_disk->fd, write_offset, SEEK_SET) < 0) {
11188 pr_err("Cannot seek to block: %s\n",
11189 strerror(errno));
11190 skipped_disks++;
11191 continue;
11192 }
11193 if (write(dl_disk->fd, buf, unit_len) != (ssize_t)unit_len) {
11194 pr_err("Cannot restore block: %s\n",
11195 strerror(errno));
11196 skipped_disks++;
11197 continue;
11198 }
11199 }
11200
11201 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
11202 new_disks,
11203 super,
11204 id->dev)) {
11205 pr_err("Cannot restore data from backup. Too many failed disks\n");
11206 goto abort;
11207 }
11208
11209 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
11210 /* ignore error == 2, this can mean end of reshape here
11211 */
11212 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
11213 } else
11214 retval = 0;
11215
11216 abort:
11217 free(buf);
11218 return retval;
11219 }
11220
11221 /**
11222 * test_and_add_drive_controller_policy_imsm() - add disk controller to policies list.
11223 * @type: Policy type to search on list.
11224 * @pols: List of currently recorded policies.
11225 * @disk_fd: File descriptor of the device to check.
11226 * @hba: The hba disk is attached, could be NULL if verification is disabled.
11227 * @verbose: verbose flag.
11228 *
11229 * IMSM cares about drive physical placement. If @hba is not set, it adds unknown policy.
11230 * If there is no controller policy on pols we are free to add first one. If there is a policy then,
11231 * new must be the same - no controller mixing allowed.
11232 */
11233 static mdadm_status_t
11234 test_and_add_drive_controller_policy_imsm(const char * const type, dev_policy_t **pols, int disk_fd,
11235 struct sys_dev *hba, const int verbose)
11236 {
11237 const char *controller_policy = get_sys_dev_type(SYS_DEV_UNKNOWN);
11238 struct dev_policy *pol = pol_find(*pols, (char *)type);
11239 char devname[MAX_RAID_SERIAL_LEN];
11240
11241 if (hba)
11242 controller_policy = get_sys_dev_type(hba->type);
11243
11244 if (!pol) {
11245 pol_add(pols, (char *)type, (char *)controller_policy, "imsm");
11246 return MDADM_STATUS_SUCCESS;
11247 }
11248
11249 if (strcmp(pol->value, controller_policy) == 0)
11250 return MDADM_STATUS_SUCCESS;
11251
11252 fd2devname(disk_fd, devname);
11253 pr_vrb("Intel(R) raid controller \"%s\" found for %s, but \"%s\" was detected earlier\n",
11254 controller_policy, devname, pol->value);
11255 pr_vrb("Disks under different controllers cannot be used, aborting\n");
11256
11257 return MDADM_STATUS_ERROR;
11258 }
11259
11260 struct imsm_drive_policy {
11261 char *type;
11262 mdadm_status_t (*test_and_add_drive_policy)(const char * const type,
11263 struct dev_policy **pols, int disk_fd,
11264 struct sys_dev *hba, const int verbose);
11265 };
11266
11267 struct imsm_drive_policy imsm_policies[] = {
11268 {"controller", test_and_add_drive_controller_policy_imsm},
11269 };
11270
11271 mdadm_status_t test_and_add_drive_policies_imsm(struct dev_policy **pols, int disk_fd,
11272 const int verbose)
11273 {
11274 struct imsm_drive_policy *imsm_pol;
11275 struct sys_dev *hba = NULL;
11276 char path[PATH_MAX];
11277 mdadm_status_t ret;
11278 unsigned int i;
11279
11280 /* If imsm platform verification is disabled, do not search for hba. */
11281 if (check_no_platform() != 1) {
11282 if (!diskfd_to_devpath(disk_fd, 1, path)) {
11283 pr_vrb("IMSM: Failed to retrieve device path by file descriptor.\n");
11284 return MDADM_STATUS_ERROR;
11285 }
11286
11287 hba = find_disk_attached_hba(disk_fd, path);
11288 if (!hba) {
11289 pr_vrb("IMSM: Failed to find hba for %s\n", path);
11290 return MDADM_STATUS_ERROR;
11291 }
11292 }
11293
11294 for (i = 0; i < ARRAY_SIZE(imsm_policies); i++) {
11295 imsm_pol = &imsm_policies[i];
11296
11297 ret = imsm_pol->test_and_add_drive_policy(imsm_pol->type, pols, disk_fd, hba,
11298 verbose);
11299 if (ret != MDADM_STATUS_SUCCESS)
11300 /* Inherit error code */
11301 return ret;
11302 }
11303
11304 return MDADM_STATUS_SUCCESS;
11305 }
11306
11307 /**
11308 * get_spare_criteria_imsm() - set spare criteria.
11309 * @st: supertype.
11310 * @mddev_path: path to md device devnode, it must be container.
11311 * @c: spare_criteria struct to fill, not NULL.
11312 *
11313 * If superblock is not loaded, use mddev_path to load_container. It must be given in this case.
11314 * Filles size and sector size accordingly to superblock.
11315 */
11316 mdadm_status_t get_spare_criteria_imsm(struct supertype *st, char *mddev_path,
11317 struct spare_criteria *c)
11318 {
11319 mdadm_status_t ret = MDADM_STATUS_ERROR;
11320 bool free_superblock = false;
11321 unsigned long long size = 0;
11322 struct intel_super *super;
11323 struct extent *e;
11324 struct dl *dl;
11325 int i;
11326
11327 /* If no superblock and no mddev_path, we cannot load superblock. */
11328 assert(st->sb || mddev_path);
11329
11330 if (mddev_path) {
11331 int fd = open(mddev_path, O_RDONLY);
11332 mdadm_status_t rv;
11333
11334 if (!is_fd_valid(fd))
11335 return MDADM_STATUS_ERROR;
11336
11337 if (!st->sb) {
11338 if (load_container_imsm(st, fd, st->devnm)) {
11339 close(fd);
11340 return MDADM_STATUS_ERROR;
11341 }
11342 free_superblock = true;
11343 }
11344
11345 rv = mddev_test_and_add_drive_policies(st, &c->pols, fd, 0);
11346 close(fd);
11347
11348 if (rv != MDADM_STATUS_SUCCESS)
11349 goto out;
11350 }
11351
11352 super = st->sb;
11353
11354 /* find first active disk in array */
11355 dl = super->disks;
11356 while (dl && (is_failed(&dl->disk) || dl->index == -1))
11357 dl = dl->next;
11358
11359 if (!dl)
11360 goto out;
11361
11362 /* find last lba used by subarrays */
11363 e = get_extents(super, dl, 0);
11364 if (!e)
11365 goto out;
11366
11367 for (i = 0; e[i].size; i++)
11368 continue;
11369 if (i > 0)
11370 size = e[i - 1].start + e[i - 1].size;
11371 free(e);
11372
11373 /* add the amount of space needed for metadata */
11374 size += imsm_min_reserved_sectors(super);
11375
11376 c->min_size = size * 512;
11377 c->sector_size = super->sector_size;
11378 c->criteria_set = true;
11379 ret = MDADM_STATUS_SUCCESS;
11380
11381 out:
11382 if (free_superblock)
11383 free_super_imsm(st);
11384
11385 if (ret != MDADM_STATUS_SUCCESS)
11386 c->criteria_set = false;
11387
11388 return ret;
11389 }
11390
11391 static char *imsm_find_array_devnm_by_subdev(int subdev, char *container)
11392 {
11393 static char devnm[32];
11394 char subdev_name[20];
11395 struct mdstat_ent *mdstat;
11396
11397 sprintf(subdev_name, "%d", subdev);
11398 mdstat = mdstat_by_subdev(subdev_name, container);
11399 if (!mdstat)
11400 return NULL;
11401
11402 strcpy(devnm, mdstat->devnm);
11403 free_mdstat(mdstat);
11404 return devnm;
11405 }
11406
11407 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
11408 struct geo_params *geo,
11409 int *old_raid_disks,
11410 int direction)
11411 {
11412 /* currently we only support increasing the number of devices
11413 * for a container. This increases the number of device for each
11414 * member array. They must all be RAID0 or RAID5.
11415 */
11416 int ret_val = 0;
11417 struct mdinfo *info, *member;
11418 int devices_that_can_grow = 0;
11419
11420 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st->devnm);
11421
11422 if (geo->size > 0 ||
11423 geo->level != UnSet ||
11424 geo->layout != UnSet ||
11425 geo->chunksize != 0 ||
11426 geo->raid_disks == UnSet) {
11427 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
11428 return ret_val;
11429 }
11430
11431 if (direction == ROLLBACK_METADATA_CHANGES) {
11432 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
11433 return ret_val;
11434 }
11435
11436 info = container_content_imsm(st, NULL);
11437 for (member = info; member; member = member->next) {
11438 char *result;
11439
11440 dprintf("imsm: checking device_num: %i\n",
11441 member->container_member);
11442
11443 if (geo->raid_disks <= member->array.raid_disks) {
11444 /* we work on container for Online Capacity Expansion
11445 * only so raid_disks has to grow
11446 */
11447 dprintf("imsm: for container operation raid disks increase is required\n");
11448 break;
11449 }
11450
11451 if (info->array.level != 0 && info->array.level != 5) {
11452 /* we cannot use this container with other raid level
11453 */
11454 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
11455 info->array.level);
11456 break;
11457 } else {
11458 /* check for platform support
11459 * for this raid level configuration
11460 */
11461 struct intel_super *super = st->sb;
11462 if (!is_raid_level_supported(super->orom,
11463 member->array.level,
11464 geo->raid_disks)) {
11465 dprintf("platform does not support raid%d with %d disk%s\n",
11466 info->array.level,
11467 geo->raid_disks,
11468 geo->raid_disks > 1 ? "s" : "");
11469 break;
11470 }
11471 /* check if component size is aligned to chunk size
11472 */
11473 if (info->component_size %
11474 (info->array.chunk_size/512)) {
11475 dprintf("Component size is not aligned to chunk size\n");
11476 break;
11477 }
11478 }
11479
11480 if (*old_raid_disks &&
11481 info->array.raid_disks != *old_raid_disks)
11482 break;
11483 *old_raid_disks = info->array.raid_disks;
11484
11485 /* All raid5 and raid0 volumes in container
11486 * have to be ready for Online Capacity Expansion
11487 * so they need to be assembled. We have already
11488 * checked that no recovery etc is happening.
11489 */
11490 result = imsm_find_array_devnm_by_subdev(member->container_member,
11491 st->container_devnm);
11492 if (result == NULL) {
11493 dprintf("imsm: cannot find array\n");
11494 break;
11495 }
11496 devices_that_can_grow++;
11497 }
11498 sysfs_free(info);
11499 if (!member && devices_that_can_grow)
11500 ret_val = 1;
11501
11502 if (ret_val)
11503 dprintf("Container operation allowed\n");
11504 else
11505 dprintf("Error: %i\n", ret_val);
11506
11507 return ret_val;
11508 }
11509
11510 /* Function: get_spares_for_grow
11511 * Description: Allocates memory and creates list of spare devices
11512 * avaliable in container. Checks if spare drive size is acceptable.
11513 * Parameters: Pointer to the supertype structure
11514 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
11515 * NULL if fail
11516 */
11517 static struct mdinfo *get_spares_for_grow(struct supertype *st)
11518 {
11519 struct spare_criteria sc = {0};
11520 struct mdinfo *spares;
11521
11522 get_spare_criteria_imsm(st, NULL, &sc);
11523 spares = container_choose_spares(st, &sc, NULL, NULL, NULL, 0);
11524
11525 dev_policy_free(sc.pols);
11526
11527 return spares;
11528 }
11529
11530 /******************************************************************************
11531 * function: imsm_create_metadata_update_for_reshape
11532 * Function creates update for whole IMSM container.
11533 *
11534 ******************************************************************************/
11535 static int imsm_create_metadata_update_for_reshape(
11536 struct supertype *st,
11537 struct geo_params *geo,
11538 int old_raid_disks,
11539 struct imsm_update_reshape **updatep)
11540 {
11541 struct intel_super *super = st->sb;
11542 struct imsm_super *mpb = super->anchor;
11543 int update_memory_size;
11544 struct imsm_update_reshape *u;
11545 struct mdinfo *spares;
11546 int i;
11547 int delta_disks;
11548 struct mdinfo *dev;
11549
11550 dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
11551
11552 delta_disks = geo->raid_disks - old_raid_disks;
11553
11554 /* size of all update data without anchor */
11555 update_memory_size = sizeof(struct imsm_update_reshape);
11556
11557 /* now add space for spare disks that we need to add. */
11558 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
11559
11560 u = xcalloc(1, update_memory_size);
11561 u->type = update_reshape_container_disks;
11562 u->old_raid_disks = old_raid_disks;
11563 u->new_raid_disks = geo->raid_disks;
11564
11565 /* now get spare disks list
11566 */
11567 spares = get_spares_for_grow(st);
11568
11569 if (spares == NULL || delta_disks > spares->array.spare_disks) {
11570 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo->dev_name);
11571 i = -1;
11572 goto abort;
11573 }
11574
11575 /* we have got spares
11576 * update disk list in imsm_disk list table in anchor
11577 */
11578 dprintf("imsm: %i spares are available.\n\n",
11579 spares->array.spare_disks);
11580
11581 dev = spares->devs;
11582 for (i = 0; i < delta_disks; i++) {
11583 struct dl *dl;
11584
11585 if (dev == NULL)
11586 break;
11587 u->new_disks[i] = makedev(dev->disk.major,
11588 dev->disk.minor);
11589 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
11590 dl->index = mpb->num_disks;
11591 mpb->num_disks++;
11592 dev = dev->next;
11593 }
11594
11595 abort:
11596 /* free spares
11597 */
11598 sysfs_free(spares);
11599
11600 dprintf("imsm: reshape update preparation :");
11601 if (i == delta_disks) {
11602 dprintf_cont(" OK\n");
11603 *updatep = u;
11604 return update_memory_size;
11605 }
11606 free(u);
11607 dprintf_cont(" Error\n");
11608
11609 return 0;
11610 }
11611
11612 /******************************************************************************
11613 * function: imsm_create_metadata_update_for_size_change()
11614 * Creates update for IMSM array for array size change.
11615 *
11616 ******************************************************************************/
11617 static int imsm_create_metadata_update_for_size_change(
11618 struct supertype *st,
11619 struct geo_params *geo,
11620 struct imsm_update_size_change **updatep)
11621 {
11622 struct intel_super *super = st->sb;
11623 int update_memory_size;
11624 struct imsm_update_size_change *u;
11625
11626 dprintf("(enter) New size = %llu\n", geo->size);
11627
11628 /* size of all update data without anchor */
11629 update_memory_size = sizeof(struct imsm_update_size_change);
11630
11631 u = xcalloc(1, update_memory_size);
11632 u->type = update_size_change;
11633 u->subdev = super->current_vol;
11634 u->new_size = geo->size;
11635
11636 dprintf("imsm: reshape update preparation : OK\n");
11637 *updatep = u;
11638
11639 return update_memory_size;
11640 }
11641
11642 /******************************************************************************
11643 * function: imsm_create_metadata_update_for_migration()
11644 * Creates update for IMSM array.
11645 *
11646 ******************************************************************************/
11647 static int imsm_create_metadata_update_for_migration(
11648 struct supertype *st,
11649 struct geo_params *geo,
11650 struct imsm_update_reshape_migration **updatep)
11651 {
11652 struct intel_super *super = st->sb;
11653 int update_memory_size;
11654 int current_chunk_size;
11655 struct imsm_update_reshape_migration *u;
11656 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11657 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11658 int previous_level = -1;
11659
11660 dprintf("(enter) New Level = %i\n", geo->level);
11661
11662 /* size of all update data without anchor */
11663 update_memory_size = sizeof(struct imsm_update_reshape_migration);
11664
11665 u = xcalloc(1, update_memory_size);
11666 u->type = update_reshape_migration;
11667 u->subdev = super->current_vol;
11668 u->new_level = geo->level;
11669 u->new_layout = geo->layout;
11670 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
11671 u->new_disks[0] = -1;
11672 u->new_chunksize = -1;
11673
11674 current_chunk_size = __le16_to_cpu(map->blocks_per_strip) / 2;
11675
11676 if (geo->chunksize != current_chunk_size) {
11677 u->new_chunksize = geo->chunksize / 1024;
11678 dprintf("imsm: chunk size change from %i to %i\n",
11679 current_chunk_size, u->new_chunksize);
11680 }
11681 previous_level = map->raid_level;
11682
11683 if (geo->level == 5 && previous_level == 0) {
11684 struct mdinfo *spares = NULL;
11685
11686 u->new_raid_disks++;
11687 spares = get_spares_for_grow(st);
11688 if (spares == NULL || spares->array.spare_disks < 1) {
11689 free(u);
11690 sysfs_free(spares);
11691 update_memory_size = 0;
11692 pr_err("cannot get spare device for requested migration\n");
11693 return 0;
11694 }
11695 sysfs_free(spares);
11696 }
11697 dprintf("imsm: reshape update preparation : OK\n");
11698 *updatep = u;
11699
11700 return update_memory_size;
11701 }
11702
11703 static void imsm_update_metadata_locally(struct supertype *st,
11704 void *buf, int len)
11705 {
11706 struct metadata_update mu;
11707
11708 mu.buf = buf;
11709 mu.len = len;
11710 mu.space = NULL;
11711 mu.space_list = NULL;
11712 mu.next = NULL;
11713 if (imsm_prepare_update(st, &mu))
11714 imsm_process_update(st, &mu);
11715
11716 while (mu.space_list) {
11717 void **space = mu.space_list;
11718 mu.space_list = *space;
11719 free(space);
11720 }
11721 }
11722
11723 /**
11724 * imsm_analyze_expand() - check expand properties and calculate new size.
11725 * @st: imsm supertype.
11726 * @geo: new geometry params.
11727 * @array: array info.
11728 * @direction: reshape direction.
11729 *
11730 * Obtain free space after the &array and verify if expand to requested size is
11731 * possible. If geo->size is set to %MAX_SIZE, assume that max free size is
11732 * requested.
11733 *
11734 * Return:
11735 * On success %IMSM_STATUS_OK is returned, geo->size and geo->raid_disks are
11736 * updated.
11737 * On error, %IMSM_STATUS_ERROR is returned.
11738 */
11739 static imsm_status_t imsm_analyze_expand(struct supertype *st,
11740 struct geo_params *geo,
11741 struct mdinfo *array,
11742 int direction)
11743 {
11744 struct intel_super *super = st->sb;
11745 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
11746 struct imsm_map *map = get_imsm_map(dev, MAP_0);
11747 int data_disks = imsm_num_data_members(map);
11748
11749 unsigned long long current_size;
11750 unsigned long long free_size;
11751 unsigned long long new_size;
11752 unsigned long long max_size;
11753
11754 const int chunk_kib = geo->chunksize / 1024;
11755 imsm_status_t rv;
11756
11757 if (direction == ROLLBACK_METADATA_CHANGES) {
11758 /**
11759 * Accept size for rollback only.
11760 */
11761 new_size = geo->size * 2;
11762 goto success;
11763 }
11764
11765 if (data_disks == 0) {
11766 pr_err("imsm: Cannot retrieve data disks.\n");
11767 return IMSM_STATUS_ERROR;
11768 }
11769 current_size = array->custom_array_size / data_disks;
11770
11771 rv = imsm_get_free_size(super, dev->vol.map->num_members, 0, chunk_kib, &free_size, true);
11772 if (rv != IMSM_STATUS_OK) {
11773 pr_err("imsm: Cannot find free space for expand.\n");
11774 return IMSM_STATUS_ERROR;
11775 }
11776 max_size = round_member_size_to_mb(free_size + current_size);
11777
11778 if (geo->size == MAX_SIZE)
11779 new_size = max_size;
11780 else
11781 new_size = round_member_size_to_mb(geo->size * 2);
11782
11783 if (new_size == 0) {
11784 pr_err("imsm: Rounded requested size is 0.\n");
11785 return IMSM_STATUS_ERROR;
11786 }
11787
11788 if (new_size > max_size) {
11789 pr_err("imsm: Rounded requested size (%llu) is larger than free space available (%llu).\n",
11790 new_size, max_size);
11791 return IMSM_STATUS_ERROR;
11792 }
11793
11794 if (new_size == current_size) {
11795 pr_err("imsm: Rounded requested size (%llu) is same as current size (%llu).\n",
11796 new_size, current_size);
11797 return IMSM_STATUS_ERROR;
11798 }
11799
11800 if (new_size < current_size) {
11801 pr_err("imsm: Size reduction is not supported, rounded requested size (%llu) is smaller than current (%llu).\n",
11802 new_size, current_size);
11803 return IMSM_STATUS_ERROR;
11804 }
11805
11806 success:
11807 dprintf("imsm: New size per member is %llu.\n", new_size);
11808 geo->size = data_disks * new_size;
11809 geo->raid_disks = dev->vol.map->num_members;
11810 return IMSM_STATUS_OK;
11811 }
11812
11813 /***************************************************************************
11814 * Function: imsm_analyze_change
11815 * Description: Function analyze change for single volume
11816 * and validate if transition is supported
11817 * Parameters: Geometry parameters, supertype structure,
11818 * metadata change direction (apply/rollback)
11819 * Returns: Operation type code on success, -1 if fail
11820 ****************************************************************************/
11821 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
11822 struct geo_params *geo,
11823 int direction)
11824 {
11825 struct mdinfo info;
11826 int change = -1;
11827 int check_devs = 0;
11828 int chunk;
11829 /* number of added/removed disks in operation result */
11830 int devNumChange = 0;
11831 /* imsm compatible layout value for array geometry verification */
11832 int imsm_layout = -1;
11833 imsm_status_t rv;
11834
11835 getinfo_super_imsm_volume(st, &info, NULL);
11836 if (geo->level != info.array.level && geo->level >= 0 &&
11837 geo->level != UnSet) {
11838 switch (info.array.level) {
11839 case 0:
11840 if (geo->level == 5) {
11841 change = CH_MIGRATION;
11842 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
11843 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11844 change = -1;
11845 goto analyse_change_exit;
11846 }
11847 imsm_layout = geo->layout;
11848 check_devs = 1;
11849 devNumChange = 1; /* parity disk added */
11850 } else if (geo->level == 10) {
11851 change = CH_TAKEOVER;
11852 check_devs = 1;
11853 devNumChange = 2; /* two mirrors added */
11854 imsm_layout = 0x102; /* imsm supported layout */
11855 }
11856 break;
11857 case 1:
11858 case 10:
11859 if (geo->level == 0) {
11860 change = CH_TAKEOVER;
11861 check_devs = 1;
11862 devNumChange = -(geo->raid_disks/2);
11863 imsm_layout = 0; /* imsm raid0 layout */
11864 }
11865 break;
11866 }
11867 if (change == -1) {
11868 pr_err("Error. Level Migration from %d to %d not supported!\n",
11869 info.array.level, geo->level);
11870 goto analyse_change_exit;
11871 }
11872 } else
11873 geo->level = info.array.level;
11874
11875 if (geo->layout != info.array.layout &&
11876 (geo->layout != UnSet && geo->layout != -1)) {
11877 change = CH_MIGRATION;
11878 if (info.array.layout == 0 && info.array.level == 5 &&
11879 geo->layout == 5) {
11880 /* reshape 5 -> 4 */
11881 } else if (info.array.layout == 5 && info.array.level == 5 &&
11882 geo->layout == 0) {
11883 /* reshape 4 -> 5 */
11884 geo->layout = 0;
11885 geo->level = 5;
11886 } else {
11887 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11888 info.array.layout, geo->layout);
11889 change = -1;
11890 goto analyse_change_exit;
11891 }
11892 } else {
11893 geo->layout = info.array.layout;
11894 if (imsm_layout == -1)
11895 imsm_layout = info.array.layout;
11896 }
11897
11898 if (geo->chunksize > 0 && geo->chunksize != UnSet &&
11899 geo->chunksize != info.array.chunk_size) {
11900 if (info.array.level == 10) {
11901 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11902 change = -1;
11903 goto analyse_change_exit;
11904 } else if (info.component_size % (geo->chunksize/512)) {
11905 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11906 geo->chunksize/1024, info.component_size/2);
11907 change = -1;
11908 goto analyse_change_exit;
11909 }
11910 change = CH_MIGRATION;
11911 } else {
11912 geo->chunksize = info.array.chunk_size;
11913 }
11914
11915 if (geo->size > 0) {
11916 if (change != -1) {
11917 pr_err("Error. Size change should be the only one at a time.\n");
11918 change = -1;
11919 goto analyse_change_exit;
11920 }
11921
11922 rv = imsm_analyze_expand(st, geo, &info, direction);
11923 if (rv != IMSM_STATUS_OK)
11924 goto analyse_change_exit;
11925 change = CH_ARRAY_SIZE;
11926 }
11927
11928 chunk = geo->chunksize / 1024;
11929 if (!validate_geometry_imsm(st,
11930 geo->level,
11931 imsm_layout,
11932 geo->raid_disks + devNumChange,
11933 &chunk,
11934 geo->size, INVALID_SECTORS,
11935 0, 0, info.consistency_policy, 1))
11936 change = -1;
11937
11938 if (check_devs) {
11939 struct intel_super *super = st->sb;
11940 struct imsm_super *mpb = super->anchor;
11941
11942 if (mpb->num_raid_devs > 1) {
11943 pr_err("Error. Cannot perform operation on %s- for this operation "
11944 "it MUST be single array in container\n", geo->dev_name);
11945 change = -1;
11946 }
11947 }
11948
11949 analyse_change_exit:
11950 if (direction == ROLLBACK_METADATA_CHANGES &&
11951 (change == CH_MIGRATION || change == CH_TAKEOVER)) {
11952 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11953 change = -1;
11954 }
11955 return change;
11956 }
11957
11958 int imsm_takeover(struct supertype *st, struct geo_params *geo)
11959 {
11960 struct intel_super *super = st->sb;
11961 struct imsm_update_takeover *u;
11962
11963 u = xmalloc(sizeof(struct imsm_update_takeover));
11964
11965 u->type = update_takeover;
11966 u->subarray = super->current_vol;
11967
11968 /* 10->0 transition */
11969 if (geo->level == 0)
11970 u->direction = R10_TO_R0;
11971
11972 /* 0->10 transition */
11973 if (geo->level == 10)
11974 u->direction = R0_TO_R10;
11975
11976 /* update metadata locally */
11977 imsm_update_metadata_locally(st, u,
11978 sizeof(struct imsm_update_takeover));
11979 /* and possibly remotely */
11980 if (st->update_tail)
11981 append_metadata_update(st, u,
11982 sizeof(struct imsm_update_takeover));
11983 else
11984 free(u);
11985
11986 return 0;
11987 }
11988
11989 /* Flush size update if size calculated by num_data_stripes is higher than
11990 * imsm_dev_size to eliminate differences during reshape.
11991 * Mdmon will recalculate them correctly.
11992 * If subarray index is not set then check whole container.
11993 * Returns:
11994 * 0 - no error occurred
11995 * 1 - error detected
11996 */
11997 static int imsm_fix_size_mismatch(struct supertype *st, int subarray_index)
11998 {
11999 struct intel_super *super = st->sb;
12000 int tmp = super->current_vol;
12001 int ret_val = 1;
12002 int i;
12003
12004 for (i = 0; i < super->anchor->num_raid_devs; i++) {
12005 if (subarray_index >= 0 && i != subarray_index)
12006 continue;
12007 super->current_vol = i;
12008 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12009 struct imsm_map *map = get_imsm_map(dev, MAP_0);
12010 unsigned int disc_count = imsm_num_data_members(map);
12011 struct geo_params geo;
12012 struct imsm_update_size_change *update;
12013 unsigned long long calc_size = per_dev_array_size(map) * disc_count;
12014 unsigned long long d_size = imsm_dev_size(dev);
12015 int u_size;
12016
12017 if (calc_size == d_size)
12018 continue;
12019
12020 /* There is a difference, confirm that imsm_dev_size is
12021 * smaller and push update.
12022 */
12023 if (d_size > calc_size) {
12024 pr_err("imsm: dev size of subarray %d is incorrect\n",
12025 i);
12026 goto exit;
12027 }
12028 memset(&geo, 0, sizeof(struct geo_params));
12029 geo.size = d_size;
12030 u_size = imsm_create_metadata_update_for_size_change(st, &geo,
12031 &update);
12032 imsm_update_metadata_locally(st, update, u_size);
12033 if (st->update_tail) {
12034 append_metadata_update(st, update, u_size);
12035 flush_metadata_updates(st);
12036 st->update_tail = &st->updates;
12037 } else {
12038 imsm_sync_metadata(st);
12039 free(update);
12040 }
12041 }
12042 ret_val = 0;
12043 exit:
12044 super->current_vol = tmp;
12045 return ret_val;
12046 }
12047
12048 static int imsm_reshape_super(struct supertype *st, unsigned long long size,
12049 int level,
12050 int layout, int chunksize, int raid_disks,
12051 int delta_disks, char *backup, char *dev,
12052 int direction, int verbose)
12053 {
12054 int ret_val = 1;
12055 struct geo_params geo;
12056
12057 dprintf("(enter)\n");
12058
12059 memset(&geo, 0, sizeof(struct geo_params));
12060
12061 geo.dev_name = dev;
12062 strcpy(geo.devnm, st->devnm);
12063 geo.size = size;
12064 geo.level = level;
12065 geo.layout = layout;
12066 geo.chunksize = chunksize;
12067 geo.raid_disks = raid_disks;
12068 if (delta_disks != UnSet)
12069 geo.raid_disks += delta_disks;
12070
12071 dprintf("for level : %i\n", geo.level);
12072 dprintf("for raid_disks : %i\n", geo.raid_disks);
12073
12074 if (strcmp(st->container_devnm, st->devnm) == 0) {
12075 /* On container level we can only increase number of devices. */
12076 dprintf("imsm: info: Container operation\n");
12077 int old_raid_disks = 0;
12078
12079 if (imsm_reshape_is_allowed_on_container(
12080 st, &geo, &old_raid_disks, direction)) {
12081 struct imsm_update_reshape *u = NULL;
12082 int len;
12083
12084 if (imsm_fix_size_mismatch(st, -1)) {
12085 dprintf("imsm: Cannot fix size mismatch\n");
12086 goto exit_imsm_reshape_super;
12087 }
12088
12089 len = imsm_create_metadata_update_for_reshape(
12090 st, &geo, old_raid_disks, &u);
12091
12092 if (len <= 0) {
12093 dprintf("imsm: Cannot prepare update\n");
12094 goto exit_imsm_reshape_super;
12095 }
12096
12097 ret_val = 0;
12098 /* update metadata locally */
12099 imsm_update_metadata_locally(st, u, len);
12100 /* and possibly remotely */
12101 if (st->update_tail)
12102 append_metadata_update(st, u, len);
12103 else
12104 free(u);
12105
12106 } else {
12107 pr_err("(imsm) Operation is not allowed on this container\n");
12108 }
12109 } else {
12110 /* On volume level we support following operations
12111 * - takeover: raid10 -> raid0; raid0 -> raid10
12112 * - chunk size migration
12113 * - migration: raid5 -> raid0; raid0 -> raid5
12114 */
12115 struct intel_super *super = st->sb;
12116 struct intel_dev *dev = super->devlist;
12117 int change;
12118 dprintf("imsm: info: Volume operation\n");
12119 /* find requested device */
12120 while (dev) {
12121 char *devnm =
12122 imsm_find_array_devnm_by_subdev(
12123 dev->index, st->container_devnm);
12124 if (devnm && strcmp(devnm, geo.devnm) == 0)
12125 break;
12126 dev = dev->next;
12127 }
12128 if (dev == NULL) {
12129 pr_err("Cannot find %s (%s) subarray\n",
12130 geo.dev_name, geo.devnm);
12131 goto exit_imsm_reshape_super;
12132 }
12133 super->current_vol = dev->index;
12134 change = imsm_analyze_change(st, &geo, direction);
12135 switch (change) {
12136 case CH_TAKEOVER:
12137 ret_val = imsm_takeover(st, &geo);
12138 break;
12139 case CH_MIGRATION: {
12140 struct imsm_update_reshape_migration *u = NULL;
12141 int len =
12142 imsm_create_metadata_update_for_migration(
12143 st, &geo, &u);
12144 if (len < 1) {
12145 dprintf("imsm: Cannot prepare update\n");
12146 break;
12147 }
12148 ret_val = 0;
12149 /* update metadata locally */
12150 imsm_update_metadata_locally(st, u, len);
12151 /* and possibly remotely */
12152 if (st->update_tail)
12153 append_metadata_update(st, u, len);
12154 else
12155 free(u);
12156 }
12157 break;
12158 case CH_ARRAY_SIZE: {
12159 struct imsm_update_size_change *u = NULL;
12160 int len =
12161 imsm_create_metadata_update_for_size_change(
12162 st, &geo, &u);
12163 if (len < 1) {
12164 dprintf("imsm: Cannot prepare update\n");
12165 break;
12166 }
12167 ret_val = 0;
12168 /* update metadata locally */
12169 imsm_update_metadata_locally(st, u, len);
12170 /* and possibly remotely */
12171 if (st->update_tail)
12172 append_metadata_update(st, u, len);
12173 else
12174 free(u);
12175 }
12176 break;
12177 default:
12178 ret_val = 1;
12179 }
12180 }
12181
12182 exit_imsm_reshape_super:
12183 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
12184 return ret_val;
12185 }
12186
12187 #define COMPLETED_OK 0
12188 #define COMPLETED_NONE 1
12189 #define COMPLETED_DELAYED 2
12190
12191 static int read_completed(int fd, unsigned long long *val)
12192 {
12193 int ret;
12194 char buf[SYSFS_MAX_BUF_SIZE];
12195
12196 ret = sysfs_fd_get_str(fd, buf, sizeof(buf));
12197 if (ret < 0)
12198 return ret;
12199
12200 ret = COMPLETED_OK;
12201 if (str_is_none(buf) == true) {
12202 ret = COMPLETED_NONE;
12203 } else if (strncmp(buf, "delayed", 7) == 0) {
12204 ret = COMPLETED_DELAYED;
12205 } else {
12206 char *ep;
12207 *val = strtoull(buf, &ep, 0);
12208 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
12209 ret = -1;
12210 }
12211 return ret;
12212 }
12213
12214 /*******************************************************************************
12215 * Function: wait_for_reshape_imsm
12216 * Description: Function writes new sync_max value and waits until
12217 * reshape process reach new position
12218 * Parameters:
12219 * sra : general array info
12220 * ndata : number of disks in new array's layout
12221 * Returns:
12222 * 0 : success,
12223 * 1 : there is no reshape in progress,
12224 * -1 : fail
12225 ******************************************************************************/
12226 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
12227 {
12228 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
12229 int retry = 3;
12230 unsigned long long completed;
12231 /* to_complete : new sync_max position */
12232 unsigned long long to_complete = sra->reshape_progress;
12233 unsigned long long position_to_set = to_complete / ndata;
12234
12235 if (!is_fd_valid(fd)) {
12236 dprintf("cannot open reshape_position\n");
12237 return 1;
12238 }
12239
12240 do {
12241 if (sysfs_fd_get_ll(fd, &completed) < 0) {
12242 if (!retry) {
12243 dprintf("cannot read reshape_position (no reshape in progres)\n");
12244 close(fd);
12245 return 1;
12246 }
12247 sleep_for(0, MSEC_TO_NSEC(30), true);
12248 } else
12249 break;
12250 } while (retry--);
12251
12252 if (completed > position_to_set) {
12253 dprintf("wrong next position to set %llu (%llu)\n",
12254 to_complete, position_to_set);
12255 close(fd);
12256 return -1;
12257 }
12258 dprintf("Position set: %llu\n", position_to_set);
12259 if (sysfs_set_num(sra, NULL, "sync_max",
12260 position_to_set) != 0) {
12261 dprintf("cannot set reshape position to %llu\n",
12262 position_to_set);
12263 close(fd);
12264 return -1;
12265 }
12266
12267 do {
12268 int rc;
12269 char action[SYSFS_MAX_BUF_SIZE];
12270 int timeout = 3000;
12271
12272 sysfs_wait(fd, &timeout);
12273 if (sysfs_get_str(sra, NULL, "sync_action",
12274 action, sizeof(action)) > 0 &&
12275 strncmp(action, "reshape", 7) != 0) {
12276 if (strncmp(action, "idle", 4) == 0)
12277 break;
12278 close(fd);
12279 return -1;
12280 }
12281
12282 rc = read_completed(fd, &completed);
12283 if (rc < 0) {
12284 dprintf("cannot read reshape_position (in loop)\n");
12285 close(fd);
12286 return 1;
12287 } else if (rc == COMPLETED_NONE)
12288 break;
12289 } while (completed < position_to_set);
12290
12291 close(fd);
12292 return 0;
12293 }
12294
12295 /*******************************************************************************
12296 * Function: check_degradation_change
12297 * Description: Check that array hasn't become failed.
12298 * Parameters:
12299 * info : for sysfs access
12300 * sources : source disks descriptors
12301 * degraded: previous degradation level
12302 * Returns:
12303 * degradation level
12304 ******************************************************************************/
12305 int check_degradation_change(struct mdinfo *info,
12306 int *sources,
12307 int degraded)
12308 {
12309 unsigned long long new_degraded;
12310 int rv;
12311
12312 rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
12313 if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
12314 /* check each device to ensure it is still working */
12315 struct mdinfo *sd;
12316 new_degraded = 0;
12317 for (sd = info->devs ; sd ; sd = sd->next) {
12318 if (sd->disk.state & (1<<MD_DISK_FAULTY))
12319 continue;
12320 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
12321 char sbuf[SYSFS_MAX_BUF_SIZE];
12322 int raid_disk = sd->disk.raid_disk;
12323
12324 if (sysfs_get_str(info,
12325 sd, "state", sbuf, sizeof(sbuf)) < 0 ||
12326 strstr(sbuf, "faulty") ||
12327 strstr(sbuf, "in_sync") == NULL) {
12328 /* this device is dead */
12329 sd->disk.state = (1<<MD_DISK_FAULTY);
12330 if (raid_disk >= 0)
12331 close_fd(&sources[raid_disk]);
12332 new_degraded++;
12333 }
12334 }
12335 }
12336 }
12337
12338 return new_degraded;
12339 }
12340
12341 /*******************************************************************************
12342 * Function: imsm_manage_reshape
12343 * Description: Function finds array under reshape and it manages reshape
12344 * process. It creates stripes backups (if required) and sets
12345 * checkpoints.
12346 * Parameters:
12347 * afd : Backup handle (nattive) - not used
12348 * sra : general array info
12349 * reshape : reshape parameters - not used
12350 * st : supertype structure
12351 * blocks : size of critical section [blocks]
12352 * fds : table of source device descriptor
12353 * offsets : start of array (offest per devices)
12354 * dests : not used
12355 * destfd : table of destination device descriptor
12356 * destoffsets : table of destination offsets (per device)
12357 * Returns:
12358 * 1 : success, reshape is done
12359 * 0 : fail
12360 ******************************************************************************/
12361 static int imsm_manage_reshape(
12362 int afd, struct mdinfo *sra, struct reshape *reshape,
12363 struct supertype *st, unsigned long backup_blocks,
12364 int *fds, unsigned long long *offsets,
12365 int dests, int *destfd, unsigned long long *destoffsets)
12366 {
12367 int ret_val = 0;
12368 struct intel_super *super = st->sb;
12369 struct intel_dev *dv;
12370 unsigned int sector_size = super->sector_size;
12371 struct imsm_dev *dev = NULL;
12372 struct imsm_map *map_src, *map_dest;
12373 int migr_vol_qan = 0;
12374 int ndata, odata; /* [bytes] */
12375 int chunk; /* [bytes] */
12376 struct migr_record *migr_rec;
12377 char *buf = NULL;
12378 unsigned int buf_size; /* [bytes] */
12379 unsigned long long max_position; /* array size [bytes] */
12380 unsigned long long next_step; /* [blocks]/[bytes] */
12381 unsigned long long old_data_stripe_length;
12382 unsigned long long start_src; /* [bytes] */
12383 unsigned long long start; /* [bytes] */
12384 unsigned long long start_buf_shift; /* [bytes] */
12385 int degraded = 0;
12386 int source_layout = 0;
12387 int subarray_index = -1;
12388
12389 if (!sra)
12390 return ret_val;
12391
12392 if (!fds || !offsets)
12393 goto abort;
12394
12395 /* Find volume during the reshape */
12396 for (dv = super->devlist; dv; dv = dv->next) {
12397 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR &&
12398 dv->dev->vol.migr_state == 1) {
12399 dev = dv->dev;
12400 migr_vol_qan++;
12401 subarray_index = dv->index;
12402 }
12403 }
12404 /* Only one volume can migrate at the same time */
12405 if (migr_vol_qan != 1) {
12406 pr_err("%s", migr_vol_qan ?
12407 "Number of migrating volumes greater than 1\n" :
12408 "There is no volume during migrationg\n");
12409 goto abort;
12410 }
12411
12412 map_dest = get_imsm_map(dev, MAP_0);
12413 map_src = get_imsm_map(dev, MAP_1);
12414 if (map_src == NULL)
12415 goto abort;
12416
12417 ndata = imsm_num_data_members(map_dest);
12418 odata = imsm_num_data_members(map_src);
12419
12420 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
12421 old_data_stripe_length = odata * chunk;
12422
12423 migr_rec = super->migr_rec;
12424
12425 /* initialize migration record for start condition */
12426 if (sra->reshape_progress == 0)
12427 init_migr_record_imsm(st, dev, sra);
12428 else {
12429 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
12430 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
12431 goto abort;
12432 }
12433 /* Save checkpoint to update migration record for current
12434 * reshape position (in md). It can be farther than current
12435 * reshape position in metadata.
12436 */
12437 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12438 /* ignore error == 2, this can mean end of reshape here
12439 */
12440 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
12441 goto abort;
12442 }
12443 }
12444
12445 /* size for data */
12446 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
12447 /* extend buffer size for parity disk */
12448 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
12449 /* add space for stripe alignment */
12450 buf_size += old_data_stripe_length;
12451 if (posix_memalign((void **)&buf, MAX_SECTOR_SIZE, buf_size)) {
12452 dprintf("imsm: Cannot allocate checkpoint buffer\n");
12453 goto abort;
12454 }
12455
12456 max_position = sra->component_size * ndata;
12457 source_layout = imsm_level_to_layout(map_src->raid_level);
12458
12459 while (current_migr_unit(migr_rec) <
12460 get_num_migr_units(migr_rec)) {
12461 /* current reshape position [blocks] */
12462 unsigned long long current_position =
12463 __le32_to_cpu(migr_rec->blocks_per_unit)
12464 * current_migr_unit(migr_rec);
12465 unsigned long long border;
12466
12467 /* Check that array hasn't become failed.
12468 */
12469 degraded = check_degradation_change(sra, fds, degraded);
12470 if (degraded > 1) {
12471 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded);
12472 goto abort;
12473 }
12474
12475 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
12476
12477 if ((current_position + next_step) > max_position)
12478 next_step = max_position - current_position;
12479
12480 start = current_position * 512;
12481
12482 /* align reading start to old geometry */
12483 start_buf_shift = start % old_data_stripe_length;
12484 start_src = start - start_buf_shift;
12485
12486 border = (start_src / odata) - (start / ndata);
12487 border /= 512;
12488 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
12489 /* save critical stripes to buf
12490 * start - start address of current unit
12491 * to backup [bytes]
12492 * start_src - start address of current unit
12493 * to backup alligned to source array
12494 * [bytes]
12495 */
12496 unsigned long long next_step_filler;
12497 unsigned long long copy_length = next_step * 512;
12498
12499 /* allign copy area length to stripe in old geometry */
12500 next_step_filler = ((copy_length + start_buf_shift)
12501 % old_data_stripe_length);
12502 if (next_step_filler)
12503 next_step_filler = (old_data_stripe_length
12504 - next_step_filler);
12505 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
12506 start, start_src, copy_length,
12507 start_buf_shift, next_step_filler);
12508
12509 if (save_stripes(fds, offsets, map_src->num_members,
12510 chunk, map_src->raid_level,
12511 source_layout, 0, NULL, start_src,
12512 copy_length +
12513 next_step_filler + start_buf_shift,
12514 buf)) {
12515 dprintf("imsm: Cannot save stripes to buffer\n");
12516 goto abort;
12517 }
12518 /* Convert data to destination format and store it
12519 * in backup general migration area
12520 */
12521 if (save_backup_imsm(st, dev, sra,
12522 buf + start_buf_shift, copy_length)) {
12523 dprintf("imsm: Cannot save stripes to target devices\n");
12524 goto abort;
12525 }
12526 if (save_checkpoint_imsm(st, sra,
12527 UNIT_SRC_IN_CP_AREA)) {
12528 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
12529 goto abort;
12530 }
12531 } else {
12532 /* set next step to use whole border area */
12533 border /= next_step;
12534 if (border > 1)
12535 next_step *= border;
12536 }
12537 /* When data backed up, checkpoint stored,
12538 * kick the kernel to reshape unit of data
12539 */
12540 next_step = next_step + sra->reshape_progress;
12541 /* limit next step to array max position */
12542 if (next_step > max_position)
12543 next_step = max_position;
12544 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
12545 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
12546 sra->reshape_progress = next_step;
12547
12548 /* wait until reshape finish */
12549 if (wait_for_reshape_imsm(sra, ndata)) {
12550 dprintf("wait_for_reshape_imsm returned error!\n");
12551 goto abort;
12552 }
12553 if (sigterm)
12554 goto abort;
12555
12556 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
12557 /* ignore error == 2, this can mean end of reshape here
12558 */
12559 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
12560 goto abort;
12561 }
12562
12563 }
12564
12565 /* clear migr_rec on disks after successful migration */
12566 struct dl *d;
12567
12568 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SECTORS*MAX_SECTOR_SIZE);
12569 for (d = super->disks; d; d = d->next) {
12570 if (d->index < 0 || is_failed(&d->disk))
12571 continue;
12572 unsigned long long dsize;
12573
12574 get_dev_size(d->fd, NULL, &dsize);
12575 if (lseek64(d->fd, dsize - MIGR_REC_SECTOR_POSITION*sector_size,
12576 SEEK_SET) >= 0) {
12577 if ((unsigned int)write(d->fd, super->migr_rec_buf,
12578 MIGR_REC_BUF_SECTORS*sector_size) !=
12579 MIGR_REC_BUF_SECTORS*sector_size)
12580 perror("Write migr_rec failed");
12581 }
12582 }
12583
12584 /* return '1' if done */
12585 ret_val = 1;
12586
12587 /* After the reshape eliminate size mismatch in metadata.
12588 * Don't update md/component_size here, volume hasn't
12589 * to take whole space. It is allowed by kernel.
12590 * md/component_size will be set propoperly after next assembly.
12591 */
12592 imsm_fix_size_mismatch(st, subarray_index);
12593
12594 abort:
12595 free(buf);
12596 /* See Grow.c: abort_reshape() for further explanation */
12597 sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
12598 sysfs_set_num(sra, NULL, "suspend_hi", 0);
12599 sysfs_set_num(sra, NULL, "suspend_lo", 0);
12600
12601 return ret_val;
12602 }
12603
12604 /*******************************************************************************
12605 * Function: calculate_bitmap_min_chunksize
12606 * Description: Calculates the minimal valid bitmap chunk size
12607 * Parameters:
12608 * max_bits : indicate how many bits can be used for the bitmap
12609 * data_area_size : the size of the data area covered by the bitmap
12610 *
12611 * Returns:
12612 * The bitmap chunk size
12613 ******************************************************************************/
12614 static unsigned long long
12615 calculate_bitmap_min_chunksize(unsigned long long max_bits,
12616 unsigned long long data_area_size)
12617 {
12618 unsigned long long min_chunk =
12619 4096; /* sub-page chunks don't work yet.. */
12620 unsigned long long bits = data_area_size / min_chunk + 1;
12621
12622 while (bits > max_bits) {
12623 min_chunk *= 2;
12624 bits = (bits + 1) / 2;
12625 }
12626 return min_chunk;
12627 }
12628
12629 /*******************************************************************************
12630 * Function: calculate_bitmap_chunksize
12631 * Description: Calculates the bitmap chunk size for the given device
12632 * Parameters:
12633 * st : supertype information
12634 * dev : device for the bitmap
12635 *
12636 * Returns:
12637 * The bitmap chunk size
12638 ******************************************************************************/
12639 static unsigned long long calculate_bitmap_chunksize(struct supertype *st,
12640 struct imsm_dev *dev)
12641 {
12642 struct intel_super *super = st->sb;
12643 unsigned long long min_chunksize;
12644 unsigned long long result = IMSM_DEFAULT_BITMAP_CHUNKSIZE;
12645 size_t dev_size = imsm_dev_size(dev);
12646
12647 min_chunksize = calculate_bitmap_min_chunksize(
12648 IMSM_BITMAP_AREA_SIZE * super->sector_size, dev_size);
12649
12650 if (result < min_chunksize)
12651 result = min_chunksize;
12652
12653 return result;
12654 }
12655
12656 /*******************************************************************************
12657 * Function: init_bitmap_header
12658 * Description: Initialize the bitmap header structure
12659 * Parameters:
12660 * st : supertype information
12661 * bms : bitmap header struct to initialize
12662 * dev : device for the bitmap
12663 *
12664 * Returns:
12665 * 0 : success
12666 * -1 : fail
12667 ******************************************************************************/
12668 static int init_bitmap_header(struct supertype *st, struct bitmap_super_s *bms,
12669 struct imsm_dev *dev)
12670 {
12671 int vol_uuid[4];
12672
12673 if (!bms || !dev)
12674 return -1;
12675
12676 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
12677 bms->version = __cpu_to_le32(BITMAP_MAJOR_HI);
12678 bms->daemon_sleep = __cpu_to_le32(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP);
12679 bms->sync_size = __cpu_to_le64(IMSM_BITMAP_AREA_SIZE);
12680 bms->write_behind = __cpu_to_le32(0);
12681
12682 uuid_from_super_imsm(st, vol_uuid);
12683 memcpy(bms->uuid, vol_uuid, 16);
12684
12685 bms->chunksize = calculate_bitmap_chunksize(st, dev);
12686
12687 return 0;
12688 }
12689
12690 /*******************************************************************************
12691 * Function: validate_internal_bitmap_for_drive
12692 * Description: Verify if the bitmap header for a given drive.
12693 * Parameters:
12694 * st : supertype information
12695 * offset : The offset from the beginning of the drive where to look for
12696 * the bitmap header.
12697 * d : the drive info
12698 *
12699 * Returns:
12700 * 0 : success
12701 * -1 : fail
12702 ******************************************************************************/
12703 static int validate_internal_bitmap_for_drive(struct supertype *st,
12704 unsigned long long offset,
12705 struct dl *d)
12706 {
12707 struct intel_super *super = st->sb;
12708 int ret = -1;
12709 int vol_uuid[4];
12710 bitmap_super_t *bms;
12711 int fd;
12712
12713 if (!d)
12714 return -1;
12715
12716 void *read_buf;
12717
12718 if (posix_memalign(&read_buf, MAX_SECTOR_SIZE, IMSM_BITMAP_HEADER_SIZE))
12719 return -1;
12720
12721 fd = d->fd;
12722 if (!is_fd_valid(fd)) {
12723 fd = open(d->devname, O_RDONLY, 0);
12724
12725 if (!is_fd_valid(fd)) {
12726 dprintf("cannot open the device %s\n", d->devname);
12727 goto abort;
12728 }
12729 }
12730
12731 if (lseek64(fd, offset * super->sector_size, SEEK_SET) < 0)
12732 goto abort;
12733 if (read(fd, read_buf, IMSM_BITMAP_HEADER_SIZE) !=
12734 IMSM_BITMAP_HEADER_SIZE)
12735 goto abort;
12736
12737 uuid_from_super_imsm(st, vol_uuid);
12738
12739 bms = read_buf;
12740 if ((bms->magic != __cpu_to_le32(BITMAP_MAGIC)) ||
12741 (bms->version != __cpu_to_le32(BITMAP_MAJOR_HI)) ||
12742 (!same_uuid((int *)bms->uuid, vol_uuid, st->ss->swapuuid))) {
12743 dprintf("wrong bitmap header detected\n");
12744 goto abort;
12745 }
12746
12747 ret = 0;
12748 abort:
12749 if (!is_fd_valid(d->fd))
12750 close_fd(&fd);
12751
12752 if (read_buf)
12753 free(read_buf);
12754
12755 return ret;
12756 }
12757
12758 /*******************************************************************************
12759 * Function: validate_internal_bitmap_imsm
12760 * Description: Verify if the bitmap header is in place and with proper data.
12761 * Parameters:
12762 * st : supertype information
12763 *
12764 * Returns:
12765 * 0 : success or device w/o RWH_BITMAP
12766 * -1 : fail
12767 ******************************************************************************/
12768 static int validate_internal_bitmap_imsm(struct supertype *st)
12769 {
12770 struct intel_super *super = st->sb;
12771 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
12772 unsigned long long offset;
12773 struct dl *d;
12774
12775 if (dev->rwh_policy != RWH_BITMAP)
12776 return 0;
12777
12778 offset = get_bitmap_header_sector(super, super->current_vol);
12779 for (d = super->disks; d; d = d->next) {
12780 if (d->index < 0 || is_failed(&d->disk))
12781 continue;
12782
12783 if (validate_internal_bitmap_for_drive(st, offset, d)) {
12784 pr_err("imsm: bitmap validation failed\n");
12785 return -1;
12786 }
12787 }
12788 return 0;
12789 }
12790
12791 /*******************************************************************************
12792 * Function: add_internal_bitmap_imsm
12793 * Description: Mark the volume to use the bitmap and updates the chunk size value.
12794 * Parameters:
12795 * st : supertype information
12796 * chunkp : bitmap chunk size
12797 * delay : not used for imsm
12798 * write_behind : not used for imsm
12799 * size : not used for imsm
12800 * may_change : not used for imsm
12801 * amajor : not used for imsm
12802 *
12803 * Returns:
12804 * 0 : success
12805 * -1 : fail
12806 ******************************************************************************/
12807 static int add_internal_bitmap_imsm(struct supertype *st, int *chunkp,
12808 int delay, int write_behind,
12809 unsigned long long size, int may_change,
12810 int amajor)
12811 {
12812 struct intel_super *super = st->sb;
12813 int vol_idx = super->current_vol;
12814 struct imsm_dev *dev;
12815
12816 if (!super->devlist || vol_idx == -1 || !chunkp)
12817 return -1;
12818
12819 dev = get_imsm_dev(super, vol_idx);
12820 dev->rwh_policy = RWH_BITMAP;
12821 *chunkp = calculate_bitmap_chunksize(st, dev);
12822 return 0;
12823 }
12824
12825 /*******************************************************************************
12826 * Function: locate_bitmap_imsm
12827 * Description: Seek 'fd' to start of write-intent-bitmap.
12828 * Parameters:
12829 * st : supertype information
12830 * fd : file descriptor for the device
12831 * node_num : not used for imsm
12832 *
12833 * Returns:
12834 * 0 : success
12835 * -1 : fail
12836 ******************************************************************************/
12837 static int locate_bitmap_imsm(struct supertype *st, int fd, int node_num)
12838 {
12839 struct intel_super *super = st->sb;
12840 unsigned long long offset;
12841 int vol_idx = super->current_vol;
12842
12843 if (!super->devlist || vol_idx == -1)
12844 return -1;
12845
12846 offset = get_bitmap_header_sector(super, super->current_vol);
12847 dprintf("bitmap header offset is %llu\n", offset);
12848
12849 lseek64(fd, offset << 9, 0);
12850
12851 return 0;
12852 }
12853
12854 /*******************************************************************************
12855 * Function: write_init_bitmap_imsm
12856 * Description: Write a bitmap header and prepares the area for the bitmap.
12857 * Parameters:
12858 * st : supertype information
12859 * fd : file descriptor for the device
12860 * update : not used for imsm
12861 *
12862 * Returns:
12863 * 0 : success
12864 * -1 : fail
12865 ******************************************************************************/
12866 static int write_init_bitmap_imsm(struct supertype *st, int fd,
12867 enum bitmap_update update)
12868 {
12869 struct intel_super *super = st->sb;
12870 int vol_idx = super->current_vol;
12871 int ret = 0;
12872 unsigned long long offset;
12873 bitmap_super_t bms = { 0 };
12874 size_t written = 0;
12875 size_t to_write;
12876 ssize_t rv_num;
12877 void *buf;
12878
12879 if (!super->devlist || !super->sector_size || vol_idx == -1)
12880 return -1;
12881
12882 struct imsm_dev *dev = get_imsm_dev(super, vol_idx);
12883
12884 /* first clear the space for bitmap header */
12885 unsigned long long bitmap_area_start =
12886 get_bitmap_header_sector(super, vol_idx);
12887
12888 dprintf("zeroing area start (%llu) and size (%u)\n", bitmap_area_start,
12889 IMSM_BITMAP_AND_HEADER_SIZE / super->sector_size);
12890 if (zero_disk_range(fd, bitmap_area_start,
12891 IMSM_BITMAP_HEADER_SIZE / super->sector_size)) {
12892 pr_err("imsm: cannot zeroing the space for the bitmap\n");
12893 return -1;
12894 }
12895
12896 /* The bitmap area should be filled with "1"s to perform initial
12897 * synchronization.
12898 */
12899 if (posix_memalign(&buf, MAX_SECTOR_SIZE, MAX_SECTOR_SIZE))
12900 return -1;
12901 memset(buf, 0xFF, MAX_SECTOR_SIZE);
12902 offset = get_bitmap_sector(super, vol_idx);
12903 lseek64(fd, offset << 9, 0);
12904 while (written < IMSM_BITMAP_AREA_SIZE) {
12905 to_write = IMSM_BITMAP_AREA_SIZE - written;
12906 if (to_write > MAX_SECTOR_SIZE)
12907 to_write = MAX_SECTOR_SIZE;
12908 rv_num = write(fd, buf, MAX_SECTOR_SIZE);
12909 if (rv_num != MAX_SECTOR_SIZE) {
12910 ret = -1;
12911 dprintf("cannot initialize bitmap area\n");
12912 goto abort;
12913 }
12914 written += rv_num;
12915 }
12916
12917 /* write a bitmap header */
12918 init_bitmap_header(st, &bms, dev);
12919 memset(buf, 0, MAX_SECTOR_SIZE);
12920 memcpy(buf, &bms, sizeof(bitmap_super_t));
12921 if (locate_bitmap_imsm(st, fd, 0)) {
12922 ret = -1;
12923 dprintf("cannot locate the bitmap\n");
12924 goto abort;
12925 }
12926 if (write(fd, buf, MAX_SECTOR_SIZE) != MAX_SECTOR_SIZE) {
12927 ret = -1;
12928 dprintf("cannot write the bitmap header\n");
12929 goto abort;
12930 }
12931 fsync(fd);
12932
12933 abort:
12934 free(buf);
12935
12936 return ret;
12937 }
12938
12939 /*******************************************************************************
12940 * Function: is_vol_to_setup_bitmap
12941 * Description: Checks if a bitmap should be activated on the dev.
12942 * Parameters:
12943 * info : info about the volume to setup the bitmap
12944 * dev : the device to check against bitmap creation
12945 *
12946 * Returns:
12947 * 0 : bitmap should be set up on the device
12948 * -1 : otherwise
12949 ******************************************************************************/
12950 static int is_vol_to_setup_bitmap(struct mdinfo *info, struct imsm_dev *dev)
12951 {
12952 if (!dev || !info)
12953 return -1;
12954
12955 if ((strcmp((char *)dev->volume, info->name) == 0) &&
12956 (dev->rwh_policy == RWH_BITMAP))
12957 return -1;
12958
12959 return 0;
12960 }
12961
12962 /*******************************************************************************
12963 * Function: set_bitmap_sysfs
12964 * Description: Set the sysfs atributes of a given volume to activate the bitmap.
12965 * Parameters:
12966 * info : info about the volume where the bitmap should be setup
12967 * chunksize : bitmap chunk size
12968 * location : location of the bitmap
12969 *
12970 * Returns:
12971 * 0 : success
12972 * -1 : fail
12973 ******************************************************************************/
12974 static int set_bitmap_sysfs(struct mdinfo *info, unsigned long long chunksize,
12975 char *location)
12976 {
12977 /* The bitmap/metadata is set to external to allow changing of value for
12978 * bitmap/location. When external is used, the kernel will treat an offset
12979 * related to the device's first lba (in opposition to the "internal" case
12980 * when this value is related to the beginning of the superblock).
12981 */
12982 if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
12983 dprintf("failed to set bitmap/metadata\n");
12984 return -1;
12985 }
12986
12987 /* It can only be changed when no bitmap is active.
12988 * Should be bigger than 512 and must be power of 2.
12989 * It is expecting the value in bytes.
12990 */
12991 if (sysfs_set_num(info, NULL, "bitmap/chunksize",
12992 __cpu_to_le32(chunksize))) {
12993 dprintf("failed to set bitmap/chunksize\n");
12994 return -1;
12995 }
12996
12997 /* It is expecting the value in sectors. */
12998 if (sysfs_set_num(info, NULL, "bitmap/space",
12999 __cpu_to_le64(IMSM_BITMAP_AREA_SIZE))) {
13000 dprintf("failed to set bitmap/space\n");
13001 return -1;
13002 }
13003
13004 /* Determines the delay between the bitmap updates.
13005 * It is expecting the value in seconds.
13006 */
13007 if (sysfs_set_num(info, NULL, "bitmap/time_base",
13008 __cpu_to_le64(IMSM_DEFAULT_BITMAP_DAEMON_SLEEP))) {
13009 dprintf("failed to set bitmap/time_base\n");
13010 return -1;
13011 }
13012
13013 /* It is expecting the value in sectors with a sign at the beginning. */
13014 if (sysfs_set_str(info, NULL, "bitmap/location", location)) {
13015 dprintf("failed to set bitmap/location\n");
13016 return -1;
13017 }
13018
13019 return 0;
13020 }
13021
13022 /*******************************************************************************
13023 * Function: set_bitmap_imsm
13024 * Description: Setup the bitmap for the given volume
13025 * Parameters:
13026 * st : supertype information
13027 * info : info about the volume where the bitmap should be setup
13028 *
13029 * Returns:
13030 * 0 : success
13031 * -1 : fail
13032 ******************************************************************************/
13033 static int set_bitmap_imsm(struct supertype *st, struct mdinfo *info)
13034 {
13035 struct intel_super *super = st->sb;
13036 int prev_current_vol = super->current_vol;
13037 struct imsm_dev *dev;
13038 int ret = -1;
13039 char location[16] = "";
13040 unsigned long long chunksize;
13041 struct intel_dev *dev_it;
13042
13043 for (dev_it = super->devlist; dev_it; dev_it = dev_it->next) {
13044 super->current_vol = dev_it->index;
13045 dev = get_imsm_dev(super, super->current_vol);
13046
13047 if (is_vol_to_setup_bitmap(info, dev)) {
13048 if (validate_internal_bitmap_imsm(st)) {
13049 dprintf("bitmap header validation failed\n");
13050 goto abort;
13051 }
13052
13053 chunksize = calculate_bitmap_chunksize(st, dev);
13054 dprintf("chunk size is %llu\n", chunksize);
13055
13056 snprintf(location, sizeof(location), "+%llu",
13057 get_bitmap_sector(super, super->current_vol));
13058 dprintf("bitmap offset is %s\n", location);
13059
13060 if (set_bitmap_sysfs(info, chunksize, location)) {
13061 dprintf("cannot setup the bitmap\n");
13062 goto abort;
13063 }
13064 }
13065 }
13066 ret = 0;
13067 abort:
13068 super->current_vol = prev_current_vol;
13069 return ret;
13070 }
13071
13072 struct superswitch super_imsm = {
13073 .examine_super = examine_super_imsm,
13074 .brief_examine_super = brief_examine_super_imsm,
13075 .brief_examine_subarrays = brief_examine_subarrays_imsm,
13076 .export_examine_super = export_examine_super_imsm,
13077 .detail_super = detail_super_imsm,
13078 .brief_detail_super = brief_detail_super_imsm,
13079 .write_init_super = write_init_super_imsm,
13080 .validate_geometry = validate_geometry_imsm,
13081 .add_to_super = add_to_super_imsm,
13082 .remove_from_super = remove_from_super_imsm,
13083 .detail_platform = detail_platform_imsm,
13084 .export_detail_platform = export_detail_platform_imsm,
13085 .kill_subarray = kill_subarray_imsm,
13086 .update_subarray = update_subarray_imsm,
13087 .load_container = load_container_imsm,
13088 .default_geometry = default_geometry_imsm,
13089 .test_and_add_drive_policies = test_and_add_drive_policies_imsm,
13090 .reshape_super = imsm_reshape_super,
13091 .manage_reshape = imsm_manage_reshape,
13092 .recover_backup = recover_backup_imsm,
13093 .examine_badblocks = examine_badblocks_imsm,
13094 .match_home = match_home_imsm,
13095 .uuid_from_super= uuid_from_super_imsm,
13096 .getinfo_super = getinfo_super_imsm,
13097 .getinfo_super_disks = getinfo_super_disks_imsm,
13098 .update_super = update_super_imsm,
13099
13100 .avail_size = avail_size_imsm,
13101 .get_spare_criteria = get_spare_criteria_imsm,
13102
13103 .compare_super = compare_super_imsm,
13104
13105 .load_super = load_super_imsm,
13106 .init_super = init_super_imsm,
13107 .store_super = store_super_imsm,
13108 .free_super = free_super_imsm,
13109 .match_metadata_desc = match_metadata_desc_imsm,
13110 .container_content = container_content_imsm,
13111 .validate_container = validate_container_imsm,
13112
13113 .add_internal_bitmap = add_internal_bitmap_imsm,
13114 .locate_bitmap = locate_bitmap_imsm,
13115 .write_bitmap = write_init_bitmap_imsm,
13116 .set_bitmap = set_bitmap_imsm,
13117
13118 .write_init_ppl = write_init_ppl_imsm,
13119 .validate_ppl = validate_ppl_imsm,
13120
13121 .external = 1,
13122 .swapuuid = 0,
13123 .name = "imsm",
13124
13125 /* for mdmon */
13126 .open_new = imsm_open_new,
13127 .set_array_state= imsm_set_array_state,
13128 .set_disk = imsm_set_disk,
13129 .sync_metadata = imsm_sync_metadata,
13130 .activate_spare = imsm_activate_spare,
13131 .process_update = imsm_process_update,
13132 .prepare_update = imsm_prepare_update,
13133 .record_bad_block = imsm_record_badblock,
13134 .clear_bad_block = imsm_clear_badblock,
13135 .get_bad_blocks = imsm_get_badblocks,
13136 };