perf/x86/intel/pt: Allow the disabling of branch tracing
[linux/fpc-iii.git] / drivers / md / linear.c
blob3e38e0207a3eb44339ad6431dc3557ae27d05612
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 <trace/events/block.h>
25 #include "md.h"
26 #include "linear.h"
29 * find which device holds a particular offset
31 static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
33 int lo, mid, hi;
34 struct linear_conf *conf;
36 lo = 0;
37 hi = mddev->raid_disks - 1;
38 conf = mddev->private;
41 * Binary Search
44 while (hi > lo) {
46 mid = (hi + lo) / 2;
47 if (sector < conf->disks[mid].end_sector)
48 hi = mid;
49 else
50 lo = mid + 1;
53 return conf->disks + lo;
57 * In linear_congested() conf->raid_disks is used as a copy of
58 * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks
59 * and conf->disks[] are created in linear_conf(), they are always
60 * consitent with each other, but mddev->raid_disks does not.
62 static int linear_congested(struct mddev *mddev, int bits)
64 struct linear_conf *conf;
65 int i, ret = 0;
67 rcu_read_lock();
68 conf = rcu_dereference(mddev->private);
70 for (i = 0; i < conf->raid_disks && !ret ; i++) {
71 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
72 ret |= bdi_congested(q->backing_dev_info, bits);
75 rcu_read_unlock();
76 return ret;
79 static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
81 struct linear_conf *conf;
82 sector_t array_sectors;
84 conf = mddev->private;
85 WARN_ONCE(sectors || raid_disks,
86 "%s does not support generic reshape\n", __func__);
87 array_sectors = conf->array_sectors;
89 return array_sectors;
92 static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
94 struct linear_conf *conf;
95 struct md_rdev *rdev;
96 int i, cnt;
97 bool discard_supported = false;
99 conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
100 GFP_KERNEL);
101 if (!conf)
102 return NULL;
104 cnt = 0;
105 conf->array_sectors = 0;
107 rdev_for_each(rdev, mddev) {
108 int j = rdev->raid_disk;
109 struct dev_info *disk = conf->disks + j;
110 sector_t sectors;
112 if (j < 0 || j >= raid_disks || disk->rdev) {
113 pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
114 mdname(mddev));
115 goto out;
118 disk->rdev = rdev;
119 if (mddev->chunk_sectors) {
120 sectors = rdev->sectors;
121 sector_div(sectors, mddev->chunk_sectors);
122 rdev->sectors = sectors * mddev->chunk_sectors;
125 disk_stack_limits(mddev->gendisk, rdev->bdev,
126 rdev->data_offset << 9);
128 conf->array_sectors += rdev->sectors;
129 cnt++;
131 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
132 discard_supported = true;
134 if (cnt != raid_disks) {
135 pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
136 mdname(mddev));
137 goto out;
140 if (!discard_supported)
141 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
142 else
143 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
146 * Here we calculate the device offsets.
148 conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
150 for (i = 1; i < raid_disks; i++)
151 conf->disks[i].end_sector =
152 conf->disks[i-1].end_sector +
153 conf->disks[i].rdev->sectors;
156 * conf->raid_disks is copy of mddev->raid_disks. The reason to
157 * keep a copy of mddev->raid_disks in struct linear_conf is,
158 * mddev->raid_disks may not be consistent with pointers number of
159 * conf->disks[] when it is updated in linear_add() and used to
160 * iterate old conf->disks[] earray in linear_congested().
161 * Here conf->raid_disks is always consitent with number of
162 * pointers in conf->disks[] array, and mddev->private is updated
163 * with rcu_assign_pointer() in linear_addr(), such race can be
164 * avoided.
166 conf->raid_disks = raid_disks;
168 return conf;
170 out:
171 kfree(conf);
172 return NULL;
175 static int linear_run (struct mddev *mddev)
177 struct linear_conf *conf;
178 int ret;
180 if (md_check_no_bitmap(mddev))
181 return -EINVAL;
182 conf = linear_conf(mddev, mddev->raid_disks);
184 if (!conf)
185 return 1;
186 mddev->private = conf;
187 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
189 ret = md_integrity_register(mddev);
190 if (ret) {
191 kfree(conf);
192 mddev->private = NULL;
194 return ret;
197 static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
199 /* Adding a drive to a linear array allows the array to grow.
200 * It is permitted if the new drive has a matching superblock
201 * already on it, with raid_disk equal to raid_disks.
202 * It is achieved by creating a new linear_private_data structure
203 * and swapping it in in-place of the current one.
204 * The current one is never freed until the array is stopped.
205 * This avoids races.
207 struct linear_conf *newconf, *oldconf;
209 if (rdev->saved_raid_disk != mddev->raid_disks)
210 return -EINVAL;
212 rdev->raid_disk = rdev->saved_raid_disk;
213 rdev->saved_raid_disk = -1;
215 newconf = linear_conf(mddev,mddev->raid_disks+1);
217 if (!newconf)
218 return -ENOMEM;
220 /* newconf->raid_disks already keeps a copy of * the increased
221 * value of mddev->raid_disks, WARN_ONCE() is just used to make
222 * sure of this. It is possible that oldconf is still referenced
223 * in linear_congested(), therefore kfree_rcu() is used to free
224 * oldconf until no one uses it anymore.
226 mddev_suspend(mddev);
227 oldconf = rcu_dereference_protected(mddev->private,
228 lockdep_is_held(&mddev->reconfig_mutex));
229 mddev->raid_disks++;
230 WARN_ONCE(mddev->raid_disks != newconf->raid_disks,
231 "copied raid_disks doesn't match mddev->raid_disks");
232 rcu_assign_pointer(mddev->private, newconf);
233 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
234 set_capacity(mddev->gendisk, mddev->array_sectors);
235 mddev_resume(mddev);
236 revalidate_disk(mddev->gendisk);
237 kfree_rcu(oldconf, rcu);
238 return 0;
241 static void linear_free(struct mddev *mddev, void *priv)
243 struct linear_conf *conf = priv;
245 kfree(conf);
248 static void linear_make_request(struct mddev *mddev, struct bio *bio)
250 char b[BDEVNAME_SIZE];
251 struct dev_info *tmp_dev;
252 struct bio *split;
253 sector_t start_sector, end_sector, data_offset;
255 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
256 md_flush_request(mddev, bio);
257 return;
260 do {
261 sector_t bio_sector = bio->bi_iter.bi_sector;
262 tmp_dev = which_dev(mddev, bio_sector);
263 start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
264 end_sector = tmp_dev->end_sector;
265 data_offset = tmp_dev->rdev->data_offset;
266 bio->bi_bdev = tmp_dev->rdev->bdev;
268 if (unlikely(bio_sector >= end_sector ||
269 bio_sector < start_sector))
270 goto out_of_bounds;
272 if (unlikely(bio_end_sector(bio) > end_sector)) {
273 /* This bio crosses a device boundary, so we have to
274 * split it.
276 split = bio_split(bio, end_sector - bio_sector,
277 GFP_NOIO, fs_bio_set);
278 bio_chain(split, bio);
279 } else {
280 split = bio;
283 split->bi_iter.bi_sector = split->bi_iter.bi_sector -
284 start_sector + data_offset;
286 if (unlikely((bio_op(split) == REQ_OP_DISCARD) &&
287 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
288 /* Just ignore it */
289 bio_endio(split);
290 } else {
291 if (mddev->gendisk)
292 trace_block_bio_remap(bdev_get_queue(split->bi_bdev),
293 split, disk_devt(mddev->gendisk),
294 bio_sector);
295 mddev_check_writesame(mddev, split);
296 generic_make_request(split);
298 } while (split != bio);
299 return;
301 out_of_bounds:
302 pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
303 mdname(mddev),
304 (unsigned long long)bio->bi_iter.bi_sector,
305 bdevname(tmp_dev->rdev->bdev, b),
306 (unsigned long long)tmp_dev->rdev->sectors,
307 (unsigned long long)start_sector);
308 bio_io_error(bio);
311 static void linear_status (struct seq_file *seq, struct mddev *mddev)
313 seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
316 static void linear_quiesce(struct mddev *mddev, int state)
320 static struct md_personality linear_personality =
322 .name = "linear",
323 .level = LEVEL_LINEAR,
324 .owner = THIS_MODULE,
325 .make_request = linear_make_request,
326 .run = linear_run,
327 .free = linear_free,
328 .status = linear_status,
329 .hot_add_disk = linear_add,
330 .size = linear_size,
331 .quiesce = linear_quiesce,
332 .congested = linear_congested,
335 static int __init linear_init (void)
337 return register_md_personality (&linear_personality);
340 static void linear_exit (void)
342 unregister_md_personality (&linear_personality);
345 module_init(linear_init);
346 module_exit(linear_exit);
347 MODULE_LICENSE("GPL");
348 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
349 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
350 MODULE_ALIAS("md-linear");
351 MODULE_ALIAS("md-level--1");