Linux Audio

Check our new training course

Loading...
v4.6
  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 <linux/module.h>
 23#include <linux/slab.h>
 
 24#include "md.h"
 25#include "linear.h"
 26
 27/*
 28 * find which device holds a particular offset
 29 */
 30static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
 31{
 32	int lo, mid, hi;
 33	struct linear_conf *conf;
 34
 35	lo = 0;
 36	hi = mddev->raid_disks - 1;
 37	conf = mddev->private;
 38
 39	/*
 40	 * Binary Search
 41	 */
 42
 43	while (hi > lo) {
 44
 45		mid = (hi + lo) / 2;
 46		if (sector < conf->disks[mid].end_sector)
 47			hi = mid;
 48		else
 49			lo = mid + 1;
 50	}
 51
 52	return conf->disks + lo;
 53}
 54
 
 
 
 
 
 
 55static int linear_congested(struct mddev *mddev, int bits)
 56{
 57	struct linear_conf *conf;
 58	int i, ret = 0;
 59
 60	conf = mddev->private;
 
 61
 62	for (i = 0; i < mddev->raid_disks && !ret ; i++) {
 63		struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
 64		ret |= bdi_congested(&q->backing_dev_info, bits);
 65	}
 66
 
 67	return ret;
 68}
 69
 70static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
 71{
 72	struct linear_conf *conf;
 73	sector_t array_sectors;
 74
 75	conf = mddev->private;
 76	WARN_ONCE(sectors || raid_disks,
 77		  "%s does not support generic reshape\n", __func__);
 78	array_sectors = conf->array_sectors;
 79
 80	return array_sectors;
 81}
 82
 83static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
 84{
 85	struct linear_conf *conf;
 86	struct md_rdev *rdev;
 87	int i, cnt;
 88	bool discard_supported = false;
 89
 90	conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
 91			GFP_KERNEL);
 92	if (!conf)
 93		return NULL;
 94
 95	cnt = 0;
 96	conf->array_sectors = 0;
 97
 98	rdev_for_each(rdev, mddev) {
 99		int j = rdev->raid_disk;
100		struct dev_info *disk = conf->disks + j;
101		sector_t sectors;
102
103		if (j < 0 || j >= raid_disks || disk->rdev) {
104			printk(KERN_ERR "md/linear:%s: disk numbering problem. Aborting!\n",
105			       mdname(mddev));
106			goto out;
107		}
108
109		disk->rdev = rdev;
110		if (mddev->chunk_sectors) {
111			sectors = rdev->sectors;
112			sector_div(sectors, mddev->chunk_sectors);
113			rdev->sectors = sectors * mddev->chunk_sectors;
114		}
115
116		disk_stack_limits(mddev->gendisk, rdev->bdev,
117				  rdev->data_offset << 9);
118
119		conf->array_sectors += rdev->sectors;
120		cnt++;
121
122		if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
123			discard_supported = true;
124	}
125	if (cnt != raid_disks) {
126		printk(KERN_ERR "md/linear:%s: not enough drives present. Aborting!\n",
127		       mdname(mddev));
128		goto out;
129	}
130
131	if (!discard_supported)
132		queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
133	else
134		queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
135
136	/*
137	 * Here we calculate the device offsets.
138	 */
139	conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
140
141	for (i = 1; i < raid_disks; i++)
142		conf->disks[i].end_sector =
143			conf->disks[i-1].end_sector +
144			conf->disks[i].rdev->sectors;
145
 
 
 
 
 
 
 
 
 
 
 
 
 
