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