xref: /linux/drivers/md/raid0.c (revision 44eeab67416711db9b84610ef18c99a60415dff8)
1 /*
2    raid0.c : Multiple Devices driver for Linux
3              Copyright (C) 1994-96 Marc ZYNGIER
4 	     <zyngier@ufr-info-p7.ibp.fr> or
5 	     <maz@gloups.fdn.fr>
6              Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
7 
8 
9    RAID-0 management functions.
10 
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15 
16    You should have received a copy of the GNU General Public License
17    (for example /usr/src/linux/COPYING); if not, write to the Free
18    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #include <linux/blkdev.h>
22 #include <linux/seq_file.h>
23 #include "md.h"
24 #include "raid0.h"
25 
26 static void raid0_unplug(struct request_queue *q)
27 {
28 	mddev_t *mddev = q->queuedata;
29 	raid0_conf_t *conf = mddev->private;
30 	mdk_rdev_t **devlist = conf->devlist;
31 	int i;
32 
33 	for (i=0; i<mddev->raid_disks; i++) {
34 		struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
35 
36 		blk_unplug(r_queue);
37 	}
38 }
39 
40 static int raid0_congested(void *data, int bits)
41 {
42 	mddev_t *mddev = data;
43 	raid0_conf_t *conf = mddev->private;
44 	mdk_rdev_t **devlist = conf->devlist;
45 	int i, ret = 0;
46 
47 	for (i = 0; i < mddev->raid_disks && !ret ; i++) {
48 		struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
49 
50 		ret |= bdi_congested(&q->backing_dev_info, bits);
51 	}
52 	return ret;
53 }
54 
55 /*
56  * inform the user of the raid configuration
57 */
58 static void dump_zones(mddev_t *mddev)
59 {
60 	int j, k, h;
61 	sector_t zone_size = 0;
62 	sector_t zone_start = 0;
63 	char b[BDEVNAME_SIZE];
64 	raid0_conf_t *conf = mddev->private;
65 	printk(KERN_INFO "******* %s configuration *********\n",
66 		mdname(mddev));
67 	h = 0;
68 	for (j = 0; j < conf->nr_strip_zones; j++) {
69 		printk(KERN_INFO "zone%d=[", j);
70 		for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
71 			printk("%s/",
72 			bdevname(conf->devlist[j*mddev->raid_disks
73 						+ k]->bdev, b));
74 		printk("]\n");
75 
76 		zone_size  = conf->strip_zone[j].zone_end - zone_start;
77 		printk(KERN_INFO "        zone offset=%llukb "
78 				"device offset=%llukb size=%llukb\n",
79 			(unsigned long long)zone_start>>1,
80 			(unsigned long long)conf->strip_zone[j].dev_start>>1,
81 			(unsigned long long)zone_size>>1);
82 		zone_start = conf->strip_zone[j].zone_end;
83 	}
84 	printk(KERN_INFO "**********************************\n\n");
85 }
86 
87 static int create_strip_zones(mddev_t *mddev)
88 {
89 	int i, c, j, err;
90 	sector_t curr_zone_end, sectors;
91 	mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev, **dev;
92 	struct strip_zone *zone;
93 	int cnt;
94 	char b[BDEVNAME_SIZE];
95 	raid0_conf_t *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
96 
97 	if (!conf)
98 		return -ENOMEM;
99 	list_for_each_entry(rdev1, &mddev->disks, same_set) {
100 		printk(KERN_INFO "raid0: looking at %s\n",
101 			bdevname(rdev1->bdev,b));
102 		c = 0;
103 
104 		/* round size to chunk_size */
105 		sectors = rdev1->sectors;
106 		sector_div(sectors, mddev->chunk_sectors);
107 		rdev1->sectors = sectors * mddev->chunk_sectors;
108 
109 		list_for_each_entry(rdev2, &mddev->disks, same_set) {
110 			printk(KERN_INFO "raid0:   comparing %s(%llu)",
111 			       bdevname(rdev1->bdev,b),
112 			       (unsigned long long)rdev1->sectors);
113 			printk(KERN_INFO " with %s(%llu)\n",
114 			       bdevname(rdev2->bdev,b),
115 			       (unsigned long long)rdev2->sectors);
116 			if (rdev2 == rdev1) {
117 				printk(KERN_INFO "raid0:   END\n");
118 				break;
119 			}
120 			if (rdev2->sectors == rdev1->sectors) {
121 				/*
122 				 * Not unique, don't count it as a new
123 				 * group
124 				 */
125 				printk(KERN_INFO "raid0:   EQUAL\n");
126 				c = 1;
127 				break;
128 			}
129 			printk(KERN_INFO "raid0:   NOT EQUAL\n");
130 		}
131 		if (!c) {
132 			printk(KERN_INFO "raid0:   ==> UNIQUE\n");
133 			conf->nr_strip_zones++;
134 			printk(KERN_INFO "raid0: %d zones\n",
135 				conf->nr_strip_zones);
136 		}
137 	}
138 	printk(KERN_INFO "raid0: FINAL %d zones\n", conf->nr_strip_zones);
139 	err = -ENOMEM;
140 	conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
141 				conf->nr_strip_zones, GFP_KERNEL);
142 	if (!conf->strip_zone)
143 		goto abort;
144 	conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
145 				conf->nr_strip_zones*mddev->raid_disks,
146 				GFP_KERNEL);
147 	if (!conf->devlist)
148 		goto abort;
149 
150 	/* The first zone must contain all devices, so here we check that
151 	 * there is a proper alignment of slots to devices and find them all
152 	 */
153 	zone = &conf->strip_zone[0];
154 	cnt = 0;
155 	smallest = NULL;
156 	dev = conf->devlist;
157 	err = -EINVAL;
158 	list_for_each_entry(rdev1, &mddev->disks, same_set) {
159 		int j = rdev1->raid_disk;
160 
161 		if (j < 0 || j >= mddev->raid_disks) {
162 			printk(KERN_ERR "raid0: bad disk number %d - "
163 				"aborting!\n", j);
164 			goto abort;
165 		}
166 		if (dev[j]) {
167 			printk(KERN_ERR "raid0: multiple devices for %d - "
168 				"aborting!\n", j);
169 			goto abort;
170 		}
171 		dev[j] = rdev1;
172 
173 		blk_queue_stack_limits(mddev->queue,
174 				       rdev1->bdev->bd_disk->queue);
175 		/* as we don't honour merge_bvec_fn, we must never risk
176 		 * violating it, so limit ->max_sector to one PAGE, as
177 		 * a one page request is never in violation.
178 		 */
179 
180 		if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
181 		    queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
182 			blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
183 
184 		if (!smallest || (rdev1->sectors < smallest->sectors))
185 			smallest = rdev1;
186 		cnt++;
187 	}
188 	if (cnt != mddev->raid_disks) {
189 		printk(KERN_ERR "raid0: too few disks (%d of %d) - "
190 			"aborting!\n", cnt, mddev->raid_disks);
191 		goto abort;
192 	}
193 	zone->nb_dev = cnt;
194 	zone->zone_end = smallest->sectors * cnt;
195 
196 	curr_zone_end = zone->zone_end;
197 
198 	/* now do the other zones */
199 	for (i = 1; i < conf->nr_strip_zones; i++)
200 	{
201 		zone = conf->strip_zone + i;
202 		dev = conf->devlist + i * mddev->raid_disks;
203 
204 		printk(KERN_INFO "raid0: zone %d\n", i);
205 		zone->dev_start = smallest->sectors;
206 		smallest = NULL;
207 		c = 0;
208 
209 		for (j=0; j<cnt; j++) {
210 			char b[BDEVNAME_SIZE];
211 			rdev = conf->devlist[j];
212 			printk(KERN_INFO "raid0: checking %s ...",
213 				bdevname(rdev->bdev, b));
214 			if (rdev->sectors <= zone->dev_start) {
215 				printk(KERN_INFO " nope.\n");
216 				continue;
217 			}
218 			printk(KERN_INFO " contained as device %d\n", c);
219 			dev[c] = rdev;
220 			c++;
221 			if (!smallest || rdev->sectors < smallest->sectors) {
222 				smallest = rdev;
223 				printk(KERN_INFO "  (%llu) is smallest!.\n",
224 					(unsigned long long)rdev->sectors);
225 			}
226 		}
227 
228 		zone->nb_dev = c;
229 		sectors = (smallest->sectors - zone->dev_start) * c;
230 		printk(KERN_INFO "raid0: zone->nb_dev: %d, sectors: %llu\n",
231 			zone->nb_dev, (unsigned long long)sectors);
232 
233 		curr_zone_end += sectors;
234 		zone->zone_end = curr_zone_end;
235 
236 		printk(KERN_INFO "raid0: current zone start: %llu\n",
237 			(unsigned long long)smallest->sectors);
238 	}
239 	mddev->queue->unplug_fn = raid0_unplug;
240 	mddev->queue->backing_dev_info.congested_fn = raid0_congested;
241 	mddev->queue->backing_dev_info.congested_data = mddev;
242 
243 	/*
244 	 * now since we have the hard sector sizes, we can make sure
245 	 * chunk size is a multiple of that sector size
246 	 */
247 	if ((mddev->chunk_sectors << 9) % queue_logical_block_size(mddev->queue)) {
248 		printk(KERN_ERR "%s chunk_size of %d not valid\n",
249 		       mdname(mddev),
250 		       mddev->chunk_sectors << 9);
251 		goto abort;
252 	}
253 	printk(KERN_INFO "raid0: done.\n");
254 	mddev->private = conf;
255 	return 0;
256 abort:
257 	kfree(conf->strip_zone);
258 	kfree(conf->devlist);
259 	kfree(conf);
260 	mddev->private = NULL;
261 	return err;
262 }
263 
264 /**
265  *	raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
266  *	@q: request queue
267  *	@bvm: properties of new bio
268  *	@biovec: the request that could be merged to it.
269  *
270  *	Return amount of bytes we can accept at this offset
271  */
272 static int raid0_mergeable_bvec(struct request_queue *q,
273 				struct bvec_merge_data *bvm,
274 				struct bio_vec *biovec)
275 {
276 	mddev_t *mddev = q->queuedata;
277 	sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
278 	int max;
279 	unsigned int chunk_sectors = mddev->chunk_sectors;
280 	unsigned int bio_sectors = bvm->bi_size >> 9;
281 
282 	if (is_power_of_2(chunk_sectors))
283 		max =  (chunk_sectors - ((sector & (chunk_sectors-1))
284 						+ bio_sectors)) << 9;
285 	else
286 		max =  (chunk_sectors - (sector_div(sector, chunk_sectors)
287 						+ bio_sectors)) << 9;
288 	if (max < 0) max = 0; /* bio_add cannot handle a negative return */
289 	if (max <= biovec->bv_len && bio_sectors == 0)
290 		return biovec->bv_len;
291 	else
292 		return max;
293 }
294 
295 static sector_t raid0_size(mddev_t *mddev, sector_t sectors, int raid_disks)
296 {
297 	sector_t array_sectors = 0;
298 	mdk_rdev_t *rdev;
299 
300 	WARN_ONCE(sectors || raid_disks,
301 		  "%s does not support generic reshape\n", __func__);
302 
303 	list_for_each_entry(rdev, &mddev->disks, same_set)
304 		array_sectors += rdev->sectors;
305 
306 	return array_sectors;
307 }
308 
309 static int raid0_run(mddev_t *mddev)
310 {
311 	int ret;
312 
313 	if (mddev->chunk_sectors == 0) {
314 		printk(KERN_ERR "md/raid0: chunk size must be set.\n");
315 		return -EINVAL;
316 	}
317 	if (md_check_no_bitmap(mddev))
318 		return -EINVAL;
319 	blk_queue_max_sectors(mddev->queue, mddev->chunk_sectors);
320 	mddev->queue->queue_lock = &mddev->queue->__queue_lock;
321 
322 	ret = create_strip_zones(mddev);
323 	if (ret < 0)
324 		return ret;
325 
326 	/* calculate array device size */
327 	md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
328 
329 	printk(KERN_INFO "raid0 : md_size is %llu sectors.\n",
330 		(unsigned long long)mddev->array_sectors);
331 	/* calculate the max read-ahead size.
332 	 * For read-ahead of large files to be effective, we need to
333 	 * readahead at least twice a whole stripe. i.e. number of devices
334 	 * multiplied by chunk size times 2.
335 	 * If an individual device has an ra_pages greater than the
336 	 * chunk size, then we will not drive that device as hard as it
337 	 * wants.  We consider this a configuration error: a larger
338 	 * chunksize should be used in that case.
339 	 */
340 	{
341 		int stripe = mddev->raid_disks *
342 			(mddev->chunk_sectors << 9) / PAGE_SIZE;
343 		if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
344 			mddev->queue->backing_dev_info.ra_pages = 2* stripe;
345 	}
346 
347 	blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
348 	dump_zones(mddev);
349 	return 0;
350 }
351 
352 static int raid0_stop(mddev_t *mddev)
353 {
354 	raid0_conf_t *conf = mddev->private;
355 
356 	blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
357 	kfree(conf->strip_zone);
358 	kfree(conf->devlist);
359 	kfree(conf);
360 	mddev->private = NULL;
361 	return 0;
362 }
363 
364 /* Find the zone which holds a particular offset
365  * Update *sectorp to be an offset in that zone
366  */
367 static struct strip_zone *find_zone(struct raid0_private_data *conf,
368 				    sector_t *sectorp)
369 {
370 	int i;
371 	struct strip_zone *z = conf->strip_zone;
372 	sector_t sector = *sectorp;
373 
374 	for (i = 0; i < conf->nr_strip_zones; i++)
375 		if (sector < z[i].zone_end) {
376 			if (i)
377 				*sectorp = sector - z[i-1].zone_end;
378 			return z + i;
379 		}
380 	BUG();
381 }
382 
383 /*
384  * remaps the bio to the target device. we separate two flows.
385  * power 2 flow and a general flow for the sake of perfromance
386 */
387 static mdk_rdev_t *map_sector(mddev_t *mddev, struct strip_zone *zone,
388 				sector_t sector, sector_t *sector_offset)
389 {
390 	unsigned int sect_in_chunk;
391 	sector_t chunk;
392 	raid0_conf_t *conf = mddev->private;
393 	unsigned int chunk_sects = mddev->chunk_sectors;
394 
395 	if (is_power_of_2(chunk_sects)) {
396 		int chunksect_bits = ffz(~chunk_sects);
397 		/* find the sector offset inside the chunk */
398 		sect_in_chunk  = sector & (chunk_sects - 1);
399 		sector >>= chunksect_bits;
400 		/* chunk in zone */
401 		chunk = *sector_offset;
402 		/* quotient is the chunk in real device*/
403 		sector_div(chunk, zone->nb_dev << chunksect_bits);
404 	} else{
405 		sect_in_chunk = sector_div(sector, chunk_sects);
406 		chunk = *sector_offset;
407 		sector_div(chunk, chunk_sects * zone->nb_dev);
408 	}
409 	/*
410 	*  position the bio over the real device
411 	*  real sector = chunk in device + starting of zone
412 	*	+ the position in the chunk
413 	*/
414 	*sector_offset = (chunk * chunk_sects) + sect_in_chunk;
415 	return conf->devlist[(zone - conf->strip_zone)*mddev->raid_disks
416 			     + sector_div(sector, zone->nb_dev)];
417 }
418 
419 /*
420  * Is io distribute over 1 or more chunks ?
421 */
422 static inline int is_io_in_chunk_boundary(mddev_t *mddev,
423 			unsigned int chunk_sects, struct bio *bio)
424 {
425 	if (likely(is_power_of_2(chunk_sects))) {
426 		return chunk_sects >= ((bio->bi_sector & (chunk_sects-1))
427 					+ (bio->bi_size >> 9));
428 	} else{
429 		sector_t sector = bio->bi_sector;
430 		return chunk_sects >= (sector_div(sector, chunk_sects)
431 						+ (bio->bi_size >> 9));
432 	}
433 }
434 
435 static int raid0_make_request(struct request_queue *q, struct bio *bio)
436 {
437 	mddev_t *mddev = q->queuedata;
438 	unsigned int chunk_sects;
439 	sector_t sector_offset;
440 	struct strip_zone *zone;
441 	mdk_rdev_t *tmp_dev;
442 	const int rw = bio_data_dir(bio);
443 	int cpu;
444 
445 	if (unlikely(bio_barrier(bio))) {
446 		bio_endio(bio, -EOPNOTSUPP);
447 		return 0;
448 	}
449 
450 	cpu = part_stat_lock();
451 	part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
452 	part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
453 		      bio_sectors(bio));
454 	part_stat_unlock();
455 
456 	chunk_sects = mddev->chunk_sectors;
457 	if (unlikely(!is_io_in_chunk_boundary(mddev, chunk_sects, bio))) {
458 		sector_t sector = bio->bi_sector;
459 		struct bio_pair *bp;
460 		/* Sanity check -- queue functions should prevent this happening */
461 		if (bio->bi_vcnt != 1 ||
462 		    bio->bi_idx != 0)
463 			goto bad_map;
464 		/* This is a one page bio that upper layers
465 		 * refuse to split for us, so we need to split it.
466 		 */
467 		if (likely(is_power_of_2(chunk_sects)))
468 			bp = bio_split(bio, chunk_sects - (sector &
469 							   (chunk_sects-1)));
470 		else
471 			bp = bio_split(bio, chunk_sects -
472 				       sector_div(sector, chunk_sects));
473 		if (raid0_make_request(q, &bp->bio1))
474 			generic_make_request(&bp->bio1);
475 		if (raid0_make_request(q, &bp->bio2))
476 			generic_make_request(&bp->bio2);
477 
478 		bio_pair_release(bp);
479 		return 0;
480 	}
481 
482 	sector_offset = bio->bi_sector;
483 	zone =  find_zone(mddev->private, &sector_offset);
484 	tmp_dev = map_sector(mddev, zone, bio->bi_sector,
485 			     &sector_offset);
486 	bio->bi_bdev = tmp_dev->bdev;
487 	bio->bi_sector = sector_offset + zone->dev_start +
488 		tmp_dev->data_offset;
489 	/*
490 	 * Let the main block layer submit the IO and resolve recursion:
491 	 */
492 	return 1;
493 
494 bad_map:
495 	printk("raid0_make_request bug: can't convert block across chunks"
496 		" or bigger than %dk %llu %d\n", chunk_sects / 2,
497 		(unsigned long long)bio->bi_sector, bio->bi_size >> 10);
498 
499 	bio_io_error(bio);
500 	return 0;
501 }
502 
503 static void raid0_status(struct seq_file *seq, mddev_t *mddev)
504 {
505 #undef MD_DEBUG
506 #ifdef MD_DEBUG
507 	int j, k, h;
508 	char b[BDEVNAME_SIZE];
509 	raid0_conf_t *conf = mddev->private;
510 
511 	sector_t zone_size;
512 	sector_t zone_start = 0;
513 	h = 0;
514 
515 	for (j = 0; j < conf->nr_strip_zones; j++) {
516 		seq_printf(seq, "      z%d", j);
517 		seq_printf(seq, "=[");
518 		for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
519 			seq_printf(seq, "%s/", bdevname(
520 				conf->devlist[j*mddev->raid_disks + k]
521 						->bdev, b));
522 
523 		zone_size  = conf->strip_zone[j].zone_end - zone_start;
524 		seq_printf(seq, "] ze=%lld ds=%lld s=%lld\n",
525 			(unsigned long long)zone_start>>1,
526 			(unsigned long long)conf->strip_zone[j].dev_start>>1,
527 			(unsigned long long)zone_size>>1);
528 		zone_start = conf->strip_zone[j].zone_end;
529 	}
530 #endif
531 	seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
532 	return;
533 }
534 
535 static struct mdk_personality raid0_personality=
536 {
537 	.name		= "raid0",
538 	.level		= 0,
539 	.owner		= THIS_MODULE,
540 	.make_request	= raid0_make_request,
541 	.run		= raid0_run,
542 	.stop		= raid0_stop,
543 	.status		= raid0_status,
544 	.size		= raid0_size,
545 };
546 
547 static int __init raid0_init (void)
548 {
549 	return register_md_personality (&raid0_personality);
550 }
551 
552 static void raid0_exit (void)
553 {
554 	unregister_md_personality (&raid0_personality);
555 }
556 
557 module_init(raid0_init);
558 module_exit(raid0_exit);
559 MODULE_LICENSE("GPL");
560 MODULE_ALIAS("md-personality-2"); /* RAID0 */
561 MODULE_ALIAS("md-raid0");
562 MODULE_ALIAS("md-level-0");
563