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)
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
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
);
49 memset(mpb
, 0, sizeof(*mpb
));
53 static void mp_pool_free(void *mpb
, void *data
)
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
) {
73 atomic_inc(&rdev
->nr_pending
);
74 spin_unlock_irq(&conf
->device_lock
);
78 spin_unlock_irq(&conf
->device_lock
);
80 printk(KERN_ERR
"multipath_map(): no more operational IO paths?\n");
84 static void multipath_reschedule_retry (struct multipath_bh
*mp_bh
)
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
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
;
125 multipath_end_bh_io(mp_bh
, uptodate
);
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
);
142 * This routine returns the disk from which the requested read should
146 static int multipath_read_balance (multipath_conf_t
*conf
)
150 for (disk
= 0; disk
< conf
->raid_disks
; disk
++) {
151 mdk_rdev_t
*rdev
= conf
->multipaths
[disk
].rdev
;
152 if (rdev
&& rdev
->in_sync
)
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
);
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
);
188 static void multipath_status (struct seq_file
*seq
, mddev_t
*mddev
)
190 multipath_conf_t
*conf
= mddev_to_conf(mddev
);
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.
217 "multipath: only one IO path left and IO error.\n");
218 /* leave it active... it's all we have */
221 * Mark disk as unusable
224 char b
[BDEVNAME_SIZE
];
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
)
241 struct multipath_info
*tmp
;
243 printk("MULTIPATH conf printout:\n");
245 printk("(conf==NULL)\n");
248 printk(" --- wd:%d rd:%d\n", conf
->working_disks
,
251 for (i
= 0; i
< conf
->raid_disks
; i
++) {
252 char b
[BDEVNAME_SIZE
];
253 tmp
= conf
->multipaths
+ i
;
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;
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
) {
274 conf
->working_disks
++;
275 rdev
->raid_disk
= path
;
279 spin_unlock_irq(&conf
->device_lock
);
281 print_multipath_conf(conf
);
285 static int multipath_remove_disk(mddev_t
*mddev
, int number
)
287 multipath_conf_t
*conf
= mddev
->private;
289 struct multipath_info
*p
= conf
->multipaths
+ number
;
291 print_multipath_conf(conf
);
292 spin_lock_irq(&conf
->device_lock
);
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
);
307 spin_unlock_irq(&conf
->device_lock
);
309 print_multipath_conf(conf
);
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
;
330 md_check_recovery(mddev
);
332 char b
[BDEVNAME_SIZE
];
333 spin_lock_irqsave(&retry_list_lock
, flags
);
334 mp_bh
= multipath_retry_list
;
337 multipath_retry_list
= mp_bh
->next_mp
;
338 spin_unlock_irqrestore(&retry_list_lock
, flags
);
340 mddev
= mp_bh
->mddev
;
342 bio
->bi_sector
= mp_bh
->master_bio
->bi_sector
;
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);
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
;
367 struct multipath_info
*disk
;
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
);
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
;
386 "multipath: couldn't allocate memory for md%d\n",
390 memset(conf
, 0, sizeof(*conf
));
392 conf
->multipaths
= kmalloc(sizeof(struct multipath_info
)*mddev
->raid_disks
,
394 if (!conf
->multipaths
) {
396 "multipath: couldn't allocate memory for md%d\n",
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
;
406 disk_idx
>= mddev
->raid_disks
)
409 disk
= conf
->multipaths
+ disk_idx
;
412 conf
->working_disks
++;
415 conf
->raid_disks
= mddev
->raid_disks
;
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",
425 mddev
->degraded
= conf
->raid_disks
= conf
->working_disks
;
427 conf
->pool
= mempool_create(NR_RESERVED_BUFS
,
428 mp_pool_alloc
, mp_pool_free
,
430 if (conf
->pool
== NULL
) {
432 "multipath: couldn't allocate memory for md%d\n",
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
));
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
;
459 mempool_destroy(conf
->pool
);
460 if (conf
->multipaths
)
461 kfree(conf
->multipaths
);
463 mddev
->private = NULL
;
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
);
477 mddev
->private = NULL
;
481 static mdk_personality_t multipath_personality
=
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");