2 raid0.c : Multiple Devices driver for Linux
3 Copyright (C) 1994-96 Marc ZYNGIER
4 <zyngier@ufr-info-p7.ibp.fr> or
6 Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
9 RAID-0 management functions.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 You should have received a copy of the GNU General Public License
17 (for example /usr/src/linux/COPYING); if not, write to the Free
18 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/blkdev.h>
22 #include <linux/seq_file.h>
26 static void raid0_unplug(struct request_queue
*q
)
28 mddev_t
*mddev
= q
->queuedata
;
29 raid0_conf_t
*conf
= mddev_to_conf(mddev
);
30 mdk_rdev_t
**devlist
= conf
->devlist
;
33 for (i
=0; i
<mddev
->raid_disks
; i
++) {
34 struct request_queue
*r_queue
= bdev_get_queue(devlist
[i
]->bdev
);
40 static int raid0_congested(void *data
, int bits
)
42 mddev_t
*mddev
= data
;
43 raid0_conf_t
*conf
= mddev_to_conf(mddev
);
44 mdk_rdev_t
**devlist
= conf
->devlist
;
47 for (i
= 0; i
< mddev
->raid_disks
&& !ret
; i
++) {
48 struct request_queue
*q
= bdev_get_queue(devlist
[i
]->bdev
);
50 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
55 static int create_strip_zones(mddev_t
*mddev
)
58 sector_t curr_zone_end
, sectors
;
59 mdk_rdev_t
*smallest
, *rdev1
, *rdev2
, *rdev
, **dev
;
60 struct strip_zone
*zone
;
62 char b
[BDEVNAME_SIZE
];
63 raid0_conf_t
*conf
= kzalloc(sizeof(*conf
), GFP_KERNEL
);
67 list_for_each_entry(rdev1
, &mddev
->disks
, same_set
) {
68 printk(KERN_INFO
"raid0: looking at %s\n",
69 bdevname(rdev1
->bdev
,b
));
71 list_for_each_entry(rdev2
, &mddev
->disks
, same_set
) {
72 printk(KERN_INFO
"raid0: comparing %s(%llu)",
73 bdevname(rdev1
->bdev
,b
),
74 (unsigned long long)rdev1
->sectors
);
75 printk(KERN_INFO
" with %s(%llu)\n",
76 bdevname(rdev2
->bdev
,b
),
77 (unsigned long long)rdev2
->sectors
);
79 printk(KERN_INFO
"raid0: END\n");
82 if (rdev2
->sectors
== rdev1
->sectors
) {
84 * Not unique, don't count it as a new
87 printk(KERN_INFO
"raid0: EQUAL\n");
91 printk(KERN_INFO
"raid0: NOT EQUAL\n");
94 printk(KERN_INFO
"raid0: ==> UNIQUE\n");
95 conf
->nr_strip_zones
++;
96 printk(KERN_INFO
"raid0: %d zones\n",
97 conf
->nr_strip_zones
);
100 printk(KERN_INFO
"raid0: FINAL %d zones\n", conf
->nr_strip_zones
);
102 conf
->strip_zone
= kzalloc(sizeof(struct strip_zone
)*
103 conf
->nr_strip_zones
, GFP_KERNEL
);
104 if (!conf
->strip_zone
)
106 conf
->devlist
= kzalloc(sizeof(mdk_rdev_t
*)*
107 conf
->nr_strip_zones
*mddev
->raid_disks
,
112 /* The first zone must contain all devices, so here we check that
113 * there is a proper alignment of slots to devices and find them all
115 zone
= &conf
->strip_zone
[0];
120 list_for_each_entry(rdev1
, &mddev
->disks
, same_set
) {
121 int j
= rdev1
->raid_disk
;
123 if (j
< 0 || j
>= mddev
->raid_disks
) {
124 printk(KERN_ERR
"raid0: bad disk number %d - "
129 printk(KERN_ERR
"raid0: multiple devices for %d - "
135 blk_queue_stack_limits(mddev
->queue
,
136 rdev1
->bdev
->bd_disk
->queue
);
137 /* as we don't honour merge_bvec_fn, we must never risk
138 * violating it, so limit ->max_sector to one PAGE, as
139 * a one page request is never in violation.
142 if (rdev1
->bdev
->bd_disk
->queue
->merge_bvec_fn
&&
143 queue_max_sectors(mddev
->queue
) > (PAGE_SIZE
>>9))
144 blk_queue_max_sectors(mddev
->queue
, PAGE_SIZE
>>9);
146 if (!smallest
|| (rdev1
->sectors
< smallest
->sectors
))
150 if (cnt
!= mddev
->raid_disks
) {
151 printk(KERN_ERR
"raid0: too few disks (%d of %d) - "
152 "aborting!\n", cnt
, mddev
->raid_disks
);
156 zone
->zone_end
= smallest
->sectors
* cnt
;
158 curr_zone_end
= zone
->zone_end
;
160 /* now do the other zones */
161 for (i
= 1; i
< conf
->nr_strip_zones
; i
++)
163 zone
= conf
->strip_zone
+ i
;
164 dev
= conf
->devlist
+ i
* mddev
->raid_disks
;
166 printk(KERN_INFO
"raid0: zone %d\n", i
);
167 zone
->dev_start
= smallest
->sectors
;
171 for (j
=0; j
<cnt
; j
++) {
172 char b
[BDEVNAME_SIZE
];
173 rdev
= conf
->devlist
[j
];
174 printk(KERN_INFO
"raid0: checking %s ...",
175 bdevname(rdev
->bdev
, b
));
176 if (rdev
->sectors
<= zone
->dev_start
) {
177 printk(KERN_INFO
" nope.\n");
180 printk(KERN_INFO
" contained as device %d\n", c
);
183 if (!smallest
|| rdev
->sectors
< smallest
->sectors
) {
185 printk(KERN_INFO
" (%llu) is smallest!.\n",
186 (unsigned long long)rdev
->sectors
);
191 sectors
= (smallest
->sectors
- zone
->dev_start
) * c
;
192 printk(KERN_INFO
"raid0: zone->nb_dev: %d, sectors: %llu\n",
193 zone
->nb_dev
, (unsigned long long)sectors
);
195 curr_zone_end
+= sectors
;
196 zone
->zone_end
= curr_zone_end
;
198 printk(KERN_INFO
"raid0: current zone start: %llu\n",
199 (unsigned long long)smallest
->sectors
);
201 mddev
->queue
->unplug_fn
= raid0_unplug
;
202 mddev
->queue
->backing_dev_info
.congested_fn
= raid0_congested
;
203 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
205 printk(KERN_INFO
"raid0: done.\n");
206 mddev
->private = conf
;
209 kfree(conf
->strip_zone
);
210 kfree(conf
->devlist
);
212 mddev
->private = NULL
;
217 * raid0_mergeable_bvec -- tell bio layer if a two requests can be merged
219 * @bvm: properties of new bio
220 * @biovec: the request that could be merged to it.
222 * Return amount of bytes we can accept at this offset
224 static int raid0_mergeable_bvec(struct request_queue
*q
,
225 struct bvec_merge_data
*bvm
,
226 struct bio_vec
*biovec
)
228 mddev_t
*mddev
= q
->queuedata
;
229 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
231 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
232 unsigned int bio_sectors
= bvm
->bi_size
>> 9;
234 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
235 if (max
< 0) max
= 0; /* bio_add cannot handle a negative return */
236 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
237 return biovec
->bv_len
;
242 static sector_t
raid0_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
244 sector_t array_sectors
= 0;
247 WARN_ONCE(sectors
|| raid_disks
,
248 "%s does not support generic reshape\n", __func__
);
250 list_for_each_entry(rdev
, &mddev
->disks
, same_set
)
251 array_sectors
+= rdev
->sectors
;
253 return array_sectors
;
256 static int raid0_run(mddev_t
*mddev
)
260 if (mddev
->chunk_size
== 0) {
261 printk(KERN_ERR
"md/raid0: non-zero chunk size required.\n");
264 printk(KERN_INFO
"%s: setting max_sectors to %d, segment boundary to %d\n",
266 mddev
->chunk_size
>> 9,
267 (mddev
->chunk_size
>>1)-1);
268 blk_queue_max_sectors(mddev
->queue
, mddev
->chunk_size
>> 9);
269 blk_queue_segment_boundary(mddev
->queue
, (mddev
->chunk_size
>>1) - 1);
270 mddev
->queue
->queue_lock
= &mddev
->queue
->__queue_lock
;
272 ret
= create_strip_zones(mddev
);
276 /* calculate array device size */
277 md_set_array_sectors(mddev
, raid0_size(mddev
, 0, 0));
279 printk(KERN_INFO
"raid0 : md_size is %llu sectors.\n",
280 (unsigned long long)mddev
->array_sectors
);
281 /* calculate the max read-ahead size.
282 * For read-ahead of large files to be effective, we need to
283 * readahead at least twice a whole stripe. i.e. number of devices
284 * multiplied by chunk size times 2.
285 * If an individual device has an ra_pages greater than the
286 * chunk size, then we will not drive that device as hard as it
287 * wants. We consider this a configuration error: a larger
288 * chunksize should be used in that case.
291 int stripe
= mddev
->raid_disks
* mddev
->chunk_size
/ PAGE_SIZE
;
292 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2* stripe
)
293 mddev
->queue
->backing_dev_info
.ra_pages
= 2* stripe
;
296 blk_queue_merge_bvec(mddev
->queue
, raid0_mergeable_bvec
);
300 static int raid0_stop(mddev_t
*mddev
)
302 raid0_conf_t
*conf
= mddev_to_conf(mddev
);
304 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
305 kfree(conf
->strip_zone
);
306 kfree(conf
->devlist
);
308 mddev
->private = NULL
;
312 /* Find the zone which holds a particular offset
313 * Update *sectorp to be an offset in that zone
315 static struct strip_zone
*find_zone(struct raid0_private_data
*conf
,
319 struct strip_zone
*z
= conf
->strip_zone
;
320 sector_t sector
= *sectorp
;
322 for (i
= 0; i
< conf
->nr_strip_zones
; i
++)
323 if (sector
< z
[i
].zone_end
) {
325 *sectorp
= sector
- z
[i
-1].zone_end
;
331 static int raid0_make_request (struct request_queue
*q
, struct bio
*bio
)
333 mddev_t
*mddev
= q
->queuedata
;
334 unsigned int sect_in_chunk
, chunksect_bits
, chunk_sects
;
335 raid0_conf_t
*conf
= mddev_to_conf(mddev
);
336 struct strip_zone
*zone
;
339 sector_t sector
, rsect
, sector_offset
;
340 const int rw
= bio_data_dir(bio
);
343 if (unlikely(bio_barrier(bio
))) {
344 bio_endio(bio
, -EOPNOTSUPP
);
348 cpu
= part_stat_lock();
349 part_stat_inc(cpu
, &mddev
->gendisk
->part0
, ios
[rw
]);
350 part_stat_add(cpu
, &mddev
->gendisk
->part0
, sectors
[rw
],
354 chunk_sects
= mddev
->chunk_size
>> 9;
355 chunksect_bits
= ffz(~chunk_sects
);
356 sector
= bio
->bi_sector
;
358 if (unlikely(chunk_sects
< (bio
->bi_sector
& (chunk_sects
- 1)) + (bio
->bi_size
>> 9))) {
360 /* Sanity check -- queue functions should prevent this happening */
361 if (bio
->bi_vcnt
!= 1 ||
364 /* This is a one page bio that upper layers
365 * refuse to split for us, so we need to split it.
367 bp
= bio_split(bio
, chunk_sects
- (bio
->bi_sector
& (chunk_sects
- 1)));
368 if (raid0_make_request(q
, &bp
->bio1
))
369 generic_make_request(&bp
->bio1
);
370 if (raid0_make_request(q
, &bp
->bio2
))
371 generic_make_request(&bp
->bio2
);
373 bio_pair_release(bp
);
376 sector_offset
= sector
;
377 zone
= find_zone(conf
, §or_offset
);
378 sect_in_chunk
= bio
->bi_sector
& (chunk_sects
- 1);
380 sector_t x
= sector_offset
>> chunksect_bits
;
382 sector_div(x
, zone
->nb_dev
);
385 x
= sector
>> chunksect_bits
;
386 tmp_dev
= conf
->devlist
[(zone
- conf
->strip_zone
)*mddev
->raid_disks
387 + sector_div(x
, zone
->nb_dev
)];
389 rsect
= (chunk
<< chunksect_bits
) + zone
->dev_start
+ sect_in_chunk
;
391 bio
->bi_bdev
= tmp_dev
->bdev
;
392 bio
->bi_sector
= rsect
+ tmp_dev
->data_offset
;
395 * Let the main block layer submit the IO and resolve recursion:
400 printk("raid0_make_request bug: can't convert block across chunks"
401 " or bigger than %dk %llu %d\n", chunk_sects
/ 2,
402 (unsigned long long)bio
->bi_sector
, bio
->bi_size
>> 10);
408 static void raid0_status (struct seq_file
*seq
, mddev_t
*mddev
)
413 char b
[BDEVNAME_SIZE
];
414 raid0_conf_t
*conf
= mddev_to_conf(mddev
);
417 for (j
= 0; j
< conf
->nr_strip_zones
; j
++) {
418 seq_printf(seq
, " z%d", j
);
419 seq_printf(seq
, "=[");
420 for (k
= 0; k
< conf
->strip_zone
[j
].nb_dev
; k
++)
421 seq_printf(seq
, "%s/", bdevname(
422 conf
->strip_zone
[j
].dev
[k
]->bdev
,b
));
424 seq_printf(seq
, "] ze=%d ds=%d s=%d\n",
425 conf
->strip_zone
[j
].zone_end
,
426 conf
->strip_zone
[j
].dev_start
,
427 conf
->strip_zone
[j
].sectors
);
430 seq_printf(seq
, " %dk chunks", mddev
->chunk_size
/1024);
434 static struct mdk_personality raid0_personality
=
438 .owner
= THIS_MODULE
,
439 .make_request
= raid0_make_request
,
442 .status
= raid0_status
,
446 static int __init
raid0_init (void)
448 return register_md_personality (&raid0_personality
);
451 static void raid0_exit (void)
453 unregister_md_personality (&raid0_personality
);
456 module_init(raid0_init
);
457 module_exit(raid0_exit
);
458 MODULE_LICENSE("GPL");
459 MODULE_ALIAS("md-personality-2"); /* RAID0 */
460 MODULE_ALIAS("md-raid0");
461 MODULE_ALIAS("md-level-0");