1/*
2   linear.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
7   Linear mode management functions.
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2, or (at your option)
12   any later version.
13
14   You should have received a copy of the GNU General Public License
15   (for example /usr/src/linux/COPYING); if not, write to the Free
16   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19#include <linux/module.h>
20
21#include <linux/raid/md.h>
22#include <linux/slab.h>
23#include <linux/raid/linear.h>
24
25#define MAJOR_NR MD_MAJOR
26#define MD_DRIVER
27#define MD_PERSONALITY
28
29/*
30 * find which device holds a particular offset
31 */
32static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
33{
34	dev_info_t *hash;
35	linear_conf_t *conf = mddev_to_conf(mddev);
36	sector_t block = sector >> 1;
37
38	/*
39	 * sector_div(a,b) returns the remainer and sets a to a/b
40	 */
41	block >>= conf->preshift;
42	(void)sector_div(block, conf->hash_spacing);
43	hash = conf->hash_table[block];
44
45	while ((sector>>1) >= (hash->size + hash->offset))
46		hash++;
47	return hash;
48}
49
50/**
51 *	linear_mergeable_bvec -- tell bio layer if two requests can be merged
52 *	@q: request queue
53 *	@bio: the buffer head that's been built up so far
54 *	@biovec: the request that could be merged to it.
55 *
56 *	Return amount of bytes we can take at this offset
57 */
58static int linear_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
59{
60	mddev_t *mddev = q->queuedata;
61	dev_info_t *dev0;
62	unsigned long maxsectors, bio_sectors = bio->bi_size >> 9;
63	sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
64
65	dev0 = which_dev(mddev, sector);
66	maxsectors = (dev0->size << 1) - (sector - (dev0->offset<<1));
67
68	if (maxsectors < bio_sectors)
69		maxsectors = 0;
70	else
71		maxsectors -= bio_sectors;
72
73	if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
74		return biovec->bv_len;
75	/* The bytes available at this offset could be really big,
76	 * so we cap at 2^31 to avoid overflow */
77	if (maxsectors > (1 << (31-9)))
78		return 1<<31;
79	return maxsectors << 9;
80}
81
82static void linear_unplug(request_queue_t *q)
83{
84	mddev_t *mddev = q->queuedata;
85	linear_conf_t *conf = mddev_to_conf(mddev);
86	int i;
87
88	for (i=0; i < mddev->raid_disks; i++) {
89		request_queue_t *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
90		if (r_queue->unplug_fn)
91			r_queue->unplug_fn(r_queue);
92	}
93}
94
95static int linear_issue_flush(request_queue_t *q, struct gendisk *disk,
96			      sector_t *error_sector)
97{
98	mddev_t *mddev = q->queuedata;
99	linear_conf_t *conf = mddev_to_conf(mddev);
100	int i, ret = 0;
101
102	for (i=0; i < mddev->raid_disks && ret == 0; i++) {
103		struct block_device *bdev = conf->disks[i].rdev->bdev;
104		request_queue_t *r_queue = bdev_get_queue(bdev);
105
106		if (!r_queue->issue_flush_fn)
107			ret = -EOPNOTSUPP;
108		else
109			ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, error_sector);
110	}
111	return ret;
112}
113
114static int linear_congested(void *data, int bits)
115{
116	mddev_t *mddev = data;
117	linear_conf_t *conf = mddev_to_conf(mddev);
118	int i, ret = 0;
119
120	for (i = 0; i < mddev->raid_disks && !ret ; i++) {
121		request_queue_t *q = bdev_get_queue(conf->disks[i].rdev->bdev);
122		ret |= bdi_congested(&q->backing_dev_info, bits);
123	}
124	return ret;
125}
126
127static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
128{
129	linear_conf_t *conf;
130	dev_info_t **table;
131	mdk_rdev_t *rdev;
132	int i, nb_zone, cnt;
133	sector_t min_spacing;
134	sector_t curr_offset;
135	struct list_head *tmp;
136
137	conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
138			GFP_KERNEL);
139	if (!conf)
140		return NULL;
141
142	cnt = 0;
143	conf->array_size = 0;
144
145	ITERATE_RDEV(mddev,rdev,tmp) {
146		int j = rdev->raid_disk;
147		dev_info_t *disk = conf->disks + j;
148
149		if (j < 0 || j > raid_disks || disk->rdev) {
150			printk("linear: disk numbering problem. Aborting!\n");
151			goto out;
152		}
153
154		disk->rdev = rdev;
155
156		blk_queue_stack_limits(mddev->queue,
157				       rdev->bdev->bd_disk->queue);
158		/* as we don't honour merge_bvec_fn, we must never risk
159		 * violating it, so limit ->max_sector to one PAGE, as
160		 * a one page request is never in violation.
161		 */
162		if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
163		    mddev->queue->max_sectors > (PAGE_SIZE>>9))
164			blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
165
166		disk->size = rdev->size;
167		conf->array_size += rdev->size;
168
169		cnt++;
170	}
171	if (cnt != raid_disks) {
172		printk("linear: not enough drives present. Aborting!\n");
173		goto out;
174	}
175
176	min_spacing = conf->array_size;
177	sector_div(min_spacing, PAGE_SIZE/sizeof(struct dev_info *));
178
179	/* min_spacing is the minimum spacing that will fit the hash
180	 * table in one PAGE.  This may be much smaller than needed.
181	 * We find the smallest non-terminal set of consecutive devices
182	 * that is larger than min_spacing as use the size of that as
183	 * the actual spacing
184	 */
185	conf->hash_spacing = conf->array_size;
186	for (i=0; i < cnt-1 ; i++) {
187		sector_t sz = 0;
188		int j;
189		for (j = i; j < cnt - 1 && sz < min_spacing; j++)
190			sz += conf->disks[j].size;
191		if (sz >= min_spacing && sz < conf->hash_spacing)
192			conf->hash_spacing = sz;
193	}
194
195	/* hash_spacing may be too large for sector_div to work with,
196	 * so we might need to pre-shift
197	 */
198	conf->preshift = 0;
199	if (sizeof(sector_t) > sizeof(u32)) {
200		sector_t space = conf->hash_spacing;
201		while (space > (sector_t)(~(u32)0)) {
202			space >>= 1;
203			conf->preshift++;
204		}
205	}
206	{
207		sector_t sz;
208		unsigned round;
209		unsigned long base;
210
211		sz = conf->array_size >> conf->preshift;
212		sz += 1; /* force round-up */
213		base = conf->hash_spacing >> conf->preshift;
214		round = sector_div(sz, base);
215		nb_zone = sz + (round ? 1 : 0);
216	}
217	BUG_ON(nb_zone > PAGE_SIZE / sizeof(struct dev_info *));
218
219	conf->hash_table = kmalloc (sizeof (struct dev_info *) * nb_zone,
220					GFP_KERNEL);
221	if (!conf->hash_table)
222		goto out;
223
224	/*
225	 * Here we generate the linear hash table
226	 * First calculate the device offsets.
227	 */
228	conf->disks[0].offset = 0;
229	for (i = 1; i < raid_disks; i++)
230		conf->disks[i].offset =
231			conf->disks[i-1].offset +
232			conf->disks[i-1].size;
233
234	table = conf->hash_table;
235	curr_offset = 0;
236	i = 0;
237	for (curr_offset = 0;
238	     curr_offset < conf->array_size;
239	     curr_offset += conf->hash_spacing) {
240
241		while (i < raid_disks-1 &&
242		       curr_offset >= conf->disks[i+1].offset)
243			i++;
244
245		*table ++ = conf->disks + i;
246	}
247
248	if (conf->preshift) {
249		conf->hash_spacing >>= conf->preshift;
250		/* round hash_spacing up so that when we divide by it,
251		 * we err on the side of "too-low", which is safest.
252		 */
253		conf->hash_spacing++;
254	}
255
256	BUG_ON(table - conf->hash_table > nb_zone);
257
258	return conf;
259
260out:
261	kfree(conf);
262	return NULL;
263}
264
265static int linear_run (mddev_t *mddev)
266{
267	linear_conf_t *conf;
268
269	conf = linear_conf(mddev, mddev->raid_disks);
270
271	if (!conf)
272		return 1;
273	mddev->private = conf;
274	mddev->array_size = conf->array_size;
275
276	blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
277	mddev->queue->unplug_fn = linear_unplug;
278	mddev->queue->issue_flush_fn = linear_issue_flush;
279	mddev->queue->backing_dev_info.congested_fn = linear_congested;
280	mddev->queue->backing_dev_info.congested_data = mddev;
281	return 0;
282}
283
284static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
285{
286	/* Adding a drive to a linear array allows the array to grow.
287	 * It is permitted if the new drive has a matching superblock
288	 * already on it, with raid_disk equal to raid_disks.
289	 * It is achieved by creating a new linear_private_data structure
290	 * and swapping it in in-place of the current one.
291	 * The current one is never freed until the array is stopped.
292	 * This avoids races.
293	 */
294	linear_conf_t *newconf;
295
296	if (rdev->saved_raid_disk != mddev->raid_disks)
297		return -EINVAL;
298
299	rdev->raid_disk = rdev->saved_raid_disk;
300
301	newconf = linear_conf(mddev,mddev->raid_disks+1);
302
303	if (!newconf)
304		return -ENOMEM;
305
306	newconf->prev = mddev_to_conf(mddev);
307	mddev->private = newconf;
308	mddev->raid_disks++;
309	mddev->array_size = newconf->array_size;
310	set_capacity(mddev->gendisk, mddev->array_size << 1);
311	return 0;
312}
313
314static int linear_stop (mddev_t *mddev)
315{
316	linear_conf_t *conf = mddev_to_conf(mddev);
317
318	blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
319	do {
320		linear_conf_t *t = conf->prev;
321		kfree(conf->hash_table);
322		kfree(conf);
323		conf = t;
324	} while (conf);
325
326	return 0;
327}
328
329static int linear_make_request (request_queue_t *q, struct bio *bio)
330{
331	const int rw = bio_data_dir(bio);
332	mddev_t *mddev = q->queuedata;
333	dev_info_t *tmp_dev;
334	sector_t block;
335
336	if (unlikely(bio_barrier(bio))) {
337		bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
338		return 0;
339	}
340
341	disk_stat_inc(mddev->gendisk, ios[rw]);
342	disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
343
344	tmp_dev = which_dev(mddev, bio->bi_sector);
345	block = bio->bi_sector >> 1;
346
347	if (unlikely(block >= (tmp_dev->size + tmp_dev->offset)
348		     || block < tmp_dev->offset)) {
349		char b[BDEVNAME_SIZE];
350
351		printk("linear_make_request: Block %llu out of bounds on "
352			"dev %s size %llu offset %llu\n",
353			(unsigned long long)block,
354			bdevname(tmp_dev->rdev->bdev, b),
355			(unsigned long long)tmp_dev->size,
356		        (unsigned long long)tmp_dev->offset);
357		bio_io_error(bio, bio->bi_size);
358		return 0;
359	}
360	if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
361		     (tmp_dev->offset + tmp_dev->size)<<1)) {
362		/* This bio crosses a device boundary, so we have to
363		 * split it.
364		 */
365		struct bio_pair *bp;
366		bp = bio_split(bio, bio_split_pool,
367			       ((tmp_dev->offset + tmp_dev->size)<<1) - bio->bi_sector);
368		if (linear_make_request(q, &bp->bio1))
369			generic_make_request(&bp->bio1);
370		if (linear_make_request(q, &bp->bio2))
371			generic_make_request(&bp->bio2);
372		bio_pair_release(bp);
373		return 0;
374	}
375
376	bio->bi_bdev = tmp_dev->rdev->bdev;
377	bio->bi_sector = bio->bi_sector - (tmp_dev->offset << 1) + tmp_dev->rdev->data_offset;
378
379	return 1;
380}
381
382static void linear_status (struct seq_file *seq, mddev_t *mddev)
383{
384
385#undef MD_DEBUG
386#ifdef MD_DEBUG
387	int j;
388	linear_conf_t *conf = mddev_to_conf(mddev);
389	sector_t s = 0;
390
391	seq_printf(seq, "      ");
392	for (j = 0; j < mddev->raid_disks; j++)
393	{
394		char b[BDEVNAME_SIZE];
395		s += conf->smallest_size;
396		seq_printf(seq, "[%s",
397			   bdevname(conf->hash_table[j][0].rdev->bdev,b));
398
399		while (s > conf->hash_table[j][0].offset +
400		           conf->hash_table[j][0].size)
401			seq_printf(seq, "/%s] ",
402				   bdevname(conf->hash_table[j][1].rdev->bdev,b));
403		else
404			seq_printf(seq, "] ");
405	}
406	seq_printf(seq, "\n");
407#endif
408	seq_printf(seq, " %dk rounding", mddev->chunk_size/1024);
409}
410
411
412static struct mdk_personality linear_personality =
413{
414	.name		= "linear",
415	.level		= LEVEL_LINEAR,
416	.owner		= THIS_MODULE,
417	.make_request	= linear_make_request,
418	.run		= linear_run,
419	.stop		= linear_stop,
420	.status		= linear_status,
421	.hot_add_disk	= linear_add,
422};
423
424static int __init linear_init (void)
425{
426	return register_md_personality (&linear_personality);
427}
428
429static void linear_exit (void)
430{
431	unregister_md_personality (&linear_personality);
432}
433
434
435module_init(linear_init);
436module_exit(linear_exit);
437MODULE_LICENSE("GPL");
438MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
439MODULE_ALIAS("md-linear");
440MODULE_ALIAS("md-level--1");
441