2 linear.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
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)
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>
29 * find which device holds a particular offset
31 static inline struct dev_info
*which_dev(struct mddev
*mddev
, sector_t sector
)
34 struct linear_conf
*conf
;
37 hi
= mddev
->raid_disks
- 1;
38 conf
= mddev
->private;
47 if (sector
< conf
->disks
[mid
].end_sector
)
53 return conf
->disks
+ lo
;
56 static int linear_congested(struct mddev
*mddev
, int bits
)
58 struct linear_conf
*conf
;
61 conf
= mddev
->private;
63 for (i
= 0; i
< mddev
->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
);
71 static sector_t
linear_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
73 struct linear_conf
*conf
;
74 sector_t array_sectors
;
76 conf
= mddev
->private;
77 WARN_ONCE(sectors
|| raid_disks
,
78 "%s does not support generic reshape\n", __func__
);
79 array_sectors
= conf
->array_sectors
;
84 static struct linear_conf
*linear_conf(struct mddev
*mddev
, int raid_disks
)
86 struct linear_conf
*conf
;
89 bool discard_supported
= false;
91 conf
= kzalloc (sizeof (*conf
) + raid_disks
*sizeof(struct dev_info
),
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 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
135 queue_flag_set_unlocked(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
;
154 static int linear_run (struct mddev
*mddev
)
156 struct linear_conf
*conf
;
159 if (md_check_no_bitmap(mddev
))
161 conf
= linear_conf(mddev
, mddev
->raid_disks
);
165 mddev
->private = conf
;
166 md_set_array_sectors(mddev
, linear_size(mddev
, 0, 0));
168 ret
= md_integrity_register(mddev
);
171 mddev
->private = NULL
;
176 static int linear_add(struct mddev
*mddev
, struct md_rdev
*rdev
)
178 /* Adding a drive to a linear array allows the array to grow.
179 * It is permitted if the new drive has a matching superblock
180 * already on it, with raid_disk equal to raid_disks.
181 * It is achieved by creating a new linear_private_data structure
182 * and swapping it in in-place of the current one.
183 * The current one is never freed until the array is stopped.
186 struct linear_conf
*newconf
, *oldconf
;
188 if (rdev
->saved_raid_disk
!= mddev
->raid_disks
)
191 rdev
->raid_disk
= rdev
->saved_raid_disk
;
192 rdev
->saved_raid_disk
= -1;
194 newconf
= linear_conf(mddev
,mddev
->raid_disks
+1);
199 mddev_suspend(mddev
);
200 oldconf
= mddev
->private;
202 mddev
->private = newconf
;
203 md_set_array_sectors(mddev
, linear_size(mddev
, 0, 0));
204 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
206 revalidate_disk(mddev
->gendisk
);
211 static void linear_free(struct mddev
*mddev
, void *priv
)
213 struct linear_conf
*conf
= priv
;
218 static void linear_make_request(struct mddev
*mddev
, struct bio
*bio
)
220 char b
[BDEVNAME_SIZE
];
221 struct dev_info
*tmp_dev
;
223 sector_t start_sector
, end_sector
, data_offset
;
225 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
226 md_flush_request(mddev
, bio
);
231 sector_t bio_sector
= bio
->bi_iter
.bi_sector
;
232 tmp_dev
= which_dev(mddev
, bio_sector
);
233 start_sector
= tmp_dev
->end_sector
- tmp_dev
->rdev
->sectors
;
234 end_sector
= tmp_dev
->end_sector
;
235 data_offset
= tmp_dev
->rdev
->data_offset
;
236 bio
->bi_bdev
= tmp_dev
->rdev
->bdev
;
238 if (unlikely(bio_sector
>= end_sector
||
239 bio_sector
< start_sector
))
242 if (unlikely(bio_end_sector(bio
) > end_sector
)) {
243 /* This bio crosses a device boundary, so we have to
246 split
= bio_split(bio
, end_sector
- bio_sector
,
247 GFP_NOIO
, fs_bio_set
);
248 bio_chain(split
, bio
);
253 split
->bi_iter
.bi_sector
= split
->bi_iter
.bi_sector
-
254 start_sector
+ data_offset
;
256 if (unlikely((bio_op(split
) == REQ_OP_DISCARD
) &&
257 !blk_queue_discard(bdev_get_queue(split
->bi_bdev
)))) {
262 trace_block_bio_remap(bdev_get_queue(split
->bi_bdev
),
263 split
, disk_devt(mddev
->gendisk
),
265 generic_make_request(split
);
267 } while (split
!= bio
);
271 pr_err("md/linear:%s: make_request: Sector %llu out of bounds on dev %s: %llu sectors, offset %llu\n",
273 (unsigned long long)bio
->bi_iter
.bi_sector
,
274 bdevname(tmp_dev
->rdev
->bdev
, b
),
275 (unsigned long long)tmp_dev
->rdev
->sectors
,
276 (unsigned long long)start_sector
);
280 static void linear_status (struct seq_file
*seq
, struct mddev
*mddev
)
282 seq_printf(seq
, " %dk rounding", mddev
->chunk_sectors
/ 2);
285 static void linear_quiesce(struct mddev
*mddev
, int state
)
289 static struct md_personality linear_personality
=
292 .level
= LEVEL_LINEAR
,
293 .owner
= THIS_MODULE
,
294 .make_request
= linear_make_request
,
297 .status
= linear_status
,
298 .hot_add_disk
= linear_add
,
300 .quiesce
= linear_quiesce
,
301 .congested
= linear_congested
,
304 static int __init
linear_init (void)
306 return register_md_personality (&linear_personality
);
309 static void linear_exit (void)
311 unregister_md_personality (&linear_personality
);
314 module_init(linear_init
);
315 module_exit(linear_exit
);
316 MODULE_LICENSE("GPL");
317 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
318 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
319 MODULE_ALIAS("md-linear");
320 MODULE_ALIAS("md-level--1");