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