]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
imsm: FIX: Chunk size migration problem
[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 if (is_gen_migration(dev) && missing) {
2687 /* during general migration we need all disks
2688 * that process is running on.
2689 * No new missing disk is allowed.
2690 */
2691 max_enough = -1;
2692 enough = -1;
2693 /* no more checks necessary
2694 */
2695 break;
2696 }
2697 /* in the missing/failed disk case check to see
2698 * if at least one array is runnable
2699 */
2700 max_enough = max(max_enough, enough);
2701 }
2702 dprintf("%s: enough: %d\n", __func__, max_enough);
2703 info->container_enough = max_enough;
2704
2705 if (super->disks) {
2706 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2707
2708 disk = &super->disks->disk;
2709 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
2710 info->component_size = reserved;
2711 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
2712 /* we don't change info->disk.raid_disk here because
2713 * this state will be finalized in mdmon after we have
2714 * found the 'most fresh' version of the metadata
2715 */
2716 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2717 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
2718 }
2719
2720 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2721 * ->compare_super may have updated the 'num_raid_devs' field for spares
2722 */
2723 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
2724 uuid_from_super_imsm(st, info->uuid);
2725 else
2726 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
2727
2728 /* I don't know how to compute 'map' on imsm, so use safe default */
2729 if (map) {
2730 int i;
2731 for (i = 0; i < map_disks; i++)
2732 map[i] = 1;
2733 }
2734
2735 }
2736
2737 /* allocates memory and fills disk in mdinfo structure
2738 * for each disk in array */
2739 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
2740 {
2741 struct mdinfo *mddev = NULL;
2742 struct intel_super *super = st->sb;
2743 struct imsm_disk *disk;
2744 int count = 0;
2745 struct dl *dl;
2746 if (!super || !super->disks)
2747 return NULL;
2748 dl = super->disks;
2749 mddev = malloc(sizeof(*mddev));
2750 if (!mddev) {
2751 fprintf(stderr, Name ": Failed to allocate memory.\n");
2752 return NULL;
2753 }
2754 memset(mddev, 0, sizeof(*mddev));
2755 while (dl) {
2756 struct mdinfo *tmp;
2757 disk = &dl->disk;
2758 tmp = malloc(sizeof(*tmp));
2759 if (!tmp) {
2760 fprintf(stderr, Name ": Failed to allocate memory.\n");
2761 if (mddev)
2762 sysfs_free(mddev);
2763 return NULL;
2764 }
2765 memset(tmp, 0, sizeof(*tmp));
2766 if (mddev->devs)
2767 tmp->next = mddev->devs;
2768 mddev->devs = tmp;
2769 tmp->disk.number = count++;
2770 tmp->disk.major = dl->major;
2771 tmp->disk.minor = dl->minor;
2772 tmp->disk.state = is_configured(disk) ?
2773 (1 << MD_DISK_ACTIVE) : 0;
2774 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2775 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
2776 tmp->disk.raid_disk = -1;
2777 dl = dl->next;
2778 }
2779 return mddev;
2780 }
2781
2782 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
2783 char *update, char *devname, int verbose,
2784 int uuid_set, char *homehost)
2785 {
2786 /* For 'assemble' and 'force' we need to return non-zero if any
2787 * change was made. For others, the return value is ignored.
2788 * Update options are:
2789 * force-one : This device looks a bit old but needs to be included,
2790 * update age info appropriately.
2791 * assemble: clear any 'faulty' flag to allow this device to
2792 * be assembled.
2793 * force-array: Array is degraded but being forced, mark it clean
2794 * if that will be needed to assemble it.
2795 *
2796 * newdev: not used ????
2797 * grow: Array has gained a new device - this is currently for
2798 * linear only
2799 * resync: mark as dirty so a resync will happen.
2800 * name: update the name - preserving the homehost
2801 * uuid: Change the uuid of the array to match watch is given
2802 *
2803 * Following are not relevant for this imsm:
2804 * sparc2.2 : update from old dodgey metadata
2805 * super-minor: change the preferred_minor number
2806 * summaries: update redundant counters.
2807 * homehost: update the recorded homehost
2808 * _reshape_progress: record new reshape_progress position.
2809 */
2810 int rv = 1;
2811 struct intel_super *super = st->sb;
2812 struct imsm_super *mpb;
2813
2814 /* we can only update container info */
2815 if (!super || super->current_vol >= 0 || !super->anchor)
2816 return 1;
2817
2818 mpb = super->anchor;
2819
2820 if (strcmp(update, "uuid") == 0) {
2821 /* We take this to mean that the family_num should be updated.
2822 * However that is much smaller than the uuid so we cannot really
2823 * allow an explicit uuid to be given. And it is hard to reliably
2824 * know if one was.
2825 * So if !uuid_set we know the current uuid is random and just used
2826 * the first 'int' and copy it to the other 3 positions.
2827 * Otherwise we require the 4 'int's to be the same as would be the
2828 * case if we are using a random uuid. So an explicit uuid will be
2829 * accepted as long as all for ints are the same... which shouldn't hurt
2830 */
2831 if (!uuid_set) {
2832 info->uuid[1] = info->uuid[2] = info->uuid[3] = info->uuid[0];
2833 rv = 0;
2834 } else {
2835 if (info->uuid[0] != info->uuid[1] ||
2836 info->uuid[1] != info->uuid[2] ||
2837 info->uuid[2] != info->uuid[3])
2838 rv = -1;
2839 else
2840 rv = 0;
2841 }
2842 if (rv == 0)
2843 mpb->orig_family_num = info->uuid[0];
2844 } else if (strcmp(update, "assemble") == 0)
2845 rv = 0;
2846 else
2847 rv = -1;
2848
2849 /* successful update? recompute checksum */
2850 if (rv == 0)
2851 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
2852
2853 return rv;
2854 }
2855
2856 static size_t disks_to_mpb_size(int disks)
2857 {
2858 size_t size;
2859
2860 size = sizeof(struct imsm_super);
2861 size += (disks - 1) * sizeof(struct imsm_disk);
2862 size += 2 * sizeof(struct imsm_dev);
2863 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2864 size += (4 - 2) * sizeof(struct imsm_map);
2865 /* 4 possible disk_ord_tbl's */
2866 size += 4 * (disks - 1) * sizeof(__u32);
2867
2868 return size;
2869 }
2870
2871 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
2872 {
2873 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
2874 return 0;
2875
2876 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
2877 }
2878
2879 static void free_devlist(struct intel_super *super)
2880 {
2881 struct intel_dev *dv;
2882
2883 while (super->devlist) {
2884 dv = super->devlist->next;
2885 free(super->devlist->dev);
2886 free(super->devlist);
2887 super->devlist = dv;
2888 }
2889 }
2890
2891 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
2892 {
2893 memcpy(dest, src, sizeof_imsm_dev(src, 0));
2894 }
2895
2896 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
2897 {
2898 /*
2899 * return:
2900 * 0 same, or first was empty, and second was copied
2901 * 1 second had wrong number
2902 * 2 wrong uuid
2903 * 3 wrong other info
2904 */
2905 struct intel_super *first = st->sb;
2906 struct intel_super *sec = tst->sb;
2907
2908 if (!first) {
2909 st->sb = tst->sb;
2910 tst->sb = NULL;
2911 return 0;
2912 }
2913 /* in platform dependent environment test if the disks
2914 * use the same Intel hba
2915 */
2916 if (!check_env("IMSM_NO_PLATFORM")) {
2917 if (!first->hba || !sec->hba ||
2918 (first->hba->type != sec->hba->type)) {
2919 fprintf(stderr,
2920 "HBAs of devices does not match %s != %s\n",
2921 first->hba ? get_sys_dev_type(first->hba->type) : NULL,
2922 sec->hba ? get_sys_dev_type(sec->hba->type) : NULL);
2923 return 3;
2924 }
2925 }
2926
2927 /* if an anchor does not have num_raid_devs set then it is a free
2928 * floating spare
2929 */
2930 if (first->anchor->num_raid_devs > 0 &&
2931 sec->anchor->num_raid_devs > 0) {
2932 /* Determine if these disks might ever have been
2933 * related. Further disambiguation can only take place
2934 * in load_super_imsm_all
2935 */
2936 __u32 first_family = first->anchor->orig_family_num;
2937 __u32 sec_family = sec->anchor->orig_family_num;
2938
2939 if (memcmp(first->anchor->sig, sec->anchor->sig,
2940 MAX_SIGNATURE_LENGTH) != 0)
2941 return 3;
2942
2943 if (first_family == 0)
2944 first_family = first->anchor->family_num;
2945 if (sec_family == 0)
2946 sec_family = sec->anchor->family_num;
2947
2948 if (first_family != sec_family)
2949 return 3;
2950
2951 }
2952
2953
2954 /* if 'first' is a spare promote it to a populated mpb with sec's
2955 * family number
2956 */
2957 if (first->anchor->num_raid_devs == 0 &&
2958 sec->anchor->num_raid_devs > 0) {
2959 int i;
2960 struct intel_dev *dv;
2961 struct imsm_dev *dev;
2962
2963 /* we need to copy raid device info from sec if an allocation
2964 * fails here we don't associate the spare
2965 */
2966 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
2967 dv = malloc(sizeof(*dv));
2968 if (!dv)
2969 break;
2970 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
2971 if (!dev) {
2972 free(dv);
2973 break;
2974 }
2975 dv->dev = dev;
2976 dv->index = i;
2977 dv->next = first->devlist;
2978 first->devlist = dv;
2979 }
2980 if (i < sec->anchor->num_raid_devs) {
2981 /* allocation failure */
2982 free_devlist(first);
2983 fprintf(stderr, "imsm: failed to associate spare\n");
2984 return 3;
2985 }
2986 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
2987 first->anchor->orig_family_num = sec->anchor->orig_family_num;
2988 first->anchor->family_num = sec->anchor->family_num;
2989 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
2990 for (i = 0; i < sec->anchor->num_raid_devs; i++)
2991 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
2992 }
2993
2994 return 0;
2995 }
2996
2997 static void fd2devname(int fd, char *name)
2998 {
2999 struct stat st;
3000 char path[256];
3001 char dname[PATH_MAX];
3002 char *nm;
3003 int rv;
3004
3005 name[0] = '\0';
3006 if (fstat(fd, &st) != 0)
3007 return;
3008 sprintf(path, "/sys/dev/block/%d:%d",
3009 major(st.st_rdev), minor(st.st_rdev));
3010
3011 rv = readlink(path, dname, sizeof(dname)-1);
3012 if (rv <= 0)
3013 return;
3014
3015 dname[rv] = '\0';
3016 nm = strrchr(dname, '/');
3017 if (nm) {
3018 nm++;
3019 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
3020 }
3021 }
3022
3023 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
3024
3025 static int imsm_read_serial(int fd, char *devname,
3026 __u8 serial[MAX_RAID_SERIAL_LEN])
3027 {
3028 unsigned char scsi_serial[255];
3029 int rv;
3030 int rsp_len;
3031 int len;
3032 char *dest;
3033 char *src;
3034 char *rsp_buf;
3035 int i;
3036
3037 memset(scsi_serial, 0, sizeof(scsi_serial));
3038
3039 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
3040
3041 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
3042 memset(serial, 0, MAX_RAID_SERIAL_LEN);
3043 fd2devname(fd, (char *) serial);
3044 return 0;
3045 }
3046
3047 if (rv != 0) {
3048 if (devname)
3049 fprintf(stderr,
3050 Name ": Failed to retrieve serial for %s\n",
3051 devname);
3052 return rv;
3053 }
3054
3055 rsp_len = scsi_serial[3];
3056 if (!rsp_len) {
3057 if (devname)
3058 fprintf(stderr,
3059 Name ": Failed to retrieve serial for %s\n",
3060 devname);
3061 return 2;
3062 }
3063 rsp_buf = (char *) &scsi_serial[4];
3064
3065 /* trim all whitespace and non-printable characters and convert
3066 * ':' to ';'
3067 */
3068 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
3069 src = &rsp_buf[i];
3070 if (*src > 0x20) {
3071 /* ':' is reserved for use in placeholder serial
3072 * numbers for missing disks
3073 */
3074 if (*src == ':')
3075 *dest++ = ';';
3076 else
3077 *dest++ = *src;
3078 }
3079 }
3080 len = dest - rsp_buf;
3081 dest = rsp_buf;
3082
3083 /* truncate leading characters */
3084 if (len > MAX_RAID_SERIAL_LEN) {
3085 dest += len - MAX_RAID_SERIAL_LEN;
3086 len = MAX_RAID_SERIAL_LEN;
3087 }
3088
3089 memset(serial, 0, MAX_RAID_SERIAL_LEN);
3090 memcpy(serial, dest, len);
3091
3092 return 0;
3093 }
3094
3095 static int serialcmp(__u8 *s1, __u8 *s2)
3096 {
3097 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
3098 }
3099
3100 static void serialcpy(__u8 *dest, __u8 *src)
3101 {
3102 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
3103 }
3104
3105 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
3106 {
3107 struct dl *dl;
3108
3109 for (dl = super->disks; dl; dl = dl->next)
3110 if (serialcmp(dl->serial, serial) == 0)
3111 break;
3112
3113 return dl;
3114 }
3115
3116 static struct imsm_disk *
3117 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
3118 {
3119 int i;
3120
3121 for (i = 0; i < mpb->num_disks; i++) {
3122 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
3123
3124 if (serialcmp(disk->serial, serial) == 0) {
3125 if (idx)
3126 *idx = i;
3127 return disk;
3128 }
3129 }
3130
3131 return NULL;
3132 }
3133
3134 static int
3135 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
3136 {
3137 struct imsm_disk *disk;
3138 struct dl *dl;
3139 struct stat stb;
3140 int rv;
3141 char name[40];
3142 __u8 serial[MAX_RAID_SERIAL_LEN];
3143
3144 rv = imsm_read_serial(fd, devname, serial);
3145
3146 if (rv != 0)
3147 return 2;
3148
3149 dl = calloc(1, sizeof(*dl));
3150 if (!dl) {
3151 if (devname)
3152 fprintf(stderr,
3153 Name ": failed to allocate disk buffer for %s\n",
3154 devname);
3155 return 2;
3156 }
3157
3158 fstat(fd, &stb);
3159 dl->major = major(stb.st_rdev);
3160 dl->minor = minor(stb.st_rdev);
3161 dl->next = super->disks;
3162 dl->fd = keep_fd ? fd : -1;
3163 assert(super->disks == NULL);
3164 super->disks = dl;
3165 serialcpy(dl->serial, serial);
3166 dl->index = -2;
3167 dl->e = NULL;
3168 fd2devname(fd, name);
3169 if (devname)
3170 dl->devname = strdup(devname);
3171 else
3172 dl->devname = strdup(name);
3173
3174 /* look up this disk's index in the current anchor */
3175 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
3176 if (disk) {
3177 dl->disk = *disk;
3178 /* only set index on disks that are a member of a
3179 * populated contianer, i.e. one with raid_devs
3180 */
3181 if (is_failed(&dl->disk))
3182 dl->index = -2;
3183 else if (is_spare(&dl->disk))
3184 dl->index = -1;
3185 }
3186
3187 return 0;
3188 }
3189
3190 #ifndef MDASSEMBLE
3191 /* When migrating map0 contains the 'destination' state while map1
3192 * contains the current state. When not migrating map0 contains the
3193 * current state. This routine assumes that map[0].map_state is set to
3194 * the current array state before being called.
3195 *
3196 * Migration is indicated by one of the following states
3197 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3198 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3199 * map1state=unitialized)
3200 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3201 * map1state=normal)
3202 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3203 * map1state=degraded)
3204 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3205 * map1state=normal)
3206 */
3207 static void migrate(struct imsm_dev *dev, struct intel_super *super,
3208 __u8 to_state, int migr_type)
3209 {
3210 struct imsm_map *dest;
3211 struct imsm_map *src = get_imsm_map(dev, MAP_0);
3212
3213 dev->vol.migr_state = 1;
3214 set_migr_type(dev, migr_type);
3215 dev->vol.curr_migr_unit = 0;
3216 dest = get_imsm_map(dev, MAP_1);
3217
3218 /* duplicate and then set the target end state in map[0] */
3219 memcpy(dest, src, sizeof_imsm_map(src));
3220 if ((migr_type == MIGR_REBUILD) ||
3221 (migr_type == MIGR_GEN_MIGR)) {
3222 __u32 ord;
3223 int i;
3224
3225 for (i = 0; i < src->num_members; i++) {
3226 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
3227 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
3228 }
3229 }
3230
3231 if (migr_type == MIGR_GEN_MIGR)
3232 /* Clear migration record */
3233 memset(super->migr_rec, 0, sizeof(struct migr_record));
3234
3235 src->map_state = to_state;
3236 }
3237
3238 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
3239 __u8 map_state)
3240 {
3241 struct imsm_map *map = get_imsm_map(dev, MAP_0);
3242 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
3243 MAP_0 : MAP_1);
3244 int i, j;
3245
3246 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3247 * completed in the last migration.
3248 *
3249 * FIXME add support for raid-level-migration
3250 */
3251 if ((map_state != map->map_state) && (is_gen_migration(dev) == 0) &&
3252 (prev->map_state != IMSM_T_STATE_UNINITIALIZED)) {
3253 /* when final map state is other than expected
3254 * merge maps (not for migration)
3255 */
3256 int failed;
3257
3258 for (i = 0; i < prev->num_members; i++)
3259 for (j = 0; j < map->num_members; j++)
3260 /* during online capacity expansion
3261 * disks position can be changed
3262 * if takeover is used
3263 */
3264 if (ord_to_idx(map->disk_ord_tbl[j]) ==
3265 ord_to_idx(prev->disk_ord_tbl[i])) {
3266 map->disk_ord_tbl[j] |=
3267 prev->disk_ord_tbl[i];
3268 break;
3269 }
3270 failed = imsm_count_failed(super, dev, MAP_0);
3271 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
3272 }
3273
3274 dev->vol.migr_state = 0;
3275 set_migr_type(dev, 0);
3276 dev->vol.curr_migr_unit = 0;
3277 map->map_state = map_state;
3278 }
3279 #endif
3280
3281 static int parse_raid_devices(struct intel_super *super)
3282 {
3283 int i;
3284 struct imsm_dev *dev_new;
3285 size_t len, len_migr;
3286 size_t max_len = 0;
3287 size_t space_needed = 0;
3288 struct imsm_super *mpb = super->anchor;
3289
3290 for (i = 0; i < super->anchor->num_raid_devs; i++) {
3291 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
3292 struct intel_dev *dv;
3293
3294 len = sizeof_imsm_dev(dev_iter, 0);
3295 len_migr = sizeof_imsm_dev(dev_iter, 1);
3296 if (len_migr > len)
3297 space_needed += len_migr - len;
3298
3299 dv = malloc(sizeof(*dv));
3300 if (!dv)
3301 return 1;
3302 if (max_len < len_migr)
3303 max_len = len_migr;
3304 if (max_len > len_migr)
3305 space_needed += max_len - len_migr;
3306 dev_new = malloc(max_len);
3307 if (!dev_new) {
3308 free(dv);
3309 return 1;
3310 }
3311 imsm_copy_dev(dev_new, dev_iter);
3312 dv->dev = dev_new;
3313 dv->index = i;
3314 dv->next = super->devlist;
3315 super->devlist = dv;
3316 }
3317
3318 /* ensure that super->buf is large enough when all raid devices
3319 * are migrating
3320 */
3321 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
3322 void *buf;
3323
3324 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
3325 if (posix_memalign(&buf, 512, len) != 0)
3326 return 1;
3327
3328 memcpy(buf, super->buf, super->len);
3329 memset(buf + super->len, 0, len - super->len);
3330 free(super->buf);
3331 super->buf = buf;
3332 super->len = len;
3333 }
3334
3335 return 0;
3336 }
3337
3338 /* retrieve a pointer to the bbm log which starts after all raid devices */
3339 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
3340 {
3341 void *ptr = NULL;
3342
3343 if (__le32_to_cpu(mpb->bbm_log_size)) {
3344 ptr = mpb;
3345 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
3346 }
3347
3348 return ptr;
3349 }
3350
3351 /*******************************************************************************
3352 * Function: check_mpb_migr_compatibility
3353 * Description: Function checks for unsupported migration features:
3354 * - migration optimization area (pba_of_lba0)
3355 * - descending reshape (ascending_migr)
3356 * Parameters:
3357 * super : imsm metadata information
3358 * Returns:
3359 * 0 : migration is compatible
3360 * -1 : migration is not compatible
3361 ******************************************************************************/
3362 int check_mpb_migr_compatibility(struct intel_super *super)
3363 {
3364 struct imsm_map *map0, *map1;
3365 struct migr_record *migr_rec = super->migr_rec;
3366 int i;
3367
3368 for (i = 0; i < super->anchor->num_raid_devs; i++) {
3369 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
3370
3371 if (dev_iter &&
3372 dev_iter->vol.migr_state == 1 &&
3373 dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
3374 /* This device is migrating */
3375 map0 = get_imsm_map(dev_iter, MAP_0);
3376 map1 = get_imsm_map(dev_iter, MAP_1);
3377 if (map0->pba_of_lba0 != map1->pba_of_lba0)
3378 /* migration optimization area was used */
3379 return -1;
3380 if (migr_rec->ascending_migr == 0
3381 && migr_rec->dest_depth_per_unit > 0)
3382 /* descending reshape not supported yet */
3383 return -1;
3384 }
3385 }
3386 return 0;
3387 }
3388
3389 static void __free_imsm(struct intel_super *super, int free_disks);
3390
3391 /* load_imsm_mpb - read matrix metadata
3392 * allocates super->mpb to be freed by free_imsm
3393 */
3394 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
3395 {
3396 unsigned long long dsize;
3397 unsigned long long sectors;
3398 struct stat;
3399 struct imsm_super *anchor;
3400 __u32 check_sum;
3401
3402 get_dev_size(fd, NULL, &dsize);
3403 if (dsize < 1024) {
3404 if (devname)
3405 fprintf(stderr,
3406 Name ": %s: device to small for imsm\n",
3407 devname);
3408 return 1;
3409 }
3410
3411 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
3412 if (devname)
3413 fprintf(stderr, Name
3414 ": Cannot seek to anchor block on %s: %s\n",
3415 devname, strerror(errno));
3416 return 1;
3417 }
3418
3419 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
3420 if (devname)
3421 fprintf(stderr,
3422 Name ": Failed to allocate imsm anchor buffer"
3423 " on %s\n", devname);
3424 return 1;
3425 }
3426 if (read(fd, anchor, 512) != 512) {
3427 if (devname)
3428 fprintf(stderr,
3429 Name ": Cannot read anchor block on %s: %s\n",
3430 devname, strerror(errno));
3431 free(anchor);
3432 return 1;
3433 }
3434
3435 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
3436 if (devname)
3437 fprintf(stderr,
3438 Name ": no IMSM anchor on %s\n", devname);
3439 free(anchor);
3440 return 2;
3441 }
3442
3443 __free_imsm(super, 0);
3444 /* reload capability and hba */
3445
3446 /* capability and hba must be updated with new super allocation */
3447 find_intel_hba_capability(fd, super, devname);
3448 super->len = ROUND_UP(anchor->mpb_size, 512);
3449 if (posix_memalign(&super->buf, 512, super->len) != 0) {
3450 if (devname)
3451 fprintf(stderr,
3452 Name ": unable to allocate %zu byte mpb buffer\n",
3453 super->len);
3454 free(anchor);
3455 return 2;
3456 }
3457 memcpy(super->buf, anchor, 512);
3458
3459 sectors = mpb_sectors(anchor) - 1;
3460 free(anchor);
3461
3462 if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) {
3463 fprintf(stderr, Name
3464 ": %s could not allocate migr_rec buffer\n", __func__);
3465 free(super->buf);
3466 return 2;
3467 }
3468
3469 if (!sectors) {
3470 check_sum = __gen_imsm_checksum(super->anchor);
3471 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
3472 if (devname)
3473 fprintf(stderr,
3474 Name ": IMSM checksum %x != %x on %s\n",
3475 check_sum,
3476 __le32_to_cpu(super->anchor->check_sum),
3477 devname);
3478 return 2;
3479 }
3480
3481 return 0;
3482 }
3483
3484 /* read the extended mpb */
3485 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
3486 if (devname)
3487 fprintf(stderr,
3488 Name ": Cannot seek to extended mpb on %s: %s\n",
3489 devname, strerror(errno));
3490 return 1;
3491 }
3492
3493 if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
3494 if (devname)
3495 fprintf(stderr,
3496 Name ": Cannot read extended mpb on %s: %s\n",
3497 devname, strerror(errno));
3498 return 2;
3499 }
3500
3501 check_sum = __gen_imsm_checksum(super->anchor);
3502 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
3503 if (devname)
3504 fprintf(stderr,
3505 Name ": IMSM checksum %x != %x on %s\n",
3506 check_sum, __le32_to_cpu(super->anchor->check_sum),
3507 devname);
3508 return 3;
3509 }
3510
3511 /* FIXME the BBM log is disk specific so we cannot use this global
3512 * buffer for all disks. Ok for now since we only look at the global
3513 * bbm_log_size parameter to gate assembly
3514 */
3515 super->bbm_log = __get_imsm_bbm_log(super->anchor);
3516
3517 return 0;
3518 }
3519
3520 static int read_imsm_migr_rec(int fd, struct intel_super *super);
3521
3522 static int
3523 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
3524 {
3525 int err;
3526
3527 err = load_imsm_mpb(fd, super, devname);
3528 if (err)
3529 return err;
3530 err = load_imsm_disk(fd, super, devname, keep_fd);
3531 if (err)
3532 return err;
3533 err = parse_raid_devices(super);
3534
3535 return err;
3536 }
3537
3538 static void __free_imsm_disk(struct dl *d)
3539 {
3540 if (d->fd >= 0)
3541 close(d->fd);
3542 if (d->devname)
3543 free(d->devname);
3544 if (d->e)
3545 free(d->e);
3546 free(d);
3547
3548 }
3549
3550 static void free_imsm_disks(struct intel_super *super)
3551 {
3552 struct dl *d;
3553
3554 while (super->disks) {
3555 d = super->disks;
3556 super->disks = d->next;
3557 __free_imsm_disk(d);
3558 }
3559 while (super->disk_mgmt_list) {
3560 d = super->disk_mgmt_list;
3561 super->disk_mgmt_list = d->next;
3562 __free_imsm_disk(d);
3563 }
3564 while (super->missing) {
3565 d = super->missing;
3566 super->missing = d->next;
3567 __free_imsm_disk(d);
3568 }
3569
3570 }
3571
3572 /* free all the pieces hanging off of a super pointer */
3573 static void __free_imsm(struct intel_super *super, int free_disks)
3574 {
3575 struct intel_hba *elem, *next;
3576
3577 if (super->buf) {
3578 free(super->buf);
3579 super->buf = NULL;
3580 }
3581 /* unlink capability description */
3582 super->orom = NULL;
3583 if (super->migr_rec_buf) {
3584 free(super->migr_rec_buf);
3585 super->migr_rec_buf = NULL;
3586 }
3587 if (free_disks)
3588 free_imsm_disks(super);
3589 free_devlist(super);
3590 elem = super->hba;
3591 while (elem) {
3592 if (elem->path)
3593 free((void *)elem->path);
3594 next = elem->next;
3595 free(elem);
3596 elem = next;
3597 }
3598 super->hba = NULL;
3599 }
3600
3601 static void free_imsm(struct intel_super *super)
3602 {
3603 __free_imsm(super, 1);
3604 free(super);
3605 }
3606
3607 static void free_super_imsm(struct supertype *st)
3608 {
3609 struct intel_super *super = st->sb;
3610
3611 if (!super)
3612 return;
3613
3614 free_imsm(super);
3615 st->sb = NULL;
3616 }
3617
3618 static struct intel_super *alloc_super(void)
3619 {
3620 struct intel_super *super = malloc(sizeof(*super));
3621
3622 if (super) {
3623 memset(super, 0, sizeof(*super));
3624 super->current_vol = -1;
3625 super->create_offset = ~((__u32 ) 0);
3626 }
3627 return super;
3628 }
3629
3630 /*
3631 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3632 */
3633 static int find_intel_hba_capability(int fd, struct intel_super *super, char *devname)
3634 {
3635 struct sys_dev *hba_name;
3636 int rv = 0;
3637
3638 if ((fd < 0) || check_env("IMSM_NO_PLATFORM")) {
3639 super->orom = NULL;
3640 super->hba = NULL;
3641 return 0;
3642 }
3643 hba_name = find_disk_attached_hba(fd, NULL);
3644 if (!hba_name) {
3645 if (devname)
3646 fprintf(stderr,
3647 Name ": %s is not attached to Intel(R) RAID controller.\n",
3648 devname);
3649 return 1;
3650 }
3651 rv = attach_hba_to_super(super, hba_name);
3652 if (rv == 2) {
3653 if (devname) {
3654 struct intel_hba *hba = super->hba;
3655
3656 fprintf(stderr, Name ": %s is attached to Intel(R) %s RAID "
3657 "controller (%s),\n"
3658 " but the container is assigned to Intel(R) "
3659 "%s RAID controller (",
3660 devname,
3661 hba_name->path,
3662 hba_name->pci_id ? : "Err!",
3663 get_sys_dev_type(hba_name->type));
3664
3665 while (hba) {
3666 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
3667 if (hba->next)
3668 fprintf(stderr, ", ");
3669 hba = hba->next;
3670 }
3671
3672 fprintf(stderr, ").\n"
3673 " Mixing devices attached to different controllers "
3674 "is not allowed.\n");
3675 }
3676 free_sys_dev(&hba_name);
3677 return 2;
3678 }
3679 super->orom = find_imsm_capability(hba_name->type);
3680 free_sys_dev(&hba_name);
3681 if (!super->orom)
3682 return 3;
3683 return 0;
3684 }
3685
3686 /* find_missing - helper routine for load_super_imsm_all that identifies
3687 * disks that have disappeared from the system. This routine relies on
3688 * the mpb being uptodate, which it is at load time.
3689 */
3690 static int find_missing(struct intel_super *super)
3691 {
3692 int i;
3693 struct imsm_super *mpb = super->anchor;
3694 struct dl *dl;
3695 struct imsm_disk *disk;
3696
3697 for (i = 0; i < mpb->num_disks; i++) {
3698 disk = __get_imsm_disk(mpb, i);
3699 dl = serial_to_dl(disk->serial, super);
3700 if (dl)
3701 continue;
3702
3703 dl = malloc(sizeof(*dl));
3704 if (!dl)
3705 return 1;
3706 dl->major = 0;
3707 dl->minor = 0;
3708 dl->fd = -1;
3709 dl->devname = strdup("missing");
3710 dl->index = i;
3711 serialcpy(dl->serial, disk->serial);
3712 dl->disk = *disk;
3713 dl->e = NULL;
3714 dl->next = super->missing;
3715 super->missing = dl;
3716 }
3717
3718 return 0;
3719 }
3720
3721 #ifndef MDASSEMBLE
3722 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
3723 {
3724 struct intel_disk *idisk = disk_list;
3725
3726 while (idisk) {
3727 if (serialcmp(idisk->disk.serial, serial) == 0)
3728 break;
3729 idisk = idisk->next;
3730 }
3731
3732 return idisk;
3733 }
3734
3735 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
3736 struct intel_super *super,
3737 struct intel_disk **disk_list)
3738 {
3739 struct imsm_disk *d = &super->disks->disk;
3740 struct imsm_super *mpb = super->anchor;
3741 int i, j;
3742
3743 for (i = 0; i < tbl_size; i++) {
3744 struct imsm_super *tbl_mpb = table[i]->anchor;
3745 struct imsm_disk *tbl_d = &table[i]->disks->disk;
3746
3747 if (tbl_mpb->family_num == mpb->family_num) {
3748 if (tbl_mpb->check_sum == mpb->check_sum) {
3749 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3750 __func__, super->disks->major,
3751 super->disks->minor,
3752 table[i]->disks->major,
3753 table[i]->disks->minor);
3754 break;
3755 }
3756
3757 if (((is_configured(d) && !is_configured(tbl_d)) ||
3758 is_configured(d) == is_configured(tbl_d)) &&
3759 tbl_mpb->generation_num < mpb->generation_num) {
3760 /* current version of the mpb is a
3761 * better candidate than the one in
3762 * super_table, but copy over "cross
3763 * generational" status
3764 */
3765 struct intel_disk *idisk;
3766
3767 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3768 __func__, super->disks->major,
3769 super->disks->minor,
3770 table[i]->disks->major,
3771 table[i]->disks->minor);
3772
3773 idisk = disk_list_get(tbl_d->serial, *disk_list);
3774 if (idisk && is_failed(&idisk->disk))
3775 tbl_d->status |= FAILED_DISK;
3776 break;
3777 } else {
3778 struct intel_disk *idisk;
3779 struct imsm_disk *disk;
3780
3781 /* tbl_mpb is more up to date, but copy
3782 * over cross generational status before
3783 * returning
3784 */
3785 disk = __serial_to_disk(d->serial, mpb, NULL);
3786 if (disk && is_failed(disk))
3787 d->status |= FAILED_DISK;
3788
3789 idisk = disk_list_get(d->serial, *disk_list);
3790 if (idisk) {
3791 idisk->owner = i;
3792 if (disk && is_configured(disk))
3793 idisk->disk.status |= CONFIGURED_DISK;
3794 }
3795
3796 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3797 __func__, super->disks->major,
3798 super->disks->minor,
3799 table[i]->disks->major,
3800 table[i]->disks->minor);
3801
3802 return tbl_size;
3803 }
3804 }
3805 }
3806
3807 if (i >= tbl_size)
3808 table[tbl_size++] = super;
3809 else
3810 table[i] = super;
3811
3812 /* update/extend the merged list of imsm_disk records */
3813 for (j = 0; j < mpb->num_disks; j++) {
3814 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
3815 struct intel_disk *idisk;
3816
3817 idisk = disk_list_get(disk->serial, *disk_list);
3818 if (idisk) {
3819 idisk->disk.status |= disk->status;
3820 if (is_configured(&idisk->disk) ||
3821 is_failed(&idisk->disk))
3822 idisk->disk.status &= ~(SPARE_DISK);
3823 } else {
3824 idisk = calloc(1, sizeof(*idisk));
3825 if (!idisk)
3826 return -1;
3827 idisk->owner = IMSM_UNKNOWN_OWNER;
3828 idisk->disk = *disk;
3829 idisk->next = *disk_list;
3830 *disk_list = idisk;
3831 }
3832
3833 if (serialcmp(idisk->disk.serial, d->serial) == 0)
3834 idisk->owner = i;
3835 }
3836
3837 return tbl_size;
3838 }
3839
3840 static struct intel_super *
3841 validate_members(struct intel_super *super, struct intel_disk *disk_list,
3842 const int owner)
3843 {
3844 struct imsm_super *mpb = super->anchor;
3845 int ok_count = 0;
3846 int i;
3847
3848 for (i = 0; i < mpb->num_disks; i++) {
3849 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
3850 struct intel_disk *idisk;
3851
3852 idisk = disk_list_get(disk->serial, disk_list);
3853 if (idisk) {
3854 if (idisk->owner == owner ||
3855 idisk->owner == IMSM_UNKNOWN_OWNER)
3856 ok_count++;
3857 else
3858 dprintf("%s: '%.16s' owner %d != %d\n",
3859 __func__, disk->serial, idisk->owner,
3860 owner);
3861 } else {
3862 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3863 __func__, __le32_to_cpu(mpb->family_num), i,
3864 disk->serial);
3865 break;
3866 }
3867 }
3868
3869 if (ok_count == mpb->num_disks)
3870 return super;
3871 return NULL;
3872 }
3873
3874 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
3875 {
3876 struct intel_super *s;
3877
3878 for (s = super_list; s; s = s->next) {
3879 if (family_num != s->anchor->family_num)
3880 continue;
3881 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
3882 __le32_to_cpu(family_num), s->disks->devname);
3883 }
3884 }
3885
3886 static struct intel_super *
3887 imsm_thunderdome(struct intel_super **super_list, int len)
3888 {
3889 struct intel_super *super_table[len];
3890 struct intel_disk *disk_list = NULL;
3891 struct intel_super *champion, *spare;
3892 struct intel_super *s, **del;
3893 int tbl_size = 0;
3894 int conflict;
3895 int i;
3896
3897 memset(super_table, 0, sizeof(super_table));
3898 for (s = *super_list; s; s = s->next)
3899 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
3900
3901 for (i = 0; i < tbl_size; i++) {
3902 struct imsm_disk *d;
3903 struct intel_disk *idisk;
3904 struct imsm_super *mpb = super_table[i]->anchor;
3905
3906 s = super_table[i];
3907 d = &s->disks->disk;
3908
3909 /* 'd' must appear in merged disk list for its
3910 * configuration to be valid
3911 */
3912 idisk = disk_list_get(d->serial, disk_list);
3913 if (idisk && idisk->owner == i)
3914 s = validate_members(s, disk_list, i);
3915 else
3916 s = NULL;
3917
3918 if (!s)
3919 dprintf("%s: marking family: %#x from %d:%d offline\n",
3920 __func__, mpb->family_num,
3921 super_table[i]->disks->major,
3922 super_table[i]->disks->minor);
3923 super_table[i] = s;
3924 }
3925
3926 /* This is where the mdadm implementation differs from the Windows
3927 * driver which has no strict concept of a container. We can only
3928 * assemble one family from a container, so when returning a prodigal
3929 * array member to this system the code will not be able to disambiguate
3930 * the container contents that should be assembled ("foreign" versus
3931 * "local"). It requires user intervention to set the orig_family_num
3932 * to a new value to establish a new container. The Windows driver in
3933 * this situation fixes up the volume name in place and manages the
3934 * foreign array as an independent entity.
3935 */
3936 s = NULL;
3937 spare = NULL;
3938 conflict = 0;
3939 for (i = 0; i < tbl_size; i++) {
3940 struct intel_super *tbl_ent = super_table[i];
3941 int is_spare = 0;
3942
3943 if (!tbl_ent)
3944 continue;
3945
3946 if (tbl_ent->anchor->num_raid_devs == 0) {
3947 spare = tbl_ent;
3948 is_spare = 1;
3949 }
3950
3951 if (s && !is_spare) {
3952 show_conflicts(tbl_ent->anchor->family_num, *super_list);
3953 conflict++;
3954 } else if (!s && !is_spare)
3955 s = tbl_ent;
3956 }
3957
3958 if (!s)
3959 s = spare;
3960 if (!s) {
3961 champion = NULL;
3962 goto out;
3963 }
3964 champion = s;
3965
3966 if (conflict)
3967 fprintf(stderr, "Chose family %#x on '%s', "
3968 "assemble conflicts to new container with '--update=uuid'\n",
3969 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
3970
3971 /* collect all dl's onto 'champion', and update them to
3972 * champion's version of the status
3973 */
3974 for (s = *super_list; s; s = s->next) {
3975 struct imsm_super *mpb = champion->anchor;
3976 struct dl *dl = s->disks;
3977
3978 if (s == champion)
3979 continue;
3980
3981 for (i = 0; i < mpb->num_disks; i++) {
3982 struct imsm_disk *disk;
3983
3984 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
3985 if (disk) {
3986 dl->disk = *disk;
3987 /* only set index on disks that are a member of
3988 * a populated contianer, i.e. one with
3989 * raid_devs
3990 */
3991 if (is_failed(&dl->disk))
3992 dl->index = -2;
3993 else if (is_spare(&dl->disk))
3994 dl->index = -1;
3995 break;
3996 }
3997 }
3998
3999 if (i >= mpb->num_disks) {
4000 struct intel_disk *idisk;
4001
4002 idisk = disk_list_get(dl->serial, disk_list);
4003 if (idisk && is_spare(&idisk->disk) &&
4004 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
4005 dl->index = -1;
4006 else {
4007 dl->index = -2;
4008 continue;
4009 }
4010 }
4011
4012 dl->next = champion->disks;
4013 champion->disks = dl;
4014 s->disks = NULL;
4015 }
4016
4017 /* delete 'champion' from super_list */
4018 for (del = super_list; *del; ) {
4019 if (*del == champion) {
4020 *del = (*del)->next;
4021 break;
4022 } else
4023 del = &(*del)->next;
4024 }
4025 champion->next = NULL;
4026
4027 out:
4028 while (disk_list) {
4029 struct intel_disk *idisk = disk_list;
4030
4031 disk_list = disk_list->next;
4032 free(idisk);
4033 }
4034
4035 return champion;
4036 }
4037
4038
4039 static int
4040 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd);
4041 static int get_super_block(struct intel_super **super_list, int devnum, char *devname,
4042 int major, int minor, int keep_fd);
4043 static int
4044 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
4045 int *max, int keep_fd);
4046
4047
4048 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
4049 char *devname, struct md_list *devlist,
4050 int keep_fd)
4051 {
4052 struct intel_super *super_list = NULL;
4053 struct intel_super *super = NULL;
4054 int err = 0;
4055 int i = 0;
4056
4057 if (fd >= 0)
4058 /* 'fd' is an opened container */
4059 err = get_sra_super_block(fd, &super_list, devname, &i, keep_fd);
4060 else
4061 /* get super block from devlist devices */
4062 err = get_devlist_super_block(devlist, &super_list, &i, keep_fd);
4063 if (err)
4064 goto error;
4065 /* all mpbs enter, maybe one leaves */
4066 super = imsm_thunderdome(&super_list, i);
4067 if (!super) {
4068 err = 1;
4069 goto error;
4070 }
4071
4072 if (find_missing(super) != 0) {
4073 free_imsm(super);
4074 err = 2;
4075 goto error;
4076 }
4077
4078 /* load migration record */
4079 err = load_imsm_migr_rec(super, NULL);
4080 if (err == -1) {
4081 /* migration is in progress,
4082 * but migr_rec cannot be loaded,
4083 */
4084 err = 4;
4085 goto error;
4086 }
4087
4088 /* Check migration compatibility */
4089 if ((err == 0) && (check_mpb_migr_compatibility(super) != 0)) {
4090 fprintf(stderr, Name ": Unsupported migration detected");
4091 if (devname)
4092 fprintf(stderr, " on %s\n", devname);
4093 else
4094 fprintf(stderr, " (IMSM).\n");
4095
4096 err = 5;
4097 goto error;
4098 }
4099
4100 err = 0;
4101
4102 error:
4103 while (super_list) {
4104 struct intel_super *s = super_list;
4105
4106 super_list = super_list->next;
4107 free_imsm(s);
4108 }
4109
4110
4111 if (err)
4112 return err;
4113
4114 *sbp = super;
4115 if (fd >= 0)
4116 st->container_dev = fd2devnum(fd);
4117 else
4118 st->container_dev = NoMdDev;
4119 if (err == 0 && st->ss == NULL) {
4120 st->ss = &super_imsm;
4121 st->minor_version = 0;
4122 st->max_devs = IMSM_MAX_DEVICES;
4123 }
4124 return 0;
4125 }
4126
4127
4128 static int
4129 get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list,
4130 int *max, int keep_fd)
4131 {
4132 struct md_list *tmpdev;
4133 int err = 0;
4134 int i = 0;
4135
4136 for (i = 0, tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
4137 if (tmpdev->used != 1)
4138 continue;
4139 if (tmpdev->container == 1) {
4140 int lmax = 0;
4141 int fd = dev_open(tmpdev->devname, O_RDONLY|O_EXCL);
4142 if (fd < 0) {
4143 fprintf(stderr, Name ": cannot open device %s: %s\n",
4144 tmpdev->devname, strerror(errno));
4145 err = 8;
4146 goto error;
4147 }
4148 err = get_sra_super_block(fd, super_list,
4149 tmpdev->devname, &lmax,
4150 keep_fd);
4151 i += lmax;
4152 close(fd);
4153 if (err) {
4154 err = 7;
4155 goto error;
4156 }
4157 } else {
4158 int major = major(tmpdev->st_rdev);
4159 int minor = minor(tmpdev->st_rdev);
4160 err = get_super_block(super_list,
4161 -1,
4162 tmpdev->devname,
4163 major, minor,
4164 keep_fd);
4165 i++;
4166 if (err) {
4167 err = 6;
4168 goto error;
4169 }
4170 }
4171 }
4172 error:
4173 *max = i;
4174 return err;
4175 }
4176
4177 static int get_super_block(struct intel_super **super_list, int devnum, char *devname,
4178 int major, int minor, int keep_fd)
4179 {
4180 struct intel_super*s = NULL;
4181 char nm[32];
4182 int dfd = -1;
4183 int rv;
4184 int err = 0;
4185 int retry;
4186
4187 s = alloc_super();
4188 if (!s) {
4189 err = 1;
4190 goto error;
4191 }
4192
4193 sprintf(nm, "%d:%d", major, minor);
4194 dfd = dev_open(nm, O_RDWR);
4195 if (dfd < 0) {
4196 err = 2;
4197 goto error;
4198 }
4199
4200 rv = find_intel_hba_capability(dfd, s, devname);
4201 /* no orom/efi or non-intel hba of the disk */
4202 if (rv != 0) {
4203 err = 4;
4204 goto error;
4205 }
4206
4207 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
4208
4209 /* retry the load if we might have raced against mdmon */
4210 if (err == 3 && (devnum != -1) && mdmon_running(devnum))
4211 for (retry = 0; retry < 3; retry++) {
4212 usleep(3000);
4213 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
4214 if (err != 3)
4215 break;
4216 }
4217 error:
4218 if (!err) {
4219 s->next = *super_list;
4220 *super_list = s;
4221 } else {
4222 if (s)
4223 free(s);
4224 if (dfd)
4225 close(dfd);
4226 }
4227 if ((dfd >= 0) && (!keep_fd))
4228 close(dfd);
4229 return err;
4230
4231 }
4232
4233 static int
4234 get_sra_super_block(int fd, struct intel_super **super_list, char *devname, int *max, int keep_fd)
4235 {
4236 struct mdinfo *sra;
4237 int devnum;
4238 struct mdinfo *sd;
4239 int err = 0;
4240 int i = 0;
4241 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
4242 if (!sra)
4243 return 1;
4244
4245 if (sra->array.major_version != -1 ||
4246 sra->array.minor_version != -2 ||
4247 strcmp(sra->text_version, "imsm") != 0) {
4248 err = 1;
4249 goto error;
4250 }
4251 /* load all mpbs */
4252 devnum = fd2devnum(fd);
4253 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
4254 if (get_super_block(super_list, devnum, devname,
4255 sd->disk.major, sd->disk.minor, keep_fd) != 0) {
4256 err = 7;
4257 goto error;
4258 }
4259 }
4260 error:
4261 sysfs_free(sra);
4262 *max = i;
4263 return err;
4264 }
4265
4266 static int load_container_imsm(struct supertype *st, int fd, char *devname)
4267 {
4268 return load_super_imsm_all(st, fd, &st->sb, devname, NULL, 1);
4269 }
4270 #endif
4271
4272 static int load_super_imsm(struct supertype *st, int fd, char *devname)
4273 {
4274 struct intel_super *super;
4275 int rv;
4276
4277 if (test_partition(fd))
4278 /* IMSM not allowed on partitions */
4279 return 1;
4280
4281 free_super_imsm(st);
4282
4283 super = alloc_super();
4284 if (!super) {
4285 fprintf(stderr,
4286 Name ": malloc of %zu failed.\n",
4287 sizeof(*super));
4288 return 1;
4289 }
4290 /* Load hba and capabilities if they exist.
4291 * But do not preclude loading metadata in case capabilities or hba are
4292 * non-compliant and ignore_hw_compat is set.
4293 */
4294 rv = find_intel_hba_capability(fd, super, devname);
4295 /* no orom/efi or non-intel hba of the disk */
4296 if ((rv != 0) && (st->ignore_hw_compat == 0)) {
4297 if (devname)
4298 fprintf(stderr,
4299 Name ": No OROM/EFI properties for %s\n", devname);
4300 free_imsm(super);
4301 return 2;
4302 }
4303 rv = load_and_parse_mpb(fd, super, devname, 0);
4304
4305 if (rv) {
4306 if (devname)
4307 fprintf(stderr,
4308 Name ": Failed to load all information "
4309 "sections on %s\n", devname);
4310 free_imsm(super);
4311 return rv;
4312 }
4313
4314 st->sb = super;
4315 if (st->ss == NULL) {
4316 st->ss = &super_imsm;
4317 st->minor_version = 0;
4318 st->max_devs = IMSM_MAX_DEVICES;
4319 }
4320
4321 /* load migration record */
4322 if (load_imsm_migr_rec(super, NULL) == 0) {
4323 /* Check for unsupported migration features */
4324 if (check_mpb_migr_compatibility(super) != 0) {
4325 fprintf(stderr,
4326 Name ": Unsupported migration detected");
4327 if (devname)
4328 fprintf(stderr, " on %s\n", devname);
4329 else
4330 fprintf(stderr, " (IMSM).\n");
4331 return 3;
4332 }
4333 }
4334
4335 return 0;
4336 }
4337
4338 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
4339 {
4340 if (info->level == 1)
4341 return 128;
4342 return info->chunk_size >> 9;
4343 }
4344
4345 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
4346 {
4347 __u32 num_stripes;
4348
4349 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
4350 num_stripes /= num_domains;
4351
4352 return num_stripes;
4353 }
4354
4355 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
4356 {
4357 if (info->level == 1)
4358 return info->size * 2;
4359 else
4360 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
4361 }
4362
4363 static void imsm_update_version_info(struct intel_super *super)
4364 {
4365 /* update the version and attributes */
4366 struct imsm_super *mpb = super->anchor;
4367 char *version;
4368 struct imsm_dev *dev;
4369 struct imsm_map *map;
4370 int i;
4371
4372 for (i = 0; i < mpb->num_raid_devs; i++) {
4373 dev = get_imsm_dev(super, i);
4374 map = get_imsm_map(dev, MAP_0);
4375 if (__le32_to_cpu(dev->size_high) > 0)
4376 mpb->attributes |= MPB_ATTRIB_2TB;
4377
4378 /* FIXME detect when an array spans a port multiplier */
4379 #if 0
4380 mpb->attributes |= MPB_ATTRIB_PM;
4381 #endif
4382
4383 if (mpb->num_raid_devs > 1 ||
4384 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
4385 version = MPB_VERSION_ATTRIBS;
4386 switch (get_imsm_raid_level(map)) {
4387 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
4388 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
4389 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
4390 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
4391 }
4392 } else {
4393 if (map->num_members >= 5)
4394 version = MPB_VERSION_5OR6_DISK_ARRAY;
4395 else if (dev->status == DEV_CLONE_N_GO)
4396 version = MPB_VERSION_CNG;
4397 else if (get_imsm_raid_level(map) == 5)
4398 version = MPB_VERSION_RAID5;
4399 else if (map->num_members >= 3)
4400 version = MPB_VERSION_3OR4_DISK_ARRAY;
4401 else if (get_imsm_raid_level(map) == 1)
4402 version = MPB_VERSION_RAID1;
4403 else
4404 version = MPB_VERSION_RAID0;
4405 }
4406 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
4407 }
4408 }
4409
4410 static int check_name(struct intel_super *super, char *name, int quiet)
4411 {
4412 struct imsm_super *mpb = super->anchor;
4413 char *reason = NULL;
4414 int i;
4415
4416 if (strlen(name) > MAX_RAID_SERIAL_LEN)
4417 reason = "must be 16 characters or less";
4418
4419 for (i = 0; i < mpb->num_raid_devs; i++) {
4420 struct imsm_dev *dev = get_imsm_dev(super, i);
4421
4422 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
4423 reason = "already exists";
4424 break;
4425 }
4426 }
4427
4428 if (reason && !quiet)
4429 fprintf(stderr, Name ": imsm volume name %s\n", reason);
4430
4431 return !reason;
4432 }
4433
4434 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
4435 unsigned long long size, char *name,
4436 char *homehost, int *uuid)
4437 {
4438 /* We are creating a volume inside a pre-existing container.
4439 * so st->sb is already set.
4440 */
4441 struct intel_super *super = st->sb;
4442 struct imsm_super *mpb = super->anchor;
4443 struct intel_dev *dv;
4444 struct imsm_dev *dev;
4445 struct imsm_vol *vol;
4446 struct imsm_map *map;
4447 int idx = mpb->num_raid_devs;
4448 int i;
4449 unsigned long long array_blocks;
4450 size_t size_old, size_new;
4451 __u32 num_data_stripes;
4452
4453 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
4454 fprintf(stderr, Name": This imsm-container already has the "
4455 "maximum of %d volumes\n", super->orom->vpa);
4456 return 0;
4457 }
4458
4459 /* ensure the mpb is large enough for the new data */
4460 size_old = __le32_to_cpu(mpb->mpb_size);
4461 size_new = disks_to_mpb_size(info->nr_disks);
4462 if (size_new > size_old) {
4463 void *mpb_new;
4464 size_t size_round = ROUND_UP(size_new, 512);
4465
4466 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
4467 fprintf(stderr, Name": could not allocate new mpb\n");
4468 return 0;
4469 }
4470 if (posix_memalign(&super->migr_rec_buf, 512,
4471 MIGR_REC_BUF_SIZE) != 0) {
4472 fprintf(stderr, Name
4473 ": %s could not allocate migr_rec buffer\n",
4474 __func__);
4475 free(super->buf);
4476 free(super);
4477 free(mpb_new);
4478 return 0;
4479 }
4480 memcpy(mpb_new, mpb, size_old);
4481 free(mpb);
4482 mpb = mpb_new;
4483 super->anchor = mpb_new;
4484 mpb->mpb_size = __cpu_to_le32(size_new);
4485 memset(mpb_new + size_old, 0, size_round - size_old);
4486 }
4487 super->current_vol = idx;
4488
4489 /* handle 'failed_disks' by either:
4490 * a) create dummy disk entries in the table if this the first
4491 * volume in the array. We add them here as this is the only
4492 * opportunity to add them. add_to_super_imsm_volume()
4493 * handles the non-failed disks and continues incrementing
4494 * mpb->num_disks.
4495 * b) validate that 'failed_disks' matches the current number
4496 * of missing disks if the container is populated
4497 */
4498 if (super->current_vol == 0) {
4499 mpb->num_disks = 0;
4500 for (i = 0; i < info->failed_disks; i++) {
4501 struct imsm_disk *disk;
4502
4503 mpb->num_disks++;
4504 disk = __get_imsm_disk(mpb, i);
4505 disk->status = CONFIGURED_DISK | FAILED_DISK;
4506 disk->scsi_id = __cpu_to_le32(~(__u32)0);
4507 snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
4508 "missing:%d", i);
4509 }
4510 find_missing(super);
4511 } else {
4512 int missing = 0;
4513 struct dl *d;
4514
4515 for (d = super->missing; d; d = d->next)
4516 missing++;
4517 if (info->failed_disks > missing) {
4518 fprintf(stderr, Name": unable to add 'missing' disk to container\n");
4519 return 0;
4520 }
4521 }
4522
4523 if (!check_name(super, name, 0))
4524 return 0;
4525 dv = malloc(sizeof(*dv));
4526 if (!dv) {
4527 fprintf(stderr, Name ": failed to allocate device list entry\n");
4528 return 0;
4529 }
4530 dev = calloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
4531 if (!dev) {
4532 free(dv);
4533 fprintf(stderr, Name": could not allocate raid device\n");
4534 return 0;
4535 }
4536
4537 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
4538 if (info->level == 1)
4539 array_blocks = info_to_blocks_per_member(info);
4540 else
4541 array_blocks = calc_array_size(info->level, info->raid_disks,
4542 info->layout, info->chunk_size,
4543 info->size*2);
4544 /* round array size down to closest MB */
4545 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
4546
4547 dev->size_low = __cpu_to_le32((__u32) array_blocks);
4548 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
4549 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
4550 vol = &dev->vol;
4551 vol->migr_state = 0;
4552 set_migr_type(dev, MIGR_INIT);
4553 vol->dirty = !info->state;
4554 vol->curr_migr_unit = 0;
4555 map = get_imsm_map(dev, MAP_0);
4556 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
4557 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
4558 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
4559 map->failed_disk_num = ~0;
4560 if (info->level > 0)
4561 map->map_state = IMSM_T_STATE_UNINITIALIZED;
4562 else
4563 map->map_state = info->failed_disks ? IMSM_T_STATE_FAILED :
4564 IMSM_T_STATE_NORMAL;
4565 map->ddf = 1;
4566
4567 if (info->level == 1 && info->raid_disks > 2) {
4568 free(dev);
4569 free(dv);
4570 fprintf(stderr, Name": imsm does not support more than 2 disks"
4571 "in a raid1 volume\n");
4572 return 0;
4573 }
4574
4575 map->raid_level = info->level;
4576 if (info->level == 10) {
4577 map->raid_level = 1;
4578 map->num_domains = info->raid_disks / 2;
4579 } else if (info->level == 1)
4580 map->num_domains = info->raid_disks;
4581 else
4582 map->num_domains = 1;
4583
4584 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
4585 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
4586
4587 map->num_members = info->raid_disks;
4588 for (i = 0; i < map->num_members; i++) {
4589 /* initialized in add_to_super */
4590 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
4591 }
4592 mpb->num_raid_devs++;
4593
4594 dv->dev = dev;
4595 dv->index = super->current_vol;
4596 dv->next = super->devlist;
4597 super->devlist = dv;
4598
4599 imsm_update_version_info(super);
4600
4601 return 1;
4602 }
4603
4604 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
4605 unsigned long long size, char *name,
4606 char *homehost, int *uuid)
4607 {
4608 /* This is primarily called by Create when creating a new array.
4609 * We will then get add_to_super called for each component, and then
4610 * write_init_super called to write it out to each device.
4611 * For IMSM, Create can create on fresh devices or on a pre-existing
4612 * array.
4613 * To create on a pre-existing array a different method will be called.
4614 * This one is just for fresh drives.
4615 */
4616 struct intel_super *super;
4617 struct imsm_super *mpb;
4618 size_t mpb_size;
4619 char *version;
4620
4621 if (st->sb)
4622 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
4623
4624 if (info)
4625 mpb_size = disks_to_mpb_size(info->nr_disks);
4626 else
4627 mpb_size = 512;
4628
4629 super = alloc_super();
4630 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
4631 free(super);
4632 super = NULL;
4633 }
4634 if (!super) {
4635 fprintf(stderr, Name
4636 ": %s could not allocate superblock\n", __func__);
4637 return 0;
4638 }
4639 if (posix_memalign(&super->migr_rec_buf, 512, MIGR_REC_BUF_SIZE) != 0) {
4640 fprintf(stderr, Name
4641 ": %s could not allocate migr_rec buffer\n", __func__);
4642 free(super->buf);
4643 free(super);
4644 return 0;
4645 }
4646 memset(super->buf, 0, mpb_size);
4647 mpb = super->buf;
4648 mpb->mpb_size = __cpu_to_le32(mpb_size);
4649 st->sb = super;
4650
4651 if (info == NULL) {
4652 /* zeroing superblock */
4653 return 0;
4654 }
4655
4656 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
4657
4658 version = (char *) mpb->sig;
4659 strcpy(version, MPB_SIGNATURE);
4660 version += strlen(MPB_SIGNATURE);
4661 strcpy(version, MPB_VERSION_RAID0);
4662
4663 return 1;
4664 }
4665
4666 #ifndef MDASSEMBLE
4667 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
4668 int fd, char *devname)
4669 {
4670 struct intel_super *super = st->sb;
4671 struct imsm_super *mpb = super->anchor;
4672 struct imsm_disk *_disk;
4673 struct imsm_dev *dev;
4674 struct imsm_map *map;
4675 struct dl *dl, *df;
4676 int slot;
4677
4678 dev = get_imsm_dev(super, super->current_vol);
4679 map = get_imsm_map(dev, MAP_0);
4680
4681 if (! (dk->state & (1<<MD_DISK_SYNC))) {
4682 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
4683 devname);
4684 return 1;
4685 }
4686
4687 if (fd == -1) {
4688 /* we're doing autolayout so grab the pre-marked (in
4689 * validate_geometry) raid_disk
4690 */
4691 for (dl = super->disks; dl; dl = dl->next)
4692 if (dl->raiddisk == dk->raid_disk)
4693 break;
4694 } else {
4695 for (dl = super->disks; dl ; dl = dl->next)
4696 if (dl->major == dk->major &&
4697 dl->minor == dk->minor)
4698 break;
4699 }
4700
4701 if (!dl) {
4702 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
4703 return 1;
4704 }
4705
4706 /* add a pristine spare to the metadata */
4707 if (dl->index < 0) {
4708 dl->index = super->anchor->num_disks;
4709 super->anchor->num_disks++;
4710 }
4711 /* Check the device has not already been added */
4712 slot = get_imsm_disk_slot(map, dl->index);
4713 if (slot >= 0 &&
4714 (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
4715 fprintf(stderr, Name ": %s has been included in this array twice\n",
4716 devname);
4717 return 1;
4718 }
4719 set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
4720 dl->disk.status = CONFIGURED_DISK;
4721
4722 /* update size of 'missing' disks to be at least as large as the
4723 * largest acitve member (we only have dummy missing disks when
4724 * creating the first volume)
4725 */
4726 if (super->current_vol == 0) {
4727 for (df = super->missing; df; df = df->next) {
4728 if (dl->disk.total_blocks > df->disk.total_blocks)
4729 df->disk.total_blocks = dl->disk.total_blocks;
4730 _disk = __get_imsm_disk(mpb, df->index);
4731 *_disk = df->disk;
4732 }
4733 }
4734
4735 /* refresh unset/failed slots to point to valid 'missing' entries */
4736 for (df = super->missing; df; df = df->next)
4737 for (slot = 0; slot < mpb->num_disks; slot++) {
4738 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
4739
4740 if ((ord & IMSM_ORD_REBUILD) == 0)
4741 continue;
4742 set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
4743 if (is_gen_migration(dev)) {
4744 struct imsm_map *map2 = get_imsm_map(dev,
4745 MAP_1);
4746 int slot2 = get_imsm_disk_slot(map2, df->index);
4747 if ((slot2 < map2->num_members) &&
4748 (slot2 >= 0)) {
4749 __u32 ord2 = get_imsm_ord_tbl_ent(dev,
4750 slot2,
4751 MAP_1);
4752 if ((unsigned)df->index ==
4753 ord_to_idx(ord2))
4754 set_imsm_ord_tbl_ent(map2,
4755 slot2,
4756 df->index |
4757 IMSM_ORD_REBUILD);
4758 }
4759 }
4760 dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
4761 break;
4762 }
4763
4764 /* if we are creating the first raid device update the family number */
4765 if (super->current_vol == 0) {
4766 __u32 sum;
4767 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
4768
4769 _disk = __get_imsm_disk(mpb, dl->index);
4770 if (!_dev || !_disk) {
4771 fprintf(stderr, Name ": BUG mpb setup error\n");
4772 return 1;
4773 }
4774 *_dev = *dev;
4775 *_disk = dl->disk;
4776 sum = random32();
4777 sum += __gen_imsm_checksum(mpb);
4778 mpb->family_num = __cpu_to_le32(sum);
4779 mpb->orig_family_num = mpb->family_num;
4780 }
4781 super->current_disk = dl;
4782 return 0;
4783 }
4784
4785 /* mark_spare()
4786 * Function marks disk as spare and restores disk serial
4787 * in case it was previously marked as failed by takeover operation
4788 * reruns:
4789 * -1 : critical error
4790 * 0 : disk is marked as spare but serial is not set
4791 * 1 : success
4792 */
4793 int mark_spare(struct dl *disk)
4794 {
4795 __u8 serial[MAX_RAID_SERIAL_LEN];
4796 int ret_val = -1;
4797
4798 if (!disk)
4799 return ret_val;
4800
4801 ret_val = 0;
4802 if (!imsm_read_serial(disk->fd, NULL, serial)) {
4803 /* Restore disk serial number, because takeover marks disk
4804 * as failed and adds to serial ':0' before it becomes
4805 * a spare disk.
4806 */
4807 serialcpy(disk->serial, serial);
4808 serialcpy(disk->disk.serial, serial);
4809 ret_val = 1;
4810 }
4811 disk->disk.status = SPARE_DISK;
4812 disk->index = -1;
4813
4814 return ret_val;
4815 }
4816
4817 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
4818 int fd, char *devname)
4819 {
4820 struct intel_super *super = st->sb;
4821 struct dl *dd;
4822 unsigned long long size;
4823 __u32 id;
4824 int rv;
4825 struct stat stb;
4826
4827 /* If we are on an RAID enabled platform check that the disk is
4828 * attached to the raid controller.
4829 * We do not need to test disks attachment for container based additions,
4830 * they shall be already tested when container was created/assembled.
4831 */
4832 rv = find_intel_hba_capability(fd, super, devname);
4833 /* no orom/efi or non-intel hba of the disk */
4834 if (rv != 0) {
4835 dprintf("capability: %p fd: %d ret: %d\n",
4836 super->orom, fd, rv);
4837 return 1;
4838 }
4839
4840 if (super->current_vol >= 0)
4841 return add_to_super_imsm_volume(st, dk, fd, devname);
4842
4843 fstat(fd, &stb);
4844 dd = malloc(sizeof(*dd));
4845 if (!dd) {
4846 fprintf(stderr,
4847 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
4848 return 1;
4849 }
4850 memset(dd, 0, sizeof(*dd));
4851 dd->major = major(stb.st_rdev);
4852 dd->minor = minor(stb.st_rdev);
4853 dd->devname = devname ? strdup(devname) : NULL;
4854 dd->fd = fd;
4855 dd->e = NULL;
4856 dd->action = DISK_ADD;
4857 rv = imsm_read_serial(fd, devname, dd->serial);
4858 if (rv) {
4859 fprintf(stderr,
4860 Name ": failed to retrieve scsi serial, aborting\n");
4861 free(dd);
4862 abort();
4863 }
4864
4865 get_dev_size(fd, NULL, &size);
4866 size /= 512;
4867 serialcpy(dd->disk.serial, dd->serial);
4868 dd->disk.total_blocks = __cpu_to_le32(size);
4869 mark_spare(dd);
4870 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
4871 dd->disk.scsi_id = __cpu_to_le32(id);
4872 else
4873 dd->disk.scsi_id = __cpu_to_le32(0);
4874
4875 if (st->update_tail) {
4876 dd->next = super->disk_mgmt_list;
4877 super->disk_mgmt_list = dd;
4878 } else {
4879 dd->next = super->disks;
4880 super->disks = dd;
4881 super->updates_pending++;
4882 }
4883
4884 return 0;
4885 }
4886
4887
4888 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
4889 {
4890 struct intel_super *super = st->sb;
4891 struct dl *dd;
4892
4893 /* remove from super works only in mdmon - for communication
4894 * manager - monitor. Check if communication memory buffer
4895 * is prepared.
4896 */
4897 if (!st->update_tail) {
4898 fprintf(stderr,
4899 Name ": %s shall be used in mdmon context only"
4900 "(line %d).\n", __func__, __LINE__);
4901 return 1;
4902 }
4903 dd = malloc(sizeof(*dd));
4904 if (!dd) {
4905 fprintf(stderr,
4906 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
4907 return 1;
4908 }
4909 memset(dd, 0, sizeof(*dd));
4910 dd->major = dk->major;
4911 dd->minor = dk->minor;
4912 dd->fd = -1;
4913 mark_spare(dd);
4914 dd->action = DISK_REMOVE;
4915
4916 dd->next = super->disk_mgmt_list;
4917 super->disk_mgmt_list = dd;
4918
4919
4920 return 0;
4921 }
4922
4923 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
4924
4925 static union {
4926 char buf[512];
4927 struct imsm_super anchor;
4928 } spare_record __attribute__ ((aligned(512)));
4929
4930 /* spare records have their own family number and do not have any defined raid
4931 * devices
4932 */
4933 static int write_super_imsm_spares(struct intel_super *super, int doclose)
4934 {
4935 struct imsm_super *mpb = super->anchor;
4936 struct imsm_super *spare = &spare_record.anchor;
4937 __u32 sum;
4938 struct dl *d;
4939
4940 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
4941 spare->generation_num = __cpu_to_le32(1UL),
4942 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
4943 spare->num_disks = 1,
4944 spare->num_raid_devs = 0,
4945 spare->cache_size = mpb->cache_size,
4946 spare->pwr_cycle_count = __cpu_to_le32(1),
4947
4948 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
4949 MPB_SIGNATURE MPB_VERSION_RAID0);
4950
4951 for (d = super->disks; d; d = d->next) {
4952 if (d->index != -1)
4953 continue;
4954
4955 spare->disk[0] = d->disk;
4956 sum = __gen_imsm_checksum(spare);
4957 spare->family_num = __cpu_to_le32(sum);
4958 spare->orig_family_num = 0;
4959 sum = __gen_imsm_checksum(spare);
4960 spare->check_sum = __cpu_to_le32(sum);
4961
4962 if (store_imsm_mpb(d->fd, spare)) {
4963 fprintf(stderr, "%s: failed for device %d:%d %s\n",
4964 __func__, d->major, d->minor, strerror(errno));
4965 return 1;
4966 }
4967 if (doclose) {
4968 close(d->fd);
4969 d->fd = -1;
4970 }
4971 }
4972
4973 return 0;
4974 }
4975
4976 static int write_super_imsm(struct supertype *st, int doclose)
4977 {
4978 struct intel_super *super = st->sb;
4979 struct imsm_super *mpb = super->anchor;
4980 struct dl *d;
4981 __u32 generation;
4982 __u32 sum;
4983 int spares = 0;
4984 int i;
4985 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
4986 int num_disks = 0;
4987 int clear_migration_record = 1;
4988
4989 /* 'generation' is incremented everytime the metadata is written */
4990 generation = __le32_to_cpu(mpb->generation_num);
4991 generation++;
4992 mpb->generation_num = __cpu_to_le32(generation);
4993
4994 /* fix up cases where previous mdadm releases failed to set
4995 * orig_family_num
4996 */
4997 if (mpb->orig_family_num == 0)
4998 mpb->orig_family_num = mpb->family_num;
4999
5000 for (d = super->disks; d; d = d->next) {
5001 if (d->index == -1)
5002 spares++;
5003 else {
5004 mpb->disk[d->index] = d->disk;
5005 num_disks++;
5006 }
5007 }
5008 for (d = super->missing; d; d = d->next) {
5009 mpb->disk[d->index] = d->disk;
5010 num_disks++;
5011 }
5012 mpb->num_disks = num_disks;
5013 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
5014
5015 for (i = 0; i < mpb->num_raid_devs; i++) {
5016 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
5017 struct imsm_dev *dev2 = get_imsm_dev(super, i);
5018 if (dev && dev2) {
5019 imsm_copy_dev(dev, dev2);
5020 mpb_size += sizeof_imsm_dev(dev, 0);
5021 }
5022 if (is_gen_migration(dev2))
5023 clear_migration_record = 0;
5024 }
5025 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
5026 mpb->mpb_size = __cpu_to_le32(mpb_size);
5027
5028 /* recalculate checksum */
5029 sum = __gen_imsm_checksum(mpb);
5030 mpb->check_sum = __cpu_to_le32(sum);
5031
5032 if (clear_migration_record)
5033 memset(super->migr_rec_buf, 0, MIGR_REC_BUF_SIZE);
5034
5035 /* write the mpb for disks that compose raid devices */
5036 for (d = super->disks; d ; d = d->next) {
5037 if (d->index < 0 || is_failed(&d->disk))
5038 continue;
5039 if (store_imsm_mpb(d->fd, mpb))
5040 fprintf(stderr, "%s: failed for device %d:%d (fd: %d)%s\n",
5041 __func__, d->major, d->minor, d->fd, strerror(errno));
5042
5043 if (clear_migration_record) {
5044 unsigned long long dsize;
5045
5046 get_dev_size(d->fd, NULL, &dsize);
5047 if (lseek64(d->fd, dsize - 512, SEEK_SET) >= 0) {
5048 if (write(d->fd, super->migr_rec_buf,
5049 MIGR_REC_BUF_SIZE) != MIGR_REC_BUF_SIZE)
5050 perror("Write migr_rec failed");
5051 }
5052 }
5053 if (doclose) {
5054 close(d->fd);
5055 d->fd = -1;
5056 }
5057 }
5058
5059 if (spares)
5060 return write_super_imsm_spares(super, doclose);
5061
5062 return 0;
5063 }
5064
5065
5066 static int create_array(struct supertype *st, int dev_idx)
5067 {
5068 size_t len;
5069 struct imsm_update_create_array *u;
5070 struct intel_super *super = st->sb;
5071 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
5072 struct imsm_map *map = get_imsm_map(dev, MAP_0);
5073 struct disk_info *inf;
5074 struct imsm_disk *disk;
5075 int i;
5076
5077 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
5078 sizeof(*inf) * map->num_members;
5079 u = malloc(len);
5080 if (!u) {
5081 fprintf(stderr, "%s: failed to allocate update buffer\n",
5082 __func__);
5083 return 1;
5084 }
5085
5086 u->type = update_create_array;
5087 u->dev_idx = dev_idx;
5088 imsm_copy_dev(&u->dev, dev);
5089 inf = get_disk_info(u);
5090 for (i = 0; i < map->num_members; i++) {
5091 int idx = get_imsm_disk_idx(dev, i, MAP_X);
5092
5093 disk = get_imsm_disk(super, idx);
5094 serialcpy(inf[i].serial, disk->serial);
5095 }
5096 append_metadata_update(st, u, len);
5097
5098 return 0;
5099 }
5100
5101 static int mgmt_disk(struct supertype *st)
5102 {
5103 struct intel_super *super = st->sb;
5104 size_t len;
5105 struct imsm_update_add_remove_disk *u;
5106
5107 if (!super->disk_mgmt_list)
5108 return 0;
5109
5110 len = sizeof(*u);
5111 u = malloc(len);
5112 if (!u) {
5113 fprintf(stderr, "%s: failed to allocate update buffer\n",
5114 __func__);
5115 return 1;
5116 }
5117
5118 u->type = update_add_remove_disk;
5119 append_metadata_update(st, u, len);
5120
5121 return 0;
5122 }
5123
5124 static int write_init_super_imsm(struct supertype *st)
5125 {
5126 struct intel_super *super = st->sb;
5127 int current_vol = super->current_vol;
5128
5129 /* we are done with current_vol reset it to point st at the container */
5130 super->current_vol = -1;
5131
5132 if (st->update_tail) {
5133 /* queue the recently created array / added disk
5134 * as a metadata update */
5135 int rv;
5136
5137 /* determine if we are creating a volume or adding a disk */
5138 if (current_vol < 0) {
5139 /* in the mgmt (add/remove) disk case we are running
5140 * in mdmon context, so don't close fd's
5141 */
5142 return mgmt_disk(st);
5143 } else
5144 rv = create_array(st, current_vol);
5145
5146 return rv;
5147 } else {
5148 struct dl *d;
5149 for (d = super->disks; d; d = d->next)
5150 Kill(d->devname, NULL, 0, 1, 1);
5151 return write_super_imsm(st, 1);
5152 }
5153 }
5154 #endif
5155
5156 static int store_super_imsm(struct supertype *st, int fd)
5157 {
5158 struct intel_super *super = st->sb;
5159 struct imsm_super *mpb = super ? super->anchor : NULL;
5160
5161 if (!mpb)
5162 return 1;
5163
5164 #ifndef MDASSEMBLE
5165 return store_imsm_mpb(fd, mpb);
5166 #else
5167 return 1;
5168 #endif
5169 }
5170
5171 static int imsm_bbm_log_size(struct imsm_super *mpb)
5172 {
5173 return __le32_to_cpu(mpb->bbm_log_size);
5174 }
5175
5176 #ifndef MDASSEMBLE
5177 static int validate_geometry_imsm_container(struct supertype *st, int level,
5178 int layout, int raiddisks, int chunk,
5179 unsigned long long size, char *dev,
5180 unsigned long long *freesize,
5181 int verbose)
5182 {
5183 int fd;
5184 unsigned long long ldsize;
5185 struct intel_super *super=NULL;
5186 int rv = 0;
5187
5188 if (level != LEVEL_CONTAINER)
5189 return 0;
5190 if (!dev)
5191 return 1;
5192
5193 fd = open(dev, O_RDONLY|O_EXCL, 0);
5194 if (fd < 0) {
5195 if (verbose)
5196 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
5197 dev, strerror(errno));
5198 return 0;
5199 }
5200 if (!get_dev_size(fd, dev, &ldsize)) {
5201 close(fd);
5202 return 0;
5203 }
5204
5205 /* capabilities retrieve could be possible
5206 * note that there is no fd for the disks in array.
5207 */
5208 super = alloc_super();
5209 if (!super) {
5210 fprintf(stderr,
5211 Name ": malloc of %zu failed.\n",
5212 sizeof(*super));
5213 close(fd);
5214 return 0;
5215 }
5216
5217 rv = find_intel_hba_capability(fd, super, verbose ? dev : NULL);
5218 if (rv != 0) {
5219 #if DEBUG
5220 char str[256];
5221 fd2devname(fd, str);
5222 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5223 fd, str, super->orom, rv, raiddisks);
5224 #endif
5225 /* no orom/efi or non-intel hba of the disk */
5226 close(fd);
5227 free_imsm(super);
5228 return 0;
5229 }
5230 close(fd);
5231 if (super->orom && raiddisks > super->orom->tds) {
5232 if (verbose)
5233 fprintf(stderr, Name ": %d exceeds maximum number of"
5234 " platform supported disks: %d\n",
5235 raiddisks, super->orom->tds);
5236
5237 free_imsm(super);
5238 return 0;
5239 }
5240
5241 *freesize = avail_size_imsm(st, ldsize >> 9);
5242 free_imsm(super);
5243
5244 return 1;
5245 }
5246
5247 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
5248 {
5249 const unsigned long long base_start = e[*idx].start;
5250 unsigned long long end = base_start + e[*idx].size;
5251 int i;
5252
5253 if (base_start == end)
5254 return 0;
5255
5256 *idx = *idx + 1;
5257 for (i = *idx; i < num_extents; i++) {
5258 /* extend overlapping extents */
5259 if (e[i].start >= base_start &&
5260 e[i].start <= end) {
5261 if (e[i].size == 0)
5262 return 0;
5263 if (e[i].start + e[i].size > end)
5264 end = e[i].start + e[i].size;
5265 } else if (e[i].start > end) {
5266 *idx = i;
5267 break;
5268 }
5269 }
5270
5271 return end - base_start;
5272 }
5273
5274 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
5275 {
5276 /* build a composite disk with all known extents and generate a new
5277 * 'maxsize' given the "all disks in an array must share a common start
5278 * offset" constraint
5279 */
5280 struct extent *e = calloc(sum_extents, sizeof(*e));
5281 struct dl *dl;
5282 int i, j;
5283 int start_extent;
5284 unsigned long long pos;
5285 unsigned long long start = 0;
5286 unsigned long long maxsize;
5287 unsigned long reserve;
5288
5289 if (!e)
5290 return 0;
5291
5292 /* coalesce and sort all extents. also, check to see if we need to
5293 * reserve space between member arrays
5294 */
5295 j = 0;
5296 for (dl = super->disks; dl; dl = dl->next) {
5297 if (!dl->e)
5298 continue;
5299 for (i = 0; i < dl->extent_cnt; i++)
5300 e[j++] = dl->e[i];
5301 }
5302 qsort(e, sum_extents, sizeof(*e), cmp_extent);
5303
5304 /* merge extents */
5305 i = 0;
5306 j = 0;
5307 while (i < sum_extents) {
5308 e[j].start = e[i].start;
5309 e[j].size = find_size(e, &i, sum_extents);
5310 j++;
5311 if (e[j-1].size == 0)
5312 break;
5313 }
5314
5315 pos = 0;
5316 maxsize = 0;
5317 start_extent = 0;
5318 i = 0;
5319 do {
5320 unsigned long long esize;
5321
5322 esize = e[i].start - pos;
5323 if (esize >= maxsize) {
5324 maxsize = esize;
5325 start = pos;
5326 start_extent = i;
5327 }
5328 pos = e[i].start + e[i].size;
5329 i++;
5330 } while (e[i-1].size);
5331 free(e);
5332
5333 if (maxsize == 0)
5334 return 0;
5335
5336 /* FIXME assumes volume at offset 0 is the first volume in a
5337 * container
5338 */
5339 if (start_extent > 0)
5340 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
5341 else
5342 reserve = 0;
5343
5344 if (maxsize < reserve)
5345 return 0;
5346
5347 super->create_offset = ~((__u32) 0);
5348 if (start + reserve > super->create_offset)
5349 return 0; /* start overflows create_offset */
5350 super->create_offset = start + reserve;
5351
5352 return maxsize - reserve;
5353 }
5354
5355 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
5356 {
5357 if (level < 0 || level == 6 || level == 4)
5358 return 0;
5359
5360 /* if we have an orom prevent invalid raid levels */
5361 if (orom)
5362 switch (level) {
5363 case 0: return imsm_orom_has_raid0(orom);
5364 case 1:
5365 if (raiddisks > 2)
5366 return imsm_orom_has_raid1e(orom);
5367 return imsm_orom_has_raid1(orom) && raiddisks == 2;
5368 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
5369 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
5370 }
5371 else
5372 return 1; /* not on an Intel RAID platform so anything goes */
5373
5374 return 0;
5375 }
5376
5377
5378 static int
5379 active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
5380 int dpa, int verbose)
5381 {
5382 struct mdstat_ent *mdstat = mdstat_read(0, 0);
5383 struct mdstat_ent *memb = NULL;
5384 int count = 0;
5385 int num = 0;
5386 struct md_list *dv = NULL;
5387 int found;
5388
5389 for (memb = mdstat ; memb ; memb = memb->next) {
5390 if (memb->metadata_version &&
5391 (strncmp(memb->metadata_version, "external:", 9) == 0) &&
5392 (strcmp(&memb->metadata_version[9], name) == 0) &&
5393 !is_subarray(memb->metadata_version+9) &&
5394 memb->members) {
5395 struct dev_member *dev = memb->members;
5396 int fd = -1;
5397 while(dev && (fd < 0)) {
5398 char *path = malloc(strlen(dev->name) + strlen("/dev/") + 1);
5399 if (path) {
5400 num = sprintf(path, "%s%s", "/dev/", dev->name);
5401 if (num > 0)
5402 fd = open(path, O_RDONLY, 0);
5403 if ((num <= 0) || (fd < 0)) {
5404 pr_vrb(": Cannot open %s: %s\n",
5405 dev->name, strerror(errno));
5406 }
5407 free(path);
5408 }
5409 dev = dev->next;
5410 }
5411 found = 0;
5412 if ((fd >= 0) && disk_attached_to_hba(fd, hba)) {
5413 struct mdstat_ent *vol;
5414 for (vol = mdstat ; vol ; vol = vol->next) {
5415 if ((vol->active > 0) &&
5416 vol->metadata_version &&
5417 is_container_member(vol, memb->dev)) {
5418 found++;
5419 count++;
5420 }
5421 }
5422 if (*devlist && (found < dpa)) {
5423 dv = calloc(1, sizeof(*dv));
5424 if (dv == NULL)
5425 fprintf(stderr, Name ": calloc failed\n");
5426 else {
5427 dv->devname = malloc(strlen(memb->dev) + strlen("/dev/") + 1);
5428 if (dv->devname != NULL) {
5429 sprintf(dv->devname, "%s%s", "/dev/", memb->dev);
5430 dv->found = found;
5431 dv->used = 0;
5432 dv->next = *devlist;
5433 *devlist = dv;
5434 } else
5435 free(dv);
5436 }
5437 }
5438 }
5439 if (fd >= 0)
5440 close(fd);
5441 }
5442 }
5443 free_mdstat(mdstat);
5444 return count;
5445 }
5446
5447 #ifdef DEBUG_LOOP
5448 static struct md_list*
5449 get_loop_devices(void)
5450 {
5451 int i;
5452 struct md_list *devlist = NULL;
5453 struct md_list *dv = NULL;
5454
5455 for(i = 0; i < 12; i++) {
5456 dv = calloc(1, sizeof(*dv));
5457 if (dv == NULL) {
5458 fprintf(stderr, Name ": calloc failed\n");
5459 break;
5460 }
5461 dv->devname = malloc(40);
5462 if (dv->devname == NULL) {
5463 fprintf(stderr, Name ": malloc failed\n");
5464 free(dv);
5465 break;
5466 }
5467 sprintf(dv->devname, "/dev/loop%d", i);
5468 dv->next = devlist;
5469 devlist = dv;
5470 }
5471 return devlist;
5472 }
5473 #endif
5474
5475 static struct md_list*
5476 get_devices(const char *hba_path)
5477 {
5478 struct md_list *devlist = NULL;
5479 struct md_list *dv = NULL;
5480 struct dirent *ent;
5481 DIR *dir;
5482 int err = 0;
5483
5484 #if DEBUG_LOOP
5485 devlist = get_loop_devices();
5486 return devlist;
5487 #endif
5488 /* scroll through /sys/dev/block looking for devices attached to
5489 * this hba
5490 */
5491 dir = opendir("/sys/dev/block");
5492 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
5493 int fd;
5494 char buf[1024];
5495 int major, minor;
5496 char *path = NULL;
5497 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
5498 continue;
5499 path = devt_to_devpath(makedev(major, minor));
5500 if (!path)
5501 continue;
5502 if (!path_attached_to_hba(path, hba_path)) {
5503 free(path);
5504 path = NULL;
5505 continue;
5506 }
5507 free(path);
5508 path = NULL;
5509 fd = dev_open(ent->d_name, O_RDONLY);
5510 if (fd >= 0) {
5511 fd2devname(fd, buf);
5512 close(fd);
5513 } else {
5514 fprintf(stderr, Name ": cannot open device: %s\n",
5515 ent->d_name);
5516 continue;
5517 }
5518
5519
5520 dv = calloc(1, sizeof(*dv));
5521 if (dv == NULL) {
5522 fprintf(stderr, Name ": malloc failed\n");
5523 err = 1;
5524 break;
5525 }
5526 dv->devname = strdup(buf);
5527 if (dv->devname == NULL) {
5528 fprintf(stderr, Name ": malloc failed\n");
5529 err = 1;
5530 free(dv);
5531 break;
5532 }
5533 dv->next = devlist;
5534 devlist = dv;
5535 }
5536 if (err) {
5537 while(devlist) {
5538 dv = devlist;
5539 devlist = devlist->next;
5540 free(dv->devname);
5541 free(dv);
5542 }
5543 }
5544 return devlist;
5545 }
5546
5547 static int
5548 count_volumes_list(struct md_list *devlist, char *homehost,
5549 int verbose, int *found)
5550 {
5551 struct md_list *tmpdev;
5552 int count = 0;
5553 struct supertype *st = NULL;
5554
5555 /* first walk the list of devices to find a consistent set
5556 * that match the criterea, if that is possible.
5557 * We flag the ones we like with 'used'.
5558 */
5559 *found = 0;
5560 st = match_metadata_desc_imsm("imsm");
5561 if (st == NULL) {
5562 pr_vrb(": cannot allocate memory for imsm supertype\n");
5563 return 0;
5564 }
5565
5566 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5567 char *devname = tmpdev->devname;
5568 struct stat stb;
5569 struct supertype *tst;
5570 int dfd;
5571 if (tmpdev->used > 1)
5572 continue;
5573 tst = dup_super(st);
5574 if (tst == NULL) {
5575 pr_vrb(": cannot allocate memory for imsm supertype\n");
5576 goto err_1;
5577 }
5578 tmpdev->container = 0;
5579 dfd = dev_open(devname, O_RDONLY|O_EXCL);
5580 if (dfd < 0) {
5581 dprintf(": cannot open device %s: %s\n",
5582 devname, strerror(errno));
5583 tmpdev->used = 2;
5584 } else if (fstat(dfd, &stb)< 0) {
5585 /* Impossible! */
5586 dprintf(": fstat failed for %s: %s\n",
5587 devname, strerror(errno));
5588 tmpdev->used = 2;
5589 } else if ((stb.st_mode & S_IFMT) != S_IFBLK) {
5590 dprintf(": %s is not a block device.\n",
5591 devname);
5592 tmpdev->used = 2;
5593 } else if (must_be_container(dfd)) {
5594 struct supertype *cst;
5595 cst = super_by_fd(dfd, NULL);
5596 if (cst == NULL) {
5597 dprintf(": cannot recognize container type %s\n",
5598 devname);
5599 tmpdev->used = 2;
5600 } else if (tst->ss != st->ss) {
5601 dprintf(": non-imsm container - ignore it: %s\n",
5602 devname);
5603 tmpdev->used = 2;
5604 } else if (!tst->ss->load_container ||
5605 tst->ss->load_container(tst, dfd, NULL))
5606 tmpdev->used = 2;
5607 else {
5608 tmpdev->container = 1;
5609 }
5610 if (cst)
5611 cst->ss->free_super(cst);
5612 } else {
5613 tmpdev->st_rdev = stb.st_rdev;
5614 if (tst->ss->load_super(tst,dfd, NULL)) {
5615 dprintf(": no RAID superblock on %s\n",
5616 devname);
5617 tmpdev->used = 2;
5618 } else if (tst->ss->compare_super == NULL) {
5619 dprintf(": Cannot assemble %s metadata on %s\n",
5620 tst->ss->name, devname);
5621 tmpdev->used = 2;
5622 }
5623 }
5624 if (dfd >= 0)
5625 close(dfd);
5626 if (tmpdev->used == 2 || tmpdev->used == 4) {
5627 /* Ignore unrecognised devices during auto-assembly */
5628 goto loop;
5629 }
5630 else {
5631 struct mdinfo info;
5632 tst->ss->getinfo_super(tst, &info, NULL);
5633
5634 if (st->minor_version == -1)
5635 st->minor_version = tst->minor_version;
5636
5637 if (memcmp(info.uuid, uuid_zero,
5638 sizeof(int[4])) == 0) {
5639 /* this is a floating spare. It cannot define
5640 * an array unless there are no more arrays of
5641 * this type to be found. It can be included
5642 * in an array of this type though.
5643 */
5644 tmpdev->used = 3;
5645 goto loop;
5646 }
5647
5648 if (st->ss != tst->ss ||
5649 st->minor_version != tst->minor_version ||
5650 st->ss->compare_super(st, tst) != 0) {
5651 /* Some mismatch. If exactly one array matches this host,
5652 * we can resolve on that one.
5653 * Or, if we are auto assembling, we just ignore the second
5654 * for now.
5655 */
5656 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5657 devname);
5658 goto loop;
5659 }
5660 tmpdev->used = 1;
5661 *found = 1;
5662 dprintf("found: devname: %s\n", devname);
5663 }
5664 loop:
5665 if (tst)
5666 tst->ss->free_super(tst);
5667 }
5668 if (*found != 0) {
5669 int err;
5670 if ((err = load_super_imsm_all(st, -1, &st->sb, NULL, devlist, 0)) == 0) {
5671 struct mdinfo *iter, *head = st->ss->container_content(st, NULL);
5672 for (iter = head; iter; iter = iter->next) {
5673 dprintf("content->text_version: %s vol\n",
5674 iter->text_version);
5675 if (iter->array.state & (1<<MD_SB_BLOCK_VOLUME)) {
5676 /* do not assemble arrays with unsupported
5677 configurations */
5678 dprintf(": Cannot activate member %s.\n",
5679 iter->text_version);
5680 } else
5681 count++;
5682 }
5683 sysfs_free(head);
5684
5685 } else {
5686 dprintf(" no valid super block on device list: err: %d %p\n",
5687 err, st->sb);
5688 }
5689 } else {
5690 dprintf(" no more devices to examin\n");
5691 }
5692
5693 for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
5694 if ((tmpdev->used == 1) && (tmpdev->found)) {
5695 if (count) {
5696 if (count < tmpdev->found)
5697 count = 0;
5698 else
5699 count -= tmpdev->found;
5700 }
5701 }
5702 if (tmpdev->used == 1)
5703 tmpdev->used = 4;
5704 }
5705 err_1:
5706 if (st)
5707 st->ss->free_super(st);
5708 return count;
5709 }
5710
5711
5712 static int
5713 count_volumes(char *hba, int dpa, int verbose)
5714 {
5715 struct md_list *devlist = NULL;
5716 int count = 0;
5717 int found = 0;;
5718
5719 devlist = get_devices(hba);
5720 /* if no intel devices return zero volumes */
5721 if (devlist == NULL)
5722 return 0;
5723
5724 count = active_arrays_by_format("imsm", hba, &devlist, dpa, verbose);
5725 dprintf(" path: %s active arrays: %d\n", hba, count);
5726 if (devlist == NULL)
5727 return 0;
5728 do {
5729 found = 0;
5730 count += count_volumes_list(devlist,
5731 NULL,
5732 verbose,
5733 &found);
5734 dprintf("found %d count: %d\n", found, count);
5735 } while (found);
5736
5737 dprintf("path: %s total number of volumes: %d\n", hba, count);
5738
5739 while(devlist) {
5740 struct md_list *dv = devlist;
5741 devlist = devlist->next;
5742 free(dv->devname);
5743 free(dv);
5744 }
5745 return count;
5746 }
5747
5748 static int imsm_default_chunk(const struct imsm_orom *orom)
5749 {
5750 /* up to 512 if the plaform supports it, otherwise the platform max.
5751 * 128 if no platform detected
5752 */
5753 int fs = max(7, orom ? fls(orom->sss) : 0);
5754
5755 return min(512, (1 << fs));
5756 }
5757
5758 static int
5759 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
5760 int raiddisks, int *chunk, int verbose)
5761 {
5762 /* check/set platform and metadata limits/defaults */
5763 if (super->orom && raiddisks > super->orom->dpa) {
5764 pr_vrb(": platform supports a maximum of %d disks per array\n",
5765 super->orom->dpa);
5766 return 0;
5767 }
5768
5769 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5770 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
5771 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5772 level, raiddisks, raiddisks > 1 ? "s" : "");
5773 return 0;
5774 }
5775
5776 if (chunk && (*chunk == 0 || *chunk == UnSet))
5777 *chunk = imsm_default_chunk(super->orom);
5778
5779 if (super->orom && chunk && !imsm_orom_has_chunk(super->orom, *chunk)) {
5780 pr_vrb(": platform does not support a chunk size of: "
5781 "%d\n", *chunk);
5782 return 0;
5783 }
5784
5785 if (layout != imsm_level_to_layout(level)) {
5786 if (level == 5)
5787 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5788 else if (level == 10)
5789 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5790 else
5791 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5792 layout, level);
5793 return 0;
5794 }
5795 return 1;
5796 }
5797
5798 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5799 * FIX ME add ahci details
5800 */
5801 static int validate_geometry_imsm_volume(struct supertype *st, int level,
5802 int layout, int raiddisks, int *chunk,
5803 unsigned long long size, char *dev,
5804 unsigned long long *freesize,
5805 int verbose)
5806 {
5807 struct stat stb;
5808 struct intel_super *super = st->sb;
5809 struct imsm_super *mpb;
5810 struct dl *dl;
5811 unsigned long long pos = 0;
5812 unsigned long long maxsize;
5813 struct extent *e;
5814 int i;
5815
5816 /* We must have the container info already read in. */
5817 if (!super)
5818 return 0;
5819
5820 mpb = super->anchor;
5821
5822 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose)) {
5823 fprintf(stderr, Name ": RAID gemetry validation failed. "
5824 "Cannot proceed with the action(s).\n");
5825 return 0;
5826 }
5827 if (!dev) {
5828 /* General test: make sure there is space for
5829 * 'raiddisks' device extents of size 'size' at a given
5830 * offset
5831 */
5832 unsigned long long minsize = size;
5833 unsigned long long start_offset = MaxSector;
5834 int dcnt = 0;
5835 if (minsize == 0)
5836 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
5837 for (dl = super->disks; dl ; dl = dl->next) {
5838 int found = 0;
5839
5840 pos = 0;
5841 i = 0;
5842 e = get_extents(super, dl);
5843 if (!e) continue;
5844 do {
5845 unsigned long long esize;
5846 esize = e[i].start - pos;
5847 if (esize >= minsize)
5848 found = 1;
5849 if (found && start_offset == MaxSector) {
5850 start_offset = pos;
5851 break;
5852 } else if (found && pos != start_offset) {
5853 found = 0;
5854 break;
5855 }
5856 pos = e[i].start + e[i].size;
5857 i++;
5858 } while (e[i-1].size);
5859 if (found)
5860 dcnt++;
5861 free(e);
5862 }
5863 if (dcnt < raiddisks) {
5864 if (verbose)
5865 fprintf(stderr, Name ": imsm: Not enough "
5866 "devices with space for this array "
5867 "(%d < %d)\n",
5868 dcnt, raiddisks);
5869 return 0;
5870 }
5871 return 1;
5872 }
5873
5874 /* This device must be a member of the set */
5875 if (stat(dev, &stb) < 0)
5876 return 0;
5877 if ((S_IFMT & stb.st_mode) != S_IFBLK)
5878 return 0;
5879 for (dl = super->disks ; dl ; dl = dl->next) {
5880 if (dl->major == (int)major(stb.st_rdev) &&
5881 dl->minor == (int)minor(stb.st_rdev))
5882 break;
5883 }
5884 if (!dl) {
5885 if (verbose)
5886 fprintf(stderr, Name ": %s is not in the "
5887 "same imsm set\n", dev);
5888 return 0;
5889 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
5890 /* If a volume is present then the current creation attempt
5891 * cannot incorporate new spares because the orom may not
5892 * understand this configuration (all member disks must be
5893 * members of each array in the container).
5894 */
5895 fprintf(stderr, Name ": %s is a spare and a volume"
5896 " is already defined for this container\n", dev);
5897 fprintf(stderr, Name ": The option-rom requires all member"
5898 " disks to be a member of all volumes\n");
5899 return 0;
5900 } else if (super->orom && mpb->num_raid_devs > 0 &&
5901 mpb->num_disks != raiddisks) {
5902 fprintf(stderr, Name ": The option-rom requires all member"
5903 " disks to be a member of all volumes\n");
5904 return 0;
5905 }
5906
5907 /* retrieve the largest free space block */
5908 e = get_extents(super, dl);
5909 maxsize = 0;
5910 i = 0;
5911 if (e) {
5912 do {
5913 unsigned long long esize;
5914
5915 esize = e[i].start - pos;
5916 if (esize >= maxsize)
5917 maxsize = esize;
5918 pos = e[i].start + e[i].size;
5919 i++;
5920 } while (e[i-1].size);
5921 dl->e = e;
5922 dl->extent_cnt = i;
5923 } else {
5924 if (verbose)
5925 fprintf(stderr, Name ": unable to determine free space for: %s\n",
5926 dev);
5927 return 0;
5928 }
5929 if (maxsize < size) {
5930 if (verbose)
5931 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
5932 dev, maxsize, size);
5933 return 0;
5934 }
5935
5936 /* count total number of extents for merge */
5937 i = 0;
5938 for (dl = super->disks; dl; dl = dl->next)
5939 if (dl->e)
5940 i += dl->extent_cnt;
5941
5942 maxsize = merge_extents(super, i);
5943
5944 if (!check_env("IMSM_NO_PLATFORM") &&
5945 mpb->num_raid_devs > 0 && size && size != maxsize) {
5946 fprintf(stderr, Name ": attempting to create a second "
5947 "volume with size less then remaining space. "
5948 "Aborting...\n");
5949 return 0;
5950 }
5951
5952 if (maxsize < size || maxsize == 0) {
5953 if (verbose) {
5954 if (maxsize == 0)
5955 fprintf(stderr, Name ": no free space"
5956 " left on device. Aborting...\n");
5957 else
5958 fprintf(stderr, Name ": not enough space"
5959 " to create volume of given size"
5960 " (%llu < %llu). Aborting...\n",
5961 maxsize, size);
5962 }
5963 return 0;
5964 }
5965
5966 *freesize = maxsize;
5967
5968 if (super->orom) {
5969 int count = count_volumes(super->hba->path,
5970 super->orom->dpa, verbose);
5971 if (super->orom->vphba <= count) {
5972 pr_vrb(": platform does not support more then %d raid volumes.\n",
5973 super->orom->vphba);
5974 return 0;
5975 }
5976 }
5977 return 1;
5978 }
5979
5980 static int reserve_space(struct supertype *st, int raiddisks,
5981 unsigned long long size, int chunk,
5982 unsigned long long *freesize)
5983 {
5984 struct intel_super *super = st->sb;
5985 struct imsm_super *mpb = super->anchor;
5986 struct dl *dl;
5987 int i;
5988 int extent_cnt;
5989 struct extent *e;
5990 unsigned long long maxsize;
5991 unsigned long long minsize;
5992 int cnt;
5993 int used;
5994
5995 /* find the largest common start free region of the possible disks */
5996 used = 0;
5997 extent_cnt = 0;
5998 cnt = 0;
5999 for (dl = super->disks; dl; dl = dl->next) {
6000 dl->raiddisk = -1;
6001
6002 if (dl->index >= 0)
6003 used++;
6004
6005 /* don't activate new spares if we are orom constrained
6006 * and there is already a volume active in the container
6007 */
6008 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
6009 continue;
6010
6011 e = get_extents(super, dl);
6012 if (!e)
6013 continue;
6014 for (i = 1; e[i-1].size; i++)
6015 ;
6016 dl->e = e;
6017 dl->extent_cnt = i;
6018 extent_cnt += i;
6019 cnt++;
6020 }
6021
6022 maxsize = merge_extents(super, extent_cnt);
6023 minsize = size;
6024 if (size == 0)
6025 /* chunk is in K */
6026 minsize = chunk * 2;
6027
6028 if (cnt < raiddisks ||
6029 (super->orom && used && used != raiddisks) ||
6030 maxsize < minsize ||
6031 maxsize == 0) {
6032 fprintf(stderr, Name ": not enough devices with space to create array.\n");
6033 return 0; /* No enough free spaces large enough */
6034 }
6035
6036 if (size == 0) {
6037 size = maxsize;
6038 if (chunk) {
6039 size /= 2 * chunk;
6040 size *= 2 * chunk;
6041 }
6042 maxsize = size;
6043 }
6044 if (!check_env("IMSM_NO_PLATFORM") &&
6045 mpb->num_raid_devs > 0 && size && size != maxsize) {
6046 fprintf(stderr, Name ": attempting to create a second "
6047 "volume with size less then remaining space. "
6048 "Aborting...\n");
6049 return 0;
6050 }
6051 cnt = 0;
6052 for (dl = super->disks; dl; dl = dl->next)
6053 if (dl->e)
6054 dl->raiddisk = cnt++;
6055
6056 *freesize = size;
6057
6058 return 1;
6059 }
6060
6061 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
6062 int raiddisks, int *chunk, unsigned long long size,
6063 char *dev, unsigned long long *freesize,
6064 int verbose)
6065 {
6066 int fd, cfd;
6067 struct mdinfo *sra;
6068 int is_member = 0;
6069
6070 /* load capability
6071 * if given unused devices create a container
6072 * if given given devices in a container create a member volume
6073 */
6074 if (level == LEVEL_CONTAINER) {
6075 /* Must be a fresh device to add to a container */
6076 return validate_geometry_imsm_container(st, level, layout,
6077 raiddisks,
6078 chunk?*chunk:0, size,
6079 dev, freesize,
6080 verbose);
6081 }
6082
6083 if (!dev) {
6084 if (st->sb) {
6085 struct intel_super *super = st->sb;
6086 if (!validate_geometry_imsm_orom(st->sb, level, layout,
6087 raiddisks, chunk,
6088 verbose))
6089 return 0;
6090 /* we are being asked to automatically layout a
6091 * new volume based on the current contents of
6092 * the container. If the the parameters can be
6093 * satisfied reserve_space will record the disks,
6094 * start offset, and size of the volume to be
6095 * created. add_to_super and getinfo_super
6096 * detect when autolayout is in progress.
6097 */
6098 /* assuming that freesize is always given when array is
6099 created */
6100 if (super->orom && freesize) {
6101 int count;
6102 count = count_volumes(super->hba->path,
6103 super->orom->dpa, verbose);
6104 if (super->orom->vphba <= count) {
6105 pr_vrb(": platform does not support more"
6106 "then %d raid volumes.\n",
6107 super->orom->vphba);
6108 return 0;
6109 }
6110 }
6111 if (freesize)
6112 return reserve_space(st, raiddisks, size,
6113 chunk?*chunk:0, freesize);
6114 }
6115 return 1;
6116 }
6117 if (st->sb) {
6118 /* creating in a given container */
6119 return validate_geometry_imsm_volume(st, level, layout,
6120 raiddisks, chunk, size,
6121 dev, freesize, verbose);
6122 }
6123
6124 /* This device needs to be a device in an 'imsm' container */
6125 fd = open(dev, O_RDONLY|O_EXCL, 0);
6126 if (fd >= 0) {
6127 if (verbose)
6128 fprintf(stderr,
6129 Name ": Cannot create this array on device %s\n",
6130 dev);
6131 close(fd);
6132 return 0;
6133 }
6134 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
6135 if (verbose)
6136 fprintf(stderr, Name ": Cannot open %s: %s\n",
6137 dev, strerror(errno));
6138 return 0;
6139 }
6140 /* Well, it is in use by someone, maybe an 'imsm' container. */
6141 cfd = open_container(fd);
6142 close(fd);
6143 if (cfd < 0) {
6144 if (verbose)
6145 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
6146 dev);
6147 return 0;
6148 }
6149 sra = sysfs_read(cfd, 0, GET_VERSION);
6150 if (sra && sra->array.major_version == -1 &&
6151 strcmp(sra->text_version, "imsm") == 0)
6152 is_member = 1;
6153 sysfs_free(sra);
6154 if (is_member) {
6155 /* This is a member of a imsm container. Load the container
6156 * and try to create a volume
6157 */
6158 struct intel_super *super;
6159
6160 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, NULL, 1) == 0) {
6161 st->sb = super;
6162 st->container_dev = fd2devnum(cfd);
6163 close(cfd);
6164 return validate_geometry_imsm_volume(st, level, layout,
6165 raiddisks, chunk,
6166 size, dev,
6167 freesize, 1)
6168 ? 1 : -1;
6169 }
6170 }
6171
6172 if (verbose)
6173 fprintf(stderr, Name ": failed container membership check\n");
6174
6175 close(cfd);
6176 return 0;
6177 }
6178
6179 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
6180 {
6181 struct intel_super *super = st->sb;
6182
6183 if (level && *level == UnSet)
6184 *level = LEVEL_CONTAINER;
6185
6186 if (level && layout && *layout == UnSet)
6187 *layout = imsm_level_to_layout(*level);
6188
6189 if (chunk && (*chunk == UnSet || *chunk == 0))
6190 *chunk = imsm_default_chunk(super->orom);
6191 }
6192
6193 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
6194
6195 static int kill_subarray_imsm(struct supertype *st)
6196 {
6197 /* remove the subarray currently referenced by ->current_vol */
6198 __u8 i;
6199 struct intel_dev **dp;
6200 struct intel_super *super = st->sb;
6201 __u8 current_vol = super->current_vol;
6202 struct imsm_super *mpb = super->anchor;
6203
6204 if (super->current_vol < 0)
6205 return 2;
6206 super->current_vol = -1; /* invalidate subarray cursor */
6207
6208 /* block deletions that would change the uuid of active subarrays
6209 *
6210 * FIXME when immutable ids are available, but note that we'll
6211 * also need to fixup the invalidated/active subarray indexes in
6212 * mdstat
6213 */
6214 for (i = 0; i < mpb->num_raid_devs; i++) {
6215 char subarray[4];
6216
6217 if (i < current_vol)
6218 continue;
6219 sprintf(subarray, "%u", i);
6220 if (is_subarray_active(subarray, st->devname)) {
6221 fprintf(stderr,
6222 Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6223 current_vol, i);
6224
6225 return 2;
6226 }
6227 }
6228
6229 if (st->update_tail) {
6230 struct imsm_update_kill_array *u = malloc(sizeof(*u));
6231
6232 if (!u)
6233 return 2;
6234 u->type = update_kill_array;
6235 u->dev_idx = current_vol;
6236 append_metadata_update(st, u, sizeof(*u));
6237
6238 return 0;
6239 }
6240
6241 for (dp = &super->devlist; *dp;)
6242 if ((*dp)->index == current_vol) {
6243 *dp = (*dp)->next;
6244 } else {
6245 handle_missing(super, (*dp)->dev);
6246 if ((*dp)->index > current_vol)
6247 (*dp)->index--;
6248 dp = &(*dp)->next;
6249 }
6250
6251 /* no more raid devices, all active components are now spares,
6252 * but of course failed are still failed
6253 */
6254 if (--mpb->num_raid_devs == 0) {
6255 struct dl *d;
6256
6257 for (d = super->disks; d; d = d->next)
6258 if (d->index > -2)
6259 mark_spare(d);
6260 }
6261
6262 super->updates_pending++;
6263
6264 return 0;
6265 }
6266
6267 static int update_subarray_imsm(struct supertype *st, char *subarray,
6268 char *update, struct mddev_ident *ident)
6269 {
6270 /* update the subarray currently referenced by ->current_vol */
6271 struct intel_super *super = st->sb;
6272 struct imsm_super *mpb = super->anchor;
6273
6274 if (strcmp(update, "name") == 0) {
6275 char *name = ident->name;
6276 char *ep;
6277 int vol;
6278
6279 if (is_subarray_active(subarray, st->devname)) {
6280 fprintf(stderr,
6281 Name ": Unable to update name of active subarray\n");
6282 return 2;
6283 }
6284
6285 if (!check_name(super, name, 0))
6286 return 2;
6287
6288 vol = strtoul(subarray, &ep, 10);
6289 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
6290 return 2;
6291
6292 if (st->update_tail) {
6293 struct imsm_update_rename_array *u = malloc(sizeof(*u));
6294
6295 if (!u)
6296 return 2;
6297 u->type = update_rename_array;
6298 u->dev_idx = vol;
6299 snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
6300 append_metadata_update(st, u, sizeof(*u));
6301 } else {
6302 struct imsm_dev *dev;
6303 int i;
6304
6305 dev = get_imsm_dev(super, vol);
6306 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
6307 for (i = 0; i < mpb->num_raid_devs; i++) {
6308 dev = get_imsm_dev(super, i);
6309 handle_missing(super, dev);
6310 }
6311 super->updates_pending++;
6312 }
6313 } else
6314 return 2;
6315
6316 return 0;
6317 }
6318 #endif /* MDASSEMBLE */
6319
6320 static int is_gen_migration(struct imsm_dev *dev)
6321 {
6322 if (dev == NULL)
6323 return 0;
6324
6325 if (!dev->vol.migr_state)
6326 return 0;
6327
6328 if (migr_type(dev) == MIGR_GEN_MIGR)
6329 return 1;
6330
6331 return 0;
6332 }
6333
6334 static int is_rebuilding(struct imsm_dev *dev)
6335 {
6336 struct imsm_map *migr_map;
6337
6338 if (!dev->vol.migr_state)
6339 return 0;
6340
6341 if (migr_type(dev) != MIGR_REBUILD)
6342 return 0;
6343
6344 migr_map = get_imsm_map(dev, MAP_1);
6345
6346 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
6347 return 1;
6348 else
6349 return 0;
6350 }
6351
6352 #ifndef MDASSEMBLE
6353 static int is_initializing(struct imsm_dev *dev)
6354 {
6355 struct imsm_map *migr_map;
6356
6357 if (!dev->vol.migr_state)
6358 return 0;
6359
6360 if (migr_type(dev) != MIGR_INIT)
6361 return 0;
6362
6363 migr_map = get_imsm_map(dev, MAP_1);
6364
6365 if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
6366 return 1;
6367
6368 return 0;
6369 }
6370 #endif
6371
6372 static void update_recovery_start(struct intel_super *super,
6373 struct imsm_dev *dev,
6374 struct mdinfo *array)
6375 {
6376 struct mdinfo *rebuild = NULL;
6377 struct mdinfo *d;
6378 __u32 units;
6379
6380 if (!is_rebuilding(dev))
6381 return;
6382
6383 /* Find the rebuild target, but punt on the dual rebuild case */
6384 for (d = array->devs; d; d = d->next)
6385 if (d->recovery_start == 0) {
6386 if (rebuild)
6387 return;
6388 rebuild = d;
6389 }
6390
6391 if (!rebuild) {
6392 /* (?) none of the disks are marked with
6393 * IMSM_ORD_REBUILD, so assume they are missing and the
6394 * disk_ord_tbl was not correctly updated
6395 */
6396 dprintf("%s: failed to locate out-of-sync disk\n", __func__);
6397 return;
6398 }
6399
6400 units = __le32_to_cpu(dev->vol.curr_migr_unit);
6401 rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
6402 }
6403
6404 #ifndef MDASSEMBLE
6405 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
6406 #endif
6407
6408 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
6409 {
6410 /* Given a container loaded by load_super_imsm_all,
6411 * extract information about all the arrays into
6412 * an mdinfo tree.
6413 * If 'subarray' is given, just extract info about that array.
6414 *
6415 * For each imsm_dev create an mdinfo, fill it in,
6416 * then look for matching devices in super->disks
6417 * and create appropriate device mdinfo.
6418 */
6419 struct intel_super *super = st->sb;
6420 struct imsm_super *mpb = super->anchor;
6421 struct mdinfo *rest = NULL;
6422 unsigned int i;
6423 int sb_errors = 0;
6424 struct dl *d;
6425 int spare_disks = 0;
6426
6427 /* do not assemble arrays when not all attributes are supported */
6428 if (imsm_check_attributes(mpb->attributes) == 0) {
6429 sb_errors = 1;
6430 fprintf(stderr, Name ": Unsupported attributes in IMSM metadata."
6431 "Arrays activation is blocked.\n");
6432 }
6433
6434 /* check for bad blocks */
6435 if (imsm_bbm_log_size(super->anchor)) {
6436 fprintf(stderr, Name ": BBM log found in IMSM metadata."
6437 "Arrays activation is blocked.\n");
6438 sb_errors = 1;
6439 }
6440
6441
6442 /* count spare devices, not used in maps
6443 */
6444 for (d = super->disks; d; d = d->next)
6445 if (d->index == -1)
6446 spare_disks++;
6447
6448 for (i = 0; i < mpb->num_raid_devs; i++) {
6449 struct imsm_dev *dev;
6450 struct imsm_map *map;
6451 struct imsm_map *map2;
6452 struct mdinfo *this;
6453 int slot;
6454 #ifndef MDASSEMBLE
6455 int chunk;
6456 #endif
6457 char *ep;
6458
6459 if (subarray &&
6460 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
6461 continue;
6462
6463 dev = get_imsm_dev(super, i);
6464 map = get_imsm_map(dev, MAP_0);
6465 map2 = get_imsm_map(dev, MAP_1);
6466
6467 /* do not publish arrays that are in the middle of an
6468 * unsupported migration
6469 */
6470 if (dev->vol.migr_state &&
6471 (migr_type(dev) == MIGR_STATE_CHANGE)) {
6472 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
6473 " unsupported migration in progress\n",
6474 dev->volume);
6475 continue;
6476 }
6477 /* do not publish arrays that are not support by controller's
6478 * OROM/EFI
6479 */
6480
6481 this = malloc(sizeof(*this));
6482 if (!this) {
6483 fprintf(stderr, Name ": failed to allocate %zu bytes\n",
6484 sizeof(*this));
6485 break;
6486 }
6487
6488 super->current_vol = i;
6489 getinfo_super_imsm_volume(st, this, NULL);
6490 this->next = rest;
6491 #ifndef MDASSEMBLE
6492 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
6493 /* mdadm does not support all metadata features- set the bit in all arrays state */
6494 if (!validate_geometry_imsm_orom(super,
6495 get_imsm_raid_level(map), /* RAID level */
6496 imsm_level_to_layout(get_imsm_raid_level(map)),
6497 map->num_members, /* raid disks */
6498 &chunk,
6499 1 /* verbose */)) {
6500 fprintf(stderr, Name ": IMSM RAID geometry validation"
6501 " failed. Array %s activation is blocked.\n",
6502 dev->volume);
6503 this->array.state |=
6504 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
6505 (1<<MD_SB_BLOCK_VOLUME);
6506 }
6507 #endif
6508
6509 /* if array has bad blocks, set suitable bit in all arrays state */
6510 if (sb_errors)
6511 this->array.state |=
6512 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
6513 (1<<MD_SB_BLOCK_VOLUME);
6514
6515 for (slot = 0 ; slot < map->num_members; slot++) {
6516 unsigned long long recovery_start;
6517 struct mdinfo *info_d;
6518 struct dl *d;
6519 int idx;
6520 int skip;
6521 __u32 ord;
6522
6523 skip = 0;
6524 idx = get_imsm_disk_idx(dev, slot, MAP_0);
6525 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
6526 for (d = super->disks; d ; d = d->next)
6527 if (d->index == idx)
6528 break;
6529
6530 recovery_start = MaxSector;
6531 if (d == NULL)
6532 skip = 1;
6533 if (d && is_failed(&d->disk))
6534 skip = 1;
6535 if (ord & IMSM_ORD_REBUILD)
6536 recovery_start = 0;
6537
6538 /*
6539 * if we skip some disks the array will be assmebled degraded;
6540 * reset resync start to avoid a dirty-degraded
6541 * situation when performing the intial sync
6542 *
6543 * FIXME handle dirty degraded
6544 */
6545 if ((skip || recovery_start == 0) && !dev->vol.dirty)
6546 this->resync_start = MaxSector;
6547 if (skip)
6548 continue;
6549
6550 info_d = calloc(1, sizeof(*info_d));
6551 if (!info_d) {
6552 fprintf(stderr, Name ": failed to allocate disk"
6553 " for volume %.16s\n", dev->volume);
6554 info_d = this->devs;
6555 while (info_d) {
6556 struct mdinfo *d = info_d->next;
6557
6558 free(info_d);
6559 info_d = d;
6560 }
6561 free(this);
6562 this = rest;
6563 break;
6564 }
6565 info_d->next = this->devs;
6566 this->devs = info_d;
6567
6568 info_d->disk.number = d->index;
6569 info_d->disk.major = d->major;
6570 info_d->disk.minor = d->minor;
6571 info_d->disk.raid_disk = slot;
6572 info_d->recovery_start = recovery_start;
6573 if (map2) {
6574 if (slot < map2->num_members)
6575 info_d->disk.state = (1 << MD_DISK_ACTIVE);
6576 else
6577 this->array.spare_disks++;
6578 } else {
6579 if (slot < map->num_members)
6580 info_d->disk.state = (1 << MD_DISK_ACTIVE);
6581 else
6582 this->array.spare_disks++;
6583 }
6584 if (info_d->recovery_start == MaxSector)
6585 this->array.working_disks++;
6586
6587 info_d->events = __le32_to_cpu(mpb->generation_num);
6588 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
6589 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
6590 }
6591 /* now that the disk list is up-to-date fixup recovery_start */
6592 update_recovery_start(super, dev, this);
6593 this->array.spare_disks += spare_disks;
6594
6595 #ifndef MDASSEMBLE
6596 /* check for reshape */
6597 if (this->reshape_active == 1)
6598 recover_backup_imsm(st, this);
6599 #endif
6600 rest = this;
6601 }
6602
6603 return rest;
6604 }
6605
6606
6607 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
6608 int failed, int look_in_map)
6609 {
6610 struct imsm_map *map;
6611
6612 map = get_imsm_map(dev, look_in_map);
6613
6614 if (!failed)
6615 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
6616 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
6617
6618 switch (get_imsm_raid_level(map)) {
6619 case 0:
6620 return IMSM_T_STATE_FAILED;
6621 break;
6622 case 1:
6623 if (failed < map->num_members)
6624 return IMSM_T_STATE_DEGRADED;
6625 else
6626 return IMSM_T_STATE_FAILED;
6627 break;
6628 case 10:
6629 {
6630 /**
6631 * check to see if any mirrors have failed, otherwise we
6632 * are degraded. Even numbered slots are mirrored on
6633 * slot+1
6634 */
6635 int i;
6636 /* gcc -Os complains that this is unused */
6637 int insync = insync;
6638
6639 for (i = 0; i < map->num_members; i++) {
6640 __u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
6641 int idx = ord_to_idx(ord);
6642 struct imsm_disk *disk;
6643
6644 /* reset the potential in-sync count on even-numbered
6645 * slots. num_copies is always 2 for imsm raid10
6646 */
6647 if ((i & 1) == 0)
6648 insync = 2;
6649
6650 disk = get_imsm_disk(super, idx);
6651 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
6652 insync--;
6653
6654 /* no in-sync disks left in this mirror the
6655 * array has failed
6656 */
6657 if (insync == 0)
6658 return IMSM_T_STATE_FAILED;
6659 }
6660
6661 return IMSM_T_STATE_DEGRADED;
6662 }
6663 case 5:
6664 if (failed < 2)
6665 return IMSM_T_STATE_DEGRADED;
6666 else
6667 return IMSM_T_STATE_FAILED;
6668 break;
6669 default:
6670 break;
6671 }
6672
6673 return map->map_state;
6674 }
6675
6676 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
6677 int look_in_map)
6678 {
6679 int i;
6680 int failed = 0;
6681 struct imsm_disk *disk;
6682 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6683 struct imsm_map *prev = get_imsm_map(dev, MAP_1);
6684 struct imsm_map *map_for_loop;
6685 __u32 ord;
6686 int idx;
6687 int idx_1;
6688
6689 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6690 * disks that are being rebuilt. New failures are recorded to
6691 * map[0]. So we look through all the disks we started with and
6692 * see if any failures are still present, or if any new ones
6693 * have arrived
6694 */
6695 map_for_loop = map;
6696 if (prev && (map->num_members < prev->num_members))
6697 map_for_loop = prev;
6698
6699 for (i = 0; i < map_for_loop->num_members; i++) {
6700 idx_1 = -255;
6701 /* when MAP_X is passed both maps failures are counted
6702 */
6703 if (prev &&
6704 ((look_in_map == MAP_1) || (look_in_map == MAP_X)) &&
6705 (i < prev->num_members)) {
6706 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
6707 idx_1 = ord_to_idx(ord);
6708
6709 disk = get_imsm_disk(super, idx_1);
6710 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
6711 failed++;
6712 }
6713 if (((look_in_map == MAP_0) || (look_in_map == MAP_X)) &&
6714 (i < map->num_members)) {
6715 ord = __le32_to_cpu(map->disk_ord_tbl[i]);
6716 idx = ord_to_idx(ord);
6717
6718 if (idx != idx_1) {
6719 disk = get_imsm_disk(super, idx);
6720 if (!disk || is_failed(disk) ||
6721 ord & IMSM_ORD_REBUILD)
6722 failed++;
6723 }
6724 }
6725 }
6726
6727 return failed;
6728 }
6729
6730 #ifndef MDASSEMBLE
6731 static int imsm_open_new(struct supertype *c, struct active_array *a,
6732 char *inst)
6733 {
6734 struct intel_super *super = c->sb;
6735 struct imsm_super *mpb = super->anchor;
6736
6737 if (atoi(inst) >= mpb->num_raid_devs) {
6738 fprintf(stderr, "%s: subarry index %d, out of range\n",
6739 __func__, atoi(inst));
6740 return -ENODEV;
6741 }
6742
6743 dprintf("imsm: open_new %s\n", inst);
6744 a->info.container_member = atoi(inst);
6745 return 0;
6746 }
6747
6748 static int is_resyncing(struct imsm_dev *dev)
6749 {
6750 struct imsm_map *migr_map;
6751
6752 if (!dev->vol.migr_state)
6753 return 0;
6754
6755 if (migr_type(dev) == MIGR_INIT ||
6756 migr_type(dev) == MIGR_REPAIR)
6757 return 1;
6758
6759 if (migr_type(dev) == MIGR_GEN_MIGR)
6760 return 0;
6761
6762 migr_map = get_imsm_map(dev, MAP_1);
6763
6764 if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
6765 (dev->vol.migr_type != MIGR_GEN_MIGR))
6766 return 1;
6767 else
6768 return 0;
6769 }
6770
6771 /* return true if we recorded new information */
6772 static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
6773 {
6774 __u32 ord;
6775 int slot;
6776 struct imsm_map *map;
6777 char buf[MAX_RAID_SERIAL_LEN+3];
6778 unsigned int len, shift = 0;
6779
6780 /* new failures are always set in map[0] */
6781 map = get_imsm_map(dev, MAP_0);
6782
6783 slot = get_imsm_disk_slot(map, idx);
6784 if (slot < 0)
6785 return 0;
6786
6787 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
6788 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
6789 return 0;
6790
6791 memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
6792 buf[MAX_RAID_SERIAL_LEN] = '\000';
6793 strcat(buf, ":0");
6794 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
6795 shift = len - MAX_RAID_SERIAL_LEN + 1;
6796 strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN);
6797
6798 disk->status |= FAILED_DISK;
6799 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
6800 /* mark failures in second map if second map exists and this disk
6801 * in this slot.
6802 * This is valid for migration, initialization and rebuild
6803 */
6804 if (dev->vol.migr_state) {
6805 struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
6806 int slot2 = get_imsm_disk_slot(map2, idx);
6807
6808 if ((slot2 < map2->num_members) &&
6809 (slot2 >= 0))
6810 set_imsm_ord_tbl_ent(map2, slot2,
6811 idx | IMSM_ORD_REBUILD);
6812 }
6813 if (map->failed_disk_num == 0xff)
6814 map->failed_disk_num = slot;
6815 return 1;
6816 }
6817
6818 static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
6819 {
6820 mark_failure(dev, disk, idx);
6821
6822 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
6823 return;
6824
6825 disk->scsi_id = __cpu_to_le32(~(__u32)0);
6826 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
6827 }
6828
6829 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
6830 {
6831 struct dl *dl;
6832
6833 if (!super->missing)
6834 return;
6835
6836 dprintf("imsm: mark missing\n");
6837 /* end process for initialization and rebuild only
6838 */
6839 if (is_gen_migration(dev) == 0) {
6840 __u8 map_state;
6841 int failed;
6842
6843 failed = imsm_count_failed(super, dev, MAP_0);
6844 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
6845
6846 end_migration(dev, super, map_state);
6847 }
6848 for (dl = super->missing; dl; dl = dl->next)
6849 mark_missing(dev, &dl->disk, dl->index);
6850 super->updates_pending++;
6851 }
6852
6853 static unsigned long long imsm_set_array_size(struct imsm_dev *dev)
6854 {
6855 int used_disks = imsm_num_data_members(dev, MAP_0);
6856 unsigned long long array_blocks;
6857 struct imsm_map *map;
6858
6859 if (used_disks == 0) {
6860 /* when problems occures
6861 * return current array_blocks value
6862 */
6863 array_blocks = __le32_to_cpu(dev->size_high);
6864 array_blocks = array_blocks << 32;
6865 array_blocks += __le32_to_cpu(dev->size_low);
6866
6867 return array_blocks;
6868 }
6869
6870 /* set array size in metadata
6871 */
6872 map = get_imsm_map(dev, MAP_0);
6873 array_blocks = map->blocks_per_member * used_disks;
6874
6875 /* round array size down to closest MB
6876 */
6877 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
6878 dev->size_low = __cpu_to_le32((__u32)array_blocks);
6879 dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32));
6880
6881 return array_blocks;
6882 }
6883
6884 static void imsm_set_disk(struct active_array *a, int n, int state);
6885
6886 static void imsm_progress_container_reshape(struct intel_super *super)
6887 {
6888 /* if no device has a migr_state, but some device has a
6889 * different number of members than the previous device, start
6890 * changing the number of devices in this device to match
6891 * previous.
6892 */
6893 struct imsm_super *mpb = super->anchor;
6894 int prev_disks = -1;
6895 int i;
6896 int copy_map_size;
6897
6898 for (i = 0; i < mpb->num_raid_devs; i++) {
6899 struct imsm_dev *dev = get_imsm_dev(super, i);
6900 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6901 struct imsm_map *map2;
6902 int prev_num_members;
6903
6904 if (dev->vol.migr_state)
6905 return;
6906
6907 if (prev_disks == -1)
6908 prev_disks = map->num_members;
6909 if (prev_disks == map->num_members)
6910 continue;
6911
6912 /* OK, this array needs to enter reshape mode.
6913 * i.e it needs a migr_state
6914 */
6915
6916 copy_map_size = sizeof_imsm_map(map);
6917 prev_num_members = map->num_members;
6918 map->num_members = prev_disks;
6919 dev->vol.migr_state = 1;
6920 dev->vol.curr_migr_unit = 0;
6921 set_migr_type(dev, MIGR_GEN_MIGR);
6922 for (i = prev_num_members;
6923 i < map->num_members; i++)
6924 set_imsm_ord_tbl_ent(map, i, i);
6925 map2 = get_imsm_map(dev, MAP_1);
6926 /* Copy the current map */
6927 memcpy(map2, map, copy_map_size);
6928 map2->num_members = prev_num_members;
6929
6930 imsm_set_array_size(dev);
6931 super->updates_pending++;
6932 }
6933 }
6934
6935 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6936 * states are handled in imsm_set_disk() with one exception, when a
6937 * resync is stopped due to a new failure this routine will set the
6938 * 'degraded' state for the array.
6939 */
6940 static int imsm_set_array_state(struct active_array *a, int consistent)
6941 {
6942 int inst = a->info.container_member;
6943 struct intel_super *super = a->container->sb;
6944 struct imsm_dev *dev = get_imsm_dev(super, inst);
6945 struct imsm_map *map = get_imsm_map(dev, MAP_0);
6946 int failed = imsm_count_failed(super, dev, MAP_0);
6947 __u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
6948 __u32 blocks_per_unit;
6949
6950 if (dev->vol.migr_state &&
6951 dev->vol.migr_type == MIGR_GEN_MIGR) {
6952 /* array state change is blocked due to reshape action
6953 * We might need to
6954 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6955 * - finish the reshape (if last_checkpoint is big and action != reshape)
6956 * - update curr_migr_unit
6957 */
6958 if (a->curr_action == reshape) {
6959 /* still reshaping, maybe update curr_migr_unit */
6960 goto mark_checkpoint;
6961 } else {
6962 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
6963 /* for some reason we aborted the reshape.
6964 *
6965 * disable automatic metadata rollback
6966 * user action is required to recover process
6967 */
6968 if (0) {
6969 struct imsm_map *map2 =
6970 get_imsm_map(dev, MAP_1);
6971 dev->vol.migr_state = 0;
6972 set_migr_type(dev, 0);
6973 dev->vol.curr_migr_unit = 0;
6974 memcpy(map, map2,
6975 sizeof_imsm_map(map2));
6976 super->updates_pending++;
6977 }
6978 }
6979 if (a->last_checkpoint >= a->info.component_size) {
6980 unsigned long long array_blocks;
6981 int used_disks;
6982 struct mdinfo *mdi;
6983
6984 used_disks = imsm_num_data_members(dev, MAP_0);
6985 if (used_disks > 0) {
6986 array_blocks =
6987 map->blocks_per_member *
6988 used_disks;
6989 /* round array size down to closest MB
6990 */
6991 array_blocks = (array_blocks
6992 >> SECT_PER_MB_SHIFT)
6993 << SECT_PER_MB_SHIFT;
6994 a->info.custom_array_size = array_blocks;
6995 /* encourage manager to update array
6996 * size
6997 */
6998
6999 a->check_reshape = 1;
7000 }
7001 /* finalize online capacity expansion/reshape */
7002 for (mdi = a->info.devs; mdi; mdi = mdi->next)
7003 imsm_set_disk(a,
7004 mdi->disk.raid_disk,
7005 mdi->curr_state);
7006
7007 imsm_progress_container_reshape(super);
7008 }
7009 }
7010 }
7011
7012 /* before we activate this array handle any missing disks */
7013 if (consistent == 2)
7014 handle_missing(super, dev);
7015
7016 if (consistent == 2 &&
7017 (!is_resync_complete(&a->info) ||
7018 map_state != IMSM_T_STATE_NORMAL ||
7019 dev->vol.migr_state))
7020 consistent = 0;
7021
7022 if (is_resync_complete(&a->info)) {
7023 /* complete intialization / resync,
7024 * recovery and interrupted recovery is completed in
7025 * ->set_disk
7026 */
7027 if (is_resyncing(dev)) {
7028 dprintf("imsm: mark resync done\n");
7029 end_migration(dev, super, map_state);
7030 super->updates_pending++;
7031 a->last_checkpoint = 0;
7032 }
7033 } else if ((!is_resyncing(dev) && !failed) &&
7034 (imsm_reshape_blocks_arrays_changes(super) == 0)) {
7035 /* mark the start of the init process if nothing is failed */
7036 dprintf("imsm: mark resync start\n");
7037 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
7038 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_INIT);
7039 else
7040 migrate(dev, super, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
7041 super->updates_pending++;
7042 }
7043
7044 mark_checkpoint:
7045 /* skip checkpointing for general migration,
7046 * it is controlled in mdadm
7047 */
7048 if (is_gen_migration(dev))
7049 goto skip_mark_checkpoint;
7050
7051 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7052 blocks_per_unit = blocks_per_migr_unit(super, dev);
7053 if (blocks_per_unit) {
7054 __u32 units32;
7055 __u64 units;
7056
7057 units = a->last_checkpoint / blocks_per_unit;
7058 units32 = units;
7059
7060 /* check that we did not overflow 32-bits, and that
7061 * curr_migr_unit needs updating
7062 */
7063 if (units32 == units &&
7064 units32 != 0 &&
7065 __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
7066 dprintf("imsm: mark checkpoint (%u)\n", units32);
7067 dev->vol.curr_migr_unit = __cpu_to_le32(units32);
7068 super->updates_pending++;
7069 }
7070 }
7071
7072 skip_mark_checkpoint:
7073 /* mark dirty / clean */
7074 if (dev->vol.dirty != !consistent) {
7075 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
7076 if (consistent)
7077 dev->vol.dirty = 0;
7078 else
7079 dev->vol.dirty = 1;
7080 super->updates_pending++;
7081 }
7082
7083 return consistent;
7084 }
7085
7086 static void imsm_set_disk(struct active_array *a, int n, int state)
7087 {
7088 int inst = a->info.container_member;
7089 struct intel_super *super = a->container->sb;
7090 struct imsm_dev *dev = get_imsm_dev(super, inst);
7091 struct imsm_map *map = get_imsm_map(dev, MAP_0);
7092 struct imsm_disk *disk;
7093 int failed;
7094 __u32 ord;
7095 __u8 map_state;
7096
7097 if (n > map->num_members)
7098 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
7099 n, map->num_members - 1);
7100
7101 if (n < 0)
7102 return;
7103
7104 dprintf("imsm: set_disk %d:%x\n", n, state);
7105
7106 ord = get_imsm_ord_tbl_ent(dev, n, MAP_0);
7107 disk = get_imsm_disk(super, ord_to_idx(ord));
7108
7109 /* check for new failures */
7110 if (state & DS_FAULTY) {
7111 if (mark_failure(dev, disk, ord_to_idx(ord)))
7112 super->updates_pending++;
7113 }
7114
7115 /* check if in_sync */
7116 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
7117 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
7118
7119 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
7120 super->updates_pending++;
7121 }
7122
7123 failed = imsm_count_failed(super, dev, MAP_0);
7124 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
7125
7126 /* check if recovery complete, newly degraded, or failed */
7127 dprintf("imsm: Detected transition to state ");
7128 switch (map_state) {
7129 case IMSM_T_STATE_NORMAL: /* transition to normal state */
7130 dprintf("normal: ");
7131 if (is_rebuilding(dev)) {
7132 dprintf("while rebuilding");
7133 end_migration(dev, super, map_state);
7134 map = get_imsm_map(dev, MAP_0);
7135 map->failed_disk_num = ~0;
7136 super->updates_pending++;
7137 a->last_checkpoint = 0;
7138 break;
7139 }
7140 if (is_gen_migration(dev)) {
7141 dprintf("while general migration");
7142 if (a->last_checkpoint >= a->info.component_size)
7143 end_migration(dev, super, map_state);
7144 else
7145 map->map_state = map_state;
7146 map = get_imsm_map(dev, MAP_0);
7147 map->failed_disk_num = ~0;
7148 super->updates_pending++;
7149 break;
7150 }
7151 break;
7152 case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
7153 dprintf("degraded: ");
7154 if ((map->map_state != map_state) &&
7155 !dev->vol.migr_state) {
7156 dprintf("mark degraded");
7157 map->map_state = map_state;
7158 super->updates_pending++;
7159 a->last_checkpoint = 0;
7160 break;
7161 }
7162 if (is_rebuilding(dev)) {
7163 dprintf("while rebuilding.");
7164 if (map->map_state != map_state) {
7165 dprintf(" Map state change");
7166 end_migration(dev, super, map_state);
7167 super->updates_pending++;
7168 }
7169 break;
7170 }
7171 if (is_gen_migration(dev)) {
7172 dprintf("while general migration");
7173 if (a->last_checkpoint >= a->info.component_size)
7174 end_migration(dev, super, map_state);
7175 else {
7176 map->map_state = map_state;
7177 manage_second_map(super, dev);
7178 }
7179 super->updates_pending++;
7180 break;
7181 }
7182 if (is_initializing(dev)) {
7183 dprintf("while initialization.");
7184 map->map_state = map_state;
7185 super->updates_pending++;
7186 break;
7187 }
7188 break;
7189 case IMSM_T_STATE_FAILED: /* transition to failed state */
7190 dprintf("failed: ");
7191 if (is_gen_migration(dev)) {
7192 dprintf("while general migration");
7193 map->map_state = map_state;
7194 super->updates_pending++;
7195 break;
7196 }
7197 if (map->map_state != map_state) {
7198 dprintf("mark failed");
7199 end_migration(dev, super, map_state);
7200 super->updates_pending++;
7201 a->last_checkpoint = 0;
7202 break;
7203 }
7204 break;
7205 default:
7206 dprintf("state %i\n", map_state);
7207 }
7208 dprintf("\n");
7209
7210 }
7211
7212 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
7213 {
7214 void *buf = mpb;
7215 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
7216 unsigned long long dsize;
7217 unsigned long long sectors;
7218
7219 get_dev_size(fd, NULL, &dsize);
7220
7221 if (mpb_size > 512) {
7222 /* -1 to account for anchor */
7223 sectors = mpb_sectors(mpb) - 1;
7224
7225 /* write the extended mpb to the sectors preceeding the anchor */
7226 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
7227 return 1;
7228
7229 if ((unsigned long long)write(fd, buf + 512, 512 * sectors)
7230 != 512 * sectors)
7231 return 1;
7232 }
7233
7234 /* first block is stored on second to last sector of the disk */
7235 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
7236 return 1;
7237
7238 if (write(fd, buf, 512) != 512)
7239 return 1;
7240
7241 return 0;
7242 }
7243
7244 static void imsm_sync_metadata(struct supertype *container)
7245 {
7246 struct intel_super *super = container->sb;
7247
7248 dprintf("sync metadata: %d\n", super->updates_pending);
7249 if (!super->updates_pending)
7250 return;
7251
7252 write_super_imsm(container, 0);
7253
7254 super->updates_pending = 0;
7255 }
7256
7257 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
7258 {
7259 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
7260 int i = get_imsm_disk_idx(dev, idx, MAP_X);
7261 struct dl *dl;
7262
7263 for (dl = super->disks; dl; dl = dl->next)
7264 if (dl->index == i)
7265 break;
7266
7267 if (dl && is_failed(&dl->disk))
7268 dl = NULL;
7269
7270 if (dl)
7271 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
7272
7273 return dl;
7274 }
7275
7276 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
7277 struct active_array *a, int activate_new,
7278 struct mdinfo *additional_test_list)
7279 {
7280 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
7281 int idx = get_imsm_disk_idx(dev, slot, MAP_X);
7282 struct imsm_super *mpb = super->anchor;
7283 struct imsm_map *map;
7284 unsigned long long pos;
7285 struct mdinfo *d;
7286 struct extent *ex;
7287 int i, j;
7288 int found;
7289 __u32 array_start = 0;
7290 __u32 array_end = 0;
7291 struct dl *dl;
7292 struct mdinfo *test_list;
7293
7294 for (dl = super->disks; dl; dl = dl->next) {
7295 /* If in this array, skip */
7296 for (d = a->info.devs ; d ; d = d->next)
7297 if (d->state_fd >= 0 &&
7298 d->disk.major == dl->major &&
7299 d->disk.minor == dl->minor) {
7300 dprintf("%x:%x already in array\n",
7301 dl->major, dl->minor);
7302 break;
7303 }
7304 if (d)
7305 continue;
7306 test_list = additional_test_list;
7307 while (test_list) {
7308 if (test_list->disk.major == dl->major &&
7309 test_list->disk.minor == dl->minor) {
7310 dprintf("%x:%x already in additional test list\n",
7311 dl->major, dl->minor);
7312 break;
7313 }
7314 test_list = test_list->next;
7315 }
7316 if (test_list)
7317 continue;
7318
7319 /* skip in use or failed drives */
7320 if (is_failed(&dl->disk) || idx == dl->index ||
7321 dl->index == -2) {
7322 dprintf("%x:%x status (failed: %d index: %d)\n",
7323 dl->major, dl->minor, is_failed(&dl->disk), idx);
7324 continue;
7325 }
7326
7327 /* skip pure spares when we are looking for partially
7328 * assimilated drives
7329 */
7330 if (dl->index == -1 && !activate_new)
7331 continue;
7332
7333 /* Does this unused device have the requisite free space?
7334 * It needs to be able to cover all member volumes
7335 */
7336 ex = get_extents(super, dl);
7337 if (!ex) {
7338 dprintf("cannot get extents\n");
7339 continue;
7340 }
7341 for (i = 0; i < mpb->num_raid_devs; i++) {
7342 dev = get_imsm_dev(super, i);
7343 map = get_imsm_map(dev, MAP_0);
7344
7345 /* check if this disk is already a member of
7346 * this array
7347 */
7348 if (get_imsm_disk_slot(map, dl->index) >= 0)
7349 continue;
7350
7351 found = 0;
7352 j = 0;
7353 pos = 0;
7354 array_start = __le32_to_cpu(map->pba_of_lba0);
7355 array_end = array_start +
7356 __le32_to_cpu(map->blocks_per_member) - 1;
7357
7358 do {
7359 /* check that we can start at pba_of_lba0 with
7360 * blocks_per_member of space
7361 */
7362 if (array_start >= pos && array_end < ex[j].start) {
7363 found = 1;
7364 break;
7365 }
7366 pos = ex[j].start + ex[j].size;
7367 j++;
7368 } while (ex[j-1].size);
7369
7370 if (!found)
7371 break;
7372 }
7373
7374 free(ex);
7375 if (i < mpb->num_raid_devs) {
7376 dprintf("%x:%x does not have %u to %u available\n",
7377 dl->major, dl->minor, array_start, array_end);
7378 /* No room */
7379 continue;
7380 }
7381 return dl;
7382 }
7383
7384 return dl;
7385 }
7386
7387
7388 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
7389 {
7390 struct imsm_dev *dev2;
7391 struct imsm_map *map;
7392 struct dl *idisk;
7393 int slot;
7394 int idx;
7395 __u8 state;
7396
7397 dev2 = get_imsm_dev(cont->sb, dev_idx);
7398 if (dev2) {
7399 state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
7400 if (state == IMSM_T_STATE_FAILED) {
7401 map = get_imsm_map(dev2, MAP_0);
7402 if (!map)
7403 return 1;
7404 for (slot = 0; slot < map->num_members; slot++) {
7405 /*
7406 * Check if failed disks are deleted from intel
7407 * disk list or are marked to be deleted
7408 */
7409 idx = get_imsm_disk_idx(dev2, slot, MAP_X);
7410 idisk = get_imsm_dl_disk(cont->sb, idx);
7411 /*
7412 * Do not rebuild the array if failed disks
7413 * from failed sub-array are not removed from
7414 * container.
7415 */
7416 if (idisk &&
7417 is_failed(&idisk->disk) &&
7418 (idisk->action != DISK_REMOVE))
7419 return 0;
7420 }
7421 }
7422 }
7423 return 1;
7424 }
7425
7426 static struct mdinfo *imsm_activate_spare(struct active_array *a,
7427 struct metadata_update **updates)
7428 {
7429 /**
7430 * Find a device with unused free space and use it to replace a
7431 * failed/vacant region in an array. We replace failed regions one a
7432 * array at a time. The result is that a new spare disk will be added
7433 * to the first failed array and after the monitor has finished
7434 * propagating failures the remainder will be consumed.
7435 *
7436 * FIXME add a capability for mdmon to request spares from another
7437 * container.
7438 */
7439
7440 struct intel_super *super = a->container->sb;
7441 int inst = a->info.container_member;
7442 struct imsm_dev *dev = get_imsm_dev(super, inst);
7443 struct imsm_map *map = get_imsm_map(dev, MAP_0);
7444 int failed = a->info.array.raid_disks;
7445 struct mdinfo *rv = NULL;
7446 struct mdinfo *d;
7447 struct mdinfo *di;
7448 struct metadata_update *mu;
7449 struct dl *dl;
7450 struct imsm_update_activate_spare *u;
7451 int num_spares = 0;
7452 int i;
7453 int allowed;
7454
7455 for (d = a->info.devs ; d ; d = d->next) {
7456 if ((d->curr_state & DS_FAULTY) &&
7457 d->state_fd >= 0)
7458 /* wait for Removal to happen */
7459 return NULL;
7460 if (d->state_fd >= 0)
7461 failed--;
7462 }
7463
7464 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7465 inst, failed, a->info.array.raid_disks, a->info.array.level);
7466
7467 if (imsm_reshape_blocks_arrays_changes(super))
7468 return NULL;
7469
7470 /* Cannot activate another spare if rebuild is in progress already
7471 */
7472 if (is_rebuilding(dev)) {
7473 dprintf("imsm: No spare activation allowed. "
7474 "Rebuild in progress already.\n");
7475 return NULL;
7476 }
7477
7478 if (a->info.array.level == 4)
7479 /* No repair for takeovered array
7480 * imsm doesn't support raid4
7481 */
7482 return NULL;
7483
7484 if (imsm_check_degraded(super, dev, failed, MAP_0) !=
7485 IMSM_T_STATE_DEGRADED)
7486 return NULL;
7487
7488 /*
7489 * If there are any failed disks check state of the other volume.
7490 * Block rebuild if the another one is failed until failed disks
7491 * are removed from container.
7492 */
7493 if (failed) {
7494 dprintf("found failed disks in %.*s, check if there another"
7495 "failed sub-array.\n",
7496 MAX_RAID_SERIAL_LEN, dev->volume);
7497 /* check if states of the other volumes allow for rebuild */
7498 for (i = 0; i < super->anchor->num_raid_devs; i++) {
7499 if (i != inst) {
7500 allowed = imsm_rebuild_allowed(a->container,
7501 i, failed);
7502 if (!allowed)
7503 return NULL;
7504 }
7505 }
7506 }
7507
7508 /* For each slot, if it is not working, find a spare */
7509 for (i = 0; i < a->info.array.raid_disks; i++) {
7510 for (d = a->info.devs ; d ; d = d->next)
7511 if (d->disk.raid_disk == i)
7512 break;
7513 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
7514 if (d && (d->state_fd >= 0))
7515 continue;
7516
7517 /*
7518 * OK, this device needs recovery. Try to re-add the
7519 * previous occupant of this slot, if this fails see if
7520 * we can continue the assimilation of a spare that was
7521 * partially assimilated, finally try to activate a new
7522 * spare.
7523 */
7524 dl = imsm_readd(super, i, a);
7525 if (!dl)
7526 dl = imsm_add_spare(super, i, a, 0, rv);
7527 if (!dl)
7528 dl = imsm_add_spare(super, i, a, 1, rv);
7529 if (!dl)
7530 continue;
7531
7532 /* found a usable disk with enough space */
7533 di = malloc(sizeof(*di));
7534 if (!di)
7535 continue;
7536 memset(di, 0, sizeof(*di));
7537
7538 /* dl->index will be -1 in the case we are activating a
7539 * pristine spare. imsm_process_update() will create a
7540 * new index in this case. Once a disk is found to be
7541 * failed in all member arrays it is kicked from the
7542 * metadata
7543 */
7544 di->disk.number = dl->index;
7545
7546 /* (ab)use di->devs to store a pointer to the device
7547 * we chose
7548 */
7549 di->devs = (struct mdinfo *) dl;
7550
7551 di->disk.raid_disk = i;
7552 di->disk.major = dl->major;
7553 di->disk.minor = dl->minor;
7554 di->disk.state = 0;
7555 di->recovery_start = 0;
7556 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
7557 di->component_size = a->info.component_size;
7558 di->container_member = inst;
7559 super->random = random32();
7560 di->next = rv;
7561 rv = di;
7562 num_spares++;
7563 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
7564 i, di->data_offset);
7565 }
7566
7567 if (!rv)
7568 /* No spares found */
7569 return rv;
7570 /* Now 'rv' has a list of devices to return.
7571 * Create a metadata_update record to update the
7572 * disk_ord_tbl for the array
7573 */
7574 mu = malloc(sizeof(*mu));
7575 if (mu) {
7576 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
7577 if (mu->buf == NULL) {
7578 free(mu);
7579 mu = NULL;
7580 }
7581 }
7582 if (!mu) {
7583 while (rv) {
7584 struct mdinfo *n = rv->next;
7585
7586 free(rv);
7587 rv = n;
7588 }
7589 return NULL;
7590 }
7591
7592 mu->space = NULL;
7593 mu->space_list = NULL;
7594 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
7595 mu->next = *updates;
7596 u = (struct imsm_update_activate_spare *) mu->buf;
7597
7598 for (di = rv ; di ; di = di->next) {
7599 u->type = update_activate_spare;
7600 u->dl = (struct dl *) di->devs;
7601 di->devs = NULL;
7602 u->slot = di->disk.raid_disk;
7603 u->array = inst;
7604 u->next = u + 1;
7605 u++;
7606 }
7607 (u-1)->next = NULL;
7608 *updates = mu;
7609
7610 return rv;
7611 }
7612
7613 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
7614 {
7615 struct imsm_dev *dev = get_imsm_dev(super, idx);
7616 struct imsm_map *map = get_imsm_map(dev, MAP_0);
7617 struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
7618 struct disk_info *inf = get_disk_info(u);
7619 struct imsm_disk *disk;
7620 int i;
7621 int j;
7622
7623 for (i = 0; i < map->num_members; i++) {
7624 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
7625 for (j = 0; j < new_map->num_members; j++)
7626 if (serialcmp(disk->serial, inf[j].serial) == 0)
7627 return 1;
7628 }
7629
7630 return 0;
7631 }
7632
7633
7634 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
7635 {
7636 struct dl *dl = NULL;
7637 for (dl = super->disks; dl; dl = dl->next)
7638 if ((dl->major == major) && (dl->minor == minor))
7639 return dl;
7640 return NULL;
7641 }
7642
7643 static int remove_disk_super(struct intel_super *super, int major, int minor)
7644 {
7645 struct dl *prev = NULL;
7646 struct dl *dl;
7647
7648 prev = NULL;
7649 for (dl = super->disks; dl; dl = dl->next) {
7650 if ((dl->major == major) && (dl->minor == minor)) {
7651 /* remove */
7652 if (prev)
7653 prev->next = dl->next;
7654 else
7655 super->disks = dl->next;
7656 dl->next = NULL;
7657 __free_imsm_disk(dl);
7658 dprintf("%s: removed %x:%x\n",
7659 __func__, major, minor);
7660 break;
7661 }
7662 prev = dl;
7663 }
7664 return 0;
7665 }
7666
7667 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
7668
7669 static int add_remove_disk_update(struct intel_super *super)
7670 {
7671 int check_degraded = 0;
7672 struct dl *disk = NULL;
7673 /* add/remove some spares to/from the metadata/contrainer */
7674 while (super->disk_mgmt_list) {
7675 struct dl *disk_cfg;
7676
7677 disk_cfg = super->disk_mgmt_list;
7678 super->disk_mgmt_list = disk_cfg->next;
7679 disk_cfg->next = NULL;
7680
7681 if (disk_cfg->action == DISK_ADD) {
7682 disk_cfg->next = super->disks;
7683 super->disks = disk_cfg;
7684 check_degraded = 1;
7685 dprintf("%s: added %x:%x\n",
7686 __func__, disk_cfg->major,
7687 disk_cfg->minor);
7688 } else if (disk_cfg->action == DISK_REMOVE) {
7689 dprintf("Disk remove action processed: %x.%x\n",
7690 disk_cfg->major, disk_cfg->minor);
7691 disk = get_disk_super(super,
7692 disk_cfg->major,
7693 disk_cfg->minor);
7694 if (disk) {
7695 /* store action status */
7696 disk->action = DISK_REMOVE;
7697 /* remove spare disks only */
7698 if (disk->index == -1) {
7699 remove_disk_super(super,
7700 disk_cfg->major,
7701 disk_cfg->minor);
7702 }
7703 }
7704 /* release allocate disk structure */
7705 __free_imsm_disk(disk_cfg);
7706 }
7707 }
7708 return check_degraded;
7709 }
7710
7711
7712 static int apply_reshape_migration_update(struct imsm_update_reshape_migration *u,
7713 struct intel_super *super,
7714 void ***space_list)
7715 {
7716 struct intel_dev *id;
7717 void **tofree = NULL;
7718 int ret_val = 0;
7719
7720 dprintf("apply_reshape_migration_update()\n");
7721 if ((u->subdev < 0) ||
7722 (u->subdev > 1)) {
7723 dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
7724 return ret_val;
7725 }
7726 if ((space_list == NULL) || (*space_list == NULL)) {
7727 dprintf("imsm: Error: Memory is not allocated\n");
7728 return ret_val;
7729 }
7730
7731 for (id = super->devlist ; id; id = id->next) {
7732 if (id->index == (unsigned)u->subdev) {
7733 struct imsm_dev *dev = get_imsm_dev(super, u->subdev);
7734 struct imsm_map *map;
7735 struct imsm_dev *new_dev =
7736 (struct imsm_dev *)*space_list;
7737 struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
7738 int to_state;
7739 struct dl *new_disk;
7740
7741 if (new_dev == NULL)
7742 return ret_val;
7743 *space_list = **space_list;
7744 memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
7745 map = get_imsm_map(new_dev, MAP_0);
7746 if (migr_map) {
7747 dprintf("imsm: Error: migration in progress");
7748 return ret_val;
7749 }
7750
7751 to_state = map->map_state;
7752 if ((u->new_level == 5) && (map->raid_level == 0)) {
7753 map->num_members++;
7754 /* this should not happen */
7755 if (u->new_disks[0] < 0) {
7756 map->failed_disk_num =
7757 map->num_members - 1;
7758 to_state = IMSM_T_STATE_DEGRADED;
7759 } else
7760 to_state = IMSM_T_STATE_NORMAL;
7761 }
7762 migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
7763 if (u->new_level > -1)
7764 map->raid_level = u->new_level;
7765 migr_map = get_imsm_map(new_dev, MAP_1);
7766 if ((u->new_level == 5) &&
7767 (migr_map->raid_level == 0)) {
7768 int ord = map->num_members - 1;
7769 migr_map->num_members--;
7770 if (u->new_disks[0] < 0)
7771 ord |= IMSM_ORD_REBUILD;
7772 set_imsm_ord_tbl_ent(map,
7773 map->num_members - 1,
7774 ord);
7775 }
7776 id->dev = new_dev;
7777 tofree = (void **)dev;
7778
7779 /* update chunk size
7780 */
7781 if (u->new_chunksize > 0)
7782 map->blocks_per_strip =
7783 __cpu_to_le16(u->new_chunksize * 2);
7784
7785 /* add disk
7786 */
7787 if ((u->new_level != 5) ||
7788 (migr_map->raid_level != 0) ||
7789 (migr_map->raid_level == map->raid_level))
7790 goto skip_disk_add;
7791
7792 if (u->new_disks[0] >= 0) {
7793 /* use passes spare
7794 */
7795 new_disk = get_disk_super(super,
7796 major(u->new_disks[0]),
7797 minor(u->new_disks[0]));
7798 dprintf("imsm: new disk for reshape is: %i:%i "
7799 "(%p, index = %i)\n",
7800 major(u->new_disks[0]),
7801 minor(u->new_disks[0]),
7802 new_disk, new_disk->index);
7803 if (new_disk == NULL)
7804 goto error_disk_add;
7805
7806 new_disk->index = map->num_members - 1;
7807 /* slot to fill in autolayout
7808 */
7809 new_disk->raiddisk = new_disk->index;
7810 new_disk->disk.status |= CONFIGURED_DISK;
7811 new_disk->disk.status &= ~SPARE_DISK;
7812 } else
7813 goto error_disk_add;
7814
7815 skip_disk_add:
7816 *tofree = *space_list;
7817 /* calculate new size
7818 */
7819 imsm_set_array_size(new_dev);
7820
7821 ret_val = 1;
7822 }
7823 }
7824
7825 if (tofree)
7826 *space_list = tofree;
7827 return ret_val;
7828
7829 error_disk_add:
7830 dprintf("Error: imsm: Cannot find disk.\n");
7831 return ret_val;
7832 }
7833
7834 static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
7835 struct intel_super *super,
7836 struct active_array *active_array)
7837 {
7838 struct imsm_super *mpb = super->anchor;
7839 struct imsm_dev *dev = get_imsm_dev(super, u->array);
7840 struct imsm_map *map = get_imsm_map(dev, MAP_0);
7841 struct imsm_map *migr_map;
7842 struct active_array *a;
7843 struct imsm_disk *disk;
7844 __u8 to_state;
7845 struct dl *dl;
7846 unsigned int found;
7847 int failed;
7848 int victim;
7849 int i;
7850 int second_map_created = 0;
7851
7852 for (; u; u = u->next) {
7853 victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
7854
7855 if (victim < 0)
7856 return 0;
7857
7858 for (dl = super->disks; dl; dl = dl->next)
7859 if (dl == u->dl)
7860 break;
7861
7862 if (!dl) {
7863 fprintf(stderr, "error: imsm_activate_spare passed "
7864 "an unknown disk (index: %d)\n",
7865 u->dl->index);
7866 return 0;
7867 }
7868
7869 /* count failures (excluding rebuilds and the victim)
7870 * to determine map[0] state
7871 */
7872 failed = 0;
7873 for (i = 0; i < map->num_members; i++) {
7874 if (i == u->slot)
7875 continue;
7876 disk = get_imsm_disk(super,
7877 get_imsm_disk_idx(dev, i, MAP_X));
7878 if (!disk || is_failed(disk))
7879 failed++;
7880 }
7881
7882 /* adding a pristine spare, assign a new index */
7883 if (dl->index < 0) {
7884 dl->index = super->anchor->num_disks;
7885 super->anchor->num_disks++;
7886 }
7887 disk = &dl->disk;
7888 disk->status |= CONFIGURED_DISK;
7889 disk->status &= ~SPARE_DISK;
7890
7891 /* mark rebuild */
7892 to_state = imsm_check_degraded(super, dev, failed, MAP_0);
7893 if (!second_map_created) {
7894 second_map_created = 1;
7895 map->map_state = IMSM_T_STATE_DEGRADED;
7896 migrate(dev, super, to_state, MIGR_REBUILD);
7897 } else
7898 map->map_state = to_state;
7899 migr_map = get_imsm_map(dev, MAP_1);
7900 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
7901 set_imsm_ord_tbl_ent(migr_map, u->slot,
7902 dl->index | IMSM_ORD_REBUILD);
7903
7904 /* update the family_num to mark a new container
7905 * generation, being careful to record the existing
7906 * family_num in orig_family_num to clean up after
7907 * earlier mdadm versions that neglected to set it.
7908 */
7909 if (mpb->orig_family_num == 0)
7910 mpb->orig_family_num = mpb->family_num;
7911 mpb->family_num += super->random;
7912
7913 /* count arrays using the victim in the metadata */
7914 found = 0;
7915 for (a = active_array; a ; a = a->next) {
7916 dev = get_imsm_dev(super, a->info.container_member);
7917 map = get_imsm_map(dev, MAP_0);
7918
7919 if (get_imsm_disk_slot(map, victim) >= 0)
7920 found++;
7921 }
7922
7923 /* delete the victim if it is no longer being
7924 * utilized anywhere
7925 */
7926 if (!found) {
7927 struct dl **dlp;
7928
7929 /* We know that 'manager' isn't touching anything,
7930 * so it is safe to delete
7931 */
7932 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
7933 if ((*dlp)->index == victim)
7934 break;
7935
7936 /* victim may be on the missing list */
7937 if (!*dlp)
7938 for (dlp = &super->missing; *dlp;
7939 dlp = &(*dlp)->next)
7940 if ((*dlp)->index == victim)
7941 break;
7942 imsm_delete(super, dlp, victim);
7943 }
7944 }
7945
7946 return 1;
7947 }
7948
7949 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
7950 struct intel_super *super,
7951 void ***space_list)
7952 {
7953 struct dl *new_disk;
7954 struct intel_dev *id;
7955 int i;
7956 int delta_disks = u->new_raid_disks - u->old_raid_disks;
7957 int disk_count = u->old_raid_disks;
7958 void **tofree = NULL;
7959 int devices_to_reshape = 1;
7960 struct imsm_super *mpb = super->anchor;
7961 int ret_val = 0;
7962 unsigned int dev_id;
7963
7964 dprintf("imsm: apply_reshape_container_disks_update()\n");
7965
7966 /* enable spares to use in array */
7967 for (i = 0; i < delta_disks; i++) {
7968 new_disk = get_disk_super(super,
7969 major(u->new_disks[i]),
7970 minor(u->new_disks[i]));
7971 dprintf("imsm: new disk for reshape is: %i:%i "
7972 "(%p, index = %i)\n",
7973 major(u->new_disks[i]), minor(u->new_disks[i]),
7974 new_disk, new_disk->index);
7975 if ((new_disk == NULL) ||
7976 ((new_disk->index >= 0) &&
7977 (new_disk->index < u->old_raid_disks)))
7978 goto update_reshape_exit;
7979 new_disk->index = disk_count++;
7980 /* slot to fill in autolayout
7981 */
7982 new_disk->raiddisk = new_disk->index;
7983 new_disk->disk.status |=
7984 CONFIGURED_DISK;
7985 new_disk->disk.status &= ~SPARE_DISK;
7986 }
7987
7988 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
7989 mpb->num_raid_devs);
7990 /* manage changes in volume
7991 */
7992 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
7993 void **sp = *space_list;
7994 struct imsm_dev *newdev;
7995 struct imsm_map *newmap, *oldmap;
7996
7997 for (id = super->devlist ; id; id = id->next) {
7998 if (id->index == dev_id)
7999 break;
8000 }
8001 if (id == NULL)
8002 break;
8003 if (!sp)
8004 continue;
8005 *space_list = *sp;
8006 newdev = (void*)sp;
8007 /* Copy the dev, but not (all of) the map */
8008 memcpy(newdev, id->dev, sizeof(*newdev));
8009 oldmap = get_imsm_map(id->dev, MAP_0);
8010 newmap = get_imsm_map(newdev, MAP_0);
8011 /* Copy the current map */
8012 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
8013 /* update one device only
8014 */
8015 if (devices_to_reshape) {
8016 dprintf("imsm: modifying subdev: %i\n",
8017 id->index);
8018 devices_to_reshape--;
8019 newdev->vol.migr_state = 1;
8020 newdev->vol.curr_migr_unit = 0;
8021 set_migr_type(newdev, MIGR_GEN_MIGR);
8022 newmap->num_members = u->new_raid_disks;
8023 for (i = 0; i < delta_disks; i++) {
8024 set_imsm_ord_tbl_ent(newmap,
8025 u->old_raid_disks + i,
8026 u->old_raid_disks + i);
8027 }
8028 /* New map is correct, now need to save old map
8029 */
8030 newmap = get_imsm_map(newdev, MAP_1);
8031 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
8032
8033 imsm_set_array_size(newdev);
8034 }
8035
8036 sp = (void **)id->dev;
8037 id->dev = newdev;
8038 *sp = tofree;
8039 tofree = sp;
8040
8041 /* Clear migration record */
8042 memset(super->migr_rec, 0, sizeof(struct migr_record));
8043 }
8044 if (tofree)
8045 *space_list = tofree;
8046 ret_val = 1;
8047
8048 update_reshape_exit:
8049
8050 return ret_val;
8051 }
8052
8053 static int apply_takeover_update(struct imsm_update_takeover *u,
8054 struct intel_super *super,
8055 void ***space_list)
8056 {
8057 struct imsm_dev *dev = NULL;
8058 struct intel_dev *dv;
8059 struct imsm_dev *dev_new;
8060 struct imsm_map *map;
8061 struct dl *dm, *du;
8062 int i;
8063
8064 for (dv = super->devlist; dv; dv = dv->next)
8065 if (dv->index == (unsigned int)u->subarray) {
8066 dev = dv->dev;
8067 break;
8068 }
8069
8070 if (dev == NULL)
8071 return 0;
8072
8073 map = get_imsm_map(dev, MAP_0);
8074
8075 if (u->direction == R10_TO_R0) {
8076 /* Number of failed disks must be half of initial disk number */
8077 if (imsm_count_failed(super, dev, MAP_0) !=
8078 (map->num_members / 2))
8079 return 0;
8080
8081 /* iterate through devices to mark removed disks as spare */
8082 for (dm = super->disks; dm; dm = dm->next) {
8083 if (dm->disk.status & FAILED_DISK) {
8084 int idx = dm->index;
8085 /* update indexes on the disk list */
8086 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8087 the index values will end up being correct.... NB */
8088 for (du = super->disks; du; du = du->next)
8089 if (du->index > idx)
8090 du->index--;
8091 /* mark as spare disk */
8092 mark_spare(dm);
8093 }
8094 }
8095 /* update map */
8096 map->num_members = map->num_members / 2;
8097 map->map_state = IMSM_T_STATE_NORMAL;
8098 map->num_domains = 1;
8099 map->raid_level = 0;
8100 map->failed_disk_num = -1;
8101 }
8102
8103 if (u->direction == R0_TO_R10) {
8104 void **space;
8105 /* update slots in current disk list */
8106 for (dm = super->disks; dm; dm = dm->next) {
8107 if (dm->index >= 0)
8108 dm->index *= 2;
8109 }
8110 /* create new *missing* disks */
8111 for (i = 0; i < map->num_members; i++) {
8112 space = *space_list;
8113 if (!space)
8114 continue;
8115 *space_list = *space;
8116 du = (void *)space;
8117 memcpy(du, super->disks, sizeof(*du));
8118 du->fd = -1;
8119 du->minor = 0;
8120 du->major = 0;
8121 du->index = (i * 2) + 1;
8122 sprintf((char *)du->disk.serial,
8123 " MISSING_%d", du->index);
8124 sprintf((char *)du->serial,
8125 "MISSING_%d", du->index);
8126 du->next = super->missing;
8127 super->missing = du;
8128 }
8129 /* create new dev and map */
8130 space = *space_list;
8131 if (!space)
8132 return 0;
8133 *space_list = *space;
8134 dev_new = (void *)space;
8135 memcpy(dev_new, dev, sizeof(*dev));
8136 /* update new map */
8137 map = get_imsm_map(dev_new, MAP_0);
8138 map->num_members = map->num_members * 2;
8139 map->map_state = IMSM_T_STATE_DEGRADED;
8140 map->num_domains = 2;
8141 map->raid_level = 1;
8142 /* replace dev<->dev_new */
8143 dv->dev = dev_new;
8144 }
8145 /* update disk order table */
8146 for (du = super->disks; du; du = du->next)
8147 if (du->index >= 0)
8148 set_imsm_ord_tbl_ent(map, du->index, du->index);
8149 for (du = super->missing; du; du = du->next)
8150 if (du->index >= 0) {
8151 set_imsm_ord_tbl_ent(map, du->index, du->index);
8152 mark_missing(dv->dev, &du->disk, du->index);
8153 }
8154
8155 return 1;
8156 }
8157
8158 static void imsm_process_update(struct supertype *st,
8159 struct metadata_update *update)
8160 {
8161 /**
8162 * crack open the metadata_update envelope to find the update record
8163 * update can be one of:
8164 * update_reshape_container_disks - all the arrays in the container
8165 * are being reshaped to have more devices. We need to mark
8166 * the arrays for general migration and convert selected spares
8167 * into active devices.
8168 * update_activate_spare - a spare device has replaced a failed
8169 * device in an array, update the disk_ord_tbl. If this disk is
8170 * present in all member arrays then also clear the SPARE_DISK
8171 * flag
8172 * update_create_array
8173 * update_kill_array
8174 * update_rename_array
8175 * update_add_remove_disk
8176 */
8177 struct intel_super *super = st->sb;
8178 struct imsm_super *mpb;
8179 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
8180
8181 /* update requires a larger buf but the allocation failed */
8182 if (super->next_len && !super->next_buf) {
8183 super->next_len = 0;
8184 return;
8185 }
8186
8187 if (super->next_buf) {
8188 memcpy(super->next_buf, super->buf, super->len);
8189 free(super->buf);
8190 super->len = super->next_len;
8191 super->buf = super->next_buf;
8192
8193 super->next_len = 0;
8194 super->next_buf = NULL;
8195 }
8196
8197 mpb = super->anchor;
8198
8199 switch (type) {
8200 case update_general_migration_checkpoint: {
8201 struct intel_dev *id;
8202 struct imsm_update_general_migration_checkpoint *u =
8203 (void *)update->buf;
8204
8205 dprintf("imsm: process_update() "
8206 "for update_general_migration_checkpoint called\n");
8207
8208 /* find device under general migration */
8209 for (id = super->devlist ; id; id = id->next) {
8210 if (is_gen_migration(id->dev)) {
8211 id->dev->vol.curr_migr_unit =
8212 __cpu_to_le32(u->curr_migr_unit);
8213 super->updates_pending++;
8214 }
8215 }
8216 break;
8217 }
8218 case update_takeover: {
8219 struct imsm_update_takeover *u = (void *)update->buf;
8220 if (apply_takeover_update(u, super, &update->space_list)) {
8221 imsm_update_version_info(super);
8222 super->updates_pending++;
8223 }
8224 break;
8225 }
8226
8227 case update_reshape_container_disks: {
8228 struct imsm_update_reshape *u = (void *)update->buf;
8229 if (apply_reshape_container_disks_update(
8230 u, super, &update->space_list))
8231 super->updates_pending++;
8232 break;
8233 }
8234 case update_reshape_migration: {
8235 struct imsm_update_reshape_migration *u = (void *)update->buf;
8236 if (apply_reshape_migration_update(
8237 u, super, &update->space_list))
8238 super->updates_pending++;
8239 break;
8240 }
8241 case update_activate_spare: {
8242 struct imsm_update_activate_spare *u = (void *) update->buf;
8243 if (apply_update_activate_spare(u, super, st->arrays))
8244 super->updates_pending++;
8245 break;
8246 }
8247 case update_create_array: {
8248 /* someone wants to create a new array, we need to be aware of
8249 * a few races/collisions:
8250 * 1/ 'Create' called by two separate instances of mdadm
8251 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8252 * devices that have since been assimilated via
8253 * activate_spare.
8254 * In the event this update can not be carried out mdadm will
8255 * (FIX ME) notice that its update did not take hold.
8256 */
8257 struct imsm_update_create_array *u = (void *) update->buf;
8258 struct intel_dev *dv;
8259 struct imsm_dev *dev;
8260 struct imsm_map *map, *new_map;
8261 unsigned long long start, end;
8262 unsigned long long new_start, new_end;
8263 int i;
8264 struct disk_info *inf;
8265 struct dl *dl;
8266
8267 /* handle racing creates: first come first serve */
8268 if (u->dev_idx < mpb->num_raid_devs) {
8269 dprintf("%s: subarray %d already defined\n",
8270 __func__, u->dev_idx);
8271 goto create_error;
8272 }
8273
8274 /* check update is next in sequence */
8275 if (u->dev_idx != mpb->num_raid_devs) {
8276 dprintf("%s: can not create array %d expected index %d\n",
8277 __func__, u->dev_idx, mpb->num_raid_devs);
8278 goto create_error;
8279 }
8280
8281 new_map = get_imsm_map(&u->dev, MAP_0);
8282 new_start = __le32_to_cpu(new_map->pba_of_lba0);
8283 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
8284 inf = get_disk_info(u);
8285
8286 /* handle activate_spare versus create race:
8287 * check to make sure that overlapping arrays do not include
8288 * overalpping disks
8289 */
8290 for (i = 0; i < mpb->num_raid_devs; i++) {
8291 dev = get_imsm_dev(super, i);
8292 map = get_imsm_map(dev, MAP_0);
8293 start = __le32_to_cpu(map->pba_of_lba0);
8294 end = start + __le32_to_cpu(map->blocks_per_member);
8295 if ((new_start >= start && new_start <= end) ||
8296 (start >= new_start && start <= new_end))
8297 /* overlap */;
8298 else
8299 continue;
8300
8301 if (disks_overlap(super, i, u)) {
8302 dprintf("%s: arrays overlap\n", __func__);
8303 goto create_error;
8304 }
8305 }
8306
8307 /* check that prepare update was successful */
8308 if (!update->space) {
8309 dprintf("%s: prepare update failed\n", __func__);
8310 goto create_error;
8311 }
8312
8313 /* check that all disks are still active before committing
8314 * changes. FIXME: could we instead handle this by creating a
8315 * degraded array? That's probably not what the user expects,
8316 * so better to drop this update on the floor.
8317 */
8318 for (i = 0; i < new_map->num_members; i++) {
8319 dl = serial_to_dl(inf[i].serial, super);
8320 if (!dl) {
8321 dprintf("%s: disk disappeared\n", __func__);
8322 goto create_error;
8323 }
8324 }
8325
8326 super->updates_pending++;
8327
8328 /* convert spares to members and fixup ord_tbl */
8329 for (i = 0; i < new_map->num_members; i++) {
8330 dl = serial_to_dl(inf[i].serial, super);
8331 if (dl->index == -1) {
8332 dl->index = mpb->num_disks;
8333 mpb->num_disks++;
8334 dl->disk.status |= CONFIGURED_DISK;
8335 dl->disk.status &= ~SPARE_DISK;
8336 }
8337 set_imsm_ord_tbl_ent(new_map, i, dl->index);
8338 }
8339
8340 dv = update->space;
8341 dev = dv->dev;
8342 update->space = NULL;
8343 imsm_copy_dev(dev, &u->dev);
8344 dv->index = u->dev_idx;
8345 dv->next = super->devlist;
8346 super->devlist = dv;
8347 mpb->num_raid_devs++;
8348
8349 imsm_update_version_info(super);
8350 break;
8351 create_error:
8352 /* mdmon knows how to release update->space, but not
8353 * ((struct intel_dev *) update->space)->dev
8354 */
8355 if (update->space) {
8356 dv = update->space;
8357 free(dv->dev);
8358 }
8359 break;
8360 }
8361 case update_kill_array: {
8362 struct imsm_update_kill_array *u = (void *) update->buf;
8363 int victim = u->dev_idx;
8364 struct active_array *a;
8365 struct intel_dev **dp;
8366 struct imsm_dev *dev;
8367
8368 /* sanity check that we are not affecting the uuid of
8369 * active arrays, or deleting an active array
8370 *
8371 * FIXME when immutable ids are available, but note that
8372 * we'll also need to fixup the invalidated/active
8373 * subarray indexes in mdstat
8374 */
8375 for (a = st->arrays; a; a = a->next)
8376 if (a->info.container_member >= victim)
8377 break;
8378 /* by definition if mdmon is running at least one array
8379 * is active in the container, so checking
8380 * mpb->num_raid_devs is just extra paranoia
8381 */
8382 dev = get_imsm_dev(super, victim);
8383 if (a || !dev || mpb->num_raid_devs == 1) {
8384 dprintf("failed to delete subarray-%d\n", victim);
8385 break;
8386 }
8387
8388 for (dp = &super->devlist; *dp;)
8389 if ((*dp)->index == (unsigned)super->current_vol) {
8390 *dp = (*dp)->next;
8391 } else {
8392 if ((*dp)->index > (unsigned)victim)
8393 (*dp)->index--;
8394 dp = &(*dp)->next;
8395 }
8396 mpb->num_raid_devs--;
8397 super->updates_pending++;
8398 break;
8399 }
8400 case update_rename_array: {
8401 struct imsm_update_rename_array *u = (void *) update->buf;
8402 char name[MAX_RAID_SERIAL_LEN+1];
8403 int target = u->dev_idx;
8404 struct active_array *a;
8405 struct imsm_dev *dev;
8406
8407 /* sanity check that we are not affecting the uuid of
8408 * an active array
8409 */
8410 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
8411 name[MAX_RAID_SERIAL_LEN] = '\0';
8412 for (a = st->arrays; a; a = a->next)
8413 if (a->info.container_member == target)
8414 break;
8415 dev = get_imsm_dev(super, u->dev_idx);
8416 if (a || !dev || !check_name(super, name, 1)) {
8417 dprintf("failed to rename subarray-%d\n", target);
8418 break;
8419 }
8420
8421 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
8422 super->updates_pending++;
8423 break;
8424 }
8425 case update_add_remove_disk: {
8426 /* we may be able to repair some arrays if disks are
8427 * being added, check teh status of add_remove_disk
8428 * if discs has been added.
8429 */
8430 if (add_remove_disk_update(super)) {
8431 struct active_array *a;
8432
8433 super->updates_pending++;
8434 for (a = st->arrays; a; a = a->next)
8435 a->check_degraded = 1;
8436 }
8437 break;
8438 }
8439 default:
8440 fprintf(stderr, "error: unsuported process update type:"
8441 "(type: %d)\n", type);
8442 }
8443 }
8444
8445 static struct mdinfo *get_spares_for_grow(struct supertype *st);
8446
8447 static void imsm_prepare_update(struct supertype *st,
8448 struct metadata_update *update)
8449 {
8450 /**
8451 * Allocate space to hold new disk entries, raid-device entries or a new
8452 * mpb if necessary. The manager synchronously waits for updates to
8453 * complete in the monitor, so new mpb buffers allocated here can be
8454 * integrated by the monitor thread without worrying about live pointers
8455 * in the manager thread.
8456 */
8457 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
8458 struct intel_super *super = st->sb;
8459 struct imsm_super *mpb = super->anchor;
8460 size_t buf_len;
8461 size_t len = 0;
8462
8463 switch (type) {
8464 case update_general_migration_checkpoint:
8465 dprintf("imsm: prepare_update() "
8466 "for update_general_migration_checkpoint called\n");
8467 break;
8468 case update_takeover: {
8469 struct imsm_update_takeover *u = (void *)update->buf;
8470 if (u->direction == R0_TO_R10) {
8471 void **tail = (void **)&update->space_list;
8472 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
8473 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8474 int num_members = map->num_members;
8475 void *space;
8476 int size, i;
8477 int err = 0;
8478 /* allocate memory for added disks */
8479 for (i = 0; i < num_members; i++) {
8480 size = sizeof(struct dl);
8481 space = malloc(size);
8482 if (!space) {
8483 err++;
8484 break;
8485 }
8486 *tail = space;
8487 tail = space;
8488 *tail = NULL;
8489 }
8490 /* allocate memory for new device */
8491 size = sizeof_imsm_dev(super->devlist->dev, 0) +
8492 (num_members * sizeof(__u32));
8493 space = malloc(size);
8494 if (!space)
8495 err++;
8496 else {
8497 *tail = space;
8498 tail = space;
8499 *tail = NULL;
8500 }
8501 if (!err) {
8502 len = disks_to_mpb_size(num_members * 2);
8503 } else {
8504 /* if allocation didn't success, free buffer */
8505 while (update->space_list) {
8506 void **sp = update->space_list;
8507 update->space_list = *sp;
8508 free(sp);
8509 }
8510 }
8511 }
8512
8513 break;
8514 }
8515 case update_reshape_container_disks: {
8516 /* Every raid device in the container is about to
8517 * gain some more devices, and we will enter a
8518 * reconfiguration.
8519 * So each 'imsm_map' will be bigger, and the imsm_vol
8520 * will now hold 2 of them.
8521 * Thus we need new 'struct imsm_dev' allocations sized
8522 * as sizeof_imsm_dev but with more devices in both maps.
8523 */
8524 struct imsm_update_reshape *u = (void *)update->buf;
8525 struct intel_dev *dl;
8526 void **space_tail = (void**)&update->space_list;
8527
8528 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8529
8530 for (dl = super->devlist; dl; dl = dl->next) {
8531 int size = sizeof_imsm_dev(dl->dev, 1);
8532 void *s;
8533 if (u->new_raid_disks > u->old_raid_disks)
8534 size += sizeof(__u32)*2*
8535 (u->new_raid_disks - u->old_raid_disks);
8536 s = malloc(size);
8537 if (!s)
8538 break;
8539 *space_tail = s;
8540 space_tail = s;
8541 *space_tail = NULL;
8542 }
8543
8544 len = disks_to_mpb_size(u->new_raid_disks);
8545 dprintf("New anchor length is %llu\n", (unsigned long long)len);
8546 break;
8547 }
8548 case update_reshape_migration: {
8549 /* for migration level 0->5 we need to add disks
8550 * so the same as for container operation we will copy
8551 * device to the bigger location.
8552 * in memory prepared device and new disk area are prepared
8553 * for usage in process update
8554 */
8555 struct imsm_update_reshape_migration *u = (void *)update->buf;
8556 struct intel_dev *id;
8557 void **space_tail = (void **)&update->space_list;
8558 int size;
8559 void *s;
8560 int current_level = -1;
8561
8562 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8563
8564 /* add space for bigger array in update
8565 */
8566 for (id = super->devlist; id; id = id->next) {
8567 if (id->index == (unsigned)u->subdev) {
8568 size = sizeof_imsm_dev(id->dev, 1);
8569 if (u->new_raid_disks > u->old_raid_disks)
8570 size += sizeof(__u32)*2*
8571 (u->new_raid_disks - u->old_raid_disks);
8572 s = malloc(size);
8573 if (!s)
8574 break;
8575 *space_tail = s;
8576 space_tail = s;
8577 *space_tail = NULL;
8578 break;
8579 }
8580 }
8581 if (update->space_list == NULL)
8582 break;
8583
8584 /* add space for disk in update
8585 */
8586 size = sizeof(struct dl);
8587 s = malloc(size);
8588 if (!s) {
8589 free(update->space_list);
8590 update->space_list = NULL;
8591 break;
8592 }
8593 *space_tail = s;
8594 space_tail = s;
8595 *space_tail = NULL;
8596
8597 /* add spare device to update
8598 */
8599 for (id = super->devlist ; id; id = id->next)
8600 if (id->index == (unsigned)u->subdev) {
8601 struct imsm_dev *dev;
8602 struct imsm_map *map;
8603
8604 dev = get_imsm_dev(super, u->subdev);
8605 map = get_imsm_map(dev, MAP_0);
8606 current_level = map->raid_level;
8607 break;
8608 }
8609 if ((u->new_level == 5) && (u->new_level != current_level)) {
8610 struct mdinfo *spares;
8611
8612 spares = get_spares_for_grow(st);
8613 if (spares) {
8614 struct dl *dl;
8615 struct mdinfo *dev;
8616
8617 dev = spares->devs;
8618 if (dev) {
8619 u->new_disks[0] =
8620 makedev(dev->disk.major,
8621 dev->disk.minor);
8622 dl = get_disk_super(super,
8623 dev->disk.major,
8624 dev->disk.minor);
8625 dl->index = u->old_raid_disks;
8626 dev = dev->next;
8627 }
8628 sysfs_free(spares);
8629 }
8630 }
8631 len = disks_to_mpb_size(u->new_raid_disks);
8632 dprintf("New anchor length is %llu\n", (unsigned long long)len);
8633 break;
8634 }
8635 case update_create_array: {
8636 struct imsm_update_create_array *u = (void *) update->buf;
8637 struct intel_dev *dv;
8638 struct imsm_dev *dev = &u->dev;
8639 struct imsm_map *map = get_imsm_map(dev, MAP_0);
8640 struct dl *dl;
8641 struct disk_info *inf;
8642 int i;
8643 int activate = 0;
8644
8645 inf = get_disk_info(u);
8646 len = sizeof_imsm_dev(dev, 1);
8647 /* allocate a new super->devlist entry */
8648 dv = malloc(sizeof(*dv));
8649 if (dv) {
8650 dv->dev = malloc(len);
8651 if (dv->dev)
8652 update->space = dv;
8653 else {
8654 free(dv);
8655 update->space = NULL;
8656 }
8657 }
8658
8659 /* count how many spares will be converted to members */
8660 for (i = 0; i < map->num_members; i++) {
8661 dl = serial_to_dl(inf[i].serial, super);
8662 if (!dl) {
8663 /* hmm maybe it failed?, nothing we can do about
8664 * it here
8665 */
8666 continue;
8667 }
8668 if (count_memberships(dl, super) == 0)
8669 activate++;
8670 }
8671 len += activate * sizeof(struct imsm_disk);
8672 break;
8673 default:
8674 break;
8675 }
8676 }
8677
8678 /* check if we need a larger metadata buffer */
8679 if (super->next_buf)
8680 buf_len = super->next_len;
8681 else
8682 buf_len = super->len;
8683
8684 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
8685 /* ok we need a larger buf than what is currently allocated
8686 * if this allocation fails process_update will notice that
8687 * ->next_len is set and ->next_buf is NULL
8688 */
8689 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
8690 if (super->next_buf)
8691 free(super->next_buf);
8692
8693 super->next_len = buf_len;
8694 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
8695 memset(super->next_buf, 0, buf_len);
8696 else
8697 super->next_buf = NULL;
8698 }
8699 }
8700
8701 /* must be called while manager is quiesced */
8702 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
8703 {
8704 struct imsm_super *mpb = super->anchor;
8705 struct dl *iter;
8706 struct imsm_dev *dev;
8707 struct imsm_map *map;
8708 int i, j, num_members;
8709 __u32 ord;
8710
8711 dprintf("%s: deleting device[%d] from imsm_super\n",
8712 __func__, index);
8713
8714 /* shift all indexes down one */
8715 for (iter = super->disks; iter; iter = iter->next)
8716 if (iter->index > (int)index)
8717 iter->index--;
8718 for (iter = super->missing; iter; iter = iter->next)
8719 if (iter->index > (int)index)
8720 iter->index--;
8721
8722 for (i = 0; i < mpb->num_raid_devs; i++) {
8723 dev = get_imsm_dev(super, i);
8724 map = get_imsm_map(dev, MAP_0);
8725 num_members = map->num_members;
8726 for (j = 0; j < num_members; j++) {
8727 /* update ord entries being careful not to propagate
8728 * ord-flags to the first map
8729 */
8730 ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
8731
8732 if (ord_to_idx(ord) <= index)
8733 continue;
8734
8735 map = get_imsm_map(dev, MAP_0);
8736 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
8737 map = get_imsm_map(dev, MAP_1);
8738 if (map)
8739 set_imsm_ord_tbl_ent(map, j, ord - 1);
8740 }
8741 }
8742
8743 mpb->num_disks--;
8744 super->updates_pending++;
8745 if (*dlp) {
8746 struct dl *dl = *dlp;
8747
8748 *dlp = (*dlp)->next;
8749 __free_imsm_disk(dl);
8750 }
8751 }
8752 #endif /* MDASSEMBLE */
8753
8754 static void close_targets(int *targets, int new_disks)
8755 {
8756 int i;
8757
8758 if (!targets)
8759 return;
8760
8761 for (i = 0; i < new_disks; i++) {
8762 if (targets[i] >= 0) {
8763 close(targets[i]);
8764 targets[i] = -1;
8765 }
8766 }
8767 }
8768
8769 static int imsm_get_allowed_degradation(int level, int raid_disks,
8770 struct intel_super *super,
8771 struct imsm_dev *dev)
8772 {
8773 switch (level) {
8774 case 1:
8775 case 10:{
8776 int ret_val = 0;
8777 struct imsm_map *map;
8778 int i;
8779
8780 ret_val = raid_disks/2;
8781 /* check map if all disks pairs not failed
8782 * in both maps
8783 */
8784 map = get_imsm_map(dev, MAP_0);
8785 for (i = 0; i < ret_val; i++) {
8786 int degradation = 0;
8787 if (get_imsm_disk(super, i) == NULL)
8788 degradation++;
8789 if (get_imsm_disk(super, i + 1) == NULL)
8790 degradation++;
8791 if (degradation == 2)
8792 return 0;
8793 }
8794 map = get_imsm_map(dev, MAP_1);
8795 /* if there is no second map
8796 * result can be returned
8797 */
8798 if (map == NULL)
8799 return ret_val;
8800 /* check degradation in second map
8801 */
8802 for (i = 0; i < ret_val; i++) {
8803 int degradation = 0;
8804 if (get_imsm_disk(super, i) == NULL)
8805 degradation++;
8806 if (get_imsm_disk(super, i + 1) == NULL)
8807 degradation++;
8808 if (degradation == 2)
8809 return 0;
8810 }
8811 return ret_val;
8812 }
8813 case 5:
8814 return 1;
8815 case 6:
8816 return 2;
8817 default:
8818 return 0;
8819 }
8820 }
8821
8822
8823 /*******************************************************************************
8824 * Function: open_backup_targets
8825 * Description: Function opens file descriptors for all devices given in
8826 * info->devs
8827 * Parameters:
8828 * info : general array info
8829 * raid_disks : number of disks
8830 * raid_fds : table of device's file descriptors
8831 * super : intel super for raid10 degradation check
8832 * dev : intel device for raid10 degradation check
8833 * Returns:
8834 * 0 : success
8835 * -1 : fail
8836 ******************************************************************************/
8837 int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds,
8838 struct intel_super *super, struct imsm_dev *dev)
8839 {
8840 struct mdinfo *sd;
8841 int i;
8842 int opened = 0;
8843
8844 for (i = 0; i < raid_disks; i++)
8845 raid_fds[i] = -1;
8846
8847 for (sd = info->devs ; sd ; sd = sd->next) {
8848 char *dn;
8849
8850 if (sd->disk.state & (1<<MD_DISK_FAULTY)) {
8851 dprintf("disk is faulty!!\n");
8852 continue;
8853 }
8854
8855 if ((sd->disk.raid_disk >= raid_disks) ||
8856 (sd->disk.raid_disk < 0))
8857 continue;
8858
8859 dn = map_dev(sd->disk.major,
8860 sd->disk.minor, 1);
8861 raid_fds[sd->disk.raid_disk] = dev_open(dn, O_RDWR);
8862 if (raid_fds[sd->disk.raid_disk] < 0) {
8863 fprintf(stderr, "cannot open component\n");
8864 continue;
8865 }
8866 opened++;
8867 }
8868 /* check if maximum array degradation level is not exceeded
8869 */
8870 if ((raid_disks - opened) >
8871 imsm_get_allowed_degradation(info->new_level,
8872 raid_disks,
8873 super, dev)) {
8874 fprintf(stderr, "Not enough disks can be opened.\n");
8875 close_targets(raid_fds, raid_disks);
8876 return -2;
8877 }
8878 return 0;
8879 }
8880
8881 #ifndef MDASSEMBLE
8882 /*******************************************************************************
8883 * Function: init_migr_record_imsm
8884 * Description: Function inits imsm migration record
8885 * Parameters:
8886 * super : imsm internal array info
8887 * dev : device under migration
8888 * info : general array info to find the smallest device
8889 * Returns:
8890 * none
8891 ******************************************************************************/
8892 void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
8893 struct mdinfo *info)
8894 {
8895 struct intel_super *super = st->sb;
8896 struct migr_record *migr_rec = super->migr_rec;
8897 int new_data_disks;
8898 unsigned long long dsize, dev_sectors;
8899 long long unsigned min_dev_sectors = -1LLU;
8900 struct mdinfo *sd;
8901 char nm[30];
8902 int fd;
8903 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
8904 struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
8905 unsigned long long num_migr_units;
8906 unsigned long long array_blocks;
8907
8908 memset(migr_rec, 0, sizeof(struct migr_record));
8909 migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
8910
8911 /* only ascending reshape supported now */
8912 migr_rec->ascending_migr = __cpu_to_le32(1);
8913
8914 migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
8915 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
8916 migr_rec->dest_depth_per_unit *=
8917 max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
8918 new_data_disks = imsm_num_data_members(dev, MAP_0);
8919 migr_rec->blocks_per_unit =
8920 __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
8921 migr_rec->dest_depth_per_unit =
8922 __cpu_to_le32(migr_rec->dest_depth_per_unit);
8923 array_blocks = info->component_size * new_data_disks;
8924 num_migr_units =
8925 array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
8926
8927 if (array_blocks % __le32_to_cpu(migr_rec->blocks_per_unit))
8928 num_migr_units++;
8929 migr_rec->num_migr_units = __cpu_to_le32(num_migr_units);
8930
8931 migr_rec->post_migr_vol_cap = dev->size_low;
8932 migr_rec->post_migr_vol_cap_hi = dev->size_high;
8933
8934
8935 /* Find the smallest dev */
8936 for (sd = info->devs ; sd ; sd = sd->next) {
8937 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
8938 fd = dev_open(nm, O_RDONLY);
8939 if (fd < 0)
8940 continue;
8941 get_dev_size(fd, NULL, &dsize);
8942 dev_sectors = dsize / 512;
8943 if (dev_sectors < min_dev_sectors)
8944 min_dev_sectors = dev_sectors;
8945 close(fd);
8946 }
8947 migr_rec->ckpt_area_pba = __cpu_to_le32(min_dev_sectors -
8948 RAID_DISK_RESERVED_BLOCKS_IMSM_HI);
8949
8950 write_imsm_migr_rec(st);
8951
8952 return;
8953 }
8954
8955 /*******************************************************************************
8956 * Function: save_backup_imsm
8957 * Description: Function saves critical data stripes to Migration Copy Area
8958 * and updates the current migration unit status.
8959 * Use restore_stripes() to form a destination stripe,
8960 * and to write it to the Copy Area.
8961 * Parameters:
8962 * st : supertype information
8963 * dev : imsm device that backup is saved for
8964 * info : general array info
8965 * buf : input buffer
8966 * length : length of data to backup (blocks_per_unit)
8967 * Returns:
8968 * 0 : success
8969 *, -1 : fail
8970 ******************************************************************************/
8971 int save_backup_imsm(struct supertype *st,
8972 struct imsm_dev *dev,
8973 struct mdinfo *info,
8974 void *buf,
8975 int length)
8976 {
8977 int rv = -1;
8978 struct intel_super *super = st->sb;
8979 unsigned long long *target_offsets = NULL;
8980 int *targets = NULL;
8981 int i;
8982 struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
8983 int new_disks = map_dest->num_members;
8984 int dest_layout = 0;
8985 int dest_chunk;
8986 unsigned long long start;
8987 int data_disks = imsm_num_data_members(dev, MAP_0);
8988
8989 targets = malloc(new_disks * sizeof(int));
8990 if (!targets)
8991 goto abort;
8992
8993 for (i = 0; i < new_disks; i++)
8994 targets[i] = -1;
8995
8996 target_offsets = malloc(new_disks * sizeof(unsigned long long));
8997 if (!target_offsets)
8998 goto abort;
8999
9000 start = info->reshape_progress * 512;
9001 for (i = 0; i < new_disks; i++) {
9002 target_offsets[i] = (unsigned long long)
9003 __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512;
9004 /* move back copy area adderss, it will be moved forward
9005 * in restore_stripes() using start input variable
9006 */
9007 target_offsets[i] -= start/data_disks;
9008 }
9009
9010 if (open_backup_targets(info, new_disks, targets,
9011 super, dev))
9012 goto abort;
9013
9014 dest_layout = imsm_level_to_layout(map_dest->raid_level);
9015 dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
9016
9017 if (restore_stripes(targets, /* list of dest devices */
9018 target_offsets, /* migration record offsets */
9019 new_disks,
9020 dest_chunk,
9021 map_dest->raid_level,
9022 dest_layout,
9023 -1, /* source backup file descriptor */
9024 0, /* input buf offset
9025 * always 0 buf is already offseted */
9026 start,
9027 length,
9028 buf) != 0) {
9029 fprintf(stderr, Name ": Error restoring stripes\n");
9030 goto abort;
9031 }
9032
9033 rv = 0;
9034
9035 abort:
9036 if (targets) {
9037 close_targets(targets, new_disks);
9038 free(targets);
9039 }
9040 free(target_offsets);
9041
9042 return rv;
9043 }
9044
9045 /*******************************************************************************
9046 * Function: save_checkpoint_imsm
9047 * Description: Function called for current unit status update
9048 * in the migration record. It writes it to disk.
9049 * Parameters:
9050 * super : imsm internal array info
9051 * info : general array info
9052 * Returns:
9053 * 0: success
9054 * 1: failure
9055 * 2: failure, means no valid migration record
9056 * / no general migration in progress /
9057 ******************************************************************************/
9058 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
9059 {
9060 struct intel_super *super = st->sb;
9061 unsigned long long blocks_per_unit;
9062 unsigned long long curr_migr_unit;
9063
9064 if (load_imsm_migr_rec(super, info) != 0) {
9065 dprintf("imsm: ERROR: Cannot read migration record "
9066 "for checkpoint save.\n");
9067 return 1;
9068 }
9069
9070 blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
9071 if (blocks_per_unit == 0) {
9072 dprintf("imsm: no migration in progress.\n");
9073 return 2;
9074 }
9075 curr_migr_unit = info->reshape_progress / blocks_per_unit;
9076 /* check if array is alligned to copy area
9077 * if it is not alligned, add one to current migration unit value
9078 * this can happend on array reshape finish only
9079 */
9080 if (info->reshape_progress % blocks_per_unit)
9081 curr_migr_unit++;
9082
9083 super->migr_rec->curr_migr_unit =
9084 __cpu_to_le32(curr_migr_unit);
9085 super->migr_rec->rec_status = __cpu_to_le32(state);
9086 super->migr_rec->dest_1st_member_lba =
9087 __cpu_to_le32(curr_migr_unit *
9088 __le32_to_cpu(super->migr_rec->dest_depth_per_unit));
9089 if (write_imsm_migr_rec(st) < 0) {
9090 dprintf("imsm: Cannot write migration record "
9091 "outside backup area\n");
9092 return 1;
9093 }
9094
9095 return 0;
9096 }
9097
9098 /*******************************************************************************
9099 * Function: recover_backup_imsm
9100 * Description: Function recovers critical data from the Migration Copy Area
9101 * while assembling an array.
9102 * Parameters:
9103 * super : imsm internal array info
9104 * info : general array info
9105 * Returns:
9106 * 0 : success (or there is no data to recover)
9107 * 1 : fail
9108 ******************************************************************************/
9109 int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
9110 {
9111 struct intel_super *super = st->sb;
9112 struct migr_record *migr_rec = super->migr_rec;
9113 struct imsm_map *map_dest = NULL;
9114 struct intel_dev *id = NULL;
9115 unsigned long long read_offset;
9116 unsigned long long write_offset;
9117 unsigned unit_len;
9118 int *targets = NULL;
9119 int new_disks, i, err;
9120 char *buf = NULL;
9121 int retval = 1;
9122 unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit);
9123 unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units);
9124 char buffer[20];
9125 int skipped_disks = 0;
9126
9127 err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
9128 if (err < 1)
9129 return 1;
9130
9131 /* recover data only during assemblation */
9132 if (strncmp(buffer, "inactive", 8) != 0)
9133 return 0;
9134 /* no data to recover */
9135 if (__le32_to_cpu(migr_rec->rec_status) == UNIT_SRC_NORMAL)
9136 return 0;
9137 if (curr_migr_unit >= num_migr_units)
9138 return 1;
9139
9140 /* find device during reshape */
9141 for (id = super->devlist; id; id = id->next)
9142 if (is_gen_migration(id->dev))
9143 break;
9144 if (id == NULL)
9145 return 1;
9146
9147 map_dest = get_imsm_map(id->dev, MAP_0);
9148 new_disks = map_dest->num_members;
9149
9150 read_offset = (unsigned long long)
9151 __le32_to_cpu(migr_rec->ckpt_area_pba) * 512;
9152
9153 write_offset = ((unsigned long long)
9154 __le32_to_cpu(migr_rec->dest_1st_member_lba) +
9155 __le32_to_cpu(map_dest->pba_of_lba0)) * 512;
9156
9157 unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
9158 if (posix_memalign((void **)&buf, 512, unit_len) != 0)
9159 goto abort;
9160 targets = malloc(new_disks * sizeof(int));
9161 if (!targets)
9162 goto abort;
9163
9164 if (open_backup_targets(info, new_disks, targets, super, id->dev)) {
9165 fprintf(stderr,
9166 Name ": Cannot open some devices belonging to array.\n");
9167 goto abort;
9168 }
9169
9170 for (i = 0; i < new_disks; i++) {
9171 if (targets[i] < 0) {
9172 skipped_disks++;
9173 continue;
9174 }
9175 if (lseek64(targets[i], read_offset, SEEK_SET) < 0) {
9176 fprintf(stderr,
9177 Name ": Cannot seek to block: %s\n",
9178 strerror(errno));
9179 skipped_disks++;
9180 continue;
9181 }
9182 if ((unsigned)read(targets[i], buf, unit_len) != unit_len) {
9183 fprintf(stderr,
9184 Name ": Cannot read copy area block: %s\n",
9185 strerror(errno));
9186 skipped_disks++;
9187 continue;
9188 }
9189 if (lseek64(targets[i], write_offset, SEEK_SET) < 0) {
9190 fprintf(stderr,
9191 Name ": Cannot seek to block: %s\n",
9192 strerror(errno));
9193 skipped_disks++;
9194 continue;
9195 }
9196 if ((unsigned)write(targets[i], buf, unit_len) != unit_len) {
9197 fprintf(stderr,
9198 Name ": Cannot restore block: %s\n",
9199 strerror(errno));
9200 skipped_disks++;
9201 continue;
9202 }
9203 }
9204
9205 if (skipped_disks > imsm_get_allowed_degradation(info->new_level,
9206 new_disks,
9207 super,
9208 id->dev)) {
9209 fprintf(stderr,
9210 Name ": Cannot restore data from backup."
9211 " Too many failed disks\n");
9212 goto abort;
9213 }
9214
9215 if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
9216 /* ignore error == 2, this can mean end of reshape here
9217 */
9218 dprintf("imsm: Cannot write checkpoint to "
9219 "migration record (UNIT_SRC_NORMAL) during restart\n");
9220 } else
9221 retval = 0;
9222
9223 abort:
9224 if (targets) {
9225 for (i = 0; i < new_disks; i++)
9226 if (targets[i])
9227 close(targets[i]);
9228 free(targets);
9229 }
9230 free(buf);
9231 return retval;
9232 }
9233
9234 static char disk_by_path[] = "/dev/disk/by-path/";
9235
9236 static const char *imsm_get_disk_controller_domain(const char *path)
9237 {
9238 char disk_path[PATH_MAX];
9239 char *drv=NULL;
9240 struct stat st;
9241
9242 strncpy(disk_path, disk_by_path, PATH_MAX - 1);
9243 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
9244 if (stat(disk_path, &st) == 0) {
9245 struct sys_dev* hba;
9246 char *path=NULL;
9247
9248 path = devt_to_devpath(st.st_rdev);
9249 if (path == NULL)
9250 return "unknown";
9251 hba = find_disk_attached_hba(-1, path);
9252 if (hba && hba->type == SYS_DEV_SAS)
9253 drv = "isci";
9254 else if (hba && hba->type == SYS_DEV_SATA)
9255 drv = "ahci";
9256 else
9257 drv = "unknown";
9258 dprintf("path: %s hba: %s attached: %s\n",
9259 path, (hba) ? hba->path : "NULL", drv);
9260 free(path);
9261 if (hba)
9262 free_sys_dev(&hba);
9263 }
9264 return drv;
9265 }
9266
9267 static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
9268 {
9269 char subdev_name[20];
9270 struct mdstat_ent *mdstat;
9271
9272 sprintf(subdev_name, "%d", subdev);
9273 mdstat = mdstat_by_subdev(subdev_name, container);
9274 if (!mdstat)
9275 return -1;
9276
9277 *minor = mdstat->devnum;
9278 free_mdstat(mdstat);
9279 return 0;
9280 }
9281
9282 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
9283 struct geo_params *geo,
9284 int *old_raid_disks)
9285 {
9286 /* currently we only support increasing the number of devices
9287 * for a container. This increases the number of device for each
9288 * member array. They must all be RAID0 or RAID5.
9289 */
9290 int ret_val = 0;
9291 struct mdinfo *info, *member;
9292 int devices_that_can_grow = 0;
9293
9294 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9295 "st->devnum = (%i)\n",
9296 st->devnum);
9297
9298 if (geo->size != -1 ||
9299 geo->level != UnSet ||
9300 geo->layout != UnSet ||
9301 geo->chunksize != 0 ||
9302 geo->raid_disks == UnSet) {
9303 dprintf("imsm: Container operation is allowed for "
9304 "raid disks number change only.\n");
9305 return ret_val;
9306 }
9307
9308 info = container_content_imsm(st, NULL);
9309 for (member = info; member; member = member->next) {
9310 int result;
9311 int minor;
9312
9313 dprintf("imsm: checking device_num: %i\n",
9314 member->container_member);
9315
9316 if (geo->raid_disks <= member->array.raid_disks) {
9317 /* we work on container for Online Capacity Expansion
9318 * only so raid_disks has to grow
9319 */
9320 dprintf("imsm: for container operation raid disks "
9321 "increase is required\n");
9322 break;
9323 }
9324
9325 if ((info->array.level != 0) &&
9326 (info->array.level != 5)) {
9327 /* we cannot use this container with other raid level
9328 */
9329 dprintf("imsm: for container operation wrong"
9330 " raid level (%i) detected\n",
9331 info->array.level);
9332 break;
9333 } else {
9334 /* check for platform support
9335 * for this raid level configuration
9336 */
9337 struct intel_super *super = st->sb;
9338 if (!is_raid_level_supported(super->orom,
9339 member->array.level,
9340 geo->raid_disks)) {
9341 dprintf("platform does not support raid%d with"
9342 " %d disk%s\n",
9343 info->array.level,
9344 geo->raid_disks,
9345 geo->raid_disks > 1 ? "s" : "");
9346 break;
9347 }
9348 /* check if component size is aligned to chunk size
9349 */
9350 if (info->component_size %
9351 (info->array.chunk_size/512)) {
9352 dprintf("Component size is not aligned to "
9353 "chunk size\n");
9354 break;
9355 }
9356 }
9357
9358 if (*old_raid_disks &&
9359 info->array.raid_disks != *old_raid_disks)
9360 break;
9361 *old_raid_disks = info->array.raid_disks;
9362
9363 /* All raid5 and raid0 volumes in container
9364 * have to be ready for Online Capacity Expansion
9365 * so they need to be assembled. We have already
9366 * checked that no recovery etc is happening.
9367 */
9368 result = imsm_find_array_minor_by_subdev(member->container_member,
9369 st->container_dev,
9370 &minor);
9371 if (result < 0) {
9372 dprintf("imsm: cannot find array\n");
9373 break;
9374 }
9375 devices_that_can_grow++;
9376 }
9377 sysfs_free(info);
9378 if (!member && devices_that_can_grow)
9379 ret_val = 1;
9380
9381 if (ret_val)
9382 dprintf("\tContainer operation allowed\n");
9383 else
9384 dprintf("\tError: %i\n", ret_val);
9385
9386 return ret_val;
9387 }
9388
9389 /* Function: get_spares_for_grow
9390 * Description: Allocates memory and creates list of spare devices
9391 * avaliable in container. Checks if spare drive size is acceptable.
9392 * Parameters: Pointer to the supertype structure
9393 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9394 * NULL if fail
9395 */
9396 static struct mdinfo *get_spares_for_grow(struct supertype *st)
9397 {
9398 unsigned long long min_size = min_acceptable_spare_size_imsm(st);
9399 return container_choose_spares(st, min_size, NULL, NULL, NULL, 0);
9400 }
9401
9402 /******************************************************************************
9403 * function: imsm_create_metadata_update_for_reshape
9404 * Function creates update for whole IMSM container.
9405 *
9406 ******************************************************************************/
9407 static int imsm_create_metadata_update_for_reshape(
9408 struct supertype *st,
9409 struct geo_params *geo,
9410 int old_raid_disks,
9411 struct imsm_update_reshape **updatep)
9412 {
9413 struct intel_super *super = st->sb;
9414 struct imsm_super *mpb = super->anchor;
9415 int update_memory_size = 0;
9416 struct imsm_update_reshape *u = NULL;
9417 struct mdinfo *spares = NULL;
9418 int i;
9419 int delta_disks = 0;
9420 struct mdinfo *dev;
9421
9422 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9423 geo->raid_disks);
9424
9425 delta_disks = geo->raid_disks - old_raid_disks;
9426
9427 /* size of all update data without anchor */
9428 update_memory_size = sizeof(struct imsm_update_reshape);
9429
9430 /* now add space for spare disks that we need to add. */
9431 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
9432
9433 u = calloc(1, update_memory_size);
9434 if (u == NULL) {
9435 dprintf("error: "
9436 "cannot get memory for imsm_update_reshape update\n");
9437 return 0;
9438 }
9439 u->type = update_reshape_container_disks;
9440 u->old_raid_disks = old_raid_disks;
9441 u->new_raid_disks = geo->raid_disks;
9442
9443 /* now get spare disks list
9444 */
9445 spares = get_spares_for_grow(st);
9446
9447 if (spares == NULL
9448 || delta_disks > spares->array.spare_disks) {
9449 fprintf(stderr, Name ": imsm: ERROR: Cannot get spare devices "
9450 "for %s.\n", geo->dev_name);
9451 i = -1;
9452 goto abort;
9453 }
9454
9455 /* we have got spares
9456 * update disk list in imsm_disk list table in anchor
9457 */
9458 dprintf("imsm: %i spares are available.\n\n",
9459 spares->array.spare_disks);
9460
9461 dev = spares->devs;
9462 for (i = 0; i < delta_disks; i++) {
9463 struct dl *dl;
9464
9465 if (dev == NULL)
9466 break;
9467 u->new_disks[i] = makedev(dev->disk.major,
9468 dev->disk.minor);
9469 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
9470 dl->index = mpb->num_disks;
9471 mpb->num_disks++;
9472 dev = dev->next;
9473 }
9474
9475 abort:
9476 /* free spares
9477 */
9478 sysfs_free(spares);
9479
9480 dprintf("imsm: reshape update preparation :");
9481 if (i == delta_disks) {
9482 dprintf(" OK\n");
9483 *updatep = u;
9484 return update_memory_size;
9485 }
9486 free(u);
9487 dprintf(" Error\n");
9488
9489 return 0;
9490 }
9491
9492 /******************************************************************************
9493 * function: imsm_create_metadata_update_for_migration()
9494 * Creates update for IMSM array.
9495 *
9496 ******************************************************************************/
9497 static int imsm_create_metadata_update_for_migration(
9498 struct supertype *st,
9499 struct geo_params *geo,
9500 struct imsm_update_reshape_migration **updatep)
9501 {
9502 struct intel_super *super = st->sb;
9503 int update_memory_size = 0;
9504 struct imsm_update_reshape_migration *u = NULL;
9505 struct imsm_dev *dev;
9506 int previous_level = -1;
9507
9508 dprintf("imsm_create_metadata_update_for_migration(enter)"
9509 " New Level = %i\n", geo->level);
9510
9511 /* size of all update data without anchor */
9512 update_memory_size = sizeof(struct imsm_update_reshape_migration);
9513
9514 u = calloc(1, update_memory_size);
9515 if (u == NULL) {
9516 dprintf("error: cannot get memory for "
9517 "imsm_create_metadata_update_for_migration\n");
9518 return 0;
9519 }
9520 u->type = update_reshape_migration;
9521 u->subdev = super->current_vol;
9522 u->new_level = geo->level;
9523 u->new_layout = geo->layout;
9524 u->new_raid_disks = u->old_raid_disks = geo->raid_disks;
9525 u->new_disks[0] = -1;
9526 u->new_chunksize = -1;
9527
9528 dev = get_imsm_dev(super, u->subdev);
9529 if (dev) {
9530 struct imsm_map *map;
9531
9532 map = get_imsm_map(dev, MAP_0);
9533 if (map) {
9534 int current_chunk_size =
9535 __le16_to_cpu(map->blocks_per_strip) / 2;
9536
9537 if (geo->chunksize != current_chunk_size) {
9538 u->new_chunksize = geo->chunksize / 1024;
9539 dprintf("imsm: "
9540 "chunk size change from %i to %i\n",
9541 current_chunk_size, u->new_chunksize);
9542 }
9543 previous_level = map->raid_level;
9544 }
9545 }
9546 if ((geo->level == 5) && (previous_level == 0)) {
9547 struct mdinfo *spares = NULL;
9548
9549 u->new_raid_disks++;
9550 spares = get_spares_for_grow(st);
9551 if ((spares == NULL) || (spares->array.spare_disks < 1)) {
9552 free(u);
9553 sysfs_free(spares);
9554 update_memory_size = 0;
9555 dprintf("error: cannot get spare device "
9556 "for requested migration");
9557 return 0;
9558 }
9559 sysfs_free(spares);
9560 }
9561 dprintf("imsm: reshape update preparation : OK\n");
9562 *updatep = u;
9563
9564 return update_memory_size;
9565 }
9566
9567 static void imsm_update_metadata_locally(struct supertype *st,
9568 void *buf, int len)
9569 {
9570 struct metadata_update mu;
9571
9572 mu.buf = buf;
9573 mu.len = len;
9574 mu.space = NULL;
9575 mu.space_list = NULL;
9576 mu.next = NULL;
9577 imsm_prepare_update(st, &mu);
9578 imsm_process_update(st, &mu);
9579
9580 while (mu.space_list) {
9581 void **space = mu.space_list;
9582 mu.space_list = *space;
9583 free(space);
9584 }
9585 }
9586
9587 /***************************************************************************
9588 * Function: imsm_analyze_change
9589 * Description: Function analyze change for single volume
9590 * and validate if transition is supported
9591 * Parameters: Geometry parameters, supertype structure
9592 * Returns: Operation type code on success, -1 if fail
9593 ****************************************************************************/
9594 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
9595 struct geo_params *geo)
9596 {
9597 struct mdinfo info;
9598 int change = -1;
9599 int check_devs = 0;
9600 int chunk;
9601 /* number of added/removed disks in operation result */
9602 int devNumChange = 0;
9603 /* imsm compatible layout value for array geometry verification */
9604 int imsm_layout = -1;
9605
9606 getinfo_super_imsm_volume(st, &info, NULL);
9607 if ((geo->level != info.array.level) &&
9608 (geo->level >= 0) &&
9609 (geo->level != UnSet)) {
9610 switch (info.array.level) {
9611 case 0:
9612 if (geo->level == 5) {
9613 change = CH_MIGRATION;
9614 if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
9615 fprintf(stderr,
9616 Name " Error. Requested Layout "
9617 "not supported (left-asymmetric layout "
9618 "is supported only)!\n");
9619 change = -1;
9620 goto analyse_change_exit;
9621 }
9622 imsm_layout = geo->layout;
9623 check_devs = 1;
9624 devNumChange = 1; /* parity disk added */
9625 } else if (geo->level == 10) {
9626 change = CH_TAKEOVER;
9627 check_devs = 1;
9628 devNumChange = 2; /* two mirrors added */
9629 imsm_layout = 0x102; /* imsm supported layout */
9630 }
9631 break;
9632 case 1:
9633 case 10:
9634 if (geo->level == 0) {
9635 change = CH_TAKEOVER;
9636 check_devs = 1;
9637 devNumChange = -(geo->raid_disks/2);
9638 imsm_layout = 0; /* imsm raid0 layout */
9639 }
9640 break;
9641 }
9642 if (change == -1) {
9643 fprintf(stderr,
9644 Name " Error. Level Migration from %d to %d "
9645 "not supported!\n",
9646 info.array.level, geo->level);
9647 goto analyse_change_exit;
9648 }
9649 } else
9650 geo->level = info.array.level;
9651
9652 if ((geo->layout != info.array.layout)
9653 && ((geo->layout != UnSet) && (geo->layout != -1))) {
9654 change = CH_MIGRATION;
9655 if ((info.array.layout == 0)
9656 && (info.array.level == 5)
9657 && (geo->layout == 5)) {
9658 /* reshape 5 -> 4 */
9659 } else if ((info.array.layout == 5)
9660 && (info.array.level == 5)
9661 && (geo->layout == 0)) {
9662 /* reshape 4 -> 5 */
9663 geo->layout = 0;
9664 geo->level = 5;
9665 } else {
9666 fprintf(stderr,
9667 Name " Error. Layout Migration from %d to %d "
9668 "not supported!\n",
9669 info.array.layout, geo->layout);
9670 change = -1;
9671 goto analyse_change_exit;
9672 }
9673 } else {
9674 geo->layout = info.array.layout;
9675 if (imsm_layout == -1)
9676 imsm_layout = info.array.layout;
9677 }
9678
9679 if ((geo->chunksize > 0) && (geo->chunksize != UnSet)
9680 && (geo->chunksize != info.array.chunk_size))
9681 change = CH_MIGRATION;
9682 else
9683 geo->chunksize = info.array.chunk_size;
9684
9685 chunk = geo->chunksize / 1024;
9686 if (!validate_geometry_imsm(st,
9687 geo->level,
9688 imsm_layout,
9689 geo->raid_disks + devNumChange,
9690 &chunk,
9691 geo->size,
9692 0, 0, 1))
9693 change = -1;
9694
9695 if (check_devs) {
9696 struct intel_super *super = st->sb;
9697 struct imsm_super *mpb = super->anchor;
9698
9699 if (mpb->num_raid_devs > 1) {
9700 fprintf(stderr,
9701 Name " Error. Cannot perform operation on %s"
9702 "- for this operation it MUST be single "
9703 "array in container\n",
9704 geo->dev_name);
9705 change = -1;
9706 }
9707 }
9708
9709 analyse_change_exit:
9710
9711 return change;
9712 }
9713
9714 int imsm_takeover(struct supertype *st, struct geo_params *geo)
9715 {
9716 struct intel_super *super = st->sb;
9717 struct imsm_update_takeover *u;
9718
9719 u = malloc(sizeof(struct imsm_update_takeover));
9720 if (u == NULL)
9721 return 1;
9722
9723 u->type = update_takeover;
9724 u->subarray = super->current_vol;
9725
9726 /* 10->0 transition */
9727 if (geo->level == 0)
9728 u->direction = R10_TO_R0;
9729
9730 /* 0->10 transition */
9731 if (geo->level == 10)
9732 u->direction = R0_TO_R10;
9733
9734 /* update metadata locally */
9735 imsm_update_metadata_locally(st, u,
9736 sizeof(struct imsm_update_takeover));
9737 /* and possibly remotely */
9738 if (st->update_tail)
9739 append_metadata_update(st, u,
9740 sizeof(struct imsm_update_takeover));
9741 else
9742 free(u);
9743
9744 return 0;
9745 }
9746
9747 static int imsm_reshape_super(struct supertype *st, long long size, int level,
9748 int layout, int chunksize, int raid_disks,
9749 int delta_disks, char *backup, char *dev,
9750 int verbose)
9751 {
9752 int ret_val = 1;
9753 struct geo_params geo;
9754
9755 dprintf("imsm: reshape_super called.\n");
9756
9757 memset(&geo, 0, sizeof(struct geo_params));
9758
9759 geo.dev_name = dev;
9760 geo.dev_id = st->devnum;
9761 geo.size = size;
9762 geo.level = level;
9763 geo.layout = layout;
9764 geo.chunksize = chunksize;
9765 geo.raid_disks = raid_disks;
9766 if (delta_disks != UnSet)
9767 geo.raid_disks += delta_disks;
9768
9769 dprintf("\tfor level : %i\n", geo.level);
9770 dprintf("\tfor raid_disks : %i\n", geo.raid_disks);
9771
9772 if (experimental() == 0)
9773 return ret_val;
9774
9775 if (st->container_dev == st->devnum) {
9776 /* On container level we can only increase number of devices. */
9777 dprintf("imsm: info: Container operation\n");
9778 int old_raid_disks = 0;
9779
9780 if (imsm_reshape_is_allowed_on_container(
9781 st, &geo, &old_raid_disks)) {
9782 struct imsm_update_reshape *u = NULL;
9783 int len;
9784
9785 len = imsm_create_metadata_update_for_reshape(
9786 st, &geo, old_raid_disks, &u);
9787
9788 if (len <= 0) {
9789 dprintf("imsm: Cannot prepare update\n");
9790 goto exit_imsm_reshape_super;
9791 }
9792
9793 ret_val = 0;
9794 /* update metadata locally */
9795 imsm_update_metadata_locally(st, u, len);
9796 /* and possibly remotely */
9797 if (st->update_tail)
9798 append_metadata_update(st, u, len);
9799 else
9800 free(u);
9801
9802 } else {
9803 fprintf(stderr, Name ": (imsm) Operation "
9804 "is not allowed on this container\n");
9805 }
9806 } else {
9807 /* On volume level we support following operations
9808 * - takeover: raid10 -> raid0; raid0 -> raid10
9809 * - chunk size migration
9810 * - migration: raid5 -> raid0; raid0 -> raid5
9811 */
9812 struct intel_super *super = st->sb;
9813 struct intel_dev *dev = super->devlist;
9814 int change, devnum;
9815 dprintf("imsm: info: Volume operation\n");
9816 /* find requested device */
9817 while (dev) {
9818 if (imsm_find_array_minor_by_subdev(
9819 dev->index, st->container_dev, &devnum) == 0
9820 && devnum == geo.dev_id)
9821 break;
9822 dev = dev->next;
9823 }
9824 if (dev == NULL) {
9825 fprintf(stderr, Name " Cannot find %s (%i) subarray\n",
9826 geo.dev_name, geo.dev_id);
9827 goto exit_imsm_reshape_super;
9828 }
9829 super->current_vol = dev->index;
9830 change = imsm_analyze_change(st, &geo);
9831 switch (change) {
9832 case CH_TAKEOVER:
9833 ret_val = imsm_takeover(st, &geo);
9834 break;
9835 case CH_MIGRATION: {
9836 struct imsm_update_reshape_migration *u = NULL;
9837 int len =
9838 imsm_create_metadata_update_for_migration(
9839 st, &geo, &u);
9840 if (len < 1) {
9841 dprintf("imsm: "
9842 "Cannot prepare update\n");
9843 break;
9844 }
9845 ret_val = 0;
9846 /* update metadata locally */
9847 imsm_update_metadata_locally(st, u, len);
9848 /* and possibly remotely */
9849 if (st->update_tail)
9850 append_metadata_update(st, u, len);
9851 else
9852 free(u);
9853 }
9854 break;
9855 default:
9856 ret_val = 1;
9857 }
9858 }
9859
9860 exit_imsm_reshape_super:
9861 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
9862 return ret_val;
9863 }
9864
9865 /*******************************************************************************
9866 * Function: wait_for_reshape_imsm
9867 * Description: Function writes new sync_max value and waits until
9868 * reshape process reach new position
9869 * Parameters:
9870 * sra : general array info
9871 * ndata : number of disks in new array's layout
9872 * Returns:
9873 * 0 : success,
9874 * 1 : there is no reshape in progress,
9875 * -1 : fail
9876 ******************************************************************************/
9877 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
9878 {
9879 int fd = sysfs_get_fd(sra, NULL, "reshape_position");
9880 unsigned long long completed;
9881 /* to_complete : new sync_max position */
9882 unsigned long long to_complete = sra->reshape_progress;
9883 unsigned long long position_to_set = to_complete / ndata;
9884
9885 if (fd < 0) {
9886 dprintf("imsm: wait_for_reshape_imsm() "
9887 "cannot open reshape_position\n");
9888 return 1;
9889 }
9890
9891 if (sysfs_fd_get_ll(fd, &completed) < 0) {
9892 dprintf("imsm: wait_for_reshape_imsm() "
9893 "cannot read reshape_position (no reshape in progres)\n");
9894 close(fd);
9895 return 0;
9896 }
9897
9898 if (completed > to_complete) {
9899 dprintf("imsm: wait_for_reshape_imsm() "
9900 "wrong next position to set %llu (%llu)\n",
9901 to_complete, completed);
9902 close(fd);
9903 return -1;
9904 }
9905 dprintf("Position set: %llu\n", position_to_set);
9906 if (sysfs_set_num(sra, NULL, "sync_max",
9907 position_to_set) != 0) {
9908 dprintf("imsm: wait_for_reshape_imsm() "
9909 "cannot set reshape position to %llu\n",
9910 position_to_set);
9911 close(fd);
9912 return -1;
9913 }
9914
9915 do {
9916 char action[20];
9917 fd_set rfds;
9918 FD_ZERO(&rfds);
9919 FD_SET(fd, &rfds);
9920 select(fd+1, &rfds, NULL, NULL, NULL);
9921 if (sysfs_get_str(sra, NULL, "sync_action",
9922 action, 20) > 0 &&
9923 strncmp(action, "reshape", 7) != 0)
9924 break;
9925 if (sysfs_fd_get_ll(fd, &completed) < 0) {
9926 dprintf("imsm: wait_for_reshape_imsm() "
9927 "cannot read reshape_position (in loop)\n");
9928 close(fd);
9929 return 1;
9930 }
9931 } while (completed < to_complete);
9932 close(fd);
9933 return 0;
9934
9935 }
9936
9937 /*******************************************************************************
9938 * Function: check_degradation_change
9939 * Description: Check that array hasn't become failed.
9940 * Parameters:
9941 * info : for sysfs access
9942 * sources : source disks descriptors
9943 * degraded: previous degradation level
9944 * Returns:
9945 * degradation level
9946 ******************************************************************************/
9947 int check_degradation_change(struct mdinfo *info,
9948 int *sources,
9949 int degraded)
9950 {
9951 unsigned long long new_degraded;
9952 sysfs_get_ll(info, NULL, "degraded", &new_degraded);
9953 if (new_degraded != (unsigned long long)degraded) {
9954 /* check each device to ensure it is still working */
9955 struct mdinfo *sd;
9956 new_degraded = 0;
9957 for (sd = info->devs ; sd ; sd = sd->next) {
9958 if (sd->disk.state & (1<<MD_DISK_FAULTY))
9959 continue;
9960 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
9961 char sbuf[20];
9962 if (sysfs_get_str(info,
9963 sd, "state", sbuf, 20) < 0 ||
9964 strstr(sbuf, "faulty") ||
9965 strstr(sbuf, "in_sync") == NULL) {
9966 /* this device is dead */
9967 sd->disk.state = (1<<MD_DISK_FAULTY);
9968 if (sd->disk.raid_disk >= 0 &&
9969 sources[sd->disk.raid_disk] >= 0) {
9970 close(sources[
9971 sd->disk.raid_disk]);
9972 sources[sd->disk.raid_disk] =
9973 -1;
9974 }
9975 new_degraded++;
9976 }
9977 }
9978 }
9979 }
9980
9981 return new_degraded;
9982 }
9983
9984 /*******************************************************************************
9985 * Function: imsm_manage_reshape
9986 * Description: Function finds array under reshape and it manages reshape
9987 * process. It creates stripes backups (if required) and sets
9988 * checheckpoits.
9989 * Parameters:
9990 * afd : Backup handle (nattive) - not used
9991 * sra : general array info
9992 * reshape : reshape parameters - not used
9993 * st : supertype structure
9994 * blocks : size of critical section [blocks]
9995 * fds : table of source device descriptor
9996 * offsets : start of array (offest per devices)
9997 * dests : not used
9998 * destfd : table of destination device descriptor
9999 * destoffsets : table of destination offsets (per device)
10000 * Returns:
10001 * 1 : success, reshape is done
10002 * 0 : fail
10003 ******************************************************************************/
10004 static int imsm_manage_reshape(
10005 int afd, struct mdinfo *sra, struct reshape *reshape,
10006 struct supertype *st, unsigned long backup_blocks,
10007 int *fds, unsigned long long *offsets,
10008 int dests, int *destfd, unsigned long long *destoffsets)
10009 {
10010 int ret_val = 0;
10011 struct intel_super *super = st->sb;
10012 struct intel_dev *dv = NULL;
10013 struct imsm_dev *dev = NULL;
10014 struct imsm_map *map_src;
10015 int migr_vol_qan = 0;
10016 int ndata, odata; /* [bytes] */
10017 int chunk; /* [bytes] */
10018 struct migr_record *migr_rec;
10019 char *buf = NULL;
10020 unsigned int buf_size; /* [bytes] */
10021 unsigned long long max_position; /* array size [bytes] */
10022 unsigned long long next_step; /* [blocks]/[bytes] */
10023 unsigned long long old_data_stripe_length;
10024 unsigned long long start_src; /* [bytes] */
10025 unsigned long long start; /* [bytes] */
10026 unsigned long long start_buf_shift; /* [bytes] */
10027 int degraded = 0;
10028 int source_layout = 0;
10029
10030 if (!fds || !offsets || !sra)
10031 goto abort;
10032
10033 /* Find volume during the reshape */
10034 for (dv = super->devlist; dv; dv = dv->next) {
10035 if (dv->dev->vol.migr_type == MIGR_GEN_MIGR
10036 && dv->dev->vol.migr_state == 1) {
10037 dev = dv->dev;
10038 migr_vol_qan++;
10039 }
10040 }
10041 /* Only one volume can migrate at the same time */
10042 if (migr_vol_qan != 1) {
10043 fprintf(stderr, Name " : %s", migr_vol_qan ?
10044 "Number of migrating volumes greater than 1\n" :
10045 "There is no volume during migrationg\n");
10046 goto abort;
10047 }
10048
10049 map_src = get_imsm_map(dev, MAP_1);
10050 if (map_src == NULL)
10051 goto abort;
10052
10053 ndata = imsm_num_data_members(dev, MAP_0);
10054 odata = imsm_num_data_members(dev, MAP_1);
10055
10056 chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
10057 old_data_stripe_length = odata * chunk;
10058
10059 migr_rec = super->migr_rec;
10060
10061 /* initialize migration record for start condition */
10062 if (sra->reshape_progress == 0)
10063 init_migr_record_imsm(st, dev, sra);
10064 else {
10065 if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
10066 dprintf("imsm: cannot restart migration when data "
10067 "are present in copy area.\n");
10068 goto abort;
10069 }
10070 }
10071
10072 /* size for data */
10073 buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
10074 /* extend buffer size for parity disk */
10075 buf_size += __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
10076 /* add space for stripe aligment */
10077 buf_size += old_data_stripe_length;
10078 if (posix_memalign((void **)&buf, 4096, buf_size)) {
10079 dprintf("imsm: Cannot allocate checpoint buffer\n");
10080 goto abort;
10081 }
10082
10083 max_position = sra->component_size * ndata;
10084 source_layout = imsm_level_to_layout(map_src->raid_level);
10085
10086 while (__le32_to_cpu(migr_rec->curr_migr_unit) <
10087 __le32_to_cpu(migr_rec->num_migr_units)) {
10088 /* current reshape position [blocks] */
10089 unsigned long long current_position =
10090 __le32_to_cpu(migr_rec->blocks_per_unit)
10091 * __le32_to_cpu(migr_rec->curr_migr_unit);
10092 unsigned long long border;
10093
10094 /* Check that array hasn't become failed.
10095 */
10096 degraded = check_degradation_change(sra, fds, degraded);
10097 if (degraded > 1) {
10098 dprintf("imsm: Abort reshape due to degradation"
10099 " level (%i)\n", degraded);
10100 goto abort;
10101 }
10102
10103 next_step = __le32_to_cpu(migr_rec->blocks_per_unit);
10104
10105 if ((current_position + next_step) > max_position)
10106 next_step = max_position - current_position;
10107
10108 start = current_position * 512;
10109
10110 /* allign reading start to old geometry */
10111 start_buf_shift = start % old_data_stripe_length;
10112 start_src = start - start_buf_shift;
10113
10114 border = (start_src / odata) - (start / ndata);
10115 border /= 512;
10116 if (border <= __le32_to_cpu(migr_rec->dest_depth_per_unit)) {
10117 /* save critical stripes to buf
10118 * start - start address of current unit
10119 * to backup [bytes]
10120 * start_src - start address of current unit
10121 * to backup alligned to source array
10122 * [bytes]
10123 */
10124 unsigned long long next_step_filler = 0;
10125 unsigned long long copy_length = next_step * 512;
10126
10127 /* allign copy area length to stripe in old geometry */
10128 next_step_filler = ((copy_length + start_buf_shift)
10129 % old_data_stripe_length);
10130 if (next_step_filler)
10131 next_step_filler = (old_data_stripe_length
10132 - next_step_filler);
10133 dprintf("save_stripes() parameters: start = %llu,"
10134 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10135 "\tstart_in_buf_shift = %llu,"
10136 "\tnext_step_filler = %llu\n",
10137 start, start_src, copy_length,
10138 start_buf_shift, next_step_filler);
10139
10140 if (save_stripes(fds, offsets, map_src->num_members,
10141 chunk, map_src->raid_level,
10142 source_layout, 0, NULL, start_src,
10143 copy_length +
10144 next_step_filler + start_buf_shift,
10145 buf)) {
10146 dprintf("imsm: Cannot save stripes"
10147 " to buffer\n");
10148 goto abort;
10149 }
10150 /* Convert data to destination format and store it
10151 * in backup general migration area
10152 */
10153 if (save_backup_imsm(st, dev, sra,
10154 buf + start_buf_shift, copy_length)) {
10155 dprintf("imsm: Cannot save stripes to "
10156 "target devices\n");
10157 goto abort;
10158 }
10159 if (save_checkpoint_imsm(st, sra,
10160 UNIT_SRC_IN_CP_AREA)) {
10161 dprintf("imsm: Cannot write checkpoint to "
10162 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10163 goto abort;
10164 }
10165 } else {
10166 /* set next step to use whole border area */
10167 border /= next_step;
10168 if (border > 1)
10169 next_step *= border;
10170 }
10171 /* When data backed up, checkpoint stored,
10172 * kick the kernel to reshape unit of data
10173 */
10174 next_step = next_step + sra->reshape_progress;
10175 /* limit next step to array max position */
10176 if (next_step > max_position)
10177 next_step = max_position;
10178 sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
10179 sysfs_set_num(sra, NULL, "suspend_hi", next_step);
10180 sra->reshape_progress = next_step;
10181
10182 /* wait until reshape finish */
10183 if (wait_for_reshape_imsm(sra, ndata) < 0) {
10184 dprintf("wait_for_reshape_imsm returned error!\n");
10185 goto abort;
10186 }
10187
10188 if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
10189 /* ignore error == 2, this can mean end of reshape here
10190 */
10191 dprintf("imsm: Cannot write checkpoint to "
10192 "migration record (UNIT_SRC_NORMAL)\n");
10193 goto abort;
10194 }
10195
10196 }
10197
10198 /* return '1' if done */
10199 ret_val = 1;
10200 abort:
10201 free(buf);
10202 abort_reshape(sra);
10203
10204 return ret_val;
10205 }
10206 #endif /* MDASSEMBLE */
10207
10208 struct superswitch super_imsm = {
10209 #ifndef MDASSEMBLE
10210 .examine_super = examine_super_imsm,
10211 .brief_examine_super = brief_examine_super_imsm,
10212 .brief_examine_subarrays = brief_examine_subarrays_imsm,
10213 .export_examine_super = export_examine_super_imsm,
10214 .detail_super = detail_super_imsm,
10215 .brief_detail_super = brief_detail_super_imsm,
10216 .write_init_super = write_init_super_imsm,
10217 .validate_geometry = validate_geometry_imsm,
10218 .add_to_super = add_to_super_imsm,
10219 .remove_from_super = remove_from_super_imsm,
10220 .detail_platform = detail_platform_imsm,
10221 .kill_subarray = kill_subarray_imsm,
10222 .update_subarray = update_subarray_imsm,
10223 .load_container = load_container_imsm,
10224 .default_geometry = default_geometry_imsm,
10225 .get_disk_controller_domain = imsm_get_disk_controller_domain,
10226 .reshape_super = imsm_reshape_super,
10227 .manage_reshape = imsm_manage_reshape,
10228 .recover_backup = recover_backup_imsm,
10229 #endif
10230 .match_home = match_home_imsm,
10231 .uuid_from_super= uuid_from_super_imsm,
10232 .getinfo_super = getinfo_super_imsm,
10233 .getinfo_super_disks = getinfo_super_disks_imsm,
10234 .update_super = update_super_imsm,
10235
10236 .avail_size = avail_size_imsm,
10237 .min_acceptable_spare_size = min_acceptable_spare_size_imsm,
10238
10239 .compare_super = compare_super_imsm,
10240
10241 .load_super = load_super_imsm,
10242 .init_super = init_super_imsm,
10243 .store_super = store_super_imsm,
10244 .free_super = free_super_imsm,
10245 .match_metadata_desc = match_metadata_desc_imsm,
10246 .container_content = container_content_imsm,
10247
10248
10249 .external = 1,
10250 .name = "imsm",
10251
10252 #ifndef MDASSEMBLE
10253 /* for mdmon */
10254 .open_new = imsm_open_new,
10255 .set_array_state= imsm_set_array_state,
10256 .set_disk = imsm_set_disk,
10257 .sync_metadata = imsm_sync_metadata,
10258 .activate_spare = imsm_activate_spare,
10259 .process_update = imsm_process_update,
10260 .prepare_update = imsm_prepare_update,
10261 #endif /* MDASSEMBLE */
10262 };