]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * raid10.c : Multiple Devices driver for Linux | |
3 | * | |
4 | * Copyright (C) 2000-2004 Neil Brown | |
5 | * | |
6 | * RAID-10 support for md. | |
7 | * | |
8 | * Base on code in raid1.c. See raid1.c for futher copyright information. | |
9 | * | |
10 | * | |
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) | |
14 | * any later version. | |
15 | * | |
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. | |
19 | */ | |
20 | ||
6cce3b23 | 21 | #include "dm-bio-list.h" |
1da177e4 | 22 | #include <linux/raid/raid10.h> |
6cce3b23 | 23 | #include <linux/raid/bitmap.h> |
1da177e4 LT |
24 | |
25 | /* | |
26 | * RAID10 provides a combination of RAID0 and RAID1 functionality. | |
27 | * The layout of data is defined by | |
28 | * chunk_size | |
29 | * raid_disks | |
30 | * near_copies (stored in low byte of layout) | |
31 | * far_copies (stored in second byte of layout) | |
c93983bf | 32 | * far_offset (stored in bit 16 of layout ) |
1da177e4 LT |
33 | * |
34 | * The data to be stored is divided into chunks using chunksize. | |
35 | * Each device is divided into far_copies sections. | |
36 | * In each section, chunks are laid out in a style similar to raid0, but | |
37 | * near_copies copies of each chunk is stored (each on a different drive). | |
38 | * The starting device for each section is offset near_copies from the starting | |
39 | * device of the previous section. | |
c93983bf | 40 | * Thus they are (near_copies*far_copies) of each chunk, and each is on a different |
1da177e4 LT |
41 | * drive. |
42 | * near_copies and far_copies must be at least one, and their product is at most | |
43 | * raid_disks. | |
c93983bf N |
44 | * |
45 | * If far_offset is true, then the far_copies are handled a bit differently. | |
46 | * The copies are still in different stripes, but instead of be very far apart | |
47 | * on disk, there are adjacent stripes. | |
1da177e4 LT |
48 | */ |
49 | ||
50 | /* | |
51 | * Number of guaranteed r10bios in case of extreme VM load: | |
52 | */ | |
53 | #define NR_RAID10_BIOS 256 | |
54 | ||
55 | static void unplug_slaves(mddev_t *mddev); | |
56 | ||
0a27ec96 N |
57 | static void allow_barrier(conf_t *conf); |
58 | static void lower_barrier(conf_t *conf); | |
59 | ||
dd0fc66f | 60 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 LT |
61 | { |
62 | conf_t *conf = data; | |
63 | r10bio_t *r10_bio; | |
64 | int size = offsetof(struct r10bio_s, devs[conf->copies]); | |
65 | ||
66 | /* allocate a r10bio with room for raid_disks entries in the bios array */ | |
9ffae0cf N |
67 | r10_bio = kzalloc(size, gfp_flags); |
68 | if (!r10_bio) | |
1da177e4 LT |
69 | unplug_slaves(conf->mddev); |
70 | ||
71 | return r10_bio; | |
72 | } | |
73 | ||
74 | static void r10bio_pool_free(void *r10_bio, void *data) | |
75 | { | |
76 | kfree(r10_bio); | |
77 | } | |
78 | ||
79 | #define RESYNC_BLOCK_SIZE (64*1024) | |
80 | //#define RESYNC_BLOCK_SIZE PAGE_SIZE | |
81 | #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) | |
82 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) | |
83 | #define RESYNC_WINDOW (2048*1024) | |
84 | ||
85 | /* | |
86 | * When performing a resync, we need to read and compare, so | |
87 | * we need as many pages are there are copies. | |
88 | * When performing a recovery, we need 2 bios, one for read, | |
89 | * one for write (we recover only one drive per r10buf) | |
90 | * | |
91 | */ | |
dd0fc66f | 92 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 LT |
93 | { |
94 | conf_t *conf = data; | |
95 | struct page *page; | |
96 | r10bio_t *r10_bio; | |
97 | struct bio *bio; | |
98 | int i, j; | |
99 | int nalloc; | |
100 | ||
101 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); | |
102 | if (!r10_bio) { | |
103 | unplug_slaves(conf->mddev); | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) | |
108 | nalloc = conf->copies; /* resync */ | |
109 | else | |
110 | nalloc = 2; /* recovery */ | |
111 | ||
112 | /* | |
113 | * Allocate bios. | |
114 | */ | |
115 | for (j = nalloc ; j-- ; ) { | |
116 | bio = bio_alloc(gfp_flags, RESYNC_PAGES); | |
117 | if (!bio) | |
118 | goto out_free_bio; | |
119 | r10_bio->devs[j].bio = bio; | |
120 | } | |
121 | /* | |
122 | * Allocate RESYNC_PAGES data pages and attach them | |
123 | * where needed. | |
124 | */ | |
125 | for (j = 0 ; j < nalloc; j++) { | |
126 | bio = r10_bio->devs[j].bio; | |
127 | for (i = 0; i < RESYNC_PAGES; i++) { | |
128 | page = alloc_page(gfp_flags); | |
129 | if (unlikely(!page)) | |
130 | goto out_free_pages; | |
131 | ||
132 | bio->bi_io_vec[i].bv_page = page; | |
133 | } | |
134 | } | |
135 | ||
136 | return r10_bio; | |
137 | ||
138 | out_free_pages: | |
139 | for ( ; i > 0 ; i--) | |
1345b1d8 | 140 | safe_put_page(bio->bi_io_vec[i-1].bv_page); |
1da177e4 LT |
141 | while (j--) |
142 | for (i = 0; i < RESYNC_PAGES ; i++) | |
1345b1d8 | 143 | safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page); |
1da177e4 LT |
144 | j = -1; |
145 | out_free_bio: | |
146 | while ( ++j < nalloc ) | |
147 | bio_put(r10_bio->devs[j].bio); | |
148 | r10bio_pool_free(r10_bio, conf); | |
149 | return NULL; | |
150 | } | |
151 | ||
152 | static void r10buf_pool_free(void *__r10_bio, void *data) | |
153 | { | |
154 | int i; | |
155 | conf_t *conf = data; | |
156 | r10bio_t *r10bio = __r10_bio; | |
157 | int j; | |
158 | ||
159 | for (j=0; j < conf->copies; j++) { | |
160 | struct bio *bio = r10bio->devs[j].bio; | |
161 | if (bio) { | |
162 | for (i = 0; i < RESYNC_PAGES; i++) { | |
1345b1d8 | 163 | safe_put_page(bio->bi_io_vec[i].bv_page); |
1da177e4 LT |
164 | bio->bi_io_vec[i].bv_page = NULL; |
165 | } | |
166 | bio_put(bio); | |
167 | } | |
168 | } | |
169 | r10bio_pool_free(r10bio, conf); | |
170 | } | |
171 | ||
172 | static void put_all_bios(conf_t *conf, r10bio_t *r10_bio) | |
173 | { | |
174 | int i; | |
175 | ||
176 | for (i = 0; i < conf->copies; i++) { | |
177 | struct bio **bio = & r10_bio->devs[i].bio; | |
0eb3ff12 | 178 | if (*bio && *bio != IO_BLOCKED) |
1da177e4 LT |
179 | bio_put(*bio); |
180 | *bio = NULL; | |
181 | } | |
182 | } | |
183 | ||
858119e1 | 184 | static void free_r10bio(r10bio_t *r10_bio) |
1da177e4 | 185 | { |
1da177e4 LT |
186 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
187 | ||
188 | /* | |
189 | * Wake up any possible resync thread that waits for the device | |
190 | * to go idle. | |
191 | */ | |
0a27ec96 | 192 | allow_barrier(conf); |
1da177e4 LT |
193 | |
194 | put_all_bios(conf, r10_bio); | |
195 | mempool_free(r10_bio, conf->r10bio_pool); | |
196 | } | |
197 | ||
858119e1 | 198 | static void put_buf(r10bio_t *r10_bio) |
1da177e4 LT |
199 | { |
200 | conf_t *conf = mddev_to_conf(r10_bio->mddev); | |
1da177e4 LT |
201 | |
202 | mempool_free(r10_bio, conf->r10buf_pool); | |
203 | ||
0a27ec96 | 204 | lower_barrier(conf); |
1da177e4 LT |
205 | } |
206 | ||
207 | static void reschedule_retry(r10bio_t *r10_bio) | |
208 | { | |
209 | unsigned long flags; | |
210 | mddev_t *mddev = r10_bio->mddev; | |
211 | conf_t *conf = mddev_to_conf(mddev); | |
212 | ||
213 | spin_lock_irqsave(&conf->device_lock, flags); | |
214 | list_add(&r10_bio->retry_list, &conf->retry_list); | |
4443ae10 | 215 | conf->nr_queued ++; |
1da177e4 LT |
216 | spin_unlock_irqrestore(&conf->device_lock, flags); |
217 | ||
218 | md_wakeup_thread(mddev->thread); | |
219 | } | |
220 | ||
221 | /* | |
222 | * raid_end_bio_io() is called when we have finished servicing a mirrored | |
223 | * operation and are ready to return a success/failure code to the buffer | |
224 | * cache layer. | |
225 | */ | |
226 | static void raid_end_bio_io(r10bio_t *r10_bio) | |
227 | { | |
228 | struct bio *bio = r10_bio->master_bio; | |
229 | ||
6712ecf8 | 230 | bio_endio(bio, |
1da177e4 LT |
231 | test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO); |
232 | free_r10bio(r10_bio); | |
233 | } | |
234 | ||
235 | /* | |
236 | * Update disk head position estimator based on IRQ completion info. | |
237 | */ | |
238 | static inline void update_head_pos(int slot, r10bio_t *r10_bio) | |
239 | { | |
240 | conf_t *conf = mddev_to_conf(r10_bio->mddev); | |
241 | ||
242 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = | |
243 | r10_bio->devs[slot].addr + (r10_bio->sectors); | |
244 | } | |
245 | ||
6712ecf8 | 246 | static void raid10_end_read_request(struct bio *bio, int error) |
1da177e4 LT |
247 | { |
248 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
249 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); | |
250 | int slot, dev; | |
251 | conf_t *conf = mddev_to_conf(r10_bio->mddev); | |
252 | ||
1da177e4 LT |
253 | |
254 | slot = r10_bio->read_slot; | |
255 | dev = r10_bio->devs[slot].devnum; | |
256 | /* | |
257 | * this branch is our 'one mirror IO has finished' event handler: | |
258 | */ | |
4443ae10 N |
259 | update_head_pos(slot, r10_bio); |
260 | ||
261 | if (uptodate) { | |
1da177e4 LT |
262 | /* |
263 | * Set R10BIO_Uptodate in our master bio, so that | |
264 | * we will return a good error code to the higher | |
265 | * levels even if IO on some other mirrored buffer fails. | |
266 | * | |
267 | * The 'master' represents the composite IO operation to | |
268 | * user-side. So if something waits for IO, then it will | |
269 | * wait for the 'master' bio. | |
270 | */ | |
271 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
1da177e4 | 272 | raid_end_bio_io(r10_bio); |
4443ae10 | 273 | } else { |
1da177e4 LT |
274 | /* |
275 | * oops, read error: | |
276 | */ | |
277 | char b[BDEVNAME_SIZE]; | |
278 | if (printk_ratelimit()) | |
279 | printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n", | |
280 | bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector); | |
281 | reschedule_retry(r10_bio); | |
282 | } | |
283 | ||
284 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); | |
1da177e4 LT |
285 | } |
286 | ||
6712ecf8 | 287 | static void raid10_end_write_request(struct bio *bio, int error) |
1da177e4 LT |
288 | { |
289 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
290 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); | |
291 | int slot, dev; | |
292 | conf_t *conf = mddev_to_conf(r10_bio->mddev); | |
293 | ||
1da177e4 LT |
294 | for (slot = 0; slot < conf->copies; slot++) |
295 | if (r10_bio->devs[slot].bio == bio) | |
296 | break; | |
297 | dev = r10_bio->devs[slot].devnum; | |
298 | ||
299 | /* | |
300 | * this branch is our 'one mirror IO has finished' event handler: | |
301 | */ | |
6cce3b23 | 302 | if (!uptodate) { |
1da177e4 | 303 | md_error(r10_bio->mddev, conf->mirrors[dev].rdev); |
6cce3b23 N |
304 | /* an I/O failed, we can't clear the bitmap */ |
305 | set_bit(R10BIO_Degraded, &r10_bio->state); | |
306 | } else | |
1da177e4 LT |
307 | /* |
308 | * Set R10BIO_Uptodate in our master bio, so that | |
309 | * we will return a good error code for to the higher | |
310 | * levels even if IO on some other mirrored buffer fails. | |
311 | * | |
312 | * The 'master' represents the composite IO operation to | |
313 | * user-side. So if something waits for IO, then it will | |
314 | * wait for the 'master' bio. | |
315 | */ | |
316 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
317 | ||
318 | update_head_pos(slot, r10_bio); | |
319 | ||
320 | /* | |
321 | * | |
322 | * Let's see if all mirrored write operations have finished | |
323 | * already. | |
324 | */ | |
325 | if (atomic_dec_and_test(&r10_bio->remaining)) { | |
6cce3b23 N |
326 | /* clear the bitmap if all writes complete successfully */ |
327 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, | |
328 | r10_bio->sectors, | |
329 | !test_bit(R10BIO_Degraded, &r10_bio->state), | |
330 | 0); | |
1da177e4 LT |
331 | md_write_end(r10_bio->mddev); |
332 | raid_end_bio_io(r10_bio); | |
333 | } | |
334 | ||
335 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); | |
1da177e4 LT |
336 | } |
337 | ||
338 | ||
339 | /* | |
340 | * RAID10 layout manager | |
341 | * Aswell as the chunksize and raid_disks count, there are two | |
342 | * parameters: near_copies and far_copies. | |
343 | * near_copies * far_copies must be <= raid_disks. | |
344 | * Normally one of these will be 1. | |
345 | * If both are 1, we get raid0. | |
346 | * If near_copies == raid_disks, we get raid1. | |
347 | * | |
348 | * Chunks are layed out in raid0 style with near_copies copies of the | |
349 | * first chunk, followed by near_copies copies of the next chunk and | |
350 | * so on. | |
351 | * If far_copies > 1, then after 1/far_copies of the array has been assigned | |
352 | * as described above, we start again with a device offset of near_copies. | |
353 | * So we effectively have another copy of the whole array further down all | |
354 | * the drives, but with blocks on different drives. | |
355 | * With this layout, and block is never stored twice on the one device. | |
356 | * | |
357 | * raid10_find_phys finds the sector offset of a given virtual sector | |
c93983bf | 358 | * on each device that it is on. |
1da177e4 LT |
359 | * |
360 | * raid10_find_virt does the reverse mapping, from a device and a | |
361 | * sector offset to a virtual address | |
362 | */ | |
363 | ||
364 | static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio) | |
365 | { | |
366 | int n,f; | |
367 | sector_t sector; | |
368 | sector_t chunk; | |
369 | sector_t stripe; | |
370 | int dev; | |
371 | ||
372 | int slot = 0; | |
373 | ||
374 | /* now calculate first sector/dev */ | |
375 | chunk = r10bio->sector >> conf->chunk_shift; | |
376 | sector = r10bio->sector & conf->chunk_mask; | |
377 | ||
378 | chunk *= conf->near_copies; | |
379 | stripe = chunk; | |
380 | dev = sector_div(stripe, conf->raid_disks); | |
c93983bf N |
381 | if (conf->far_offset) |
382 | stripe *= conf->far_copies; | |
1da177e4 LT |
383 | |
384 | sector += stripe << conf->chunk_shift; | |
385 | ||
386 | /* and calculate all the others */ | |
387 | for (n=0; n < conf->near_copies; n++) { | |
388 | int d = dev; | |
389 | sector_t s = sector; | |
390 | r10bio->devs[slot].addr = sector; | |
391 | r10bio->devs[slot].devnum = d; | |
392 | slot++; | |
393 | ||
394 | for (f = 1; f < conf->far_copies; f++) { | |
395 | d += conf->near_copies; | |
396 | if (d >= conf->raid_disks) | |
397 | d -= conf->raid_disks; | |
398 | s += conf->stride; | |
399 | r10bio->devs[slot].devnum = d; | |
400 | r10bio->devs[slot].addr = s; | |
401 | slot++; | |
402 | } | |
403 | dev++; | |
404 | if (dev >= conf->raid_disks) { | |
405 | dev = 0; | |
406 | sector += (conf->chunk_mask + 1); | |
407 | } | |
408 | } | |
409 | BUG_ON(slot != conf->copies); | |
410 | } | |
411 | ||
412 | static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev) | |
413 | { | |
414 | sector_t offset, chunk, vchunk; | |
415 | ||
1da177e4 | 416 | offset = sector & conf->chunk_mask; |
c93983bf N |
417 | if (conf->far_offset) { |
418 | int fc; | |
419 | chunk = sector >> conf->chunk_shift; | |
420 | fc = sector_div(chunk, conf->far_copies); | |
421 | dev -= fc * conf->near_copies; | |
422 | if (dev < 0) | |
423 | dev += conf->raid_disks; | |
424 | } else { | |
64a742bc | 425 | while (sector >= conf->stride) { |
c93983bf N |
426 | sector -= conf->stride; |
427 | if (dev < conf->near_copies) | |
428 | dev += conf->raid_disks - conf->near_copies; | |
429 | else | |
430 | dev -= conf->near_copies; | |
431 | } | |
432 | chunk = sector >> conf->chunk_shift; | |
433 | } | |
1da177e4 LT |
434 | vchunk = chunk * conf->raid_disks + dev; |
435 | sector_div(vchunk, conf->near_copies); | |
436 | return (vchunk << conf->chunk_shift) + offset; | |
437 | } | |
438 | ||
439 | /** | |
440 | * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged | |
441 | * @q: request queue | |
442 | * @bio: the buffer head that's been built up so far | |
443 | * @biovec: the request that could be merged to it. | |
444 | * | |
445 | * Return amount of bytes we can accept at this offset | |
446 | * If near_copies == raid_disk, there are no striping issues, | |
447 | * but in that case, the function isn't called at all. | |
448 | */ | |
165125e1 | 449 | static int raid10_mergeable_bvec(struct request_queue *q, struct bio *bio, |
1da177e4 LT |
450 | struct bio_vec *bio_vec) |
451 | { | |
452 | mddev_t *mddev = q->queuedata; | |
453 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); | |
454 | int max; | |
455 | unsigned int chunk_sectors = mddev->chunk_size >> 9; | |
456 | unsigned int bio_sectors = bio->bi_size >> 9; | |
457 | ||
458 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; | |
459 | if (max < 0) max = 0; /* bio_add cannot handle a negative return */ | |
460 | if (max <= bio_vec->bv_len && bio_sectors == 0) | |
461 | return bio_vec->bv_len; | |
462 | else | |
463 | return max; | |
464 | } | |
465 | ||
466 | /* | |
467 | * This routine returns the disk from which the requested read should | |
468 | * be done. There is a per-array 'next expected sequential IO' sector | |
469 | * number - if this matches on the next IO then we use the last disk. | |
470 | * There is also a per-disk 'last know head position' sector that is | |
471 | * maintained from IRQ contexts, both the normal and the resync IO | |
472 | * completion handlers update this position correctly. If there is no | |
473 | * perfect sequential match then we pick the disk whose head is closest. | |
474 | * | |
475 | * If there are 2 mirrors in the same 2 devices, performance degrades | |
476 | * because position is mirror, not device based. | |
477 | * | |
478 | * The rdev for the device selected will have nr_pending incremented. | |
479 | */ | |
480 | ||
481 | /* | |
482 | * FIXME: possibly should rethink readbalancing and do it differently | |
483 | * depending on near_copies / far_copies geometry. | |
484 | */ | |
485 | static int read_balance(conf_t *conf, r10bio_t *r10_bio) | |
486 | { | |
487 | const unsigned long this_sector = r10_bio->sector; | |
488 | int disk, slot, nslot; | |
489 | const int sectors = r10_bio->sectors; | |
490 | sector_t new_distance, current_distance; | |
d6065f7b | 491 | mdk_rdev_t *rdev; |
1da177e4 LT |
492 | |
493 | raid10_find_phys(conf, r10_bio); | |
494 | rcu_read_lock(); | |
495 | /* | |
496 | * Check if we can balance. We can balance on the whole | |
6cce3b23 N |
497 | * device if no resync is going on (recovery is ok), or below |
498 | * the resync window. We take the first readable disk when | |
499 | * above the resync window. | |
1da177e4 LT |
500 | */ |
501 | if (conf->mddev->recovery_cp < MaxSector | |
502 | && (this_sector + sectors >= conf->next_resync)) { | |
503 | /* make sure that disk is operational */ | |
504 | slot = 0; | |
505 | disk = r10_bio->devs[slot].devnum; | |
506 | ||
d6065f7b | 507 | while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL || |
0eb3ff12 | 508 | r10_bio->devs[slot].bio == IO_BLOCKED || |
b2d444d7 | 509 | !test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
510 | slot++; |
511 | if (slot == conf->copies) { | |
512 | slot = 0; | |
513 | disk = -1; | |
514 | break; | |
515 | } | |
516 | disk = r10_bio->devs[slot].devnum; | |
517 | } | |
518 | goto rb_out; | |
519 | } | |
520 | ||
521 | ||
522 | /* make sure the disk is operational */ | |
523 | slot = 0; | |
524 | disk = r10_bio->devs[slot].devnum; | |
d6065f7b | 525 | while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL || |
0eb3ff12 | 526 | r10_bio->devs[slot].bio == IO_BLOCKED || |
b2d444d7 | 527 | !test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
528 | slot ++; |
529 | if (slot == conf->copies) { | |
530 | disk = -1; | |
531 | goto rb_out; | |
532 | } | |
533 | disk = r10_bio->devs[slot].devnum; | |
534 | } | |
535 | ||
536 | ||
3ec67ac1 N |
537 | current_distance = abs(r10_bio->devs[slot].addr - |
538 | conf->mirrors[disk].head_position); | |
1da177e4 | 539 | |
8ed3a195 KS |
540 | /* Find the disk whose head is closest, |
541 | * or - for far > 1 - find the closest to partition beginning */ | |
1da177e4 LT |
542 | |
543 | for (nslot = slot; nslot < conf->copies; nslot++) { | |
544 | int ndisk = r10_bio->devs[nslot].devnum; | |
545 | ||
546 | ||
d6065f7b | 547 | if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL || |
0eb3ff12 | 548 | r10_bio->devs[nslot].bio == IO_BLOCKED || |
b2d444d7 | 549 | !test_bit(In_sync, &rdev->flags)) |
1da177e4 LT |
550 | continue; |
551 | ||
22dfdf52 N |
552 | /* This optimisation is debatable, and completely destroys |
553 | * sequential read speed for 'far copies' arrays. So only | |
554 | * keep it for 'near' arrays, and review those later. | |
555 | */ | |
556 | if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) { | |
1da177e4 LT |
557 | disk = ndisk; |
558 | slot = nslot; | |
559 | break; | |
560 | } | |
8ed3a195 KS |
561 | |
562 | /* for far > 1 always use the lowest address */ | |
563 | if (conf->far_copies > 1) | |
564 | new_distance = r10_bio->devs[nslot].addr; | |
565 | else | |
566 | new_distance = abs(r10_bio->devs[nslot].addr - | |
567 | conf->mirrors[ndisk].head_position); | |
1da177e4 LT |
568 | if (new_distance < current_distance) { |
569 | current_distance = new_distance; | |
570 | disk = ndisk; | |
571 | slot = nslot; | |
572 | } | |
573 | } | |
574 | ||
575 | rb_out: | |
576 | r10_bio->read_slot = slot; | |
577 | /* conf->next_seq_sect = this_sector + sectors;*/ | |
578 | ||
d6065f7b | 579 | if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL) |
1da177e4 | 580 | atomic_inc(&conf->mirrors[disk].rdev->nr_pending); |
29fc7e3e N |
581 | else |
582 | disk = -1; | |
1da177e4 LT |
583 | rcu_read_unlock(); |
584 | ||
585 | return disk; | |
586 | } | |
587 | ||
588 | static void unplug_slaves(mddev_t *mddev) | |
589 | { | |
590 | conf_t *conf = mddev_to_conf(mddev); | |
591 | int i; | |
592 | ||
593 | rcu_read_lock(); | |
594 | for (i=0; i<mddev->raid_disks; i++) { | |
d6065f7b | 595 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
b2d444d7 | 596 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
165125e1 | 597 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
1da177e4 LT |
598 | |
599 | atomic_inc(&rdev->nr_pending); | |
600 | rcu_read_unlock(); | |
601 | ||
2ad8b1ef | 602 | blk_unplug(r_queue); |
1da177e4 LT |
603 | |
604 | rdev_dec_pending(rdev, mddev); | |
605 | rcu_read_lock(); | |
606 | } | |
607 | } | |
608 | rcu_read_unlock(); | |
609 | } | |
610 | ||
165125e1 | 611 | static void raid10_unplug(struct request_queue *q) |
1da177e4 | 612 | { |
6cce3b23 N |
613 | mddev_t *mddev = q->queuedata; |
614 | ||
1da177e4 | 615 | unplug_slaves(q->queuedata); |
6cce3b23 | 616 | md_wakeup_thread(mddev->thread); |
1da177e4 LT |
617 | } |
618 | ||
0d129228 N |
619 | static int raid10_congested(void *data, int bits) |
620 | { | |
621 | mddev_t *mddev = data; | |
622 | conf_t *conf = mddev_to_conf(mddev); | |
623 | int i, ret = 0; | |
624 | ||
625 | rcu_read_lock(); | |
626 | for (i = 0; i < mddev->raid_disks && ret == 0; i++) { | |
627 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); | |
628 | if (rdev && !test_bit(Faulty, &rdev->flags)) { | |
165125e1 | 629 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
0d129228 N |
630 | |
631 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
632 | } | |
633 | } | |
634 | rcu_read_unlock(); | |
635 | return ret; | |
636 | } | |
637 | ||
a35e63ef N |
638 | static int flush_pending_writes(conf_t *conf) |
639 | { | |
640 | /* Any writes that have been queued but are awaiting | |
641 | * bitmap updates get flushed here. | |
642 | * We return 1 if any requests were actually submitted. | |
643 | */ | |
644 | int rv = 0; | |
645 | ||
646 | spin_lock_irq(&conf->device_lock); | |
647 | ||
648 | if (conf->pending_bio_list.head) { | |
649 | struct bio *bio; | |
650 | bio = bio_list_get(&conf->pending_bio_list); | |
651 | blk_remove_plug(conf->mddev->queue); | |
652 | spin_unlock_irq(&conf->device_lock); | |
653 | /* flush any pending bitmap writes to disk | |
654 | * before proceeding w/ I/O */ | |
655 | bitmap_unplug(conf->mddev->bitmap); | |
656 | ||
657 | while (bio) { /* submit pending writes */ | |
658 | struct bio *next = bio->bi_next; | |
659 | bio->bi_next = NULL; | |
660 | generic_make_request(bio); | |
661 | bio = next; | |
662 | } | |
663 | rv = 1; | |
664 | } else | |
665 | spin_unlock_irq(&conf->device_lock); | |
666 | return rv; | |
667 | } | |
0a27ec96 N |
668 | /* Barriers.... |
669 | * Sometimes we need to suspend IO while we do something else, | |
670 | * either some resync/recovery, or reconfigure the array. | |
671 | * To do this we raise a 'barrier'. | |
672 | * The 'barrier' is a counter that can be raised multiple times | |
673 | * to count how many activities are happening which preclude | |
674 | * normal IO. | |
675 | * We can only raise the barrier if there is no pending IO. | |
676 | * i.e. if nr_pending == 0. | |
677 | * We choose only to raise the barrier if no-one is waiting for the | |
678 | * barrier to go down. This means that as soon as an IO request | |
679 | * is ready, no other operations which require a barrier will start | |
680 | * until the IO request has had a chance. | |
681 | * | |
682 | * So: regular IO calls 'wait_barrier'. When that returns there | |
683 | * is no backgroup IO happening, It must arrange to call | |
684 | * allow_barrier when it has finished its IO. | |
685 | * backgroup IO calls must call raise_barrier. Once that returns | |
686 | * there is no normal IO happeing. It must arrange to call | |
687 | * lower_barrier when the particular background IO completes. | |
1da177e4 LT |
688 | */ |
689 | #define RESYNC_DEPTH 32 | |
690 | ||
6cce3b23 | 691 | static void raise_barrier(conf_t *conf, int force) |
1da177e4 | 692 | { |
6cce3b23 | 693 | BUG_ON(force && !conf->barrier); |
1da177e4 | 694 | spin_lock_irq(&conf->resync_lock); |
0a27ec96 | 695 | |
6cce3b23 N |
696 | /* Wait until no block IO is waiting (unless 'force') */ |
697 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, | |
0a27ec96 N |
698 | conf->resync_lock, |
699 | raid10_unplug(conf->mddev->queue)); | |
700 | ||
701 | /* block any new IO from starting */ | |
702 | conf->barrier++; | |
703 | ||
704 | /* No wait for all pending IO to complete */ | |
705 | wait_event_lock_irq(conf->wait_barrier, | |
706 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, | |
707 | conf->resync_lock, | |
708 | raid10_unplug(conf->mddev->queue)); | |
709 | ||
710 | spin_unlock_irq(&conf->resync_lock); | |
711 | } | |
712 | ||
713 | static void lower_barrier(conf_t *conf) | |
714 | { | |
715 | unsigned long flags; | |
716 | spin_lock_irqsave(&conf->resync_lock, flags); | |
717 | conf->barrier--; | |
718 | spin_unlock_irqrestore(&conf->resync_lock, flags); | |
719 | wake_up(&conf->wait_barrier); | |
720 | } | |
721 | ||
722 | static void wait_barrier(conf_t *conf) | |
723 | { | |
724 | spin_lock_irq(&conf->resync_lock); | |
725 | if (conf->barrier) { | |
726 | conf->nr_waiting++; | |
727 | wait_event_lock_irq(conf->wait_barrier, !conf->barrier, | |
728 | conf->resync_lock, | |
729 | raid10_unplug(conf->mddev->queue)); | |
730 | conf->nr_waiting--; | |
1da177e4 | 731 | } |
0a27ec96 | 732 | conf->nr_pending++; |
1da177e4 LT |
733 | spin_unlock_irq(&conf->resync_lock); |
734 | } | |
735 | ||
0a27ec96 N |
736 | static void allow_barrier(conf_t *conf) |
737 | { | |
738 | unsigned long flags; | |
739 | spin_lock_irqsave(&conf->resync_lock, flags); | |
740 | conf->nr_pending--; | |
741 | spin_unlock_irqrestore(&conf->resync_lock, flags); | |
742 | wake_up(&conf->wait_barrier); | |
743 | } | |
744 | ||
4443ae10 N |
745 | static void freeze_array(conf_t *conf) |
746 | { | |
747 | /* stop syncio and normal IO and wait for everything to | |
f188593e | 748 | * go quiet. |
4443ae10 | 749 | * We increment barrier and nr_waiting, and then |
1c830532 N |
750 | * wait until nr_pending match nr_queued+1 |
751 | * This is called in the context of one normal IO request | |
752 | * that has failed. Thus any sync request that might be pending | |
753 | * will be blocked by nr_pending, and we need to wait for | |
754 | * pending IO requests to complete or be queued for re-try. | |
755 | * Thus the number queued (nr_queued) plus this request (1) | |
756 | * must match the number of pending IOs (nr_pending) before | |
757 | * we continue. | |
4443ae10 N |
758 | */ |
759 | spin_lock_irq(&conf->resync_lock); | |
760 | conf->barrier++; | |
761 | conf->nr_waiting++; | |
762 | wait_event_lock_irq(conf->wait_barrier, | |
1c830532 | 763 | conf->nr_pending == conf->nr_queued+1, |
4443ae10 | 764 | conf->resync_lock, |
a35e63ef N |
765 | ({ flush_pending_writes(conf); |
766 | raid10_unplug(conf->mddev->queue); })); | |
4443ae10 N |
767 | spin_unlock_irq(&conf->resync_lock); |
768 | } | |
769 | ||
770 | static void unfreeze_array(conf_t *conf) | |
771 | { | |
772 | /* reverse the effect of the freeze */ | |
773 | spin_lock_irq(&conf->resync_lock); | |
774 | conf->barrier--; | |
775 | conf->nr_waiting--; | |
776 | wake_up(&conf->wait_barrier); | |
777 | spin_unlock_irq(&conf->resync_lock); | |
778 | } | |
779 | ||
165125e1 | 780 | static int make_request(struct request_queue *q, struct bio * bio) |
1da177e4 LT |
781 | { |
782 | mddev_t *mddev = q->queuedata; | |
783 | conf_t *conf = mddev_to_conf(mddev); | |
784 | mirror_info_t *mirror; | |
785 | r10bio_t *r10_bio; | |
786 | struct bio *read_bio; | |
787 | int i; | |
788 | int chunk_sects = conf->chunk_mask + 1; | |
a362357b | 789 | const int rw = bio_data_dir(bio); |
e3881a68 | 790 | const int do_sync = bio_sync(bio); |
6cce3b23 N |
791 | struct bio_list bl; |
792 | unsigned long flags; | |
6bfe0b49 | 793 | mdk_rdev_t *blocked_rdev; |
1da177e4 | 794 | |
e5dcdd80 | 795 | if (unlikely(bio_barrier(bio))) { |
6712ecf8 | 796 | bio_endio(bio, -EOPNOTSUPP); |
e5dcdd80 N |
797 | return 0; |
798 | } | |
799 | ||
1da177e4 LT |
800 | /* If this request crosses a chunk boundary, we need to |
801 | * split it. This will only happen for 1 PAGE (or less) requests. | |
802 | */ | |
803 | if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9) | |
804 | > chunk_sects && | |
805 | conf->near_copies < conf->raid_disks)) { | |
806 | struct bio_pair *bp; | |
807 | /* Sanity check -- queue functions should prevent this happening */ | |
808 | if (bio->bi_vcnt != 1 || | |
809 | bio->bi_idx != 0) | |
810 | goto bad_map; | |
811 | /* This is a one page bio that upper layers | |
812 | * refuse to split for us, so we need to split it. | |
813 | */ | |
814 | bp = bio_split(bio, bio_split_pool, | |
815 | chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); | |
816 | if (make_request(q, &bp->bio1)) | |
817 | generic_make_request(&bp->bio1); | |
818 | if (make_request(q, &bp->bio2)) | |
819 | generic_make_request(&bp->bio2); | |
820 | ||
821 | bio_pair_release(bp); | |
822 | return 0; | |
823 | bad_map: | |
824 | printk("raid10_make_request bug: can't convert block across chunks" | |
825 | " or bigger than %dk %llu %d\n", chunk_sects/2, | |
826 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); | |
827 | ||
6712ecf8 | 828 | bio_io_error(bio); |
1da177e4 LT |
829 | return 0; |
830 | } | |
831 | ||
3d310eb7 | 832 | md_write_start(mddev, bio); |
06d91a5f | 833 | |
1da177e4 LT |
834 | /* |
835 | * Register the new request and wait if the reconstruction | |
836 | * thread has put up a bar for new requests. | |
837 | * Continue immediately if no resync is active currently. | |
838 | */ | |
0a27ec96 | 839 | wait_barrier(conf); |
1da177e4 | 840 | |
a362357b JA |
841 | disk_stat_inc(mddev->gendisk, ios[rw]); |
842 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio)); | |
1da177e4 LT |
843 | |
844 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); | |
845 | ||
846 | r10_bio->master_bio = bio; | |
847 | r10_bio->sectors = bio->bi_size >> 9; | |
848 | ||
849 | r10_bio->mddev = mddev; | |
850 | r10_bio->sector = bio->bi_sector; | |
6cce3b23 | 851 | r10_bio->state = 0; |
1da177e4 | 852 | |
a362357b | 853 | if (rw == READ) { |
1da177e4 LT |
854 | /* |
855 | * read balancing logic: | |
856 | */ | |
857 | int disk = read_balance(conf, r10_bio); | |
858 | int slot = r10_bio->read_slot; | |
859 | if (disk < 0) { | |
860 | raid_end_bio_io(r10_bio); | |
861 | return 0; | |
862 | } | |
863 | mirror = conf->mirrors + disk; | |
864 | ||
865 | read_bio = bio_clone(bio, GFP_NOIO); | |
866 | ||
867 | r10_bio->devs[slot].bio = read_bio; | |
868 | ||
869 | read_bio->bi_sector = r10_bio->devs[slot].addr + | |
870 | mirror->rdev->data_offset; | |
871 | read_bio->bi_bdev = mirror->rdev->bdev; | |
872 | read_bio->bi_end_io = raid10_end_read_request; | |
e3881a68 | 873 | read_bio->bi_rw = READ | do_sync; |
1da177e4 LT |
874 | read_bio->bi_private = r10_bio; |
875 | ||
876 | generic_make_request(read_bio); | |
877 | return 0; | |
878 | } | |
879 | ||
880 | /* | |
881 | * WRITE: | |
882 | */ | |
6bfe0b49 | 883 | /* first select target devices under rcu_lock and |
1da177e4 LT |
884 | * inc refcount on their rdev. Record them by setting |
885 | * bios[x] to bio | |
886 | */ | |
887 | raid10_find_phys(conf, r10_bio); | |
6bfe0b49 | 888 | retry_write: |
cb6969e8 | 889 | blocked_rdev = NULL; |
1da177e4 LT |
890 | rcu_read_lock(); |
891 | for (i = 0; i < conf->copies; i++) { | |
892 | int d = r10_bio->devs[i].devnum; | |
d6065f7b | 893 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev); |
6bfe0b49 DW |
894 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
895 | atomic_inc(&rdev->nr_pending); | |
896 | blocked_rdev = rdev; | |
897 | break; | |
898 | } | |
899 | if (rdev && !test_bit(Faulty, &rdev->flags)) { | |
d6065f7b | 900 | atomic_inc(&rdev->nr_pending); |
1da177e4 | 901 | r10_bio->devs[i].bio = bio; |
6cce3b23 | 902 | } else { |
1da177e4 | 903 | r10_bio->devs[i].bio = NULL; |
6cce3b23 N |
904 | set_bit(R10BIO_Degraded, &r10_bio->state); |
905 | } | |
1da177e4 LT |
906 | } |
907 | rcu_read_unlock(); | |
908 | ||
6bfe0b49 DW |
909 | if (unlikely(blocked_rdev)) { |
910 | /* Have to wait for this device to get unblocked, then retry */ | |
911 | int j; | |
912 | int d; | |
913 | ||
914 | for (j = 0; j < i; j++) | |
915 | if (r10_bio->devs[j].bio) { | |
916 | d = r10_bio->devs[j].devnum; | |
917 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); | |
918 | } | |
919 | allow_barrier(conf); | |
920 | md_wait_for_blocked_rdev(blocked_rdev, mddev); | |
921 | wait_barrier(conf); | |
922 | goto retry_write; | |
923 | } | |
924 | ||
6cce3b23 | 925 | atomic_set(&r10_bio->remaining, 0); |
06d91a5f | 926 | |
6cce3b23 | 927 | bio_list_init(&bl); |
1da177e4 LT |
928 | for (i = 0; i < conf->copies; i++) { |
929 | struct bio *mbio; | |
930 | int d = r10_bio->devs[i].devnum; | |
931 | if (!r10_bio->devs[i].bio) | |
932 | continue; | |
933 | ||
934 | mbio = bio_clone(bio, GFP_NOIO); | |
935 | r10_bio->devs[i].bio = mbio; | |
936 | ||
937 | mbio->bi_sector = r10_bio->devs[i].addr+ | |
938 | conf->mirrors[d].rdev->data_offset; | |
939 | mbio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
940 | mbio->bi_end_io = raid10_end_write_request; | |
e3881a68 | 941 | mbio->bi_rw = WRITE | do_sync; |
1da177e4 LT |
942 | mbio->bi_private = r10_bio; |
943 | ||
944 | atomic_inc(&r10_bio->remaining); | |
6cce3b23 | 945 | bio_list_add(&bl, mbio); |
1da177e4 LT |
946 | } |
947 | ||
f6f953aa AR |
948 | if (unlikely(!atomic_read(&r10_bio->remaining))) { |
949 | /* the array is dead */ | |
950 | md_write_end(mddev); | |
951 | raid_end_bio_io(r10_bio); | |
952 | return 0; | |
953 | } | |
954 | ||
6cce3b23 N |
955 | bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0); |
956 | spin_lock_irqsave(&conf->device_lock, flags); | |
957 | bio_list_merge(&conf->pending_bio_list, &bl); | |
958 | blk_plug_device(mddev->queue); | |
959 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 | 960 | |
a35e63ef N |
961 | /* In case raid10d snuck in to freeze_array */ |
962 | wake_up(&conf->wait_barrier); | |
963 | ||
e3881a68 LE |
964 | if (do_sync) |
965 | md_wakeup_thread(mddev->thread); | |
966 | ||
1da177e4 LT |
967 | return 0; |
968 | } | |
969 | ||
970 | static void status(struct seq_file *seq, mddev_t *mddev) | |
971 | { | |
972 | conf_t *conf = mddev_to_conf(mddev); | |
973 | int i; | |
974 | ||
975 | if (conf->near_copies < conf->raid_disks) | |
976 | seq_printf(seq, " %dK chunks", mddev->chunk_size/1024); | |
977 | if (conf->near_copies > 1) | |
978 | seq_printf(seq, " %d near-copies", conf->near_copies); | |
c93983bf N |
979 | if (conf->far_copies > 1) { |
980 | if (conf->far_offset) | |
981 | seq_printf(seq, " %d offset-copies", conf->far_copies); | |
982 | else | |
983 | seq_printf(seq, " %d far-copies", conf->far_copies); | |
984 | } | |
1da177e4 | 985 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
76186dd8 | 986 | conf->raid_disks - mddev->degraded); |
1da177e4 LT |
987 | for (i = 0; i < conf->raid_disks; i++) |
988 | seq_printf(seq, "%s", | |
989 | conf->mirrors[i].rdev && | |
b2d444d7 | 990 | test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); |
1da177e4 LT |
991 | seq_printf(seq, "]"); |
992 | } | |
993 | ||
994 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) | |
995 | { | |
996 | char b[BDEVNAME_SIZE]; | |
997 | conf_t *conf = mddev_to_conf(mddev); | |
998 | ||
999 | /* | |
1000 | * If it is not operational, then we have already marked it as dead | |
1001 | * else if it is the last working disks, ignore the error, let the | |
1002 | * next level up know. | |
1003 | * else mark the drive as failed | |
1004 | */ | |
b2d444d7 | 1005 | if (test_bit(In_sync, &rdev->flags) |
76186dd8 | 1006 | && conf->raid_disks-mddev->degraded == 1) |
1da177e4 LT |
1007 | /* |
1008 | * Don't fail the drive, just return an IO error. | |
1009 | * The test should really be more sophisticated than | |
1010 | * "working_disks == 1", but it isn't critical, and | |
1011 | * can wait until we do more sophisticated "is the drive | |
1012 | * really dead" tests... | |
1013 | */ | |
1014 | return; | |
c04be0aa N |
1015 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
1016 | unsigned long flags; | |
1017 | spin_lock_irqsave(&conf->device_lock, flags); | |
1da177e4 | 1018 | mddev->degraded++; |
c04be0aa | 1019 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 LT |
1020 | /* |
1021 | * if recovery is running, make sure it aborts. | |
1022 | */ | |
dfc70645 | 1023 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
1da177e4 | 1024 | } |
b2d444d7 | 1025 | set_bit(Faulty, &rdev->flags); |
850b2b42 | 1026 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
d7a420c9 NA |
1027 | printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n" |
1028 | "raid10: Operation continuing on %d devices.\n", | |
76186dd8 | 1029 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
1da177e4 LT |
1030 | } |
1031 | ||
1032 | static void print_conf(conf_t *conf) | |
1033 | { | |
1034 | int i; | |
1035 | mirror_info_t *tmp; | |
1036 | ||
1037 | printk("RAID10 conf printout:\n"); | |
1038 | if (!conf) { | |
1039 | printk("(!conf)\n"); | |
1040 | return; | |
1041 | } | |
76186dd8 | 1042 | printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
1da177e4 LT |
1043 | conf->raid_disks); |
1044 | ||
1045 | for (i = 0; i < conf->raid_disks; i++) { | |
1046 | char b[BDEVNAME_SIZE]; | |
1047 | tmp = conf->mirrors + i; | |
1048 | if (tmp->rdev) | |
1049 | printk(" disk %d, wo:%d, o:%d, dev:%s\n", | |
b2d444d7 N |
1050 | i, !test_bit(In_sync, &tmp->rdev->flags), |
1051 | !test_bit(Faulty, &tmp->rdev->flags), | |
1da177e4 LT |
1052 | bdevname(tmp->rdev->bdev,b)); |
1053 | } | |
1054 | } | |
1055 | ||
1056 | static void close_sync(conf_t *conf) | |
1057 | { | |
0a27ec96 N |
1058 | wait_barrier(conf); |
1059 | allow_barrier(conf); | |
1da177e4 LT |
1060 | |
1061 | mempool_destroy(conf->r10buf_pool); | |
1062 | conf->r10buf_pool = NULL; | |
1063 | } | |
1064 | ||
6d508242 N |
1065 | /* check if there are enough drives for |
1066 | * every block to appear on atleast one | |
1067 | */ | |
1068 | static int enough(conf_t *conf) | |
1069 | { | |
1070 | int first = 0; | |
1071 | ||
1072 | do { | |
1073 | int n = conf->copies; | |
1074 | int cnt = 0; | |
1075 | while (n--) { | |
1076 | if (conf->mirrors[first].rdev) | |
1077 | cnt++; | |
1078 | first = (first+1) % conf->raid_disks; | |
1079 | } | |
1080 | if (cnt == 0) | |
1081 | return 0; | |
1082 | } while (first != 0); | |
1083 | return 1; | |
1084 | } | |
1085 | ||
1da177e4 LT |
1086 | static int raid10_spare_active(mddev_t *mddev) |
1087 | { | |
1088 | int i; | |
1089 | conf_t *conf = mddev->private; | |
1090 | mirror_info_t *tmp; | |
1091 | ||
1092 | /* | |
1093 | * Find all non-in_sync disks within the RAID10 configuration | |
1094 | * and mark them in_sync | |
1095 | */ | |
1096 | for (i = 0; i < conf->raid_disks; i++) { | |
1097 | tmp = conf->mirrors + i; | |
1098 | if (tmp->rdev | |
b2d444d7 | 1099 | && !test_bit(Faulty, &tmp->rdev->flags) |
c04be0aa N |
1100 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
1101 | unsigned long flags; | |
1102 | spin_lock_irqsave(&conf->device_lock, flags); | |
1da177e4 | 1103 | mddev->degraded--; |
c04be0aa | 1104 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 LT |
1105 | } |
1106 | } | |
1107 | ||
1108 | print_conf(conf); | |
1109 | return 0; | |
1110 | } | |
1111 | ||
1112 | ||
1113 | static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) | |
1114 | { | |
1115 | conf_t *conf = mddev->private; | |
1116 | int found = 0; | |
1117 | int mirror; | |
1118 | mirror_info_t *p; | |
1119 | ||
1120 | if (mddev->recovery_cp < MaxSector) | |
1121 | /* only hot-add to in-sync arrays, as recovery is | |
1122 | * very different from resync | |
1123 | */ | |
1124 | return 0; | |
6d508242 N |
1125 | if (!enough(conf)) |
1126 | return 0; | |
1da177e4 | 1127 | |
6cce3b23 N |
1128 | if (rdev->saved_raid_disk >= 0 && |
1129 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) | |
1130 | mirror = rdev->saved_raid_disk; | |
1131 | else | |
1132 | mirror = 0; | |
1133 | for ( ; mirror < mddev->raid_disks; mirror++) | |
1da177e4 LT |
1134 | if ( !(p=conf->mirrors+mirror)->rdev) { |
1135 | ||
1136 | blk_queue_stack_limits(mddev->queue, | |
1137 | rdev->bdev->bd_disk->queue); | |
1138 | /* as we don't honour merge_bvec_fn, we must never risk | |
1139 | * violating it, so limit ->max_sector to one PAGE, as | |
1140 | * a one page request is never in violation. | |
1141 | */ | |
1142 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && | |
1143 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) | |
1144 | mddev->queue->max_sectors = (PAGE_SIZE>>9); | |
1145 | ||
1146 | p->head_position = 0; | |
1147 | rdev->raid_disk = mirror; | |
1148 | found = 1; | |
6cce3b23 N |
1149 | if (rdev->saved_raid_disk != mirror) |
1150 | conf->fullsync = 1; | |
d6065f7b | 1151 | rcu_assign_pointer(p->rdev, rdev); |
1da177e4 LT |
1152 | break; |
1153 | } | |
1154 | ||
1155 | print_conf(conf); | |
1156 | return found; | |
1157 | } | |
1158 | ||
1159 | static int raid10_remove_disk(mddev_t *mddev, int number) | |
1160 | { | |
1161 | conf_t *conf = mddev->private; | |
1162 | int err = 0; | |
1163 | mdk_rdev_t *rdev; | |
1164 | mirror_info_t *p = conf->mirrors+ number; | |
1165 | ||
1166 | print_conf(conf); | |
1167 | rdev = p->rdev; | |
1168 | if (rdev) { | |
b2d444d7 | 1169 | if (test_bit(In_sync, &rdev->flags) || |
1da177e4 LT |
1170 | atomic_read(&rdev->nr_pending)) { |
1171 | err = -EBUSY; | |
1172 | goto abort; | |
1173 | } | |
dfc70645 N |
1174 | /* Only remove faulty devices in recovery |
1175 | * is not possible. | |
1176 | */ | |
1177 | if (!test_bit(Faulty, &rdev->flags) && | |
1178 | enough(conf)) { | |
1179 | err = -EBUSY; | |
1180 | goto abort; | |
1181 | } | |
1da177e4 | 1182 | p->rdev = NULL; |
fbd568a3 | 1183 | synchronize_rcu(); |
1da177e4 LT |
1184 | if (atomic_read(&rdev->nr_pending)) { |
1185 | /* lost the race, try later */ | |
1186 | err = -EBUSY; | |
1187 | p->rdev = rdev; | |
1188 | } | |
1189 | } | |
1190 | abort: | |
1191 | ||
1192 | print_conf(conf); | |
1193 | return err; | |
1194 | } | |
1195 | ||
1196 | ||
6712ecf8 | 1197 | static void end_sync_read(struct bio *bio, int error) |
1da177e4 | 1198 | { |
1da177e4 LT |
1199 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); |
1200 | conf_t *conf = mddev_to_conf(r10_bio->mddev); | |
1201 | int i,d; | |
1202 | ||
1da177e4 LT |
1203 | for (i=0; i<conf->copies; i++) |
1204 | if (r10_bio->devs[i].bio == bio) | |
1205 | break; | |
b6385483 | 1206 | BUG_ON(i == conf->copies); |
1da177e4 LT |
1207 | update_head_pos(i, r10_bio); |
1208 | d = r10_bio->devs[i].devnum; | |
0eb3ff12 N |
1209 | |
1210 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
1211 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
4dbcdc75 N |
1212 | else { |
1213 | atomic_add(r10_bio->sectors, | |
1214 | &conf->mirrors[d].rdev->corrected_errors); | |
1215 | if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) | |
1216 | md_error(r10_bio->mddev, | |
1217 | conf->mirrors[d].rdev); | |
1218 | } | |
1da177e4 LT |
1219 | |
1220 | /* for reconstruct, we always reschedule after a read. | |
1221 | * for resync, only after all reads | |
1222 | */ | |
1223 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || | |
1224 | atomic_dec_and_test(&r10_bio->remaining)) { | |
1225 | /* we have read all the blocks, | |
1226 | * do the comparison in process context in raid10d | |
1227 | */ | |
1228 | reschedule_retry(r10_bio); | |
1229 | } | |
1230 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); | |
1da177e4 LT |
1231 | } |
1232 | ||
6712ecf8 | 1233 | static void end_sync_write(struct bio *bio, int error) |
1da177e4 LT |
1234 | { |
1235 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
1236 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); | |
1237 | mddev_t *mddev = r10_bio->mddev; | |
1238 | conf_t *conf = mddev_to_conf(mddev); | |
1239 | int i,d; | |
1240 | ||
1da177e4 LT |
1241 | for (i = 0; i < conf->copies; i++) |
1242 | if (r10_bio->devs[i].bio == bio) | |
1243 | break; | |
1244 | d = r10_bio->devs[i].devnum; | |
1245 | ||
1246 | if (!uptodate) | |
1247 | md_error(mddev, conf->mirrors[d].rdev); | |
dfc70645 | 1248 | |
1da177e4 LT |
1249 | update_head_pos(i, r10_bio); |
1250 | ||
1251 | while (atomic_dec_and_test(&r10_bio->remaining)) { | |
1252 | if (r10_bio->master_bio == NULL) { | |
1253 | /* the primary of several recovery bios */ | |
1254 | md_done_sync(mddev, r10_bio->sectors, 1); | |
1255 | put_buf(r10_bio); | |
1256 | break; | |
1257 | } else { | |
1258 | r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio; | |
1259 | put_buf(r10_bio); | |
1260 | r10_bio = r10_bio2; | |
1261 | } | |
1262 | } | |
1263 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); | |
1da177e4 LT |
1264 | } |
1265 | ||
1266 | /* | |
1267 | * Note: sync and recover and handled very differently for raid10 | |
1268 | * This code is for resync. | |
1269 | * For resync, we read through virtual addresses and read all blocks. | |
1270 | * If there is any error, we schedule a write. The lowest numbered | |
1271 | * drive is authoritative. | |
1272 | * However requests come for physical address, so we need to map. | |
1273 | * For every physical address there are raid_disks/copies virtual addresses, | |
1274 | * which is always are least one, but is not necessarly an integer. | |
1275 | * This means that a physical address can span multiple chunks, so we may | |
1276 | * have to submit multiple io requests for a single sync request. | |
1277 | */ | |
1278 | /* | |
1279 | * We check if all blocks are in-sync and only write to blocks that | |
1280 | * aren't in sync | |
1281 | */ | |
1282 | static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio) | |
1283 | { | |
1284 | conf_t *conf = mddev_to_conf(mddev); | |
1285 | int i, first; | |
1286 | struct bio *tbio, *fbio; | |
1287 | ||
1288 | atomic_set(&r10_bio->remaining, 1); | |
1289 | ||
1290 | /* find the first device with a block */ | |
1291 | for (i=0; i<conf->copies; i++) | |
1292 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) | |
1293 | break; | |
1294 | ||
1295 | if (i == conf->copies) | |
1296 | goto done; | |
1297 | ||
1298 | first = i; | |
1299 | fbio = r10_bio->devs[i].bio; | |
1300 | ||
1301 | /* now find blocks with errors */ | |
0eb3ff12 N |
1302 | for (i=0 ; i < conf->copies ; i++) { |
1303 | int j, d; | |
1304 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); | |
1da177e4 | 1305 | |
1da177e4 | 1306 | tbio = r10_bio->devs[i].bio; |
0eb3ff12 N |
1307 | |
1308 | if (tbio->bi_end_io != end_sync_read) | |
1309 | continue; | |
1310 | if (i == first) | |
1da177e4 | 1311 | continue; |
0eb3ff12 N |
1312 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { |
1313 | /* We know that the bi_io_vec layout is the same for | |
1314 | * both 'first' and 'i', so we just compare them. | |
1315 | * All vec entries are PAGE_SIZE; | |
1316 | */ | |
1317 | for (j = 0; j < vcnt; j++) | |
1318 | if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), | |
1319 | page_address(tbio->bi_io_vec[j].bv_page), | |
1320 | PAGE_SIZE)) | |
1321 | break; | |
1322 | if (j == vcnt) | |
1323 | continue; | |
1324 | mddev->resync_mismatches += r10_bio->sectors; | |
1325 | } | |
18f08819 N |
1326 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
1327 | /* Don't fix anything. */ | |
1328 | continue; | |
1da177e4 LT |
1329 | /* Ok, we need to write this bio |
1330 | * First we need to fixup bv_offset, bv_len and | |
1331 | * bi_vecs, as the read request might have corrupted these | |
1332 | */ | |
1333 | tbio->bi_vcnt = vcnt; | |
1334 | tbio->bi_size = r10_bio->sectors << 9; | |
1335 | tbio->bi_idx = 0; | |
1336 | tbio->bi_phys_segments = 0; | |
1337 | tbio->bi_hw_segments = 0; | |
1338 | tbio->bi_hw_front_size = 0; | |
1339 | tbio->bi_hw_back_size = 0; | |
1340 | tbio->bi_flags &= ~(BIO_POOL_MASK - 1); | |
1341 | tbio->bi_flags |= 1 << BIO_UPTODATE; | |
1342 | tbio->bi_next = NULL; | |
1343 | tbio->bi_rw = WRITE; | |
1344 | tbio->bi_private = r10_bio; | |
1345 | tbio->bi_sector = r10_bio->devs[i].addr; | |
1346 | ||
1347 | for (j=0; j < vcnt ; j++) { | |
1348 | tbio->bi_io_vec[j].bv_offset = 0; | |
1349 | tbio->bi_io_vec[j].bv_len = PAGE_SIZE; | |
1350 | ||
1351 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), | |
1352 | page_address(fbio->bi_io_vec[j].bv_page), | |
1353 | PAGE_SIZE); | |
1354 | } | |
1355 | tbio->bi_end_io = end_sync_write; | |
1356 | ||
1357 | d = r10_bio->devs[i].devnum; | |
1358 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
1359 | atomic_inc(&r10_bio->remaining); | |
1360 | md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); | |
1361 | ||
1362 | tbio->bi_sector += conf->mirrors[d].rdev->data_offset; | |
1363 | tbio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
1364 | generic_make_request(tbio); | |
1365 | } | |
1366 | ||
1367 | done: | |
1368 | if (atomic_dec_and_test(&r10_bio->remaining)) { | |
1369 | md_done_sync(mddev, r10_bio->sectors, 1); | |
1370 | put_buf(r10_bio); | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | /* | |
1375 | * Now for the recovery code. | |
1376 | * Recovery happens across physical sectors. | |
1377 | * We recover all non-is_sync drives by finding the virtual address of | |
1378 | * each, and then choose a working drive that also has that virt address. | |
1379 | * There is a separate r10_bio for each non-in_sync drive. | |
1380 | * Only the first two slots are in use. The first for reading, | |
1381 | * The second for writing. | |
1382 | * | |
1383 | */ | |
1384 | ||
1385 | static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio) | |
1386 | { | |
1387 | conf_t *conf = mddev_to_conf(mddev); | |
1388 | int i, d; | |
1389 | struct bio *bio, *wbio; | |
1390 | ||
1391 | ||
1392 | /* move the pages across to the second bio | |
1393 | * and submit the write request | |
1394 | */ | |
1395 | bio = r10_bio->devs[0].bio; | |
1396 | wbio = r10_bio->devs[1].bio; | |
1397 | for (i=0; i < wbio->bi_vcnt; i++) { | |
1398 | struct page *p = bio->bi_io_vec[i].bv_page; | |
1399 | bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page; | |
1400 | wbio->bi_io_vec[i].bv_page = p; | |
1401 | } | |
1402 | d = r10_bio->devs[1].devnum; | |
1403 | ||
1404 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
1405 | md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); | |
0eb3ff12 N |
1406 | if (test_bit(R10BIO_Uptodate, &r10_bio->state)) |
1407 | generic_make_request(wbio); | |
1408 | else | |
6712ecf8 | 1409 | bio_endio(wbio, -EIO); |
1da177e4 LT |
1410 | } |
1411 | ||
1412 | ||
1413 | /* | |
1414 | * This is a kernel thread which: | |
1415 | * | |
1416 | * 1. Retries failed read operations on working mirrors. | |
1417 | * 2. Updates the raid superblock when problems encounter. | |
6814d536 | 1418 | * 3. Performs writes following reads for array synchronising. |
1da177e4 LT |
1419 | */ |
1420 | ||
6814d536 N |
1421 | static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio) |
1422 | { | |
1423 | int sect = 0; /* Offset from r10_bio->sector */ | |
1424 | int sectors = r10_bio->sectors; | |
1425 | mdk_rdev_t*rdev; | |
1426 | while(sectors) { | |
1427 | int s = sectors; | |
1428 | int sl = r10_bio->read_slot; | |
1429 | int success = 0; | |
1430 | int start; | |
1431 | ||
1432 | if (s > (PAGE_SIZE>>9)) | |
1433 | s = PAGE_SIZE >> 9; | |
1434 | ||
1435 | rcu_read_lock(); | |
1436 | do { | |
1437 | int d = r10_bio->devs[sl].devnum; | |
1438 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1439 | if (rdev && | |
1440 | test_bit(In_sync, &rdev->flags)) { | |
1441 | atomic_inc(&rdev->nr_pending); | |
1442 | rcu_read_unlock(); | |
1443 | success = sync_page_io(rdev->bdev, | |
1444 | r10_bio->devs[sl].addr + | |
1445 | sect + rdev->data_offset, | |
1446 | s<<9, | |
1447 | conf->tmppage, READ); | |
1448 | rdev_dec_pending(rdev, mddev); | |
1449 | rcu_read_lock(); | |
1450 | if (success) | |
1451 | break; | |
1452 | } | |
1453 | sl++; | |
1454 | if (sl == conf->copies) | |
1455 | sl = 0; | |
1456 | } while (!success && sl != r10_bio->read_slot); | |
1457 | rcu_read_unlock(); | |
1458 | ||
1459 | if (!success) { | |
1460 | /* Cannot read from anywhere -- bye bye array */ | |
1461 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; | |
1462 | md_error(mddev, conf->mirrors[dn].rdev); | |
1463 | break; | |
1464 | } | |
1465 | ||
1466 | start = sl; | |
1467 | /* write it back and re-read */ | |
1468 | rcu_read_lock(); | |
1469 | while (sl != r10_bio->read_slot) { | |
1470 | int d; | |
1471 | if (sl==0) | |
1472 | sl = conf->copies; | |
1473 | sl--; | |
1474 | d = r10_bio->devs[sl].devnum; | |
1475 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1476 | if (rdev && | |
1477 | test_bit(In_sync, &rdev->flags)) { | |
1478 | atomic_inc(&rdev->nr_pending); | |
1479 | rcu_read_unlock(); | |
1480 | atomic_add(s, &rdev->corrected_errors); | |
1481 | if (sync_page_io(rdev->bdev, | |
1482 | r10_bio->devs[sl].addr + | |
1483 | sect + rdev->data_offset, | |
1484 | s<<9, conf->tmppage, WRITE) | |
1485 | == 0) | |
1486 | /* Well, this device is dead */ | |
1487 | md_error(mddev, rdev); | |
1488 | rdev_dec_pending(rdev, mddev); | |
1489 | rcu_read_lock(); | |
1490 | } | |
1491 | } | |
1492 | sl = start; | |
1493 | while (sl != r10_bio->read_slot) { | |
1494 | int d; | |
1495 | if (sl==0) | |
1496 | sl = conf->copies; | |
1497 | sl--; | |
1498 | d = r10_bio->devs[sl].devnum; | |
1499 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1500 | if (rdev && | |
1501 | test_bit(In_sync, &rdev->flags)) { | |
1502 | char b[BDEVNAME_SIZE]; | |
1503 | atomic_inc(&rdev->nr_pending); | |
1504 | rcu_read_unlock(); | |
1505 | if (sync_page_io(rdev->bdev, | |
1506 | r10_bio->devs[sl].addr + | |
1507 | sect + rdev->data_offset, | |
1508 | s<<9, conf->tmppage, READ) == 0) | |
1509 | /* Well, this device is dead */ | |
1510 | md_error(mddev, rdev); | |
1511 | else | |
1512 | printk(KERN_INFO | |
1513 | "raid10:%s: read error corrected" | |
1514 | " (%d sectors at %llu on %s)\n", | |
1515 | mdname(mddev), s, | |
969b755a RD |
1516 | (unsigned long long)(sect+ |
1517 | rdev->data_offset), | |
6814d536 N |
1518 | bdevname(rdev->bdev, b)); |
1519 | ||
1520 | rdev_dec_pending(rdev, mddev); | |
1521 | rcu_read_lock(); | |
1522 | } | |
1523 | } | |
1524 | rcu_read_unlock(); | |
1525 | ||
1526 | sectors -= s; | |
1527 | sect += s; | |
1528 | } | |
1529 | } | |
1530 | ||
1da177e4 LT |
1531 | static void raid10d(mddev_t *mddev) |
1532 | { | |
1533 | r10bio_t *r10_bio; | |
1534 | struct bio *bio; | |
1535 | unsigned long flags; | |
1536 | conf_t *conf = mddev_to_conf(mddev); | |
1537 | struct list_head *head = &conf->retry_list; | |
1538 | int unplug=0; | |
1539 | mdk_rdev_t *rdev; | |
1540 | ||
1541 | md_check_recovery(mddev); | |
1da177e4 LT |
1542 | |
1543 | for (;;) { | |
1544 | char b[BDEVNAME_SIZE]; | |
6cce3b23 | 1545 | |
a35e63ef | 1546 | unplug += flush_pending_writes(conf); |
6cce3b23 | 1547 | |
a35e63ef N |
1548 | spin_lock_irqsave(&conf->device_lock, flags); |
1549 | if (list_empty(head)) { | |
1550 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 | 1551 | break; |
a35e63ef | 1552 | } |
1da177e4 LT |
1553 | r10_bio = list_entry(head->prev, r10bio_t, retry_list); |
1554 | list_del(head->prev); | |
4443ae10 | 1555 | conf->nr_queued--; |
1da177e4 LT |
1556 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1557 | ||
1558 | mddev = r10_bio->mddev; | |
1559 | conf = mddev_to_conf(mddev); | |
1560 | if (test_bit(R10BIO_IsSync, &r10_bio->state)) { | |
1561 | sync_request_write(mddev, r10_bio); | |
1562 | unplug = 1; | |
1563 | } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) { | |
1564 | recovery_request_write(mddev, r10_bio); | |
1565 | unplug = 1; | |
1566 | } else { | |
1567 | int mirror; | |
4443ae10 N |
1568 | /* we got a read error. Maybe the drive is bad. Maybe just |
1569 | * the block and we can fix it. | |
1570 | * We freeze all other IO, and try reading the block from | |
1571 | * other devices. When we find one, we re-write | |
1572 | * and check it that fixes the read error. | |
1573 | * This is all done synchronously while the array is | |
1574 | * frozen. | |
1575 | */ | |
6814d536 N |
1576 | if (mddev->ro == 0) { |
1577 | freeze_array(conf); | |
1578 | fix_read_error(conf, mddev, r10_bio); | |
1579 | unfreeze_array(conf); | |
4443ae10 N |
1580 | } |
1581 | ||
1da177e4 | 1582 | bio = r10_bio->devs[r10_bio->read_slot].bio; |
0eb3ff12 N |
1583 | r10_bio->devs[r10_bio->read_slot].bio = |
1584 | mddev->ro ? IO_BLOCKED : NULL; | |
1da177e4 LT |
1585 | mirror = read_balance(conf, r10_bio); |
1586 | if (mirror == -1) { | |
1587 | printk(KERN_ALERT "raid10: %s: unrecoverable I/O" | |
1588 | " read error for block %llu\n", | |
1589 | bdevname(bio->bi_bdev,b), | |
1590 | (unsigned long long)r10_bio->sector); | |
1591 | raid_end_bio_io(r10_bio); | |
14e71344 | 1592 | bio_put(bio); |
1da177e4 | 1593 | } else { |
e3881a68 | 1594 | const int do_sync = bio_sync(r10_bio->master_bio); |
14e71344 | 1595 | bio_put(bio); |
1da177e4 LT |
1596 | rdev = conf->mirrors[mirror].rdev; |
1597 | if (printk_ratelimit()) | |
1598 | printk(KERN_ERR "raid10: %s: redirecting sector %llu to" | |
1599 | " another mirror\n", | |
1600 | bdevname(rdev->bdev,b), | |
1601 | (unsigned long long)r10_bio->sector); | |
1602 | bio = bio_clone(r10_bio->master_bio, GFP_NOIO); | |
1603 | r10_bio->devs[r10_bio->read_slot].bio = bio; | |
1604 | bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr | |
1605 | + rdev->data_offset; | |
1606 | bio->bi_bdev = rdev->bdev; | |
e3881a68 | 1607 | bio->bi_rw = READ | do_sync; |
1da177e4 LT |
1608 | bio->bi_private = r10_bio; |
1609 | bio->bi_end_io = raid10_end_read_request; | |
1610 | unplug = 1; | |
1611 | generic_make_request(bio); | |
1612 | } | |
1613 | } | |
1614 | } | |
1da177e4 LT |
1615 | if (unplug) |
1616 | unplug_slaves(mddev); | |
1617 | } | |
1618 | ||
1619 | ||
1620 | static int init_resync(conf_t *conf) | |
1621 | { | |
1622 | int buffs; | |
1623 | ||
1624 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; | |
b6385483 | 1625 | BUG_ON(conf->r10buf_pool); |
1da177e4 LT |
1626 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
1627 | if (!conf->r10buf_pool) | |
1628 | return -ENOMEM; | |
1629 | conf->next_resync = 0; | |
1630 | return 0; | |
1631 | } | |
1632 | ||
1633 | /* | |
1634 | * perform a "sync" on one "block" | |
1635 | * | |
1636 | * We need to make sure that no normal I/O request - particularly write | |
1637 | * requests - conflict with active sync requests. | |
1638 | * | |
1639 | * This is achieved by tracking pending requests and a 'barrier' concept | |
1640 | * that can be installed to exclude normal IO requests. | |
1641 | * | |
1642 | * Resync and recovery are handled very differently. | |
1643 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. | |
1644 | * | |
1645 | * For resync, we iterate over virtual addresses, read all copies, | |
1646 | * and update if there are differences. If only one copy is live, | |
1647 | * skip it. | |
1648 | * For recovery, we iterate over physical addresses, read a good | |
1649 | * value for each non-in_sync drive, and over-write. | |
1650 | * | |
1651 | * So, for recovery we may have several outstanding complex requests for a | |
1652 | * given address, one for each out-of-sync device. We model this by allocating | |
1653 | * a number of r10_bio structures, one for each out-of-sync device. | |
1654 | * As we setup these structures, we collect all bio's together into a list | |
1655 | * which we then process collectively to add pages, and then process again | |
1656 | * to pass to generic_make_request. | |
1657 | * | |
1658 | * The r10_bio structures are linked using a borrowed master_bio pointer. | |
1659 | * This link is counted in ->remaining. When the r10_bio that points to NULL | |
1660 | * has its remaining count decremented to 0, the whole complex operation | |
1661 | * is complete. | |
1662 | * | |
1663 | */ | |
1664 | ||
57afd89f | 1665 | static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
1da177e4 LT |
1666 | { |
1667 | conf_t *conf = mddev_to_conf(mddev); | |
1668 | r10bio_t *r10_bio; | |
1669 | struct bio *biolist = NULL, *bio; | |
1670 | sector_t max_sector, nr_sectors; | |
1671 | int disk; | |
1672 | int i; | |
6cce3b23 N |
1673 | int max_sync; |
1674 | int sync_blocks; | |
1da177e4 LT |
1675 | |
1676 | sector_t sectors_skipped = 0; | |
1677 | int chunks_skipped = 0; | |
1678 | ||
1679 | if (!conf->r10buf_pool) | |
1680 | if (init_resync(conf)) | |
57afd89f | 1681 | return 0; |
1da177e4 LT |
1682 | |
1683 | skipped: | |
1684 | max_sector = mddev->size << 1; | |
1685 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) | |
1686 | max_sector = mddev->resync_max_sectors; | |
1687 | if (sector_nr >= max_sector) { | |
6cce3b23 N |
1688 | /* If we aborted, we need to abort the |
1689 | * sync on the 'current' bitmap chucks (there can | |
1690 | * be several when recovering multiple devices). | |
1691 | * as we may have started syncing it but not finished. | |
1692 | * We can find the current address in | |
1693 | * mddev->curr_resync, but for recovery, | |
1694 | * we need to convert that to several | |
1695 | * virtual addresses. | |
1696 | */ | |
1697 | if (mddev->curr_resync < max_sector) { /* aborted */ | |
1698 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) | |
1699 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
1700 | &sync_blocks, 1); | |
1701 | else for (i=0; i<conf->raid_disks; i++) { | |
1702 | sector_t sect = | |
1703 | raid10_find_virt(conf, mddev->curr_resync, i); | |
1704 | bitmap_end_sync(mddev->bitmap, sect, | |
1705 | &sync_blocks, 1); | |
1706 | } | |
1707 | } else /* completed sync */ | |
1708 | conf->fullsync = 0; | |
1709 | ||
1710 | bitmap_close_sync(mddev->bitmap); | |
1da177e4 | 1711 | close_sync(conf); |
57afd89f | 1712 | *skipped = 1; |
1da177e4 LT |
1713 | return sectors_skipped; |
1714 | } | |
1715 | if (chunks_skipped >= conf->raid_disks) { | |
1716 | /* if there has been nothing to do on any drive, | |
1717 | * then there is nothing to do at all.. | |
1718 | */ | |
57afd89f N |
1719 | *skipped = 1; |
1720 | return (max_sector - sector_nr) + sectors_skipped; | |
1da177e4 LT |
1721 | } |
1722 | ||
c6207277 N |
1723 | if (max_sector > mddev->resync_max) |
1724 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ | |
1725 | ||
1da177e4 LT |
1726 | /* make sure whole request will fit in a chunk - if chunks |
1727 | * are meaningful | |
1728 | */ | |
1729 | if (conf->near_copies < conf->raid_disks && | |
1730 | max_sector > (sector_nr | conf->chunk_mask)) | |
1731 | max_sector = (sector_nr | conf->chunk_mask) + 1; | |
1732 | /* | |
1733 | * If there is non-resync activity waiting for us then | |
1734 | * put in a delay to throttle resync. | |
1735 | */ | |
0a27ec96 | 1736 | if (!go_faster && conf->nr_waiting) |
1da177e4 | 1737 | msleep_interruptible(1000); |
1da177e4 | 1738 | |
b47490c9 N |
1739 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
1740 | ||
1da177e4 LT |
1741 | /* Again, very different code for resync and recovery. |
1742 | * Both must result in an r10bio with a list of bios that | |
1743 | * have bi_end_io, bi_sector, bi_bdev set, | |
1744 | * and bi_private set to the r10bio. | |
1745 | * For recovery, we may actually create several r10bios | |
1746 | * with 2 bios in each, that correspond to the bios in the main one. | |
1747 | * In this case, the subordinate r10bios link back through a | |
1748 | * borrowed master_bio pointer, and the counter in the master | |
1749 | * includes a ref from each subordinate. | |
1750 | */ | |
1751 | /* First, we decide what to do and set ->bi_end_io | |
1752 | * To end_sync_read if we want to read, and | |
1753 | * end_sync_write if we will want to write. | |
1754 | */ | |
1755 | ||
6cce3b23 | 1756 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
1da177e4 LT |
1757 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
1758 | /* recovery... the complicated one */ | |
1759 | int i, j, k; | |
1760 | r10_bio = NULL; | |
1761 | ||
1762 | for (i=0 ; i<conf->raid_disks; i++) | |
1763 | if (conf->mirrors[i].rdev && | |
b2d444d7 | 1764 | !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) { |
6cce3b23 | 1765 | int still_degraded = 0; |
1da177e4 LT |
1766 | /* want to reconstruct this device */ |
1767 | r10bio_t *rb2 = r10_bio; | |
6cce3b23 N |
1768 | sector_t sect = raid10_find_virt(conf, sector_nr, i); |
1769 | int must_sync; | |
1770 | /* Unless we are doing a full sync, we only need | |
1771 | * to recover the block if it is set in the bitmap | |
1772 | */ | |
1773 | must_sync = bitmap_start_sync(mddev->bitmap, sect, | |
1774 | &sync_blocks, 1); | |
1775 | if (sync_blocks < max_sync) | |
1776 | max_sync = sync_blocks; | |
1777 | if (!must_sync && | |
1778 | !conf->fullsync) { | |
1779 | /* yep, skip the sync_blocks here, but don't assume | |
1780 | * that there will never be anything to do here | |
1781 | */ | |
1782 | chunks_skipped = -1; | |
1783 | continue; | |
1784 | } | |
1da177e4 LT |
1785 | |
1786 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); | |
6cce3b23 | 1787 | raise_barrier(conf, rb2 != NULL); |
1da177e4 LT |
1788 | atomic_set(&r10_bio->remaining, 0); |
1789 | ||
1790 | r10_bio->master_bio = (struct bio*)rb2; | |
1791 | if (rb2) | |
1792 | atomic_inc(&rb2->remaining); | |
1793 | r10_bio->mddev = mddev; | |
1794 | set_bit(R10BIO_IsRecover, &r10_bio->state); | |
6cce3b23 N |
1795 | r10_bio->sector = sect; |
1796 | ||
1da177e4 | 1797 | raid10_find_phys(conf, r10_bio); |
6cce3b23 N |
1798 | /* Need to check if this section will still be |
1799 | * degraded | |
1800 | */ | |
1801 | for (j=0; j<conf->copies;j++) { | |
1802 | int d = r10_bio->devs[j].devnum; | |
1803 | if (conf->mirrors[d].rdev == NULL || | |
a24a8dd8 | 1804 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) { |
6cce3b23 | 1805 | still_degraded = 1; |
a24a8dd8 N |
1806 | break; |
1807 | } | |
6cce3b23 N |
1808 | } |
1809 | must_sync = bitmap_start_sync(mddev->bitmap, sect, | |
1810 | &sync_blocks, still_degraded); | |
1811 | ||
1da177e4 LT |
1812 | for (j=0; j<conf->copies;j++) { |
1813 | int d = r10_bio->devs[j].devnum; | |
1814 | if (conf->mirrors[d].rdev && | |
b2d444d7 | 1815 | test_bit(In_sync, &conf->mirrors[d].rdev->flags)) { |
1da177e4 LT |
1816 | /* This is where we read from */ |
1817 | bio = r10_bio->devs[0].bio; | |
1818 | bio->bi_next = biolist; | |
1819 | biolist = bio; | |
1820 | bio->bi_private = r10_bio; | |
1821 | bio->bi_end_io = end_sync_read; | |
802ba064 | 1822 | bio->bi_rw = READ; |
1da177e4 LT |
1823 | bio->bi_sector = r10_bio->devs[j].addr + |
1824 | conf->mirrors[d].rdev->data_offset; | |
1825 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
1826 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
1827 | atomic_inc(&r10_bio->remaining); | |
1828 | /* and we write to 'i' */ | |
1829 | ||
1830 | for (k=0; k<conf->copies; k++) | |
1831 | if (r10_bio->devs[k].devnum == i) | |
1832 | break; | |
64a742bc | 1833 | BUG_ON(k == conf->copies); |
1da177e4 LT |
1834 | bio = r10_bio->devs[1].bio; |
1835 | bio->bi_next = biolist; | |
1836 | biolist = bio; | |
1837 | bio->bi_private = r10_bio; | |
1838 | bio->bi_end_io = end_sync_write; | |
802ba064 | 1839 | bio->bi_rw = WRITE; |
1da177e4 LT |
1840 | bio->bi_sector = r10_bio->devs[k].addr + |
1841 | conf->mirrors[i].rdev->data_offset; | |
1842 | bio->bi_bdev = conf->mirrors[i].rdev->bdev; | |
1843 | ||
1844 | r10_bio->devs[0].devnum = d; | |
1845 | r10_bio->devs[1].devnum = i; | |
1846 | ||
1847 | break; | |
1848 | } | |
1849 | } | |
1850 | if (j == conf->copies) { | |
87fc767b N |
1851 | /* Cannot recover, so abort the recovery */ |
1852 | put_buf(r10_bio); | |
a07e6ab4 T |
1853 | if (rb2) |
1854 | atomic_dec(&rb2->remaining); | |
87fc767b | 1855 | r10_bio = rb2; |
dfc70645 N |
1856 | if (!test_and_set_bit(MD_RECOVERY_INTR, |
1857 | &mddev->recovery)) | |
87fc767b N |
1858 | printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n", |
1859 | mdname(mddev)); | |
1860 | break; | |
1da177e4 LT |
1861 | } |
1862 | } | |
1863 | if (biolist == NULL) { | |
1864 | while (r10_bio) { | |
1865 | r10bio_t *rb2 = r10_bio; | |
1866 | r10_bio = (r10bio_t*) rb2->master_bio; | |
1867 | rb2->master_bio = NULL; | |
1868 | put_buf(rb2); | |
1869 | } | |
1870 | goto giveup; | |
1871 | } | |
1872 | } else { | |
1873 | /* resync. Schedule a read for every block at this virt offset */ | |
1874 | int count = 0; | |
6cce3b23 N |
1875 | |
1876 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, | |
1877 | &sync_blocks, mddev->degraded) && | |
1878 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
1879 | /* We can skip this block */ | |
1880 | *skipped = 1; | |
1881 | return sync_blocks + sectors_skipped; | |
1882 | } | |
1883 | if (sync_blocks < max_sync) | |
1884 | max_sync = sync_blocks; | |
1da177e4 LT |
1885 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
1886 | ||
1da177e4 LT |
1887 | r10_bio->mddev = mddev; |
1888 | atomic_set(&r10_bio->remaining, 0); | |
6cce3b23 N |
1889 | raise_barrier(conf, 0); |
1890 | conf->next_resync = sector_nr; | |
1da177e4 LT |
1891 | |
1892 | r10_bio->master_bio = NULL; | |
1893 | r10_bio->sector = sector_nr; | |
1894 | set_bit(R10BIO_IsSync, &r10_bio->state); | |
1895 | raid10_find_phys(conf, r10_bio); | |
1896 | r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1; | |
1897 | ||
1898 | for (i=0; i<conf->copies; i++) { | |
1899 | int d = r10_bio->devs[i].devnum; | |
1900 | bio = r10_bio->devs[i].bio; | |
1901 | bio->bi_end_io = NULL; | |
af03b8e4 | 1902 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
1da177e4 | 1903 | if (conf->mirrors[d].rdev == NULL || |
b2d444d7 | 1904 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) |
1da177e4 LT |
1905 | continue; |
1906 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
1907 | atomic_inc(&r10_bio->remaining); | |
1908 | bio->bi_next = biolist; | |
1909 | biolist = bio; | |
1910 | bio->bi_private = r10_bio; | |
1911 | bio->bi_end_io = end_sync_read; | |
802ba064 | 1912 | bio->bi_rw = READ; |
1da177e4 LT |
1913 | bio->bi_sector = r10_bio->devs[i].addr + |
1914 | conf->mirrors[d].rdev->data_offset; | |
1915 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
1916 | count++; | |
1917 | } | |
1918 | ||
1919 | if (count < 2) { | |
1920 | for (i=0; i<conf->copies; i++) { | |
1921 | int d = r10_bio->devs[i].devnum; | |
1922 | if (r10_bio->devs[i].bio->bi_end_io) | |
1923 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); | |
1924 | } | |
1925 | put_buf(r10_bio); | |
1926 | biolist = NULL; | |
1927 | goto giveup; | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | for (bio = biolist; bio ; bio=bio->bi_next) { | |
1932 | ||
1933 | bio->bi_flags &= ~(BIO_POOL_MASK - 1); | |
1934 | if (bio->bi_end_io) | |
1935 | bio->bi_flags |= 1 << BIO_UPTODATE; | |
1936 | bio->bi_vcnt = 0; | |
1937 | bio->bi_idx = 0; | |
1938 | bio->bi_phys_segments = 0; | |
1939 | bio->bi_hw_segments = 0; | |
1940 | bio->bi_size = 0; | |
1941 | } | |
1942 | ||
1943 | nr_sectors = 0; | |
6cce3b23 N |
1944 | if (sector_nr + max_sync < max_sector) |
1945 | max_sector = sector_nr + max_sync; | |
1da177e4 LT |
1946 | do { |
1947 | struct page *page; | |
1948 | int len = PAGE_SIZE; | |
1949 | disk = 0; | |
1950 | if (sector_nr + (len>>9) > max_sector) | |
1951 | len = (max_sector - sector_nr) << 9; | |
1952 | if (len == 0) | |
1953 | break; | |
1954 | for (bio= biolist ; bio ; bio=bio->bi_next) { | |
1955 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; | |
1956 | if (bio_add_page(bio, page, len, 0) == 0) { | |
1957 | /* stop here */ | |
1958 | struct bio *bio2; | |
1959 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; | |
1960 | for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) { | |
1961 | /* remove last page from this bio */ | |
1962 | bio2->bi_vcnt--; | |
1963 | bio2->bi_size -= len; | |
1964 | bio2->bi_flags &= ~(1<< BIO_SEG_VALID); | |
1965 | } | |
1966 | goto bio_full; | |
1967 | } | |
1968 | disk = i; | |
1969 | } | |
1970 | nr_sectors += len>>9; | |
1971 | sector_nr += len>>9; | |
1972 | } while (biolist->bi_vcnt < RESYNC_PAGES); | |
1973 | bio_full: | |
1974 | r10_bio->sectors = nr_sectors; | |
1975 | ||
1976 | while (biolist) { | |
1977 | bio = biolist; | |
1978 | biolist = biolist->bi_next; | |
1979 | ||
1980 | bio->bi_next = NULL; | |
1981 | r10_bio = bio->bi_private; | |
1982 | r10_bio->sectors = nr_sectors; | |
1983 | ||
1984 | if (bio->bi_end_io == end_sync_read) { | |
1985 | md_sync_acct(bio->bi_bdev, nr_sectors); | |
1986 | generic_make_request(bio); | |
1987 | } | |
1988 | } | |
1989 | ||
57afd89f N |
1990 | if (sectors_skipped) |
1991 | /* pretend they weren't skipped, it makes | |
1992 | * no important difference in this case | |
1993 | */ | |
1994 | md_done_sync(mddev, sectors_skipped, 1); | |
1995 | ||
1da177e4 LT |
1996 | return sectors_skipped + nr_sectors; |
1997 | giveup: | |
1998 | /* There is nowhere to write, so all non-sync | |
1999 | * drives must be failed, so try the next chunk... | |
2000 | */ | |
2001 | { | |
57afd89f | 2002 | sector_t sec = max_sector - sector_nr; |
1da177e4 LT |
2003 | sectors_skipped += sec; |
2004 | chunks_skipped ++; | |
2005 | sector_nr = max_sector; | |
1da177e4 LT |
2006 | goto skipped; |
2007 | } | |
2008 | } | |
2009 | ||
2010 | static int run(mddev_t *mddev) | |
2011 | { | |
2012 | conf_t *conf; | |
2013 | int i, disk_idx; | |
2014 | mirror_info_t *disk; | |
2015 | mdk_rdev_t *rdev; | |
2016 | struct list_head *tmp; | |
c93983bf | 2017 | int nc, fc, fo; |
1da177e4 LT |
2018 | sector_t stride, size; |
2019 | ||
2604b703 N |
2020 | if (mddev->chunk_size == 0) { |
2021 | printk(KERN_ERR "md/raid10: non-zero chunk size required.\n"); | |
2022 | return -EINVAL; | |
1da177e4 | 2023 | } |
2604b703 | 2024 | |
1da177e4 LT |
2025 | nc = mddev->layout & 255; |
2026 | fc = (mddev->layout >> 8) & 255; | |
c93983bf | 2027 | fo = mddev->layout & (1<<16); |
1da177e4 | 2028 | if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks || |
c93983bf | 2029 | (mddev->layout >> 17)) { |
1da177e4 LT |
2030 | printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n", |
2031 | mdname(mddev), mddev->layout); | |
2032 | goto out; | |
2033 | } | |
2034 | /* | |
2035 | * copy the already verified devices into our private RAID10 | |
2036 | * bookkeeping area. [whatever we allocate in run(), | |
2037 | * should be freed in stop()] | |
2038 | */ | |
4443ae10 | 2039 | conf = kzalloc(sizeof(conf_t), GFP_KERNEL); |
1da177e4 LT |
2040 | mddev->private = conf; |
2041 | if (!conf) { | |
2042 | printk(KERN_ERR "raid10: couldn't allocate memory for %s\n", | |
2043 | mdname(mddev)); | |
2044 | goto out; | |
2045 | } | |
4443ae10 | 2046 | conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
1da177e4 LT |
2047 | GFP_KERNEL); |
2048 | if (!conf->mirrors) { | |
2049 | printk(KERN_ERR "raid10: couldn't allocate memory for %s\n", | |
2050 | mdname(mddev)); | |
2051 | goto out_free_conf; | |
2052 | } | |
4443ae10 N |
2053 | |
2054 | conf->tmppage = alloc_page(GFP_KERNEL); | |
2055 | if (!conf->tmppage) | |
2056 | goto out_free_conf; | |
1da177e4 | 2057 | |
64a742bc N |
2058 | conf->mddev = mddev; |
2059 | conf->raid_disks = mddev->raid_disks; | |
1da177e4 LT |
2060 | conf->near_copies = nc; |
2061 | conf->far_copies = fc; | |
2062 | conf->copies = nc*fc; | |
c93983bf | 2063 | conf->far_offset = fo; |
1da177e4 LT |
2064 | conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1; |
2065 | conf->chunk_shift = ffz(~mddev->chunk_size) - 9; | |
64a742bc N |
2066 | size = mddev->size >> (conf->chunk_shift-1); |
2067 | sector_div(size, fc); | |
2068 | size = size * conf->raid_disks; | |
2069 | sector_div(size, nc); | |
2070 | /* 'size' is now the number of chunks in the array */ | |
2071 | /* calculate "used chunks per device" in 'stride' */ | |
2072 | stride = size * conf->copies; | |
af03b8e4 N |
2073 | |
2074 | /* We need to round up when dividing by raid_disks to | |
2075 | * get the stride size. | |
2076 | */ | |
2077 | stride += conf->raid_disks - 1; | |
64a742bc N |
2078 | sector_div(stride, conf->raid_disks); |
2079 | mddev->size = stride << (conf->chunk_shift-1); | |
2080 | ||
c93983bf | 2081 | if (fo) |
64a742bc N |
2082 | stride = 1; |
2083 | else | |
c93983bf | 2084 | sector_div(stride, fc); |
64a742bc N |
2085 | conf->stride = stride << conf->chunk_shift; |
2086 | ||
1da177e4 LT |
2087 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, |
2088 | r10bio_pool_free, conf); | |
2089 | if (!conf->r10bio_pool) { | |
2090 | printk(KERN_ERR "raid10: couldn't allocate memory for %s\n", | |
2091 | mdname(mddev)); | |
2092 | goto out_free_conf; | |
2093 | } | |
1da177e4 | 2094 | |
e7e72bf6 NB |
2095 | spin_lock_init(&conf->device_lock); |
2096 | mddev->queue->queue_lock = &conf->device_lock; | |
2097 | ||
d089c6af | 2098 | rdev_for_each(rdev, tmp, mddev) { |
1da177e4 LT |
2099 | disk_idx = rdev->raid_disk; |
2100 | if (disk_idx >= mddev->raid_disks | |
2101 | || disk_idx < 0) | |
2102 | continue; | |
2103 | disk = conf->mirrors + disk_idx; | |
2104 | ||
2105 | disk->rdev = rdev; | |
2106 | ||
2107 | blk_queue_stack_limits(mddev->queue, | |
2108 | rdev->bdev->bd_disk->queue); | |
2109 | /* as we don't honour merge_bvec_fn, we must never risk | |
2110 | * violating it, so limit ->max_sector to one PAGE, as | |
2111 | * a one page request is never in violation. | |
2112 | */ | |
2113 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && | |
2114 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) | |
2115 | mddev->queue->max_sectors = (PAGE_SIZE>>9); | |
2116 | ||
2117 | disk->head_position = 0; | |
1da177e4 | 2118 | } |
1da177e4 LT |
2119 | INIT_LIST_HEAD(&conf->retry_list); |
2120 | ||
2121 | spin_lock_init(&conf->resync_lock); | |
0a27ec96 | 2122 | init_waitqueue_head(&conf->wait_barrier); |
1da177e4 | 2123 | |
6d508242 N |
2124 | /* need to check that every block has at least one working mirror */ |
2125 | if (!enough(conf)) { | |
2126 | printk(KERN_ERR "raid10: not enough operational mirrors for %s\n", | |
2127 | mdname(mddev)); | |
1da177e4 LT |
2128 | goto out_free_conf; |
2129 | } | |
2130 | ||
2131 | mddev->degraded = 0; | |
2132 | for (i = 0; i < conf->raid_disks; i++) { | |
2133 | ||
2134 | disk = conf->mirrors + i; | |
2135 | ||
5fd6c1dc | 2136 | if (!disk->rdev || |
2e333e89 | 2137 | !test_bit(In_sync, &disk->rdev->flags)) { |
1da177e4 LT |
2138 | disk->head_position = 0; |
2139 | mddev->degraded++; | |
2140 | } | |
2141 | } | |
2142 | ||
2143 | ||
2144 | mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10"); | |
2145 | if (!mddev->thread) { | |
2146 | printk(KERN_ERR | |
2147 | "raid10: couldn't allocate thread for %s\n", | |
2148 | mdname(mddev)); | |
2149 | goto out_free_conf; | |
2150 | } | |
2151 | ||
2152 | printk(KERN_INFO | |
2153 | "raid10: raid set %s active with %d out of %d devices\n", | |
2154 | mdname(mddev), mddev->raid_disks - mddev->degraded, | |
2155 | mddev->raid_disks); | |
2156 | /* | |
2157 | * Ok, everything is just fine now | |
2158 | */ | |
64a742bc N |
2159 | mddev->array_size = size << (conf->chunk_shift-1); |
2160 | mddev->resync_max_sectors = size << conf->chunk_shift; | |
1da177e4 | 2161 | |
7a5febe9 | 2162 | mddev->queue->unplug_fn = raid10_unplug; |
0d129228 N |
2163 | mddev->queue->backing_dev_info.congested_fn = raid10_congested; |
2164 | mddev->queue->backing_dev_info.congested_data = mddev; | |
7a5febe9 | 2165 | |
1da177e4 LT |
2166 | /* Calculate max read-ahead size. |
2167 | * We need to readahead at least twice a whole stripe.... | |
2168 | * maybe... | |
2169 | */ | |
2170 | { | |
8932c2e0 | 2171 | int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE); |
1da177e4 LT |
2172 | stripe /= conf->near_copies; |
2173 | if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) | |
2174 | mddev->queue->backing_dev_info.ra_pages = 2* stripe; | |
2175 | } | |
2176 | ||
2177 | if (conf->near_copies < mddev->raid_disks) | |
2178 | blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); | |
2179 | return 0; | |
2180 | ||
2181 | out_free_conf: | |
2182 | if (conf->r10bio_pool) | |
2183 | mempool_destroy(conf->r10bio_pool); | |
1345b1d8 | 2184 | safe_put_page(conf->tmppage); |
990a8baf | 2185 | kfree(conf->mirrors); |
1da177e4 LT |
2186 | kfree(conf); |
2187 | mddev->private = NULL; | |
2188 | out: | |
2189 | return -EIO; | |
2190 | } | |
2191 | ||
2192 | static int stop(mddev_t *mddev) | |
2193 | { | |
2194 | conf_t *conf = mddev_to_conf(mddev); | |
2195 | ||
2196 | md_unregister_thread(mddev->thread); | |
2197 | mddev->thread = NULL; | |
2198 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ | |
2199 | if (conf->r10bio_pool) | |
2200 | mempool_destroy(conf->r10bio_pool); | |
990a8baf | 2201 | kfree(conf->mirrors); |
1da177e4 LT |
2202 | kfree(conf); |
2203 | mddev->private = NULL; | |
2204 | return 0; | |
2205 | } | |
2206 | ||
6cce3b23 N |
2207 | static void raid10_quiesce(mddev_t *mddev, int state) |
2208 | { | |
2209 | conf_t *conf = mddev_to_conf(mddev); | |
2210 | ||
2211 | switch(state) { | |
2212 | case 1: | |
2213 | raise_barrier(conf, 0); | |
2214 | break; | |
2215 | case 0: | |
2216 | lower_barrier(conf); | |
2217 | break; | |
2218 | } | |
2219 | if (mddev->thread) { | |
2220 | if (mddev->bitmap) | |
2221 | mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ; | |
2222 | else | |
2223 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; | |
2224 | md_wakeup_thread(mddev->thread); | |
2225 | } | |
2226 | } | |
1da177e4 | 2227 | |
2604b703 | 2228 | static struct mdk_personality raid10_personality = |
1da177e4 LT |
2229 | { |
2230 | .name = "raid10", | |
2604b703 | 2231 | .level = 10, |
1da177e4 LT |
2232 | .owner = THIS_MODULE, |
2233 | .make_request = make_request, | |
2234 | .run = run, | |
2235 | .stop = stop, | |
2236 | .status = status, | |
2237 | .error_handler = error, | |
2238 | .hot_add_disk = raid10_add_disk, | |
2239 | .hot_remove_disk= raid10_remove_disk, | |
2240 | .spare_active = raid10_spare_active, | |
2241 | .sync_request = sync_request, | |
6cce3b23 | 2242 | .quiesce = raid10_quiesce, |
1da177e4 LT |
2243 | }; |
2244 | ||
2245 | static int __init raid_init(void) | |
2246 | { | |
2604b703 | 2247 | return register_md_personality(&raid10_personality); |
1da177e4 LT |
2248 | } |
2249 | ||
2250 | static void raid_exit(void) | |
2251 | { | |
2604b703 | 2252 | unregister_md_personality(&raid10_personality); |
1da177e4 LT |
2253 | } |
2254 | ||
2255 | module_init(raid_init); | |
2256 | module_exit(raid_exit); | |
2257 | MODULE_LICENSE("GPL"); | |
2258 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ | |
d9d166c2 | 2259 | MODULE_ALIAS("md-raid10"); |
2604b703 | 2260 | MODULE_ALIAS("md-level-10"); |