xref: /openbmc/linux/drivers/md/raid0.c (revision 98366c20)
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/module.h>
22 #include <linux/raid/raid0.h>
23 
24 #define MAJOR_NR MD_MAJOR
25 #define MD_DRIVER
26 #define MD_PERSONALITY
27 
28 static void raid0_unplug(struct request_queue *q)
29 {
30 	mddev_t *mddev = q->queuedata;
31 	raid0_conf_t *conf = mddev_to_conf(mddev);
32 	mdk_rdev_t **devlist = conf->strip_zone[0].dev;
33 	int i;
34 
35 	for (i=0; i<mddev->raid_disks; i++) {
36 		struct request_queue *r_queue = bdev_get_queue(devlist[i]->bdev);
37 
38 		if (r_queue->unplug_fn)
39 			r_queue->unplug_fn(r_queue);
40 	}
41 }
42 
43 static int raid0_congested(void *data, int bits)
44 {
45 	mddev_t *mddev = data;
46 	raid0_conf_t *conf = mddev_to_conf(mddev);
47 	mdk_rdev_t **devlist = conf->strip_zone[0].dev;
48 	int i, ret = 0;
49 
50 	for (i = 0; i < mddev->raid_disks && !ret ; i++) {
51 		struct request_queue *q = bdev_get_queue(devlist[i]->bdev);
52 
53 		ret |= bdi_congested(&q->backing_dev_info, bits);
54 	}
55 	return ret;
56 }
57 
58 
59 static int create_strip_zones (mddev_t *mddev)
60 {
61 	int i, c, j;
62 	sector_t current_offset, curr_zone_offset;
63 	sector_t min_spacing;
64 	raid0_conf_t *conf = mddev_to_conf(mddev);
65 	mdk_rdev_t *smallest, *rdev1, *rdev2, *rdev;
66 	struct list_head *tmp1, *tmp2;
67 	struct strip_zone *zone;
68 	int cnt;
69 	char b[BDEVNAME_SIZE];
70 
71 	/*
72 	 * The number of 'same size groups'
73 	 */
74 	conf->nr_strip_zones = 0;
75 
76 	ITERATE_RDEV(mddev,rdev1,tmp1) {
77 		printk("raid0: looking at %s\n",
78 			bdevname(rdev1->bdev,b));
79 		c = 0;
80 		ITERATE_RDEV(mddev,rdev2,tmp2) {
81 			printk("raid0:   comparing %s(%llu)",
82 			       bdevname(rdev1->bdev,b),
83 			       (unsigned long long)rdev1->size);
84 			printk(" with %s(%llu)\n",
85 			       bdevname(rdev2->bdev,b),
86 			       (unsigned long long)rdev2->size);
87 			if (rdev2 == rdev1) {
88 				printk("raid0:   END\n");
89 				break;
90 			}
91 			if (rdev2->size == rdev1->size)
92 			{
93 				/*
94 				 * Not unique, don't count it as a new
95 				 * group
96 				 */
97 				printk("raid0:   EQUAL\n");
98 				c = 1;
99 				break;
100 			}
101 			printk("raid0:   NOT EQUAL\n");
102 		}
103 		if (!c) {
104 			printk("raid0:   ==> UNIQUE\n");
105 			conf->nr_strip_zones++;
106 			printk("raid0: %d zones\n", conf->nr_strip_zones);
107 		}
108 	}
109 	printk("raid0: FINAL %d zones\n", conf->nr_strip_zones);
110 
111 	conf->strip_zone = kzalloc(sizeof(struct strip_zone)*
112 				conf->nr_strip_zones, GFP_KERNEL);
113 	if (!conf->strip_zone)
114 		return 1;
115 	conf->devlist = kzalloc(sizeof(mdk_rdev_t*)*
116 				conf->nr_strip_zones*mddev->raid_disks,
117 				GFP_KERNEL);
118 	if (!conf->devlist)
119 		return 1;
120 
121 	/* The first zone must contain all devices, so here we check that
122 	 * there is a proper alignment of slots to devices and find them all
123 	 */
124 	zone = &conf->strip_zone[0];
125 	cnt = 0;
126 	smallest = NULL;
127 	zone->dev = conf->devlist;
128 	ITERATE_RDEV(mddev, rdev1, tmp1) {
129 		int j = rdev1->raid_disk;
130 
131 		if (j < 0 || j >= mddev->raid_disks) {
132 			printk("raid0: bad disk number %d - aborting!\n", j);
133 			goto abort;
134 		}
135 		if (zone->dev[j]) {
136 			printk("raid0: multiple devices for %d - aborting!\n",
137 				j);
138 			goto abort;
139 		}
140 		zone->dev[j] = rdev1;
141 
142 		blk_queue_stack_limits(mddev->queue,
143 				       rdev1->bdev->bd_disk->queue);
144 		/* as we don't honour merge_bvec_fn, we must never risk
145 		 * violating it, so limit ->max_sector to one PAGE, as
146 		 * a one page request is never in violation.
147 		 */
148 
149 		if (rdev1->bdev->bd_disk->queue->merge_bvec_fn &&
150 		    mddev->queue->max_sectors > (PAGE_SIZE>>9))
151 			blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
152 
153 		if (!smallest || (rdev1->size <smallest->size))
154 			smallest = rdev1;
155 		cnt++;
156 	}
157 	if (cnt != mddev->raid_disks) {
158 		printk("raid0: too few disks (%d of %d) - aborting!\n",
159 			cnt, mddev->raid_disks);
160 		goto abort;
161 	}
162 	zone->nb_dev = cnt;
163 	zone->size = smallest->size * cnt;
164 	zone->zone_offset = 0;
165 
166 	current_offset = smallest->size;
167 	curr_zone_offset = zone->size;
168 
169 	/* now do the other zones */
170 	for (i = 1; i < conf->nr_strip_zones; i++)
171 	{
172 		zone = conf->strip_zone + i;
173 		zone->dev = conf->strip_zone[i-1].dev + mddev->raid_disks;
174 
175 		printk("raid0: zone %d\n", i);
176 		zone->dev_offset = current_offset;
177 		smallest = NULL;
178 		c = 0;
179 
180 		for (j=0; j<cnt; j++) {
181 			char b[BDEVNAME_SIZE];
182 			rdev = conf->strip_zone[0].dev[j];
183 			printk("raid0: checking %s ...", bdevname(rdev->bdev,b));
184 			if (rdev->size > current_offset)
185 			{
186 				printk(" contained as device %d\n", c);
187 				zone->dev[c] = rdev;
188 				c++;
189 				if (!smallest || (rdev->size <smallest->size)) {
190 					smallest = rdev;
191 					printk("  (%llu) is smallest!.\n",
192 						(unsigned long long)rdev->size);
193 				}
194 			} else
195 				printk(" nope.\n");
196 		}
197 
198 		zone->nb_dev = c;
199 		zone->size = (smallest->size - current_offset) * c;
200 		printk("raid0: zone->nb_dev: %d, size: %llu\n",
201 			zone->nb_dev, (unsigned long long)zone->size);
202 
203 		zone->zone_offset = curr_zone_offset;
204 		curr_zone_offset += zone->size;
205 
206 		current_offset = smallest->size;
207 		printk("raid0: current zone offset: %llu\n",
208 			(unsigned long long)current_offset);
209 	}
210 
211 	/* Now find appropriate hash spacing.
212 	 * We want a number which causes most hash entries to cover
213 	 * at most two strips, but the hash table must be at most
214 	 * 1 PAGE.  We choose the smallest strip, or contiguous collection
215 	 * of strips, that has big enough size.  We never consider the last
216 	 * strip though as it's size has no bearing on the efficacy of the hash
217 	 * table.
218 	 */
219 	conf->hash_spacing = curr_zone_offset;
220 	min_spacing = curr_zone_offset;
221 	sector_div(min_spacing, PAGE_SIZE/sizeof(struct strip_zone*));
222 	for (i=0; i < conf->nr_strip_zones-1; i++) {
223 		sector_t sz = 0;
224 		for (j=i; j<conf->nr_strip_zones-1 &&
225 			     sz < min_spacing ; j++)
226 			sz += conf->strip_zone[j].size;
227 		if (sz >= min_spacing && sz < conf->hash_spacing)
228 			conf->hash_spacing = sz;
229 	}
230 
231 	mddev->queue->unplug_fn = raid0_unplug;
232 
233 	mddev->queue->backing_dev_info.congested_fn = raid0_congested;
234 	mddev->queue->backing_dev_info.congested_data = mddev;
235 
236 	printk("raid0: done.\n");
237 	return 0;
238  abort:
239 	return 1;
240 }
241 
242 /**
243  *	raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
244  *	@q: request queue
245  *	@bio: the buffer head that's been built up so far
246  *	@biovec: the request that could be merged to it.
247  *
248  *	Return amount of bytes we can accept at this offset
249  */
250 static int raid0_mergeable_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *biovec)
251 {
252 	mddev_t *mddev = q->queuedata;
253 	sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
254 	int max;
255 	unsigned int chunk_sectors = mddev->chunk_size >> 9;
256 	unsigned int bio_sectors = bio->bi_size >> 9;
257 
258 	max =  (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
259 	if (max < 0) max = 0; /* bio_add cannot handle a negative return */
260 	if (max <= biovec->bv_len && bio_sectors == 0)
261 		return biovec->bv_len;
262 	else
263 		return max;
264 }
265 
266 static int raid0_run (mddev_t *mddev)
267 {
268 	unsigned  cur=0, i=0, nb_zone;
269 	s64 size;
270 	raid0_conf_t *conf;
271 	mdk_rdev_t *rdev;
272 	struct list_head *tmp;
273 
274 	if (mddev->chunk_size == 0) {
275 		printk(KERN_ERR "md/raid0: non-zero chunk size required.\n");
276 		return -EINVAL;
277 	}
278 	printk(KERN_INFO "%s: setting max_sectors to %d, segment boundary to %d\n",
279 	       mdname(mddev),
280 	       mddev->chunk_size >> 9,
281 	       (mddev->chunk_size>>1)-1);
282 	blk_queue_max_sectors(mddev->queue, mddev->chunk_size >> 9);
283 	blk_queue_segment_boundary(mddev->queue, (mddev->chunk_size>>1) - 1);
284 
285 	conf = kmalloc(sizeof (raid0_conf_t), GFP_KERNEL);
286 	if (!conf)
287 		goto out;
288 	mddev->private = (void *)conf;
289 
290 	conf->strip_zone = NULL;
291 	conf->devlist = NULL;
292 	if (create_strip_zones (mddev))
293 		goto out_free_conf;
294 
295 	/* calculate array device size */
296 	mddev->array_size = 0;
297 	ITERATE_RDEV(mddev,rdev,tmp)
298 		mddev->array_size += rdev->size;
299 
300 	printk("raid0 : md_size is %llu blocks.\n",
301 		(unsigned long long)mddev->array_size);
302 	printk("raid0 : conf->hash_spacing is %llu blocks.\n",
303 		(unsigned long long)conf->hash_spacing);
304 	{
305 		sector_t s = mddev->array_size;
306 		sector_t space = conf->hash_spacing;
307 		int round;
308 		conf->preshift = 0;
309 		if (sizeof(sector_t) > sizeof(u32)) {
310 			/*shift down space and s so that sector_div will work */
311 			while (space > (sector_t) (~(u32)0)) {
312 				s >>= 1;
313 				space >>= 1;
314 				s += 1; /* force round-up */
315 				conf->preshift++;
316 			}
317 		}
318 		round = sector_div(s, (u32)space) ? 1 : 0;
319 		nb_zone = s + round;
320 	}
321 	printk("raid0 : nb_zone is %d.\n", nb_zone);
322 
323 	printk("raid0 : Allocating %Zd bytes for hash.\n",
324 				nb_zone*sizeof(struct strip_zone*));
325 	conf->hash_table = kmalloc (sizeof (struct strip_zone *)*nb_zone, GFP_KERNEL);
326 	if (!conf->hash_table)
327 		goto out_free_conf;
328 	size = conf->strip_zone[cur].size;
329 
330 	conf->hash_table[0] = conf->strip_zone + cur;
331 	for (i=1; i< nb_zone; i++) {
332 		while (size <= conf->hash_spacing) {
333 			cur++;
334 			size += conf->strip_zone[cur].size;
335 		}
336 		size -= conf->hash_spacing;
337 		conf->hash_table[i] = conf->strip_zone + cur;
338 	}
339 	if (conf->preshift) {
340 		conf->hash_spacing >>= conf->preshift;
341 		/* round hash_spacing up so when we divide by it, we
342 		 * err on the side of too-low, which is safest
343 		 */
344 		conf->hash_spacing++;
345 	}
346 
347 	/* calculate the max read-ahead size.
348 	 * For read-ahead of large files to be effective, we need to
349 	 * readahead at least twice a whole stripe. i.e. number of devices
350 	 * multiplied by chunk size times 2.
351 	 * If an individual device has an ra_pages greater than the
352 	 * chunk size, then we will not drive that device as hard as it
353 	 * wants.  We consider this a configuration error: a larger
354 	 * chunksize should be used in that case.
355 	 */
356 	{
357 		int stripe = mddev->raid_disks * mddev->chunk_size / PAGE_SIZE;
358 		if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
359 			mddev->queue->backing_dev_info.ra_pages = 2* stripe;
360 	}
361 
362 
363 	blk_queue_merge_bvec(mddev->queue, raid0_mergeable_bvec);
364 	return 0;
365 
366 out_free_conf:
367 	kfree(conf->strip_zone);
368 	kfree(conf->devlist);
369 	kfree(conf);
370 	mddev->private = NULL;
371 out:
372 	return -ENOMEM;
373 }
374 
375 static int raid0_stop (mddev_t *mddev)
376 {
377 	raid0_conf_t *conf = mddev_to_conf(mddev);
378 
379 	blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
380 	kfree(conf->hash_table);
381 	conf->hash_table = NULL;
382 	kfree(conf->strip_zone);
383 	conf->strip_zone = NULL;
384 	kfree(conf);
385 	mddev->private = NULL;
386 
387 	return 0;
388 }
389 
390 static int raid0_make_request (struct request_queue *q, struct bio *bio)
391 {
392 	mddev_t *mddev = q->queuedata;
393 	unsigned int sect_in_chunk, chunksize_bits,  chunk_size, chunk_sects;
394 	raid0_conf_t *conf = mddev_to_conf(mddev);
395 	struct strip_zone *zone;
396 	mdk_rdev_t *tmp_dev;
397 	sector_t chunk;
398 	sector_t block, rsect;
399 	const int rw = bio_data_dir(bio);
400 
401 	if (unlikely(bio_barrier(bio))) {
402 		bio_endio(bio, -EOPNOTSUPP);
403 		return 0;
404 	}
405 
406 	disk_stat_inc(mddev->gendisk, ios[rw]);
407 	disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
408 
409 	chunk_size = mddev->chunk_size >> 10;
410 	chunk_sects = mddev->chunk_size >> 9;
411 	chunksize_bits = ffz(~chunk_size);
412 	block = bio->bi_sector >> 1;
413 
414 
415 	if (unlikely(chunk_sects < (bio->bi_sector & (chunk_sects - 1)) + (bio->bi_size >> 9))) {
416 		struct bio_pair *bp;
417 		/* Sanity check -- queue functions should prevent this happening */
418 		if (bio->bi_vcnt != 1 ||
419 		    bio->bi_idx != 0)
420 			goto bad_map;
421 		/* This is a one page bio that upper layers
422 		 * refuse to split for us, so we need to split it.
423 		 */
424 		bp = bio_split(bio, bio_split_pool, chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
425 		if (raid0_make_request(q, &bp->bio1))
426 			generic_make_request(&bp->bio1);
427 		if (raid0_make_request(q, &bp->bio2))
428 			generic_make_request(&bp->bio2);
429 
430 		bio_pair_release(bp);
431 		return 0;
432 	}
433 
434 
435 	{
436 		sector_t x = block >> conf->preshift;
437 		sector_div(x, (u32)conf->hash_spacing);
438 		zone = conf->hash_table[x];
439 	}
440 
441 	while (block >= (zone->zone_offset + zone->size))
442 		zone++;
443 
444 	sect_in_chunk = bio->bi_sector & ((chunk_size<<1) -1);
445 
446 
447 	{
448 		sector_t x =  (block - zone->zone_offset) >> chunksize_bits;
449 
450 		sector_div(x, zone->nb_dev);
451 		chunk = x;
452 
453 		x = block >> chunksize_bits;
454 		tmp_dev = zone->dev[sector_div(x, zone->nb_dev)];
455 	}
456 	rsect = (((chunk << chunksize_bits) + zone->dev_offset)<<1)
457 		+ sect_in_chunk;
458 
459 	bio->bi_bdev = tmp_dev->bdev;
460 	bio->bi_sector = rsect + tmp_dev->data_offset;
461 
462 	/*
463 	 * Let the main block layer submit the IO and resolve recursion:
464 	 */
465 	return 1;
466 
467 bad_map:
468 	printk("raid0_make_request bug: can't convert block across chunks"
469 		" or bigger than %dk %llu %d\n", chunk_size,
470 		(unsigned long long)bio->bi_sector, bio->bi_size >> 10);
471 
472 	bio_io_error(bio);
473 	return 0;
474 }
475 
476 static void raid0_status (struct seq_file *seq, mddev_t *mddev)
477 {
478 #undef MD_DEBUG
479 #ifdef MD_DEBUG
480 	int j, k, h;
481 	char b[BDEVNAME_SIZE];
482 	raid0_conf_t *conf = mddev_to_conf(mddev);
483 
484 	h = 0;
485 	for (j = 0; j < conf->nr_strip_zones; j++) {
486 		seq_printf(seq, "      z%d", j);
487 		if (conf->hash_table[h] == conf->strip_zone+j)
488 			seq_printf(seq, "(h%d)", h++);
489 		seq_printf(seq, "=[");
490 		for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
491 			seq_printf(seq, "%s/", bdevname(
492 				conf->strip_zone[j].dev[k]->bdev,b));
493 
494 		seq_printf(seq, "] zo=%d do=%d s=%d\n",
495 				conf->strip_zone[j].zone_offset,
496 				conf->strip_zone[j].dev_offset,
497 				conf->strip_zone[j].size);
498 	}
499 #endif
500 	seq_printf(seq, " %dk chunks", mddev->chunk_size/1024);
501 	return;
502 }
503 
504 static struct mdk_personality raid0_personality=
505 {
506 	.name		= "raid0",
507 	.level		= 0,
508 	.owner		= THIS_MODULE,
509 	.make_request	= raid0_make_request,
510 	.run		= raid0_run,
511 	.stop		= raid0_stop,
512 	.status		= raid0_status,
513 };
514 
515 static int __init raid0_init (void)
516 {
517 	return register_md_personality (&raid0_personality);
518 }
519 
520 static void raid0_exit (void)
521 {
522 	unregister_md_personality (&raid0_personality);
523 }
524 
525 module_init(raid0_init);
526 module_exit(raid0_exit);
527 MODULE_LICENSE("GPL");
528 MODULE_ALIAS("md-personality-2"); /* RAID0 */
529 MODULE_ALIAS("md-raid0");
530 MODULE_ALIAS("md-level-0");
531