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