ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / md / multipath.c
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/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>
28
29 #define MAJOR_NR MD_MAJOR
30 #define MD_DRIVER
31 #define MD_PERSONALITY
32
33 #define MAX_WORK_PER_DISK 128
34
35 #define NR_RESERVED_BUFS        32
36
37
38 static mdk_personality_t multipath_personality;
39 static spinlock_t retry_list_lock = SPIN_LOCK_UNLOCKED;
40 struct multipath_bh *multipath_retry_list = NULL, **multipath_retry_tail;
41
42
43 static void *mp_pool_alloc(int gfp_flags, void *data)
44 {
45         struct multipath_bh *mpb;
46         mpb = kmalloc(sizeof(*mpb), gfp_flags);
47         if (mpb) 
48                 memset(mpb, 0, sizeof(*mpb));
49         return mpb;
50 }
51
52 static void mp_pool_free(void *mpb, void *data)
53 {
54         kfree(mpb);
55 }
56
57 static int multipath_map (mddev_t *mddev, mdk_rdev_t **rdevp)
58 {
59         multipath_conf_t *conf = mddev_to_conf(mddev);
60         int i, disks = conf->raid_disks;
61
62         /*
63          * Later we do read balancing on the read side 
64          * now we use the first available disk.
65          */
66
67         spin_lock_irq(&conf->device_lock);
68         for (i = 0; i < disks; i++) {
69                 mdk_rdev_t *rdev = conf->multipaths[i].rdev;
70                 if (rdev && rdev->in_sync) {
71                         *rdevp = rdev;
72                         atomic_inc(&rdev->nr_pending);
73                         spin_unlock_irq(&conf->device_lock);
74                         return 0;
75                 }
76         }
77         spin_unlock_irq(&conf->device_lock);
78
79         printk(KERN_ERR "multipath_map(): no more operational IO paths?\n");
80         return (-1);
81 }
82
83 static void multipath_reschedule_retry (struct multipath_bh *mp_bh)
84 {
85         unsigned long flags;
86         mddev_t *mddev = mp_bh->mddev;
87
88         spin_lock_irqsave(&retry_list_lock, flags);
89         if (multipath_retry_list == NULL)
90                 multipath_retry_tail = &multipath_retry_list;
91         *multipath_retry_tail = mp_bh;
92         multipath_retry_tail = &mp_bh->next_mp;
93         mp_bh->next_mp = NULL;
94         spin_unlock_irqrestore(&retry_list_lock, flags);
95         md_wakeup_thread(mddev->thread);
96 }
97
98
99 /*
100  * multipath_end_bh_io() is called when we have finished servicing a multipathed
101  * operation and are ready to return a success/failure code to the buffer
102  * cache layer.
103  */
104 static void multipath_end_bh_io (struct multipath_bh *mp_bh, int uptodate)
105 {
106         struct bio *bio = mp_bh->master_bio;
107         multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
108
109         bio_endio(bio, bio->bi_size, uptodate ? 0 : -EIO);
110         mempool_free(mp_bh, conf->pool);
111 }
112
113 int multipath_end_request(struct bio *bio, unsigned int bytes_done, int error)
114 {
115         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
116         struct multipath_bh * mp_bh = (struct multipath_bh *)(bio->bi_private);
117         multipath_conf_t *conf = mddev_to_conf(mp_bh->mddev);
118         mdk_rdev_t *rdev = conf->multipaths[mp_bh->path].rdev;
119
120         if (bio->bi_size)
121                 return 1;
122
123         if (uptodate)
124                 multipath_end_bh_io(mp_bh, uptodate);
125         else {
126                 /*
127                  * oops, IO error:
128                  */
129                 char b[BDEVNAME_SIZE];
130                 md_error (mp_bh->mddev, rdev);
131                 printk(KERN_ERR "multipath: %s: rescheduling sector %llu\n", 
132                        bdevname(rdev->bdev,b), 
133                        (unsigned long long)bio->bi_sector);
134                 multipath_reschedule_retry(mp_bh);
135         }
136         atomic_dec(&rdev->nr_pending);
137         return 0;
138 }
139
140 /*
141  * This routine returns the disk from which the requested read should
142  * be done.
143  */
144
145 static int multipath_read_balance (multipath_conf_t *conf)
146 {
147         int disk;
148
149         for (disk = 0; disk < conf->raid_disks; disk++) {
150                 mdk_rdev_t *rdev = conf->multipaths[disk].rdev;
151                 if (rdev && rdev->in_sync)
152                         return disk;
153         }
154         BUG();
155         return 0;
156 }
157
158 static void unplug_slaves(mddev_t *mddev)
159 {
160         multipath_conf_t *conf = mddev_to_conf(mddev);
161         int i;
162
163         for (i=0; i<mddev->raid_disks; i++) {
164                 mdk_rdev_t *rdev = conf->multipaths[i].rdev;
165                 if (rdev && !rdev->faulty) {
166                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
167
168                         if (r_queue->unplug_fn)
169                                 r_queue->unplug_fn(r_queue);
170                 }
171         }
172 }
173 static void multipath_unplug(request_queue_t *q)
174 {
175         unplug_slaves(q->queuedata);
176 }
177
178
179 static int multipath_make_request (request_queue_t *q, struct bio * bio)
180 {
181         mddev_t *mddev = q->queuedata;
182         multipath_conf_t *conf = mddev_to_conf(mddev);
183         struct multipath_bh * mp_bh;
184         struct multipath_info *multipath;
185
186         mp_bh = mempool_alloc(conf->pool, GFP_NOIO);
187
188         mp_bh->master_bio = bio;
189         mp_bh->mddev = mddev;
190
191         if (bio_data_dir(bio)==WRITE) {
192                 disk_stat_inc(mddev->gendisk, writes);
193                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
194         } else {
195                 disk_stat_inc(mddev->gendisk, reads);
196                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
197         }
198         /*
199          * read balancing logic:
200          */
201         spin_lock_irq(&conf->device_lock);
202         mp_bh->path = multipath_read_balance(conf);
203         multipath = conf->multipaths + mp_bh->path;
204         atomic_inc(&multipath->rdev->nr_pending);
205         spin_unlock_irq(&conf->device_lock);
206
207         mp_bh->bio = *bio;
208         mp_bh->bio.bi_bdev = multipath->rdev->bdev;
209         mp_bh->bio.bi_rw |= (1 << BIO_RW_FAILFAST);
210         mp_bh->bio.bi_end_io = multipath_end_request;
211         mp_bh->bio.bi_private = mp_bh;
212         generic_make_request(&mp_bh->bio);
213         return 0;
214 }
215
216 static void multipath_status (struct seq_file *seq, mddev_t *mddev)
217 {
218         multipath_conf_t *conf = mddev_to_conf(mddev);
219         int i;
220         
221         seq_printf (seq, " [%d/%d] [", conf->raid_disks,
222                                                  conf->working_disks);
223         for (i = 0; i < conf->raid_disks; i++)
224                 seq_printf (seq, "%s",
225                                conf->multipaths[i].rdev && 
226                                conf->multipaths[i].rdev->in_sync ? "U" : "_");
227         seq_printf (seq, "]");
228 }
229
230
231 /*
232  * Careful, this can execute in IRQ contexts as well!
233  */
234 static void multipath_error (mddev_t *mddev, mdk_rdev_t *rdev)
235 {
236         multipath_conf_t *conf = mddev_to_conf(mddev);
237
238         if (conf->working_disks <= 1) {
239                 /*
240                  * Uh oh, we can do nothing if this is our last path, but
241                  * first check if this is a queued request for a device
242                  * which has just failed.
243                  */
244                 printk(KERN_ALERT 
245                         "multipath: only one IO path left and IO error.\n");
246                 /* leave it active... it's all we have */
247         } else {
248                 /*
249                  * Mark disk as unusable
250                  */
251                 if (!rdev->faulty) {
252                         char b[BDEVNAME_SIZE];
253                         rdev->in_sync = 0;
254                         rdev->faulty = 1;
255                         mddev->sb_dirty = 1;
256                         conf->working_disks--;
257                         printk(KERN_ALERT "multipath: IO failure on %s,"
258                                 " disabling IO path. \n Operation continuing"
259                                 " on %d IO paths.\n",
260                                 bdevname (rdev->bdev,b),
261                                 conf->working_disks);
262                 }
263         }
264 }
265
266 static void print_multipath_conf (multipath_conf_t *conf)
267 {
268         int i;
269         struct multipath_info *tmp;
270
271         printk("MULTIPATH conf printout:\n");
272         if (!conf) {
273                 printk("(conf==NULL)\n");
274                 return;
275         }
276         printk(" --- wd:%d rd:%d\n", conf->working_disks,
277                          conf->raid_disks);
278
279         for (i = 0; i < conf->raid_disks; i++) {
280                 char b[BDEVNAME_SIZE];
281                 tmp = conf->multipaths + i;
282                 if (tmp->rdev)
283                         printk(" disk%d, o:%d, dev:%s\n",
284                                 i,!tmp->rdev->faulty,
285                                bdevname(tmp->rdev->bdev,b));
286         }
287 }
288
289
290 static int multipath_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
291 {
292         multipath_conf_t *conf = mddev->private;
293         int found = 0;
294         int path;
295         struct multipath_info *p;
296
297         print_multipath_conf(conf);
298         spin_lock_irq(&conf->device_lock);
299         for (path=0; path<mddev->raid_disks; path++) 
300                 if ((p=conf->multipaths+path)->rdev == NULL) {
301                         p->rdev = rdev;
302                         blk_queue_stack_limits(mddev->queue,
303                                                rdev->bdev->bd_disk->queue);
304
305                 /* as we don't honour merge_bvec_fn, we must never risk
306                  * violating it, so limit ->max_sector to one PAGE, as
307                  * a one page request is never in violation.
308                  * (Note: it is very unlikely that a device with
309                  * merge_bvec_fn will be involved in multipath.)
310                  */
311                         if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
312                             mddev->queue->max_sectors > (PAGE_SIZE>>9))
313                                 mddev->queue->max_sectors = (PAGE_SIZE>>9);
314
315                         conf->working_disks++;
316                         rdev->raid_disk = path;
317                         rdev->in_sync = 1;
318                         found = 1;
319                 }
320         spin_unlock_irq(&conf->device_lock);
321
322         print_multipath_conf(conf);
323         return found;
324 }
325
326 static int multipath_remove_disk(mddev_t *mddev, int number)
327 {
328         multipath_conf_t *conf = mddev->private;
329         int err = 1;
330         struct multipath_info *p = conf->multipaths + number;
331
332         print_multipath_conf(conf);
333         spin_lock_irq(&conf->device_lock);
334
335         if (p->rdev) {
336                 if (p->rdev->in_sync ||
337                     atomic_read(&p->rdev->nr_pending)) {
338                         printk(KERN_ERR "hot-remove-disk, slot %d is identified"                                " but is still operational!\n", number);
339                         err = -EBUSY;
340                         goto abort;
341                 }
342                 p->rdev = NULL;
343                 err = 0;
344         }
345         if (err)
346                 MD_BUG();
347 abort:
348         spin_unlock_irq(&conf->device_lock);
349
350         print_multipath_conf(conf);
351         return err;
352 }
353
354
355
356 /*
357  * This is a kernel thread which:
358  *
359  *      1.      Retries failed read operations on working multipaths.
360  *      2.      Updates the raid superblock when problems encounter.
361  *      3.      Performs writes following reads for array syncronising.
362  */
363
364 static void multipathd (mddev_t *mddev)
365 {
366         struct multipath_bh *mp_bh;
367         struct bio *bio;
368         unsigned long flags;
369         mdk_rdev_t *rdev;
370
371         md_check_recovery(mddev);
372         for (;;) {
373                 char b[BDEVNAME_SIZE];
374                 spin_lock_irqsave(&retry_list_lock, flags);
375                 mp_bh = multipath_retry_list;
376                 if (!mp_bh)
377                         break;
378                 multipath_retry_list = mp_bh->next_mp;
379                 spin_unlock_irqrestore(&retry_list_lock, flags);
380
381                 mddev = mp_bh->mddev;
382                 bio = &mp_bh->bio;
383                 bio->bi_sector = mp_bh->master_bio->bi_sector;
384                 
385                 rdev = NULL;
386                 if (multipath_map (mddev, &rdev)<0) {
387                         printk(KERN_ALERT "multipath: %s: unrecoverable IO read"
388                                 " error for block %llu\n",
389                                 bdevname(bio->bi_bdev,b),
390                                 (unsigned long long)bio->bi_sector);
391                         multipath_end_bh_io(mp_bh, 0);
392                 } else {
393                         printk(KERN_ERR "multipath: %s: redirecting sector %llu"
394                                 " to another IO path\n",
395                                 bdevname(bio->bi_bdev,b),
396                                 (unsigned long long)bio->bi_sector);
397                         bio->bi_bdev = rdev->bdev;
398                         generic_make_request(bio);
399                 }
400         }
401         spin_unlock_irqrestore(&retry_list_lock, flags);
402 }
403
404 static int multipath_run (mddev_t *mddev)
405 {
406         multipath_conf_t *conf;
407         int disk_idx;
408         struct multipath_info *disk;
409         mdk_rdev_t *rdev;
410         struct list_head *tmp;
411
412         if (mddev->level != LEVEL_MULTIPATH) {
413                 printk("multipath: %s: raid level not set to multipath IO (%d)\n",
414                        mdname(mddev), mddev->level);
415                 goto out;
416         }
417         /*
418          * copy the already verified devices into our private MULTIPATH
419          * bookkeeping area. [whatever we allocate in multipath_run(),
420          * should be freed in multipath_stop()]
421          */
422
423         conf = kmalloc(sizeof(multipath_conf_t), GFP_KERNEL);
424         mddev->private = conf;
425         if (!conf) {
426                 printk(KERN_ERR 
427                         "multipath: couldn't allocate memory for %s\n",
428                         mdname(mddev));
429                 goto out;
430         }
431         memset(conf, 0, sizeof(*conf));
432
433         conf->multipaths = kmalloc(sizeof(struct multipath_info)*mddev->raid_disks,
434                                    GFP_KERNEL);
435         if (!conf->multipaths) {
436                 printk(KERN_ERR 
437                         "multipath: couldn't allocate memory for %s\n",
438                         mdname(mddev));
439                 goto out_free_conf;
440         }
441         memset(conf->multipaths, 0, sizeof(struct multipath_info)*mddev->raid_disks);
442
443         mddev->queue->unplug_fn = multipath_unplug;
444
445         conf->working_disks = 0;
446         ITERATE_RDEV(mddev,rdev,tmp) {
447                 disk_idx = rdev->raid_disk;
448                 if (disk_idx < 0 ||
449                     disk_idx >= mddev->raid_disks)
450                         continue;
451
452                 disk = conf->multipaths + disk_idx;
453                 disk->rdev = rdev;
454
455                 blk_queue_stack_limits(mddev->queue,
456                                        rdev->bdev->bd_disk->queue);
457                 /* as we don't honour merge_bvec_fn, we must never risk
458                  * violating it, not that we ever expect a device with
459                  * a merge_bvec_fn to be involved in multipath */
460                 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
461                     mddev->queue->max_sectors > (PAGE_SIZE>>9))
462                         mddev->queue->max_sectors = (PAGE_SIZE>>9);
463
464                 if (!rdev->faulty) 
465                         conf->working_disks++;
466         }
467
468         conf->raid_disks = mddev->raid_disks;
469         mddev->sb_dirty = 1;
470         conf->mddev = mddev;
471         conf->device_lock = SPIN_LOCK_UNLOCKED;
472
473         if (!conf->working_disks) {
474                 printk(KERN_ERR "multipath: no operational IO paths for %s\n",
475                         mdname(mddev));
476                 goto out_free_conf;
477         }
478         mddev->degraded = conf->raid_disks = conf->working_disks;
479
480         conf->pool = mempool_create(NR_RESERVED_BUFS,
481                                     mp_pool_alloc, mp_pool_free,
482                                     NULL);
483         if (conf->pool == NULL) {
484                 printk(KERN_ERR 
485                         "multipath: couldn't allocate memory for %s\n",
486                         mdname(mddev));
487                 goto out_free_conf;
488         }
489
490         {
491                 mddev->thread = md_register_thread(multipathd, mddev, "%s_multipath");
492                 if (!mddev->thread) {
493                         printk(KERN_ERR "multipath: couldn't allocate thread"
494                                 " for %s\n", mdname(mddev));
495                         goto out_free_conf;
496                 }
497         }
498
499         printk(KERN_INFO 
500                 "multipath: array %s active with %d out of %d IO paths\n",
501                 mdname(mddev), conf->working_disks, mddev->raid_disks);
502         /*
503          * Ok, everything is just fine now
504          */
505         mddev->array_size = mddev->size;
506         return 0;
507
508 out_free_conf:
509         if (conf->pool)
510                 mempool_destroy(conf->pool);
511         if (conf->multipaths)
512                 kfree(conf->multipaths);
513         kfree(conf);
514         mddev->private = NULL;
515 out:
516         return -EIO;
517 }
518
519
520 static int multipath_stop (mddev_t *mddev)
521 {
522         multipath_conf_t *conf = mddev_to_conf(mddev);
523
524         md_unregister_thread(mddev->thread);
525         mempool_destroy(conf->pool);
526         kfree(conf->multipaths);
527         kfree(conf);
528         mddev->private = NULL;
529         return 0;
530 }
531
532 static mdk_personality_t multipath_personality=
533 {
534         .name           = "multipath",
535         .owner          = THIS_MODULE,
536         .make_request   = multipath_make_request,
537         .run            = multipath_run,
538         .stop           = multipath_stop,
539         .status         = multipath_status,
540         .error_handler  = multipath_error,
541         .hot_add_disk   = multipath_add_disk,
542         .hot_remove_disk= multipath_remove_disk,
543 };
544
545 static int __init multipath_init (void)
546 {
547         return register_md_personality (MULTIPATH, &multipath_personality);
548 }
549
550 static void __exit multipath_exit (void)
551 {
552         unregister_md_personality (MULTIPATH);
553 }
554
555 module_init(multipath_init);
556 module_exit(multipath_exit);
557 MODULE_LICENSE("GPL");
558 MODULE_ALIAS("md-personality-7"); /* MULTIPATH */