1 /* 2 * multipath.c : Multiple Devices driver for Linux 3 * 4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat 5 * 6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman 7 * 8 * MULTIPATH management functions. 9 * 10 * derived from raid1.c. 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2, or (at your option) 15 * any later version. 16 * 17 * You should have received a copy of the GNU General Public License 18 * (for example /usr/src/linux/COPYING); if not, write to the Free 19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22 #include <linux/blkdev.h> 23 #include <linux/module.h> 24 #include <linux/raid/md_u.h> 25 #include <linux/seq_file.h> 26 #include <linux/slab.h> 27 #include "md.h" 28 #include "md-multipath.h" 29 30 #define MAX_WORK_PER_DISK 128 31 32 #define NR_RESERVED_BUFS 32 33 34 static int multipath_map (struct mpconf *conf) 35 { 36 int i, disks = conf->raid_disks; 37 38 /* 39 * Later we do read balancing on the read side 40 * now we use the first available disk. 41 */ 42 43 rcu_read_lock(); 44 for (i = 0; i < disks; i++) { 45 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev); 46 if (rdev && test_bit(In_sync, &rdev->flags) && 47 !test_bit(Faulty, &rdev->flags)) { 48 atomic_inc(&rdev->nr_pending); 49 rcu_read_unlock(); 50 return i; 51 } 52 } 53 rcu_read_unlock(); 54 55 pr_crit_ratelimited("multipath_map(): no more operational IO paths?\n"); 56 return (-1); 57 } 58 59 static void multipath_reschedule_retry (struct multipath_bh *mp_bh) 60 { 61 unsigned long flags; 62 struct mddev *mddev = mp_bh->mddev; 63 struct mpconf *conf = mddev->private; 64 65 spin_lock_irqsave(&conf->device_lock, flags); 66 list_add(&mp_bh->retry_list, &conf->retry_list); 67 spin_unlock_irqrestore(&conf->device_lock, flags); 68 md_wakeup_thread(mddev->thread); 69 } 70 71 /* 72 * multipath_end_bh_io() is called when we have finished servicing a multipathed 73 * operation and are ready to return a success/failure code to the buffer 74 * cache layer. 75 */ 76 static void multipath_end_bh_io(struct multipath_bh *mp_bh, blk_status_t status) 77 { 78 struct bio *bio = mp_bh->master_bio; 79 struct mpconf *conf = mp_bh->mddev->private; 80 81 bio->bi_status = status; 82 bio_endio(bio); 83 mempool_free(mp_bh, conf->pool); 84 } 85 86 static void multipath_end_request(struct bio *bio) 87 { 88 struct multipath_bh *mp_bh = bio->bi_private; 89 struct mpconf *conf = mp_bh->mddev->private; 90 struct md_rdev *rdev = conf->multipaths[mp_bh->path].rdev; 91 92 if (!bio->bi_status) 93 multipath_end_bh_io(mp_bh, 0); 94 else if (!(bio->bi_opf & REQ_RAHEAD)) { 95 /* 96 * oops, IO error: 97 */ 98 char b[BDEVNAME_SIZE]; 99 md_error (mp_bh->mddev, rdev); 100 pr_info("multipath: %s: rescheduling sector %llu\n", 101 bdevname(rdev->bdev,b), 102 (unsigned long long)bio->bi_iter.bi_sector); 103 multipath_reschedule_retry(mp_bh); 104 } else 105 multipath_end_bh_io(mp_bh, bio->bi_status); 106 rdev_dec_pending(rdev, conf->mddev); 107 } 108 109 static bool multipath_make_request(struct mddev *mddev, struct bio * bio) 110 { 111 struct mpconf *conf = mddev->private; 112 struct multipath_bh * mp_bh; 113 struct multipath_info *multipath; 114 115 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { 116 md_flush_request(mddev, bio); 117 return true; 118 } 119 120 mp_bh = mempool_alloc(conf->pool, GFP_NOIO); 121 122 mp_bh->master_bio = bio; 123 mp_bh->mddev = mddev; 124 125 mp_bh->path = multipath_map(conf); 126 if (mp_bh->path < 0) { 127 bio_io_error(bio); 128 mempool_free(mp_bh, conf->pool); 129 return true; 130 } 131 multipath = conf->multipaths + mp_bh->path; 132 133 bio_init(&mp_bh->bio, NULL, 0); 134 __bio_clone_fast(&mp_bh->bio, bio); 135 136 mp_bh->bio.bi_iter.bi_sector += multipath->rdev->data_offset; 137 bio_set_dev(&mp_bh->bio, multipath->rdev->bdev); 138 mp_bh->bio.bi_opf |= REQ_FAILFAST_TRANSPORT; 139 mp_bh->bio.bi_end_io = multipath_end_request; 140 mp_bh->bio.bi_private = mp_bh; 141 mddev_check_writesame(mddev, &mp_bh->bio); 142 mddev_check_write_zeroes(mddev, &mp_bh->bio); 143 generic_make_request(&mp_bh->bio); 144 return true; 145 } 146 147 static void multipath_status(struct seq_file *seq, struct mddev *mddev) 148 { 149 struct mpconf *conf = mddev->private; 150 int i; 151 152 seq_printf (seq, " [%d/%d] [", conf->raid_disks, 153 conf->raid_disks - mddev->degraded); 154 rcu_read_lock(); 155 for (i = 0; i < conf->raid_disks; i++) { 156 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev); 157 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); 158 } 159 rcu_read_unlock(); 160 seq_putc(seq, ']'); 161 } 162 163 static int multipath_congested(struct mddev *mddev, int bits) 164 { 165 struct mpconf *conf = mddev->private; 166 int i, ret = 0; 167 168 rcu_read_lock(); 169 for (i = 0; i < mddev->raid_disks ; i++) { 170 struct md_rdev *rdev = rcu_dereference(conf->multipaths[i].rdev); 171 if (rdev && !test_bit(Faulty, &rdev->flags)) { 172 struct request_queue *q = bdev_get_queue(rdev->bdev); 173 174 ret |= bdi_congested(q->backing_dev_info, bits); 175 /* Just like multipath_map, we just check the 176 * first available device 177 */ 178 break; 179 } 180 } 181 rcu_read_unlock(); 182 return ret; 183 } 184 185 /* 186 * Careful, this can execute in IRQ contexts as well! 187 */ 188 static void multipath_error (struct mddev *mddev, struct md_rdev *rdev) 189 { 190 struct mpconf *conf = mddev->private; 191 char b[BDEVNAME_SIZE]; 192 193 if (conf->raid_disks - mddev->degraded <= 1) { 194 /* 195 * Uh oh, we can do nothing if this is our last path, but 196 * first check if this is a queued request for a device 197 * which has just failed. 198 */ 199 pr_warn("multipath: only one IO path left and IO error.\n"); 200 /* leave it active... it's all we have */ 201 return; 202 } 203 /* 204 * Mark disk as unusable 205 */ 206 if (test_and_clear_bit(In_sync, &rdev->flags)) { 207 unsigned long flags; 208 spin_lock_irqsave(&conf->device_lock, flags); 209 mddev->degraded++; 210 spin_unlock_irqrestore(&conf->device_lock, flags); 211 } 212 set_bit(Faulty, &rdev->flags); 213 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 214 pr_err("multipath: IO failure on %s, disabling IO path.\n" 215 "multipath: Operation continuing on %d IO paths.\n", 216 bdevname(rdev->bdev, b), 217 conf->raid_disks - mddev->degraded); 218 } 219 220 static void print_multipath_conf (struct mpconf *conf) 221 { 222 int i; 223 struct multipath_info *tmp; 224 225 pr_debug("MULTIPATH conf printout:\n"); 226 if (!conf) { 227 pr_debug("(conf==NULL)\n"); 228 return; 229 } 230 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, 231 conf->raid_disks); 232 233 for (i = 0; i < conf->raid_disks; i++) { 234 char b[BDEVNAME_SIZE]; 235 tmp = conf->multipaths + i; 236 if (tmp->rdev) 237 pr_debug(" disk%d, o:%d, dev:%s\n", 238 i,!test_bit(Faulty, &tmp->rdev->flags), 239 bdevname(tmp->rdev->bdev,b)); 240 } 241 } 242 243 static int multipath_add_disk(struct mddev *mddev, struct md_rdev *rdev) 244 { 245 struct mpconf *conf = mddev->private; 246 int err = -EEXIST; 247 int path; 248 struct multipath_info *p; 249 int first = 0; 250 int last = mddev->raid_disks - 1; 251 252 if (rdev->raid_disk >= 0) 253 first = last = rdev->raid_disk; 254 255 print_multipath_conf(conf); 256 257 for (path = first; path <= last; path++) 258 if ((p=conf->multipaths+path)->rdev == NULL) { 259 disk_stack_limits(mddev->gendisk, rdev->bdev, 260 rdev->data_offset << 9); 261 262 err = md_integrity_add_rdev(rdev, mddev); 263 if (err) 264 break; 265 spin_lock_irq(&conf->device_lock); 266 mddev->degraded--; 267 rdev->raid_disk = path; 268 set_bit(In_sync, &rdev->flags); 269 spin_unlock_irq(&conf->device_lock); 270 rcu_assign_pointer(p->rdev, rdev); 271 err = 0; 272 break; 273 } 274 275 print_multipath_conf(conf); 276 277 return err; 278 } 279 280 static int multipath_remove_disk(struct mddev *mddev, struct md_rdev *rdev) 281 { 282 struct mpconf *conf = mddev->private; 283 int err = 0; 284 int number = rdev->raid_disk; 285 struct multipath_info *p = conf->multipaths + number; 286 287 print_multipath_conf(conf); 288 289 if (rdev == p->rdev) { 290 if (test_bit(In_sync, &rdev->flags) || 291 atomic_read(&rdev->nr_pending)) { 292 pr_warn("hot-remove-disk, slot %d is identified but is still operational!\n", number); 293 err = -EBUSY; 294 goto abort; 295 } 296 p->rdev = NULL; 297 if (!test_bit(RemoveSynchronized, &rdev->flags)) { 298 synchronize_rcu(); 299 if (atomic_read(&rdev->nr_pending)) { 300 /* lost the race, try later */ 301 err = -EBUSY; 302 p->rdev = rdev; 303 goto abort; 304 } 305 } 306 err = md_integrity_register(mddev); 307 } 308 abort: 309 310 print_multipath_conf(conf); 311 return err; 312 } 313 314 /* 315 * This is a kernel thread which: 316 * 317 * 1. Retries failed read operations on working multipaths. 318 * 2. Updates the raid superblock when problems encounter. 319 * 3. Performs writes following reads for array syncronising. 320 */ 321 322 static void multipathd(struct md_thread *thread) 323 { 324 struct mddev *mddev = thread->mddev; 325 struct multipath_bh *mp_bh; 326 struct bio *bio; 327 unsigned long flags; 328 struct mpconf *conf = mddev->private; 329 struct list_head *head = &conf->retry_list; 330 331 md_check_recovery(mddev); 332 for (;;) { 333 char b[BDEVNAME_SIZE]; 334 spin_lock_irqsave(&conf->device_lock, flags); 335 if (list_empty(head)) 336 break; 337 mp_bh = list_entry(head->prev, struct multipath_bh, retry_list); 338 list_del(head->prev); 339 spin_unlock_irqrestore(&conf->device_lock, flags); 340 341 bio = &mp_bh->bio; 342 bio->bi_iter.bi_sector = mp_bh->master_bio->bi_iter.bi_sector; 343 344 if ((mp_bh->path = multipath_map (conf))<0) { 345 pr_err("multipath: %s: unrecoverable IO read error for block %llu\n", 346 bio_devname(bio, b), 347 (unsigned long long)bio->bi_iter.bi_sector); 348 multipath_end_bh_io(mp_bh, BLK_STS_IOERR); 349 } else { 350 pr_err("multipath: %s: redirecting sector %llu to another IO path\n", 351 bio_devname(bio, b), 352 (unsigned long long)bio->bi_iter.bi_sector); 353 *bio = *(mp_bh->master_bio); 354 bio->bi_iter.bi_sector += 355 conf->multipaths[mp_bh->path].rdev->data_offset; 356 bio_set_dev(bio, conf->multipaths[mp_bh->path].rdev->bdev); 357 bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 358 bio->bi_end_io = multipath_end_request; 359 bio->bi_private = mp_bh; 360 generic_make_request(bio); 361 } 362 } 363 spin_unlock_irqrestore(&conf->device_lock, flags); 364 } 365 366 static sector_t multipath_size(struct mddev *mddev, sector_t sectors, int raid_disks) 367 { 368 WARN_ONCE(sectors || raid_disks, 369 "%s does not support generic reshape\n", __func__); 370 371 return mddev->dev_sectors; 372 } 373 374 static int multipath_run (struct mddev *mddev) 375 { 376 struct mpconf *conf; 377 int disk_idx; 378 struct multipath_info *disk; 379 struct md_rdev *rdev; 380 int working_disks; 381 382 if (md_check_no_bitmap(mddev)) 383 return -EINVAL; 384 385 if (mddev->level != LEVEL_MULTIPATH) { 386 pr_warn("multipath: %s: raid level not set to multipath IO (%d)\n", 387 mdname(mddev), mddev->level); 388 goto out; 389 } 390 /* 391 * copy the already verified devices into our private MULTIPATH 392 * bookkeeping area. [whatever we allocate in multipath_run(), 393 * should be freed in multipath_free()] 394 */ 395 396 conf = kzalloc(sizeof(struct mpconf), GFP_KERNEL); 397 mddev->private = conf; 398 if (!conf) 399 goto out; 400 401 conf->multipaths = kzalloc(sizeof(struct multipath_info)*mddev->raid_disks, 402 GFP_KERNEL); 403 if (!conf->multipaths) 404 goto out_free_conf; 405 406 working_disks = 0; 407 rdev_for_each(rdev, mddev) { 408 disk_idx = rdev->raid_disk; 409 if (disk_idx < 0 || 410 disk_idx >= mddev->raid_disks) 411 continue; 412 413 disk = conf->multipaths + disk_idx; 414 disk->rdev = rdev; 415 disk_stack_limits(mddev->gendisk, rdev->bdev, 416 rdev->data_offset << 9); 417 418 if (!test_bit(Faulty, &rdev->flags)) 419 working_disks++; 420 } 421 422 conf->raid_disks = mddev->raid_disks; 423 conf->mddev = mddev; 424 spin_lock_init(&conf->device_lock); 425 INIT_LIST_HEAD(&conf->retry_list); 426 427 if (!working_disks) { 428 pr_warn("multipath: no operational IO paths for %s\n", 429 mdname(mddev)); 430 goto out_free_conf; 431 } 432 mddev->degraded = conf->raid_disks - working_disks; 433 434 conf->pool = mempool_create_kmalloc_pool(NR_RESERVED_BUFS, 435 sizeof(struct multipath_bh)); 436 if (conf->pool == NULL) 437 goto out_free_conf; 438 439 mddev->thread = md_register_thread(multipathd, mddev, 440 "multipath"); 441 if (!mddev->thread) 442 goto out_free_conf; 443 444 pr_info("multipath: array %s active with %d out of %d IO paths\n", 445 mdname(mddev), conf->raid_disks - mddev->degraded, 446 mddev->raid_disks); 447 /* 448 * Ok, everything is just fine now 449 */ 450 md_set_array_sectors(mddev, multipath_size(mddev, 0, 0)); 451 452 if (md_integrity_register(mddev)) 453 goto out_free_conf; 454 455 return 0; 456 457 out_free_conf: 458 mempool_destroy(conf->pool); 459 kfree(conf->multipaths); 460 kfree(conf); 461 mddev->private = NULL; 462 out: 463 return -EIO; 464 } 465 466 static void multipath_free(struct mddev *mddev, void *priv) 467 { 468 struct mpconf *conf = priv; 469 470 mempool_destroy(conf->pool); 471 kfree(conf->multipaths); 472 kfree(conf); 473 } 474 475 static struct md_personality multipath_personality = 476 { 477 .name = "multipath", 478 .level = LEVEL_MULTIPATH, 479 .owner = THIS_MODULE, 480 .make_request = multipath_make_request, 481 .run = multipath_run, 482 .free = multipath_free, 483 .status = multipath_status, 484 .error_handler = multipath_error, 485 .hot_add_disk = multipath_add_disk, 486 .hot_remove_disk= multipath_remove_disk, 487 .size = multipath_size, 488 .congested = multipath_congested, 489 }; 490 491 static int __init multipath_init (void) 492 { 493 return register_md_personality (&multipath_personality); 494 } 495 496 static void __exit multipath_exit (void) 497 { 498 unregister_md_personality (&multipath_personality); 499 } 500 501 module_init(multipath_init); 502 module_exit(multipath_exit); 503 MODULE_LICENSE("GPL"); 504 MODULE_DESCRIPTION("simple multi-path personality for MD"); 505 MODULE_ALIAS("md-personality-7"); /* MULTIPATH */ 506 MODULE_ALIAS("md-multipath"); 507 MODULE_ALIAS("md-level--4"); 508