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