dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / md / linear.c
blob7ffb20ec1a46a57f76c1af44273fc71b870d11fe
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>
7 Linear mode management functions.
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.
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.
19 #include <linux/blkdev.h>
20 #include <linux/raid/md_u.h>
21 #include <linux/seq_file.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include "md.h"
25 #include "linear.h"
28 * find which device holds a particular offset
30 static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
32 int lo, mid, hi;
33 struct linear_conf *conf;
35 lo = 0;
36 hi = mddev->raid_disks - 1;
37 conf = mddev->private;
40 * Binary Search
43 while (hi > lo) {
45 mid = (hi + lo) / 2;
46 if (sector < conf->disks[mid].end_sector)
47 hi = mid;
48 else
49 lo = mid + 1;
52 return conf->disks + lo;
56 * In linear_congested() conf->raid_disks is used as a copy of
57 * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks
58 * and conf->disks[] are created in linear_conf(), they are always
59 * consitent with each other, but mddev->raid_disks does not.
61 static int linear_congested(struct mddev *mddev, int bits)
63 struct linear_conf *conf;
64 int i, ret = 0;
66 rcu_read_lock();
67 conf = rcu_dereference(mddev->private);
69 for (i = 0; i < conf->raid_disks && !ret ; i++) {
70 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
71 ret |= bdi_congested(&q->backing_dev_info, bits);
74 rcu_read_unlock();
75 return ret;
78 static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80 struct linear_conf *conf;
81 sector_t array_sectors;
83 conf = mddev->private;
84 WARN_ONCE(sectors || raid_disks,
85 "%s does not support generic reshape\n", __func__);
86 array_sectors = conf->array_sectors;
88 return array_sectors;
91 static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
93 struct linear_conf *conf;
94 struct md_rdev *rdev;
95 int i, cnt;
96 bool discard_supported = false;
98 conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
99 GFP_KERNEL);
100 if (!conf)
101 return NULL;
103 cnt = 0;
104 conf->array_sectors = 0;
106 rdev_for_each(rdev, mddev) {
107 int j = rdev->raid_disk;
108 struct dev_info *disk = conf->disks + j;
109 sector_t sectors;
111 if (j < 0 || j >= raid_disks || disk->rdev) {
112 printk(KERN_ERR "md/linear:%s: disk numbering problem. Aborting!\n",
113 mdname(mddev));
114 goto out;
117 disk->rdev = rdev;
118 if (mddev->chunk_sectors) {
119 sectors = rdev->sectors;
120 sector_div(sectors, mddev->chunk_sectors);
121 rdev->sectors = sectors * mddev->chunk_sectors;
124 disk_stack_limits(mddev->gendisk, rdev->bdev,
125 rdev->data_offset << 9);
127 conf->array_sectors += rdev->sectors;
128 cnt++;
130 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
131 discard_supported = true;
133 if (cnt != raid_disks) {
134 printk(KERN_ERR "md/linear:%s: not enough drives present. Aborting!\n",
135 mdname(mddev));
136 goto out;
139 if (!discard_supported)
140 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
141 else
142 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
145 * Here we calculate the device offsets.
147 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
149 for (i = 1; i < raid_disks; i++)
150 conf->disks[i].end_sector =
151 conf->disks[i-1].end_sector +
152 conf->disks[i].rdev->sectors;
155 * conf->raid_disks is copy of mddev->raid_disks. The reason to
156 * keep a copy of mddev->raid_disks in struct linear_conf is,
157 * mddev->raid_disks may not be consistent with pointers number of
158 * conf->disks[] when it is updated in linear_add() and used to
159 * iterate old conf->disks[] earray in linear_congested().
160 * Here conf->raid_disks is always consitent with number of
161 * pointers in conf->disks[] array, and mddev->private is updated
162 * with rcu_assign_pointer() in linear_addr(), such race can be
163 * avoided.
165 conf->raid_disks = raid_disks;
167 return conf;
169 out:
170 kfree(conf);
171 return NULL;
174 static int linear_run (struct mddev *mddev)
176 struct linear_conf *conf;
177 int ret;
179 if (md_check_no_bitmap(mddev))
180 return -EINVAL;
181 conf = linear_conf(mddev, mddev->raid_disks);
183 if (!conf)
184 return 1;
185 mddev->private = conf;
186 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
188 ret = md_integrity_register(mddev);
189 if (ret) {
190 kfree(conf);
191 mddev->private = NULL;
193 return ret;
196 static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
198 /* Adding a drive to a linear array allows the array to grow.
199 * It is permitted if the new drive has a matching superblock
200 * already on it, with raid_disk equal to raid_disks.
201 * It is achieved by creating a new linear_private_data structure
202 * and swapping it in in-place of the current one.
203 * The current one is never freed until the array is stopped.
204 * This avoids races.
206 struct linear_conf *newconf, *oldconf;
208 if (rdev->saved_raid_disk != mddev->raid_disks)
209 return -EINVAL;
211 rdev->raid_disk = rdev->saved_raid_disk;
212 rdev->saved_raid_disk = -1;
214 newconf = linear_conf(mddev,mddev->raid_disks+1);
216 if (!newconf)
217 return -ENOMEM;
219 /* newconf->raid_disks already keeps a copy of * the increased
220 * value of mddev->raid_disks, WARN_ONCE() is just used to make
221 * sure of this. It is possible that oldconf is still referenced
222 * in linear_congested(), therefore kfree_rcu() is used to free
223 * oldconf until no one uses it anymore.
225 mddev_suspend(mddev);
226 oldconf = rcu_dereference_protected(mddev->private,
227 lockdep_is_held(&mddev->reconfig_mutex));
228 mddev->raid_disks++;
229 WARN_ONCE(mddev->raid_disks != newconf->raid_disks,
230 "copied raid_disks doesn't match mddev->raid_disks");
231 rcu_assign_pointer(mddev->private, newconf);
232 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
233 set_capacity(mddev->gendisk, mddev->array_sectors);
234 mddev_resume(mddev);
235 revalidate_disk(mddev->gendisk);
236 kfree_rcu(oldconf, rcu);
237 return 0;
240 static void linear_free(struct mddev *mddev, void *priv)
242 struct linear_conf *conf = priv;
244 kfree(conf);
247 static void linear_make_request(struct mddev *mddev, struct bio *bio)
249 char b[BDEVNAME_SIZE];
250 struct dev_info *tmp_dev;
251 struct bio *split;
252 sector_t start_sector, end_sector, data_offset;
254 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
255 md_flush_request(mddev, bio);
256 return;
259 do {
260 tmp_dev = which_dev(mddev, bio->bi_iter.bi_sector);
261 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
262 end_sector = tmp_dev->end_sector;
263 data_offset = tmp_dev->rdev->data_offset;
264 bio->bi_bdev = tmp_dev->rdev->bdev;
266 if (unlikely(bio->bi_iter.bi_sector >= end_sector ||
267 bio->bi_iter.bi_sector < start_sector))
268 goto out_of_bounds;
270 if (unlikely(bio_end_sector(bio) > end_sector)) {
271 /* This bio crosses a device boundary, so we have to
272 * split it.
274 split = bio_split(bio, end_sector -
275 bio->bi_iter.bi_sector,
276 GFP_NOIO, fs_bio_set);
277 bio_chain(split, bio);
278 } else {
279 split = bio;
282 split->bi_iter.bi_sector = split->bi_iter.bi_sector -
283 start_sector + data_offset;
285 if (unlikely((split->bi_rw & REQ_DISCARD) &&
286 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
287 /* Just ignore it */
288 bio_endio(split);
289 } else
290 generic_make_request(split);
291 } while (split != bio);
292 return;
294 out_of_bounds:
295 printk(KERN_ERR
296 "md/linear:%s: make_request: Sector %llu out of bounds on "
297 "dev %s: %llu sectors, offset %llu\n",
298 mdname(mddev),
299 (unsigned long long)bio->bi_iter.bi_sector,
300 bdevname(tmp_dev->rdev->bdev, b),
301 (unsigned long long)tmp_dev->rdev->sectors,
302 (unsigned long long)start_sector);
303 bio_io_error(bio);
306 static void linear_status (struct seq_file *seq, struct mddev *mddev)
309 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
312 static void linear_quiesce(struct mddev *mddev, int state)
316 static struct md_personality linear_personality =
318 .name = "linear",
319 .level = LEVEL_LINEAR,
320 .owner = THIS_MODULE,
321 .make_request = linear_make_request,
322 .run = linear_run,
323 .free = linear_free,
324 .status = linear_status,
325 .hot_add_disk = linear_add,
326 .size = linear_size,
327 .quiesce = linear_quiesce,
328 .congested = linear_congested,
331 static int __init linear_init (void)
333 return register_md_personality (&linear_personality);
336 static void linear_exit (void)
338 unregister_md_personality (&linear_personality);
341 module_init(linear_init);
342 module_exit(linear_exit);
343 MODULE_LICENSE("GPL");
344 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
345 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
346 MODULE_ALIAS("md-linear");
347 MODULE_ALIAS("md-level--1");