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