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