md: Make mddev->chunk_size sector-based.
[safe/jmp/linux-2.6] / drivers / md / linear.c
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/blkdev.h>
20 #include <linux/raid/md_u.h>
21 #include <linux/seq_file.h>
22 #include "md.h"
23 #include "linear.h"
24
25 /*
26  * find which device holds a particular offset 
27  */
28 static inline dev_info_t *which_dev(mddev_t *mddev, sector_t sector)
29 {
30         int lo, mid, hi;
31         linear_conf_t *conf = mddev->private;
32
33         lo = 0;
34         hi = mddev->raid_disks - 1;
35
36         /*
37          * Binary Search
38          */
39
40         while (hi > lo) {
41
42                 mid = (hi + lo) / 2;
43                 if (sector < conf->disks[mid].end_sector)
44                         hi = mid;
45                 else
46                         lo = mid + 1;
47         }
48
49         return conf->disks + lo;
50 }
51
52 /**
53  *      linear_mergeable_bvec -- tell bio layer if two requests can be merged
54  *      @q: request queue
55  *      @bvm: properties of new bio
56  *      @biovec: the request that could be merged to it.
57  *
58  *      Return amount of bytes we can take at this offset
59  */
60 static int linear_mergeable_bvec(struct request_queue *q,
61                                  struct bvec_merge_data *bvm,
62                                  struct bio_vec *biovec)
63 {
64         mddev_t *mddev = q->queuedata;
65         dev_info_t *dev0;
66         unsigned long maxsectors, bio_sectors = bvm->bi_size >> 9;
67         sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
68
69         dev0 = which_dev(mddev, sector);
70         maxsectors = dev0->end_sector - sector;
71
72         if (maxsectors < bio_sectors)
73                 maxsectors = 0;
74         else
75                 maxsectors -= bio_sectors;
76
77         if (maxsectors <= (PAGE_SIZE >> 9 ) && bio_sectors == 0)
78                 return biovec->bv_len;
79         /* The bytes available at this offset could be really big,
80          * so we cap at 2^31 to avoid overflow */
81         if (maxsectors > (1 << (31-9)))
82                 return 1<<31;
83         return maxsectors << 9;
84 }
85
86 static void linear_unplug(struct request_queue *q)
87 {
88         mddev_t *mddev = q->queuedata;
89         linear_conf_t *conf = mddev->private;
90         int i;
91
92         for (i=0; i < mddev->raid_disks; i++) {
93                 struct request_queue *r_queue = bdev_get_queue(conf->disks[i].rdev->bdev);
94                 blk_unplug(r_queue);
95         }
96 }
97
98 static int linear_congested(void *data, int bits)
99 {
100         mddev_t *mddev = data;
101         linear_conf_t *conf = mddev->private;
102         int i, ret = 0;
103
104         for (i = 0; i < mddev->raid_disks && !ret ; i++) {
105                 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
106                 ret |= bdi_congested(&q->backing_dev_info, bits);
107         }
108         return ret;
109 }
110
111 static sector_t linear_size(mddev_t *mddev, sector_t sectors, int raid_disks)
112 {
113         linear_conf_t *conf = mddev->private;
114
115         WARN_ONCE(sectors || raid_disks,
116                   "%s does not support generic reshape\n", __func__);
117
118         return conf->array_sectors;
119 }
120
121 static linear_conf_t *linear_conf(mddev_t *mddev, int raid_disks)
122 {
123         linear_conf_t *conf;
124         mdk_rdev_t *rdev;
125         int i, cnt;
126
127         conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(dev_info_t),
128                         GFP_KERNEL);
129         if (!conf)
130                 return NULL;
131
132         cnt = 0;
133         conf->array_sectors = 0;
134
135         list_for_each_entry(rdev, &mddev->disks, same_set) {
136                 int j = rdev->raid_disk;
137                 dev_info_t *disk = conf->disks + j;
138
139                 if (j < 0 || j >= raid_disks || disk->rdev) {
140                         printk("linear: disk numbering problem. Aborting!\n");
141                         goto out;
142                 }
143
144                 disk->rdev = rdev;
145
146                 blk_queue_stack_limits(mddev->queue,
147                                        rdev->bdev->bd_disk->queue);
148                 /* as we don't honour merge_bvec_fn, we must never risk
149                  * violating it, so limit ->max_sector to one PAGE, as
150                  * a one page request is never in violation.
151                  */
152                 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
153                     queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
154                         blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
155
156                 conf->array_sectors += rdev->sectors;
157                 cnt++;
158
159         }
160         if (cnt != raid_disks) {
161                 printk("linear: not enough drives present. Aborting!\n");
162                 goto out;
163         }
164
165         /*
166          * Here we calculate the device offsets.
167          */
168         conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
169
170         for (i = 1; i < raid_disks; i++)
171                 conf->disks[i].end_sector =
172                         conf->disks[i-1].end_sector +
173                         conf->disks[i].rdev->sectors;
174
175         return conf;
176
177 out:
178         kfree(conf);
179         return NULL;
180 }
181
182 static int linear_run (mddev_t *mddev)
183 {
184         linear_conf_t *conf;
185
186         mddev->queue->queue_lock = &mddev->queue->__queue_lock;
187         conf = linear_conf(mddev, mddev->raid_disks);
188
189         if (!conf)
190                 return 1;
191         mddev->private = conf;
192         md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
193
194         blk_queue_merge_bvec(mddev->queue, linear_mergeable_bvec);
195         mddev->queue->unplug_fn = linear_unplug;
196         mddev->queue->backing_dev_info.congested_fn = linear_congested;
197         mddev->queue->backing_dev_info.congested_data = mddev;
198         return 0;
199 }
200
201 static int linear_add(mddev_t *mddev, mdk_rdev_t *rdev)
202 {
203         /* Adding a drive to a linear array allows the array to grow.
204          * It is permitted if the new drive has a matching superblock
205          * already on it, with raid_disk equal to raid_disks.
206          * It is achieved by creating a new linear_private_data structure
207          * and swapping it in in-place of the current one.
208          * The current one is never freed until the array is stopped.
209          * This avoids races.
210          */
211         linear_conf_t *newconf;
212
213         if (rdev->saved_raid_disk != mddev->raid_disks)
214                 return -EINVAL;
215
216         rdev->raid_disk = rdev->saved_raid_disk;
217
218         newconf = linear_conf(mddev,mddev->raid_disks+1);
219
220         if (!newconf)
221                 return -ENOMEM;
222
223         newconf->prev = mddev->private;
224         mddev->private = newconf;
225         mddev->raid_disks++;
226         md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
227         set_capacity(mddev->gendisk, mddev->array_sectors);
228         return 0;
229 }
230
231 static int linear_stop (mddev_t *mddev)
232 {
233         linear_conf_t *conf = mddev->private;
234   
235         blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
236         do {
237                 linear_conf_t *t = conf->prev;
238                 kfree(conf);
239                 conf = t;
240         } while (conf);
241
242         return 0;
243 }
244
245 static int linear_make_request (struct request_queue *q, struct bio *bio)
246 {
247         const int rw = bio_data_dir(bio);
248         mddev_t *mddev = q->queuedata;
249         dev_info_t *tmp_dev;
250         sector_t start_sector;
251         int cpu;
252
253         if (unlikely(bio_barrier(bio))) {
254                 bio_endio(bio, -EOPNOTSUPP);
255                 return 0;
256         }
257
258         cpu = part_stat_lock();
259         part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
260         part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
261                       bio_sectors(bio));
262         part_stat_unlock();
263
264         tmp_dev = which_dev(mddev, bio->bi_sector);
265         start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
266
267         if (unlikely(bio->bi_sector >= (tmp_dev->end_sector)
268                      || (bio->bi_sector < start_sector))) {
269                 char b[BDEVNAME_SIZE];
270
271                 printk("linear_make_request: Sector %llu out of bounds on "
272                         "dev %s: %llu sectors, offset %llu\n",
273                         (unsigned long long)bio->bi_sector,
274                         bdevname(tmp_dev->rdev->bdev, b),
275                         (unsigned long long)tmp_dev->rdev->sectors,
276                         (unsigned long long)start_sector);
277                 bio_io_error(bio);
278                 return 0;
279         }
280         if (unlikely(bio->bi_sector + (bio->bi_size >> 9) >
281                      tmp_dev->end_sector)) {
282                 /* This bio crosses a device boundary, so we have to
283                  * split it.
284                  */
285                 struct bio_pair *bp;
286
287                 bp = bio_split(bio,
288                                tmp_dev->end_sector - bio->bi_sector);
289
290                 if (linear_make_request(q, &bp->bio1))
291                         generic_make_request(&bp->bio1);
292                 if (linear_make_request(q, &bp->bio2))
293                         generic_make_request(&bp->bio2);
294                 bio_pair_release(bp);
295                 return 0;
296         }
297                     
298         bio->bi_bdev = tmp_dev->rdev->bdev;
299         bio->bi_sector = bio->bi_sector - start_sector
300                 + tmp_dev->rdev->data_offset;
301
302         return 1;
303 }
304
305 static void linear_status (struct seq_file *seq, mddev_t *mddev)
306 {
307
308         seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
309 }
310
311
312 static struct mdk_personality linear_personality =
313 {
314         .name           = "linear",
315         .level          = LEVEL_LINEAR,
316         .owner          = THIS_MODULE,
317         .make_request   = linear_make_request,
318         .run            = linear_run,
319         .stop           = linear_stop,
320         .status         = linear_status,
321         .hot_add_disk   = linear_add,
322         .size           = linear_size,
323 };
324
325 static int __init linear_init (void)
326 {
327         return register_md_personality (&linear_personality);
328 }
329
330 static void linear_exit (void)
331 {
332         unregister_md_personality (&linear_personality);
333 }
334
335
336 module_init(linear_init);
337 module_exit(linear_exit);
338 MODULE_LICENSE("GPL");
339 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
340 MODULE_ALIAS("md-linear");
341 MODULE_ALIAS("md-level--1");