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>
28 * find which device holds a particular offset
30 static inline struct dev_info
*which_dev(struct mddev
*mddev
, sector_t sector
)
33 struct linear_conf
*conf
;
36 hi
= mddev
->raid_disks
- 1;
37 conf
= mddev
->private;
46 if (sector
< conf
->disks
[mid
].end_sector
)
52 return conf
->disks
+ lo
;
55 static int linear_congested(struct mddev
*mddev
, int bits
)
57 struct linear_conf
*conf
;
60 conf
= mddev
->private;
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
);
70 static sector_t
linear_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
72 struct linear_conf
*conf
;
73 sector_t array_sectors
;
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
;
83 static struct linear_conf
*linear_conf(struct mddev
*mddev
, int raid_disks
)
85 struct linear_conf
*conf
;
88 bool discard_supported
= false;
90 conf
= kzalloc (sizeof (*conf
) + raid_disks
*sizeof(struct dev_info
),
96 conf
->array_sectors
= 0;
98 rdev_for_each(rdev
, mddev
) {
99 int j
= rdev
->raid_disk
;
100 struct dev_info
*disk
= conf
->disks
+ j
;
103 if (j
< 0 || j
>= raid_disks
|| disk
->rdev
) {
104 printk(KERN_ERR
"md/linear:%s: disk numbering problem. Aborting!\n",
110 if (mddev
->chunk_sectors
) {
111 sectors
= rdev
->sectors
;
112 sector_div(sectors
, mddev
->chunk_sectors
);
113 rdev
->sectors
= sectors
* mddev
->chunk_sectors
;
116 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
117 rdev
->data_offset
<< 9);
119 conf
->array_sectors
+= rdev
->sectors
;
122 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
123 discard_supported
= true;
125 if (cnt
!= raid_disks
) {
126 printk(KERN_ERR
"md/linear:%s: not enough drives present. Aborting!\n",
131 if (!discard_supported
)
132 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
134 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
137 * Here we calculate the device offsets.
139 conf
->disks
[0].end_sector
= conf
->disks
[0].rdev
->sectors
;
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
;
153 static int linear_run (struct mddev
*mddev
)
155 struct linear_conf
*conf
;
158 if (md_check_no_bitmap(mddev
))
160 conf
= linear_conf(mddev
, mddev
->raid_disks
);
164 mddev
->private = conf
;
165 md_set_array_sectors(mddev
, linear_size(mddev
, 0, 0));
167 ret
= md_integrity_register(mddev
);
170 mddev
->private = NULL
;
175 static int linear_add(struct mddev
*mddev
, struct md_rdev
*rdev
)
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.
185 struct linear_conf
*newconf
, *oldconf
;
187 if (rdev
->saved_raid_disk
!= mddev
->raid_disks
)
190 rdev
->raid_disk
= rdev
->saved_raid_disk
;
191 rdev
->saved_raid_disk
= -1;
193 newconf
= linear_conf(mddev
,mddev
->raid_disks
+1);
198 mddev_suspend(mddev
);
199 oldconf
= mddev
->private;
201 mddev
->private = newconf
;
202 md_set_array_sectors(mddev
, linear_size(mddev
, 0, 0));
203 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
205 revalidate_disk(mddev
->gendisk
);
210 static void linear_free(struct mddev
*mddev
, void *priv
)
212 struct linear_conf
*conf
= priv
;
217 static void linear_make_request(struct mddev
*mddev
, struct bio
*bio
)
219 char b
[BDEVNAME_SIZE
];
220 struct dev_info
*tmp_dev
;
222 sector_t start_sector
, end_sector
, data_offset
;
224 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
225 md_flush_request(mddev
, bio
);
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
;
236 if (unlikely(bio
->bi_iter
.bi_sector
>= end_sector
||
237 bio
->bi_iter
.bi_sector
< start_sector
))
240 if (unlikely(bio_end_sector(bio
) > end_sector
)) {
241 /* This bio crosses a device boundary, so we have to
244 split
= bio_split(bio
, end_sector
-
245 bio
->bi_iter
.bi_sector
,
246 GFP_NOIO
, fs_bio_set
);
247 bio_chain(split
, bio
);
252 split
->bi_iter
.bi_sector
= split
->bi_iter
.bi_sector
-
253 start_sector
+ data_offset
;
255 if (unlikely((bio_op(split
) == REQ_OP_DISCARD
) &&
256 !blk_queue_discard(bdev_get_queue(split
->bi_bdev
)))) {
260 generic_make_request(split
);
261 } while (split
!= bio
);
266 "md/linear:%s: make_request: Sector %llu out of bounds on "
267 "dev %s: %llu sectors, offset %llu\n",
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
);
276 static void linear_status (struct seq_file
*seq
, struct mddev
*mddev
)
279 seq_printf(seq
, " %dk rounding", mddev
->chunk_sectors
/ 2);
282 static void linear_quiesce(struct mddev
*mddev
, int state
)
286 static struct md_personality linear_personality
=
289 .level
= LEVEL_LINEAR
,
290 .owner
= THIS_MODULE
,
291 .make_request
= linear_make_request
,
294 .status
= linear_status
,
295 .hot_add_disk
= linear_add
,
297 .quiesce
= linear_quiesce
,
298 .congested
= linear_congested
,
301 static int __init
linear_init (void)
303 return register_md_personality (&linear_personality
);
306 static void linear_exit (void)
308 unregister_md_personality (&linear_personality
);
311 module_init(linear_init
);
312 module_exit(linear_exit
);
313 MODULE_LICENSE("GPL");
314 MODULE_DESCRIPTION("Linear device concatenation personality for MD");
315 MODULE_ALIAS("md-personality-1"); /* LINEAR - deprecated*/
316 MODULE_ALIAS("md-linear");
317 MODULE_ALIAS("md-level--1");