blob: fea67727f807ca1a99c2b688519f1dc22bcc5331 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080056#include <linux/nodemask.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110057#include <trace/events/block.h>
58
NeilBrown43b2e5d2009-03-31 14:33:13 +110059#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110060#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110061#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110062#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070063
Shaohua Li851c30c2013-08-28 14:30:16 +080064#define cpu_to_group(cpu) cpu_to_node(cpu)
65#define ANY_GROUP NUMA_NO_NODE
66
67static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Stripe cache
70 */
71
72#define NR_STRIPES 256
73#define STRIPE_SIZE PAGE_SIZE
74#define STRIPE_SHIFT (PAGE_SHIFT - 9)
75#define STRIPE_SECTORS (STRIPE_SIZE>>9)
76#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070077#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080078#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define HASH_MASK (NR_HASH - 1)
Shaohua Libfc90cb2013-08-29 15:40:32 +080080#define MAX_STRIPE_BATCH 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
NeilBrownd1688a62011-10-11 16:49:52 +110082static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110083{
84 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
85 return &conf->stripe_hashtbl[hash];
86}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Shaohua Li566c09c2013-11-14 15:16:17 +110088static inline int stripe_hash_locks_hash(sector_t sect)
89{
90 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
91}
92
93static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
94{
95 spin_lock_irq(conf->hash_locks + hash);
96 spin_lock(&conf->device_lock);
97}
98
99static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
100{
101 spin_unlock(&conf->device_lock);
102 spin_unlock_irq(conf->hash_locks + hash);
103}
104
105static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
106{
107 int i;
108 local_irq_disable();
109 spin_lock(conf->hash_locks);
110 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
111 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
112 spin_lock(&conf->device_lock);
113}
114
115static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
116{
117 int i;
118 spin_unlock(&conf->device_lock);
119 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
120 spin_unlock(conf->hash_locks + i - 1);
121 local_irq_enable();
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* bio's attached to a stripe+device for I/O are linked together in bi_sector
125 * order without overlap. There may be several bio's per stripe+device, and
126 * a bio could span several devices.
127 * When walking this list for a particular stripe+device, we must never proceed
128 * beyond a bio that extends past this device, as the next bio might no longer
129 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +1100130 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * of the current stripe+device
132 */
NeilBrowndb298e12011-10-07 14:23:00 +1100133static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
134{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800135 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +1100136 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
137 return bio->bi_next;
138 else
139 return NULL;
140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jens Axboe960e7392008-08-15 10:41:18 +0200142/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200143 * We maintain a biased count of active stripes in the bottom 16 bits of
144 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200145 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000146static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200147{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000148 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
149 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200150}
151
Shaohua Lie7836bd62012-07-19 16:01:31 +1000152static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200153{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000154 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
155 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200156}
157
Shaohua Lie7836bd62012-07-19 16:01:31 +1000158static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200159{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000160 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
161 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200162}
163
Shaohua Lie7836bd62012-07-19 16:01:31 +1000164static inline void raid5_set_bi_processed_stripes(struct bio *bio,
165 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200166{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000167 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
168 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200169
Shaohua Lie7836bd62012-07-19 16:01:31 +1000170 do {
171 old = atomic_read(segments);
172 new = (old & 0xffff) | (cnt << 16);
173 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200174}
175
Shaohua Lie7836bd62012-07-19 16:01:31 +1000176static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200177{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000178 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
179 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200180}
181
NeilBrownd0dabf72009-03-31 14:39:38 +1100182/* Find first data disk in a raid6 stripe */
183static inline int raid6_d0(struct stripe_head *sh)
184{
NeilBrown67cc2b82009-03-31 14:39:38 +1100185 if (sh->ddf_layout)
186 /* ddf always start from first device */
187 return 0;
188 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100189 if (sh->qd_idx == sh->disks - 1)
190 return 0;
191 else
192 return sh->qd_idx + 1;
193}
NeilBrown16a53ec2006-06-26 00:27:38 -0700194static inline int raid6_next_disk(int disk, int raid_disks)
195{
196 disk++;
197 return (disk < raid_disks) ? disk : 0;
198}
Dan Williamsa4456852007-07-09 11:56:43 -0700199
NeilBrownd0dabf72009-03-31 14:39:38 +1100200/* When walking through the disks in a raid5, starting at raid6_d0,
201 * We need to map each disk to a 'slot', where the data disks are slot
202 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
203 * is raid_disks-1. This help does that mapping.
204 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100205static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
206 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100207{
Dan Williams66295422009-10-19 18:09:32 -0700208 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100209
NeilBrowne4424fe2009-10-16 16:27:34 +1100210 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700211 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100212 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100213 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100214 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100215 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100216 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700217 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100218 return slot;
219}
220
Dan Williamsa4456852007-07-09 11:56:43 -0700221static void return_io(struct bio *return_bi)
222{
223 struct bio *bi = return_bi;
224 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700225
226 return_bi = bi->bi_next;
227 bi->bi_next = NULL;
228 bi->bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700229 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
230 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000231 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700232 bi = return_bi;
233 }
234}
235
NeilBrownd1688a62011-10-11 16:49:52 +1100236static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Dan Williams600aa102008-06-28 08:32:05 +1000238static int stripe_operations_active(struct stripe_head *sh)
239{
240 return sh->check_state || sh->reconstruct_state ||
241 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
242 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
243}
244
Shaohua Li851c30c2013-08-28 14:30:16 +0800245static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
246{
247 struct r5conf *conf = sh->raid_conf;
248 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800249 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800250 int i, cpu = sh->cpu;
251
252 if (!cpu_online(cpu)) {
253 cpu = cpumask_any(cpu_online_mask);
254 sh->cpu = cpu;
255 }
256
257 if (list_empty(&sh->lru)) {
258 struct r5worker_group *group;
259 group = conf->worker_groups + cpu_to_group(cpu);
260 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800261 group->stripes_cnt++;
262 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800263 }
264
265 if (conf->worker_cnt_per_group == 0) {
266 md_wakeup_thread(conf->mddev->thread);
267 return;
268 }
269
270 group = conf->worker_groups + cpu_to_group(sh->cpu);
271
Shaohua Libfc90cb2013-08-29 15:40:32 +0800272 group->workers[0].working = true;
273 /* at least one worker should run to avoid race */
274 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
275
276 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
277 /* wakeup more workers */
278 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
279 if (group->workers[i].working == false) {
280 group->workers[i].working = true;
281 queue_work_on(sh->cpu, raid5_wq,
282 &group->workers[i].work);
283 thread_cnt--;
284 }
285 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800286}
287
Shaohua Li566c09c2013-11-14 15:16:17 +1100288static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
289 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000291 BUG_ON(!list_empty(&sh->lru));
292 BUG_ON(atomic_read(&conf->active_stripes)==0);
293 if (test_bit(STRIPE_HANDLE, &sh->state)) {
294 if (test_bit(STRIPE_DELAYED, &sh->state) &&
295 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
296 list_add_tail(&sh->lru, &conf->delayed_list);
297 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
298 sh->bm_seq - conf->seq_write > 0)
299 list_add_tail(&sh->lru, &conf->bitmap_list);
300 else {
301 clear_bit(STRIPE_DELAYED, &sh->state);
302 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800303 if (conf->worker_cnt_per_group == 0) {
304 list_add_tail(&sh->lru, &conf->handle_list);
305 } else {
306 raid5_wakeup_stripe_thread(sh);
307 return;
308 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000309 }
310 md_wakeup_thread(conf->mddev->thread);
311 } else {
312 BUG_ON(stripe_operations_active(sh));
313 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
314 if (atomic_dec_return(&conf->preread_active_stripes)
315 < IO_THRESHOLD)
316 md_wakeup_thread(conf->mddev->thread);
317 atomic_dec(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100318 if (!test_bit(STRIPE_EXPANDING, &sh->state))
319 list_add_tail(&sh->lru, temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321}
NeilBrownd0dabf72009-03-31 14:39:38 +1100322
Shaohua Li566c09c2013-11-14 15:16:17 +1100323static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
324 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000325{
326 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100327 do_release_stripe(conf, sh, temp_inactive_list);
328}
329
330/*
331 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
332 *
333 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
334 * given time. Adding stripes only takes device lock, while deleting stripes
335 * only takes hash lock.
336 */
337static void release_inactive_stripe_list(struct r5conf *conf,
338 struct list_head *temp_inactive_list,
339 int hash)
340{
341 int size;
342 bool do_wakeup = false;
343 unsigned long flags;
344
345 if (hash == NR_STRIPE_HASH_LOCKS) {
346 size = NR_STRIPE_HASH_LOCKS;
347 hash = NR_STRIPE_HASH_LOCKS - 1;
348 } else
349 size = 1;
350 while (size) {
351 struct list_head *list = &temp_inactive_list[size - 1];
352
353 /*
354 * We don't hold any lock here yet, get_active_stripe() might
355 * remove stripes from the list
356 */
357 if (!list_empty_careful(list)) {
358 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100359 if (list_empty(conf->inactive_list + hash) &&
360 !list_empty(list))
361 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100362 list_splice_tail_init(list, conf->inactive_list + hash);
363 do_wakeup = true;
364 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
365 }
366 size--;
367 hash--;
368 }
369
370 if (do_wakeup) {
371 wake_up(&conf->wait_for_stripe);
372 if (conf->retry_read_aligned)
373 md_wakeup_thread(conf->mddev->thread);
374 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000375}
376
Shaohua Lid265d9d2013-08-28 14:29:05 +0800377static struct llist_node *llist_reverse_order(struct llist_node *head)
378{
379 struct llist_node *new_head = NULL;
380
381 while (head) {
382 struct llist_node *tmp = head;
383 head = head->next;
384 tmp->next = new_head;
385 new_head = tmp;
386 }
387
388 return new_head;
389}
390
Shaohua Li773ca822013-08-27 17:50:39 +0800391/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100392static int release_stripe_list(struct r5conf *conf,
393 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800394{
395 struct stripe_head *sh;
396 int count = 0;
397 struct llist_node *head;
398
399 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800400 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800401 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100402 int hash;
403
Shaohua Li773ca822013-08-27 17:50:39 +0800404 sh = llist_entry(head, struct stripe_head, release_list);
405 head = llist_next(head);
406 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
407 smp_mb();
408 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
409 /*
410 * Don't worry the bit is set here, because if the bit is set
411 * again, the count is always > 1. This is true for
412 * STRIPE_ON_UNPLUG_LIST bit too.
413 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100414 hash = sh->hash_lock_index;
415 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800416 count++;
417 }
418
419 return count;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static void release_stripe(struct stripe_head *sh)
423{
NeilBrownd1688a62011-10-11 16:49:52 +1100424 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100426 struct list_head list;
427 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800428 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700429
majianpengad4068d2013-11-14 15:16:15 +1100430 if (unlikely(!conf->mddev->thread) ||
431 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800432 goto slow_path;
433 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
434 if (wakeup)
435 md_wakeup_thread(conf->mddev->thread);
436 return;
437slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000438 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800439 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000440 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100441 INIT_LIST_HEAD(&list);
442 hash = sh->hash_lock_index;
443 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000444 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100445 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000446 }
447 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
NeilBrownfccddba2006-01-06 00:20:33 -0800450static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Dan Williams45b42332007-07-09 11:56:43 -0700452 pr_debug("remove_hash(), stripe %llu\n",
453 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
NeilBrownfccddba2006-01-06 00:20:33 -0800455 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
NeilBrownd1688a62011-10-11 16:49:52 +1100458static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
NeilBrownfccddba2006-01-06 00:20:33 -0800460 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dan Williams45b42332007-07-09 11:56:43 -0700462 pr_debug("insert_hash(), stripe %llu\n",
463 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
NeilBrownfccddba2006-01-06 00:20:33 -0800465 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468
469/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100470static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct stripe_head *sh = NULL;
473 struct list_head *first;
474
Shaohua Li566c09c2013-11-14 15:16:17 +1100475 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100477 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sh = list_entry(first, struct stripe_head, lru);
479 list_del_init(first);
480 remove_hash(sh);
481 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100482 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100483 if (list_empty(conf->inactive_list + hash))
484 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485out:
486 return sh;
487}
488
NeilBrowne4e11e32010-06-16 16:45:16 +1000489static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct page *p;
492 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000493 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrowne4e11e32010-06-16 16:45:16 +1000495 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 p = sh->dev[i].page;
497 if (!p)
498 continue;
499 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800500 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502}
503
NeilBrowne4e11e32010-06-16 16:45:16 +1000504static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000507 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
NeilBrowne4e11e32010-06-16 16:45:16 +1000509 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct page *page;
511
512 if (!(page = alloc_page(GFP_KERNEL))) {
513 return 1;
514 }
515 sh->dev[i].page = page;
516 }
517 return 0;
518}
519
NeilBrown784052e2009-03-31 15:19:07 +1100520static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100521static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100522 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
NeilBrownb5663ba2009-03-31 14:39:38 +1100524static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
NeilBrownd1688a62011-10-11 16:49:52 +1100526 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100527 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200529 BUG_ON(atomic_read(&sh->count) != 0);
530 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000531 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700532
Dan Williams45b42332007-07-09 11:56:43 -0700533 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 (unsigned long long)sh->sector);
535
536 remove_hash(sh);
Shaohua Li566c09c2013-11-14 15:16:17 +1100537retry:
538 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100539 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100540 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100542 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sh->state = 0;
544
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800545
546 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct r5dev *dev = &sh->dev[i];
548
Dan Williamsd84e0f12007-01-02 13:52:30 -0700549 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700551 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700553 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000555 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100558 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100560 if (read_seqcount_retry(&conf->gen_lock, seq))
561 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800563 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
NeilBrownd1688a62011-10-11 16:49:52 +1100566static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100567 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct stripe_head *sh;
570
Dan Williams45b42332007-07-09 11:56:43 -0700571 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800572 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100573 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700575 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NULL;
577}
578
NeilBrown674806d2010-06-16 17:17:53 +1000579/*
580 * Need to check if array has failed when deciding whether to:
581 * - start an array
582 * - remove non-faulty devices
583 * - add a spare
584 * - allow a reshape
585 * This determination is simple when no reshape is happening.
586 * However if there is a reshape, we need to carefully check
587 * both the before and after sections.
588 * This is because some failed devices may only affect one
589 * of the two sections, and some non-in_sync devices may
590 * be insync in the section most affected by failed devices.
591 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100592static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000593{
NeilBrown908f4fb2011-12-23 10:17:50 +1100594 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000595 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000596
597 rcu_read_lock();
598 degraded = 0;
599 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100600 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000601 if (rdev && test_bit(Faulty, &rdev->flags))
602 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000603 if (!rdev || test_bit(Faulty, &rdev->flags))
604 degraded++;
605 else if (test_bit(In_sync, &rdev->flags))
606 ;
607 else
608 /* not in-sync or faulty.
609 * If the reshape increases the number of devices,
610 * this is being recovered by the reshape, so
611 * this 'previous' section is not in_sync.
612 * If the number of devices is being reduced however,
613 * the device can only be part of the array if
614 * we are reverting a reshape, so this section will
615 * be in-sync.
616 */
617 if (conf->raid_disks >= conf->previous_raid_disks)
618 degraded++;
619 }
620 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100621 if (conf->raid_disks == conf->previous_raid_disks)
622 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000623 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100624 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000625 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100626 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000627 if (rdev && test_bit(Faulty, &rdev->flags))
628 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000629 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100630 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000631 else if (test_bit(In_sync, &rdev->flags))
632 ;
633 else
634 /* not in-sync or faulty.
635 * If reshape increases the number of devices, this
636 * section has already been recovered, else it
637 * almost certainly hasn't.
638 */
639 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100640 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000641 }
642 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100643 if (degraded2 > degraded)
644 return degraded2;
645 return degraded;
646}
647
648static int has_failed(struct r5conf *conf)
649{
650 int degraded;
651
652 if (conf->mddev->reshape_position == MaxSector)
653 return conf->mddev->degraded > conf->max_degraded;
654
655 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000656 if (degraded > conf->max_degraded)
657 return 1;
658 return 0;
659}
660
NeilBrownb5663ba2009-03-31 14:39:38 +1100661static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100662get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000663 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100666 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dan Williams45b42332007-07-09 11:56:43 -0700668 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Shaohua Li566c09c2013-11-14 15:16:17 +1100670 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 do {
NeilBrown72626682005-09-09 16:23:54 -0700673 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000674 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100675 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100676 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!sh) {
678 if (!conf->inactive_blocked)
Shaohua Li566c09c2013-11-14 15:16:17 +1100679 sh = get_free_stripe(conf, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (noblock && sh == NULL)
681 break;
682 if (!sh) {
683 conf->inactive_blocked = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +1100684 wait_event_lock_irq(
685 conf->wait_for_stripe,
686 !list_empty(conf->inactive_list + hash) &&
687 (atomic_read(&conf->active_stripes)
688 < (conf->max_nr_stripes * 3 / 4)
689 || !conf->inactive_blocked),
690 *(conf->hash_locks + hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 conf->inactive_blocked = 0;
692 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100693 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else {
695 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100696 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000697 && !test_bit(STRIPE_EXPANDING, &sh->state)
Shaohua Li773ca822013-08-27 17:50:39 +0800698 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
699 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
Shaohua Li566c09c2013-11-14 15:16:17 +1100701 spin_lock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!test_bit(STRIPE_HANDLE, &sh->state))
703 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700704 if (list_empty(&sh->lru) &&
Shaohua Li566c09c2013-11-14 15:16:17 +1100705 !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state) &&
NeilBrownff4e8d92006-07-10 04:44:16 -0700706 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700707 BUG();
708 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800709 if (sh->group) {
710 sh->group->stripes_cnt--;
711 sh->group = NULL;
712 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100713 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
716 } while (sh == NULL);
717
718 if (sh)
719 atomic_inc(&sh->count);
720
Shaohua Li566c09c2013-11-14 15:16:17 +1100721 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return sh;
723}
724
NeilBrown05616be2012-05-21 09:27:00 +1000725/* Determine if 'data_offset' or 'new_data_offset' should be used
726 * in this stripe_head.
727 */
728static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
729{
730 sector_t progress = conf->reshape_progress;
731 /* Need a memory barrier to make sure we see the value
732 * of conf->generation, or ->data_offset that was set before
733 * reshape_progress was updated.
734 */
735 smp_rmb();
736 if (progress == MaxSector)
737 return 0;
738 if (sh->generation == conf->generation - 1)
739 return 0;
740 /* We are in a reshape, and this is a new-generation stripe,
741 * so use new_data_offset.
742 */
743 return 1;
744}
745
NeilBrown6712ecf2007-09-27 12:47:43 +0200746static void
747raid5_end_read_request(struct bio *bi, int error);
748static void
749raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700750
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000751static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700752{
NeilBrownd1688a62011-10-11 16:49:52 +1100753 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700754 int i, disks = sh->disks;
755
756 might_sleep();
757
758 for (i = disks; i--; ) {
759 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100760 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100761 struct bio *bi, *rbi;
762 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200763 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
764 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
765 rw = WRITE_FUA;
766 else
767 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100768 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100769 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200770 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700771 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100772 else if (test_and_clear_bit(R5_WantReplace,
773 &sh->dev[i].flags)) {
774 rw = WRITE;
775 replace_only = 1;
776 } else
Dan Williams91c00922007-01-02 13:52:30 -0700777 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000778 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
779 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700780
781 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100782 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700783
Dan Williams91c00922007-01-02 13:52:30 -0700784 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100785 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100786 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
787 rdev = rcu_dereference(conf->disks[i].rdev);
788 if (!rdev) {
789 rdev = rrdev;
790 rrdev = NULL;
791 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100792 if (rw & WRITE) {
793 if (replace_only)
794 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100795 if (rdev == rrdev)
796 /* We raced and saw duplicates */
797 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100798 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100799 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100800 rdev = rrdev;
801 rrdev = NULL;
802 }
NeilBrown977df362011-12-23 10:17:53 +1100803
Dan Williams91c00922007-01-02 13:52:30 -0700804 if (rdev && test_bit(Faulty, &rdev->flags))
805 rdev = NULL;
806 if (rdev)
807 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100808 if (rrdev && test_bit(Faulty, &rrdev->flags))
809 rrdev = NULL;
810 if (rrdev)
811 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700812 rcu_read_unlock();
813
NeilBrown73e92e52011-07-28 11:39:22 +1000814 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100815 * need to check for writes. We never accept write errors
816 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000817 */
818 while ((rw & WRITE) && rdev &&
819 test_bit(WriteErrorSeen, &rdev->flags)) {
820 sector_t first_bad;
821 int bad_sectors;
822 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
823 &first_bad, &bad_sectors);
824 if (!bad)
825 break;
826
827 if (bad < 0) {
828 set_bit(BlockedBadBlocks, &rdev->flags);
829 if (!conf->mddev->external &&
830 conf->mddev->flags) {
831 /* It is very unlikely, but we might
832 * still need to write out the
833 * bad block log - better give it
834 * a chance*/
835 md_check_recovery(conf->mddev);
836 }
majianpeng18507532012-07-03 12:11:54 +1000837 /*
838 * Because md_wait_for_blocked_rdev
839 * will dec nr_pending, we must
840 * increment it first.
841 */
842 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000843 md_wait_for_blocked_rdev(rdev, conf->mddev);
844 } else {
845 /* Acknowledged bad block - skip the write */
846 rdev_dec_pending(rdev, conf->mddev);
847 rdev = NULL;
848 }
849 }
850
Dan Williams91c00922007-01-02 13:52:30 -0700851 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100852 if (s->syncing || s->expanding || s->expanded
853 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700854 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
855
Dan Williams2b7497f2008-06-28 08:31:52 +1000856 set_bit(STRIPE_IO_STARTED, &sh->state);
857
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700858 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700859 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700860 bi->bi_rw = rw;
861 bi->bi_end_io = (rw & WRITE)
862 ? raid5_end_write_request
863 : raid5_end_read_request;
864 bi->bi_private = sh;
865
Dan Williams91c00922007-01-02 13:52:30 -0700866 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700867 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700868 bi->bi_rw, i);
869 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000870 if (use_new_offset(conf, sh))
871 bi->bi_sector = (sh->sector
872 + rdev->new_data_offset);
873 else
874 bi->bi_sector = (sh->sector
875 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000876 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
majianpenge59aa232013-11-14 15:16:19 +1100877 bi->bi_rw |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +1000878
Kent Overstreet4997b722013-05-30 08:44:39 +0200879 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700880 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
881 bi->bi_io_vec[0].bv_offset = 0;
882 bi->bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800883 /*
884 * If this is discard request, set bi_vcnt 0. We don't
885 * want to confuse SCSI because SCSI will replace payload
886 */
887 if (rw & REQ_DISCARD)
888 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100889 if (rrdev)
890 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600891
892 if (conf->mddev->gendisk)
893 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
894 bi, disk_devt(conf->mddev->gendisk),
895 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700896 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100897 }
898 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100899 if (s->syncing || s->expanding || s->expanded
900 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100901 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
902
903 set_bit(STRIPE_IO_STARTED, &sh->state);
904
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700905 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100906 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700907 rbi->bi_rw = rw;
908 BUG_ON(!(rw & WRITE));
909 rbi->bi_end_io = raid5_end_write_request;
910 rbi->bi_private = sh;
911
NeilBrown977df362011-12-23 10:17:53 +1100912 pr_debug("%s: for %llu schedule op %ld on "
913 "replacement disc %d\n",
914 __func__, (unsigned long long)sh->sector,
915 rbi->bi_rw, i);
916 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000917 if (use_new_offset(conf, sh))
918 rbi->bi_sector = (sh->sector
919 + rrdev->new_data_offset);
920 else
921 rbi->bi_sector = (sh->sector
922 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200923 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100924 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
925 rbi->bi_io_vec[0].bv_offset = 0;
926 rbi->bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800927 /*
928 * If this is discard request, set bi_vcnt 0. We don't
929 * want to confuse SCSI because SCSI will replace payload
930 */
931 if (rw & REQ_DISCARD)
932 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600933 if (conf->mddev->gendisk)
934 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
935 rbi, disk_devt(conf->mddev->gendisk),
936 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100937 generic_make_request(rbi);
938 }
939 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000940 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700941 set_bit(STRIPE_DEGRADED, &sh->state);
942 pr_debug("skip op %ld on disc %d for sector %llu\n",
943 bi->bi_rw, i, (unsigned long long)sh->sector);
944 clear_bit(R5_LOCKED, &sh->dev[i].flags);
945 set_bit(STRIPE_HANDLE, &sh->state);
946 }
947 }
948}
949
950static struct dma_async_tx_descriptor *
951async_copy_data(int frombio, struct bio *bio, struct page *page,
952 sector_t sector, struct dma_async_tx_descriptor *tx)
953{
954 struct bio_vec *bvl;
955 struct page *bio_page;
956 int i;
957 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700958 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700959 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700960
961 if (bio->bi_sector >= sector)
962 page_offset = (signed)(bio->bi_sector - sector) * 512;
963 else
964 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700965
Dan Williams0403e382009-09-08 17:42:50 -0700966 if (frombio)
967 flags |= ASYNC_TX_FENCE;
968 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
969
Dan Williams91c00922007-01-02 13:52:30 -0700970 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000971 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700972 int clen;
973 int b_offset = 0;
974
975 if (page_offset < 0) {
976 b_offset = -page_offset;
977 page_offset += b_offset;
978 len -= b_offset;
979 }
980
981 if (len > 0 && page_offset + len > STRIPE_SIZE)
982 clen = STRIPE_SIZE - page_offset;
983 else
984 clen = len;
985
986 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000987 b_offset += bvl->bv_offset;
988 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700989 if (frombio)
990 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700991 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700992 else
993 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700994 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700995 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700996 /* chain the operations */
997 submit.depend_tx = tx;
998
Dan Williams91c00922007-01-02 13:52:30 -0700999 if (clen < len) /* hit end of page */
1000 break;
1001 page_offset += len;
1002 }
1003
1004 return tx;
1005}
1006
1007static void ops_complete_biofill(void *stripe_head_ref)
1008{
1009 struct stripe_head *sh = stripe_head_ref;
1010 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -07001011 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001012
Harvey Harrisone46b2722008-04-28 02:15:50 -07001013 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001014 (unsigned long long)sh->sector);
1015
1016 /* clear completed biofills */
1017 for (i = sh->disks; i--; ) {
1018 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001019
1020 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001021 /* and check if we need to reply to a read request,
1022 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001023 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001024 */
1025 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001026 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001027
Dan Williams91c00922007-01-02 13:52:30 -07001028 BUG_ON(!dev->read);
1029 rbi = dev->read;
1030 dev->read = NULL;
1031 while (rbi && rbi->bi_sector <
1032 dev->sector + STRIPE_SECTORS) {
1033 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10001034 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -07001035 rbi->bi_next = return_bi;
1036 return_bi = rbi;
1037 }
Dan Williams91c00922007-01-02 13:52:30 -07001038 rbi = rbi2;
1039 }
1040 }
1041 }
Dan Williams83de75c2008-06-28 08:31:58 +10001042 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001043
1044 return_io(return_bi);
1045
Dan Williamse4d84902007-09-24 10:06:13 -07001046 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001047 release_stripe(sh);
1048}
1049
1050static void ops_run_biofill(struct stripe_head *sh)
1051{
1052 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001053 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001054 int i;
1055
Harvey Harrisone46b2722008-04-28 02:15:50 -07001056 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001057 (unsigned long long)sh->sector);
1058
1059 for (i = sh->disks; i--; ) {
1060 struct r5dev *dev = &sh->dev[i];
1061 if (test_bit(R5_Wantfill, &dev->flags)) {
1062 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001063 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001064 dev->read = rbi = dev->toread;
1065 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001066 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001067 while (rbi && rbi->bi_sector <
1068 dev->sector + STRIPE_SECTORS) {
1069 tx = async_copy_data(0, rbi, dev->page,
1070 dev->sector, tx);
1071 rbi = r5_next_bio(rbi, dev->sector);
1072 }
1073 }
1074 }
1075
1076 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001077 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1078 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001079}
1080
Dan Williams4e7d2c02009-08-29 19:13:11 -07001081static void mark_target_uptodate(struct stripe_head *sh, int target)
1082{
1083 struct r5dev *tgt;
1084
1085 if (target < 0)
1086 return;
1087
1088 tgt = &sh->dev[target];
1089 set_bit(R5_UPTODATE, &tgt->flags);
1090 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1091 clear_bit(R5_Wantcompute, &tgt->flags);
1092}
1093
Dan Williamsac6b53b2009-07-14 13:40:19 -07001094static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001095{
1096 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001097
Harvey Harrisone46b2722008-04-28 02:15:50 -07001098 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001099 (unsigned long long)sh->sector);
1100
Dan Williamsac6b53b2009-07-14 13:40:19 -07001101 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001102 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001103 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001104
Dan Williamsecc65c92008-06-28 08:31:57 +10001105 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1106 if (sh->check_state == check_state_compute_run)
1107 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001108 set_bit(STRIPE_HANDLE, &sh->state);
1109 release_stripe(sh);
1110}
1111
Dan Williamsd6f38f32009-07-14 11:50:52 -07001112/* return a pointer to the address conversion region of the scribble buffer */
1113static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1114 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001115{
Dan Williamsd6f38f32009-07-14 11:50:52 -07001116 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
1117}
1118
1119static struct dma_async_tx_descriptor *
1120ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1121{
Dan Williams91c00922007-01-02 13:52:30 -07001122 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001123 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001124 int target = sh->ops.target;
1125 struct r5dev *tgt = &sh->dev[target];
1126 struct page *xor_dest = tgt->page;
1127 int count = 0;
1128 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001129 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001130 int i;
1131
1132 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001133 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001134 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1135
1136 for (i = disks; i--; )
1137 if (i != target)
1138 xor_srcs[count++] = sh->dev[i].page;
1139
1140 atomic_inc(&sh->count);
1141
Dan Williams0403e382009-09-08 17:42:50 -07001142 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001143 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -07001144 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001145 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001146 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001147 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001148
Dan Williams91c00922007-01-02 13:52:30 -07001149 return tx;
1150}
1151
Dan Williamsac6b53b2009-07-14 13:40:19 -07001152/* set_syndrome_sources - populate source buffers for gen_syndrome
1153 * @srcs - (struct page *) array of size sh->disks
1154 * @sh - stripe_head to parse
1155 *
1156 * Populates srcs in proper layout order for the stripe and returns the
1157 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1158 * destination buffer is recorded in srcs[count] and the Q destination
1159 * is recorded in srcs[count+1]].
1160 */
1161static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1162{
1163 int disks = sh->disks;
1164 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1165 int d0_idx = raid6_d0(sh);
1166 int count;
1167 int i;
1168
1169 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001170 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001171
1172 count = 0;
1173 i = d0_idx;
1174 do {
1175 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1176
1177 srcs[slot] = sh->dev[i].page;
1178 i = raid6_next_disk(i, disks);
1179 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001180
NeilBrowne4424fe2009-10-16 16:27:34 +11001181 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001182}
1183
1184static struct dma_async_tx_descriptor *
1185ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1186{
1187 int disks = sh->disks;
1188 struct page **blocks = percpu->scribble;
1189 int target;
1190 int qd_idx = sh->qd_idx;
1191 struct dma_async_tx_descriptor *tx;
1192 struct async_submit_ctl submit;
1193 struct r5dev *tgt;
1194 struct page *dest;
1195 int i;
1196 int count;
1197
1198 if (sh->ops.target < 0)
1199 target = sh->ops.target2;
1200 else if (sh->ops.target2 < 0)
1201 target = sh->ops.target;
1202 else
1203 /* we should only have one valid target */
1204 BUG();
1205 BUG_ON(target < 0);
1206 pr_debug("%s: stripe %llu block: %d\n",
1207 __func__, (unsigned long long)sh->sector, target);
1208
1209 tgt = &sh->dev[target];
1210 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1211 dest = tgt->page;
1212
1213 atomic_inc(&sh->count);
1214
1215 if (target == qd_idx) {
1216 count = set_syndrome_sources(blocks, sh);
1217 blocks[count] = NULL; /* regenerating p is not necessary */
1218 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001219 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1220 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001221 to_addr_conv(sh, percpu));
1222 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1223 } else {
1224 /* Compute any data- or p-drive using XOR */
1225 count = 0;
1226 for (i = disks; i-- ; ) {
1227 if (i == target || i == qd_idx)
1228 continue;
1229 blocks[count++] = sh->dev[i].page;
1230 }
1231
Dan Williams0403e382009-09-08 17:42:50 -07001232 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1233 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001234 to_addr_conv(sh, percpu));
1235 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1236 }
1237
1238 return tx;
1239}
1240
1241static struct dma_async_tx_descriptor *
1242ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1243{
1244 int i, count, disks = sh->disks;
1245 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1246 int d0_idx = raid6_d0(sh);
1247 int faila = -1, failb = -1;
1248 int target = sh->ops.target;
1249 int target2 = sh->ops.target2;
1250 struct r5dev *tgt = &sh->dev[target];
1251 struct r5dev *tgt2 = &sh->dev[target2];
1252 struct dma_async_tx_descriptor *tx;
1253 struct page **blocks = percpu->scribble;
1254 struct async_submit_ctl submit;
1255
1256 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1257 __func__, (unsigned long long)sh->sector, target, target2);
1258 BUG_ON(target < 0 || target2 < 0);
1259 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1260 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1261
Dan Williams6c910a72009-09-16 12:24:54 -07001262 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001263 * slot number conversion for 'faila' and 'failb'
1264 */
1265 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001266 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001267 count = 0;
1268 i = d0_idx;
1269 do {
1270 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1271
1272 blocks[slot] = sh->dev[i].page;
1273
1274 if (i == target)
1275 faila = slot;
1276 if (i == target2)
1277 failb = slot;
1278 i = raid6_next_disk(i, disks);
1279 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001280
1281 BUG_ON(faila == failb);
1282 if (failb < faila)
1283 swap(faila, failb);
1284 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1285 __func__, (unsigned long long)sh->sector, faila, failb);
1286
1287 atomic_inc(&sh->count);
1288
1289 if (failb == syndrome_disks+1) {
1290 /* Q disk is one of the missing disks */
1291 if (faila == syndrome_disks) {
1292 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001293 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1294 ops_complete_compute, sh,
1295 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001296 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001297 STRIPE_SIZE, &submit);
1298 } else {
1299 struct page *dest;
1300 int data_target;
1301 int qd_idx = sh->qd_idx;
1302
1303 /* Missing D+Q: recompute D from P, then recompute Q */
1304 if (target == qd_idx)
1305 data_target = target2;
1306 else
1307 data_target = target;
1308
1309 count = 0;
1310 for (i = disks; i-- ; ) {
1311 if (i == data_target || i == qd_idx)
1312 continue;
1313 blocks[count++] = sh->dev[i].page;
1314 }
1315 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001316 init_async_submit(&submit,
1317 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1318 NULL, NULL, NULL,
1319 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001320 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1321 &submit);
1322
1323 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001324 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1325 ops_complete_compute, sh,
1326 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001327 return async_gen_syndrome(blocks, 0, count+2,
1328 STRIPE_SIZE, &submit);
1329 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001330 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001331 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1332 ops_complete_compute, sh,
1333 to_addr_conv(sh, percpu));
1334 if (failb == syndrome_disks) {
1335 /* We're missing D+P. */
1336 return async_raid6_datap_recov(syndrome_disks+2,
1337 STRIPE_SIZE, faila,
1338 blocks, &submit);
1339 } else {
1340 /* We're missing D+D. */
1341 return async_raid6_2data_recov(syndrome_disks+2,
1342 STRIPE_SIZE, faila, failb,
1343 blocks, &submit);
1344 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001345 }
1346}
1347
1348
Dan Williams91c00922007-01-02 13:52:30 -07001349static void ops_complete_prexor(void *stripe_head_ref)
1350{
1351 struct stripe_head *sh = stripe_head_ref;
1352
Harvey Harrisone46b2722008-04-28 02:15:50 -07001353 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001354 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001355}
1356
1357static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001358ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1359 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001360{
Dan Williams91c00922007-01-02 13:52:30 -07001361 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001362 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001363 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001364 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001365
1366 /* existing parity data subtracted */
1367 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1368
Harvey Harrisone46b2722008-04-28 02:15:50 -07001369 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001370 (unsigned long long)sh->sector);
1371
1372 for (i = disks; i--; ) {
1373 struct r5dev *dev = &sh->dev[i];
1374 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001375 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001376 xor_srcs[count++] = dev->page;
1377 }
1378
Dan Williams0403e382009-09-08 17:42:50 -07001379 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001380 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001381 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001382
1383 return tx;
1384}
1385
1386static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001387ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001388{
1389 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001390 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001391
Harvey Harrisone46b2722008-04-28 02:15:50 -07001392 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001393 (unsigned long long)sh->sector);
1394
1395 for (i = disks; i--; ) {
1396 struct r5dev *dev = &sh->dev[i];
1397 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001398
Dan Williamsd8ee0722008-06-28 08:32:06 +10001399 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001400 struct bio *wbi;
1401
Shaohua Lib17459c2012-07-19 16:01:31 +10001402 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001403 chosen = dev->towrite;
1404 dev->towrite = NULL;
1405 BUG_ON(dev->written);
1406 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001407 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001408
1409 while (wbi && wbi->bi_sector <
1410 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001411 if (wbi->bi_rw & REQ_FUA)
1412 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001413 if (wbi->bi_rw & REQ_SYNC)
1414 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001415 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001416 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001417 else
Shaohua Li620125f2012-10-11 13:49:05 +11001418 tx = async_copy_data(1, wbi, dev->page,
1419 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001420 wbi = r5_next_bio(wbi, dev->sector);
1421 }
1422 }
1423 }
1424
1425 return tx;
1426}
1427
Dan Williamsac6b53b2009-07-14 13:40:19 -07001428static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001429{
1430 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001431 int disks = sh->disks;
1432 int pd_idx = sh->pd_idx;
1433 int qd_idx = sh->qd_idx;
1434 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001435 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001436
Harvey Harrisone46b2722008-04-28 02:15:50 -07001437 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001438 (unsigned long long)sh->sector);
1439
Shaohua Libc0934f2012-05-22 13:55:05 +10001440 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001441 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001442 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001443 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001444 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001445
Dan Williams91c00922007-01-02 13:52:30 -07001446 for (i = disks; i--; ) {
1447 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001448
Tejun Heoe9c74692010-09-03 11:56:18 +02001449 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001450 if (!discard)
1451 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001452 if (fua)
1453 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001454 if (sync)
1455 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001456 }
Dan Williams91c00922007-01-02 13:52:30 -07001457 }
1458
Dan Williamsd8ee0722008-06-28 08:32:06 +10001459 if (sh->reconstruct_state == reconstruct_state_drain_run)
1460 sh->reconstruct_state = reconstruct_state_drain_result;
1461 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1462 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1463 else {
1464 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1465 sh->reconstruct_state = reconstruct_state_result;
1466 }
Dan Williams91c00922007-01-02 13:52:30 -07001467
1468 set_bit(STRIPE_HANDLE, &sh->state);
1469 release_stripe(sh);
1470}
1471
1472static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001473ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1474 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001475{
Dan Williams91c00922007-01-02 13:52:30 -07001476 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001477 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001478 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001479 int count = 0, pd_idx = sh->pd_idx, i;
1480 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001481 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001482 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001483
Harvey Harrisone46b2722008-04-28 02:15:50 -07001484 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001485 (unsigned long long)sh->sector);
1486
Shaohua Li620125f2012-10-11 13:49:05 +11001487 for (i = 0; i < sh->disks; i++) {
1488 if (pd_idx == i)
1489 continue;
1490 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1491 break;
1492 }
1493 if (i >= sh->disks) {
1494 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001495 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1496 ops_complete_reconstruct(sh);
1497 return;
1498 }
Dan Williams91c00922007-01-02 13:52:30 -07001499 /* check if prexor is active which means only process blocks
1500 * that are part of a read-modify-write (written)
1501 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001502 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1503 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001504 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1505 for (i = disks; i--; ) {
1506 struct r5dev *dev = &sh->dev[i];
1507 if (dev->written)
1508 xor_srcs[count++] = dev->page;
1509 }
1510 } else {
1511 xor_dest = sh->dev[pd_idx].page;
1512 for (i = disks; i--; ) {
1513 struct r5dev *dev = &sh->dev[i];
1514 if (i != pd_idx)
1515 xor_srcs[count++] = dev->page;
1516 }
1517 }
1518
Dan Williams91c00922007-01-02 13:52:30 -07001519 /* 1/ if we prexor'd then the dest is reused as a source
1520 * 2/ if we did not prexor then we are redoing the parity
1521 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1522 * for the synchronous xor case
1523 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001524 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001525 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1526
1527 atomic_inc(&sh->count);
1528
Dan Williamsac6b53b2009-07-14 13:40:19 -07001529 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001530 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001531 if (unlikely(count == 1))
1532 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1533 else
1534 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001535}
1536
Dan Williamsac6b53b2009-07-14 13:40:19 -07001537static void
1538ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1539 struct dma_async_tx_descriptor *tx)
1540{
1541 struct async_submit_ctl submit;
1542 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001543 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001544
1545 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1546
Shaohua Li620125f2012-10-11 13:49:05 +11001547 for (i = 0; i < sh->disks; i++) {
1548 if (sh->pd_idx == i || sh->qd_idx == i)
1549 continue;
1550 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1551 break;
1552 }
1553 if (i >= sh->disks) {
1554 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001555 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1556 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1557 ops_complete_reconstruct(sh);
1558 return;
1559 }
1560
Dan Williamsac6b53b2009-07-14 13:40:19 -07001561 count = set_syndrome_sources(blocks, sh);
1562
1563 atomic_inc(&sh->count);
1564
1565 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1566 sh, to_addr_conv(sh, percpu));
1567 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001568}
1569
1570static void ops_complete_check(void *stripe_head_ref)
1571{
1572 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001573
Harvey Harrisone46b2722008-04-28 02:15:50 -07001574 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001575 (unsigned long long)sh->sector);
1576
Dan Williamsecc65c92008-06-28 08:31:57 +10001577 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001578 set_bit(STRIPE_HANDLE, &sh->state);
1579 release_stripe(sh);
1580}
1581
Dan Williamsac6b53b2009-07-14 13:40:19 -07001582static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001583{
Dan Williams91c00922007-01-02 13:52:30 -07001584 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001585 int pd_idx = sh->pd_idx;
1586 int qd_idx = sh->qd_idx;
1587 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001588 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001589 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001590 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001591 int count;
1592 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001593
Harvey Harrisone46b2722008-04-28 02:15:50 -07001594 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001595 (unsigned long long)sh->sector);
1596
Dan Williamsac6b53b2009-07-14 13:40:19 -07001597 count = 0;
1598 xor_dest = sh->dev[pd_idx].page;
1599 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001600 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001601 if (i == pd_idx || i == qd_idx)
1602 continue;
1603 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001604 }
1605
Dan Williamsd6f38f32009-07-14 11:50:52 -07001606 init_async_submit(&submit, 0, NULL, NULL, NULL,
1607 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001608 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001609 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001610
Dan Williams91c00922007-01-02 13:52:30 -07001611 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001612 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1613 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001614}
1615
Dan Williamsac6b53b2009-07-14 13:40:19 -07001616static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1617{
1618 struct page **srcs = percpu->scribble;
1619 struct async_submit_ctl submit;
1620 int count;
1621
1622 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1623 (unsigned long long)sh->sector, checkp);
1624
1625 count = set_syndrome_sources(srcs, sh);
1626 if (!checkp)
1627 srcs[count] = NULL;
1628
1629 atomic_inc(&sh->count);
1630 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1631 sh, to_addr_conv(sh, percpu));
1632 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1633 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1634}
1635
NeilBrown51acbce2013-02-28 09:08:34 +11001636static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001637{
1638 int overlap_clear = 0, i, disks = sh->disks;
1639 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001640 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001641 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001642 struct raid5_percpu *percpu;
1643 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001644
Dan Williamsd6f38f32009-07-14 11:50:52 -07001645 cpu = get_cpu();
1646 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001647 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001648 ops_run_biofill(sh);
1649 overlap_clear++;
1650 }
1651
Dan Williams7b3a8712008-06-28 08:32:09 +10001652 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001653 if (level < 6)
1654 tx = ops_run_compute5(sh, percpu);
1655 else {
1656 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1657 tx = ops_run_compute6_1(sh, percpu);
1658 else
1659 tx = ops_run_compute6_2(sh, percpu);
1660 }
1661 /* terminate the chain if reconstruct is not set to be run */
1662 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001663 async_tx_ack(tx);
1664 }
Dan Williams91c00922007-01-02 13:52:30 -07001665
Dan Williams600aa102008-06-28 08:32:05 +10001666 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001667 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001668
Dan Williams600aa102008-06-28 08:32:05 +10001669 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001670 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001671 overlap_clear++;
1672 }
1673
Dan Williamsac6b53b2009-07-14 13:40:19 -07001674 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1675 if (level < 6)
1676 ops_run_reconstruct5(sh, percpu, tx);
1677 else
1678 ops_run_reconstruct6(sh, percpu, tx);
1679 }
Dan Williams91c00922007-01-02 13:52:30 -07001680
Dan Williamsac6b53b2009-07-14 13:40:19 -07001681 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1682 if (sh->check_state == check_state_run)
1683 ops_run_check_p(sh, percpu);
1684 else if (sh->check_state == check_state_run_q)
1685 ops_run_check_pq(sh, percpu, 0);
1686 else if (sh->check_state == check_state_run_pq)
1687 ops_run_check_pq(sh, percpu, 1);
1688 else
1689 BUG();
1690 }
Dan Williams91c00922007-01-02 13:52:30 -07001691
Dan Williams91c00922007-01-02 13:52:30 -07001692 if (overlap_clear)
1693 for (i = disks; i--; ) {
1694 struct r5dev *dev = &sh->dev[i];
1695 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1696 wake_up(&sh->raid_conf->wait_for_overlap);
1697 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001698 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001699}
1700
Shaohua Li566c09c2013-11-14 15:16:17 +11001701static int grow_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001704 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001705 if (!sh)
1706 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001707
NeilBrown3f294f42005-11-08 21:39:25 -08001708 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001709
Shaohua Lib17459c2012-07-19 16:01:31 +10001710 spin_lock_init(&sh->stripe_lock);
1711
NeilBrowne4e11e32010-06-16 16:45:16 +10001712 if (grow_buffers(sh)) {
1713 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001714 kmem_cache_free(conf->slab_cache, sh);
1715 return 0;
1716 }
Shaohua Li566c09c2013-11-14 15:16:17 +11001717 sh->hash_lock_index = hash;
NeilBrown3f294f42005-11-08 21:39:25 -08001718 /* we just created an active stripe so... */
1719 atomic_set(&sh->count, 1);
1720 atomic_inc(&conf->active_stripes);
1721 INIT_LIST_HEAD(&sh->lru);
1722 release_stripe(sh);
1723 return 1;
1724}
1725
NeilBrownd1688a62011-10-11 16:49:52 +11001726static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001727{
Christoph Lametere18b8902006-12-06 20:33:20 -08001728 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001729 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Shaohua Li566c09c2013-11-14 15:16:17 +11001730 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
NeilBrownf4be6b42010-06-01 19:37:25 +10001732 if (conf->mddev->gendisk)
1733 sprintf(conf->cache_name[0],
1734 "raid%d-%s", conf->level, mdname(conf->mddev));
1735 else
1736 sprintf(conf->cache_name[0],
1737 "raid%d-%p", conf->level, conf->mddev);
1738 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1739
NeilBrownad01c9e2006-03-27 01:18:07 -08001740 conf->active_name = 0;
1741 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001743 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (!sc)
1745 return 1;
1746 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001747 conf->pool_size = devs;
Shaohua Li566c09c2013-11-14 15:16:17 +11001748 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
1749 while (num--) {
1750 if (!grow_one_stripe(conf, hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11001752 conf->max_nr_stripes++;
1753 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return 0;
1756}
NeilBrown29269552006-03-27 01:18:10 -08001757
Dan Williamsd6f38f32009-07-14 11:50:52 -07001758/**
1759 * scribble_len - return the required size of the scribble region
1760 * @num - total number of disks in the array
1761 *
1762 * The size must be enough to contain:
1763 * 1/ a struct page pointer for each device in the array +2
1764 * 2/ room to convert each entry in (1) to its corresponding dma
1765 * (dma_map_page()) or page (page_address()) address.
1766 *
1767 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1768 * calculate over all devices (not just the data blocks), using zeros in place
1769 * of the P and Q blocks.
1770 */
1771static size_t scribble_len(int num)
1772{
1773 size_t len;
1774
1775 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1776
1777 return len;
1778}
1779
NeilBrownd1688a62011-10-11 16:49:52 +11001780static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001781{
1782 /* Make all the stripes able to hold 'newsize' devices.
1783 * New slots in each stripe get 'page' set to a new page.
1784 *
1785 * This happens in stages:
1786 * 1/ create a new kmem_cache and allocate the required number of
1787 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001788 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001789 * to the new stripe_heads. This will have the side effect of
1790 * freezing the array as once all stripe_heads have been collected,
1791 * no IO will be possible. Old stripe heads are freed once their
1792 * pages have been transferred over, and the old kmem_cache is
1793 * freed when all stripes are done.
1794 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1795 * we simple return a failre status - no need to clean anything up.
1796 * 4/ allocate new pages for the new slots in the new stripe_heads.
1797 * If this fails, we don't bother trying the shrink the
1798 * stripe_heads down again, we just leave them as they are.
1799 * As each stripe_head is processed the new one is released into
1800 * active service.
1801 *
1802 * Once step2 is started, we cannot afford to wait for a write,
1803 * so we use GFP_NOIO allocations.
1804 */
1805 struct stripe_head *osh, *nsh;
1806 LIST_HEAD(newstripes);
1807 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001808 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001809 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001810 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001811 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11001812 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08001813
1814 if (newsize <= conf->pool_size)
1815 return 0; /* never bother to shrink */
1816
Dan Williamsb5470dc2008-06-27 21:44:04 -07001817 err = md_allow_write(conf->mddev);
1818 if (err)
1819 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001820
NeilBrownad01c9e2006-03-27 01:18:07 -08001821 /* Step 1 */
1822 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1823 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001824 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001825 if (!sc)
1826 return -ENOMEM;
1827
1828 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001829 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001830 if (!nsh)
1831 break;
1832
NeilBrownad01c9e2006-03-27 01:18:07 -08001833 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001834 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001835
1836 list_add(&nsh->lru, &newstripes);
1837 }
1838 if (i) {
1839 /* didn't get enough, give up */
1840 while (!list_empty(&newstripes)) {
1841 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1842 list_del(&nsh->lru);
1843 kmem_cache_free(sc, nsh);
1844 }
1845 kmem_cache_destroy(sc);
1846 return -ENOMEM;
1847 }
1848 /* Step 2 - Must use GFP_NOIO now.
1849 * OK, we have enough stripes, start collecting inactive
1850 * stripes and copying them over
1851 */
Shaohua Li566c09c2013-11-14 15:16:17 +11001852 hash = 0;
1853 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08001854 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11001855 lock_device_hash_lock(conf, hash);
1856 wait_event_cmd(conf->wait_for_stripe,
1857 !list_empty(conf->inactive_list + hash),
1858 unlock_device_hash_lock(conf, hash),
1859 lock_device_hash_lock(conf, hash));
1860 osh = get_free_stripe(conf, hash);
1861 unlock_device_hash_lock(conf, hash);
NeilBrownad01c9e2006-03-27 01:18:07 -08001862 atomic_set(&nsh->count, 1);
1863 for(i=0; i<conf->pool_size; i++)
1864 nsh->dev[i].page = osh->dev[i].page;
1865 for( ; i<newsize; i++)
1866 nsh->dev[i].page = NULL;
Shaohua Li566c09c2013-11-14 15:16:17 +11001867 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08001868 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11001869 cnt++;
1870 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
1871 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
1872 hash++;
1873 cnt = 0;
1874 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001875 }
1876 kmem_cache_destroy(conf->slab_cache);
1877
1878 /* Step 3.
1879 * At this point, we are holding all the stripes so the array
1880 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001881 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001882 */
1883 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1884 if (ndisks) {
1885 for (i=0; i<conf->raid_disks; i++)
1886 ndisks[i] = conf->disks[i];
1887 kfree(conf->disks);
1888 conf->disks = ndisks;
1889 } else
1890 err = -ENOMEM;
1891
Dan Williamsd6f38f32009-07-14 11:50:52 -07001892 get_online_cpus();
1893 conf->scribble_len = scribble_len(newsize);
1894 for_each_present_cpu(cpu) {
1895 struct raid5_percpu *percpu;
1896 void *scribble;
1897
1898 percpu = per_cpu_ptr(conf->percpu, cpu);
1899 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1900
1901 if (scribble) {
1902 kfree(percpu->scribble);
1903 percpu->scribble = scribble;
1904 } else {
1905 err = -ENOMEM;
1906 break;
1907 }
1908 }
1909 put_online_cpus();
1910
NeilBrownad01c9e2006-03-27 01:18:07 -08001911 /* Step 4, return new stripes to service */
1912 while(!list_empty(&newstripes)) {
1913 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1914 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001915
NeilBrownad01c9e2006-03-27 01:18:07 -08001916 for (i=conf->raid_disks; i < newsize; i++)
1917 if (nsh->dev[i].page == NULL) {
1918 struct page *p = alloc_page(GFP_NOIO);
1919 nsh->dev[i].page = p;
1920 if (!p)
1921 err = -ENOMEM;
1922 }
1923 release_stripe(nsh);
1924 }
1925 /* critical section pass, GFP_NOIO no longer needed */
1926
1927 conf->slab_cache = sc;
1928 conf->active_name = 1-conf->active_name;
1929 conf->pool_size = newsize;
1930 return err;
1931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Shaohua Li566c09c2013-11-14 15:16:17 +11001933static int drop_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct stripe_head *sh;
1936
Shaohua Li566c09c2013-11-14 15:16:17 +11001937 spin_lock_irq(conf->hash_locks + hash);
1938 sh = get_free_stripe(conf, hash);
1939 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08001940 if (!sh)
1941 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001942 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001943 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001944 kmem_cache_free(conf->slab_cache, sh);
1945 atomic_dec(&conf->active_stripes);
1946 return 1;
1947}
1948
NeilBrownd1688a62011-10-11 16:49:52 +11001949static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001950{
Shaohua Li566c09c2013-11-14 15:16:17 +11001951 int hash;
1952 for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++)
1953 while (drop_one_stripe(conf, hash))
1954 ;
NeilBrown3f294f42005-11-08 21:39:25 -08001955
NeilBrown29fc7e32006-02-03 03:03:41 -08001956 if (conf->slab_cache)
1957 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 conf->slab_cache = NULL;
1959}
1960
NeilBrown6712ecf2007-09-27 12:47:43 +02001961static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
NeilBrown99c0fb52009-03-31 14:39:38 +11001963 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001964 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001965 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001967 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001968 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001969 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 for (i=0 ; i<disks; i++)
1972 if (bi == &sh->dev[i].req)
1973 break;
1974
Dan Williams45b42332007-07-09 11:56:43 -07001975 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1976 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 uptodate);
1978 if (i == disks) {
1979 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
NeilBrown14a75d32011-12-23 10:17:52 +11001982 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001983 /* If replacement finished while this request was outstanding,
1984 * 'replacement' might be NULL already.
1985 * In that case it moved down to 'rdev'.
1986 * rdev is not removed until all requests are finished.
1987 */
NeilBrown14a75d32011-12-23 10:17:52 +11001988 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001989 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001990 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
NeilBrown05616be2012-05-21 09:27:00 +10001992 if (use_new_offset(conf, sh))
1993 s = sh->sector + rdev->new_data_offset;
1994 else
1995 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001998 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001999 /* Note that this cannot happen on a
2000 * replacement device. We just fail those on
2001 * any error
2002 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10002003 printk_ratelimited(
2004 KERN_INFO
2005 "md/raid:%s: read error corrected"
2006 " (%lu sectors at %llu on %s)\n",
2007 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002008 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002009 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002010 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002011 clear_bit(R5_ReadError, &sh->dev[i].flags);
2012 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002013 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2014 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2015
NeilBrown14a75d32011-12-23 10:17:52 +11002016 if (atomic_read(&rdev->read_errors))
2017 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002019 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002020 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002021 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002024 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002025 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2026 printk_ratelimited(
2027 KERN_WARNING
2028 "md/raid:%s: read error on replacement device "
2029 "(sector %llu on %s).\n",
2030 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002031 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002032 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002033 else if (conf->mddev->degraded >= conf->max_degraded) {
2034 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002035 printk_ratelimited(
2036 KERN_WARNING
2037 "md/raid:%s: read error not correctable "
2038 "(sector %llu on %s).\n",
2039 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002040 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002041 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002042 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002043 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002044 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002045 printk_ratelimited(
2046 KERN_WARNING
2047 "md/raid:%s: read error NOT corrected!! "
2048 "(sector %llu on %s).\n",
2049 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002050 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002051 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002052 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002053 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002054 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002055 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002056 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002057 else
2058 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002059 if (set_bad && test_bit(In_sync, &rdev->flags)
2060 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2061 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002062 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002063 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2064 set_bit(R5_ReadError, &sh->dev[i].flags);
2065 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2066 } else
2067 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002068 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002069 clear_bit(R5_ReadError, &sh->dev[i].flags);
2070 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002071 if (!(set_bad
2072 && test_bit(In_sync, &rdev->flags)
2073 && rdev_set_badblocks(
2074 rdev, sh->sector, STRIPE_SECTORS, 0)))
2075 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
NeilBrown14a75d32011-12-23 10:17:52 +11002078 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2080 set_bit(STRIPE_HANDLE, &sh->state);
2081 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
NeilBrownd710e132008-10-13 11:55:12 +11002084static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
NeilBrown99c0fb52009-03-31 14:39:38 +11002086 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002087 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002088 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002089 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10002091 sector_t first_bad;
2092 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002093 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
NeilBrown977df362011-12-23 10:17:53 +11002095 for (i = 0 ; i < disks; i++) {
2096 if (bi == &sh->dev[i].req) {
2097 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 break;
NeilBrown977df362011-12-23 10:17:53 +11002099 }
2100 if (bi == &sh->dev[i].rreq) {
2101 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002102 if (rdev)
2103 replacement = 1;
2104 else
2105 /* rdev was removed and 'replacement'
2106 * replaced it. rdev is not removed
2107 * until all requests are finished.
2108 */
2109 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002110 break;
2111 }
2112 }
Dan Williams45b42332007-07-09 11:56:43 -07002113 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2115 uptodate);
2116 if (i == disks) {
2117 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
NeilBrown977df362011-12-23 10:17:53 +11002121 if (replacement) {
2122 if (!uptodate)
2123 md_error(conf->mddev, rdev);
2124 else if (is_badblock(rdev, sh->sector,
2125 STRIPE_SECTORS,
2126 &first_bad, &bad_sectors))
2127 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2128 } else {
2129 if (!uptodate) {
2130 set_bit(WriteErrorSeen, &rdev->flags);
2131 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002132 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2133 set_bit(MD_RECOVERY_NEEDED,
2134 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002135 } else if (is_badblock(rdev, sh->sector,
2136 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002137 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002138 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002139 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2140 /* That was a successful write so make
2141 * sure it looks like we already did
2142 * a re-write.
2143 */
2144 set_bit(R5_ReWrite, &sh->dev[i].flags);
2145 }
NeilBrown977df362011-12-23 10:17:53 +11002146 }
2147 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
NeilBrown977df362011-12-23 10:17:53 +11002149 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2150 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002152 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
NeilBrown784052e2009-03-31 15:19:07 +11002155static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
NeilBrown784052e2009-03-31 15:19:07 +11002157static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct r5dev *dev = &sh->dev[i];
2160
2161 bio_init(&dev->req);
2162 dev->req.bi_io_vec = &dev->vec;
2163 dev->req.bi_vcnt++;
2164 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11002166 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
NeilBrown977df362011-12-23 10:17:53 +11002168 bio_init(&dev->rreq);
2169 dev->rreq.bi_io_vec = &dev->rvec;
2170 dev->rreq.bi_vcnt++;
2171 dev->rreq.bi_max_vecs++;
2172 dev->rreq.bi_private = sh;
2173 dev->rvec.bv_page = dev->page;
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002176 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
NeilBrownfd01b882011-10-11 16:47:53 +11002179static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002182 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002183 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002184 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
NeilBrown908f4fb2011-12-23 10:17:50 +11002186 spin_lock_irqsave(&conf->device_lock, flags);
2187 clear_bit(In_sync, &rdev->flags);
2188 mddev->degraded = calc_degraded(conf);
2189 spin_unlock_irqrestore(&conf->device_lock, flags);
2190 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2191
NeilBrownde393cd2011-07-28 11:31:48 +10002192 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002193 set_bit(Faulty, &rdev->flags);
2194 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2195 printk(KERN_ALERT
2196 "md/raid:%s: Disk failure on %s, disabling device.\n"
2197 "md/raid:%s: Operation continuing on %d devices.\n",
2198 mdname(mddev),
2199 bdevname(rdev->bdev, b),
2200 mdname(mddev),
2201 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204/*
2205 * Input: a 'big' sector number,
2206 * Output: index of the data and parity disk, and the sector # in them.
2207 */
NeilBrownd1688a62011-10-11 16:49:52 +11002208static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002209 int previous, int *dd_idx,
2210 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002212 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002213 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002215 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002216 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002218 int algorithm = previous ? conf->prev_algo
2219 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002220 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2221 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002222 int raid_disks = previous ? conf->previous_raid_disks
2223 : conf->raid_disks;
2224 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* First compute the information on this sector */
2227
2228 /*
2229 * Compute the chunk number and the sector offset inside the chunk
2230 */
2231 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2232 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 /*
2235 * Compute the stripe number
2236 */
NeilBrown35f2a592010-04-20 14:13:34 +10002237 stripe = chunk_number;
2238 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002239 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /*
2241 * Select the parity disk based on the user selected algorithm.
2242 */
NeilBrown84789552011-07-27 11:00:36 +10002243 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002244 switch(conf->level) {
2245 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002246 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002247 break;
2248 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002249 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002251 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002252 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 (*dd_idx)++;
2254 break;
2255 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002256 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002257 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 (*dd_idx)++;
2259 break;
2260 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002261 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002262 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 break;
2264 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002265 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002266 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002268 case ALGORITHM_PARITY_0:
2269 pd_idx = 0;
2270 (*dd_idx)++;
2271 break;
2272 case ALGORITHM_PARITY_N:
2273 pd_idx = data_disks;
2274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002276 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002277 }
2278 break;
2279 case 6:
2280
NeilBrowne183eae2009-03-31 15:20:22 +11002281 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002282 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002283 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002284 qd_idx = pd_idx + 1;
2285 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002286 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002287 qd_idx = 0;
2288 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002289 (*dd_idx) += 2; /* D D P Q D */
2290 break;
2291 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002292 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002293 qd_idx = pd_idx + 1;
2294 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002295 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002296 qd_idx = 0;
2297 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002298 (*dd_idx) += 2; /* D D P Q D */
2299 break;
2300 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002301 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002302 qd_idx = (pd_idx + 1) % raid_disks;
2303 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002304 break;
2305 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002306 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002307 qd_idx = (pd_idx + 1) % raid_disks;
2308 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002309 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002310
2311 case ALGORITHM_PARITY_0:
2312 pd_idx = 0;
2313 qd_idx = 1;
2314 (*dd_idx) += 2;
2315 break;
2316 case ALGORITHM_PARITY_N:
2317 pd_idx = data_disks;
2318 qd_idx = data_disks + 1;
2319 break;
2320
2321 case ALGORITHM_ROTATING_ZERO_RESTART:
2322 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2323 * of blocks for computing Q is different.
2324 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002325 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002326 qd_idx = pd_idx + 1;
2327 if (pd_idx == raid_disks-1) {
2328 (*dd_idx)++; /* Q D D D P */
2329 qd_idx = 0;
2330 } else if (*dd_idx >= pd_idx)
2331 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002332 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002333 break;
2334
2335 case ALGORITHM_ROTATING_N_RESTART:
2336 /* Same a left_asymmetric, by first stripe is
2337 * D D D P Q rather than
2338 * Q D D D P
2339 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002340 stripe2 += 1;
2341 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002342 qd_idx = pd_idx + 1;
2343 if (pd_idx == raid_disks-1) {
2344 (*dd_idx)++; /* Q D D D P */
2345 qd_idx = 0;
2346 } else if (*dd_idx >= pd_idx)
2347 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002348 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002349 break;
2350
2351 case ALGORITHM_ROTATING_N_CONTINUE:
2352 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002353 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002354 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2355 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002356 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002357 break;
2358
2359 case ALGORITHM_LEFT_ASYMMETRIC_6:
2360 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002361 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002362 if (*dd_idx >= pd_idx)
2363 (*dd_idx)++;
2364 qd_idx = raid_disks - 1;
2365 break;
2366
2367 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002368 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002369 if (*dd_idx >= pd_idx)
2370 (*dd_idx)++;
2371 qd_idx = raid_disks - 1;
2372 break;
2373
2374 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002375 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002376 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2377 qd_idx = raid_disks - 1;
2378 break;
2379
2380 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002381 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002382 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2383 qd_idx = raid_disks - 1;
2384 break;
2385
2386 case ALGORITHM_PARITY_0_6:
2387 pd_idx = 0;
2388 (*dd_idx)++;
2389 qd_idx = raid_disks - 1;
2390 break;
2391
NeilBrown16a53ec2006-06-26 00:27:38 -07002392 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002393 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002394 }
2395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
NeilBrown911d4ee2009-03-31 14:39:38 +11002398 if (sh) {
2399 sh->pd_idx = pd_idx;
2400 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002401 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /*
2404 * Finally, compute the new sector number
2405 */
2406 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2407 return new_sector;
2408}
2409
2410
NeilBrown784052e2009-03-31 15:19:07 +11002411static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
NeilBrownd1688a62011-10-11 16:49:52 +11002413 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002414 int raid_disks = sh->disks;
2415 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002417 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2418 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002419 int algorithm = previous ? conf->prev_algo
2420 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 sector_t stripe;
2422 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002423 sector_t chunk_number;
2424 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002426 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
NeilBrown16a53ec2006-06-26 00:27:38 -07002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2430 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
NeilBrown16a53ec2006-06-26 00:27:38 -07002432 if (i == sh->pd_idx)
2433 return 0;
2434 switch(conf->level) {
2435 case 4: break;
2436 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002437 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 case ALGORITHM_LEFT_ASYMMETRIC:
2439 case ALGORITHM_RIGHT_ASYMMETRIC:
2440 if (i > sh->pd_idx)
2441 i--;
2442 break;
2443 case ALGORITHM_LEFT_SYMMETRIC:
2444 case ALGORITHM_RIGHT_SYMMETRIC:
2445 if (i < sh->pd_idx)
2446 i += raid_disks;
2447 i -= (sh->pd_idx + 1);
2448 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002449 case ALGORITHM_PARITY_0:
2450 i -= 1;
2451 break;
2452 case ALGORITHM_PARITY_N:
2453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002455 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002456 }
2457 break;
2458 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002459 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002460 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002461 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002462 case ALGORITHM_LEFT_ASYMMETRIC:
2463 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002464 case ALGORITHM_ROTATING_ZERO_RESTART:
2465 case ALGORITHM_ROTATING_N_RESTART:
2466 if (sh->pd_idx == raid_disks-1)
2467 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002468 else if (i > sh->pd_idx)
2469 i -= 2; /* D D P Q D */
2470 break;
2471 case ALGORITHM_LEFT_SYMMETRIC:
2472 case ALGORITHM_RIGHT_SYMMETRIC:
2473 if (sh->pd_idx == raid_disks-1)
2474 i--; /* Q D D D P */
2475 else {
2476 /* D D P Q D */
2477 if (i < sh->pd_idx)
2478 i += raid_disks;
2479 i -= (sh->pd_idx + 2);
2480 }
2481 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002482 case ALGORITHM_PARITY_0:
2483 i -= 2;
2484 break;
2485 case ALGORITHM_PARITY_N:
2486 break;
2487 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002488 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002489 if (sh->pd_idx == 0)
2490 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002491 else {
2492 /* D D Q P D */
2493 if (i < sh->pd_idx)
2494 i += raid_disks;
2495 i -= (sh->pd_idx + 1);
2496 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002497 break;
2498 case ALGORITHM_LEFT_ASYMMETRIC_6:
2499 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2500 if (i > sh->pd_idx)
2501 i--;
2502 break;
2503 case ALGORITHM_LEFT_SYMMETRIC_6:
2504 case ALGORITHM_RIGHT_SYMMETRIC_6:
2505 if (i < sh->pd_idx)
2506 i += data_disks + 1;
2507 i -= (sh->pd_idx + 1);
2508 break;
2509 case ALGORITHM_PARITY_0_6:
2510 i -= 1;
2511 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002512 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002513 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002514 }
2515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517
2518 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002519 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
NeilBrown112bf892009-03-31 14:39:38 +11002521 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002522 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002523 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2524 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002525 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2526 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return 0;
2528 }
2529 return r_sector;
2530}
2531
2532
Dan Williams600aa102008-06-28 08:32:05 +10002533static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002534schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002535 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002536{
2537 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002538 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002539 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002540
Dan Williamse33129d2007-01-02 13:52:30 -07002541 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002542
2543 for (i = disks; i--; ) {
2544 struct r5dev *dev = &sh->dev[i];
2545
2546 if (dev->towrite) {
2547 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002548 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002549 if (!expand)
2550 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002551 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002552 }
2553 }
NeilBrownce7d3632013-03-04 12:37:14 +11002554 /* if we are not expanding this is a proper write request, and
2555 * there will be bios with new data to be drained into the
2556 * stripe cache
2557 */
2558 if (!expand) {
2559 if (!s->locked)
2560 /* False alarm, nothing to do */
2561 return;
2562 sh->reconstruct_state = reconstruct_state_drain_run;
2563 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2564 } else
2565 sh->reconstruct_state = reconstruct_state_run;
2566
2567 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2568
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002569 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002570 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002571 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002572 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002573 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002574 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2575 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2576
Dan Williamse33129d2007-01-02 13:52:30 -07002577 for (i = disks; i--; ) {
2578 struct r5dev *dev = &sh->dev[i];
2579 if (i == pd_idx)
2580 continue;
2581
Dan Williamse33129d2007-01-02 13:52:30 -07002582 if (dev->towrite &&
2583 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002584 test_bit(R5_Wantcompute, &dev->flags))) {
2585 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002586 set_bit(R5_LOCKED, &dev->flags);
2587 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002588 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002589 }
2590 }
NeilBrownce7d3632013-03-04 12:37:14 +11002591 if (!s->locked)
2592 /* False alarm - nothing to do */
2593 return;
2594 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2595 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2596 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2597 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002598 }
2599
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002600 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002601 * are in flight
2602 */
2603 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2604 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002605 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002606
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002607 if (level == 6) {
2608 int qd_idx = sh->qd_idx;
2609 struct r5dev *dev = &sh->dev[qd_idx];
2610
2611 set_bit(R5_LOCKED, &dev->flags);
2612 clear_bit(R5_UPTODATE, &dev->flags);
2613 s->locked++;
2614 }
2615
Dan Williams600aa102008-06-28 08:32:05 +10002616 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002617 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002618 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002619}
NeilBrown16a53ec2006-06-26 00:27:38 -07002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621/*
2622 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002623 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 * The bi_next chain must be in order.
2625 */
2626static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2627{
2628 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002629 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002630 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
NeilBrowncbe47ec2011-07-26 11:20:35 +10002632 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 (unsigned long long)bi->bi_sector,
2634 (unsigned long long)sh->sector);
2635
Shaohua Lib17459c2012-07-19 16:01:31 +10002636 /*
2637 * If several bio share a stripe. The bio bi_phys_segments acts as a
2638 * reference count to avoid race. The reference count should already be
2639 * increased before this function is called (for example, in
2640 * make_request()), so other bio sharing this stripe will not free the
2641 * stripe. If a stripe is owned by one stripe, the stripe lock will
2642 * protect it.
2643 */
2644 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002645 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002647 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002648 firstwrite = 1;
2649 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 bip = &sh->dev[dd_idx].toread;
2651 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002652 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 goto overlap;
2654 bip = & (*bip)->bi_next;
2655 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002656 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 goto overlap;
2658
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002659 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if (*bip)
2661 bi->bi_next = *bip;
2662 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002663 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (forwrite) {
2666 /* check if page is covered */
2667 sector_t sector = sh->dev[dd_idx].sector;
2668 for (bi=sh->dev[dd_idx].towrite;
2669 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2670 bi && bi->bi_sector <= sector;
2671 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002672 if (bio_end_sector(bi) >= sector)
2673 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2676 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2677 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002678
2679 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2680 (unsigned long long)(*bip)->bi_sector,
2681 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002682 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002683
2684 if (conf->mddev->bitmap && firstwrite) {
2685 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2686 STRIPE_SECTORS, 0);
2687 sh->bm_seq = conf->seq_flush+1;
2688 set_bit(STRIPE_BIT_DELAY, &sh->state);
2689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return 1;
2691
2692 overlap:
2693 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002694 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return 0;
2696}
2697
NeilBrownd1688a62011-10-11 16:49:52 +11002698static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002699
NeilBrownd1688a62011-10-11 16:49:52 +11002700static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002701 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002702{
NeilBrown784052e2009-03-31 15:19:07 +11002703 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002704 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002705 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002706 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002707 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002708
NeilBrown112bf892009-03-31 14:39:38 +11002709 raid5_compute_sector(conf,
2710 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002711 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002712 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002713 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002714}
2715
Dan Williamsa4456852007-07-09 11:56:43 -07002716static void
NeilBrownd1688a62011-10-11 16:49:52 +11002717handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002718 struct stripe_head_state *s, int disks,
2719 struct bio **return_bi)
2720{
2721 int i;
2722 for (i = disks; i--; ) {
2723 struct bio *bi;
2724 int bitmap_end = 0;
2725
2726 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002727 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002728 rcu_read_lock();
2729 rdev = rcu_dereference(conf->disks[i].rdev);
2730 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002731 atomic_inc(&rdev->nr_pending);
2732 else
2733 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002734 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002735 if (rdev) {
2736 if (!rdev_set_badblocks(
2737 rdev,
2738 sh->sector,
2739 STRIPE_SECTORS, 0))
2740 md_error(conf->mddev, rdev);
2741 rdev_dec_pending(rdev, conf->mddev);
2742 }
Dan Williamsa4456852007-07-09 11:56:43 -07002743 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002744 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002745 /* fail all writes first */
2746 bi = sh->dev[i].towrite;
2747 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002748 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002749 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002750 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002751
2752 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2753 wake_up(&conf->wait_for_overlap);
2754
2755 while (bi && bi->bi_sector <
2756 sh->dev[i].sector + STRIPE_SECTORS) {
2757 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2758 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002759 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002760 md_write_end(conf->mddev);
2761 bi->bi_next = *return_bi;
2762 *return_bi = bi;
2763 }
2764 bi = nextbi;
2765 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002766 if (bitmap_end)
2767 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2768 STRIPE_SECTORS, 0, 0);
2769 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002770 /* and fail all 'written' */
2771 bi = sh->dev[i].written;
2772 sh->dev[i].written = NULL;
2773 if (bi) bitmap_end = 1;
2774 while (bi && bi->bi_sector <
2775 sh->dev[i].sector + STRIPE_SECTORS) {
2776 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2777 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002778 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002779 md_write_end(conf->mddev);
2780 bi->bi_next = *return_bi;
2781 *return_bi = bi;
2782 }
2783 bi = bi2;
2784 }
2785
Dan Williamsb5e98d62007-01-02 13:52:31 -07002786 /* fail any reads if this device is non-operational and
2787 * the data has not reached the cache yet.
2788 */
2789 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2790 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2791 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002792 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002793 bi = sh->dev[i].toread;
2794 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002795 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002796 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2797 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002798 while (bi && bi->bi_sector <
2799 sh->dev[i].sector + STRIPE_SECTORS) {
2800 struct bio *nextbi =
2801 r5_next_bio(bi, sh->dev[i].sector);
2802 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002803 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002804 bi->bi_next = *return_bi;
2805 *return_bi = bi;
2806 }
2807 bi = nextbi;
2808 }
2809 }
Dan Williamsa4456852007-07-09 11:56:43 -07002810 if (bitmap_end)
2811 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2812 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002813 /* If we were in the middle of a write the parity block might
2814 * still be locked - so just clear all R5_LOCKED flags
2815 */
2816 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002817 }
2818
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002819 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2820 if (atomic_dec_and_test(&conf->pending_full_writes))
2821 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002822}
2823
NeilBrown7f0da592011-07-28 11:39:22 +10002824static void
NeilBrownd1688a62011-10-11 16:49:52 +11002825handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002826 struct stripe_head_state *s)
2827{
2828 int abort = 0;
2829 int i;
2830
NeilBrown7f0da592011-07-28 11:39:22 +10002831 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002832 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2833 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002834 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002835 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002836 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002837 * Don't even need to abort as that is handled elsewhere
2838 * if needed, and not always wanted e.g. if there is a known
2839 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002840 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002841 * non-sync devices, or abort the recovery
2842 */
NeilBrown18b98372012-04-01 23:48:38 +10002843 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2844 /* During recovery devices cannot be removed, so
2845 * locking and refcounting of rdevs is not needed
2846 */
2847 for (i = 0; i < conf->raid_disks; i++) {
2848 struct md_rdev *rdev = conf->disks[i].rdev;
2849 if (rdev
2850 && !test_bit(Faulty, &rdev->flags)
2851 && !test_bit(In_sync, &rdev->flags)
2852 && !rdev_set_badblocks(rdev, sh->sector,
2853 STRIPE_SECTORS, 0))
2854 abort = 1;
2855 rdev = conf->disks[i].replacement;
2856 if (rdev
2857 && !test_bit(Faulty, &rdev->flags)
2858 && !test_bit(In_sync, &rdev->flags)
2859 && !rdev_set_badblocks(rdev, sh->sector,
2860 STRIPE_SECTORS, 0))
2861 abort = 1;
2862 }
2863 if (abort)
2864 conf->recovery_disabled =
2865 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002866 }
NeilBrown18b98372012-04-01 23:48:38 +10002867 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002868}
2869
NeilBrown9a3e1102011-12-23 10:17:53 +11002870static int want_replace(struct stripe_head *sh, int disk_idx)
2871{
2872 struct md_rdev *rdev;
2873 int rv = 0;
2874 /* Doing recovery so rcu locking not required */
2875 rdev = sh->raid_conf->disks[disk_idx].replacement;
2876 if (rdev
2877 && !test_bit(Faulty, &rdev->flags)
2878 && !test_bit(In_sync, &rdev->flags)
2879 && (rdev->recovery_offset <= sh->sector
2880 || rdev->mddev->recovery_cp <= sh->sector))
2881 rv = 1;
2882
2883 return rv;
2884}
2885
NeilBrown93b3dbc2011-07-27 11:00:36 +10002886/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002887 * to be read or computed to satisfy a request.
2888 *
2889 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002890 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002891 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002892static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2893 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002894{
2895 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002896 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2897 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002898
Dan Williamsf38e1212007-01-02 13:52:30 -07002899 /* is the data in this block needed, and can we get it? */
2900 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002901 !test_bit(R5_UPTODATE, &dev->flags) &&
2902 (dev->toread ||
2903 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2904 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002905 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002906 (s->failed >= 1 && fdev[0]->toread) ||
2907 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002908 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2909 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2910 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002911 /* we would like to get this block, possibly by computing it,
2912 * otherwise read it if the backing disk is insync
2913 */
2914 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2915 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2916 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002917 (s->failed && (disk_idx == s->failed_num[0] ||
2918 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002919 /* have disk failed, and we're requested to fetch it;
2920 * do compute it
2921 */
2922 pr_debug("Computing stripe %llu block %d\n",
2923 (unsigned long long)sh->sector, disk_idx);
2924 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2925 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2926 set_bit(R5_Wantcompute, &dev->flags);
2927 sh->ops.target = disk_idx;
2928 sh->ops.target2 = -1; /* no 2nd target */
2929 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002930 /* Careful: from this point on 'uptodate' is in the eye
2931 * of raid_run_ops which services 'compute' operations
2932 * before writes. R5_Wantcompute flags a block that will
2933 * be R5_UPTODATE by the time it is needed for a
2934 * subsequent operation.
2935 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002936 s->uptodate++;
2937 return 1;
2938 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2939 /* Computing 2-failure is *very* expensive; only
2940 * do it if failed >= 2
2941 */
2942 int other;
2943 for (other = disks; other--; ) {
2944 if (other == disk_idx)
2945 continue;
2946 if (!test_bit(R5_UPTODATE,
2947 &sh->dev[other].flags))
2948 break;
2949 }
2950 BUG_ON(other < 0);
2951 pr_debug("Computing stripe %llu blocks %d,%d\n",
2952 (unsigned long long)sh->sector,
2953 disk_idx, other);
2954 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2955 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2956 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2957 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2958 sh->ops.target = disk_idx;
2959 sh->ops.target2 = other;
2960 s->uptodate += 2;
2961 s->req_compute = 1;
2962 return 1;
2963 } else if (test_bit(R5_Insync, &dev->flags)) {
2964 set_bit(R5_LOCKED, &dev->flags);
2965 set_bit(R5_Wantread, &dev->flags);
2966 s->locked++;
2967 pr_debug("Reading block %d (sync=%d)\n",
2968 disk_idx, s->syncing);
2969 }
2970 }
2971
2972 return 0;
2973}
2974
2975/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002976 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002977 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002978static void handle_stripe_fill(struct stripe_head *sh,
2979 struct stripe_head_state *s,
2980 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002981{
2982 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002983
2984 /* look for blocks to read/compute, skip this if a compute
2985 * is already in flight, or if the stripe contents are in the
2986 * midst of changing due to a write
2987 */
2988 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2989 !sh->reconstruct_state)
2990 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002991 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002992 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002993 set_bit(STRIPE_HANDLE, &sh->state);
2994}
2995
2996
Dan Williams1fe797e2008-06-28 09:16:30 +10002997/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002998 * any written block on an uptodate or failed drive can be returned.
2999 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3000 * never LOCKED, so we don't need to test 'failed' directly.
3001 */
NeilBrownd1688a62011-10-11 16:49:52 +11003002static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07003003 struct stripe_head *sh, int disks, struct bio **return_bi)
3004{
3005 int i;
3006 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003007 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003008
3009 for (i = disks; i--; )
3010 if (sh->dev[i].written) {
3011 dev = &sh->dev[i];
3012 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003013 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11003014 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003015 /* We can return any write requests */
3016 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003017 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003018 if (test_and_clear_bit(R5_Discard, &dev->flags))
3019 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003020 wbi = dev->written;
3021 dev->written = NULL;
3022 while (wbi && wbi->bi_sector <
3023 dev->sector + STRIPE_SECTORS) {
3024 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003025 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003026 md_write_end(conf->mddev);
3027 wbi->bi_next = *return_bi;
3028 *return_bi = wbi;
3029 }
3030 wbi = wbi2;
3031 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003032 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3033 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003034 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003035 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003036 } else if (test_bit(R5_Discard, &dev->flags))
3037 discard_pending = 1;
3038 }
3039 if (!discard_pending &&
3040 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3041 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3042 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3043 if (sh->qd_idx >= 0) {
3044 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3045 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3046 }
3047 /* now that discard is done we can proceed with any sync */
3048 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003049 /*
3050 * SCSI discard will change some bio fields and the stripe has
3051 * no updated data, so remove it from hash list and the stripe
3052 * will be reinitialized
3053 */
3054 spin_lock_irq(&conf->device_lock);
3055 remove_hash(sh);
3056 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003057 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3058 set_bit(STRIPE_HANDLE, &sh->state);
3059
3060 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003061
3062 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3063 if (atomic_dec_and_test(&conf->pending_full_writes))
3064 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003065}
3066
NeilBrownd1688a62011-10-11 16:49:52 +11003067static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003068 struct stripe_head *sh,
3069 struct stripe_head_state *s,
3070 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003071{
3072 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003073 sector_t recovery_cp = conf->mddev->recovery_cp;
3074
3075 /* RAID6 requires 'rcw' in current implementation.
3076 * Otherwise, check whether resync is now happening or should start.
3077 * If yes, then the array is dirty (after unclean shutdown or
3078 * initial creation), so parity in some stripes might be inconsistent.
3079 * In this case, we need to always do reconstruct-write, to ensure
3080 * that in case of drive failure or read-error correction, we
3081 * generate correct data from the parity.
3082 */
3083 if (conf->max_degraded == 2 ||
3084 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
3085 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003086 * look like rcw is cheaper
3087 */
3088 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003089 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3090 conf->max_degraded, (unsigned long long)recovery_cp,
3091 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003092 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003093 /* would I have to read this buffer for read_modify_write */
3094 struct r5dev *dev = &sh->dev[i];
3095 if ((dev->towrite || i == sh->pd_idx) &&
3096 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003097 !(test_bit(R5_UPTODATE, &dev->flags) ||
3098 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003099 if (test_bit(R5_Insync, &dev->flags))
3100 rmw++;
3101 else
3102 rmw += 2*disks; /* cannot read it */
3103 }
3104 /* Would I have to read this buffer for reconstruct_write */
3105 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
3106 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003107 !(test_bit(R5_UPTODATE, &dev->flags) ||
3108 test_bit(R5_Wantcompute, &dev->flags))) {
3109 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003110 else
3111 rcw += 2*disks;
3112 }
3113 }
Dan Williams45b42332007-07-09 11:56:43 -07003114 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003115 (unsigned long long)sh->sector, rmw, rcw);
3116 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11003117 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003118 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003119 if (conf->mddev->queue)
3120 blk_add_trace_msg(conf->mddev->queue,
3121 "raid5 rmw %llu %d",
3122 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003123 for (i = disks; i--; ) {
3124 struct r5dev *dev = &sh->dev[i];
3125 if ((dev->towrite || i == sh->pd_idx) &&
3126 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003127 !(test_bit(R5_UPTODATE, &dev->flags) ||
3128 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003129 test_bit(R5_Insync, &dev->flags)) {
3130 if (
3131 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003132 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11003133 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07003134 set_bit(R5_LOCKED, &dev->flags);
3135 set_bit(R5_Wantread, &dev->flags);
3136 s->locked++;
3137 } else {
3138 set_bit(STRIPE_DELAYED, &sh->state);
3139 set_bit(STRIPE_HANDLE, &sh->state);
3140 }
3141 }
3142 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003143 }
NeilBrownc8ac1802011-07-27 11:00:36 +10003144 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003145 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003146 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003147 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003148 for (i = disks; i--; ) {
3149 struct r5dev *dev = &sh->dev[i];
3150 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003151 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003152 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003153 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003154 test_bit(R5_Wantcompute, &dev->flags))) {
3155 rcw++;
3156 if (!test_bit(R5_Insync, &dev->flags))
3157 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07003158 if (
3159 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003160 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003161 "%d for Reconstruct\n", i);
3162 set_bit(R5_LOCKED, &dev->flags);
3163 set_bit(R5_Wantread, &dev->flags);
3164 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003165 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003166 } else {
3167 set_bit(STRIPE_DELAYED, &sh->state);
3168 set_bit(STRIPE_HANDLE, &sh->state);
3169 }
3170 }
3171 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003172 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003173 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3174 (unsigned long long)sh->sector,
3175 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003176 }
Dan Williamsa4456852007-07-09 11:56:43 -07003177 /* now if nothing is locked, and if we have enough data,
3178 * we can start a write request
3179 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003180 /* since handle_stripe can be called at any time we need to handle the
3181 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003182 * subsequent call wants to start a write request. raid_run_ops only
3183 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003184 * simultaneously. If this is not the case then new writes need to be
3185 * held off until the compute completes.
3186 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003187 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3188 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3189 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003190 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003191}
3192
NeilBrownd1688a62011-10-11 16:49:52 +11003193static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003194 struct stripe_head_state *s, int disks)
3195{
Dan Williamsecc65c92008-06-28 08:31:57 +10003196 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003197
Dan Williamsbd2ab672008-04-10 21:29:27 -07003198 set_bit(STRIPE_HANDLE, &sh->state);
3199
Dan Williamsecc65c92008-06-28 08:31:57 +10003200 switch (sh->check_state) {
3201 case check_state_idle:
3202 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003203 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003204 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003205 sh->check_state = check_state_run;
3206 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003207 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003208 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003209 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003210 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003211 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003212 /* fall through */
3213 case check_state_compute_result:
3214 sh->check_state = check_state_idle;
3215 if (!dev)
3216 dev = &sh->dev[sh->pd_idx];
3217
3218 /* check that a write has not made the stripe insync */
3219 if (test_bit(STRIPE_INSYNC, &sh->state))
3220 break;
3221
3222 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003223 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3224 BUG_ON(s->uptodate != disks);
3225
3226 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003227 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003228 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003229
Dan Williamsa4456852007-07-09 11:56:43 -07003230 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003231 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003232 break;
3233 case check_state_run:
3234 break; /* we will be called again upon completion */
3235 case check_state_check_result:
3236 sh->check_state = check_state_idle;
3237
3238 /* if a failure occurred during the check operation, leave
3239 * STRIPE_INSYNC not set and let the stripe be handled again
3240 */
3241 if (s->failed)
3242 break;
3243
3244 /* handle a successful check operation, if parity is correct
3245 * we are done. Otherwise update the mismatch count and repair
3246 * parity if !MD_RECOVERY_CHECK
3247 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003248 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003249 /* parity is correct (on disc,
3250 * not in buffer any more)
3251 */
3252 set_bit(STRIPE_INSYNC, &sh->state);
3253 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003254 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003255 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3256 /* don't try to repair!! */
3257 set_bit(STRIPE_INSYNC, &sh->state);
3258 else {
3259 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003260 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003261 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3262 set_bit(R5_Wantcompute,
3263 &sh->dev[sh->pd_idx].flags);
3264 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003265 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003266 s->uptodate++;
3267 }
3268 }
3269 break;
3270 case check_state_compute_run:
3271 break;
3272 default:
3273 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3274 __func__, sh->check_state,
3275 (unsigned long long) sh->sector);
3276 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003277 }
3278}
3279
3280
NeilBrownd1688a62011-10-11 16:49:52 +11003281static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003282 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003283 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003284{
Dan Williamsa4456852007-07-09 11:56:43 -07003285 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003286 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003287 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003288
3289 set_bit(STRIPE_HANDLE, &sh->state);
3290
3291 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003292
Dan Williamsa4456852007-07-09 11:56:43 -07003293 /* Want to check and possibly repair P and Q.
3294 * However there could be one 'failed' device, in which
3295 * case we can only check one of them, possibly using the
3296 * other to generate missing data
3297 */
3298
Dan Williamsd82dfee2009-07-14 13:40:57 -07003299 switch (sh->check_state) {
3300 case check_state_idle:
3301 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003302 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003303 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003304 * makes sense to check P (If anything else were failed,
3305 * we would have used P to recreate it).
3306 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003307 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003308 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003309 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003310 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003311 * anything, so it makes sense to check it
3312 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003313 if (sh->check_state == check_state_run)
3314 sh->check_state = check_state_run_pq;
3315 else
3316 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003317 }
Dan Williams36d1c642009-07-14 11:48:22 -07003318
Dan Williamsd82dfee2009-07-14 13:40:57 -07003319 /* discard potentially stale zero_sum_result */
3320 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003321
Dan Williamsd82dfee2009-07-14 13:40:57 -07003322 if (sh->check_state == check_state_run) {
3323 /* async_xor_zero_sum destroys the contents of P */
3324 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3325 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003326 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003327 if (sh->check_state >= check_state_run &&
3328 sh->check_state <= check_state_run_pq) {
3329 /* async_syndrome_zero_sum preserves P and Q, so
3330 * no need to mark them !uptodate here
3331 */
3332 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3333 break;
3334 }
Dan Williams36d1c642009-07-14 11:48:22 -07003335
Dan Williamsd82dfee2009-07-14 13:40:57 -07003336 /* we have 2-disk failure */
3337 BUG_ON(s->failed != 2);
3338 /* fall through */
3339 case check_state_compute_result:
3340 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003341
Dan Williamsd82dfee2009-07-14 13:40:57 -07003342 /* check that a write has not made the stripe insync */
3343 if (test_bit(STRIPE_INSYNC, &sh->state))
3344 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003345
3346 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003347 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003348 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003349 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003350 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003351 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003352 s->locked++;
3353 set_bit(R5_LOCKED, &dev->flags);
3354 set_bit(R5_Wantwrite, &dev->flags);
3355 }
3356 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003357 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003358 s->locked++;
3359 set_bit(R5_LOCKED, &dev->flags);
3360 set_bit(R5_Wantwrite, &dev->flags);
3361 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003362 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003363 dev = &sh->dev[pd_idx];
3364 s->locked++;
3365 set_bit(R5_LOCKED, &dev->flags);
3366 set_bit(R5_Wantwrite, &dev->flags);
3367 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003368 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003369 dev = &sh->dev[qd_idx];
3370 s->locked++;
3371 set_bit(R5_LOCKED, &dev->flags);
3372 set_bit(R5_Wantwrite, &dev->flags);
3373 }
3374 clear_bit(STRIPE_DEGRADED, &sh->state);
3375
3376 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003377 break;
3378 case check_state_run:
3379 case check_state_run_q:
3380 case check_state_run_pq:
3381 break; /* we will be called again upon completion */
3382 case check_state_check_result:
3383 sh->check_state = check_state_idle;
3384
3385 /* handle a successful check operation, if parity is correct
3386 * we are done. Otherwise update the mismatch count and repair
3387 * parity if !MD_RECOVERY_CHECK
3388 */
3389 if (sh->ops.zero_sum_result == 0) {
3390 /* both parities are correct */
3391 if (!s->failed)
3392 set_bit(STRIPE_INSYNC, &sh->state);
3393 else {
3394 /* in contrast to the raid5 case we can validate
3395 * parity, but still have a failure to write
3396 * back
3397 */
3398 sh->check_state = check_state_compute_result;
3399 /* Returning at this point means that we may go
3400 * off and bring p and/or q uptodate again so
3401 * we make sure to check zero_sum_result again
3402 * to verify if p or q need writeback
3403 */
3404 }
3405 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003406 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003407 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3408 /* don't try to repair!! */
3409 set_bit(STRIPE_INSYNC, &sh->state);
3410 else {
3411 int *target = &sh->ops.target;
3412
3413 sh->ops.target = -1;
3414 sh->ops.target2 = -1;
3415 sh->check_state = check_state_compute_run;
3416 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3417 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3418 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3419 set_bit(R5_Wantcompute,
3420 &sh->dev[pd_idx].flags);
3421 *target = pd_idx;
3422 target = &sh->ops.target2;
3423 s->uptodate++;
3424 }
3425 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3426 set_bit(R5_Wantcompute,
3427 &sh->dev[qd_idx].flags);
3428 *target = qd_idx;
3429 s->uptodate++;
3430 }
3431 }
3432 }
3433 break;
3434 case check_state_compute_run:
3435 break;
3436 default:
3437 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3438 __func__, sh->check_state,
3439 (unsigned long long) sh->sector);
3440 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003441 }
3442}
3443
NeilBrownd1688a62011-10-11 16:49:52 +11003444static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003445{
3446 int i;
3447
3448 /* We have read all the blocks in this stripe and now we need to
3449 * copy some of them into a target stripe for expand.
3450 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003451 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003452 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3453 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003454 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003455 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003456 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003457 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003458
NeilBrown784052e2009-03-31 15:19:07 +11003459 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003460 sector_t s = raid5_compute_sector(conf, bn, 0,
3461 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003462 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003463 if (sh2 == NULL)
3464 /* so far only the early blocks of this stripe
3465 * have been requested. When later blocks
3466 * get requested, we will try again
3467 */
3468 continue;
3469 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3470 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3471 /* must have already done this block */
3472 release_stripe(sh2);
3473 continue;
3474 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003475
3476 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003477 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003478 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003479 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003480 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003481
Dan Williamsa4456852007-07-09 11:56:43 -07003482 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3483 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3484 for (j = 0; j < conf->raid_disks; j++)
3485 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003486 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003487 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3488 break;
3489 if (j == conf->raid_disks) {
3490 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3491 set_bit(STRIPE_HANDLE, &sh2->state);
3492 }
3493 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003494
Dan Williamsa4456852007-07-09 11:56:43 -07003495 }
NeilBrowna2e08552007-09-11 15:23:36 -07003496 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003497 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500/*
3501 * handle_stripe - do things to a stripe.
3502 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003503 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3504 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003506 * return some read requests which now have data
3507 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 * schedule a read on some buffers
3509 * schedule a write of some buffers
3510 * return confirmation of parity correctness
3511 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 */
Dan Williamsa4456852007-07-09 11:56:43 -07003513
NeilBrownacfe7262011-07-27 11:00:36 +10003514static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003515{
NeilBrownd1688a62011-10-11 16:49:52 +11003516 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003517 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003518 struct r5dev *dev;
3519 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003520 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003521
NeilBrownacfe7262011-07-27 11:00:36 +10003522 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003523
NeilBrownacfe7262011-07-27 11:00:36 +10003524 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3525 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3526 s->failed_num[0] = -1;
3527 s->failed_num[1] = -1;
3528
3529 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003530 rcu_read_lock();
3531 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003532 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003533 sector_t first_bad;
3534 int bad_sectors;
3535 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003536
NeilBrown16a53ec2006-06-26 00:27:38 -07003537 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003538
Dan Williams45b42332007-07-09 11:56:43 -07003539 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003540 i, dev->flags,
3541 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003542 /* maybe we can reply to a read
3543 *
3544 * new wantfill requests are only permitted while
3545 * ops_complete_biofill is guaranteed to be inactive
3546 */
3547 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3548 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3549 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003550
3551 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003552 if (test_bit(R5_LOCKED, &dev->flags))
3553 s->locked++;
3554 if (test_bit(R5_UPTODATE, &dev->flags))
3555 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003556 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003557 s->compute++;
3558 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003559 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003560
NeilBrownacfe7262011-07-27 11:00:36 +10003561 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003562 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003563 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003564 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003565 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003566 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003567 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003568 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003569 }
Dan Williamsa4456852007-07-09 11:56:43 -07003570 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003571 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003572 /* Prefer to use the replacement for reads, but only
3573 * if it is recovered enough and has no bad blocks.
3574 */
3575 rdev = rcu_dereference(conf->disks[i].replacement);
3576 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3577 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3578 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3579 &first_bad, &bad_sectors))
3580 set_bit(R5_ReadRepl, &dev->flags);
3581 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003582 if (rdev)
3583 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003584 rdev = rcu_dereference(conf->disks[i].rdev);
3585 clear_bit(R5_ReadRepl, &dev->flags);
3586 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003587 if (rdev && test_bit(Faulty, &rdev->flags))
3588 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003589 if (rdev) {
3590 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3591 &first_bad, &bad_sectors);
3592 if (s->blocked_rdev == NULL
3593 && (test_bit(Blocked, &rdev->flags)
3594 || is_bad < 0)) {
3595 if (is_bad < 0)
3596 set_bit(BlockedBadBlocks,
3597 &rdev->flags);
3598 s->blocked_rdev = rdev;
3599 atomic_inc(&rdev->nr_pending);
3600 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003601 }
NeilBrown415e72d2010-06-17 17:25:21 +10003602 clear_bit(R5_Insync, &dev->flags);
3603 if (!rdev)
3604 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003605 else if (is_bad) {
3606 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003607 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3608 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003609 /* treat as in-sync, but with a read error
3610 * which we can now try to correct
3611 */
3612 set_bit(R5_Insync, &dev->flags);
3613 set_bit(R5_ReadError, &dev->flags);
3614 }
3615 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003616 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003617 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003618 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003619 set_bit(R5_Insync, &dev->flags);
3620 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3621 test_bit(R5_Expanded, &dev->flags))
3622 /* If we've reshaped into here, we assume it is Insync.
3623 * We will shortly update recovery_offset to make
3624 * it official.
3625 */
3626 set_bit(R5_Insync, &dev->flags);
3627
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003628 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003629 /* This flag does not apply to '.replacement'
3630 * only to .rdev, so make sure to check that*/
3631 struct md_rdev *rdev2 = rcu_dereference(
3632 conf->disks[i].rdev);
3633 if (rdev2 == rdev)
3634 clear_bit(R5_Insync, &dev->flags);
3635 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003636 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003637 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003638 } else
3639 clear_bit(R5_WriteError, &dev->flags);
3640 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003641 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003642 /* This flag does not apply to '.replacement'
3643 * only to .rdev, so make sure to check that*/
3644 struct md_rdev *rdev2 = rcu_dereference(
3645 conf->disks[i].rdev);
3646 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003647 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003648 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003649 } else
3650 clear_bit(R5_MadeGood, &dev->flags);
3651 }
NeilBrown977df362011-12-23 10:17:53 +11003652 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3653 struct md_rdev *rdev2 = rcu_dereference(
3654 conf->disks[i].replacement);
3655 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3656 s->handle_bad_blocks = 1;
3657 atomic_inc(&rdev2->nr_pending);
3658 } else
3659 clear_bit(R5_MadeGoodRepl, &dev->flags);
3660 }
NeilBrown415e72d2010-06-17 17:25:21 +10003661 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003662 /* The ReadError flag will just be confusing now */
3663 clear_bit(R5_ReadError, &dev->flags);
3664 clear_bit(R5_ReWrite, &dev->flags);
3665 }
NeilBrown415e72d2010-06-17 17:25:21 +10003666 if (test_bit(R5_ReadError, &dev->flags))
3667 clear_bit(R5_Insync, &dev->flags);
3668 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003669 if (s->failed < 2)
3670 s->failed_num[s->failed] = i;
3671 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003672 if (rdev && !test_bit(Faulty, &rdev->flags))
3673 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003674 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003675 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003676 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3677 /* If there is a failed device being replaced,
3678 * we must be recovering.
3679 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003680 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003681 * else we can only be replacing
3682 * sync and recovery both need to read all devices, and so
3683 * use the same flag.
3684 */
3685 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003686 sh->sector >= conf->mddev->recovery_cp ||
3687 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003688 s->syncing = 1;
3689 else
3690 s->replacing = 1;
3691 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003692 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003693}
NeilBrownf4168852007-02-28 20:11:53 -08003694
NeilBrowncc940152011-07-26 11:35:35 +10003695static void handle_stripe(struct stripe_head *sh)
3696{
3697 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003698 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003699 int i;
NeilBrown84789552011-07-27 11:00:36 +10003700 int prexor;
3701 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003702 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003703
3704 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003705 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003706 /* already being handled, ensure it gets handled
3707 * again when current action finishes */
3708 set_bit(STRIPE_HANDLE, &sh->state);
3709 return;
3710 }
3711
NeilBrownf8dfcff2013-03-12 12:18:06 +11003712 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3713 spin_lock(&sh->stripe_lock);
3714 /* Cannot process 'sync' concurrently with 'discard' */
3715 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3716 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3717 set_bit(STRIPE_SYNCING, &sh->state);
3718 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003719 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003720 }
3721 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003722 }
3723 clear_bit(STRIPE_DELAYED, &sh->state);
3724
3725 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3726 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3727 (unsigned long long)sh->sector, sh->state,
3728 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3729 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003730
NeilBrownacfe7262011-07-27 11:00:36 +10003731 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003732
NeilBrownbc2607f2011-07-28 11:39:22 +10003733 if (s.handle_bad_blocks) {
3734 set_bit(STRIPE_HANDLE, &sh->state);
3735 goto finish;
3736 }
3737
NeilBrown474af965fe2011-07-27 11:00:36 +10003738 if (unlikely(s.blocked_rdev)) {
3739 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003740 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003741 set_bit(STRIPE_HANDLE, &sh->state);
3742 goto finish;
3743 }
3744 /* There is nothing for the blocked_rdev to block */
3745 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3746 s.blocked_rdev = NULL;
3747 }
3748
3749 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3750 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3751 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3752 }
3753
3754 pr_debug("locked=%d uptodate=%d to_read=%d"
3755 " to_write=%d failed=%d failed_num=%d,%d\n",
3756 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3757 s.failed_num[0], s.failed_num[1]);
3758 /* check if the array has lost more than max_degraded devices and,
3759 * if so, some requests might need to be failed.
3760 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003761 if (s.failed > conf->max_degraded) {
3762 sh->check_state = 0;
3763 sh->reconstruct_state = 0;
3764 if (s.to_read+s.to_write+s.written)
3765 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003766 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003767 handle_failed_sync(conf, sh, &s);
3768 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003769
NeilBrown84789552011-07-27 11:00:36 +10003770 /* Now we check to see if any write operations have recently
3771 * completed
3772 */
3773 prexor = 0;
3774 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3775 prexor = 1;
3776 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3777 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3778 sh->reconstruct_state = reconstruct_state_idle;
3779
3780 /* All the 'written' buffers and the parity block are ready to
3781 * be written back to disk
3782 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003783 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3784 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003785 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003786 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3787 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003788 for (i = disks; i--; ) {
3789 struct r5dev *dev = &sh->dev[i];
3790 if (test_bit(R5_LOCKED, &dev->flags) &&
3791 (i == sh->pd_idx || i == sh->qd_idx ||
3792 dev->written)) {
3793 pr_debug("Writing block %d\n", i);
3794 set_bit(R5_Wantwrite, &dev->flags);
3795 if (prexor)
3796 continue;
3797 if (!test_bit(R5_Insync, &dev->flags) ||
3798 ((i == sh->pd_idx || i == sh->qd_idx) &&
3799 s.failed == 0))
3800 set_bit(STRIPE_INSYNC, &sh->state);
3801 }
3802 }
3803 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3804 s.dec_preread_active = 1;
3805 }
3806
NeilBrownef5b7c62012-11-22 09:13:36 +11003807 /*
3808 * might be able to return some write requests if the parity blocks
3809 * are safe, or on a failed drive
3810 */
3811 pdev = &sh->dev[sh->pd_idx];
3812 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3813 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3814 qdev = &sh->dev[sh->qd_idx];
3815 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3816 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3817 || conf->level < 6;
3818
3819 if (s.written &&
3820 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3821 && !test_bit(R5_LOCKED, &pdev->flags)
3822 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3823 test_bit(R5_Discard, &pdev->flags))))) &&
3824 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3825 && !test_bit(R5_LOCKED, &qdev->flags)
3826 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3827 test_bit(R5_Discard, &qdev->flags))))))
3828 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3829
3830 /* Now we might consider reading some blocks, either to check/generate
3831 * parity, or to satisfy requests
3832 * or to load a block that is being partially written.
3833 */
3834 if (s.to_read || s.non_overwrite
3835 || (conf->level == 6 && s.to_write && s.failed)
3836 || (s.syncing && (s.uptodate + s.compute < disks))
3837 || s.replacing
3838 || s.expanding)
3839 handle_stripe_fill(sh, &s, disks);
3840
NeilBrown84789552011-07-27 11:00:36 +10003841 /* Now to consider new write requests and what else, if anything
3842 * should be read. We do not handle new writes when:
3843 * 1/ A 'write' operation (copy+xor) is already in flight.
3844 * 2/ A 'check' operation is in flight, as it may clobber the parity
3845 * block.
3846 */
3847 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3848 handle_stripe_dirtying(conf, sh, &s, disks);
3849
3850 /* maybe we need to check and possibly fix the parity for this stripe
3851 * Any reads will already have been scheduled, so we just see if enough
3852 * data is available. The parity check is held off while parity
3853 * dependent operations are in flight.
3854 */
3855 if (sh->check_state ||
3856 (s.syncing && s.locked == 0 &&
3857 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3858 !test_bit(STRIPE_INSYNC, &sh->state))) {
3859 if (conf->level == 6)
3860 handle_parity_checks6(conf, sh, &s, disks);
3861 else
3862 handle_parity_checks5(conf, sh, &s, disks);
3863 }
NeilBrownc5a31002011-07-27 11:00:36 +10003864
NeilBrownf94c0b62013-07-22 12:57:21 +10003865 if ((s.replacing || s.syncing) && s.locked == 0
3866 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3867 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003868 /* Write out to replacement devices where possible */
3869 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003870 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3871 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003872 set_bit(R5_WantReplace, &sh->dev[i].flags);
3873 set_bit(R5_LOCKED, &sh->dev[i].flags);
3874 s.locked++;
3875 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003876 if (s.replacing)
3877 set_bit(STRIPE_INSYNC, &sh->state);
3878 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003879 }
3880 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003881 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003882 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003883 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3884 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003885 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3886 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003887 }
3888
3889 /* If the failed drives are just a ReadError, then we might need
3890 * to progress the repair/check process
3891 */
3892 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3893 for (i = 0; i < s.failed; i++) {
3894 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3895 if (test_bit(R5_ReadError, &dev->flags)
3896 && !test_bit(R5_LOCKED, &dev->flags)
3897 && test_bit(R5_UPTODATE, &dev->flags)
3898 ) {
3899 if (!test_bit(R5_ReWrite, &dev->flags)) {
3900 set_bit(R5_Wantwrite, &dev->flags);
3901 set_bit(R5_ReWrite, &dev->flags);
3902 set_bit(R5_LOCKED, &dev->flags);
3903 s.locked++;
3904 } else {
3905 /* let's read it back */
3906 set_bit(R5_Wantread, &dev->flags);
3907 set_bit(R5_LOCKED, &dev->flags);
3908 s.locked++;
3909 }
3910 }
3911 }
3912
3913
NeilBrown3687c062011-07-27 11:00:36 +10003914 /* Finish reconstruct operations initiated by the expansion process */
3915 if (sh->reconstruct_state == reconstruct_state_result) {
3916 struct stripe_head *sh_src
3917 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3918 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3919 /* sh cannot be written until sh_src has been read.
3920 * so arrange for sh to be delayed a little
3921 */
3922 set_bit(STRIPE_DELAYED, &sh->state);
3923 set_bit(STRIPE_HANDLE, &sh->state);
3924 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3925 &sh_src->state))
3926 atomic_inc(&conf->preread_active_stripes);
3927 release_stripe(sh_src);
3928 goto finish;
3929 }
3930 if (sh_src)
3931 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003932
NeilBrown3687c062011-07-27 11:00:36 +10003933 sh->reconstruct_state = reconstruct_state_idle;
3934 clear_bit(STRIPE_EXPANDING, &sh->state);
3935 for (i = conf->raid_disks; i--; ) {
3936 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3937 set_bit(R5_LOCKED, &sh->dev[i].flags);
3938 s.locked++;
3939 }
3940 }
3941
3942 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3943 !sh->reconstruct_state) {
3944 /* Need to write out all blocks after computing parity */
3945 sh->disks = conf->raid_disks;
3946 stripe_set_idx(sh->sector, conf, 0, sh);
3947 schedule_reconstruction(sh, &s, 1, 1);
3948 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3949 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3950 atomic_dec(&conf->reshape_stripes);
3951 wake_up(&conf->wait_for_overlap);
3952 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3953 }
3954
3955 if (s.expanding && s.locked == 0 &&
3956 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3957 handle_stripe_expansion(conf, sh);
3958
3959finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003960 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003961 if (unlikely(s.blocked_rdev)) {
3962 if (conf->mddev->external)
3963 md_wait_for_blocked_rdev(s.blocked_rdev,
3964 conf->mddev);
3965 else
3966 /* Internal metadata will immediately
3967 * be written by raid5d, so we don't
3968 * need to wait here.
3969 */
3970 rdev_dec_pending(s.blocked_rdev,
3971 conf->mddev);
3972 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003973
NeilBrownbc2607f2011-07-28 11:39:22 +10003974 if (s.handle_bad_blocks)
3975 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003976 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003977 struct r5dev *dev = &sh->dev[i];
3978 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3979 /* We own a safe reference to the rdev */
3980 rdev = conf->disks[i].rdev;
3981 if (!rdev_set_badblocks(rdev, sh->sector,
3982 STRIPE_SECTORS, 0))
3983 md_error(conf->mddev, rdev);
3984 rdev_dec_pending(rdev, conf->mddev);
3985 }
NeilBrownb84db562011-07-28 11:39:23 +10003986 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3987 rdev = conf->disks[i].rdev;
3988 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003989 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003990 rdev_dec_pending(rdev, conf->mddev);
3991 }
NeilBrown977df362011-12-23 10:17:53 +11003992 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3993 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003994 if (!rdev)
3995 /* rdev have been moved down */
3996 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003997 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003998 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003999 rdev_dec_pending(rdev, conf->mddev);
4000 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004001 }
4002
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004003 if (s.ops_request)
4004 raid_run_ops(sh, s.ops_request);
4005
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004006 ops_run_io(sh, &s);
4007
NeilBrownc5709ef2011-07-26 11:35:20 +10004008 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004009 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004010 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004011 * have actually been submitted.
4012 */
4013 atomic_dec(&conf->preread_active_stripes);
4014 if (atomic_read(&conf->preread_active_stripes) <
4015 IO_THRESHOLD)
4016 md_wakeup_thread(conf->mddev->thread);
4017 }
4018
NeilBrownc5709ef2011-07-26 11:35:20 +10004019 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07004020
Dan Williams257a4b42011-11-08 16:22:06 +11004021 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004022}
4023
NeilBrownd1688a62011-10-11 16:49:52 +11004024static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4027 while (!list_empty(&conf->delayed_list)) {
4028 struct list_head *l = conf->delayed_list.next;
4029 struct stripe_head *sh;
4030 sh = list_entry(l, struct stripe_head, lru);
4031 list_del_init(l);
4032 clear_bit(STRIPE_DELAYED, &sh->state);
4033 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4034 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004035 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004036 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
NeilBrown482c0832011-04-18 18:25:42 +10004038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Shaohua Li566c09c2013-11-14 15:16:17 +11004041static void activate_bit_delay(struct r5conf *conf,
4042 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004043{
4044 /* device_lock is held */
4045 struct list_head head;
4046 list_add(&head, &conf->bitmap_list);
4047 list_del_init(&conf->bitmap_list);
4048 while (!list_empty(&head)) {
4049 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004050 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004051 list_del_init(&sh->lru);
4052 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004053 hash = sh->hash_lock_index;
4054 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004055 }
4056}
4057
NeilBrownfd01b882011-10-11 16:47:53 +11004058int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004059{
NeilBrownd1688a62011-10-11 16:49:52 +11004060 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004061
4062 /* No difference between reads and writes. Just check
4063 * how busy the stripe_cache is
4064 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004065
NeilBrownf022b2f2006-10-03 01:15:56 -07004066 if (conf->inactive_blocked)
4067 return 1;
4068 if (conf->quiesce)
4069 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004070 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004071 return 1;
4072
4073 return 0;
4074}
NeilBrown11d8a6e2010-07-26 11:57:07 +10004075EXPORT_SYMBOL_GPL(md_raid5_congested);
4076
4077static int raid5_congested(void *data, int bits)
4078{
NeilBrownfd01b882011-10-11 16:47:53 +11004079 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10004080
4081 return mddev_congested(mddev, bits) ||
4082 md_raid5_congested(mddev, bits);
4083}
NeilBrownf022b2f2006-10-03 01:15:56 -07004084
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004085/* We want read requests to align with chunks where possible,
4086 * but write requests don't need to.
4087 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004088static int raid5_mergeable_bvec(struct request_queue *q,
4089 struct bvec_merge_data *bvm,
4090 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004091{
NeilBrownfd01b882011-10-11 16:47:53 +11004092 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004093 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004094 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004095 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004096 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004097
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004098 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004099 return biovec->bv_len; /* always allow writes to be mergeable */
4100
Andre Noll664e7c42009-06-18 08:45:27 +10004101 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4102 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004103 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4104 if (max < 0) max = 0;
4105 if (max <= biovec->bv_len && bio_sectors == 0)
4106 return biovec->bv_len;
4107 else
4108 return max;
4109}
4110
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004111
NeilBrownfd01b882011-10-11 16:47:53 +11004112static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004113{
4114 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10004115 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004116 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004117
Andre Noll664e7c42009-06-18 08:45:27 +10004118 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4119 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004120 return chunk_sectors >=
4121 ((sector & (chunk_sectors - 1)) + bio_sectors);
4122}
4123
4124/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004125 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4126 * later sampled by raid5d.
4127 */
NeilBrownd1688a62011-10-11 16:49:52 +11004128static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004129{
4130 unsigned long flags;
4131
4132 spin_lock_irqsave(&conf->device_lock, flags);
4133
4134 bi->bi_next = conf->retry_read_aligned_list;
4135 conf->retry_read_aligned_list = bi;
4136
4137 spin_unlock_irqrestore(&conf->device_lock, flags);
4138 md_wakeup_thread(conf->mddev->thread);
4139}
4140
4141
NeilBrownd1688a62011-10-11 16:49:52 +11004142static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004143{
4144 struct bio *bi;
4145
4146 bi = conf->retry_read_aligned;
4147 if (bi) {
4148 conf->retry_read_aligned = NULL;
4149 return bi;
4150 }
4151 bi = conf->retry_read_aligned_list;
4152 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004153 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004154 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004155 /*
4156 * this sets the active strip count to 1 and the processed
4157 * strip count to zero (upper 8 bits)
4158 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004159 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004160 }
4161
4162 return bi;
4163}
4164
4165
4166/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004167 * The "raid5_align_endio" should check if the read succeeded and if it
4168 * did, call bio_endio on the original bio (having bio_put the new bio
4169 * first).
4170 * If the read failed..
4171 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004172static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004173{
4174 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004175 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004176 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004177 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004178 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004179
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004180 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004181
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004182 rdev = (void*)raid_bi->bi_next;
4183 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004184 mddev = rdev->mddev;
4185 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004186
4187 rdev_dec_pending(rdev, conf->mddev);
4188
4189 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004190 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4191 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004192 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004193 if (atomic_dec_and_test(&conf->active_aligned_reads))
4194 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004195 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004196 }
4197
4198
Dan Williams45b42332007-07-09 11:56:43 -07004199 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004200
4201 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004202}
4203
Neil Brown387bb172007-02-08 14:20:29 -08004204static int bio_fits_rdev(struct bio *bi)
4205{
Jens Axboe165125e2007-07-24 09:28:11 +02004206 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004207
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004208 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004209 return 0;
4210 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004211 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004212 return 0;
4213
4214 if (q->merge_bvec_fn)
4215 /* it's too hard to apply the merge_bvec_fn at this stage,
4216 * just just give up
4217 */
4218 return 0;
4219
4220 return 1;
4221}
4222
4223
NeilBrownfd01b882011-10-11 16:47:53 +11004224static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004225{
NeilBrownd1688a62011-10-11 16:49:52 +11004226 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004227 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004228 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004229 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004230 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004231
4232 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004233 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004234 return 0;
4235 }
4236 /*
NeilBrowna167f662010-10-26 18:31:13 +11004237 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004238 */
NeilBrowna167f662010-10-26 18:31:13 +11004239 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004240 if (!align_bi)
4241 return 0;
4242 /*
4243 * set bi_end_io to a new function, and set bi_private to the
4244 * original bio.
4245 */
4246 align_bi->bi_end_io = raid5_align_endio;
4247 align_bi->bi_private = raid_bio;
4248 /*
4249 * compute position
4250 */
NeilBrown112bf892009-03-31 14:39:38 +11004251 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4252 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004253 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004254
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004255 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004256 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004257 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4258 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4259 rdev->recovery_offset < end_sector) {
4260 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4261 if (rdev &&
4262 (test_bit(Faulty, &rdev->flags) ||
4263 !(test_bit(In_sync, &rdev->flags) ||
4264 rdev->recovery_offset >= end_sector)))
4265 rdev = NULL;
4266 }
4267 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004268 sector_t first_bad;
4269 int bad_sectors;
4270
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004271 atomic_inc(&rdev->nr_pending);
4272 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004273 raid_bio->bi_next = (void*)rdev;
4274 align_bi->bi_bdev = rdev->bdev;
4275 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004276
NeilBrown31c176e2011-07-28 11:39:22 +10004277 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004278 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004279 &first_bad, &bad_sectors)) {
4280 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004281 bio_put(align_bi);
4282 rdev_dec_pending(rdev, mddev);
4283 return 0;
4284 }
4285
majianpeng6c0544e2012-06-12 08:31:10 +08004286 /* No reshape active, so we can trust rdev->data_offset */
4287 align_bi->bi_sector += rdev->data_offset;
4288
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004289 spin_lock_irq(&conf->device_lock);
4290 wait_event_lock_irq(conf->wait_for_stripe,
4291 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004292 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004293 atomic_inc(&conf->active_aligned_reads);
4294 spin_unlock_irq(&conf->device_lock);
4295
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004296 if (mddev->gendisk)
4297 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4298 align_bi, disk_devt(mddev->gendisk),
4299 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004300 generic_make_request(align_bi);
4301 return 1;
4302 } else {
4303 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004304 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004305 return 0;
4306 }
4307}
4308
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004309/* __get_priority_stripe - get the next stripe to process
4310 *
4311 * Full stripe writes are allowed to pass preread active stripes up until
4312 * the bypass_threshold is exceeded. In general the bypass_count
4313 * increments when the handle_list is handled before the hold_list; however, it
4314 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4315 * stripe with in flight i/o. The bypass_count will be reset when the
4316 * head of the hold_list has changed, i.e. the head was promoted to the
4317 * handle_list.
4318 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004319static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004320{
Shaohua Li851c30c2013-08-28 14:30:16 +08004321 struct stripe_head *sh = NULL, *tmp;
4322 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004323 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004324
4325 if (conf->worker_cnt_per_group == 0) {
4326 handle_list = &conf->handle_list;
4327 } else if (group != ANY_GROUP) {
4328 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004329 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004330 } else {
4331 int i;
4332 for (i = 0; i < conf->group_cnt; i++) {
4333 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004334 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004335 if (!list_empty(handle_list))
4336 break;
4337 }
4338 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004339
4340 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4341 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004342 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004343 list_empty(&conf->hold_list) ? "empty" : "busy",
4344 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4345
Shaohua Li851c30c2013-08-28 14:30:16 +08004346 if (!list_empty(handle_list)) {
4347 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004348
4349 if (list_empty(&conf->hold_list))
4350 conf->bypass_count = 0;
4351 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4352 if (conf->hold_list.next == conf->last_hold)
4353 conf->bypass_count++;
4354 else {
4355 conf->last_hold = conf->hold_list.next;
4356 conf->bypass_count -= conf->bypass_threshold;
4357 if (conf->bypass_count < 0)
4358 conf->bypass_count = 0;
4359 }
4360 }
4361 } else if (!list_empty(&conf->hold_list) &&
4362 ((conf->bypass_threshold &&
4363 conf->bypass_count > conf->bypass_threshold) ||
4364 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004365
4366 list_for_each_entry(tmp, &conf->hold_list, lru) {
4367 if (conf->worker_cnt_per_group == 0 ||
4368 group == ANY_GROUP ||
4369 !cpu_online(tmp->cpu) ||
4370 cpu_to_group(tmp->cpu) == group) {
4371 sh = tmp;
4372 break;
4373 }
4374 }
4375
4376 if (sh) {
4377 conf->bypass_count -= conf->bypass_threshold;
4378 if (conf->bypass_count < 0)
4379 conf->bypass_count = 0;
4380 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004381 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004382 }
4383
4384 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004385 return NULL;
4386
Shaohua Libfc90cb2013-08-29 15:40:32 +08004387 if (wg) {
4388 wg->stripes_cnt--;
4389 sh->group = NULL;
4390 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004391 list_del_init(&sh->lru);
4392 atomic_inc(&sh->count);
4393 BUG_ON(atomic_read(&sh->count) != 1);
4394 return sh;
4395}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004396
Shaohua Li8811b592012-08-02 08:33:00 +10004397struct raid5_plug_cb {
4398 struct blk_plug_cb cb;
4399 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004400 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004401};
4402
4403static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4404{
4405 struct raid5_plug_cb *cb = container_of(
4406 blk_cb, struct raid5_plug_cb, cb);
4407 struct stripe_head *sh;
4408 struct mddev *mddev = cb->cb.data;
4409 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004410 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004411 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004412
4413 if (cb->list.next && !list_empty(&cb->list)) {
4414 spin_lock_irq(&conf->device_lock);
4415 while (!list_empty(&cb->list)) {
4416 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4417 list_del_init(&sh->lru);
4418 /*
4419 * avoid race release_stripe_plug() sees
4420 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4421 * is still in our list
4422 */
4423 smp_mb__before_clear_bit();
4424 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004425 /*
4426 * STRIPE_ON_RELEASE_LIST could be set here. In that
4427 * case, the count is always > 1 here
4428 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004429 hash = sh->hash_lock_index;
4430 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004431 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004432 }
4433 spin_unlock_irq(&conf->device_lock);
4434 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004435 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4436 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004437 if (mddev->queue)
4438 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004439 kfree(cb);
4440}
4441
4442static void release_stripe_plug(struct mddev *mddev,
4443 struct stripe_head *sh)
4444{
4445 struct blk_plug_cb *blk_cb = blk_check_plugged(
4446 raid5_unplug, mddev,
4447 sizeof(struct raid5_plug_cb));
4448 struct raid5_plug_cb *cb;
4449
4450 if (!blk_cb) {
4451 release_stripe(sh);
4452 return;
4453 }
4454
4455 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4456
Shaohua Li566c09c2013-11-14 15:16:17 +11004457 if (cb->list.next == NULL) {
4458 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10004459 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11004460 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4461 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4462 }
Shaohua Li8811b592012-08-02 08:33:00 +10004463
4464 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4465 list_add_tail(&sh->lru, &cb->list);
4466 else
4467 release_stripe(sh);
4468}
4469
Shaohua Li620125f2012-10-11 13:49:05 +11004470static void make_discard_request(struct mddev *mddev, struct bio *bi)
4471{
4472 struct r5conf *conf = mddev->private;
4473 sector_t logical_sector, last_sector;
4474 struct stripe_head *sh;
4475 int remaining;
4476 int stripe_sectors;
4477
4478 if (mddev->reshape_position != MaxSector)
4479 /* Skip discard while reshape is happening */
4480 return;
4481
4482 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4483 last_sector = bi->bi_sector + (bi->bi_size>>9);
4484
4485 bi->bi_next = NULL;
4486 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4487
4488 stripe_sectors = conf->chunk_sectors *
4489 (conf->raid_disks - conf->max_degraded);
4490 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4491 stripe_sectors);
4492 sector_div(last_sector, stripe_sectors);
4493
4494 logical_sector *= conf->chunk_sectors;
4495 last_sector *= conf->chunk_sectors;
4496
4497 for (; logical_sector < last_sector;
4498 logical_sector += STRIPE_SECTORS) {
4499 DEFINE_WAIT(w);
4500 int d;
4501 again:
4502 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4503 prepare_to_wait(&conf->wait_for_overlap, &w,
4504 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004505 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4506 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4507 release_stripe(sh);
4508 schedule();
4509 goto again;
4510 }
4511 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004512 spin_lock_irq(&sh->stripe_lock);
4513 for (d = 0; d < conf->raid_disks; d++) {
4514 if (d == sh->pd_idx || d == sh->qd_idx)
4515 continue;
4516 if (sh->dev[d].towrite || sh->dev[d].toread) {
4517 set_bit(R5_Overlap, &sh->dev[d].flags);
4518 spin_unlock_irq(&sh->stripe_lock);
4519 release_stripe(sh);
4520 schedule();
4521 goto again;
4522 }
4523 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004524 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004525 finish_wait(&conf->wait_for_overlap, &w);
4526 for (d = 0; d < conf->raid_disks; d++) {
4527 if (d == sh->pd_idx || d == sh->qd_idx)
4528 continue;
4529 sh->dev[d].towrite = bi;
4530 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4531 raid5_inc_bi_active_stripes(bi);
4532 }
4533 spin_unlock_irq(&sh->stripe_lock);
4534 if (conf->mddev->bitmap) {
4535 for (d = 0;
4536 d < conf->raid_disks - conf->max_degraded;
4537 d++)
4538 bitmap_startwrite(mddev->bitmap,
4539 sh->sector,
4540 STRIPE_SECTORS,
4541 0);
4542 sh->bm_seq = conf->seq_flush + 1;
4543 set_bit(STRIPE_BIT_DELAY, &sh->state);
4544 }
4545
4546 set_bit(STRIPE_HANDLE, &sh->state);
4547 clear_bit(STRIPE_DELAYED, &sh->state);
4548 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4549 atomic_inc(&conf->preread_active_stripes);
4550 release_stripe_plug(mddev, sh);
4551 }
4552
4553 remaining = raid5_dec_bi_active_stripes(bi);
4554 if (remaining == 0) {
4555 md_write_end(mddev);
4556 bio_endio(bi, 0);
4557 }
4558}
4559
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004560static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
NeilBrownd1688a62011-10-11 16:49:52 +11004562 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004563 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 sector_t new_sector;
4565 sector_t logical_sector, last_sector;
4566 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004567 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004568 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Tejun Heoe9c74692010-09-03 11:56:18 +02004570 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4571 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004572 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004573 }
4574
NeilBrown3d310eb2005-06-21 17:17:26 -07004575 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004576
NeilBrown802ba062006-12-13 00:34:13 -08004577 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004578 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004579 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004580 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004581
Shaohua Li620125f2012-10-11 13:49:05 +11004582 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4583 make_discard_request(mddev, bi);
4584 return;
4585 }
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004588 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 bi->bi_next = NULL;
4590 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4593 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004594 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10004595 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08004596
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004597 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10004598 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11004599 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004600 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004601 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004602 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004603 * 64bit on a 32bit platform, and so it might be
4604 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004605 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004606 * the lock is dropped, so once we get a reference
4607 * to the stripe that we think it is, we will have
4608 * to check again.
4609 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004610 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004611 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004612 ? logical_sector < conf->reshape_progress
4613 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004614 previous = 1;
4615 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004616 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004617 ? logical_sector < conf->reshape_safe
4618 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004619 spin_unlock_irq(&conf->device_lock);
4620 schedule();
4621 goto retry;
4622 }
4623 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004624 spin_unlock_irq(&conf->device_lock);
4625 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004626
NeilBrown112bf892009-03-31 14:39:38 +11004627 new_sector = raid5_compute_sector(conf, logical_sector,
4628 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004629 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004630 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10004631 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 (unsigned long long)logical_sector);
4633
NeilBrownb5663ba2009-03-31 14:39:38 +11004634 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004635 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004637 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004638 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004639 * stripe, so we must do the range check again.
4640 * Expansion could still move past after this
4641 * test, but as we are holding a reference to
4642 * 'sh', we know that if that happens,
4643 * STRIPE_EXPANDING will get set and the expansion
4644 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004645 */
4646 int must_retry = 0;
4647 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004648 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004649 ? logical_sector >= conf->reshape_progress
4650 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004651 /* mismatch, need to try again */
4652 must_retry = 1;
4653 spin_unlock_irq(&conf->device_lock);
4654 if (must_retry) {
4655 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004656 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004657 goto retry;
4658 }
4659 }
NeilBrownc46501b2013-08-27 15:52:13 +10004660 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4661 /* Might have got the wrong stripe_head
4662 * by accident
4663 */
4664 release_stripe(sh);
4665 goto retry;
4666 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004667
Namhyung Kimffd96e32011-07-18 17:38:51 +10004668 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004669 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004670 logical_sector < mddev->suspend_hi) {
4671 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004672 /* As the suspend_* range is controlled by
4673 * userspace, we want an interruptible
4674 * wait.
4675 */
4676 flush_signals(current);
4677 prepare_to_wait(&conf->wait_for_overlap,
4678 &w, TASK_INTERRUPTIBLE);
4679 if (logical_sector >= mddev->suspend_lo &&
4680 logical_sector < mddev->suspend_hi)
4681 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004682 goto retry;
4683 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004684
4685 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004686 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004687 /* Stripe is busy expanding or
4688 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 * and wait a while
4690 */
NeilBrown482c0832011-04-18 18:25:42 +10004691 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 release_stripe(sh);
4693 schedule();
4694 goto retry;
4695 }
4696 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004697 set_bit(STRIPE_HANDLE, &sh->state);
4698 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004699 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004700 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4701 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004702 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 } else {
4704 /* cannot get stripe for read-ahead, just give-up */
4705 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4706 finish_wait(&conf->wait_for_overlap, &w);
4707 break;
4708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004710
Shaohua Lie7836bd62012-07-19 16:01:31 +10004711 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004712 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
NeilBrown16a53ec2006-06-26 00:27:38 -07004714 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004716
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004717 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4718 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004719 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
NeilBrownfd01b882011-10-11 16:47:53 +11004723static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004724
NeilBrownfd01b882011-10-11 16:47:53 +11004725static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
NeilBrown52c03292006-06-26 00:27:43 -07004727 /* reshaping is quite different to recovery/resync so it is
4728 * handled quite separately ... here.
4729 *
4730 * On each call to sync_request, we gather one chunk worth of
4731 * destination stripes and flag them as expanding.
4732 * Then we find all the source stripes and request reads.
4733 * As the reads complete, handle_stripe will copy the data
4734 * into the destination stripe and release that stripe.
4735 */
NeilBrownd1688a62011-10-11 16:49:52 +11004736 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004738 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004739 int raid_disks = conf->previous_raid_disks;
4740 int data_disks = raid_disks - conf->max_degraded;
4741 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004742 int i;
4743 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004744 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004745 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004746 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004747 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004748
NeilBrownfef9c612009-03-31 15:16:46 +11004749 if (sector_nr == 0) {
4750 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004751 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004752 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4753 sector_nr = raid5_size(mddev, 0, 0)
4754 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004755 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004756 conf->reshape_progress > 0)
4757 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004758 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004759 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004760 mddev->curr_resync_completed = sector_nr;
4761 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004762 *skipped = 1;
4763 return sector_nr;
4764 }
NeilBrown52c03292006-06-26 00:27:43 -07004765 }
4766
NeilBrown7a661382009-03-31 15:21:40 +11004767 /* We need to process a full chunk at a time.
4768 * If old and new chunk sizes differ, we need to process the
4769 * largest of these
4770 */
Andre Noll664e7c42009-06-18 08:45:27 +10004771 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4772 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004773 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004774 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004775
NeilBrownb5254dd2012-05-21 09:27:01 +10004776 /* We update the metadata at least every 10 seconds, or when
4777 * the data about to be copied would over-write the source of
4778 * the data at the front of the range. i.e. one new_stripe
4779 * along from reshape_progress new_maps to after where
4780 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004781 */
NeilBrownfef9c612009-03-31 15:16:46 +11004782 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004783 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004784 readpos = conf->reshape_progress;
4785 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004786 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004787 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004788 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004789 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004790 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004791 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004792 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004793 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004794 readpos -= min_t(sector_t, reshape_sectors, readpos);
4795 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004796 }
NeilBrown52c03292006-06-26 00:27:43 -07004797
NeilBrownb5254dd2012-05-21 09:27:01 +10004798 /* Having calculated the 'writepos' possibly use it
4799 * to set 'stripe_addr' which is where we will write to.
4800 */
4801 if (mddev->reshape_backwards) {
4802 BUG_ON(conf->reshape_progress == 0);
4803 stripe_addr = writepos;
4804 BUG_ON((mddev->dev_sectors &
4805 ~((sector_t)reshape_sectors - 1))
4806 - reshape_sectors - stripe_addr
4807 != sector_nr);
4808 } else {
4809 BUG_ON(writepos != sector_nr + reshape_sectors);
4810 stripe_addr = sector_nr;
4811 }
4812
NeilBrownc8f517c2009-03-31 15:28:40 +11004813 /* 'writepos' is the most advanced device address we might write.
4814 * 'readpos' is the least advanced device address we might read.
4815 * 'safepos' is the least address recorded in the metadata as having
4816 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004817 * If there is a min_offset_diff, these are adjusted either by
4818 * increasing the safepos/readpos if diff is negative, or
4819 * increasing writepos if diff is positive.
4820 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004821 * ensure safety in the face of a crash - that must be done by userspace
4822 * making a backup of the data. So in that case there is no particular
4823 * rush to update metadata.
4824 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4825 * update the metadata to advance 'safepos' to match 'readpos' so that
4826 * we can be safe in the event of a crash.
4827 * So we insist on updating metadata if safepos is behind writepos and
4828 * readpos is beyond writepos.
4829 * In any case, update the metadata every 10 seconds.
4830 * Maybe that number should be configurable, but I'm not sure it is
4831 * worth it.... maybe it could be a multiple of safemode_delay???
4832 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004833 if (conf->min_offset_diff < 0) {
4834 safepos += -conf->min_offset_diff;
4835 readpos += -conf->min_offset_diff;
4836 } else
4837 writepos += conf->min_offset_diff;
4838
NeilBrown2c810cd2012-05-21 09:27:00 +10004839 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004840 ? (safepos > writepos && readpos < writepos)
4841 : (safepos < writepos && readpos > writepos)) ||
4842 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004843 /* Cannot proceed until we've updated the superblock... */
4844 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004845 atomic_read(&conf->reshape_stripes)==0
4846 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4847 if (atomic_read(&conf->reshape_stripes) != 0)
4848 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11004849 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004850 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004851 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004852 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004853 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004854 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004855 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4856 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4857 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07004858 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004859 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004860 spin_unlock_irq(&conf->device_lock);
4861 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004862 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004863 }
4864
NeilBrownab69ae12009-03-31 15:26:47 +11004865 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004866 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004867 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004868 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004869 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004870 set_bit(STRIPE_EXPANDING, &sh->state);
4871 atomic_inc(&conf->reshape_stripes);
4872 /* If any of this stripe is beyond the end of the old
4873 * array, then we need to zero those blocks
4874 */
4875 for (j=sh->disks; j--;) {
4876 sector_t s;
4877 if (j == sh->pd_idx)
4878 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004879 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004880 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004881 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004882 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004883 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004884 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004885 continue;
4886 }
4887 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4888 set_bit(R5_Expanded, &sh->dev[j].flags);
4889 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4890 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004891 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004892 set_bit(STRIPE_EXPAND_READY, &sh->state);
4893 set_bit(STRIPE_HANDLE, &sh->state);
4894 }
NeilBrownab69ae12009-03-31 15:26:47 +11004895 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004896 }
4897 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004898 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004899 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004900 else
NeilBrown7a661382009-03-31 15:21:40 +11004901 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004902 spin_unlock_irq(&conf->device_lock);
4903 /* Ok, those stripe are ready. We can start scheduling
4904 * reads on the source stripes.
4905 * The source stripes are determined by mapping the first and last
4906 * block on the destination stripes.
4907 */
NeilBrown52c03292006-06-26 00:27:43 -07004908 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004909 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004910 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004911 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004912 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004913 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004914 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004915 if (last_sector >= mddev->dev_sectors)
4916 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004917 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004918 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004919 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4920 set_bit(STRIPE_HANDLE, &sh->state);
4921 release_stripe(sh);
4922 first_sector += STRIPE_SECTORS;
4923 }
NeilBrownab69ae12009-03-31 15:26:47 +11004924 /* Now that the sources are clearly marked, we can release
4925 * the destination stripes
4926 */
4927 while (!list_empty(&stripes)) {
4928 sh = list_entry(stripes.next, struct stripe_head, lru);
4929 list_del_init(&sh->lru);
4930 release_stripe(sh);
4931 }
NeilBrownc6207272008-02-06 01:39:52 -08004932 /* If this takes us to the resync_max point where we have to pause,
4933 * then we need to write out the superblock.
4934 */
NeilBrown7a661382009-03-31 15:21:40 +11004935 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004936 if ((sector_nr - mddev->curr_resync_completed) * 2
4937 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004938 /* Cannot proceed until we've updated the superblock... */
4939 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004940 atomic_read(&conf->reshape_stripes) == 0
4941 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4942 if (atomic_read(&conf->reshape_stripes) != 0)
4943 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11004944 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004945 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004946 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004947 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4948 md_wakeup_thread(mddev->thread);
4949 wait_event(mddev->sb_wait,
4950 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11004951 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4952 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4953 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08004954 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004955 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004956 spin_unlock_irq(&conf->device_lock);
4957 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004958 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004959 }
NeilBrownc91abf52013-11-19 12:02:01 +11004960ret:
NeilBrown7a661382009-03-31 15:21:40 +11004961 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004962}
4963
4964/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004965static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004966{
NeilBrownd1688a62011-10-11 16:49:52 +11004967 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004968 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004969 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004970 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004971 int still_degraded = 0;
4972 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
NeilBrown72626682005-09-09 16:23:54 -07004974 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004976
NeilBrown29269552006-03-27 01:18:10 -08004977 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4978 end_reshape(conf);
4979 return 0;
4980 }
NeilBrown72626682005-09-09 16:23:54 -07004981
4982 if (mddev->curr_resync < max_sector) /* aborted */
4983 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4984 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004985 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004986 conf->fullsync = 0;
4987 bitmap_close_sync(mddev->bitmap);
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 return 0;
4990 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004991
NeilBrown64bd6602009-08-03 10:59:58 +10004992 /* Allow raid5_quiesce to complete */
4993 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4994
NeilBrown52c03292006-06-26 00:27:43 -07004995 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4996 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004997
NeilBrownc6207272008-02-06 01:39:52 -08004998 /* No need to check resync_max as we never do more than one
4999 * stripe, and as resync_max will always be on a chunk boundary,
5000 * if the check in md_do_sync didn't fire, there is no chance
5001 * of overstepping resync_max here
5002 */
5003
NeilBrown16a53ec2006-06-26 00:27:38 -07005004 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 * to resync, then assert that we are finished, because there is
5006 * nothing we can do.
5007 */
NeilBrown3285edf2006-06-26 00:27:55 -07005008 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005009 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005010 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005011 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return rv;
5013 }
majianpeng6f608042013-04-24 11:42:41 +10005014 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5015 !conf->fullsync &&
5016 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5017 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005018 /* we can skip this block, and probably more */
5019 sync_blocks /= STRIPE_SECTORS;
5020 *skipped = 1;
5021 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
NeilBrownb47490c2008-02-06 01:39:50 -08005024 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5025
NeilBrowna8c906c2009-06-09 14:39:59 +10005026 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005028 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005030 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005032 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005034 /* Need to check if array will still be degraded after recovery/resync
5035 * We don't need to check the 'failed' flag as when that gets set,
5036 * recovery aborts.
5037 */
NeilBrownf001a702009-06-09 14:30:31 +10005038 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07005039 if (conf->disks[i].rdev == NULL)
5040 still_degraded = 1;
5041
5042 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5043
NeilBrown83206d62011-07-26 11:19:49 +10005044 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
NeilBrown14425772009-10-16 15:55:25 +11005046 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 release_stripe(sh);
5048
5049 return STRIPE_SECTORS;
5050}
5051
NeilBrownd1688a62011-10-11 16:49:52 +11005052static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005053{
5054 /* We may not be able to submit a whole bio at once as there
5055 * may not be enough stripe_heads available.
5056 * We cannot pre-allocate enough stripe_heads as we may need
5057 * more than exist in the cache (if we allow ever large chunks).
5058 * So we do one stripe head at a time and record in
5059 * ->bi_hw_segments how many have been done.
5060 *
5061 * We *know* that this entire raid_bio is in one chunk, so
5062 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5063 */
5064 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005065 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005066 sector_t sector, logical_sector, last_sector;
5067 int scnt = 0;
5068 int remaining;
5069 int handled = 0;
5070
5071 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005072 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005073 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005074 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005075
5076 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005077 logical_sector += STRIPE_SECTORS,
5078 sector += STRIPE_SECTORS,
5079 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005080
Shaohua Lie7836bd62012-07-19 16:01:31 +10005081 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005082 /* already done this stripe */
5083 continue;
5084
NeilBrowna8c906c2009-06-09 14:39:59 +10005085 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005086
5087 if (!sh) {
5088 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005089 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005090 conf->retry_read_aligned = raid_bio;
5091 return handled;
5092 }
5093
Neil Brown387bb172007-02-08 14:20:29 -08005094 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
5095 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005096 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005097 conf->retry_read_aligned = raid_bio;
5098 return handled;
5099 }
5100
majianpeng3f9e7c12012-07-31 10:04:21 +10005101 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005102 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005103 release_stripe(sh);
5104 handled++;
5105 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005106 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005107 if (remaining == 0) {
5108 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5109 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005110 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005111 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005112 if (atomic_dec_and_test(&conf->active_aligned_reads))
5113 wake_up(&conf->wait_for_stripe);
5114 return handled;
5115}
5116
Shaohua Libfc90cb2013-08-29 15:40:32 +08005117static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005118 struct r5worker *worker,
5119 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005120{
5121 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005122 int i, batch_size = 0, hash;
5123 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005124
5125 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005126 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005127 batch[batch_size++] = sh;
5128
Shaohua Li566c09c2013-11-14 15:16:17 +11005129 if (batch_size == 0) {
5130 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5131 if (!list_empty(temp_inactive_list + i))
5132 break;
5133 if (i == NR_STRIPE_HASH_LOCKS)
5134 return batch_size;
5135 release_inactive = true;
5136 }
Shaohua Li46a06402012-08-02 08:33:15 +10005137 spin_unlock_irq(&conf->device_lock);
5138
Shaohua Li566c09c2013-11-14 15:16:17 +11005139 release_inactive_stripe_list(conf, temp_inactive_list,
5140 NR_STRIPE_HASH_LOCKS);
5141
5142 if (release_inactive) {
5143 spin_lock_irq(&conf->device_lock);
5144 return 0;
5145 }
5146
Shaohua Li46a06402012-08-02 08:33:15 +10005147 for (i = 0; i < batch_size; i++)
5148 handle_stripe(batch[i]);
5149
5150 cond_resched();
5151
5152 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005153 for (i = 0; i < batch_size; i++) {
5154 hash = batch[i]->hash_lock_index;
5155 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5156 }
Shaohua Li46a06402012-08-02 08:33:15 +10005157 return batch_size;
5158}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005159
Shaohua Li851c30c2013-08-28 14:30:16 +08005160static void raid5_do_work(struct work_struct *work)
5161{
5162 struct r5worker *worker = container_of(work, struct r5worker, work);
5163 struct r5worker_group *group = worker->group;
5164 struct r5conf *conf = group->conf;
5165 int group_id = group - conf->worker_groups;
5166 int handled;
5167 struct blk_plug plug;
5168
5169 pr_debug("+++ raid5worker active\n");
5170
5171 blk_start_plug(&plug);
5172 handled = 0;
5173 spin_lock_irq(&conf->device_lock);
5174 while (1) {
5175 int batch_size, released;
5176
Shaohua Li566c09c2013-11-14 15:16:17 +11005177 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005178
Shaohua Li566c09c2013-11-14 15:16:17 +11005179 batch_size = handle_active_stripes(conf, group_id, worker,
5180 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005181 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005182 if (!batch_size && !released)
5183 break;
5184 handled += batch_size;
5185 }
5186 pr_debug("%d stripes handled\n", handled);
5187
5188 spin_unlock_irq(&conf->device_lock);
5189 blk_finish_plug(&plug);
5190
5191 pr_debug("--- raid5worker inactive\n");
5192}
5193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194/*
5195 * This is our raid5 kernel thread.
5196 *
5197 * We scan the hash table for stripes which can be handled now.
5198 * During the scan, completed stripes are saved for us by the interrupt
5199 * handler, so that they will not have to wait for our next wakeup.
5200 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005201static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202{
Shaohua Li4ed87312012-10-11 13:34:00 +11005203 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005204 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005206 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
Dan Williams45b42332007-07-09 11:56:43 -07005208 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
5210 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005212 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 handled = 0;
5214 spin_lock_irq(&conf->device_lock);
5215 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005216 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005217 int batch_size, released;
5218
Shaohua Li566c09c2013-11-14 15:16:17 +11005219 released = release_stripe_list(conf, conf->temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
NeilBrown0021b7b2012-07-31 09:08:14 +02005221 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005222 !list_empty(&conf->bitmap_list)) {
5223 /* Now is a good time to flush some bitmap updates */
5224 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005225 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005226 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005227 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005228 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005229 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005230 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005231 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005232
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005233 while ((bio = remove_bio_from_retry(conf))) {
5234 int ok;
5235 spin_unlock_irq(&conf->device_lock);
5236 ok = retry_aligned_read(conf, bio);
5237 spin_lock_irq(&conf->device_lock);
5238 if (!ok)
5239 break;
5240 handled++;
5241 }
5242
Shaohua Li566c09c2013-11-14 15:16:17 +11005243 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5244 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005245 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005247 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Shaohua Li46a06402012-08-02 08:33:15 +10005249 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5250 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005251 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005252 spin_lock_irq(&conf->device_lock);
5253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
Dan Williams45b42332007-07-09 11:56:43 -07005255 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
5257 spin_unlock_irq(&conf->device_lock);
5258
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005259 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005260 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Dan Williams45b42332007-07-09 11:56:43 -07005262 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263}
5264
NeilBrown3f294f42005-11-08 21:39:25 -08005265static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005266raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005267{
NeilBrownd1688a62011-10-11 16:49:52 +11005268 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005269 if (conf)
5270 return sprintf(page, "%d\n", conf->max_nr_stripes);
5271 else
5272 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005273}
5274
NeilBrownc41d4ac2010-06-01 19:37:24 +10005275int
NeilBrownfd01b882011-10-11 16:47:53 +11005276raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005277{
NeilBrownd1688a62011-10-11 16:49:52 +11005278 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005279 int err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005280 int hash;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005281
5282 if (size <= 16 || size > 32768)
5283 return -EINVAL;
Shaohua Li566c09c2013-11-14 15:16:17 +11005284 hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005285 while (size < conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005286 if (drop_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005287 conf->max_nr_stripes--;
5288 else
5289 break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005290 hash--;
5291 if (hash < 0)
5292 hash = NR_STRIPE_HASH_LOCKS - 1;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005293 }
5294 err = md_allow_write(mddev);
5295 if (err)
5296 return err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005297 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005298 while (size > conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005299 if (grow_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005300 conf->max_nr_stripes++;
5301 else break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005302 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005303 }
5304 return 0;
5305}
5306EXPORT_SYMBOL(raid5_set_cache_size);
5307
NeilBrown3f294f42005-11-08 21:39:25 -08005308static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005309raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005310{
NeilBrownd1688a62011-10-11 16:49:52 +11005311 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005312 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005313 int err;
5314
NeilBrown3f294f42005-11-08 21:39:25 -08005315 if (len >= PAGE_SIZE)
5316 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08005317 if (!conf)
5318 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08005319
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005320 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005321 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005322 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005323 if (err)
5324 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08005325 return len;
5326}
NeilBrown007583c2005-11-08 21:39:30 -08005327
NeilBrown96de1e62005-11-08 21:39:39 -08005328static struct md_sysfs_entry
5329raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5330 raid5_show_stripe_cache_size,
5331 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005332
5333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005334raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005335{
NeilBrownd1688a62011-10-11 16:49:52 +11005336 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005337 if (conf)
5338 return sprintf(page, "%d\n", conf->bypass_threshold);
5339 else
5340 return 0;
5341}
5342
5343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005344raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005345{
NeilBrownd1688a62011-10-11 16:49:52 +11005346 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005347 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005348 if (len >= PAGE_SIZE)
5349 return -EINVAL;
5350 if (!conf)
5351 return -ENODEV;
5352
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005353 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005354 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005355 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005356 return -EINVAL;
5357 conf->bypass_threshold = new;
5358 return len;
5359}
5360
5361static struct md_sysfs_entry
5362raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5363 S_IRUGO | S_IWUSR,
5364 raid5_show_preread_threshold,
5365 raid5_store_preread_threshold);
5366
5367static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005368stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005369{
NeilBrownd1688a62011-10-11 16:49:52 +11005370 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005371 if (conf)
5372 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5373 else
5374 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005375}
5376
NeilBrown96de1e62005-11-08 21:39:39 -08005377static struct md_sysfs_entry
5378raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005379
Shaohua Lib7214202013-08-27 17:50:42 +08005380static ssize_t
5381raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5382{
5383 struct r5conf *conf = mddev->private;
5384 if (conf)
5385 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5386 else
5387 return 0;
5388}
5389
5390static int alloc_thread_groups(struct r5conf *conf, int cnt);
5391static ssize_t
5392raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5393{
5394 struct r5conf *conf = mddev->private;
5395 unsigned long new;
5396 int err;
5397 struct r5worker_group *old_groups;
5398 int old_group_cnt;
5399
5400 if (len >= PAGE_SIZE)
5401 return -EINVAL;
5402 if (!conf)
5403 return -ENODEV;
5404
5405 if (kstrtoul(page, 10, &new))
5406 return -EINVAL;
5407
5408 if (new == conf->worker_cnt_per_group)
5409 return len;
5410
5411 mddev_suspend(mddev);
5412
5413 old_groups = conf->worker_groups;
5414 old_group_cnt = conf->worker_cnt_per_group;
5415
majianpengd206dcf2013-11-14 15:16:19 +11005416 if (old_groups)
5417 flush_workqueue(raid5_wq);
5418
Shaohua Lib7214202013-08-27 17:50:42 +08005419 conf->worker_groups = NULL;
5420 err = alloc_thread_groups(conf, new);
5421 if (err) {
5422 conf->worker_groups = old_groups;
5423 conf->worker_cnt_per_group = old_group_cnt;
5424 } else {
5425 if (old_groups)
5426 kfree(old_groups[0].workers);
5427 kfree(old_groups);
5428 }
5429
5430 mddev_resume(mddev);
5431
5432 if (err)
5433 return err;
5434 return len;
5435}
5436
5437static struct md_sysfs_entry
5438raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5439 raid5_show_group_thread_cnt,
5440 raid5_store_group_thread_cnt);
5441
NeilBrown007583c2005-11-08 21:39:30 -08005442static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005443 &raid5_stripecache_size.attr,
5444 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005445 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005446 &raid5_group_thread_cnt.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005447 NULL,
5448};
NeilBrown007583c2005-11-08 21:39:30 -08005449static struct attribute_group raid5_attrs_group = {
5450 .name = NULL,
5451 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005452};
5453
Shaohua Li851c30c2013-08-28 14:30:16 +08005454static int alloc_thread_groups(struct r5conf *conf, int cnt)
5455{
Shaohua Li566c09c2013-11-14 15:16:17 +11005456 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08005457 ssize_t size;
5458 struct r5worker *workers;
5459
5460 conf->worker_cnt_per_group = cnt;
5461 if (cnt == 0) {
5462 conf->worker_groups = NULL;
5463 return 0;
5464 }
5465 conf->group_cnt = num_possible_nodes();
5466 size = sizeof(struct r5worker) * cnt;
5467 workers = kzalloc(size * conf->group_cnt, GFP_NOIO);
5468 conf->worker_groups = kzalloc(sizeof(struct r5worker_group) *
5469 conf->group_cnt, GFP_NOIO);
5470 if (!conf->worker_groups || !workers) {
5471 kfree(workers);
5472 kfree(conf->worker_groups);
5473 conf->worker_groups = NULL;
5474 return -ENOMEM;
5475 }
5476
5477 for (i = 0; i < conf->group_cnt; i++) {
5478 struct r5worker_group *group;
5479
5480 group = &conf->worker_groups[i];
5481 INIT_LIST_HEAD(&group->handle_list);
5482 group->conf = conf;
5483 group->workers = workers + i * cnt;
5484
5485 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005486 struct r5worker *worker = group->workers + j;
5487 worker->group = group;
5488 INIT_WORK(&worker->work, raid5_do_work);
5489
5490 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
5491 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08005492 }
5493 }
5494
5495 return 0;
5496}
5497
5498static void free_thread_groups(struct r5conf *conf)
5499{
5500 if (conf->worker_groups)
5501 kfree(conf->worker_groups[0].workers);
5502 kfree(conf->worker_groups);
5503 conf->worker_groups = NULL;
5504}
5505
Dan Williams80c3a6c2009-03-17 18:10:40 -07005506static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005507raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005508{
NeilBrownd1688a62011-10-11 16:49:52 +11005509 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005510
5511 if (!sectors)
5512 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005513 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005514 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005515 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005516
Andre Noll9d8f0362009-06-18 08:45:01 +10005517 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005518 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005519 return sectors * (raid_disks - conf->max_degraded);
5520}
5521
NeilBrownd1688a62011-10-11 16:49:52 +11005522static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005523{
5524 struct raid5_percpu *percpu;
5525 unsigned long cpu;
5526
5527 if (!conf->percpu)
5528 return;
5529
5530 get_online_cpus();
5531 for_each_possible_cpu(cpu) {
5532 percpu = per_cpu_ptr(conf->percpu, cpu);
5533 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005534 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005535 }
5536#ifdef CONFIG_HOTPLUG_CPU
5537 unregister_cpu_notifier(&conf->cpu_notify);
5538#endif
5539 put_online_cpus();
5540
5541 free_percpu(conf->percpu);
5542}
5543
NeilBrownd1688a62011-10-11 16:49:52 +11005544static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005545{
Shaohua Li851c30c2013-08-28 14:30:16 +08005546 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005547 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005548 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005549 kfree(conf->disks);
5550 kfree(conf->stripe_hashtbl);
5551 kfree(conf);
5552}
5553
Dan Williams36d1c642009-07-14 11:48:22 -07005554#ifdef CONFIG_HOTPLUG_CPU
5555static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5556 void *hcpu)
5557{
NeilBrownd1688a62011-10-11 16:49:52 +11005558 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005559 long cpu = (long)hcpu;
5560 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5561
5562 switch (action) {
5563 case CPU_UP_PREPARE:
5564 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005565 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005566 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005567 if (!percpu->scribble)
5568 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5569
5570 if (!percpu->scribble ||
5571 (conf->level == 6 && !percpu->spare_page)) {
5572 safe_put_page(percpu->spare_page);
5573 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005574 pr_err("%s: failed memory allocation for cpu%ld\n",
5575 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005576 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005577 }
5578 break;
5579 case CPU_DEAD:
5580 case CPU_DEAD_FROZEN:
5581 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005582 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005583 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005584 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005585 break;
5586 default:
5587 break;
5588 }
5589 return NOTIFY_OK;
5590}
5591#endif
5592
NeilBrownd1688a62011-10-11 16:49:52 +11005593static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005594{
5595 unsigned long cpu;
5596 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005597 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005598 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005599 int err;
5600
Dan Williams36d1c642009-07-14 11:48:22 -07005601 allcpus = alloc_percpu(struct raid5_percpu);
5602 if (!allcpus)
5603 return -ENOMEM;
5604 conf->percpu = allcpus;
5605
5606 get_online_cpus();
5607 err = 0;
5608 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005609 if (conf->level == 6) {
5610 spare_page = alloc_page(GFP_KERNEL);
5611 if (!spare_page) {
5612 err = -ENOMEM;
5613 break;
5614 }
5615 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5616 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005617 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005618 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005619 err = -ENOMEM;
5620 break;
5621 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005622 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005623 }
5624#ifdef CONFIG_HOTPLUG_CPU
5625 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5626 conf->cpu_notify.priority = 0;
5627 if (err == 0)
5628 err = register_cpu_notifier(&conf->cpu_notify);
5629#endif
5630 put_online_cpus();
5631
5632 return err;
5633}
5634
NeilBrownd1688a62011-10-11 16:49:52 +11005635static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
NeilBrownd1688a62011-10-11 16:49:52 +11005637 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005638 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005639 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005641 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11005642 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
NeilBrown91adb562009-03-31 14:39:39 +11005644 if (mddev->new_level != 5
5645 && mddev->new_level != 4
5646 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005647 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005648 mdname(mddev), mddev->new_level);
5649 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
NeilBrown91adb562009-03-31 14:39:39 +11005651 if ((mddev->new_level == 5
5652 && !algorithm_valid_raid5(mddev->new_layout)) ||
5653 (mddev->new_level == 6
5654 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005655 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005656 mdname(mddev), mddev->new_layout);
5657 return ERR_PTR(-EIO);
5658 }
5659 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005660 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005661 mdname(mddev), mddev->raid_disks);
5662 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Andre Noll664e7c42009-06-18 08:45:27 +10005665 if (!mddev->new_chunk_sectors ||
5666 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5667 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005668 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5669 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005670 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005671 }
5672
NeilBrownd1688a62011-10-11 16:49:52 +11005673 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005674 if (conf == NULL)
5675 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005676 /* Don't enable multi-threading by default*/
5677 if (alloc_thread_groups(conf, 0))
5678 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005679 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10005680 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11005681 init_waitqueue_head(&conf->wait_for_stripe);
5682 init_waitqueue_head(&conf->wait_for_overlap);
5683 INIT_LIST_HEAD(&conf->handle_list);
5684 INIT_LIST_HEAD(&conf->hold_list);
5685 INIT_LIST_HEAD(&conf->delayed_list);
5686 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005687 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005688 atomic_set(&conf->active_stripes, 0);
5689 atomic_set(&conf->preread_active_stripes, 0);
5690 atomic_set(&conf->active_aligned_reads, 0);
5691 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005692 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005693
5694 conf->raid_disks = mddev->raid_disks;
5695 if (mddev->reshape_position == MaxSector)
5696 conf->previous_raid_disks = mddev->raid_disks;
5697 else
5698 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005699 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5700 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005701
NeilBrown5e5e3e72009-10-16 16:35:30 +11005702 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005703 GFP_KERNEL);
5704 if (!conf->disks)
5705 goto abort;
5706
5707 conf->mddev = mddev;
5708
5709 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5710 goto abort;
5711
Shaohua Li566c09c2013-11-14 15:16:17 +11005712 /* We init hash_locks[0] separately to that it can be used
5713 * as the reference lock in the spin_lock_nest_lock() call
5714 * in lock_all_device_hash_locks_irq in order to convince
5715 * lockdep that we know what we are doing.
5716 */
5717 spin_lock_init(conf->hash_locks);
5718 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
5719 spin_lock_init(conf->hash_locks + i);
5720
5721 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5722 INIT_LIST_HEAD(conf->inactive_list + i);
5723
5724 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5725 INIT_LIST_HEAD(conf->temp_inactive_list + i);
5726
Dan Williams36d1c642009-07-14 11:48:22 -07005727 conf->level = mddev->new_level;
5728 if (raid5_alloc_percpu(conf) != 0)
5729 goto abort;
5730
NeilBrown0c55e022010-05-03 14:09:02 +10005731 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005732
NeilBrowndafb20f2012-03-19 12:46:39 +11005733 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005734 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005735 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005736 || raid_disk < 0)
5737 continue;
5738 disk = conf->disks + raid_disk;
5739
NeilBrown17045f52011-12-23 10:17:53 +11005740 if (test_bit(Replacement, &rdev->flags)) {
5741 if (disk->replacement)
5742 goto abort;
5743 disk->replacement = rdev;
5744 } else {
5745 if (disk->rdev)
5746 goto abort;
5747 disk->rdev = rdev;
5748 }
NeilBrown91adb562009-03-31 14:39:39 +11005749
5750 if (test_bit(In_sync, &rdev->flags)) {
5751 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005752 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5753 " disk %d\n",
5754 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005755 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005756 /* Cannot rely on bitmap to complete recovery */
5757 conf->fullsync = 1;
5758 }
5759
Andre Noll09c9e5f2009-06-18 08:45:55 +10005760 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005761 conf->level = mddev->new_level;
5762 if (conf->level == 6)
5763 conf->max_degraded = 2;
5764 else
5765 conf->max_degraded = 1;
5766 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005767 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005768 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005769 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005770 conf->prev_algo = mddev->layout;
5771 }
NeilBrown91adb562009-03-31 14:39:39 +11005772
5773 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005774 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11005775 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
Shaohua Li566c09c2013-11-14 15:16:17 +11005776 if (grow_stripes(conf, NR_STRIPES)) {
NeilBrown91adb562009-03-31 14:39:39 +11005777 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005778 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5779 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005780 goto abort;
5781 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005782 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5783 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005784
NeilBrown02326052012-07-03 15:56:52 +10005785 sprintf(pers_name, "raid%d", mddev->new_level);
5786 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005787 if (!conf->thread) {
5788 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005789 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005790 mdname(mddev));
5791 goto abort;
5792 }
5793
5794 return conf;
5795
5796 abort:
5797 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005798 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005799 return ERR_PTR(-EIO);
5800 } else
5801 return ERR_PTR(-ENOMEM);
5802}
5803
NeilBrownc148ffd2009-11-13 17:47:00 +11005804
5805static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5806{
5807 switch (algo) {
5808 case ALGORITHM_PARITY_0:
5809 if (raid_disk < max_degraded)
5810 return 1;
5811 break;
5812 case ALGORITHM_PARITY_N:
5813 if (raid_disk >= raid_disks - max_degraded)
5814 return 1;
5815 break;
5816 case ALGORITHM_PARITY_0_6:
5817 if (raid_disk == 0 ||
5818 raid_disk == raid_disks - 1)
5819 return 1;
5820 break;
5821 case ALGORITHM_LEFT_ASYMMETRIC_6:
5822 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5823 case ALGORITHM_LEFT_SYMMETRIC_6:
5824 case ALGORITHM_RIGHT_SYMMETRIC_6:
5825 if (raid_disk == raid_disks - 1)
5826 return 1;
5827 }
5828 return 0;
5829}
5830
NeilBrownfd01b882011-10-11 16:47:53 +11005831static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005832{
NeilBrownd1688a62011-10-11 16:49:52 +11005833 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005834 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005835 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005836 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005837 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005838 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005839 long long min_offset_diff = 0;
5840 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005841
Andre Noll8c6ac862009-06-18 08:48:06 +10005842 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005843 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005844 " -- starting background reconstruction\n",
5845 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005846
5847 rdev_for_each(rdev, mddev) {
5848 long long diff;
5849 if (rdev->raid_disk < 0)
5850 continue;
5851 diff = (rdev->new_data_offset - rdev->data_offset);
5852 if (first) {
5853 min_offset_diff = diff;
5854 first = 0;
5855 } else if (mddev->reshape_backwards &&
5856 diff < min_offset_diff)
5857 min_offset_diff = diff;
5858 else if (!mddev->reshape_backwards &&
5859 diff > min_offset_diff)
5860 min_offset_diff = diff;
5861 }
5862
NeilBrownf6705572006-03-27 01:18:11 -08005863 if (mddev->reshape_position != MaxSector) {
5864 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005865 * Difficulties arise if the stripe we would write to
5866 * next is at or after the stripe we would read from next.
5867 * For a reshape that changes the number of devices, this
5868 * is only possible for a very short time, and mdadm makes
5869 * sure that time appears to have past before assembling
5870 * the array. So we fail if that time hasn't passed.
5871 * For a reshape that keeps the number of devices the same
5872 * mdadm must be monitoring the reshape can keeping the
5873 * critical areas read-only and backed up. It will start
5874 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005875 */
5876 sector_t here_new, here_old;
5877 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005878 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005879
NeilBrown88ce4932009-03-31 15:24:23 +11005880 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005881 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005882 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005883 mdname(mddev));
5884 return -EINVAL;
5885 }
NeilBrownf6705572006-03-27 01:18:11 -08005886 old_disks = mddev->raid_disks - mddev->delta_disks;
5887 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005888 * further up in new geometry must map after here in old
5889 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005890 */
5891 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005892 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005893 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005894 printk(KERN_ERR "md/raid:%s: reshape_position not "
5895 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005896 return -EINVAL;
5897 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005898 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005899 /* here_new is the stripe we will write to */
5900 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005901 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005902 (old_disks-max_degraded));
5903 /* here_old is the first stripe that we might need to read
5904 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005905 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005906 if ((here_new * mddev->new_chunk_sectors !=
5907 here_old * mddev->chunk_sectors)) {
5908 printk(KERN_ERR "md/raid:%s: reshape position is"
5909 " confused - aborting\n", mdname(mddev));
5910 return -EINVAL;
5911 }
NeilBrown67ac6012009-08-13 10:06:24 +10005912 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005913 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005914 * and taking constant backups.
5915 * mdadm always starts a situation like this in
5916 * readonly mode so it can take control before
5917 * allowing any writes. So just check for that.
5918 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005919 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5920 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5921 /* not really in-place - so OK */;
5922 else if (mddev->ro == 0) {
5923 printk(KERN_ERR "md/raid:%s: in-place reshape "
5924 "must be started in read-only mode "
5925 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005926 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005927 return -EINVAL;
5928 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005929 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005930 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005931 here_old * mddev->chunk_sectors)
5932 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005933 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005934 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005935 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5936 "auto-recovery - aborting.\n",
5937 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005938 return -EINVAL;
5939 }
NeilBrown0c55e022010-05-03 14:09:02 +10005940 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5941 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005942 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005943 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005944 BUG_ON(mddev->level != mddev->new_level);
5945 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005946 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005947 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005948 }
5949
NeilBrown245f46c2009-03-31 14:39:39 +11005950 if (mddev->private == NULL)
5951 conf = setup_conf(mddev);
5952 else
5953 conf = mddev->private;
5954
NeilBrown91adb562009-03-31 14:39:39 +11005955 if (IS_ERR(conf))
5956 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005957
NeilBrownb5254dd2012-05-21 09:27:01 +10005958 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005959 mddev->thread = conf->thread;
5960 conf->thread = NULL;
5961 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
NeilBrown17045f52011-12-23 10:17:53 +11005963 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5964 i++) {
5965 rdev = conf->disks[i].rdev;
5966 if (!rdev && conf->disks[i].replacement) {
5967 /* The replacement is all we have yet */
5968 rdev = conf->disks[i].replacement;
5969 conf->disks[i].replacement = NULL;
5970 clear_bit(Replacement, &rdev->flags);
5971 conf->disks[i].rdev = rdev;
5972 }
5973 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005974 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005975 if (conf->disks[i].replacement &&
5976 conf->reshape_progress != MaxSector) {
5977 /* replacements and reshape simply do not mix. */
5978 printk(KERN_ERR "md: cannot handle concurrent "
5979 "replacement and reshape.\n");
5980 goto abort;
5981 }
NeilBrown2f115882010-06-17 17:41:03 +10005982 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005983 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005984 continue;
5985 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005986 /* This disc is not fully in-sync. However if it
5987 * just stored parity (beyond the recovery_offset),
5988 * when we don't need to be concerned about the
5989 * array being dirty.
5990 * When reshape goes 'backwards', we never have
5991 * partially completed devices, so we only need
5992 * to worry about reshape going forwards.
5993 */
5994 /* Hack because v0.91 doesn't store recovery_offset properly. */
5995 if (mddev->major_version == 0 &&
5996 mddev->minor_version > 90)
5997 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005998
NeilBrownc148ffd2009-11-13 17:47:00 +11005999 if (rdev->recovery_offset < reshape_offset) {
6000 /* We need to check old and new layout */
6001 if (!only_parity(rdev->raid_disk,
6002 conf->algorithm,
6003 conf->raid_disks,
6004 conf->max_degraded))
6005 continue;
6006 }
6007 if (!only_parity(rdev->raid_disk,
6008 conf->prev_algo,
6009 conf->previous_raid_disks,
6010 conf->max_degraded))
6011 continue;
6012 dirty_parity_disks++;
6013 }
NeilBrown91adb562009-03-31 14:39:39 +11006014
NeilBrown17045f52011-12-23 10:17:53 +11006015 /*
6016 * 0 for a fully functional array, 1 or 2 for a degraded array.
6017 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006018 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
NeilBrown674806d2010-06-16 17:17:53 +10006020 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006021 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006023 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 goto abort;
6025 }
6026
NeilBrown91adb562009-03-31 14:39:39 +11006027 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006028 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006029 mddev->resync_max_sectors = mddev->dev_sectors;
6030
NeilBrownc148ffd2009-11-13 17:47:00 +11006031 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006033 if (mddev->ok_start_degraded)
6034 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006035 "md/raid:%s: starting dirty degraded array"
6036 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006037 mdname(mddev));
6038 else {
6039 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006040 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006041 mdname(mddev));
6042 goto abort;
6043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
6045
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006047 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6048 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006049 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6050 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 else
NeilBrown0c55e022010-05-03 14:09:02 +10006052 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6053 " out of %d devices, algorithm %d\n",
6054 mdname(mddev), conf->level,
6055 mddev->raid_disks - mddev->degraded,
6056 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
6058 print_raid5_conf(conf);
6059
NeilBrownfef9c612009-03-31 15:16:46 +11006060 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006061 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006062 atomic_set(&conf->reshape_stripes, 0);
6063 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6064 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6065 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6066 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6067 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006068 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006069 }
6070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006073 if (mddev->to_remove == &raid5_attrs_group)
6074 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006075 else if (mddev->kobj.sd &&
6076 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006077 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006078 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006079 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006080 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6081
6082 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006083 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006084 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006085 /* read-ahead size must cover two whole stripes, which
6086 * is 2 * (datadisks) * chunksize where 'n' is the
6087 * number of raid devices
6088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6090 int stripe = data_disks *
6091 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6092 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6093 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006094
6095 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006096
6097 mddev->queue->backing_dev_info.congested_data = mddev;
6098 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10006099
6100 chunk_size = mddev->chunk_sectors << 9;
6101 blk_queue_io_min(mddev->queue, chunk_size);
6102 blk_queue_io_opt(mddev->queue, chunk_size *
6103 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11006104 /*
6105 * We can only discard a whole stripe. It doesn't make sense to
6106 * discard data disk but write parity disk
6107 */
6108 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006109 /* Round up to power of 2, as discard handling
6110 * currently assumes that */
6111 while ((stripe-1) & stripe)
6112 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006113 mddev->queue->limits.discard_alignment = stripe;
6114 mddev->queue->limits.discard_granularity = stripe;
6115 /*
6116 * unaligned part of discard request will be ignored, so can't
6117 * guarantee discard_zerors_data
6118 */
6119 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006120
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006121 blk_queue_max_write_same_sectors(mddev->queue, 0);
6122
NeilBrown05616be2012-05-21 09:27:00 +10006123 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006124 disk_stack_limits(mddev->gendisk, rdev->bdev,
6125 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006126 disk_stack_limits(mddev->gendisk, rdev->bdev,
6127 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006128 /*
6129 * discard_zeroes_data is required, otherwise data
6130 * could be lost. Consider a scenario: discard a stripe
6131 * (the stripe could be inconsistent if
6132 * discard_zeroes_data is 0); write one disk of the
6133 * stripe (the stripe could be inconsistent again
6134 * depending on which disks are used to calculate
6135 * parity); the disk is broken; The stripe data of this
6136 * disk is lost.
6137 */
6138 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6139 !bdev_get_queue(rdev->bdev)->
6140 limits.discard_zeroes_data)
6141 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10006142 }
Shaohua Li620125f2012-10-11 13:49:05 +11006143
6144 if (discard_supported &&
6145 mddev->queue->limits.max_discard_sectors >= stripe &&
6146 mddev->queue->limits.discard_granularity >= stripe)
6147 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6148 mddev->queue);
6149 else
6150 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6151 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 }
6153
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 return 0;
6155abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006156 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006157 print_raid5_conf(conf);
6158 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006160 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 return -EIO;
6162}
6163
NeilBrownfd01b882011-10-11 16:47:53 +11006164static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165{
NeilBrownd1688a62011-10-11 16:49:52 +11006166 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
NeilBrown01f96c02011-09-21 15:30:20 +10006168 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006169 if (mddev->queue)
6170 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10006171 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006172 mddev->private = NULL;
6173 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 return 0;
6175}
6176
NeilBrownfd01b882011-10-11 16:47:53 +11006177static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178{
NeilBrownd1688a62011-10-11 16:49:52 +11006179 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 int i;
6181
Andre Noll9d8f0362009-06-18 08:45:01 +10006182 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6183 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006184 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 for (i = 0; i < conf->raid_disks; i++)
6186 seq_printf (seq, "%s",
6187 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08006188 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190}
6191
NeilBrownd1688a62011-10-11 16:49:52 +11006192static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193{
6194 int i;
6195 struct disk_info *tmp;
6196
NeilBrown0c55e022010-05-03 14:09:02 +10006197 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 if (!conf) {
6199 printk("(conf==NULL)\n");
6200 return;
6201 }
NeilBrown0c55e022010-05-03 14:09:02 +10006202 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6203 conf->raid_disks,
6204 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
6206 for (i = 0; i < conf->raid_disks; i++) {
6207 char b[BDEVNAME_SIZE];
6208 tmp = conf->disks + i;
6209 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10006210 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6211 i, !test_bit(Faulty, &tmp->rdev->flags),
6212 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 }
6214}
6215
NeilBrownfd01b882011-10-11 16:47:53 +11006216static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217{
6218 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006219 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006221 int count = 0;
6222 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
6224 for (i = 0; i < conf->raid_disks; i++) {
6225 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006226 if (tmp->replacement
6227 && tmp->replacement->recovery_offset == MaxSector
6228 && !test_bit(Faulty, &tmp->replacement->flags)
6229 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6230 /* Replacement has just become active. */
6231 if (!tmp->rdev
6232 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6233 count++;
6234 if (tmp->rdev) {
6235 /* Replaced device not technically faulty,
6236 * but we need to be sure it gets removed
6237 * and never re-added.
6238 */
6239 set_bit(Faulty, &tmp->rdev->flags);
6240 sysfs_notify_dirent_safe(
6241 tmp->rdev->sysfs_state);
6242 }
6243 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6244 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006245 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006246 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006247 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006248 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006249 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 }
6251 }
NeilBrown6b965622010-08-18 11:56:59 +10006252 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006253 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006254 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006256 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
6258
NeilBrownb8321b62011-12-23 10:17:51 +11006259static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260{
NeilBrownd1688a62011-10-11 16:49:52 +11006261 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006263 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006264 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 struct disk_info *p = conf->disks + number;
6266
6267 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006268 if (rdev == p->rdev)
6269 rdevp = &p->rdev;
6270 else if (rdev == p->replacement)
6271 rdevp = &p->replacement;
6272 else
6273 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006274
NeilBrown657e3e42011-12-23 10:17:52 +11006275 if (number >= conf->raid_disks &&
6276 conf->reshape_progress == MaxSector)
6277 clear_bit(In_sync, &rdev->flags);
6278
6279 if (test_bit(In_sync, &rdev->flags) ||
6280 atomic_read(&rdev->nr_pending)) {
6281 err = -EBUSY;
6282 goto abort;
6283 }
6284 /* Only remove non-faulty devices if recovery
6285 * isn't possible.
6286 */
6287 if (!test_bit(Faulty, &rdev->flags) &&
6288 mddev->recovery_disabled != conf->recovery_disabled &&
6289 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006290 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006291 number < conf->raid_disks) {
6292 err = -EBUSY;
6293 goto abort;
6294 }
6295 *rdevp = NULL;
6296 synchronize_rcu();
6297 if (atomic_read(&rdev->nr_pending)) {
6298 /* lost the race, try later */
6299 err = -EBUSY;
6300 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006301 } else if (p->replacement) {
6302 /* We must have just cleared 'rdev' */
6303 p->rdev = p->replacement;
6304 clear_bit(Replacement, &p->replacement->flags);
6305 smp_mb(); /* Make sure other CPUs may see both as identical
6306 * but will never see neither - if they are careful
6307 */
6308 p->replacement = NULL;
6309 clear_bit(WantReplacement, &rdev->flags);
6310 } else
6311 /* We might have just removed the Replacement as faulty-
6312 * clear the bit just in case
6313 */
6314 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315abort:
6316
6317 print_raid5_conf(conf);
6318 return err;
6319}
6320
NeilBrownfd01b882011-10-11 16:47:53 +11006321static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322{
NeilBrownd1688a62011-10-11 16:49:52 +11006323 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006324 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 int disk;
6326 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006327 int first = 0;
6328 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
NeilBrown7f0da592011-07-28 11:39:22 +10006330 if (mddev->recovery_disabled == conf->recovery_disabled)
6331 return -EBUSY;
6332
NeilBrowndc10c642012-03-19 12:46:37 +11006333 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Neil Brown6c2fce22008-06-28 08:31:31 +10006337 if (rdev->raid_disk >= 0)
6338 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
6340 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006341 * find the disk ... but prefer rdev->saved_raid_disk
6342 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006344 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006345 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006346 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006347 first = rdev->saved_raid_disk;
6348
6349 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006350 p = conf->disks + disk;
6351 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006352 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006354 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006355 if (rdev->saved_raid_disk != disk)
6356 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006357 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006360 }
6361 for (disk = first; disk <= last; disk++) {
6362 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006363 if (test_bit(WantReplacement, &p->rdev->flags) &&
6364 p->replacement == NULL) {
6365 clear_bit(In_sync, &rdev->flags);
6366 set_bit(Replacement, &rdev->flags);
6367 rdev->raid_disk = disk;
6368 err = 0;
6369 conf->fullsync = 1;
6370 rcu_assign_pointer(p->replacement, rdev);
6371 break;
6372 }
6373 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006374out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377}
6378
NeilBrownfd01b882011-10-11 16:47:53 +11006379static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380{
6381 /* no resync is happening, and there is enough space
6382 * on all devices, so we can resize.
6383 * We need to make sure resync covers any new space.
6384 * If the array is shrinking we should possibly wait until
6385 * any io in the removed space completes, but it hardly seems
6386 * worth it.
6387 */
NeilBrowna4a61252012-05-22 13:55:27 +10006388 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006389 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006390 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6391 if (mddev->external_size &&
6392 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006393 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006394 if (mddev->bitmap) {
6395 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6396 if (ret)
6397 return ret;
6398 }
6399 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006400 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006401 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006402 if (sectors > mddev->dev_sectors &&
6403 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006404 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6406 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006407 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006408 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 return 0;
6410}
6411
NeilBrownfd01b882011-10-11 16:47:53 +11006412static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006413{
6414 /* Can only proceed if there are plenty of stripe_heads.
6415 * We need a minimum of one full stripe,, and for sensible progress
6416 * it is best to have about 4 times that.
6417 * If we require 4 times, then the default 256 4K stripe_heads will
6418 * allow for chunk sizes up to 256K, which is probably OK.
6419 * If the chunk size is greater, user-space should request more
6420 * stripe_heads first.
6421 */
NeilBrownd1688a62011-10-11 16:49:52 +11006422 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006423 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6424 > conf->max_nr_stripes ||
6425 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6426 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006427 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6428 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006429 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6430 / STRIPE_SIZE)*4);
6431 return 0;
6432 }
6433 return 1;
6434}
6435
NeilBrownfd01b882011-10-11 16:47:53 +11006436static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006437{
NeilBrownd1688a62011-10-11 16:49:52 +11006438 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006439
NeilBrown88ce4932009-03-31 15:24:23 +11006440 if (mddev->delta_disks == 0 &&
6441 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006442 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006443 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006444 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006445 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006446 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006447 /* We might be able to shrink, but the devices must
6448 * be made bigger first.
6449 * For raid6, 4 is the minimum size.
6450 * Otherwise 2 is the minimum
6451 */
6452 int min = 2;
6453 if (mddev->level == 6)
6454 min = 4;
6455 if (mddev->raid_disks + mddev->delta_disks < min)
6456 return -EINVAL;
6457 }
NeilBrown29269552006-03-27 01:18:10 -08006458
NeilBrown01ee22b2009-06-18 08:47:20 +10006459 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006460 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006461
NeilBrowne56108d62012-10-11 14:24:13 +11006462 return resize_stripes(conf, (conf->previous_raid_disks
6463 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006464}
6465
NeilBrownfd01b882011-10-11 16:47:53 +11006466static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006467{
NeilBrownd1688a62011-10-11 16:49:52 +11006468 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006469 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006470 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006471 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006472
NeilBrownf4168852007-02-28 20:11:53 -08006473 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006474 return -EBUSY;
6475
NeilBrown01ee22b2009-06-18 08:47:20 +10006476 if (!check_stripe_cache(mddev))
6477 return -ENOSPC;
6478
NeilBrown30b67642012-05-22 13:55:28 +10006479 if (has_failed(conf))
6480 return -EINVAL;
6481
NeilBrownc6563a82012-05-21 09:27:00 +10006482 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006483 if (!test_bit(In_sync, &rdev->flags)
6484 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006485 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006486 }
NeilBrown63c70c42006-03-27 01:18:13 -08006487
NeilBrownf4168852007-02-28 20:11:53 -08006488 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006489 /* Not enough devices even to make a degraded array
6490 * of that size
6491 */
6492 return -EINVAL;
6493
NeilBrownec32a2b2009-03-31 15:17:38 +11006494 /* Refuse to reduce size of the array. Any reductions in
6495 * array size must be through explicit setting of array_size
6496 * attribute.
6497 */
6498 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6499 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006500 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006501 "before number of disks\n", mdname(mddev));
6502 return -EINVAL;
6503 }
6504
NeilBrownf6705572006-03-27 01:18:11 -08006505 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006506 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006507 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006508 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006509 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006510 conf->prev_chunk_sectors = conf->chunk_sectors;
6511 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006512 conf->prev_algo = conf->algorithm;
6513 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006514 conf->generation++;
6515 /* Code that selects data_offset needs to see the generation update
6516 * if reshape_progress has been set - so a memory barrier needed.
6517 */
6518 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006519 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006520 conf->reshape_progress = raid5_size(mddev, 0, 0);
6521 else
6522 conf->reshape_progress = 0;
6523 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10006524 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006525 spin_unlock_irq(&conf->device_lock);
6526
NeilBrown4d77e3b2013-08-27 15:57:47 +10006527 /* Now make sure any requests that proceeded on the assumption
6528 * the reshape wasn't running - like Discard or Read - have
6529 * completed.
6530 */
6531 mddev_suspend(mddev);
6532 mddev_resume(mddev);
6533
NeilBrown29269552006-03-27 01:18:10 -08006534 /* Add some new drives, as many as will fit.
6535 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006536 * Don't add devices if we are reducing the number of
6537 * devices in the array. This is because it is not possible
6538 * to correctly record the "partially reconstructed" state of
6539 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006540 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006541 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006542 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006543 if (rdev->raid_disk < 0 &&
6544 !test_bit(Faulty, &rdev->flags)) {
6545 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006546 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006547 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006548 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006549 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006550 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006551
6552 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006553 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006554 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006555 } else if (rdev->raid_disk >= conf->previous_raid_disks
6556 && !test_bit(Faulty, &rdev->flags)) {
6557 /* This is a spare that was manually added */
6558 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006559 }
NeilBrown29269552006-03-27 01:18:10 -08006560
NeilBrown87a8dec2011-01-31 11:57:43 +11006561 /* When a reshape changes the number of devices,
6562 * ->degraded is measured against the larger of the
6563 * pre and post number of devices.
6564 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006565 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006566 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006567 spin_unlock_irqrestore(&conf->device_lock, flags);
6568 }
NeilBrown63c70c42006-03-27 01:18:13 -08006569 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006570 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006571 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006572
NeilBrown29269552006-03-27 01:18:10 -08006573 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6574 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6575 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6576 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6577 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006578 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006579 if (!mddev->sync_thread) {
6580 mddev->recovery = 0;
6581 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11006582 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006583 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11006584 mddev->new_chunk_sectors =
6585 conf->chunk_sectors = conf->prev_chunk_sectors;
6586 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10006587 rdev_for_each(rdev, mddev)
6588 rdev->new_data_offset = rdev->data_offset;
6589 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11006590 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11006591 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006592 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11006593 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006594 spin_unlock_irq(&conf->device_lock);
6595 return -EAGAIN;
6596 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006597 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006598 md_wakeup_thread(mddev->sync_thread);
6599 md_new_event(mddev);
6600 return 0;
6601}
NeilBrown29269552006-03-27 01:18:10 -08006602
NeilBrownec32a2b2009-03-31 15:17:38 +11006603/* This is called from the reshape thread and should make any
6604 * changes needed in 'conf'
6605 */
NeilBrownd1688a62011-10-11 16:49:52 +11006606static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006607{
NeilBrown29269552006-03-27 01:18:10 -08006608
NeilBrownf6705572006-03-27 01:18:11 -08006609 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006610 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006611
NeilBrownf6705572006-03-27 01:18:11 -08006612 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006613 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006614 rdev_for_each(rdev, conf->mddev)
6615 rdev->data_offset = rdev->new_data_offset;
6616 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006617 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006618 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006619 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006620
6621 /* read-ahead size must cover two whole stripes, which is
6622 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6623 */
NeilBrown4a5add42010-06-01 19:37:28 +10006624 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006625 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006626 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006627 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006628 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6629 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6630 }
NeilBrown29269552006-03-27 01:18:10 -08006631 }
NeilBrown29269552006-03-27 01:18:10 -08006632}
6633
NeilBrownec32a2b2009-03-31 15:17:38 +11006634/* This is called from the raid5d thread with mddev_lock held.
6635 * It makes config changes to the device.
6636 */
NeilBrownfd01b882011-10-11 16:47:53 +11006637static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006638{
NeilBrownd1688a62011-10-11 16:49:52 +11006639 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006640
6641 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6642
NeilBrownec32a2b2009-03-31 15:17:38 +11006643 if (mddev->delta_disks > 0) {
6644 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6645 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006646 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006647 } else {
6648 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006649 spin_lock_irq(&conf->device_lock);
6650 mddev->degraded = calc_degraded(conf);
6651 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006652 for (d = conf->raid_disks ;
6653 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006654 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006655 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006656 if (rdev)
6657 clear_bit(In_sync, &rdev->flags);
6658 rdev = conf->disks[d].replacement;
6659 if (rdev)
6660 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006661 }
NeilBrowncea9c222009-03-31 15:15:05 +11006662 }
NeilBrown88ce4932009-03-31 15:24:23 +11006663 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006664 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006665 mddev->reshape_position = MaxSector;
6666 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006667 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006668 }
6669}
6670
NeilBrownfd01b882011-10-11 16:47:53 +11006671static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006672{
NeilBrownd1688a62011-10-11 16:49:52 +11006673 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006674
6675 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006676 case 2: /* resume for a suspend */
6677 wake_up(&conf->wait_for_overlap);
6678 break;
6679
NeilBrown72626682005-09-09 16:23:54 -07006680 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006681 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006682 /* '2' tells resync/reshape to pause so that all
6683 * active stripes can drain
6684 */
6685 conf->quiesce = 2;
Shaohua Li566c09c2013-11-14 15:16:17 +11006686 wait_event_cmd(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006687 atomic_read(&conf->active_stripes) == 0 &&
6688 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11006689 unlock_all_device_hash_locks_irq(conf),
6690 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10006691 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11006692 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006693 /* allow reshape to continue */
6694 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006695 break;
6696
6697 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006698 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006699 conf->quiesce = 0;
6700 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006701 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11006702 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006703 break;
6704 }
NeilBrown72626682005-09-09 16:23:54 -07006705}
NeilBrownb15c2e52006-01-06 00:20:16 -08006706
NeilBrownd562b0c2009-03-31 14:39:39 +11006707
NeilBrownfd01b882011-10-11 16:47:53 +11006708static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006709{
NeilBrowne373ab12011-10-11 16:48:59 +11006710 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006711 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006712
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006713 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006714 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006715 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6716 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006717 return ERR_PTR(-EINVAL);
6718 }
6719
NeilBrowne373ab12011-10-11 16:48:59 +11006720 sectors = raid0_conf->strip_zone[0].zone_end;
6721 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006722 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006723 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006724 mddev->new_layout = ALGORITHM_PARITY_N;
6725 mddev->new_chunk_sectors = mddev->chunk_sectors;
6726 mddev->raid_disks += 1;
6727 mddev->delta_disks = 1;
6728 /* make sure it will be not marked as dirty */
6729 mddev->recovery_cp = MaxSector;
6730
6731 return setup_conf(mddev);
6732}
6733
6734
NeilBrownfd01b882011-10-11 16:47:53 +11006735static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006736{
6737 int chunksect;
6738
6739 if (mddev->raid_disks != 2 ||
6740 mddev->degraded > 1)
6741 return ERR_PTR(-EINVAL);
6742
6743 /* Should check if there are write-behind devices? */
6744
6745 chunksect = 64*2; /* 64K by default */
6746
6747 /* The array must be an exact multiple of chunksize */
6748 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6749 chunksect >>= 1;
6750
6751 if ((chunksect<<9) < STRIPE_SIZE)
6752 /* array size does not allow a suitable chunk size */
6753 return ERR_PTR(-EINVAL);
6754
6755 mddev->new_level = 5;
6756 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006757 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006758
6759 return setup_conf(mddev);
6760}
6761
NeilBrownfd01b882011-10-11 16:47:53 +11006762static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006763{
6764 int new_layout;
6765
6766 switch (mddev->layout) {
6767 case ALGORITHM_LEFT_ASYMMETRIC_6:
6768 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6769 break;
6770 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6771 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6772 break;
6773 case ALGORITHM_LEFT_SYMMETRIC_6:
6774 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6775 break;
6776 case ALGORITHM_RIGHT_SYMMETRIC_6:
6777 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6778 break;
6779 case ALGORITHM_PARITY_0_6:
6780 new_layout = ALGORITHM_PARITY_0;
6781 break;
6782 case ALGORITHM_PARITY_N:
6783 new_layout = ALGORITHM_PARITY_N;
6784 break;
6785 default:
6786 return ERR_PTR(-EINVAL);
6787 }
6788 mddev->new_level = 5;
6789 mddev->new_layout = new_layout;
6790 mddev->delta_disks = -1;
6791 mddev->raid_disks -= 1;
6792 return setup_conf(mddev);
6793}
6794
NeilBrownd562b0c2009-03-31 14:39:39 +11006795
NeilBrownfd01b882011-10-11 16:47:53 +11006796static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006797{
NeilBrown88ce4932009-03-31 15:24:23 +11006798 /* For a 2-drive array, the layout and chunk size can be changed
6799 * immediately as not restriping is needed.
6800 * For larger arrays we record the new value - after validation
6801 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006802 */
NeilBrownd1688a62011-10-11 16:49:52 +11006803 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006804 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006805
NeilBrown597a7112009-06-18 08:47:42 +10006806 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006807 return -EINVAL;
6808 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006809 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006810 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006811 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006812 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006813 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006814 /* not factor of array size */
6815 return -EINVAL;
6816 }
6817
6818 /* They look valid */
6819
NeilBrown88ce4932009-03-31 15:24:23 +11006820 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006821 /* can make the change immediately */
6822 if (mddev->new_layout >= 0) {
6823 conf->algorithm = mddev->new_layout;
6824 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006825 }
6826 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006827 conf->chunk_sectors = new_chunk ;
6828 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006829 }
6830 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6831 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006832 }
NeilBrown50ac1682009-06-18 08:47:55 +10006833 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006834}
6835
NeilBrownfd01b882011-10-11 16:47:53 +11006836static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006837{
NeilBrown597a7112009-06-18 08:47:42 +10006838 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006839
NeilBrown597a7112009-06-18 08:47:42 +10006840 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006841 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006842 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006843 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006844 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006845 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006846 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006847 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006848 /* not factor of array size */
6849 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006850 }
NeilBrown88ce4932009-03-31 15:24:23 +11006851
6852 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006853 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006854}
6855
NeilBrownfd01b882011-10-11 16:47:53 +11006856static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006857{
6858 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006859 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006860 * raid1 - if there are two drives. We need to know the chunk size
6861 * raid4 - trivial - just use a raid4 layout.
6862 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006863 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006864 if (mddev->level == 0)
6865 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006866 if (mddev->level == 1)
6867 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006868 if (mddev->level == 4) {
6869 mddev->new_layout = ALGORITHM_PARITY_N;
6870 mddev->new_level = 5;
6871 return setup_conf(mddev);
6872 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006873 if (mddev->level == 6)
6874 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006875
6876 return ERR_PTR(-EINVAL);
6877}
6878
NeilBrownfd01b882011-10-11 16:47:53 +11006879static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006880{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006881 /* raid4 can take over:
6882 * raid0 - if there is only one strip zone
6883 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006884 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006885 if (mddev->level == 0)
6886 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006887 if (mddev->level == 5 &&
6888 mddev->layout == ALGORITHM_PARITY_N) {
6889 mddev->new_layout = 0;
6890 mddev->new_level = 4;
6891 return setup_conf(mddev);
6892 }
6893 return ERR_PTR(-EINVAL);
6894}
NeilBrownd562b0c2009-03-31 14:39:39 +11006895
NeilBrown84fc4b52011-10-11 16:49:58 +11006896static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006897
NeilBrownfd01b882011-10-11 16:47:53 +11006898static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006899{
6900 /* Currently can only take over a raid5. We map the
6901 * personality to an equivalent raid6 personality
6902 * with the Q block at the end.
6903 */
6904 int new_layout;
6905
6906 if (mddev->pers != &raid5_personality)
6907 return ERR_PTR(-EINVAL);
6908 if (mddev->degraded > 1)
6909 return ERR_PTR(-EINVAL);
6910 if (mddev->raid_disks > 253)
6911 return ERR_PTR(-EINVAL);
6912 if (mddev->raid_disks < 3)
6913 return ERR_PTR(-EINVAL);
6914
6915 switch (mddev->layout) {
6916 case ALGORITHM_LEFT_ASYMMETRIC:
6917 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6918 break;
6919 case ALGORITHM_RIGHT_ASYMMETRIC:
6920 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6921 break;
6922 case ALGORITHM_LEFT_SYMMETRIC:
6923 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6924 break;
6925 case ALGORITHM_RIGHT_SYMMETRIC:
6926 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6927 break;
6928 case ALGORITHM_PARITY_0:
6929 new_layout = ALGORITHM_PARITY_0_6;
6930 break;
6931 case ALGORITHM_PARITY_N:
6932 new_layout = ALGORITHM_PARITY_N;
6933 break;
6934 default:
6935 return ERR_PTR(-EINVAL);
6936 }
6937 mddev->new_level = 6;
6938 mddev->new_layout = new_layout;
6939 mddev->delta_disks = 1;
6940 mddev->raid_disks += 1;
6941 return setup_conf(mddev);
6942}
6943
6944
NeilBrown84fc4b52011-10-11 16:49:58 +11006945static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006946{
6947 .name = "raid6",
6948 .level = 6,
6949 .owner = THIS_MODULE,
6950 .make_request = make_request,
6951 .run = run,
6952 .stop = stop,
6953 .status = status,
6954 .error_handler = error,
6955 .hot_add_disk = raid5_add_disk,
6956 .hot_remove_disk= raid5_remove_disk,
6957 .spare_active = raid5_spare_active,
6958 .sync_request = sync_request,
6959 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006960 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006961 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006962 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006963 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006964 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006965 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006966};
NeilBrown84fc4b52011-10-11 16:49:58 +11006967static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968{
6969 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006970 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 .owner = THIS_MODULE,
6972 .make_request = make_request,
6973 .run = run,
6974 .stop = stop,
6975 .status = status,
6976 .error_handler = error,
6977 .hot_add_disk = raid5_add_disk,
6978 .hot_remove_disk= raid5_remove_disk,
6979 .spare_active = raid5_spare_active,
6980 .sync_request = sync_request,
6981 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006982 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006983 .check_reshape = raid5_check_reshape,
6984 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006985 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006986 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006987 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988};
6989
NeilBrown84fc4b52011-10-11 16:49:58 +11006990static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991{
NeilBrown2604b702006-01-06 00:20:36 -08006992 .name = "raid4",
6993 .level = 4,
6994 .owner = THIS_MODULE,
6995 .make_request = make_request,
6996 .run = run,
6997 .stop = stop,
6998 .status = status,
6999 .error_handler = error,
7000 .hot_add_disk = raid5_add_disk,
7001 .hot_remove_disk= raid5_remove_disk,
7002 .spare_active = raid5_spare_active,
7003 .sync_request = sync_request,
7004 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007005 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007006 .check_reshape = raid5_check_reshape,
7007 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007008 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007009 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007010 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08007011};
7012
7013static int __init raid5_init(void)
7014{
Shaohua Li851c30c2013-08-28 14:30:16 +08007015 raid5_wq = alloc_workqueue("raid5wq",
7016 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7017 if (!raid5_wq)
7018 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07007019 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007020 register_md_personality(&raid5_personality);
7021 register_md_personality(&raid4_personality);
7022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023}
7024
NeilBrown2604b702006-01-06 00:20:36 -08007025static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026{
NeilBrown16a53ec2006-06-26 00:27:38 -07007027 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007028 unregister_md_personality(&raid5_personality);
7029 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007030 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031}
7032
7033module_init(raid5_init);
7034module_exit(raid5_exit);
7035MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007036MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007038MODULE_ALIAS("md-raid5");
7039MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007040MODULE_ALIAS("md-level-5");
7041MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007042MODULE_ALIAS("md-personality-8"); /* RAID6 */
7043MODULE_ALIAS("md-raid6");
7044MODULE_ALIAS("md-level-6");
7045
7046/* This used to be two separate modules, they were: */
7047MODULE_ALIAS("raid5");
7048MODULE_ALIAS("raid6");