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