1 // SPDX-License-Identifier: GPL-2.0-or-later
3 linear.c : Multiple Devices driver for Linux
4 Copyright (C) 1994-96 Marc ZYNGIER
5 <zyngier@ufr-info-p7.ibp.fr> or
8 Linear mode management functions.
12 #include <linux/blkdev.h>
13 #include <linux/raid/md_u.h>
14 #include <linux/seq_file.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <trace/events/block.h>
19 #include "md-linear.h"
22 * find which device holds a particular offset
24 static inline struct dev_info
*which_dev(struct mddev
*mddev
, sector_t sector
)
27 struct linear_conf
*conf
;
30 hi
= mddev
->raid_disks
- 1;
31 conf
= mddev
->private;
40 if (sector
< conf
->disks
[mid
].end_sector
)
46 return conf
->disks
+ lo
;
50 * In linear_congested() conf->raid_disks is used as a copy of
51 * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks
52 * and conf->disks[] are created in linear_conf(), they are always
53 * consitent with each other, but mddev->raid_disks does not.
55 static int linear_congested(struct mddev
*mddev
, int bits
)
57 struct linear_conf
*conf
;
61 conf
= rcu_dereference(mddev
->private);
63 for (i
= 0; i
< conf
->raid_disks
&& !ret
; i
++) {
64 struct request_queue
*q
= bdev_get_queue(conf
->disks
[i
].rdev
->bdev
);
65 ret
|= bdi_congested(q
->backing_dev_info
, bits
);
72 static sector_t
linear_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
74 struct linear_conf
*conf
;
75 sector_t array_sectors
;
77 conf
= mddev
->private;
78 WARN_ONCE(sectors
|| raid_disks
,
79 "%s does not support generic reshape\n", __func__
);
80 array_sectors
= conf
->array_sectors
;
85 static struct linear_conf
*linear_conf(struct mddev
*mddev
, int raid_disks
)
87 struct linear_conf
*conf
;
90 bool discard_supported
= false;
92 conf
= kzalloc(struct_size(conf
, disks
, raid_disks
), GFP_KERNEL
);
97 conf
->array_sectors
= 0;
99 rdev_for_each(rdev
, mddev
) {
100 int j
= rdev
->raid_disk
;
101 struct dev_info
*disk
= conf
->disks
+ j
;
104 if (j
< 0 || j
>= raid_disks
|| disk
->rdev
) {
105 pr_warn("md/linear:%s: disk numbering problem. Aborting!\n",
111 if (mddev
->chunk_sectors
) {
112 sectors
= rdev
->sectors
;
113 sector_div(sectors
, mddev
->chunk_sectors
);
114 rdev
->sectors
= sectors
* mddev
->chunk_sectors
;
117 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
118 rdev
->data_offset
<< 9);
120 conf
->array_sectors
+= rdev
->sectors
;
123 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
124 discard_supported
= true;
126 if (cnt
!= raid_disks
) {
127 pr_warn("md/linear:%s: not enough drives present. Aborting!\n",
132 if (!discard_supported
)
133 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
, mddev
->queue
);
135 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, mddev
->queue
);
138 * Here we calculate the device offsets.
140 conf
->disks
[0].end_sector
= conf
->disks
[0].rdev
->sectors
;
142 for (i
= 1; i
< raid_disks
; i
++)
143 conf
->disks
[i
].end_sector
=
144 conf
->disks
[i
-1].end_sector
+
145 conf
->disks
[i
].rdev
->sectors
;
148 * conf->raid_disks is copy of mddev->raid_disks. The reason to
149 * keep a copy of mddev->raid_disks in struct linear_conf is,
150 * mddev->raid_disks may not be consistent with pointers number of
151 * conf->disks[] when it is updated in linear_add() and used to
152 * iterate old conf->disks[] earray in linear_congested().
153 * Here conf->raid_disks is always consitent with number of
154 * pointers in conf->disks[] array, and mddev->private is updated
155 * with rcu_assign_pointer() in linear_addr(), such race can be
158 conf
->raid_disks
= raid_disks
;
167 static int linear_run (struct mddev
*mddev
)
169 struct linear_conf
*conf
;
172 if (md_check_no_bitmap(mddev
))
174 conf
= linear_conf(mddev
, mddev
->raid_disks
);
178 mddev
->private = conf
;
179 md_set_array_sectors(mddev
, linear_size(mddev
, 0, 0));
181 ret
= md_integrity_register(mddev
);
184 mddev
->private = NULL
;
189 static int linear_add(struct mddev
*mddev
, struct md_rdev
*rdev
)
191 /* Adding a drive to a linear array allows the array to grow.
192 * It is permitted if the new drive has a matching superblock
193 * already on it, with raid_disk equal to raid_disks.
194 * It is achieved by creating a new linear_private_data structure
195 * and swapping it in in-place of the current one.
196 * The current one is never freed until the array is stopped.
199 struct linear_conf
*newconf
, *oldconf
;
201 if (rdev
->saved_raid_disk
!= mddev
->raid_disks
)
204 rdev
->raid_disk
= rdev
->saved_raid_disk
;
205 rdev
->saved_raid_disk
= -1;
207 newconf
= linear_conf(mddev
,mddev
->raid_disks
+1);
212 /* newconf->raid_disks already keeps a copy of * the increased
213 * value of mddev->raid_disks, WARN_ONCE() is just used to make
214 * sure of this. It is possible that oldconf is still referenced
215 * in linear_congested(), therefore kfree_rcu() is used to free
216 * oldconf until no one uses it anymore.
218 mddev_suspend(mddev
);
219 oldconf
= rcu_dereference_protected(mddev
->private,
220 lockdep_is_held(&mddev
->reconfig_mutex
));
222 WARN_ONCE(mddev
->raid_disks
!= newconf
->raid_disks
,
223 "copied raid_disks doesn't match mddev->raid_disks");
224 rcu_assign_pointer(mddev
->private, newconf
);
225 md_set_array_sectors(mddev
, linear_size(mddev
, 0, 0));
226 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
228 revalidate_disk(mddev
->gendisk
);
229 kfree_rcu(oldconf
, rcu
);
233 static void linear_free(struct mddev
*mddev
, void *priv
)
235 struct linear_conf
*conf
= priv
;
240 static bool linear_make_request(struct mddev
*mddev
, struct bio
*bio
)
242 char b
[BDEVNAME_SIZE
];
243 struct dev_info
*tmp_dev
;
244 sector_t start_sector
, end_sector
, data_offset
;
245 sector_t bio_sector
= bio
->bi_iter
.bi_sector
;
247 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)
248 && md_flush_request(mddev
, bio
))
251 tmp_dev
= which_dev(mddev
, bio_sector
);
252 start_sector
= tmp_dev
->end_sector
- tmp_dev
->rdev
->sectors
;
253 end_sector
= tmp_dev
->end_sector
;
254 data_offset
= tmp_dev
->rdev
->data_offset
;
256 if (unlikely(bio_sector
>= end_sector
||
257 bio_sector
< start_sector
))
260 if (unlikely(is_mddev_broken(tmp_dev
->rdev
, "linear"))) {
265 if (unlikely(bio_end_sector(bio
) > end_sector
)) {
266 /* This bio crosses a device boundary, so we have to split it */
267 struct bio
*split
= bio_split(bio
, end_sector
- bio_sector
,
268 GFP_NOIO
, &mddev
->bio_set
);
269 bio_chain(split
, bio
);
270 generic_make_request(bio
);
274 bio_set_dev(bio
, tmp_dev
->rdev
->bdev
);
275 bio
->bi_iter
.bi_sector
= bio
->bi_iter
.bi_sector
-
276 start_sector
+ data_offset
;
278 if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
279 !blk_queue_discard(bio
->bi_disk
->queue
))) {
284 trace_block_bio_remap(bio
->bi_disk
->queue
,
285 bio
, disk_devt(mddev
->gendisk
),
287 mddev_check_writesame(mddev
, bio
);
288 mddev_check_write_zeroes(mddev
, bio
);
289 generic_make_request(bio
);
294 pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
296 (unsigned long long)bio
->bi_iter
.bi_sector
,
297 bdevname(tmp_dev
->rdev
->bdev
, b
),
298 (unsigned long long)tmp_dev
->rdev
->sectors
,
299 (unsigned long long)start_sector
);
304 static void linear_status (struct seq_file
*seq
, struct mddev
*mddev
)
306 seq_printf(seq
, " %dk rounding", mddev
->chunk_sectors
/ 2);
309 static void linear_quiesce(struct mddev
*mddev
, int state
)
313 static struct md_personality linear_personality
=
316 .level
= LEVEL_LINEAR
,
317 .owner
= THIS_MODULE
,
318 .make_request
= linear_make_request
,
321 .status
= linear_status
,
322 .hot_add_disk
= linear_add
,
324 .quiesce
= linear_quiesce
,
325 .congested
= linear_congested
,
328 static int __init
linear_init (void)
330 return register_md_personality (&linear_personality
);
333 static void linear_exit (void)
335 unregister_md_personality (&linear_personality
);
338 module_init(linear_init
);
339 module_exit(linear_exit
);
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
342 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
343 MODULE_ALIAS("md-linear");
344 MODULE_ALIAS("md-level--1");