146	return conf;
147
148out:
149	kfree(conf);
150	return NULL;
151}
152
153static int linear_run (struct mddev *mddev)
154{
155	struct linear_conf *conf;
156	int ret;
157
158	if (md_check_no_bitmap(mddev))
159		return -EINVAL;
160	conf = linear_conf(mddev, mddev->raid_disks);
161
162	if (!conf)
163		return 1;
164	mddev->private = conf;
165	md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
166
167	ret =  md_integrity_register(mddev);
168	if (ret) {
169		kfree(conf);
170		mddev->private = NULL;
171	}
172	return ret;
173}
174
175static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
176{
177	/* Adding a drive to a linear array allows the array to grow.
178	 * It is permitted if the new drive has a matching superblock
179	 * already on it, with raid_disk equal to raid_disks.
180	 * It is achieved by creating a new linear_private_data structure
181	 * and swapping it in in-place of the current one.
182	 * The current one is never freed until the array is stopped.
183	 * This avoids races.
184	 */
185	struct linear_conf *newconf, *oldconf;
186
187	if (rdev->saved_raid_disk != mddev->raid_disks)
188		return -EINVAL;
189
190	rdev->raid_disk = rdev->saved_raid_disk;
191	rdev->saved_raid_disk = -1;
192
193	newconf = linear_conf(mddev,mddev->raid_disks+1);
194
195	if (!newconf)
196		return -ENOMEM;
197
 
 
 
 
 
 
198	mddev_suspend(mddev);
199	oldconf = mddev->private;
200	mddev->raid_disks++;
201	mddev->private = newconf;
 
 
202	md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
203	set_capacity(mddev->gendisk, mddev->array_sectors);
204	mddev_resume(mddev);
205	revalidate_disk(mddev->gendisk);
206	kfree(oldconf);
207	return 0;
208}
209
210static void linear_free(struct mddev *mddev, void *priv)
211{
212	struct linear_conf *conf = priv;
213
214	kfree(conf);
215}
216
217static void linear_make_request(struct mddev *mddev, struct bio *bio)
218{
219	char b[BDEVNAME_SIZE];
220	struct dev_info *tmp_dev;
221	struct bio *split;
222	sector_t start_sector, end_sector, data_offset;
223
224	if (unlikely(bio->bi_rw & REQ_FLUSH)) {
225		md_flush_request(mddev, bio);
226		return;
227	}
228
229	do {
230		tmp_dev = which_dev(mddev, bio->bi_iter.bi_sector);
 
231		start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
232		end_sector = tmp_dev->end_sector;
233		data_offset = tmp_dev->rdev->data_offset;
234		bio->bi_bdev = tmp_dev->rdev->bdev;
235
236		if (unlikely(bio->bi_iter.bi_sector >= end_sector ||
237			     bio->bi_iter.bi_sector < start_sector))
238			goto out_of_bounds;
239
240		if (unlikely(bio_end_sector(bio) > end_sector)) {
241			/* This bio crosses a device boundary, so we have to
242			 * split it.
243			 */
244			split = bio_split(bio, end_sector -
245					  bio->bi_iter.bi_sector,
246					  GFP_NOIO, fs_bio_set);
247			bio_chain(split, bio);
248		} else {
249			split = bio;
250		}
251
252		split->bi_iter.bi_sector = split->bi_iter.bi_sector -
253			start_sector + data_offset;
254
255		if (unlikely((split->bi_rw & REQ_DISCARD) &&
256			 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
257			/* Just ignore it */
258			bio_endio(split);
259		} else
 
 
 
 
260			generic_make_request(split);
 
261	} while (split != bio);
262	return;
263
264out_of_bounds:
265	printk(KERN_ERR
266	       "md/linear:%s: make_request: Sector %llu out of bounds on "
267	       "dev %s: %llu sectors, offset %llu\n",
268	       mdname(mddev),
269	       (unsigned long long)bio->bi_iter.bi_sector,
270	       bdevname(tmp_dev->rdev->bdev, b),
271	       (unsigned long long)tmp_dev->rdev->sectors,
272	       (unsigned long long)start_sector);
273	bio_io_error(bio);
274}
275
276static void linear_status (struct seq_file *seq, struct mddev *mddev)
277{
278
279	seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
280}
281
282static void linear_quiesce(struct mddev *mddev, int state)
283{
284}
285
286static struct md_personality linear_personality =
287{
288	.name		= "linear",
289	.level		= LEVEL_LINEAR,
290	.owner		= THIS_MODULE,
291	.make_request	= linear_make_request,
292	.run		= linear_run,
293	.free		= linear_free,
294	.status		= linear_status,
295	.hot_add_disk	= linear_add,
296	.size		= linear_size,
297	.quiesce	= linear_quiesce,
298	.congested	= linear_congested,
299};
300
301static int __init linear_init (void)
302{
303	return register_md_personality (&linear_personality);
304}
305
306static void linear_exit (void)
307{
308	unregister_md_personality (&linear_personality);
309}
310
311module_init(linear_init);
312module_exit(linear_exit);
313MODULE_LICENSE("GPL");
314MODULE_DESCRIPTION("Linear device concatenation personality for MD");
315MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
316MODULE_ALIAS("md-linear");
317MODULE_ALIAS("md-level--1");
v4.10.11
  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 <linux/module.h>
 23#include <linux/slab.h>
 24#include <trace/events/block.h>
 25#include "md.h"
 26#include "linear.h"
 27
 28/*
 29 * find which device holds a particular offset
 30 */
 31static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
 32{
 33	int lo, mid, hi;
 34	struct linear_conf *conf;
 35
 36	lo = 0;
 37	hi = mddev->raid_disks - 1;
 38	conf = mddev->private;
 39
 40	/*
 41	 * Binary Search
 42	 */
 43
 44	while (hi > lo) {
 45
 46		mid = (hi + lo) / 2;
 47		if (sector < conf->disks[mid].end_sector)
 48			hi = mid;
 49		else
 50			lo = mid + 1;
 51	}
 52
 53	return conf->disks + lo;
 54}
 55
 56/*
 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.
 61 */
 62static int linear_congested(struct mddev *mddev, int bits)
 63{
 64	struct linear_conf *conf;
 65	int i, ret = 0;
 66
 67	rcu_read_lock();
 68	conf = rcu_dereference(mddev->private);
 69
 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);
 73	}
 74
 75	rcu_read_unlock();
 76	return ret;
 77}
 78
 79static sector_t linear_size(struct mddev *mddev, sector_t sectors, int raid_disks)
 80{
 81	struct linear_conf *conf;
 82	sector_t array_sectors;
 83
 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;
 88
 89	return array_sectors;
 90}
 91
 92static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
 93{
 94	struct linear_conf *conf;
 95	struct md_rdev *rdev;
 96	int i, cnt;
 97	bool discard_supported = false;
 98
 99	conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
100			GFP_KERNEL);
101	if (!conf)
102		return NULL;
103
104	cnt = 0;
105	conf->array_sectors = 0;
106
107	rdev_for_each(rdev, mddev) {
108		int j = rdev->raid_disk;
109		struct dev_info *disk = conf->disks + j;
110		sector_t sectors;
111
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;
116		}
117
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;
123		}
124
125		disk_stack_limits(mddev->gendisk, rdev->bdev,
126				  rdev->data_offset << 9);
127
128		conf->array_sectors += rdev->sectors;
129		cnt++;
130
131		if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
132			discard_supported = true;
133	}
134	if (cnt != raid_disks) {
135		pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
136			mdname(mddev));
137		goto out;
138	}
139
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);
144
145	/*
146	 * Here we calculate the device offsets.
147	 */
148	conf->disks[0].end_sector = conf->disks[0].rdev->sectors;
149
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;
154
155	/*
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.
165	 */
166	conf->raid_disks = raid_disks;
167
168	return conf;
169
170out:
171	kfree(conf);
172	return NULL;
173}
174
175static int linear_run (struct mddev *mddev)
176{
177	struct linear_conf *conf;
178	int ret;
179
180	if (md_check_no_bitmap(mddev))
181		return -EINVAL;
182	conf = linear_conf(mddev, mddev->raid_disks);
183
184	if (!conf)
185		return 1;
186	mddev->private = conf;
187	md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
188
189	ret =  md_integrity_register(mddev);
190	if (ret) {
191		kfree(conf);
192		mddev->private = NULL;
193	}
194	return ret;
195}
196
197static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
198{
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.
206	 */
207	struct linear_conf *newconf, *oldconf;
208
209	if (rdev->saved_raid_disk != mddev->raid_disks)
210		return -EINVAL;
211
212	rdev->raid_disk = rdev->saved_raid_disk;
213	rdev->saved_raid_disk = -1;
214
215	newconf = linear_conf(mddev,mddev->raid_disks+1);
216
217	if (!newconf)
218		return -ENOMEM;
219
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.
225	 */
226	mddev_suspend(mddev);
227	oldconf = rcu_dereference(mddev->private);
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;
238}
239
240static void linear_free(struct mddev *mddev, void *priv)
241{
242	struct linear_conf *conf = priv;
243
244	kfree(conf);
245}
246
247static void linear_make_request(struct mddev *mddev, struct bio *bio)
248{
249	char b[BDEVNAME_SIZE];
250	struct dev_info *tmp_dev;
251	struct bio *split;
252	sector_t start_sector, end_sector, data_offset;
253
254	if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
255		md_flush_request(mddev, bio);
256		return;
257	}
258
259	do {
260		sector_t bio_sector = bio->bi_iter.bi_sector;
261		tmp_dev = which_dev(mddev, bio_sector);
262		start_sector = tmp_dev->end_sector - tmp_dev->rdev->sectors;
263		end_sector = tmp_dev->end_sector;
264		data_offset = tmp_dev->rdev->data_offset;
265		bio->bi_bdev = tmp_dev->rdev->bdev;
266
267		if (unlikely(bio_sector >= end_sector ||
268			     bio_sector < start_sector))
269			goto out_of_bounds;
270
271		if (unlikely(bio_end_sector(bio) > end_sector)) {
272			/* This bio crosses a device boundary, so we have to
273			 * split it.
274			 */
275			split = bio_split(bio, end_sector - bio_sector,
 
276					  GFP_NOIO, fs_bio_set);
277			bio_chain(split, bio);
278		} else {
279			split = bio;
280		}
281
282		split->bi_iter.bi_sector = split->bi_iter.bi_sector -
283			start_sector + data_offset;
284
285		if (unlikely((bio_op(split) == REQ_OP_DISCARD) &&
286			 !blk_queue_discard(bdev_get_queue(split->bi_bdev)))) {
287			/* Just ignore it */
288			bio_endio(split);
289		} else {
290			if (mddev->gendisk)
291				trace_block_bio_remap(bdev_get_queue(split->bi_bdev),
292						      split, disk_devt(mddev->gendisk),
293						      bio_sector);
294			generic_make_request(split);
295		}
296	} while (split != bio);
297	return;
298
299out_of_bounds:
300	pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
 
 
301	       mdname(mddev),
302	       (unsigned long long)bio->bi_iter.bi_sector,
303	       bdevname(tmp_dev->rdev->bdev, b),
304	       (unsigned long long)tmp_dev->rdev->sectors,
305	       (unsigned long long)start_sector);
306	bio_io_error(bio);
307}
308
309static void linear_status (struct seq_file *seq, struct mddev *mddev)
310{
 
311	seq_printf(seq, " %dk rounding", mddev->chunk_sectors / 2);
312}
313
314static void linear_quiesce(struct mddev *mddev, int state)
315{
316}
317
318static struct md_personality linear_personality =
319{
320	.name		= "linear",
321	.level		= LEVEL_LINEAR,
322	.owner		= THIS_MODULE,
323	.make_request	= linear_make_request,
324	.run		= linear_run,
325	.free		= linear_free,
326	.status		= linear_status,
327	.hot_add_disk	= linear_add,
328	.size		= linear_size,
329	.quiesce	= linear_quiesce,
330	.congested	= linear_congested,
331};
332
333static int __init linear_init (void)
334{
335	return register_md_personality (&linear_personality);
336}
337
338static void linear_exit (void)
339{
340	unregister_md_personality (&linear_personality);
341}
342
343module_init(linear_init);
344module_exit(linear_exit);
345MODULE_LICENSE("GPL");
346MODULE_DESCRIPTION("Linear device concatenation personality for MD");
347MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
348MODULE_ALIAS("md-linear");
349MODULE_ALIAS("md-level--1");