Merge bk://kernel.bkbits.net/davem/sparc-2.5
[linux-2.6/history.git] / drivers / md / multipath.c
blobeeab0139520c67b22b45059f0b67e28f1e5936d8
1 /*
2 * multipath.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * MULTIPATH management functions.
10 * derived from raid1.c.
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.
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.
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/raid/multipath.h>
26 #include <linux/buffer_head.h>
27 #include <asm/atomic.h>
29 #define MAJOR_NR MD_MAJOR
30 #define MD_DRIVER
31 #define MD_PERSONALITY
32 #define DEVICE_NR(device) (minor(device))
34 #define MAX_WORK_PER_DISK 128
36 #define NR_RESERVED_BUFS 32
39 static mdk_personality_t multipath_personality;
40 static spinlock_t retry_list_lock = SPIN_LOCK_UNLOCKED;
41 struct multipath_bh *multipath_retry_list = NULL, **multipath_retry_tail;
44 static void *mp_pool_alloc(int gfp_flags, void *data)
46 struct multipath_bh *mpb;
47 mpb = kmalloc(sizeof(*mpb), gfp_flags);
48 if (mpb)
49 memset(mpb, 0, sizeof(*mpb));
50 return mpb;
53 static void mp_pool_free(void *mpb, void *data)
55 kfree(mpb);
58 static int multipath_map (mddev_t *mddev, mdk_rdev_t **rdevp)
60 multipath_conf_t *conf = mddev_to_conf(mddev);
61 int i, disks = conf->raid_disks;
64 * Later we do read balancing on the read side
65 * now we use the first available disk.
68 spin_lock_irq(&conf->device_lock);
69 for (i = 0; i < disks; i++) {
70 mdk_rdev_t *rdev = conf->multipaths[i].rdev;
71 if (rdev && rdev->in_sync) {
72 *rdevp = rdev;
73 atomic_inc(&rdev->nr_pending);
74 spin_unlock_irq(&conf->device_lock);
75 return 0;
78 spin_unlock_irq(&conf->device_lock);
80 printk(KERN_ERR "multipath_map(): no more operational IO paths?\n");
81 return (-1);
84 static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
86 unsigned long flags;
87 mddev_t *mddev = mp_bh->mddev;
89 spin_lock_irqsave(&retry_list_lock, flags);
90 if (multipath_retry_list == NULL)
91 multipath_retry_tail = &multipath_retry_list;
92 *multipath_retry_tail = mp_bh;
93 multipath_retry_tail = &mp_bh->next_mp;
94 mp_bh->next_mp = NULL;
95 spin_unlock_irqrestore(&retry_list_lock, flags);
96 md_wakeup_thread(mddev->thread);
101 * multipath_end_bh_io() is called when we have finished servicing a multipathed
102 * operation and are ready to return a success/failure code to the buffer
103 * cache layer.
105 static void multipath_end_bh_io (struct multipath_bh *mp_bh, int uptodate)
107 struct bio *bio = mp_bh->master_bio;
108 multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
110 bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
111 mempool_free(mp_bh, conf->pool);
114 int multipath_end_request(struct bio *bio, unsigned int bytes_done, int error)
116 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
117 struct multipath_bh * mp_bh = (struct multipath_bh *)(bio->bi_private);
118 multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
119 mdk_rdev_t *rdev = conf->multipaths[mp_bh->path].rdev;
121 if (bio->bi_size)
122 return 1;
124 if (uptodate)
125 multipath_end_bh_io(mp_bh, uptodate);
126 else {
128 * oops, IO error:
130 char b[BDEVNAME_SIZE];
131 md_error (mp_bh->mddev, rdev);
132 printk(KERN_ERR "multipath: %s: rescheduling sector %llu\n",
133 bdevname(rdev->bdev,b),
134 (unsigned long long)bio->bi_sector);
135 multipath_reschedule_retry(mp_bh);
137 atomic_dec(&rdev->nr_pending);
138 return 0;
142 * This routine returns the disk from which the requested read should
143 * be done.
146 static int multipath_read_balance (multipath_conf_t *conf)
148 int disk;
150 for (disk = 0; disk < conf->raid_disks; disk++) {
151 mdk_rdev_t *rdev = conf->multipaths[disk].rdev;
152 if (rdev && rdev->in_sync)
153 return disk;
155 BUG();
156 return 0;
159 static int multipath_make_request (request_queue_t *q, struct bio * bio)
161 mddev_t *mddev = q->queuedata;
162 multipath_conf_t *conf = mddev_to_conf(mddev);
163 struct multipath_bh * mp_bh;
164 struct multipath_info *multipath;
166 mp_bh = mempool_alloc(conf->pool, GFP_NOIO);
168 mp_bh->master_bio = bio;
169 mp_bh->mddev = mddev;
172 * read balancing logic:
174 spin_lock_irq(&conf->device_lock);
175 mp_bh->path = multipath_read_balance(conf);
176 multipath = conf->multipaths + mp_bh->path;
177 atomic_inc(&multipath->rdev->nr_pending);
178 spin_unlock_irq(&conf->device_lock);
180 mp_bh->bio = *bio;
181 mp_bh->bio.bi_bdev = multipath->rdev->bdev;
182 mp_bh->bio.bi_end_io = multipath_end_request;
183 mp_bh->bio.bi_private = mp_bh;
184 generic_make_request(&mp_bh->bio);
185 return 0;
188 static void multipath_status (struct seq_file *seq, mddev_t *mddev)
190 multipath_conf_t *conf = mddev_to_conf(mddev);
191 int i;
193 seq_printf (seq, " [%d/%d] [", conf->raid_disks,
194 conf->working_disks);
195 for (i = 0; i < conf->raid_disks; i++)
196 seq_printf (seq, "%s",
197 conf->multipaths[i].rdev &&
198 conf->multipaths[i].rdev->in_sync ? "U" : "_");
199 seq_printf (seq, "]");
204 * Careful, this can execute in IRQ contexts as well!
206 static void multipath_error (mddev_t *mddev, mdk_rdev_t *rdev)
208 multipath_conf_t *conf = mddev_to_conf(mddev);
210 if (conf->working_disks <= 1) {
212 * Uh oh, we can do nothing if this is our last path, but
213 * first check if this is a queued request for a device
214 * which has just failed.
216 printk(KERN_ALERT
217 "multipath: only one IO path left and IO error.\n");
218 /* leave it active... it's all we have */
219 } else {
221 * Mark disk as unusable
223 if (!rdev->faulty) {
224 char b[BDEVNAME_SIZE];
225 rdev->in_sync = 0;
226 rdev->faulty = 1;
227 mddev->sb_dirty = 1;
228 conf->working_disks--;
229 printk(KERN_ALERT "multipath: IO failure on %s,"
230 " disabling IO path. \n Operation continuing"
231 " on %d IO paths.\n",
232 bdevname (rdev->bdev,b),
233 conf->working_disks);
238 static void print_multipath_conf (multipath_conf_t *conf)
240 int i;
241 struct multipath_info *tmp;
243 printk("MULTIPATH conf printout:\n");
244 if (!conf) {
245 printk("(conf==NULL)\n");
246 return;
248 printk(" --- wd:%d rd:%d\n", conf->working_disks,
249 conf->raid_disks);
251 for (i = 0; i < conf->raid_disks; i++) {
252 char b[BDEVNAME_SIZE];
253 tmp = conf->multipaths + i;
254 if (tmp->rdev)
255 printk(" disk%d, o:%d, dev:%s\n",
256 i,!tmp->rdev->faulty,
257 bdevname(tmp->rdev->bdev,b));
262 static int multipath_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
264 multipath_conf_t *conf = mddev->private;
265 int found = 0;
266 int path;
267 struct multipath_info *p;
269 print_multipath_conf(conf);
270 spin_lock_irq(&conf->device_lock);
271 for (path=0; path<mddev->raid_disks; path++)
272 if ((p=conf->multipaths+path)->rdev == NULL) {
273 p->rdev = rdev;
274 conf->working_disks++;
275 rdev->raid_disk = path;
276 rdev->in_sync = 1;
277 found = 1;
279 spin_unlock_irq(&conf->device_lock);
281 print_multipath_conf(conf);
282 return found;
285 static int multipath_remove_disk(mddev_t *mddev, int number)
287 multipath_conf_t *conf = mddev->private;
288 int err = 1;
289 struct multipath_info *p = conf->multipaths + number;
291 print_multipath_conf(conf);
292 spin_lock_irq(&conf->device_lock);
294 if (p->rdev) {
295 if (p->rdev->in_sync ||
296 atomic_read(&p->rdev->nr_pending)) {
297 printk(KERN_ERR "hot-remove-disk, slot %d is identified" " but is still operational!\n", number);
298 err = -EBUSY;
299 goto abort;
301 p->rdev = NULL;
302 err = 0;
304 if (err)
305 MD_BUG();
306 abort:
307 spin_unlock_irq(&conf->device_lock);
309 print_multipath_conf(conf);
310 return err;
316 * This is a kernel thread which:
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.
323 static void multipathd (mddev_t *mddev)
325 struct multipath_bh *mp_bh;
326 struct bio *bio;
327 unsigned long flags;
328 mdk_rdev_t *rdev;
330 md_check_recovery(mddev);
331 for (;;) {
332 char b[BDEVNAME_SIZE];
333 spin_lock_irqsave(&retry_list_lock, flags);
334 mp_bh = multipath_retry_list;
335 if (!mp_bh)
336 break;
337 multipath_retry_list = mp_bh->next_mp;
338 spin_unlock_irqrestore(&retry_list_lock, flags);
340 mddev = mp_bh->mddev;
341 bio = &mp_bh->bio;
342 bio->bi_sector = mp_bh->master_bio->bi_sector;
344 rdev = NULL;
345 if (multipath_map (mddev, &rdev)<0) {
346 printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
347 " error for block %llu\n",
348 bdevname(bio->bi_bdev,b),
349 (unsigned long long)bio->bi_sector);
350 multipath_end_bh_io(mp_bh, 0);
351 } else {
352 printk(KERN_ERR "multipath: %s: redirecting sector %llu"
353 " to another IO path\n",
354 bdevname(bio->bi_bdev,b),
355 (unsigned long long)bio->bi_sector);
356 bio->bi_bdev = rdev->bdev;
357 generic_make_request(bio);
360 spin_unlock_irqrestore(&retry_list_lock, flags);
363 static int multipath_run (mddev_t *mddev)
365 multipath_conf_t *conf;
366 int disk_idx;
367 struct multipath_info *disk;
368 mdk_rdev_t *rdev;
369 struct list_head *tmp;
371 if (mddev->level != LEVEL_MULTIPATH) {
372 printk("multipath: md%d: raid level not set to multipath IO (%d)\n",
373 mdidx(mddev), mddev->level);
374 goto out;
377 * copy the already verified devices into our private MULTIPATH
378 * bookkeeping area. [whatever we allocate in multipath_run(),
379 * should be freed in multipath_stop()]
382 conf = kmalloc(sizeof(multipath_conf_t), GFP_KERNEL);
383 mddev->private = conf;
384 if (!conf) {
385 printk(KERN_ERR
386 "multipath: couldn't allocate memory for md%d\n",
387 mdidx(mddev));
388 goto out;
390 memset(conf, 0, sizeof(*conf));
392 conf->multipaths = kmalloc(sizeof(struct multipath_info)*mddev->raid_disks,
393 GFP_KERNEL);
394 if (!conf->multipaths) {
395 printk(KERN_ERR
396 "multipath: couldn't allocate memory for md%d\n",
397 mdidx(mddev));
398 goto out_free_conf;
400 memset(conf->multipaths, 0, sizeof(struct multipath_info)*mddev->raid_disks);
402 conf->working_disks = 0;
403 ITERATE_RDEV(mddev,rdev,tmp) {
404 disk_idx = rdev->raid_disk;
405 if (disk_idx < 0 ||
406 disk_idx >= mddev->raid_disks)
407 continue;
409 disk = conf->multipaths + disk_idx;
410 disk->rdev = rdev;
411 if (!rdev->faulty)
412 conf->working_disks++;
415 conf->raid_disks = mddev->raid_disks;
416 mddev->sb_dirty = 1;
417 conf->mddev = mddev;
418 conf->device_lock = SPIN_LOCK_UNLOCKED;
420 if (!conf->working_disks) {
421 printk(KERN_ERR "multipath: no operational IO paths for md%d\n",
422 mdidx(mddev));
423 goto out_free_conf;
425 mddev->degraded = conf->raid_disks = conf->working_disks;
427 conf->pool = mempool_create(NR_RESERVED_BUFS,
428 mp_pool_alloc, mp_pool_free,
429 NULL);
430 if (conf->pool == NULL) {
431 printk(KERN_ERR
432 "multipath: couldn't allocate memory for md%d\n",
433 mdidx(mddev));
434 goto out_free_conf;
438 const char * name = "md%d_multipath";
440 mddev->thread = md_register_thread(multipathd, mddev, name);
441 if (!mddev->thread) {
442 printk(KERN_ERR "multipath: couldn't allocate thread"
443 " for md%d\n", mdidx(mddev));
444 goto out_free_conf;
448 printk(KERN_INFO
449 "multipath: array md%d active with %d out of %d IO paths\n",
450 mdidx(mddev), conf->working_disks, mddev->raid_disks);
452 * Ok, everything is just fine now
454 mddev->array_size = mddev->size;
455 return 0;
457 out_free_conf:
458 if (conf->pool)
459 mempool_destroy(conf->pool);
460 if (conf->multipaths)
461 kfree(conf->multipaths);
462 kfree(conf);
463 mddev->private = NULL;
464 out:
465 return -EIO;
469 static int multipath_stop (mddev_t *mddev)
471 multipath_conf_t *conf = mddev_to_conf(mddev);
473 md_unregister_thread(mddev->thread);
474 mempool_destroy(conf->pool);
475 kfree(conf->multipaths);
476 kfree(conf);
477 mddev->private = NULL;
478 return 0;
481 static mdk_personality_t multipath_personality=
483 .name = "multipath",
484 .owner = THIS_MODULE,
485 .make_request = multipath_make_request,
486 .run = multipath_run,
487 .stop = multipath_stop,
488 .status = multipath_status,
489 .error_handler = multipath_error,
490 .hot_add_disk = multipath_add_disk,
491 .hot_remove_disk= multipath_remove_disk,
494 static int __init multipath_init (void)
496 return register_md_personality (MULTIPATH, &multipath_personality);
499 static void __exit multipath_exit (void)
501 unregister_md_personality (MULTIPATH);
504 module_init(multipath_init);
505 module_exit(multipath_exit);
506 MODULE_LICENSE("GPL");