blob: 93090b2afab482afcf83b32322188ab8b141c340 [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);
359 list_splice_tail_init(list, conf->inactive_list + hash);
360 do_wakeup = true;
361 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
362 }
363 size--;
364 hash--;
365 }
366
367 if (do_wakeup) {
368 wake_up(&conf->wait_for_stripe);
369 if (conf->retry_read_aligned)
370 md_wakeup_thread(conf->mddev->thread);
371 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000372}
373
Shaohua Lid265d9d2013-08-28 14:29:05 +0800374static struct llist_node *llist_reverse_order(struct llist_node *head)
375{
376 struct llist_node *new_head = NULL;
377
378 while (head) {
379 struct llist_node *tmp = head;
380 head = head->next;
381 tmp->next = new_head;
382 new_head = tmp;
383 }
384
385 return new_head;
386}
387
Shaohua Li773ca822013-08-27 17:50:39 +0800388/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100389static int release_stripe_list(struct r5conf *conf,
390 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800391{
392 struct stripe_head *sh;
393 int count = 0;
394 struct llist_node *head;
395
396 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800397 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800398 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100399 int hash;
400
Shaohua Li773ca822013-08-27 17:50:39 +0800401 sh = llist_entry(head, struct stripe_head, release_list);
402 head = llist_next(head);
403 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
404 smp_mb();
405 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
406 /*
407 * Don't worry the bit is set here, because if the bit is set
408 * again, the count is always > 1. This is true for
409 * STRIPE_ON_UNPLUG_LIST bit too.
410 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100411 hash = sh->hash_lock_index;
412 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800413 count++;
414 }
415
416 return count;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static void release_stripe(struct stripe_head *sh)
420{
NeilBrownd1688a62011-10-11 16:49:52 +1100421 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100423 struct list_head list;
424 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800425 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700426
majianpengad4068d2013-11-14 15:16:15 +1100427 if (unlikely(!conf->mddev->thread) ||
428 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800429 goto slow_path;
430 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
431 if (wakeup)
432 md_wakeup_thread(conf->mddev->thread);
433 return;
434slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000435 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800436 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000437 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100438 INIT_LIST_HEAD(&list);
439 hash = sh->hash_lock_index;
440 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000441 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100442 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000443 }
444 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
NeilBrownfccddba2006-01-06 00:20:33 -0800447static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Dan Williams45b42332007-07-09 11:56:43 -0700449 pr_debug("remove_hash(), stripe %llu\n",
450 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
NeilBrownfccddba2006-01-06 00:20:33 -0800452 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
NeilBrownd1688a62011-10-11 16:49:52 +1100455static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
NeilBrownfccddba2006-01-06 00:20:33 -0800457 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dan Williams45b42332007-07-09 11:56:43 -0700459 pr_debug("insert_hash(), stripe %llu\n",
460 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
NeilBrownfccddba2006-01-06 00:20:33 -0800462 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465
466/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100467static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct stripe_head *sh = NULL;
470 struct list_head *first;
471
Shaohua Li566c09c2013-11-14 15:16:17 +1100472 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100474 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 sh = list_entry(first, struct stripe_head, lru);
476 list_del_init(first);
477 remove_hash(sh);
478 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100479 BUG_ON(hash != sh->hash_lock_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480out:
481 return sh;
482}
483
NeilBrowne4e11e32010-06-16 16:45:16 +1000484static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct page *p;
487 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000488 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
NeilBrowne4e11e32010-06-16 16:45:16 +1000490 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 p = sh->dev[i].page;
492 if (!p)
493 continue;
494 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800495 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497}
498
NeilBrowne4e11e32010-06-16 16:45:16 +1000499static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000502 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
NeilBrowne4e11e32010-06-16 16:45:16 +1000504 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct page *page;
506
507 if (!(page = alloc_page(GFP_KERNEL))) {
508 return 1;
509 }
510 sh->dev[i].page = page;
511 }
512 return 0;
513}
514
NeilBrown784052e2009-03-31 15:19:07 +1100515static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100516static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100517 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
NeilBrownb5663ba2009-03-31 14:39:38 +1100519static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
NeilBrownd1688a62011-10-11 16:49:52 +1100521 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100522 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200524 BUG_ON(atomic_read(&sh->count) != 0);
525 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000526 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700527
Dan Williams45b42332007-07-09 11:56:43 -0700528 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 (unsigned long long)sh->sector);
530
531 remove_hash(sh);
Shaohua Li566c09c2013-11-14 15:16:17 +1100532retry:
533 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100534 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100535 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100537 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 sh->state = 0;
539
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800540
541 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct r5dev *dev = &sh->dev[i];
543
Dan Williamsd84e0f12007-01-02 13:52:30 -0700544 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700546 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700548 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000550 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100553 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100555 if (read_seqcount_retry(&conf->gen_lock, seq))
556 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800558 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
NeilBrownd1688a62011-10-11 16:49:52 +1100561static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100562 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct stripe_head *sh;
565
Dan Williams45b42332007-07-09 11:56:43 -0700566 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800567 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100568 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700570 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return NULL;
572}
573
NeilBrown674806d2010-06-16 17:17:53 +1000574/*
575 * Need to check if array has failed when deciding whether to:
576 * - start an array
577 * - remove non-faulty devices
578 * - add a spare
579 * - allow a reshape
580 * This determination is simple when no reshape is happening.
581 * However if there is a reshape, we need to carefully check
582 * both the before and after sections.
583 * This is because some failed devices may only affect one
584 * of the two sections, and some non-in_sync devices may
585 * be insync in the section most affected by failed devices.
586 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100587static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000588{
NeilBrown908f4fb2011-12-23 10:17:50 +1100589 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000590 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000591
592 rcu_read_lock();
593 degraded = 0;
594 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100595 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000596 if (rdev && test_bit(Faulty, &rdev->flags))
597 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000598 if (!rdev || test_bit(Faulty, &rdev->flags))
599 degraded++;
600 else if (test_bit(In_sync, &rdev->flags))
601 ;
602 else
603 /* not in-sync or faulty.
604 * If the reshape increases the number of devices,
605 * this is being recovered by the reshape, so
606 * this 'previous' section is not in_sync.
607 * If the number of devices is being reduced however,
608 * the device can only be part of the array if
609 * we are reverting a reshape, so this section will
610 * be in-sync.
611 */
612 if (conf->raid_disks >= conf->previous_raid_disks)
613 degraded++;
614 }
615 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100616 if (conf->raid_disks == conf->previous_raid_disks)
617 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000618 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100619 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000620 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100621 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000622 if (rdev && test_bit(Faulty, &rdev->flags))
623 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000624 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100625 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000626 else if (test_bit(In_sync, &rdev->flags))
627 ;
628 else
629 /* not in-sync or faulty.
630 * If reshape increases the number of devices, this
631 * section has already been recovered, else it
632 * almost certainly hasn't.
633 */
634 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100635 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000636 }
637 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100638 if (degraded2 > degraded)
639 return degraded2;
640 return degraded;
641}
642
643static int has_failed(struct r5conf *conf)
644{
645 int degraded;
646
647 if (conf->mddev->reshape_position == MaxSector)
648 return conf->mddev->degraded > conf->max_degraded;
649
650 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000651 if (degraded > conf->max_degraded)
652 return 1;
653 return 0;
654}
655
NeilBrownb5663ba2009-03-31 14:39:38 +1100656static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100657get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000658 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100661 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Dan Williams45b42332007-07-09 11:56:43 -0700663 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Shaohua Li566c09c2013-11-14 15:16:17 +1100665 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 do {
NeilBrown72626682005-09-09 16:23:54 -0700668 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000669 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100670 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100671 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!sh) {
673 if (!conf->inactive_blocked)
Shaohua Li566c09c2013-11-14 15:16:17 +1100674 sh = get_free_stripe(conf, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (noblock && sh == NULL)
676 break;
677 if (!sh) {
678 conf->inactive_blocked = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +1100679 wait_event_lock_irq(
680 conf->wait_for_stripe,
681 !list_empty(conf->inactive_list + hash) &&
682 (atomic_read(&conf->active_stripes)
683 < (conf->max_nr_stripes * 3 / 4)
684 || !conf->inactive_blocked),
685 *(conf->hash_locks + hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 conf->inactive_blocked = 0;
687 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100688 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 } else {
690 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100691 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000692 && !test_bit(STRIPE_EXPANDING, &sh->state)
Shaohua Li773ca822013-08-27 17:50:39 +0800693 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
694 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 } else {
Shaohua Li566c09c2013-11-14 15:16:17 +1100696 spin_lock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!test_bit(STRIPE_HANDLE, &sh->state))
698 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700699 if (list_empty(&sh->lru) &&
Shaohua Li566c09c2013-11-14 15:16:17 +1100700 !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state) &&
NeilBrownff4e8d92006-07-10 04:44:16 -0700701 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700702 BUG();
703 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800704 if (sh->group) {
705 sh->group->stripes_cnt--;
706 sh->group = NULL;
707 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100708 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 }
711 } while (sh == NULL);
712
713 if (sh)
714 atomic_inc(&sh->count);
715
Shaohua Li566c09c2013-11-14 15:16:17 +1100716 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return sh;
718}
719
NeilBrown05616be2012-05-21 09:27:00 +1000720/* Determine if 'data_offset' or 'new_data_offset' should be used
721 * in this stripe_head.
722 */
723static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
724{
725 sector_t progress = conf->reshape_progress;
726 /* Need a memory barrier to make sure we see the value
727 * of conf->generation, or ->data_offset that was set before
728 * reshape_progress was updated.
729 */
730 smp_rmb();
731 if (progress == MaxSector)
732 return 0;
733 if (sh->generation == conf->generation - 1)
734 return 0;
735 /* We are in a reshape, and this is a new-generation stripe,
736 * so use new_data_offset.
737 */
738 return 1;
739}
740
NeilBrown6712ecf2007-09-27 12:47:43 +0200741static void
742raid5_end_read_request(struct bio *bi, int error);
743static void
744raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700745
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000746static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700747{
NeilBrownd1688a62011-10-11 16:49:52 +1100748 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700749 int i, disks = sh->disks;
750
751 might_sleep();
752
753 for (i = disks; i--; ) {
754 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100755 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100756 struct bio *bi, *rbi;
757 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200758 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
759 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
760 rw = WRITE_FUA;
761 else
762 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100763 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100764 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200765 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700766 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100767 else if (test_and_clear_bit(R5_WantReplace,
768 &sh->dev[i].flags)) {
769 rw = WRITE;
770 replace_only = 1;
771 } else
Dan Williams91c00922007-01-02 13:52:30 -0700772 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000773 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
774 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700775
776 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100777 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700778
Dan Williams91c00922007-01-02 13:52:30 -0700779 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100780 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100781 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
782 rdev = rcu_dereference(conf->disks[i].rdev);
783 if (!rdev) {
784 rdev = rrdev;
785 rrdev = NULL;
786 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100787 if (rw & WRITE) {
788 if (replace_only)
789 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100790 if (rdev == rrdev)
791 /* We raced and saw duplicates */
792 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100793 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100794 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100795 rdev = rrdev;
796 rrdev = NULL;
797 }
NeilBrown977df362011-12-23 10:17:53 +1100798
Dan Williams91c00922007-01-02 13:52:30 -0700799 if (rdev && test_bit(Faulty, &rdev->flags))
800 rdev = NULL;
801 if (rdev)
802 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100803 if (rrdev && test_bit(Faulty, &rrdev->flags))
804 rrdev = NULL;
805 if (rrdev)
806 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700807 rcu_read_unlock();
808
NeilBrown73e92e52011-07-28 11:39:22 +1000809 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100810 * need to check for writes. We never accept write errors
811 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000812 */
813 while ((rw & WRITE) && rdev &&
814 test_bit(WriteErrorSeen, &rdev->flags)) {
815 sector_t first_bad;
816 int bad_sectors;
817 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
818 &first_bad, &bad_sectors);
819 if (!bad)
820 break;
821
822 if (bad < 0) {
823 set_bit(BlockedBadBlocks, &rdev->flags);
824 if (!conf->mddev->external &&
825 conf->mddev->flags) {
826 /* It is very unlikely, but we might
827 * still need to write out the
828 * bad block log - better give it
829 * a chance*/
830 md_check_recovery(conf->mddev);
831 }
majianpeng18507532012-07-03 12:11:54 +1000832 /*
833 * Because md_wait_for_blocked_rdev
834 * will dec nr_pending, we must
835 * increment it first.
836 */
837 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000838 md_wait_for_blocked_rdev(rdev, conf->mddev);
839 } else {
840 /* Acknowledged bad block - skip the write */
841 rdev_dec_pending(rdev, conf->mddev);
842 rdev = NULL;
843 }
844 }
845
Dan Williams91c00922007-01-02 13:52:30 -0700846 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100847 if (s->syncing || s->expanding || s->expanded
848 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700849 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
850
Dan Williams2b7497f2008-06-28 08:31:52 +1000851 set_bit(STRIPE_IO_STARTED, &sh->state);
852
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700853 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700854 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700855 bi->bi_rw = rw;
856 bi->bi_end_io = (rw & WRITE)
857 ? raid5_end_write_request
858 : raid5_end_read_request;
859 bi->bi_private = sh;
860
Dan Williams91c00922007-01-02 13:52:30 -0700861 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700862 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700863 bi->bi_rw, i);
864 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000865 if (use_new_offset(conf, sh))
866 bi->bi_sector = (sh->sector
867 + rdev->new_data_offset);
868 else
869 bi->bi_sector = (sh->sector
870 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000871 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
872 bi->bi_rw |= REQ_FLUSH;
873
Kent Overstreet4997b722013-05-30 08:44:39 +0200874 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700875 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
876 bi->bi_io_vec[0].bv_offset = 0;
877 bi->bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800878 /*
879 * If this is discard request, set bi_vcnt 0. We don't
880 * want to confuse SCSI because SCSI will replace payload
881 */
882 if (rw & REQ_DISCARD)
883 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100884 if (rrdev)
885 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600886
887 if (conf->mddev->gendisk)
888 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
889 bi, disk_devt(conf->mddev->gendisk),
890 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700891 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100892 }
893 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100894 if (s->syncing || s->expanding || s->expanded
895 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100896 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
897
898 set_bit(STRIPE_IO_STARTED, &sh->state);
899
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700900 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100901 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700902 rbi->bi_rw = rw;
903 BUG_ON(!(rw & WRITE));
904 rbi->bi_end_io = raid5_end_write_request;
905 rbi->bi_private = sh;
906
NeilBrown977df362011-12-23 10:17:53 +1100907 pr_debug("%s: for %llu schedule op %ld on "
908 "replacement disc %d\n",
909 __func__, (unsigned long long)sh->sector,
910 rbi->bi_rw, i);
911 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000912 if (use_new_offset(conf, sh))
913 rbi->bi_sector = (sh->sector
914 + rrdev->new_data_offset);
915 else
916 rbi->bi_sector = (sh->sector
917 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200918 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100919 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
920 rbi->bi_io_vec[0].bv_offset = 0;
921 rbi->bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800922 /*
923 * If this is discard request, set bi_vcnt 0. We don't
924 * want to confuse SCSI because SCSI will replace payload
925 */
926 if (rw & REQ_DISCARD)
927 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600928 if (conf->mddev->gendisk)
929 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
930 rbi, disk_devt(conf->mddev->gendisk),
931 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100932 generic_make_request(rbi);
933 }
934 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000935 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700936 set_bit(STRIPE_DEGRADED, &sh->state);
937 pr_debug("skip op %ld on disc %d for sector %llu\n",
938 bi->bi_rw, i, (unsigned long long)sh->sector);
939 clear_bit(R5_LOCKED, &sh->dev[i].flags);
940 set_bit(STRIPE_HANDLE, &sh->state);
941 }
942 }
943}
944
945static struct dma_async_tx_descriptor *
946async_copy_data(int frombio, struct bio *bio, struct page *page,
947 sector_t sector, struct dma_async_tx_descriptor *tx)
948{
949 struct bio_vec *bvl;
950 struct page *bio_page;
951 int i;
952 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700953 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700954 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700955
956 if (bio->bi_sector >= sector)
957 page_offset = (signed)(bio->bi_sector - sector) * 512;
958 else
959 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700960
Dan Williams0403e382009-09-08 17:42:50 -0700961 if (frombio)
962 flags |= ASYNC_TX_FENCE;
963 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
964
Dan Williams91c00922007-01-02 13:52:30 -0700965 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000966 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700967 int clen;
968 int b_offset = 0;
969
970 if (page_offset < 0) {
971 b_offset = -page_offset;
972 page_offset += b_offset;
973 len -= b_offset;
974 }
975
976 if (len > 0 && page_offset + len > STRIPE_SIZE)
977 clen = STRIPE_SIZE - page_offset;
978 else
979 clen = len;
980
981 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000982 b_offset += bvl->bv_offset;
983 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700984 if (frombio)
985 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700986 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700987 else
988 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700989 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700990 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700991 /* chain the operations */
992 submit.depend_tx = tx;
993
Dan Williams91c00922007-01-02 13:52:30 -0700994 if (clen < len) /* hit end of page */
995 break;
996 page_offset += len;
997 }
998
999 return tx;
1000}
1001
1002static void ops_complete_biofill(void *stripe_head_ref)
1003{
1004 struct stripe_head *sh = stripe_head_ref;
1005 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -07001006 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001007
Harvey Harrisone46b2722008-04-28 02:15:50 -07001008 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001009 (unsigned long long)sh->sector);
1010
1011 /* clear completed biofills */
1012 for (i = sh->disks; i--; ) {
1013 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001014
1015 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001016 /* and check if we need to reply to a read request,
1017 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001018 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001019 */
1020 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001021 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001022
Dan Williams91c00922007-01-02 13:52:30 -07001023 BUG_ON(!dev->read);
1024 rbi = dev->read;
1025 dev->read = NULL;
1026 while (rbi && rbi->bi_sector <
1027 dev->sector + STRIPE_SECTORS) {
1028 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10001029 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -07001030 rbi->bi_next = return_bi;
1031 return_bi = rbi;
1032 }
Dan Williams91c00922007-01-02 13:52:30 -07001033 rbi = rbi2;
1034 }
1035 }
1036 }
Dan Williams83de75c2008-06-28 08:31:58 +10001037 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001038
1039 return_io(return_bi);
1040
Dan Williamse4d84902007-09-24 10:06:13 -07001041 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001042 release_stripe(sh);
1043}
1044
1045static void ops_run_biofill(struct stripe_head *sh)
1046{
1047 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001048 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001049 int i;
1050
Harvey Harrisone46b2722008-04-28 02:15:50 -07001051 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001052 (unsigned long long)sh->sector);
1053
1054 for (i = sh->disks; i--; ) {
1055 struct r5dev *dev = &sh->dev[i];
1056 if (test_bit(R5_Wantfill, &dev->flags)) {
1057 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001058 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001059 dev->read = rbi = dev->toread;
1060 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001061 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001062 while (rbi && rbi->bi_sector <
1063 dev->sector + STRIPE_SECTORS) {
1064 tx = async_copy_data(0, rbi, dev->page,
1065 dev->sector, tx);
1066 rbi = r5_next_bio(rbi, dev->sector);
1067 }
1068 }
1069 }
1070
1071 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001072 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1073 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001074}
1075
Dan Williams4e7d2c02009-08-29 19:13:11 -07001076static void mark_target_uptodate(struct stripe_head *sh, int target)
1077{
1078 struct r5dev *tgt;
1079
1080 if (target < 0)
1081 return;
1082
1083 tgt = &sh->dev[target];
1084 set_bit(R5_UPTODATE, &tgt->flags);
1085 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1086 clear_bit(R5_Wantcompute, &tgt->flags);
1087}
1088
Dan Williamsac6b53b2009-07-14 13:40:19 -07001089static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001090{
1091 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001092
Harvey Harrisone46b2722008-04-28 02:15:50 -07001093 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001094 (unsigned long long)sh->sector);
1095
Dan Williamsac6b53b2009-07-14 13:40:19 -07001096 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001097 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001098 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001099
Dan Williamsecc65c92008-06-28 08:31:57 +10001100 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1101 if (sh->check_state == check_state_compute_run)
1102 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001103 set_bit(STRIPE_HANDLE, &sh->state);
1104 release_stripe(sh);
1105}
1106
Dan Williamsd6f38f32009-07-14 11:50:52 -07001107/* return a pointer to the address conversion region of the scribble buffer */
1108static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1109 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001110{
Dan Williamsd6f38f32009-07-14 11:50:52 -07001111 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
1112}
1113
1114static struct dma_async_tx_descriptor *
1115ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1116{
Dan Williams91c00922007-01-02 13:52:30 -07001117 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001118 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001119 int target = sh->ops.target;
1120 struct r5dev *tgt = &sh->dev[target];
1121 struct page *xor_dest = tgt->page;
1122 int count = 0;
1123 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001124 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001125 int i;
1126
1127 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001128 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001129 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1130
1131 for (i = disks; i--; )
1132 if (i != target)
1133 xor_srcs[count++] = sh->dev[i].page;
1134
1135 atomic_inc(&sh->count);
1136
Dan Williams0403e382009-09-08 17:42:50 -07001137 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001138 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -07001139 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001140 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001141 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001142 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001143
Dan Williams91c00922007-01-02 13:52:30 -07001144 return tx;
1145}
1146
Dan Williamsac6b53b2009-07-14 13:40:19 -07001147/* set_syndrome_sources - populate source buffers for gen_syndrome
1148 * @srcs - (struct page *) array of size sh->disks
1149 * @sh - stripe_head to parse
1150 *
1151 * Populates srcs in proper layout order for the stripe and returns the
1152 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1153 * destination buffer is recorded in srcs[count] and the Q destination
1154 * is recorded in srcs[count+1]].
1155 */
1156static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1157{
1158 int disks = sh->disks;
1159 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1160 int d0_idx = raid6_d0(sh);
1161 int count;
1162 int i;
1163
1164 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001165 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001166
1167 count = 0;
1168 i = d0_idx;
1169 do {
1170 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1171
1172 srcs[slot] = sh->dev[i].page;
1173 i = raid6_next_disk(i, disks);
1174 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001175
NeilBrowne4424fe2009-10-16 16:27:34 +11001176 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001177}
1178
1179static struct dma_async_tx_descriptor *
1180ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1181{
1182 int disks = sh->disks;
1183 struct page **blocks = percpu->scribble;
1184 int target;
1185 int qd_idx = sh->qd_idx;
1186 struct dma_async_tx_descriptor *tx;
1187 struct async_submit_ctl submit;
1188 struct r5dev *tgt;
1189 struct page *dest;
1190 int i;
1191 int count;
1192
1193 if (sh->ops.target < 0)
1194 target = sh->ops.target2;
1195 else if (sh->ops.target2 < 0)
1196 target = sh->ops.target;
1197 else
1198 /* we should only have one valid target */
1199 BUG();
1200 BUG_ON(target < 0);
1201 pr_debug("%s: stripe %llu block: %d\n",
1202 __func__, (unsigned long long)sh->sector, target);
1203
1204 tgt = &sh->dev[target];
1205 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1206 dest = tgt->page;
1207
1208 atomic_inc(&sh->count);
1209
1210 if (target == qd_idx) {
1211 count = set_syndrome_sources(blocks, sh);
1212 blocks[count] = NULL; /* regenerating p is not necessary */
1213 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001214 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1215 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001216 to_addr_conv(sh, percpu));
1217 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1218 } else {
1219 /* Compute any data- or p-drive using XOR */
1220 count = 0;
1221 for (i = disks; i-- ; ) {
1222 if (i == target || i == qd_idx)
1223 continue;
1224 blocks[count++] = sh->dev[i].page;
1225 }
1226
Dan Williams0403e382009-09-08 17:42:50 -07001227 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1228 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001229 to_addr_conv(sh, percpu));
1230 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1231 }
1232
1233 return tx;
1234}
1235
1236static struct dma_async_tx_descriptor *
1237ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1238{
1239 int i, count, disks = sh->disks;
1240 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1241 int d0_idx = raid6_d0(sh);
1242 int faila = -1, failb = -1;
1243 int target = sh->ops.target;
1244 int target2 = sh->ops.target2;
1245 struct r5dev *tgt = &sh->dev[target];
1246 struct r5dev *tgt2 = &sh->dev[target2];
1247 struct dma_async_tx_descriptor *tx;
1248 struct page **blocks = percpu->scribble;
1249 struct async_submit_ctl submit;
1250
1251 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1252 __func__, (unsigned long long)sh->sector, target, target2);
1253 BUG_ON(target < 0 || target2 < 0);
1254 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1255 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1256
Dan Williams6c910a72009-09-16 12:24:54 -07001257 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001258 * slot number conversion for 'faila' and 'failb'
1259 */
1260 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001261 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001262 count = 0;
1263 i = d0_idx;
1264 do {
1265 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1266
1267 blocks[slot] = sh->dev[i].page;
1268
1269 if (i == target)
1270 faila = slot;
1271 if (i == target2)
1272 failb = slot;
1273 i = raid6_next_disk(i, disks);
1274 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001275
1276 BUG_ON(faila == failb);
1277 if (failb < faila)
1278 swap(faila, failb);
1279 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1280 __func__, (unsigned long long)sh->sector, faila, failb);
1281
1282 atomic_inc(&sh->count);
1283
1284 if (failb == syndrome_disks+1) {
1285 /* Q disk is one of the missing disks */
1286 if (faila == syndrome_disks) {
1287 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001288 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1289 ops_complete_compute, sh,
1290 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001291 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001292 STRIPE_SIZE, &submit);
1293 } else {
1294 struct page *dest;
1295 int data_target;
1296 int qd_idx = sh->qd_idx;
1297
1298 /* Missing D+Q: recompute D from P, then recompute Q */
1299 if (target == qd_idx)
1300 data_target = target2;
1301 else
1302 data_target = target;
1303
1304 count = 0;
1305 for (i = disks; i-- ; ) {
1306 if (i == data_target || i == qd_idx)
1307 continue;
1308 blocks[count++] = sh->dev[i].page;
1309 }
1310 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001311 init_async_submit(&submit,
1312 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1313 NULL, NULL, NULL,
1314 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001315 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1316 &submit);
1317
1318 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001319 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1320 ops_complete_compute, sh,
1321 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322 return async_gen_syndrome(blocks, 0, count+2,
1323 STRIPE_SIZE, &submit);
1324 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001325 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001326 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1327 ops_complete_compute, sh,
1328 to_addr_conv(sh, percpu));
1329 if (failb == syndrome_disks) {
1330 /* We're missing D+P. */
1331 return async_raid6_datap_recov(syndrome_disks+2,
1332 STRIPE_SIZE, faila,
1333 blocks, &submit);
1334 } else {
1335 /* We're missing D+D. */
1336 return async_raid6_2data_recov(syndrome_disks+2,
1337 STRIPE_SIZE, faila, failb,
1338 blocks, &submit);
1339 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001340 }
1341}
1342
1343
Dan Williams91c00922007-01-02 13:52:30 -07001344static void ops_complete_prexor(void *stripe_head_ref)
1345{
1346 struct stripe_head *sh = stripe_head_ref;
1347
Harvey Harrisone46b2722008-04-28 02:15:50 -07001348 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001349 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001350}
1351
1352static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001353ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1354 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001355{
Dan Williams91c00922007-01-02 13:52:30 -07001356 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001357 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001358 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001359 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001360
1361 /* existing parity data subtracted */
1362 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1363
Harvey Harrisone46b2722008-04-28 02:15:50 -07001364 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001365 (unsigned long long)sh->sector);
1366
1367 for (i = disks; i--; ) {
1368 struct r5dev *dev = &sh->dev[i];
1369 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001370 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001371 xor_srcs[count++] = dev->page;
1372 }
1373
Dan Williams0403e382009-09-08 17:42:50 -07001374 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001375 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001376 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001377
1378 return tx;
1379}
1380
1381static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001382ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001383{
1384 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001385 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001386
Harvey Harrisone46b2722008-04-28 02:15:50 -07001387 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001388 (unsigned long long)sh->sector);
1389
1390 for (i = disks; i--; ) {
1391 struct r5dev *dev = &sh->dev[i];
1392 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001393
Dan Williamsd8ee0722008-06-28 08:32:06 +10001394 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001395 struct bio *wbi;
1396
Shaohua Lib17459c2012-07-19 16:01:31 +10001397 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001398 chosen = dev->towrite;
1399 dev->towrite = NULL;
1400 BUG_ON(dev->written);
1401 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001402 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001403
1404 while (wbi && wbi->bi_sector <
1405 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001406 if (wbi->bi_rw & REQ_FUA)
1407 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001408 if (wbi->bi_rw & REQ_SYNC)
1409 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001410 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001411 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001412 else
Shaohua Li620125f2012-10-11 13:49:05 +11001413 tx = async_copy_data(1, wbi, dev->page,
1414 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001415 wbi = r5_next_bio(wbi, dev->sector);
1416 }
1417 }
1418 }
1419
1420 return tx;
1421}
1422
Dan Williamsac6b53b2009-07-14 13:40:19 -07001423static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001424{
1425 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001426 int disks = sh->disks;
1427 int pd_idx = sh->pd_idx;
1428 int qd_idx = sh->qd_idx;
1429 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001430 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001431
Harvey Harrisone46b2722008-04-28 02:15:50 -07001432 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001433 (unsigned long long)sh->sector);
1434
Shaohua Libc0934f2012-05-22 13:55:05 +10001435 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001436 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001437 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001438 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001439 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001440
Dan Williams91c00922007-01-02 13:52:30 -07001441 for (i = disks; i--; ) {
1442 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001443
Tejun Heoe9c74692010-09-03 11:56:18 +02001444 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001445 if (!discard)
1446 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001447 if (fua)
1448 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001449 if (sync)
1450 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001451 }
Dan Williams91c00922007-01-02 13:52:30 -07001452 }
1453
Dan Williamsd8ee0722008-06-28 08:32:06 +10001454 if (sh->reconstruct_state == reconstruct_state_drain_run)
1455 sh->reconstruct_state = reconstruct_state_drain_result;
1456 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1457 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1458 else {
1459 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1460 sh->reconstruct_state = reconstruct_state_result;
1461 }
Dan Williams91c00922007-01-02 13:52:30 -07001462
1463 set_bit(STRIPE_HANDLE, &sh->state);
1464 release_stripe(sh);
1465}
1466
1467static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001468ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1469 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001470{
Dan Williams91c00922007-01-02 13:52:30 -07001471 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001472 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001473 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001474 int count = 0, pd_idx = sh->pd_idx, i;
1475 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001476 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001477 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001478
Harvey Harrisone46b2722008-04-28 02:15:50 -07001479 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001480 (unsigned long long)sh->sector);
1481
Shaohua Li620125f2012-10-11 13:49:05 +11001482 for (i = 0; i < sh->disks; i++) {
1483 if (pd_idx == i)
1484 continue;
1485 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1486 break;
1487 }
1488 if (i >= sh->disks) {
1489 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001490 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1491 ops_complete_reconstruct(sh);
1492 return;
1493 }
Dan Williams91c00922007-01-02 13:52:30 -07001494 /* check if prexor is active which means only process blocks
1495 * that are part of a read-modify-write (written)
1496 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001497 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1498 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001499 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1500 for (i = disks; i--; ) {
1501 struct r5dev *dev = &sh->dev[i];
1502 if (dev->written)
1503 xor_srcs[count++] = dev->page;
1504 }
1505 } else {
1506 xor_dest = sh->dev[pd_idx].page;
1507 for (i = disks; i--; ) {
1508 struct r5dev *dev = &sh->dev[i];
1509 if (i != pd_idx)
1510 xor_srcs[count++] = dev->page;
1511 }
1512 }
1513
Dan Williams91c00922007-01-02 13:52:30 -07001514 /* 1/ if we prexor'd then the dest is reused as a source
1515 * 2/ if we did not prexor then we are redoing the parity
1516 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1517 * for the synchronous xor case
1518 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001519 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001520 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1521
1522 atomic_inc(&sh->count);
1523
Dan Williamsac6b53b2009-07-14 13:40:19 -07001524 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001525 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001526 if (unlikely(count == 1))
1527 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1528 else
1529 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001530}
1531
Dan Williamsac6b53b2009-07-14 13:40:19 -07001532static void
1533ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1534 struct dma_async_tx_descriptor *tx)
1535{
1536 struct async_submit_ctl submit;
1537 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001538 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001539
1540 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1541
Shaohua Li620125f2012-10-11 13:49:05 +11001542 for (i = 0; i < sh->disks; i++) {
1543 if (sh->pd_idx == i || sh->qd_idx == i)
1544 continue;
1545 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1546 break;
1547 }
1548 if (i >= sh->disks) {
1549 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001550 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1551 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1552 ops_complete_reconstruct(sh);
1553 return;
1554 }
1555
Dan Williamsac6b53b2009-07-14 13:40:19 -07001556 count = set_syndrome_sources(blocks, sh);
1557
1558 atomic_inc(&sh->count);
1559
1560 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1561 sh, to_addr_conv(sh, percpu));
1562 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001563}
1564
1565static void ops_complete_check(void *stripe_head_ref)
1566{
1567 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001568
Harvey Harrisone46b2722008-04-28 02:15:50 -07001569 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001570 (unsigned long long)sh->sector);
1571
Dan Williamsecc65c92008-06-28 08:31:57 +10001572 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001573 set_bit(STRIPE_HANDLE, &sh->state);
1574 release_stripe(sh);
1575}
1576
Dan Williamsac6b53b2009-07-14 13:40:19 -07001577static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001578{
Dan Williams91c00922007-01-02 13:52:30 -07001579 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001580 int pd_idx = sh->pd_idx;
1581 int qd_idx = sh->qd_idx;
1582 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001583 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001584 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001585 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001586 int count;
1587 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001588
Harvey Harrisone46b2722008-04-28 02:15:50 -07001589 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001590 (unsigned long long)sh->sector);
1591
Dan Williamsac6b53b2009-07-14 13:40:19 -07001592 count = 0;
1593 xor_dest = sh->dev[pd_idx].page;
1594 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001595 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001596 if (i == pd_idx || i == qd_idx)
1597 continue;
1598 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001599 }
1600
Dan Williamsd6f38f32009-07-14 11:50:52 -07001601 init_async_submit(&submit, 0, NULL, NULL, NULL,
1602 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001603 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001604 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001605
Dan Williams91c00922007-01-02 13:52:30 -07001606 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001607 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1608 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001609}
1610
Dan Williamsac6b53b2009-07-14 13:40:19 -07001611static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1612{
1613 struct page **srcs = percpu->scribble;
1614 struct async_submit_ctl submit;
1615 int count;
1616
1617 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1618 (unsigned long long)sh->sector, checkp);
1619
1620 count = set_syndrome_sources(srcs, sh);
1621 if (!checkp)
1622 srcs[count] = NULL;
1623
1624 atomic_inc(&sh->count);
1625 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1626 sh, to_addr_conv(sh, percpu));
1627 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1628 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1629}
1630
NeilBrown51acbce2013-02-28 09:08:34 +11001631static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001632{
1633 int overlap_clear = 0, i, disks = sh->disks;
1634 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001635 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001636 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001637 struct raid5_percpu *percpu;
1638 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001639
Dan Williamsd6f38f32009-07-14 11:50:52 -07001640 cpu = get_cpu();
1641 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001642 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001643 ops_run_biofill(sh);
1644 overlap_clear++;
1645 }
1646
Dan Williams7b3a8712008-06-28 08:32:09 +10001647 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001648 if (level < 6)
1649 tx = ops_run_compute5(sh, percpu);
1650 else {
1651 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1652 tx = ops_run_compute6_1(sh, percpu);
1653 else
1654 tx = ops_run_compute6_2(sh, percpu);
1655 }
1656 /* terminate the chain if reconstruct is not set to be run */
1657 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001658 async_tx_ack(tx);
1659 }
Dan Williams91c00922007-01-02 13:52:30 -07001660
Dan Williams600aa102008-06-28 08:32:05 +10001661 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001662 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001663
Dan Williams600aa102008-06-28 08:32:05 +10001664 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001665 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001666 overlap_clear++;
1667 }
1668
Dan Williamsac6b53b2009-07-14 13:40:19 -07001669 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1670 if (level < 6)
1671 ops_run_reconstruct5(sh, percpu, tx);
1672 else
1673 ops_run_reconstruct6(sh, percpu, tx);
1674 }
Dan Williams91c00922007-01-02 13:52:30 -07001675
Dan Williamsac6b53b2009-07-14 13:40:19 -07001676 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1677 if (sh->check_state == check_state_run)
1678 ops_run_check_p(sh, percpu);
1679 else if (sh->check_state == check_state_run_q)
1680 ops_run_check_pq(sh, percpu, 0);
1681 else if (sh->check_state == check_state_run_pq)
1682 ops_run_check_pq(sh, percpu, 1);
1683 else
1684 BUG();
1685 }
Dan Williams91c00922007-01-02 13:52:30 -07001686
Dan Williams91c00922007-01-02 13:52:30 -07001687 if (overlap_clear)
1688 for (i = disks; i--; ) {
1689 struct r5dev *dev = &sh->dev[i];
1690 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1691 wake_up(&sh->raid_conf->wait_for_overlap);
1692 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001693 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001694}
1695
Shaohua Li566c09c2013-11-14 15:16:17 +11001696static int grow_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001699 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001700 if (!sh)
1701 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001702
NeilBrown3f294f42005-11-08 21:39:25 -08001703 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001704
Shaohua Lib17459c2012-07-19 16:01:31 +10001705 spin_lock_init(&sh->stripe_lock);
1706
NeilBrowne4e11e32010-06-16 16:45:16 +10001707 if (grow_buffers(sh)) {
1708 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001709 kmem_cache_free(conf->slab_cache, sh);
1710 return 0;
1711 }
Shaohua Li566c09c2013-11-14 15:16:17 +11001712 sh->hash_lock_index = hash;
NeilBrown3f294f42005-11-08 21:39:25 -08001713 /* we just created an active stripe so... */
1714 atomic_set(&sh->count, 1);
1715 atomic_inc(&conf->active_stripes);
1716 INIT_LIST_HEAD(&sh->lru);
1717 release_stripe(sh);
1718 return 1;
1719}
1720
NeilBrownd1688a62011-10-11 16:49:52 +11001721static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001722{
Christoph Lametere18b8902006-12-06 20:33:20 -08001723 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001724 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Shaohua Li566c09c2013-11-14 15:16:17 +11001725 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
NeilBrownf4be6b42010-06-01 19:37:25 +10001727 if (conf->mddev->gendisk)
1728 sprintf(conf->cache_name[0],
1729 "raid%d-%s", conf->level, mdname(conf->mddev));
1730 else
1731 sprintf(conf->cache_name[0],
1732 "raid%d-%p", conf->level, conf->mddev);
1733 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1734
NeilBrownad01c9e2006-03-27 01:18:07 -08001735 conf->active_name = 0;
1736 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001738 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (!sc)
1740 return 1;
1741 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001742 conf->pool_size = devs;
Shaohua Li566c09c2013-11-14 15:16:17 +11001743 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
1744 while (num--) {
1745 if (!grow_one_stripe(conf, hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11001747 conf->max_nr_stripes++;
1748 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
1749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751}
NeilBrown29269552006-03-27 01:18:10 -08001752
Dan Williamsd6f38f32009-07-14 11:50:52 -07001753/**
1754 * scribble_len - return the required size of the scribble region
1755 * @num - total number of disks in the array
1756 *
1757 * The size must be enough to contain:
1758 * 1/ a struct page pointer for each device in the array +2
1759 * 2/ room to convert each entry in (1) to its corresponding dma
1760 * (dma_map_page()) or page (page_address()) address.
1761 *
1762 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1763 * calculate over all devices (not just the data blocks), using zeros in place
1764 * of the P and Q blocks.
1765 */
1766static size_t scribble_len(int num)
1767{
1768 size_t len;
1769
1770 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1771
1772 return len;
1773}
1774
NeilBrownd1688a62011-10-11 16:49:52 +11001775static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001776{
1777 /* Make all the stripes able to hold 'newsize' devices.
1778 * New slots in each stripe get 'page' set to a new page.
1779 *
1780 * This happens in stages:
1781 * 1/ create a new kmem_cache and allocate the required number of
1782 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001783 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001784 * to the new stripe_heads. This will have the side effect of
1785 * freezing the array as once all stripe_heads have been collected,
1786 * no IO will be possible. Old stripe heads are freed once their
1787 * pages have been transferred over, and the old kmem_cache is
1788 * freed when all stripes are done.
1789 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1790 * we simple return a failre status - no need to clean anything up.
1791 * 4/ allocate new pages for the new slots in the new stripe_heads.
1792 * If this fails, we don't bother trying the shrink the
1793 * stripe_heads down again, we just leave them as they are.
1794 * As each stripe_head is processed the new one is released into
1795 * active service.
1796 *
1797 * Once step2 is started, we cannot afford to wait for a write,
1798 * so we use GFP_NOIO allocations.
1799 */
1800 struct stripe_head *osh, *nsh;
1801 LIST_HEAD(newstripes);
1802 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001803 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001804 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001805 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001806 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11001807 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08001808
1809 if (newsize <= conf->pool_size)
1810 return 0; /* never bother to shrink */
1811
Dan Williamsb5470dc2008-06-27 21:44:04 -07001812 err = md_allow_write(conf->mddev);
1813 if (err)
1814 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001815
NeilBrownad01c9e2006-03-27 01:18:07 -08001816 /* Step 1 */
1817 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1818 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001819 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001820 if (!sc)
1821 return -ENOMEM;
1822
1823 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001824 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001825 if (!nsh)
1826 break;
1827
NeilBrownad01c9e2006-03-27 01:18:07 -08001828 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001829 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001830
1831 list_add(&nsh->lru, &newstripes);
1832 }
1833 if (i) {
1834 /* didn't get enough, give up */
1835 while (!list_empty(&newstripes)) {
1836 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1837 list_del(&nsh->lru);
1838 kmem_cache_free(sc, nsh);
1839 }
1840 kmem_cache_destroy(sc);
1841 return -ENOMEM;
1842 }
1843 /* Step 2 - Must use GFP_NOIO now.
1844 * OK, we have enough stripes, start collecting inactive
1845 * stripes and copying them over
1846 */
Shaohua Li566c09c2013-11-14 15:16:17 +11001847 hash = 0;
1848 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08001849 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11001850 lock_device_hash_lock(conf, hash);
1851 wait_event_cmd(conf->wait_for_stripe,
1852 !list_empty(conf->inactive_list + hash),
1853 unlock_device_hash_lock(conf, hash),
1854 lock_device_hash_lock(conf, hash));
1855 osh = get_free_stripe(conf, hash);
1856 unlock_device_hash_lock(conf, hash);
NeilBrownad01c9e2006-03-27 01:18:07 -08001857 atomic_set(&nsh->count, 1);
1858 for(i=0; i<conf->pool_size; i++)
1859 nsh->dev[i].page = osh->dev[i].page;
1860 for( ; i<newsize; i++)
1861 nsh->dev[i].page = NULL;
Shaohua Li566c09c2013-11-14 15:16:17 +11001862 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08001863 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11001864 cnt++;
1865 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
1866 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
1867 hash++;
1868 cnt = 0;
1869 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001870 }
1871 kmem_cache_destroy(conf->slab_cache);
1872
1873 /* Step 3.
1874 * At this point, we are holding all the stripes so the array
1875 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001876 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001877 */
1878 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1879 if (ndisks) {
1880 for (i=0; i<conf->raid_disks; i++)
1881 ndisks[i] = conf->disks[i];
1882 kfree(conf->disks);
1883 conf->disks = ndisks;
1884 } else
1885 err = -ENOMEM;
1886
Dan Williamsd6f38f32009-07-14 11:50:52 -07001887 get_online_cpus();
1888 conf->scribble_len = scribble_len(newsize);
1889 for_each_present_cpu(cpu) {
1890 struct raid5_percpu *percpu;
1891 void *scribble;
1892
1893 percpu = per_cpu_ptr(conf->percpu, cpu);
1894 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1895
1896 if (scribble) {
1897 kfree(percpu->scribble);
1898 percpu->scribble = scribble;
1899 } else {
1900 err = -ENOMEM;
1901 break;
1902 }
1903 }
1904 put_online_cpus();
1905
NeilBrownad01c9e2006-03-27 01:18:07 -08001906 /* Step 4, return new stripes to service */
1907 while(!list_empty(&newstripes)) {
1908 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1909 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001910
NeilBrownad01c9e2006-03-27 01:18:07 -08001911 for (i=conf->raid_disks; i < newsize; i++)
1912 if (nsh->dev[i].page == NULL) {
1913 struct page *p = alloc_page(GFP_NOIO);
1914 nsh->dev[i].page = p;
1915 if (!p)
1916 err = -ENOMEM;
1917 }
1918 release_stripe(nsh);
1919 }
1920 /* critical section pass, GFP_NOIO no longer needed */
1921
1922 conf->slab_cache = sc;
1923 conf->active_name = 1-conf->active_name;
1924 conf->pool_size = newsize;
1925 return err;
1926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Shaohua Li566c09c2013-11-14 15:16:17 +11001928static int drop_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct stripe_head *sh;
1931
Shaohua Li566c09c2013-11-14 15:16:17 +11001932 spin_lock_irq(conf->hash_locks + hash);
1933 sh = get_free_stripe(conf, hash);
1934 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08001935 if (!sh)
1936 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001937 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001938 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001939 kmem_cache_free(conf->slab_cache, sh);
1940 atomic_dec(&conf->active_stripes);
1941 return 1;
1942}
1943
NeilBrownd1688a62011-10-11 16:49:52 +11001944static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001945{
Shaohua Li566c09c2013-11-14 15:16:17 +11001946 int hash;
1947 for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++)
1948 while (drop_one_stripe(conf, hash))
1949 ;
NeilBrown3f294f42005-11-08 21:39:25 -08001950
NeilBrown29fc7e32006-02-03 03:03:41 -08001951 if (conf->slab_cache)
1952 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 conf->slab_cache = NULL;
1954}
1955
NeilBrown6712ecf2007-09-27 12:47:43 +02001956static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
NeilBrown99c0fb52009-03-31 14:39:38 +11001958 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001959 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001960 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001962 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001963 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001964 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 for (i=0 ; i<disks; i++)
1967 if (bi == &sh->dev[i].req)
1968 break;
1969
Dan Williams45b42332007-07-09 11:56:43 -07001970 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1971 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 uptodate);
1973 if (i == disks) {
1974 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001975 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
NeilBrown14a75d32011-12-23 10:17:52 +11001977 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001978 /* If replacement finished while this request was outstanding,
1979 * 'replacement' might be NULL already.
1980 * In that case it moved down to 'rdev'.
1981 * rdev is not removed until all requests are finished.
1982 */
NeilBrown14a75d32011-12-23 10:17:52 +11001983 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001984 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001985 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
NeilBrown05616be2012-05-21 09:27:00 +10001987 if (use_new_offset(conf, sh))
1988 s = sh->sector + rdev->new_data_offset;
1989 else
1990 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001993 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001994 /* Note that this cannot happen on a
1995 * replacement device. We just fail those on
1996 * any error
1997 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001998 printk_ratelimited(
1999 KERN_INFO
2000 "md/raid:%s: read error corrected"
2001 " (%lu sectors at %llu on %s)\n",
2002 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002003 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002004 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002005 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002006 clear_bit(R5_ReadError, &sh->dev[i].flags);
2007 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002008 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2009 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2010
NeilBrown14a75d32011-12-23 10:17:52 +11002011 if (atomic_read(&rdev->read_errors))
2012 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002014 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002015 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002016 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002019 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002020 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2021 printk_ratelimited(
2022 KERN_WARNING
2023 "md/raid:%s: read error on replacement device "
2024 "(sector %llu on %s).\n",
2025 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002026 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002027 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002028 else if (conf->mddev->degraded >= conf->max_degraded) {
2029 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002030 printk_ratelimited(
2031 KERN_WARNING
2032 "md/raid:%s: read error not correctable "
2033 "(sector %llu on %s).\n",
2034 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002035 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002036 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002037 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002038 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002039 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002040 printk_ratelimited(
2041 KERN_WARNING
2042 "md/raid:%s: read error NOT corrected!! "
2043 "(sector %llu on %s).\n",
2044 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002045 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002046 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002047 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002048 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002049 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002050 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002051 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002052 else
2053 retry = 1;
2054 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002055 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2056 set_bit(R5_ReadError, &sh->dev[i].flags);
2057 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2058 } else
2059 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002060 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002061 clear_bit(R5_ReadError, &sh->dev[i].flags);
2062 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002063 if (!(set_bad
2064 && test_bit(In_sync, &rdev->flags)
2065 && rdev_set_badblocks(
2066 rdev, sh->sector, STRIPE_SECTORS, 0)))
2067 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
NeilBrown14a75d32011-12-23 10:17:52 +11002070 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2072 set_bit(STRIPE_HANDLE, &sh->state);
2073 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
NeilBrownd710e132008-10-13 11:55:12 +11002076static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
NeilBrown99c0fb52009-03-31 14:39:38 +11002078 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002079 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002080 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002081 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10002083 sector_t first_bad;
2084 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002085 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
NeilBrown977df362011-12-23 10:17:53 +11002087 for (i = 0 ; i < disks; i++) {
2088 if (bi == &sh->dev[i].req) {
2089 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 break;
NeilBrown977df362011-12-23 10:17:53 +11002091 }
2092 if (bi == &sh->dev[i].rreq) {
2093 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002094 if (rdev)
2095 replacement = 1;
2096 else
2097 /* rdev was removed and 'replacement'
2098 * replaced it. rdev is not removed
2099 * until all requests are finished.
2100 */
2101 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002102 break;
2103 }
2104 }
Dan Williams45b42332007-07-09 11:56:43 -07002105 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2107 uptodate);
2108 if (i == disks) {
2109 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002110 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
NeilBrown977df362011-12-23 10:17:53 +11002113 if (replacement) {
2114 if (!uptodate)
2115 md_error(conf->mddev, rdev);
2116 else if (is_badblock(rdev, sh->sector,
2117 STRIPE_SECTORS,
2118 &first_bad, &bad_sectors))
2119 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2120 } else {
2121 if (!uptodate) {
2122 set_bit(WriteErrorSeen, &rdev->flags);
2123 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002124 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2125 set_bit(MD_RECOVERY_NEEDED,
2126 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002127 } else if (is_badblock(rdev, sh->sector,
2128 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002129 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002130 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002131 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2132 /* That was a successful write so make
2133 * sure it looks like we already did
2134 * a re-write.
2135 */
2136 set_bit(R5_ReWrite, &sh->dev[i].flags);
2137 }
NeilBrown977df362011-12-23 10:17:53 +11002138 }
2139 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
NeilBrown977df362011-12-23 10:17:53 +11002141 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2142 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002144 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
NeilBrown784052e2009-03-31 15:19:07 +11002147static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
NeilBrown784052e2009-03-31 15:19:07 +11002149static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 struct r5dev *dev = &sh->dev[i];
2152
2153 bio_init(&dev->req);
2154 dev->req.bi_io_vec = &dev->vec;
2155 dev->req.bi_vcnt++;
2156 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11002158 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
NeilBrown977df362011-12-23 10:17:53 +11002160 bio_init(&dev->rreq);
2161 dev->rreq.bi_io_vec = &dev->rvec;
2162 dev->rreq.bi_vcnt++;
2163 dev->rreq.bi_max_vecs++;
2164 dev->rreq.bi_private = sh;
2165 dev->rvec.bv_page = dev->page;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002168 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
NeilBrownfd01b882011-10-11 16:47:53 +11002171static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002174 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002175 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002176 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
NeilBrown908f4fb2011-12-23 10:17:50 +11002178 spin_lock_irqsave(&conf->device_lock, flags);
2179 clear_bit(In_sync, &rdev->flags);
2180 mddev->degraded = calc_degraded(conf);
2181 spin_unlock_irqrestore(&conf->device_lock, flags);
2182 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2183
NeilBrownde393cd2011-07-28 11:31:48 +10002184 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002185 set_bit(Faulty, &rdev->flags);
2186 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2187 printk(KERN_ALERT
2188 "md/raid:%s: Disk failure on %s, disabling device.\n"
2189 "md/raid:%s: Operation continuing on %d devices.\n",
2190 mdname(mddev),
2191 bdevname(rdev->bdev, b),
2192 mdname(mddev),
2193 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196/*
2197 * Input: a 'big' sector number,
2198 * Output: index of the data and parity disk, and the sector # in them.
2199 */
NeilBrownd1688a62011-10-11 16:49:52 +11002200static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002201 int previous, int *dd_idx,
2202 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002204 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002205 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002207 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002208 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002210 int algorithm = previous ? conf->prev_algo
2211 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002212 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2213 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002214 int raid_disks = previous ? conf->previous_raid_disks
2215 : conf->raid_disks;
2216 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* First compute the information on this sector */
2219
2220 /*
2221 * Compute the chunk number and the sector offset inside the chunk
2222 */
2223 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2224 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /*
2227 * Compute the stripe number
2228 */
NeilBrown35f2a592010-04-20 14:13:34 +10002229 stripe = chunk_number;
2230 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002231 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /*
2233 * Select the parity disk based on the user selected algorithm.
2234 */
NeilBrown84789552011-07-27 11:00:36 +10002235 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002236 switch(conf->level) {
2237 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002238 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002239 break;
2240 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002241 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002243 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002244 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 (*dd_idx)++;
2246 break;
2247 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002248 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002249 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 (*dd_idx)++;
2251 break;
2252 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002253 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002254 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 break;
2256 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002257 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002258 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002260 case ALGORITHM_PARITY_0:
2261 pd_idx = 0;
2262 (*dd_idx)++;
2263 break;
2264 case ALGORITHM_PARITY_N:
2265 pd_idx = data_disks;
2266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002268 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002269 }
2270 break;
2271 case 6:
2272
NeilBrowne183eae2009-03-31 15:20:22 +11002273 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002274 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002275 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002276 qd_idx = pd_idx + 1;
2277 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002278 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002279 qd_idx = 0;
2280 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002281 (*dd_idx) += 2; /* D D P Q D */
2282 break;
2283 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002284 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002285 qd_idx = pd_idx + 1;
2286 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002287 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002288 qd_idx = 0;
2289 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002290 (*dd_idx) += 2; /* D D P Q D */
2291 break;
2292 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002293 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002294 qd_idx = (pd_idx + 1) % raid_disks;
2295 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002296 break;
2297 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002298 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002299 qd_idx = (pd_idx + 1) % raid_disks;
2300 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002301 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002302
2303 case ALGORITHM_PARITY_0:
2304 pd_idx = 0;
2305 qd_idx = 1;
2306 (*dd_idx) += 2;
2307 break;
2308 case ALGORITHM_PARITY_N:
2309 pd_idx = data_disks;
2310 qd_idx = data_disks + 1;
2311 break;
2312
2313 case ALGORITHM_ROTATING_ZERO_RESTART:
2314 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2315 * of blocks for computing Q is different.
2316 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002317 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002318 qd_idx = pd_idx + 1;
2319 if (pd_idx == raid_disks-1) {
2320 (*dd_idx)++; /* Q D D D P */
2321 qd_idx = 0;
2322 } else if (*dd_idx >= pd_idx)
2323 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002324 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002325 break;
2326
2327 case ALGORITHM_ROTATING_N_RESTART:
2328 /* Same a left_asymmetric, by first stripe is
2329 * D D D P Q rather than
2330 * Q D D D P
2331 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002332 stripe2 += 1;
2333 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002334 qd_idx = pd_idx + 1;
2335 if (pd_idx == raid_disks-1) {
2336 (*dd_idx)++; /* Q D D D P */
2337 qd_idx = 0;
2338 } else if (*dd_idx >= pd_idx)
2339 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002340 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002341 break;
2342
2343 case ALGORITHM_ROTATING_N_CONTINUE:
2344 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002345 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002346 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2347 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002348 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002349 break;
2350
2351 case ALGORITHM_LEFT_ASYMMETRIC_6:
2352 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002353 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002354 if (*dd_idx >= pd_idx)
2355 (*dd_idx)++;
2356 qd_idx = raid_disks - 1;
2357 break;
2358
2359 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002360 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002361 if (*dd_idx >= pd_idx)
2362 (*dd_idx)++;
2363 qd_idx = raid_disks - 1;
2364 break;
2365
2366 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002367 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002368 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2369 qd_idx = raid_disks - 1;
2370 break;
2371
2372 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002373 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002374 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2375 qd_idx = raid_disks - 1;
2376 break;
2377
2378 case ALGORITHM_PARITY_0_6:
2379 pd_idx = 0;
2380 (*dd_idx)++;
2381 qd_idx = raid_disks - 1;
2382 break;
2383
NeilBrown16a53ec2006-06-26 00:27:38 -07002384 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002385 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002386 }
2387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389
NeilBrown911d4ee2009-03-31 14:39:38 +11002390 if (sh) {
2391 sh->pd_idx = pd_idx;
2392 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002393 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /*
2396 * Finally, compute the new sector number
2397 */
2398 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2399 return new_sector;
2400}
2401
2402
NeilBrown784052e2009-03-31 15:19:07 +11002403static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
NeilBrownd1688a62011-10-11 16:49:52 +11002405 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002406 int raid_disks = sh->disks;
2407 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002409 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2410 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002411 int algorithm = previous ? conf->prev_algo
2412 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 sector_t stripe;
2414 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002415 sector_t chunk_number;
2416 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002418 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
NeilBrown16a53ec2006-06-26 00:27:38 -07002420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2422 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
NeilBrown16a53ec2006-06-26 00:27:38 -07002424 if (i == sh->pd_idx)
2425 return 0;
2426 switch(conf->level) {
2427 case 4: break;
2428 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002429 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 case ALGORITHM_LEFT_ASYMMETRIC:
2431 case ALGORITHM_RIGHT_ASYMMETRIC:
2432 if (i > sh->pd_idx)
2433 i--;
2434 break;
2435 case ALGORITHM_LEFT_SYMMETRIC:
2436 case ALGORITHM_RIGHT_SYMMETRIC:
2437 if (i < sh->pd_idx)
2438 i += raid_disks;
2439 i -= (sh->pd_idx + 1);
2440 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002441 case ALGORITHM_PARITY_0:
2442 i -= 1;
2443 break;
2444 case ALGORITHM_PARITY_N:
2445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002447 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002448 }
2449 break;
2450 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002451 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002452 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002453 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002454 case ALGORITHM_LEFT_ASYMMETRIC:
2455 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002456 case ALGORITHM_ROTATING_ZERO_RESTART:
2457 case ALGORITHM_ROTATING_N_RESTART:
2458 if (sh->pd_idx == raid_disks-1)
2459 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002460 else if (i > sh->pd_idx)
2461 i -= 2; /* D D P Q D */
2462 break;
2463 case ALGORITHM_LEFT_SYMMETRIC:
2464 case ALGORITHM_RIGHT_SYMMETRIC:
2465 if (sh->pd_idx == raid_disks-1)
2466 i--; /* Q D D D P */
2467 else {
2468 /* D D P Q D */
2469 if (i < sh->pd_idx)
2470 i += raid_disks;
2471 i -= (sh->pd_idx + 2);
2472 }
2473 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002474 case ALGORITHM_PARITY_0:
2475 i -= 2;
2476 break;
2477 case ALGORITHM_PARITY_N:
2478 break;
2479 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002480 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002481 if (sh->pd_idx == 0)
2482 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002483 else {
2484 /* D D Q P D */
2485 if (i < sh->pd_idx)
2486 i += raid_disks;
2487 i -= (sh->pd_idx + 1);
2488 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002489 break;
2490 case ALGORITHM_LEFT_ASYMMETRIC_6:
2491 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2492 if (i > sh->pd_idx)
2493 i--;
2494 break;
2495 case ALGORITHM_LEFT_SYMMETRIC_6:
2496 case ALGORITHM_RIGHT_SYMMETRIC_6:
2497 if (i < sh->pd_idx)
2498 i += data_disks + 1;
2499 i -= (sh->pd_idx + 1);
2500 break;
2501 case ALGORITHM_PARITY_0_6:
2502 i -= 1;
2503 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002504 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002505 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002506 }
2507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
2509
2510 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002511 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
NeilBrown112bf892009-03-31 14:39:38 +11002513 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002514 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002515 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2516 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002517 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2518 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
2520 }
2521 return r_sector;
2522}
2523
2524
Dan Williams600aa102008-06-28 08:32:05 +10002525static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002526schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002527 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002528{
2529 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002530 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002531 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002532
Dan Williamse33129d2007-01-02 13:52:30 -07002533 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002534
2535 for (i = disks; i--; ) {
2536 struct r5dev *dev = &sh->dev[i];
2537
2538 if (dev->towrite) {
2539 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002540 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002541 if (!expand)
2542 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002543 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002544 }
2545 }
NeilBrownce7d3632013-03-04 12:37:14 +11002546 /* if we are not expanding this is a proper write request, and
2547 * there will be bios with new data to be drained into the
2548 * stripe cache
2549 */
2550 if (!expand) {
2551 if (!s->locked)
2552 /* False alarm, nothing to do */
2553 return;
2554 sh->reconstruct_state = reconstruct_state_drain_run;
2555 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2556 } else
2557 sh->reconstruct_state = reconstruct_state_run;
2558
2559 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2560
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002561 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002562 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002563 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002564 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002565 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002566 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2567 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2568
Dan Williamse33129d2007-01-02 13:52:30 -07002569 for (i = disks; i--; ) {
2570 struct r5dev *dev = &sh->dev[i];
2571 if (i == pd_idx)
2572 continue;
2573
Dan Williamse33129d2007-01-02 13:52:30 -07002574 if (dev->towrite &&
2575 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002576 test_bit(R5_Wantcompute, &dev->flags))) {
2577 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002578 set_bit(R5_LOCKED, &dev->flags);
2579 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002580 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002581 }
2582 }
NeilBrownce7d3632013-03-04 12:37:14 +11002583 if (!s->locked)
2584 /* False alarm - nothing to do */
2585 return;
2586 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2587 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2588 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2589 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002590 }
2591
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002592 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002593 * are in flight
2594 */
2595 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2596 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002597 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002598
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002599 if (level == 6) {
2600 int qd_idx = sh->qd_idx;
2601 struct r5dev *dev = &sh->dev[qd_idx];
2602
2603 set_bit(R5_LOCKED, &dev->flags);
2604 clear_bit(R5_UPTODATE, &dev->flags);
2605 s->locked++;
2606 }
2607
Dan Williams600aa102008-06-28 08:32:05 +10002608 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002609 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002610 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002611}
NeilBrown16a53ec2006-06-26 00:27:38 -07002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613/*
2614 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002615 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 * The bi_next chain must be in order.
2617 */
2618static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2619{
2620 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002621 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002622 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
NeilBrowncbe47ec2011-07-26 11:20:35 +10002624 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 (unsigned long long)bi->bi_sector,
2626 (unsigned long long)sh->sector);
2627
Shaohua Lib17459c2012-07-19 16:01:31 +10002628 /*
2629 * If several bio share a stripe. The bio bi_phys_segments acts as a
2630 * reference count to avoid race. The reference count should already be
2631 * increased before this function is called (for example, in
2632 * make_request()), so other bio sharing this stripe will not free the
2633 * stripe. If a stripe is owned by one stripe, the stripe lock will
2634 * protect it.
2635 */
2636 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002637 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002639 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002640 firstwrite = 1;
2641 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 bip = &sh->dev[dd_idx].toread;
2643 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002644 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 goto overlap;
2646 bip = & (*bip)->bi_next;
2647 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002648 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 goto overlap;
2650
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002651 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (*bip)
2653 bi->bi_next = *bip;
2654 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002655 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (forwrite) {
2658 /* check if page is covered */
2659 sector_t sector = sh->dev[dd_idx].sector;
2660 for (bi=sh->dev[dd_idx].towrite;
2661 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2662 bi && bi->bi_sector <= sector;
2663 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002664 if (bio_end_sector(bi) >= sector)
2665 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
2667 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2668 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2669 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002670
2671 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2672 (unsigned long long)(*bip)->bi_sector,
2673 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002674 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002675
2676 if (conf->mddev->bitmap && firstwrite) {
2677 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2678 STRIPE_SECTORS, 0);
2679 sh->bm_seq = conf->seq_flush+1;
2680 set_bit(STRIPE_BIT_DELAY, &sh->state);
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 return 1;
2683
2684 overlap:
2685 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002686 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return 0;
2688}
2689
NeilBrownd1688a62011-10-11 16:49:52 +11002690static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002691
NeilBrownd1688a62011-10-11 16:49:52 +11002692static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002693 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002694{
NeilBrown784052e2009-03-31 15:19:07 +11002695 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002696 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002697 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002698 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002699 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002700
NeilBrown112bf892009-03-31 14:39:38 +11002701 raid5_compute_sector(conf,
2702 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002703 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002704 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002705 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002706}
2707
Dan Williamsa4456852007-07-09 11:56:43 -07002708static void
NeilBrownd1688a62011-10-11 16:49:52 +11002709handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002710 struct stripe_head_state *s, int disks,
2711 struct bio **return_bi)
2712{
2713 int i;
2714 for (i = disks; i--; ) {
2715 struct bio *bi;
2716 int bitmap_end = 0;
2717
2718 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002719 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002720 rcu_read_lock();
2721 rdev = rcu_dereference(conf->disks[i].rdev);
2722 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002723 atomic_inc(&rdev->nr_pending);
2724 else
2725 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002726 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002727 if (rdev) {
2728 if (!rdev_set_badblocks(
2729 rdev,
2730 sh->sector,
2731 STRIPE_SECTORS, 0))
2732 md_error(conf->mddev, rdev);
2733 rdev_dec_pending(rdev, conf->mddev);
2734 }
Dan Williamsa4456852007-07-09 11:56:43 -07002735 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002736 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002737 /* fail all writes first */
2738 bi = sh->dev[i].towrite;
2739 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002740 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002741 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002742 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002743
2744 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2745 wake_up(&conf->wait_for_overlap);
2746
2747 while (bi && bi->bi_sector <
2748 sh->dev[i].sector + STRIPE_SECTORS) {
2749 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2750 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002751 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002752 md_write_end(conf->mddev);
2753 bi->bi_next = *return_bi;
2754 *return_bi = bi;
2755 }
2756 bi = nextbi;
2757 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002758 if (bitmap_end)
2759 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2760 STRIPE_SECTORS, 0, 0);
2761 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002762 /* and fail all 'written' */
2763 bi = sh->dev[i].written;
2764 sh->dev[i].written = NULL;
2765 if (bi) bitmap_end = 1;
2766 while (bi && bi->bi_sector <
2767 sh->dev[i].sector + STRIPE_SECTORS) {
2768 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2769 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002770 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002771 md_write_end(conf->mddev);
2772 bi->bi_next = *return_bi;
2773 *return_bi = bi;
2774 }
2775 bi = bi2;
2776 }
2777
Dan Williamsb5e98d62007-01-02 13:52:31 -07002778 /* fail any reads if this device is non-operational and
2779 * the data has not reached the cache yet.
2780 */
2781 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2782 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2783 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002784 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002785 bi = sh->dev[i].toread;
2786 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002787 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002788 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2789 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002790 while (bi && bi->bi_sector <
2791 sh->dev[i].sector + STRIPE_SECTORS) {
2792 struct bio *nextbi =
2793 r5_next_bio(bi, sh->dev[i].sector);
2794 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002795 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002796 bi->bi_next = *return_bi;
2797 *return_bi = bi;
2798 }
2799 bi = nextbi;
2800 }
2801 }
Dan Williamsa4456852007-07-09 11:56:43 -07002802 if (bitmap_end)
2803 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2804 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002805 /* If we were in the middle of a write the parity block might
2806 * still be locked - so just clear all R5_LOCKED flags
2807 */
2808 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002809 }
2810
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002811 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2812 if (atomic_dec_and_test(&conf->pending_full_writes))
2813 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002814}
2815
NeilBrown7f0da592011-07-28 11:39:22 +10002816static void
NeilBrownd1688a62011-10-11 16:49:52 +11002817handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002818 struct stripe_head_state *s)
2819{
2820 int abort = 0;
2821 int i;
2822
NeilBrown7f0da592011-07-28 11:39:22 +10002823 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002824 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2825 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002826 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002827 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002828 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002829 * Don't even need to abort as that is handled elsewhere
2830 * if needed, and not always wanted e.g. if there is a known
2831 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002832 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002833 * non-sync devices, or abort the recovery
2834 */
NeilBrown18b98372012-04-01 23:48:38 +10002835 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2836 /* During recovery devices cannot be removed, so
2837 * locking and refcounting of rdevs is not needed
2838 */
2839 for (i = 0; i < conf->raid_disks; i++) {
2840 struct md_rdev *rdev = conf->disks[i].rdev;
2841 if (rdev
2842 && !test_bit(Faulty, &rdev->flags)
2843 && !test_bit(In_sync, &rdev->flags)
2844 && !rdev_set_badblocks(rdev, sh->sector,
2845 STRIPE_SECTORS, 0))
2846 abort = 1;
2847 rdev = conf->disks[i].replacement;
2848 if (rdev
2849 && !test_bit(Faulty, &rdev->flags)
2850 && !test_bit(In_sync, &rdev->flags)
2851 && !rdev_set_badblocks(rdev, sh->sector,
2852 STRIPE_SECTORS, 0))
2853 abort = 1;
2854 }
2855 if (abort)
2856 conf->recovery_disabled =
2857 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002858 }
NeilBrown18b98372012-04-01 23:48:38 +10002859 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002860}
2861
NeilBrown9a3e1102011-12-23 10:17:53 +11002862static int want_replace(struct stripe_head *sh, int disk_idx)
2863{
2864 struct md_rdev *rdev;
2865 int rv = 0;
2866 /* Doing recovery so rcu locking not required */
2867 rdev = sh->raid_conf->disks[disk_idx].replacement;
2868 if (rdev
2869 && !test_bit(Faulty, &rdev->flags)
2870 && !test_bit(In_sync, &rdev->flags)
2871 && (rdev->recovery_offset <= sh->sector
2872 || rdev->mddev->recovery_cp <= sh->sector))
2873 rv = 1;
2874
2875 return rv;
2876}
2877
NeilBrown93b3dbc2011-07-27 11:00:36 +10002878/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002879 * to be read or computed to satisfy a request.
2880 *
2881 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002882 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002883 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002884static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2885 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002886{
2887 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002888 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2889 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002890
Dan Williamsf38e1212007-01-02 13:52:30 -07002891 /* is the data in this block needed, and can we get it? */
2892 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002893 !test_bit(R5_UPTODATE, &dev->flags) &&
2894 (dev->toread ||
2895 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2896 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002897 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002898 (s->failed >= 1 && fdev[0]->toread) ||
2899 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002900 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2901 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2902 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002903 /* we would like to get this block, possibly by computing it,
2904 * otherwise read it if the backing disk is insync
2905 */
2906 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2907 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2908 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002909 (s->failed && (disk_idx == s->failed_num[0] ||
2910 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002911 /* have disk failed, and we're requested to fetch it;
2912 * do compute it
2913 */
2914 pr_debug("Computing stripe %llu block %d\n",
2915 (unsigned long long)sh->sector, disk_idx);
2916 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2917 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2918 set_bit(R5_Wantcompute, &dev->flags);
2919 sh->ops.target = disk_idx;
2920 sh->ops.target2 = -1; /* no 2nd target */
2921 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002922 /* Careful: from this point on 'uptodate' is in the eye
2923 * of raid_run_ops which services 'compute' operations
2924 * before writes. R5_Wantcompute flags a block that will
2925 * be R5_UPTODATE by the time it is needed for a
2926 * subsequent operation.
2927 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002928 s->uptodate++;
2929 return 1;
2930 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2931 /* Computing 2-failure is *very* expensive; only
2932 * do it if failed >= 2
2933 */
2934 int other;
2935 for (other = disks; other--; ) {
2936 if (other == disk_idx)
2937 continue;
2938 if (!test_bit(R5_UPTODATE,
2939 &sh->dev[other].flags))
2940 break;
2941 }
2942 BUG_ON(other < 0);
2943 pr_debug("Computing stripe %llu blocks %d,%d\n",
2944 (unsigned long long)sh->sector,
2945 disk_idx, other);
2946 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2947 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2948 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2949 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2950 sh->ops.target = disk_idx;
2951 sh->ops.target2 = other;
2952 s->uptodate += 2;
2953 s->req_compute = 1;
2954 return 1;
2955 } else if (test_bit(R5_Insync, &dev->flags)) {
2956 set_bit(R5_LOCKED, &dev->flags);
2957 set_bit(R5_Wantread, &dev->flags);
2958 s->locked++;
2959 pr_debug("Reading block %d (sync=%d)\n",
2960 disk_idx, s->syncing);
2961 }
2962 }
2963
2964 return 0;
2965}
2966
2967/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002968 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002969 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002970static void handle_stripe_fill(struct stripe_head *sh,
2971 struct stripe_head_state *s,
2972 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002973{
2974 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002975
2976 /* look for blocks to read/compute, skip this if a compute
2977 * is already in flight, or if the stripe contents are in the
2978 * midst of changing due to a write
2979 */
2980 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2981 !sh->reconstruct_state)
2982 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002983 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002984 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002985 set_bit(STRIPE_HANDLE, &sh->state);
2986}
2987
2988
Dan Williams1fe797e2008-06-28 09:16:30 +10002989/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002990 * any written block on an uptodate or failed drive can be returned.
2991 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2992 * never LOCKED, so we don't need to test 'failed' directly.
2993 */
NeilBrownd1688a62011-10-11 16:49:52 +11002994static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002995 struct stripe_head *sh, int disks, struct bio **return_bi)
2996{
2997 int i;
2998 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002999 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003000
3001 for (i = disks; i--; )
3002 if (sh->dev[i].written) {
3003 dev = &sh->dev[i];
3004 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003005 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11003006 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003007 /* We can return any write requests */
3008 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003009 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003010 if (test_and_clear_bit(R5_Discard, &dev->flags))
3011 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003012 wbi = dev->written;
3013 dev->written = NULL;
3014 while (wbi && wbi->bi_sector <
3015 dev->sector + STRIPE_SECTORS) {
3016 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003017 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003018 md_write_end(conf->mddev);
3019 wbi->bi_next = *return_bi;
3020 *return_bi = wbi;
3021 }
3022 wbi = wbi2;
3023 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003024 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3025 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003026 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003027 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003028 } else if (test_bit(R5_Discard, &dev->flags))
3029 discard_pending = 1;
3030 }
3031 if (!discard_pending &&
3032 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3033 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3034 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3035 if (sh->qd_idx >= 0) {
3036 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3037 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3038 }
3039 /* now that discard is done we can proceed with any sync */
3040 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003041 /*
3042 * SCSI discard will change some bio fields and the stripe has
3043 * no updated data, so remove it from hash list and the stripe
3044 * will be reinitialized
3045 */
3046 spin_lock_irq(&conf->device_lock);
3047 remove_hash(sh);
3048 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003049 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3050 set_bit(STRIPE_HANDLE, &sh->state);
3051
3052 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003053
3054 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3055 if (atomic_dec_and_test(&conf->pending_full_writes))
3056 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003057}
3058
NeilBrownd1688a62011-10-11 16:49:52 +11003059static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003060 struct stripe_head *sh,
3061 struct stripe_head_state *s,
3062 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003063{
3064 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003065 sector_t recovery_cp = conf->mddev->recovery_cp;
3066
3067 /* RAID6 requires 'rcw' in current implementation.
3068 * Otherwise, check whether resync is now happening or should start.
3069 * If yes, then the array is dirty (after unclean shutdown or
3070 * initial creation), so parity in some stripes might be inconsistent.
3071 * In this case, we need to always do reconstruct-write, to ensure
3072 * that in case of drive failure or read-error correction, we
3073 * generate correct data from the parity.
3074 */
3075 if (conf->max_degraded == 2 ||
3076 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
3077 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003078 * look like rcw is cheaper
3079 */
3080 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003081 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3082 conf->max_degraded, (unsigned long long)recovery_cp,
3083 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003084 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003085 /* would I have to read this buffer for read_modify_write */
3086 struct r5dev *dev = &sh->dev[i];
3087 if ((dev->towrite || i == sh->pd_idx) &&
3088 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003089 !(test_bit(R5_UPTODATE, &dev->flags) ||
3090 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003091 if (test_bit(R5_Insync, &dev->flags))
3092 rmw++;
3093 else
3094 rmw += 2*disks; /* cannot read it */
3095 }
3096 /* Would I have to read this buffer for reconstruct_write */
3097 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
3098 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003099 !(test_bit(R5_UPTODATE, &dev->flags) ||
3100 test_bit(R5_Wantcompute, &dev->flags))) {
3101 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003102 else
3103 rcw += 2*disks;
3104 }
3105 }
Dan Williams45b42332007-07-09 11:56:43 -07003106 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003107 (unsigned long long)sh->sector, rmw, rcw);
3108 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11003109 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003110 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003111 if (conf->mddev->queue)
3112 blk_add_trace_msg(conf->mddev->queue,
3113 "raid5 rmw %llu %d",
3114 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003115 for (i = disks; i--; ) {
3116 struct r5dev *dev = &sh->dev[i];
3117 if ((dev->towrite || i == sh->pd_idx) &&
3118 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003119 !(test_bit(R5_UPTODATE, &dev->flags) ||
3120 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003121 test_bit(R5_Insync, &dev->flags)) {
3122 if (
3123 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003124 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11003125 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07003126 set_bit(R5_LOCKED, &dev->flags);
3127 set_bit(R5_Wantread, &dev->flags);
3128 s->locked++;
3129 } else {
3130 set_bit(STRIPE_DELAYED, &sh->state);
3131 set_bit(STRIPE_HANDLE, &sh->state);
3132 }
3133 }
3134 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003135 }
NeilBrownc8ac1802011-07-27 11:00:36 +10003136 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003137 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003138 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003139 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003140 for (i = disks; i--; ) {
3141 struct r5dev *dev = &sh->dev[i];
3142 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003143 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003144 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003145 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003146 test_bit(R5_Wantcompute, &dev->flags))) {
3147 rcw++;
3148 if (!test_bit(R5_Insync, &dev->flags))
3149 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07003150 if (
3151 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003152 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003153 "%d for Reconstruct\n", i);
3154 set_bit(R5_LOCKED, &dev->flags);
3155 set_bit(R5_Wantread, &dev->flags);
3156 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003157 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003158 } else {
3159 set_bit(STRIPE_DELAYED, &sh->state);
3160 set_bit(STRIPE_HANDLE, &sh->state);
3161 }
3162 }
3163 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003164 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003165 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3166 (unsigned long long)sh->sector,
3167 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003168 }
Dan Williamsa4456852007-07-09 11:56:43 -07003169 /* now if nothing is locked, and if we have enough data,
3170 * we can start a write request
3171 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003172 /* since handle_stripe can be called at any time we need to handle the
3173 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003174 * subsequent call wants to start a write request. raid_run_ops only
3175 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003176 * simultaneously. If this is not the case then new writes need to be
3177 * held off until the compute completes.
3178 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003179 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3180 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3181 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003182 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003183}
3184
NeilBrownd1688a62011-10-11 16:49:52 +11003185static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003186 struct stripe_head_state *s, int disks)
3187{
Dan Williamsecc65c92008-06-28 08:31:57 +10003188 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003189
Dan Williamsbd2ab672008-04-10 21:29:27 -07003190 set_bit(STRIPE_HANDLE, &sh->state);
3191
Dan Williamsecc65c92008-06-28 08:31:57 +10003192 switch (sh->check_state) {
3193 case check_state_idle:
3194 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003195 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003196 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003197 sh->check_state = check_state_run;
3198 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003199 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003200 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003201 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003202 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003203 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003204 /* fall through */
3205 case check_state_compute_result:
3206 sh->check_state = check_state_idle;
3207 if (!dev)
3208 dev = &sh->dev[sh->pd_idx];
3209
3210 /* check that a write has not made the stripe insync */
3211 if (test_bit(STRIPE_INSYNC, &sh->state))
3212 break;
3213
3214 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003215 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3216 BUG_ON(s->uptodate != disks);
3217
3218 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003219 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003220 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003221
Dan Williamsa4456852007-07-09 11:56:43 -07003222 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003223 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003224 break;
3225 case check_state_run:
3226 break; /* we will be called again upon completion */
3227 case check_state_check_result:
3228 sh->check_state = check_state_idle;
3229
3230 /* if a failure occurred during the check operation, leave
3231 * STRIPE_INSYNC not set and let the stripe be handled again
3232 */
3233 if (s->failed)
3234 break;
3235
3236 /* handle a successful check operation, if parity is correct
3237 * we are done. Otherwise update the mismatch count and repair
3238 * parity if !MD_RECOVERY_CHECK
3239 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003240 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003241 /* parity is correct (on disc,
3242 * not in buffer any more)
3243 */
3244 set_bit(STRIPE_INSYNC, &sh->state);
3245 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003246 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003247 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3248 /* don't try to repair!! */
3249 set_bit(STRIPE_INSYNC, &sh->state);
3250 else {
3251 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003252 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003253 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3254 set_bit(R5_Wantcompute,
3255 &sh->dev[sh->pd_idx].flags);
3256 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003257 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003258 s->uptodate++;
3259 }
3260 }
3261 break;
3262 case check_state_compute_run:
3263 break;
3264 default:
3265 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3266 __func__, sh->check_state,
3267 (unsigned long long) sh->sector);
3268 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003269 }
3270}
3271
3272
NeilBrownd1688a62011-10-11 16:49:52 +11003273static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003274 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003275 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003276{
Dan Williamsa4456852007-07-09 11:56:43 -07003277 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003278 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003279 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003280
3281 set_bit(STRIPE_HANDLE, &sh->state);
3282
3283 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003284
Dan Williamsa4456852007-07-09 11:56:43 -07003285 /* Want to check and possibly repair P and Q.
3286 * However there could be one 'failed' device, in which
3287 * case we can only check one of them, possibly using the
3288 * other to generate missing data
3289 */
3290
Dan Williamsd82dfee2009-07-14 13:40:57 -07003291 switch (sh->check_state) {
3292 case check_state_idle:
3293 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003294 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003295 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003296 * makes sense to check P (If anything else were failed,
3297 * we would have used P to recreate it).
3298 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003299 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003300 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003301 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003302 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003303 * anything, so it makes sense to check it
3304 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003305 if (sh->check_state == check_state_run)
3306 sh->check_state = check_state_run_pq;
3307 else
3308 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003309 }
Dan Williams36d1c642009-07-14 11:48:22 -07003310
Dan Williamsd82dfee2009-07-14 13:40:57 -07003311 /* discard potentially stale zero_sum_result */
3312 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003313
Dan Williamsd82dfee2009-07-14 13:40:57 -07003314 if (sh->check_state == check_state_run) {
3315 /* async_xor_zero_sum destroys the contents of P */
3316 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3317 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003318 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003319 if (sh->check_state >= check_state_run &&
3320 sh->check_state <= check_state_run_pq) {
3321 /* async_syndrome_zero_sum preserves P and Q, so
3322 * no need to mark them !uptodate here
3323 */
3324 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3325 break;
3326 }
Dan Williams36d1c642009-07-14 11:48:22 -07003327
Dan Williamsd82dfee2009-07-14 13:40:57 -07003328 /* we have 2-disk failure */
3329 BUG_ON(s->failed != 2);
3330 /* fall through */
3331 case check_state_compute_result:
3332 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003333
Dan Williamsd82dfee2009-07-14 13:40:57 -07003334 /* check that a write has not made the stripe insync */
3335 if (test_bit(STRIPE_INSYNC, &sh->state))
3336 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003337
3338 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003339 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003340 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003341 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003342 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003343 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003344 s->locked++;
3345 set_bit(R5_LOCKED, &dev->flags);
3346 set_bit(R5_Wantwrite, &dev->flags);
3347 }
3348 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003349 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003350 s->locked++;
3351 set_bit(R5_LOCKED, &dev->flags);
3352 set_bit(R5_Wantwrite, &dev->flags);
3353 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003354 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003355 dev = &sh->dev[pd_idx];
3356 s->locked++;
3357 set_bit(R5_LOCKED, &dev->flags);
3358 set_bit(R5_Wantwrite, &dev->flags);
3359 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003360 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003361 dev = &sh->dev[qd_idx];
3362 s->locked++;
3363 set_bit(R5_LOCKED, &dev->flags);
3364 set_bit(R5_Wantwrite, &dev->flags);
3365 }
3366 clear_bit(STRIPE_DEGRADED, &sh->state);
3367
3368 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003369 break;
3370 case check_state_run:
3371 case check_state_run_q:
3372 case check_state_run_pq:
3373 break; /* we will be called again upon completion */
3374 case check_state_check_result:
3375 sh->check_state = check_state_idle;
3376
3377 /* handle a successful check operation, if parity is correct
3378 * we are done. Otherwise update the mismatch count and repair
3379 * parity if !MD_RECOVERY_CHECK
3380 */
3381 if (sh->ops.zero_sum_result == 0) {
3382 /* both parities are correct */
3383 if (!s->failed)
3384 set_bit(STRIPE_INSYNC, &sh->state);
3385 else {
3386 /* in contrast to the raid5 case we can validate
3387 * parity, but still have a failure to write
3388 * back
3389 */
3390 sh->check_state = check_state_compute_result;
3391 /* Returning at this point means that we may go
3392 * off and bring p and/or q uptodate again so
3393 * we make sure to check zero_sum_result again
3394 * to verify if p or q need writeback
3395 */
3396 }
3397 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003398 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003399 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3400 /* don't try to repair!! */
3401 set_bit(STRIPE_INSYNC, &sh->state);
3402 else {
3403 int *target = &sh->ops.target;
3404
3405 sh->ops.target = -1;
3406 sh->ops.target2 = -1;
3407 sh->check_state = check_state_compute_run;
3408 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3409 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3410 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3411 set_bit(R5_Wantcompute,
3412 &sh->dev[pd_idx].flags);
3413 *target = pd_idx;
3414 target = &sh->ops.target2;
3415 s->uptodate++;
3416 }
3417 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3418 set_bit(R5_Wantcompute,
3419 &sh->dev[qd_idx].flags);
3420 *target = qd_idx;
3421 s->uptodate++;
3422 }
3423 }
3424 }
3425 break;
3426 case check_state_compute_run:
3427 break;
3428 default:
3429 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3430 __func__, sh->check_state,
3431 (unsigned long long) sh->sector);
3432 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003433 }
3434}
3435
NeilBrownd1688a62011-10-11 16:49:52 +11003436static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003437{
3438 int i;
3439
3440 /* We have read all the blocks in this stripe and now we need to
3441 * copy some of them into a target stripe for expand.
3442 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003443 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003444 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3445 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003446 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003447 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003448 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003449 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003450
NeilBrown784052e2009-03-31 15:19:07 +11003451 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003452 sector_t s = raid5_compute_sector(conf, bn, 0,
3453 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003454 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003455 if (sh2 == NULL)
3456 /* so far only the early blocks of this stripe
3457 * have been requested. When later blocks
3458 * get requested, we will try again
3459 */
3460 continue;
3461 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3462 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3463 /* must have already done this block */
3464 release_stripe(sh2);
3465 continue;
3466 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003467
3468 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003469 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003470 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003471 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003472 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003473
Dan Williamsa4456852007-07-09 11:56:43 -07003474 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3475 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3476 for (j = 0; j < conf->raid_disks; j++)
3477 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003478 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003479 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3480 break;
3481 if (j == conf->raid_disks) {
3482 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3483 set_bit(STRIPE_HANDLE, &sh2->state);
3484 }
3485 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003486
Dan Williamsa4456852007-07-09 11:56:43 -07003487 }
NeilBrowna2e08552007-09-11 15:23:36 -07003488 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003489 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492/*
3493 * handle_stripe - do things to a stripe.
3494 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003495 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3496 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003498 * return some read requests which now have data
3499 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 * schedule a read on some buffers
3501 * schedule a write of some buffers
3502 * return confirmation of parity correctness
3503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 */
Dan Williamsa4456852007-07-09 11:56:43 -07003505
NeilBrownacfe7262011-07-27 11:00:36 +10003506static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003507{
NeilBrownd1688a62011-10-11 16:49:52 +11003508 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003509 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003510 struct r5dev *dev;
3511 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003512 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003513
NeilBrownacfe7262011-07-27 11:00:36 +10003514 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003515
NeilBrownacfe7262011-07-27 11:00:36 +10003516 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3517 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3518 s->failed_num[0] = -1;
3519 s->failed_num[1] = -1;
3520
3521 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003522 rcu_read_lock();
3523 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003524 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003525 sector_t first_bad;
3526 int bad_sectors;
3527 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003528
NeilBrown16a53ec2006-06-26 00:27:38 -07003529 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003530
Dan Williams45b42332007-07-09 11:56:43 -07003531 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003532 i, dev->flags,
3533 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003534 /* maybe we can reply to a read
3535 *
3536 * new wantfill requests are only permitted while
3537 * ops_complete_biofill is guaranteed to be inactive
3538 */
3539 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3540 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3541 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003542
3543 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003544 if (test_bit(R5_LOCKED, &dev->flags))
3545 s->locked++;
3546 if (test_bit(R5_UPTODATE, &dev->flags))
3547 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003548 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003549 s->compute++;
3550 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003551 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003552
NeilBrownacfe7262011-07-27 11:00:36 +10003553 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003554 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003555 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003556 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003557 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003558 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003559 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003560 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003561 }
Dan Williamsa4456852007-07-09 11:56:43 -07003562 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003563 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003564 /* Prefer to use the replacement for reads, but only
3565 * if it is recovered enough and has no bad blocks.
3566 */
3567 rdev = rcu_dereference(conf->disks[i].replacement);
3568 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3569 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3570 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3571 &first_bad, &bad_sectors))
3572 set_bit(R5_ReadRepl, &dev->flags);
3573 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003574 if (rdev)
3575 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003576 rdev = rcu_dereference(conf->disks[i].rdev);
3577 clear_bit(R5_ReadRepl, &dev->flags);
3578 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003579 if (rdev && test_bit(Faulty, &rdev->flags))
3580 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003581 if (rdev) {
3582 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3583 &first_bad, &bad_sectors);
3584 if (s->blocked_rdev == NULL
3585 && (test_bit(Blocked, &rdev->flags)
3586 || is_bad < 0)) {
3587 if (is_bad < 0)
3588 set_bit(BlockedBadBlocks,
3589 &rdev->flags);
3590 s->blocked_rdev = rdev;
3591 atomic_inc(&rdev->nr_pending);
3592 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003593 }
NeilBrown415e72d2010-06-17 17:25:21 +10003594 clear_bit(R5_Insync, &dev->flags);
3595 if (!rdev)
3596 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003597 else if (is_bad) {
3598 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003599 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3600 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003601 /* treat as in-sync, but with a read error
3602 * which we can now try to correct
3603 */
3604 set_bit(R5_Insync, &dev->flags);
3605 set_bit(R5_ReadError, &dev->flags);
3606 }
3607 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003608 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003609 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003610 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003611 set_bit(R5_Insync, &dev->flags);
3612 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3613 test_bit(R5_Expanded, &dev->flags))
3614 /* If we've reshaped into here, we assume it is Insync.
3615 * We will shortly update recovery_offset to make
3616 * it official.
3617 */
3618 set_bit(R5_Insync, &dev->flags);
3619
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003620 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003621 /* This flag does not apply to '.replacement'
3622 * only to .rdev, so make sure to check that*/
3623 struct md_rdev *rdev2 = rcu_dereference(
3624 conf->disks[i].rdev);
3625 if (rdev2 == rdev)
3626 clear_bit(R5_Insync, &dev->flags);
3627 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003628 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003629 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003630 } else
3631 clear_bit(R5_WriteError, &dev->flags);
3632 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003633 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003634 /* This flag does not apply to '.replacement'
3635 * only to .rdev, so make sure to check that*/
3636 struct md_rdev *rdev2 = rcu_dereference(
3637 conf->disks[i].rdev);
3638 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003639 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003640 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003641 } else
3642 clear_bit(R5_MadeGood, &dev->flags);
3643 }
NeilBrown977df362011-12-23 10:17:53 +11003644 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3645 struct md_rdev *rdev2 = rcu_dereference(
3646 conf->disks[i].replacement);
3647 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3648 s->handle_bad_blocks = 1;
3649 atomic_inc(&rdev2->nr_pending);
3650 } else
3651 clear_bit(R5_MadeGoodRepl, &dev->flags);
3652 }
NeilBrown415e72d2010-06-17 17:25:21 +10003653 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003654 /* The ReadError flag will just be confusing now */
3655 clear_bit(R5_ReadError, &dev->flags);
3656 clear_bit(R5_ReWrite, &dev->flags);
3657 }
NeilBrown415e72d2010-06-17 17:25:21 +10003658 if (test_bit(R5_ReadError, &dev->flags))
3659 clear_bit(R5_Insync, &dev->flags);
3660 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003661 if (s->failed < 2)
3662 s->failed_num[s->failed] = i;
3663 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003664 if (rdev && !test_bit(Faulty, &rdev->flags))
3665 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003666 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003667 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003668 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3669 /* If there is a failed device being replaced,
3670 * we must be recovering.
3671 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003672 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003673 * else we can only be replacing
3674 * sync and recovery both need to read all devices, and so
3675 * use the same flag.
3676 */
3677 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003678 sh->sector >= conf->mddev->recovery_cp ||
3679 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003680 s->syncing = 1;
3681 else
3682 s->replacing = 1;
3683 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003684 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003685}
NeilBrownf4168852007-02-28 20:11:53 -08003686
NeilBrowncc940152011-07-26 11:35:35 +10003687static void handle_stripe(struct stripe_head *sh)
3688{
3689 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003690 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003691 int i;
NeilBrown84789552011-07-27 11:00:36 +10003692 int prexor;
3693 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003694 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003695
3696 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003697 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003698 /* already being handled, ensure it gets handled
3699 * again when current action finishes */
3700 set_bit(STRIPE_HANDLE, &sh->state);
3701 return;
3702 }
3703
NeilBrownf8dfcff2013-03-12 12:18:06 +11003704 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3705 spin_lock(&sh->stripe_lock);
3706 /* Cannot process 'sync' concurrently with 'discard' */
3707 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3708 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3709 set_bit(STRIPE_SYNCING, &sh->state);
3710 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003711 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003712 }
3713 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003714 }
3715 clear_bit(STRIPE_DELAYED, &sh->state);
3716
3717 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3718 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3719 (unsigned long long)sh->sector, sh->state,
3720 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3721 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003722
NeilBrownacfe7262011-07-27 11:00:36 +10003723 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003724
NeilBrownbc2607f2011-07-28 11:39:22 +10003725 if (s.handle_bad_blocks) {
3726 set_bit(STRIPE_HANDLE, &sh->state);
3727 goto finish;
3728 }
3729
NeilBrown474af965fe2011-07-27 11:00:36 +10003730 if (unlikely(s.blocked_rdev)) {
3731 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003732 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003733 set_bit(STRIPE_HANDLE, &sh->state);
3734 goto finish;
3735 }
3736 /* There is nothing for the blocked_rdev to block */
3737 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3738 s.blocked_rdev = NULL;
3739 }
3740
3741 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3742 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3743 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3744 }
3745
3746 pr_debug("locked=%d uptodate=%d to_read=%d"
3747 " to_write=%d failed=%d failed_num=%d,%d\n",
3748 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3749 s.failed_num[0], s.failed_num[1]);
3750 /* check if the array has lost more than max_degraded devices and,
3751 * if so, some requests might need to be failed.
3752 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003753 if (s.failed > conf->max_degraded) {
3754 sh->check_state = 0;
3755 sh->reconstruct_state = 0;
3756 if (s.to_read+s.to_write+s.written)
3757 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003758 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003759 handle_failed_sync(conf, sh, &s);
3760 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003761
NeilBrown84789552011-07-27 11:00:36 +10003762 /* Now we check to see if any write operations have recently
3763 * completed
3764 */
3765 prexor = 0;
3766 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3767 prexor = 1;
3768 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3769 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3770 sh->reconstruct_state = reconstruct_state_idle;
3771
3772 /* All the 'written' buffers and the parity block are ready to
3773 * be written back to disk
3774 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003775 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3776 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003777 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003778 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3779 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003780 for (i = disks; i--; ) {
3781 struct r5dev *dev = &sh->dev[i];
3782 if (test_bit(R5_LOCKED, &dev->flags) &&
3783 (i == sh->pd_idx || i == sh->qd_idx ||
3784 dev->written)) {
3785 pr_debug("Writing block %d\n", i);
3786 set_bit(R5_Wantwrite, &dev->flags);
3787 if (prexor)
3788 continue;
3789 if (!test_bit(R5_Insync, &dev->flags) ||
3790 ((i == sh->pd_idx || i == sh->qd_idx) &&
3791 s.failed == 0))
3792 set_bit(STRIPE_INSYNC, &sh->state);
3793 }
3794 }
3795 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3796 s.dec_preread_active = 1;
3797 }
3798
NeilBrownef5b7c62012-11-22 09:13:36 +11003799 /*
3800 * might be able to return some write requests if the parity blocks
3801 * are safe, or on a failed drive
3802 */
3803 pdev = &sh->dev[sh->pd_idx];
3804 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3805 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3806 qdev = &sh->dev[sh->qd_idx];
3807 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3808 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3809 || conf->level < 6;
3810
3811 if (s.written &&
3812 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3813 && !test_bit(R5_LOCKED, &pdev->flags)
3814 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3815 test_bit(R5_Discard, &pdev->flags))))) &&
3816 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3817 && !test_bit(R5_LOCKED, &qdev->flags)
3818 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3819 test_bit(R5_Discard, &qdev->flags))))))
3820 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3821
3822 /* Now we might consider reading some blocks, either to check/generate
3823 * parity, or to satisfy requests
3824 * or to load a block that is being partially written.
3825 */
3826 if (s.to_read || s.non_overwrite
3827 || (conf->level == 6 && s.to_write && s.failed)
3828 || (s.syncing && (s.uptodate + s.compute < disks))
3829 || s.replacing
3830 || s.expanding)
3831 handle_stripe_fill(sh, &s, disks);
3832
NeilBrown84789552011-07-27 11:00:36 +10003833 /* Now to consider new write requests and what else, if anything
3834 * should be read. We do not handle new writes when:
3835 * 1/ A 'write' operation (copy+xor) is already in flight.
3836 * 2/ A 'check' operation is in flight, as it may clobber the parity
3837 * block.
3838 */
3839 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3840 handle_stripe_dirtying(conf, sh, &s, disks);
3841
3842 /* maybe we need to check and possibly fix the parity for this stripe
3843 * Any reads will already have been scheduled, so we just see if enough
3844 * data is available. The parity check is held off while parity
3845 * dependent operations are in flight.
3846 */
3847 if (sh->check_state ||
3848 (s.syncing && s.locked == 0 &&
3849 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3850 !test_bit(STRIPE_INSYNC, &sh->state))) {
3851 if (conf->level == 6)
3852 handle_parity_checks6(conf, sh, &s, disks);
3853 else
3854 handle_parity_checks5(conf, sh, &s, disks);
3855 }
NeilBrownc5a31002011-07-27 11:00:36 +10003856
NeilBrownf94c0b62013-07-22 12:57:21 +10003857 if ((s.replacing || s.syncing) && s.locked == 0
3858 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3859 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003860 /* Write out to replacement devices where possible */
3861 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003862 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3863 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003864 set_bit(R5_WantReplace, &sh->dev[i].flags);
3865 set_bit(R5_LOCKED, &sh->dev[i].flags);
3866 s.locked++;
3867 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003868 if (s.replacing)
3869 set_bit(STRIPE_INSYNC, &sh->state);
3870 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003871 }
3872 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003873 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003874 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003875 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3876 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003877 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3878 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003879 }
3880
3881 /* If the failed drives are just a ReadError, then we might need
3882 * to progress the repair/check process
3883 */
3884 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3885 for (i = 0; i < s.failed; i++) {
3886 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3887 if (test_bit(R5_ReadError, &dev->flags)
3888 && !test_bit(R5_LOCKED, &dev->flags)
3889 && test_bit(R5_UPTODATE, &dev->flags)
3890 ) {
3891 if (!test_bit(R5_ReWrite, &dev->flags)) {
3892 set_bit(R5_Wantwrite, &dev->flags);
3893 set_bit(R5_ReWrite, &dev->flags);
3894 set_bit(R5_LOCKED, &dev->flags);
3895 s.locked++;
3896 } else {
3897 /* let's read it back */
3898 set_bit(R5_Wantread, &dev->flags);
3899 set_bit(R5_LOCKED, &dev->flags);
3900 s.locked++;
3901 }
3902 }
3903 }
3904
3905
NeilBrown3687c062011-07-27 11:00:36 +10003906 /* Finish reconstruct operations initiated by the expansion process */
3907 if (sh->reconstruct_state == reconstruct_state_result) {
3908 struct stripe_head *sh_src
3909 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3910 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3911 /* sh cannot be written until sh_src has been read.
3912 * so arrange for sh to be delayed a little
3913 */
3914 set_bit(STRIPE_DELAYED, &sh->state);
3915 set_bit(STRIPE_HANDLE, &sh->state);
3916 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3917 &sh_src->state))
3918 atomic_inc(&conf->preread_active_stripes);
3919 release_stripe(sh_src);
3920 goto finish;
3921 }
3922 if (sh_src)
3923 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003924
NeilBrown3687c062011-07-27 11:00:36 +10003925 sh->reconstruct_state = reconstruct_state_idle;
3926 clear_bit(STRIPE_EXPANDING, &sh->state);
3927 for (i = conf->raid_disks; i--; ) {
3928 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3929 set_bit(R5_LOCKED, &sh->dev[i].flags);
3930 s.locked++;
3931 }
3932 }
3933
3934 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3935 !sh->reconstruct_state) {
3936 /* Need to write out all blocks after computing parity */
3937 sh->disks = conf->raid_disks;
3938 stripe_set_idx(sh->sector, conf, 0, sh);
3939 schedule_reconstruction(sh, &s, 1, 1);
3940 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3941 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3942 atomic_dec(&conf->reshape_stripes);
3943 wake_up(&conf->wait_for_overlap);
3944 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3945 }
3946
3947 if (s.expanding && s.locked == 0 &&
3948 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3949 handle_stripe_expansion(conf, sh);
3950
3951finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003952 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003953 if (unlikely(s.blocked_rdev)) {
3954 if (conf->mddev->external)
3955 md_wait_for_blocked_rdev(s.blocked_rdev,
3956 conf->mddev);
3957 else
3958 /* Internal metadata will immediately
3959 * be written by raid5d, so we don't
3960 * need to wait here.
3961 */
3962 rdev_dec_pending(s.blocked_rdev,
3963 conf->mddev);
3964 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003965
NeilBrownbc2607f2011-07-28 11:39:22 +10003966 if (s.handle_bad_blocks)
3967 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003968 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003969 struct r5dev *dev = &sh->dev[i];
3970 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3971 /* We own a safe reference to the rdev */
3972 rdev = conf->disks[i].rdev;
3973 if (!rdev_set_badblocks(rdev, sh->sector,
3974 STRIPE_SECTORS, 0))
3975 md_error(conf->mddev, rdev);
3976 rdev_dec_pending(rdev, conf->mddev);
3977 }
NeilBrownb84db562011-07-28 11:39:23 +10003978 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3979 rdev = conf->disks[i].rdev;
3980 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003981 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003982 rdev_dec_pending(rdev, conf->mddev);
3983 }
NeilBrown977df362011-12-23 10:17:53 +11003984 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3985 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003986 if (!rdev)
3987 /* rdev have been moved down */
3988 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003989 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003990 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003991 rdev_dec_pending(rdev, conf->mddev);
3992 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003993 }
3994
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003995 if (s.ops_request)
3996 raid_run_ops(sh, s.ops_request);
3997
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003998 ops_run_io(sh, &s);
3999
NeilBrownc5709ef2011-07-26 11:35:20 +10004000 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004001 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004002 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004003 * have actually been submitted.
4004 */
4005 atomic_dec(&conf->preread_active_stripes);
4006 if (atomic_read(&conf->preread_active_stripes) <
4007 IO_THRESHOLD)
4008 md_wakeup_thread(conf->mddev->thread);
4009 }
4010
NeilBrownc5709ef2011-07-26 11:35:20 +10004011 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07004012
Dan Williams257a4b42011-11-08 16:22:06 +11004013 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004014}
4015
NeilBrownd1688a62011-10-11 16:49:52 +11004016static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
4018 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4019 while (!list_empty(&conf->delayed_list)) {
4020 struct list_head *l = conf->delayed_list.next;
4021 struct stripe_head *sh;
4022 sh = list_entry(l, struct stripe_head, lru);
4023 list_del_init(l);
4024 clear_bit(STRIPE_DELAYED, &sh->state);
4025 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4026 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004027 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004028 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
NeilBrown482c0832011-04-18 18:25:42 +10004030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Shaohua Li566c09c2013-11-14 15:16:17 +11004033static void activate_bit_delay(struct r5conf *conf,
4034 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004035{
4036 /* device_lock is held */
4037 struct list_head head;
4038 list_add(&head, &conf->bitmap_list);
4039 list_del_init(&conf->bitmap_list);
4040 while (!list_empty(&head)) {
4041 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004042 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004043 list_del_init(&sh->lru);
4044 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004045 hash = sh->hash_lock_index;
4046 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004047 }
4048}
4049
NeilBrownfd01b882011-10-11 16:47:53 +11004050int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004051{
NeilBrownd1688a62011-10-11 16:49:52 +11004052 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004053
4054 /* No difference between reads and writes. Just check
4055 * how busy the stripe_cache is
4056 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004057
NeilBrownf022b2f2006-10-03 01:15:56 -07004058 if (conf->inactive_blocked)
4059 return 1;
4060 if (conf->quiesce)
4061 return 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11004062 if (atomic_read(&conf->active_stripes) == conf->max_nr_stripes)
NeilBrownf022b2f2006-10-03 01:15:56 -07004063 return 1;
4064
4065 return 0;
4066}
NeilBrown11d8a6e2010-07-26 11:57:07 +10004067EXPORT_SYMBOL_GPL(md_raid5_congested);
4068
4069static int raid5_congested(void *data, int bits)
4070{
NeilBrownfd01b882011-10-11 16:47:53 +11004071 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10004072
4073 return mddev_congested(mddev, bits) ||
4074 md_raid5_congested(mddev, bits);
4075}
NeilBrownf022b2f2006-10-03 01:15:56 -07004076
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004077/* We want read requests to align with chunks where possible,
4078 * but write requests don't need to.
4079 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004080static int raid5_mergeable_bvec(struct request_queue *q,
4081 struct bvec_merge_data *bvm,
4082 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004083{
NeilBrownfd01b882011-10-11 16:47:53 +11004084 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004085 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004086 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004087 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004088 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004089
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004090 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004091 return biovec->bv_len; /* always allow writes to be mergeable */
4092
Andre Noll664e7c42009-06-18 08:45:27 +10004093 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4094 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004095 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4096 if (max < 0) max = 0;
4097 if (max <= biovec->bv_len && bio_sectors == 0)
4098 return biovec->bv_len;
4099 else
4100 return max;
4101}
4102
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004103
NeilBrownfd01b882011-10-11 16:47:53 +11004104static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004105{
4106 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10004107 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004108 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004109
Andre Noll664e7c42009-06-18 08:45:27 +10004110 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4111 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004112 return chunk_sectors >=
4113 ((sector & (chunk_sectors - 1)) + bio_sectors);
4114}
4115
4116/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004117 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4118 * later sampled by raid5d.
4119 */
NeilBrownd1688a62011-10-11 16:49:52 +11004120static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004121{
4122 unsigned long flags;
4123
4124 spin_lock_irqsave(&conf->device_lock, flags);
4125
4126 bi->bi_next = conf->retry_read_aligned_list;
4127 conf->retry_read_aligned_list = bi;
4128
4129 spin_unlock_irqrestore(&conf->device_lock, flags);
4130 md_wakeup_thread(conf->mddev->thread);
4131}
4132
4133
NeilBrownd1688a62011-10-11 16:49:52 +11004134static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004135{
4136 struct bio *bi;
4137
4138 bi = conf->retry_read_aligned;
4139 if (bi) {
4140 conf->retry_read_aligned = NULL;
4141 return bi;
4142 }
4143 bi = conf->retry_read_aligned_list;
4144 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004145 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004146 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004147 /*
4148 * this sets the active strip count to 1 and the processed
4149 * strip count to zero (upper 8 bits)
4150 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004151 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004152 }
4153
4154 return bi;
4155}
4156
4157
4158/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004159 * The "raid5_align_endio" should check if the read succeeded and if it
4160 * did, call bio_endio on the original bio (having bio_put the new bio
4161 * first).
4162 * If the read failed..
4163 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004164static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004165{
4166 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004167 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004168 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004169 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004170 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004171
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004172 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004173
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004174 rdev = (void*)raid_bi->bi_next;
4175 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004176 mddev = rdev->mddev;
4177 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004178
4179 rdev_dec_pending(rdev, conf->mddev);
4180
4181 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004182 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4183 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004184 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004185 if (atomic_dec_and_test(&conf->active_aligned_reads))
4186 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004187 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004188 }
4189
4190
Dan Williams45b42332007-07-09 11:56:43 -07004191 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004192
4193 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004194}
4195
Neil Brown387bb172007-02-08 14:20:29 -08004196static int bio_fits_rdev(struct bio *bi)
4197{
Jens Axboe165125e2007-07-24 09:28:11 +02004198 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004199
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004200 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004201 return 0;
4202 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004203 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004204 return 0;
4205
4206 if (q->merge_bvec_fn)
4207 /* it's too hard to apply the merge_bvec_fn at this stage,
4208 * just just give up
4209 */
4210 return 0;
4211
4212 return 1;
4213}
4214
4215
NeilBrownfd01b882011-10-11 16:47:53 +11004216static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004217{
NeilBrownd1688a62011-10-11 16:49:52 +11004218 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11004219 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004220 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004221 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004222 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004223
4224 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004225 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004226 return 0;
4227 }
4228 /*
NeilBrowna167f662010-10-26 18:31:13 +11004229 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004230 */
NeilBrowna167f662010-10-26 18:31:13 +11004231 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004232 if (!align_bi)
4233 return 0;
4234 /*
4235 * set bi_end_io to a new function, and set bi_private to the
4236 * original bio.
4237 */
4238 align_bi->bi_end_io = raid5_align_endio;
4239 align_bi->bi_private = raid_bio;
4240 /*
4241 * compute position
4242 */
NeilBrown112bf892009-03-31 14:39:38 +11004243 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4244 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004245 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004246
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004247 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004248 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004249 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4250 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4251 rdev->recovery_offset < end_sector) {
4252 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4253 if (rdev &&
4254 (test_bit(Faulty, &rdev->flags) ||
4255 !(test_bit(In_sync, &rdev->flags) ||
4256 rdev->recovery_offset >= end_sector)))
4257 rdev = NULL;
4258 }
4259 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004260 sector_t first_bad;
4261 int bad_sectors;
4262
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004263 atomic_inc(&rdev->nr_pending);
4264 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004265 raid_bio->bi_next = (void*)rdev;
4266 align_bi->bi_bdev = rdev->bdev;
4267 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004268
NeilBrown31c176e2011-07-28 11:39:22 +10004269 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004270 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004271 &first_bad, &bad_sectors)) {
4272 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004273 bio_put(align_bi);
4274 rdev_dec_pending(rdev, mddev);
4275 return 0;
4276 }
4277
majianpeng6c0544e2012-06-12 08:31:10 +08004278 /* No reshape active, so we can trust rdev->data_offset */
4279 align_bi->bi_sector += rdev->data_offset;
4280
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004281 spin_lock_irq(&conf->device_lock);
4282 wait_event_lock_irq(conf->wait_for_stripe,
4283 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004284 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004285 atomic_inc(&conf->active_aligned_reads);
4286 spin_unlock_irq(&conf->device_lock);
4287
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004288 if (mddev->gendisk)
4289 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4290 align_bi, disk_devt(mddev->gendisk),
4291 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004292 generic_make_request(align_bi);
4293 return 1;
4294 } else {
4295 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004296 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004297 return 0;
4298 }
4299}
4300
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004301/* __get_priority_stripe - get the next stripe to process
4302 *
4303 * Full stripe writes are allowed to pass preread active stripes up until
4304 * the bypass_threshold is exceeded. In general the bypass_count
4305 * increments when the handle_list is handled before the hold_list; however, it
4306 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4307 * stripe with in flight i/o. The bypass_count will be reset when the
4308 * head of the hold_list has changed, i.e. the head was promoted to the
4309 * handle_list.
4310 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004311static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004312{
Shaohua Li851c30c2013-08-28 14:30:16 +08004313 struct stripe_head *sh = NULL, *tmp;
4314 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004315 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004316
4317 if (conf->worker_cnt_per_group == 0) {
4318 handle_list = &conf->handle_list;
4319 } else if (group != ANY_GROUP) {
4320 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004321 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004322 } else {
4323 int i;
4324 for (i = 0; i < conf->group_cnt; i++) {
4325 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004326 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004327 if (!list_empty(handle_list))
4328 break;
4329 }
4330 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004331
4332 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4333 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004334 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004335 list_empty(&conf->hold_list) ? "empty" : "busy",
4336 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4337
Shaohua Li851c30c2013-08-28 14:30:16 +08004338 if (!list_empty(handle_list)) {
4339 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004340
4341 if (list_empty(&conf->hold_list))
4342 conf->bypass_count = 0;
4343 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4344 if (conf->hold_list.next == conf->last_hold)
4345 conf->bypass_count++;
4346 else {
4347 conf->last_hold = conf->hold_list.next;
4348 conf->bypass_count -= conf->bypass_threshold;
4349 if (conf->bypass_count < 0)
4350 conf->bypass_count = 0;
4351 }
4352 }
4353 } else if (!list_empty(&conf->hold_list) &&
4354 ((conf->bypass_threshold &&
4355 conf->bypass_count > conf->bypass_threshold) ||
4356 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004357
4358 list_for_each_entry(tmp, &conf->hold_list, lru) {
4359 if (conf->worker_cnt_per_group == 0 ||
4360 group == ANY_GROUP ||
4361 !cpu_online(tmp->cpu) ||
4362 cpu_to_group(tmp->cpu) == group) {
4363 sh = tmp;
4364 break;
4365 }
4366 }
4367
4368 if (sh) {
4369 conf->bypass_count -= conf->bypass_threshold;
4370 if (conf->bypass_count < 0)
4371 conf->bypass_count = 0;
4372 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004373 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004374 }
4375
4376 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004377 return NULL;
4378
Shaohua Libfc90cb2013-08-29 15:40:32 +08004379 if (wg) {
4380 wg->stripes_cnt--;
4381 sh->group = NULL;
4382 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004383 list_del_init(&sh->lru);
4384 atomic_inc(&sh->count);
4385 BUG_ON(atomic_read(&sh->count) != 1);
4386 return sh;
4387}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004388
Shaohua Li8811b592012-08-02 08:33:00 +10004389struct raid5_plug_cb {
4390 struct blk_plug_cb cb;
4391 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004392 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004393};
4394
4395static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4396{
4397 struct raid5_plug_cb *cb = container_of(
4398 blk_cb, struct raid5_plug_cb, cb);
4399 struct stripe_head *sh;
4400 struct mddev *mddev = cb->cb.data;
4401 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004402 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004403 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004404
4405 if (cb->list.next && !list_empty(&cb->list)) {
4406 spin_lock_irq(&conf->device_lock);
4407 while (!list_empty(&cb->list)) {
4408 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4409 list_del_init(&sh->lru);
4410 /*
4411 * avoid race release_stripe_plug() sees
4412 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4413 * is still in our list
4414 */
4415 smp_mb__before_clear_bit();
4416 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004417 /*
4418 * STRIPE_ON_RELEASE_LIST could be set here. In that
4419 * case, the count is always > 1 here
4420 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004421 hash = sh->hash_lock_index;
4422 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004423 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004424 }
4425 spin_unlock_irq(&conf->device_lock);
4426 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004427 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4428 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004429 if (mddev->queue)
4430 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004431 kfree(cb);
4432}
4433
4434static void release_stripe_plug(struct mddev *mddev,
4435 struct stripe_head *sh)
4436{
4437 struct blk_plug_cb *blk_cb = blk_check_plugged(
4438 raid5_unplug, mddev,
4439 sizeof(struct raid5_plug_cb));
4440 struct raid5_plug_cb *cb;
4441
4442 if (!blk_cb) {
4443 release_stripe(sh);
4444 return;
4445 }
4446
4447 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4448
Shaohua Li566c09c2013-11-14 15:16:17 +11004449 if (cb->list.next == NULL) {
4450 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10004451 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11004452 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4453 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4454 }
Shaohua Li8811b592012-08-02 08:33:00 +10004455
4456 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4457 list_add_tail(&sh->lru, &cb->list);
4458 else
4459 release_stripe(sh);
4460}
4461
Shaohua Li620125f2012-10-11 13:49:05 +11004462static void make_discard_request(struct mddev *mddev, struct bio *bi)
4463{
4464 struct r5conf *conf = mddev->private;
4465 sector_t logical_sector, last_sector;
4466 struct stripe_head *sh;
4467 int remaining;
4468 int stripe_sectors;
4469
4470 if (mddev->reshape_position != MaxSector)
4471 /* Skip discard while reshape is happening */
4472 return;
4473
4474 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4475 last_sector = bi->bi_sector + (bi->bi_size>>9);
4476
4477 bi->bi_next = NULL;
4478 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4479
4480 stripe_sectors = conf->chunk_sectors *
4481 (conf->raid_disks - conf->max_degraded);
4482 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4483 stripe_sectors);
4484 sector_div(last_sector, stripe_sectors);
4485
4486 logical_sector *= conf->chunk_sectors;
4487 last_sector *= conf->chunk_sectors;
4488
4489 for (; logical_sector < last_sector;
4490 logical_sector += STRIPE_SECTORS) {
4491 DEFINE_WAIT(w);
4492 int d;
4493 again:
4494 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4495 prepare_to_wait(&conf->wait_for_overlap, &w,
4496 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004497 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4498 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4499 release_stripe(sh);
4500 schedule();
4501 goto again;
4502 }
4503 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004504 spin_lock_irq(&sh->stripe_lock);
4505 for (d = 0; d < conf->raid_disks; d++) {
4506 if (d == sh->pd_idx || d == sh->qd_idx)
4507 continue;
4508 if (sh->dev[d].towrite || sh->dev[d].toread) {
4509 set_bit(R5_Overlap, &sh->dev[d].flags);
4510 spin_unlock_irq(&sh->stripe_lock);
4511 release_stripe(sh);
4512 schedule();
4513 goto again;
4514 }
4515 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004516 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004517 finish_wait(&conf->wait_for_overlap, &w);
4518 for (d = 0; d < conf->raid_disks; d++) {
4519 if (d == sh->pd_idx || d == sh->qd_idx)
4520 continue;
4521 sh->dev[d].towrite = bi;
4522 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4523 raid5_inc_bi_active_stripes(bi);
4524 }
4525 spin_unlock_irq(&sh->stripe_lock);
4526 if (conf->mddev->bitmap) {
4527 for (d = 0;
4528 d < conf->raid_disks - conf->max_degraded;
4529 d++)
4530 bitmap_startwrite(mddev->bitmap,
4531 sh->sector,
4532 STRIPE_SECTORS,
4533 0);
4534 sh->bm_seq = conf->seq_flush + 1;
4535 set_bit(STRIPE_BIT_DELAY, &sh->state);
4536 }
4537
4538 set_bit(STRIPE_HANDLE, &sh->state);
4539 clear_bit(STRIPE_DELAYED, &sh->state);
4540 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4541 atomic_inc(&conf->preread_active_stripes);
4542 release_stripe_plug(mddev, sh);
4543 }
4544
4545 remaining = raid5_dec_bi_active_stripes(bi);
4546 if (remaining == 0) {
4547 md_write_end(mddev);
4548 bio_endio(bi, 0);
4549 }
4550}
4551
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004552static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
NeilBrownd1688a62011-10-11 16:49:52 +11004554 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004555 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 sector_t new_sector;
4557 sector_t logical_sector, last_sector;
4558 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004559 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004560 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Tejun Heoe9c74692010-09-03 11:56:18 +02004562 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4563 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004564 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004565 }
4566
NeilBrown3d310eb2005-06-21 17:17:26 -07004567 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004568
NeilBrown802ba062006-12-13 00:34:13 -08004569 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004570 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004571 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004572 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004573
Shaohua Li620125f2012-10-11 13:49:05 +11004574 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4575 make_discard_request(mddev, bi);
4576 return;
4577 }
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004580 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 bi->bi_next = NULL;
4582 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4585 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004586 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10004587 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08004588
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004589 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10004590 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11004591 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004592 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004593 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004594 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004595 * 64bit on a 32bit platform, and so it might be
4596 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004597 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004598 * the lock is dropped, so once we get a reference
4599 * to the stripe that we think it is, we will have
4600 * to check again.
4601 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004602 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004603 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004604 ? logical_sector < conf->reshape_progress
4605 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004606 previous = 1;
4607 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004608 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004609 ? logical_sector < conf->reshape_safe
4610 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004611 spin_unlock_irq(&conf->device_lock);
4612 schedule();
4613 goto retry;
4614 }
4615 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004616 spin_unlock_irq(&conf->device_lock);
4617 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004618
NeilBrown112bf892009-03-31 14:39:38 +11004619 new_sector = raid5_compute_sector(conf, logical_sector,
4620 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004621 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004622 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10004623 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 (unsigned long long)logical_sector);
4625
NeilBrownb5663ba2009-03-31 14:39:38 +11004626 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004627 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004629 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004630 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004631 * stripe, so we must do the range check again.
4632 * Expansion could still move past after this
4633 * test, but as we are holding a reference to
4634 * 'sh', we know that if that happens,
4635 * STRIPE_EXPANDING will get set and the expansion
4636 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004637 */
4638 int must_retry = 0;
4639 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004640 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004641 ? logical_sector >= conf->reshape_progress
4642 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004643 /* mismatch, need to try again */
4644 must_retry = 1;
4645 spin_unlock_irq(&conf->device_lock);
4646 if (must_retry) {
4647 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004648 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004649 goto retry;
4650 }
4651 }
NeilBrownc46501b2013-08-27 15:52:13 +10004652 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4653 /* Might have got the wrong stripe_head
4654 * by accident
4655 */
4656 release_stripe(sh);
4657 goto retry;
4658 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004659
Namhyung Kimffd96e32011-07-18 17:38:51 +10004660 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004661 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004662 logical_sector < mddev->suspend_hi) {
4663 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004664 /* As the suspend_* range is controlled by
4665 * userspace, we want an interruptible
4666 * wait.
4667 */
4668 flush_signals(current);
4669 prepare_to_wait(&conf->wait_for_overlap,
4670 &w, TASK_INTERRUPTIBLE);
4671 if (logical_sector >= mddev->suspend_lo &&
4672 logical_sector < mddev->suspend_hi)
4673 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004674 goto retry;
4675 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004676
4677 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004678 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004679 /* Stripe is busy expanding or
4680 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 * and wait a while
4682 */
NeilBrown482c0832011-04-18 18:25:42 +10004683 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 release_stripe(sh);
4685 schedule();
4686 goto retry;
4687 }
4688 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004689 set_bit(STRIPE_HANDLE, &sh->state);
4690 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004691 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004692 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4693 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004694 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 } else {
4696 /* cannot get stripe for read-ahead, just give-up */
4697 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4698 finish_wait(&conf->wait_for_overlap, &w);
4699 break;
4700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004702
Shaohua Lie7836bd62012-07-19 16:01:31 +10004703 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004704 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
NeilBrown16a53ec2006-06-26 00:27:38 -07004706 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004708
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004709 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4710 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004711 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
NeilBrownfd01b882011-10-11 16:47:53 +11004715static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004716
NeilBrownfd01b882011-10-11 16:47:53 +11004717static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
NeilBrown52c03292006-06-26 00:27:43 -07004719 /* reshaping is quite different to recovery/resync so it is
4720 * handled quite separately ... here.
4721 *
4722 * On each call to sync_request, we gather one chunk worth of
4723 * destination stripes and flag them as expanding.
4724 * Then we find all the source stripes and request reads.
4725 * As the reads complete, handle_stripe will copy the data
4726 * into the destination stripe and release that stripe.
4727 */
NeilBrownd1688a62011-10-11 16:49:52 +11004728 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004730 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004731 int raid_disks = conf->previous_raid_disks;
4732 int data_disks = raid_disks - conf->max_degraded;
4733 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004734 int i;
4735 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004736 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004737 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004738 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004739 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004740
NeilBrownfef9c612009-03-31 15:16:46 +11004741 if (sector_nr == 0) {
4742 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004743 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004744 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4745 sector_nr = raid5_size(mddev, 0, 0)
4746 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004747 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004748 conf->reshape_progress > 0)
4749 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004750 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004751 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004752 mddev->curr_resync_completed = sector_nr;
4753 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004754 *skipped = 1;
4755 return sector_nr;
4756 }
NeilBrown52c03292006-06-26 00:27:43 -07004757 }
4758
NeilBrown7a661382009-03-31 15:21:40 +11004759 /* We need to process a full chunk at a time.
4760 * If old and new chunk sizes differ, we need to process the
4761 * largest of these
4762 */
Andre Noll664e7c42009-06-18 08:45:27 +10004763 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4764 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004765 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004766 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004767
NeilBrownb5254dd2012-05-21 09:27:01 +10004768 /* We update the metadata at least every 10 seconds, or when
4769 * the data about to be copied would over-write the source of
4770 * the data at the front of the range. i.e. one new_stripe
4771 * along from reshape_progress new_maps to after where
4772 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004773 */
NeilBrownfef9c612009-03-31 15:16:46 +11004774 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004775 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004776 readpos = conf->reshape_progress;
4777 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004778 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004779 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004780 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004781 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004782 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004783 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004784 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004785 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004786 readpos -= min_t(sector_t, reshape_sectors, readpos);
4787 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004788 }
NeilBrown52c03292006-06-26 00:27:43 -07004789
NeilBrownb5254dd2012-05-21 09:27:01 +10004790 /* Having calculated the 'writepos' possibly use it
4791 * to set 'stripe_addr' which is where we will write to.
4792 */
4793 if (mddev->reshape_backwards) {
4794 BUG_ON(conf->reshape_progress == 0);
4795 stripe_addr = writepos;
4796 BUG_ON((mddev->dev_sectors &
4797 ~((sector_t)reshape_sectors - 1))
4798 - reshape_sectors - stripe_addr
4799 != sector_nr);
4800 } else {
4801 BUG_ON(writepos != sector_nr + reshape_sectors);
4802 stripe_addr = sector_nr;
4803 }
4804
NeilBrownc8f517c2009-03-31 15:28:40 +11004805 /* 'writepos' is the most advanced device address we might write.
4806 * 'readpos' is the least advanced device address we might read.
4807 * 'safepos' is the least address recorded in the metadata as having
4808 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004809 * If there is a min_offset_diff, these are adjusted either by
4810 * increasing the safepos/readpos if diff is negative, or
4811 * increasing writepos if diff is positive.
4812 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004813 * ensure safety in the face of a crash - that must be done by userspace
4814 * making a backup of the data. So in that case there is no particular
4815 * rush to update metadata.
4816 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4817 * update the metadata to advance 'safepos' to match 'readpos' so that
4818 * we can be safe in the event of a crash.
4819 * So we insist on updating metadata if safepos is behind writepos and
4820 * readpos is beyond writepos.
4821 * In any case, update the metadata every 10 seconds.
4822 * Maybe that number should be configurable, but I'm not sure it is
4823 * worth it.... maybe it could be a multiple of safemode_delay???
4824 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004825 if (conf->min_offset_diff < 0) {
4826 safepos += -conf->min_offset_diff;
4827 readpos += -conf->min_offset_diff;
4828 } else
4829 writepos += conf->min_offset_diff;
4830
NeilBrown2c810cd2012-05-21 09:27:00 +10004831 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004832 ? (safepos > writepos && readpos < writepos)
4833 : (safepos < writepos && readpos > writepos)) ||
4834 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004835 /* Cannot proceed until we've updated the superblock... */
4836 wait_event(conf->wait_for_overlap,
4837 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004838 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004839 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004840 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004841 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004842 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004843 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004844 kthread_should_stop());
4845 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004846 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004847 spin_unlock_irq(&conf->device_lock);
4848 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004849 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004850 }
4851
NeilBrownab69ae12009-03-31 15:26:47 +11004852 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004853 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004854 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004855 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004856 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004857 set_bit(STRIPE_EXPANDING, &sh->state);
4858 atomic_inc(&conf->reshape_stripes);
4859 /* If any of this stripe is beyond the end of the old
4860 * array, then we need to zero those blocks
4861 */
4862 for (j=sh->disks; j--;) {
4863 sector_t s;
4864 if (j == sh->pd_idx)
4865 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004866 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004867 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004868 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004869 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004870 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004871 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004872 continue;
4873 }
4874 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4875 set_bit(R5_Expanded, &sh->dev[j].flags);
4876 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4877 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004878 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004879 set_bit(STRIPE_EXPAND_READY, &sh->state);
4880 set_bit(STRIPE_HANDLE, &sh->state);
4881 }
NeilBrownab69ae12009-03-31 15:26:47 +11004882 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004883 }
4884 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004885 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004886 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004887 else
NeilBrown7a661382009-03-31 15:21:40 +11004888 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004889 spin_unlock_irq(&conf->device_lock);
4890 /* Ok, those stripe are ready. We can start scheduling
4891 * reads on the source stripes.
4892 * The source stripes are determined by mapping the first and last
4893 * block on the destination stripes.
4894 */
NeilBrown52c03292006-06-26 00:27:43 -07004895 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004896 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004897 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004898 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004899 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004900 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004901 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004902 if (last_sector >= mddev->dev_sectors)
4903 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004904 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004905 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004906 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4907 set_bit(STRIPE_HANDLE, &sh->state);
4908 release_stripe(sh);
4909 first_sector += STRIPE_SECTORS;
4910 }
NeilBrownab69ae12009-03-31 15:26:47 +11004911 /* Now that the sources are clearly marked, we can release
4912 * the destination stripes
4913 */
4914 while (!list_empty(&stripes)) {
4915 sh = list_entry(stripes.next, struct stripe_head, lru);
4916 list_del_init(&sh->lru);
4917 release_stripe(sh);
4918 }
NeilBrownc6207272008-02-06 01:39:52 -08004919 /* If this takes us to the resync_max point where we have to pause,
4920 * then we need to write out the superblock.
4921 */
NeilBrown7a661382009-03-31 15:21:40 +11004922 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004923 if ((sector_nr - mddev->curr_resync_completed) * 2
4924 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004925 /* Cannot proceed until we've updated the superblock... */
4926 wait_event(conf->wait_for_overlap,
4927 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004928 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004929 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004930 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4932 md_wakeup_thread(mddev->thread);
4933 wait_event(mddev->sb_wait,
4934 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4935 || kthread_should_stop());
4936 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004937 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004938 spin_unlock_irq(&conf->device_lock);
4939 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004940 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004941 }
NeilBrown7a661382009-03-31 15:21:40 +11004942 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004943}
4944
4945/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004946static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004947{
NeilBrownd1688a62011-10-11 16:49:52 +11004948 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004949 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004950 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004951 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004952 int still_degraded = 0;
4953 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
NeilBrown72626682005-09-09 16:23:54 -07004955 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004957
NeilBrown29269552006-03-27 01:18:10 -08004958 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4959 end_reshape(conf);
4960 return 0;
4961 }
NeilBrown72626682005-09-09 16:23:54 -07004962
4963 if (mddev->curr_resync < max_sector) /* aborted */
4964 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4965 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004966 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004967 conf->fullsync = 0;
4968 bitmap_close_sync(mddev->bitmap);
4969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 return 0;
4971 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004972
NeilBrown64bd6602009-08-03 10:59:58 +10004973 /* Allow raid5_quiesce to complete */
4974 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4975
NeilBrown52c03292006-06-26 00:27:43 -07004976 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4977 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004978
NeilBrownc6207272008-02-06 01:39:52 -08004979 /* No need to check resync_max as we never do more than one
4980 * stripe, and as resync_max will always be on a chunk boundary,
4981 * if the check in md_do_sync didn't fire, there is no chance
4982 * of overstepping resync_max here
4983 */
4984
NeilBrown16a53ec2006-06-26 00:27:38 -07004985 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 * to resync, then assert that we are finished, because there is
4987 * nothing we can do.
4988 */
NeilBrown3285edf2006-06-26 00:27:55 -07004989 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004990 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004991 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004992 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 return rv;
4994 }
majianpeng6f608042013-04-24 11:42:41 +10004995 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4996 !conf->fullsync &&
4997 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4998 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004999 /* we can skip this block, and probably more */
5000 sync_blocks /= STRIPE_SECTORS;
5001 *skipped = 1;
5002 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
NeilBrownb47490c2008-02-06 01:39:50 -08005005 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5006
NeilBrowna8c906c2009-06-09 14:39:59 +10005007 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005009 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005011 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005013 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005015 /* Need to check if array will still be degraded after recovery/resync
5016 * We don't need to check the 'failed' flag as when that gets set,
5017 * recovery aborts.
5018 */
NeilBrownf001a702009-06-09 14:30:31 +10005019 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07005020 if (conf->disks[i].rdev == NULL)
5021 still_degraded = 1;
5022
5023 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5024
NeilBrown83206d62011-07-26 11:19:49 +10005025 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
NeilBrown14425772009-10-16 15:55:25 +11005027 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 release_stripe(sh);
5029
5030 return STRIPE_SECTORS;
5031}
5032
NeilBrownd1688a62011-10-11 16:49:52 +11005033static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005034{
5035 /* We may not be able to submit a whole bio at once as there
5036 * may not be enough stripe_heads available.
5037 * We cannot pre-allocate enough stripe_heads as we may need
5038 * more than exist in the cache (if we allow ever large chunks).
5039 * So we do one stripe head at a time and record in
5040 * ->bi_hw_segments how many have been done.
5041 *
5042 * We *know* that this entire raid_bio is in one chunk, so
5043 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5044 */
5045 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005046 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005047 sector_t sector, logical_sector, last_sector;
5048 int scnt = 0;
5049 int remaining;
5050 int handled = 0;
5051
5052 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005053 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005054 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005055 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005056
5057 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005058 logical_sector += STRIPE_SECTORS,
5059 sector += STRIPE_SECTORS,
5060 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005061
Shaohua Lie7836bd62012-07-19 16:01:31 +10005062 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005063 /* already done this stripe */
5064 continue;
5065
NeilBrowna8c906c2009-06-09 14:39:59 +10005066 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005067
5068 if (!sh) {
5069 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005070 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005071 conf->retry_read_aligned = raid_bio;
5072 return handled;
5073 }
5074
Neil Brown387bb172007-02-08 14:20:29 -08005075 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
5076 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005077 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005078 conf->retry_read_aligned = raid_bio;
5079 return handled;
5080 }
5081
majianpeng3f9e7c12012-07-31 10:04:21 +10005082 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005083 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005084 release_stripe(sh);
5085 handled++;
5086 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005087 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005088 if (remaining == 0) {
5089 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5090 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005091 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005092 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005093 if (atomic_dec_and_test(&conf->active_aligned_reads))
5094 wake_up(&conf->wait_for_stripe);
5095 return handled;
5096}
5097
Shaohua Libfc90cb2013-08-29 15:40:32 +08005098static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005099 struct r5worker *worker,
5100 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005101{
5102 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005103 int i, batch_size = 0, hash;
5104 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005105
5106 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005107 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005108 batch[batch_size++] = sh;
5109
Shaohua Li566c09c2013-11-14 15:16:17 +11005110 if (batch_size == 0) {
5111 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5112 if (!list_empty(temp_inactive_list + i))
5113 break;
5114 if (i == NR_STRIPE_HASH_LOCKS)
5115 return batch_size;
5116 release_inactive = true;
5117 }
Shaohua Li46a06402012-08-02 08:33:15 +10005118 spin_unlock_irq(&conf->device_lock);
5119
Shaohua Li566c09c2013-11-14 15:16:17 +11005120 release_inactive_stripe_list(conf, temp_inactive_list,
5121 NR_STRIPE_HASH_LOCKS);
5122
5123 if (release_inactive) {
5124 spin_lock_irq(&conf->device_lock);
5125 return 0;
5126 }
5127
Shaohua Li46a06402012-08-02 08:33:15 +10005128 for (i = 0; i < batch_size; i++)
5129 handle_stripe(batch[i]);
5130
5131 cond_resched();
5132
5133 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005134 for (i = 0; i < batch_size; i++) {
5135 hash = batch[i]->hash_lock_index;
5136 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5137 }
Shaohua Li46a06402012-08-02 08:33:15 +10005138 return batch_size;
5139}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005140
Shaohua Li851c30c2013-08-28 14:30:16 +08005141static void raid5_do_work(struct work_struct *work)
5142{
5143 struct r5worker *worker = container_of(work, struct r5worker, work);
5144 struct r5worker_group *group = worker->group;
5145 struct r5conf *conf = group->conf;
5146 int group_id = group - conf->worker_groups;
5147 int handled;
5148 struct blk_plug plug;
5149
5150 pr_debug("+++ raid5worker active\n");
5151
5152 blk_start_plug(&plug);
5153 handled = 0;
5154 spin_lock_irq(&conf->device_lock);
5155 while (1) {
5156 int batch_size, released;
5157
Shaohua Li566c09c2013-11-14 15:16:17 +11005158 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005159
Shaohua Li566c09c2013-11-14 15:16:17 +11005160 batch_size = handle_active_stripes(conf, group_id, worker,
5161 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005162 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005163 if (!batch_size && !released)
5164 break;
5165 handled += batch_size;
5166 }
5167 pr_debug("%d stripes handled\n", handled);
5168
5169 spin_unlock_irq(&conf->device_lock);
5170 blk_finish_plug(&plug);
5171
5172 pr_debug("--- raid5worker inactive\n");
5173}
5174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175/*
5176 * This is our raid5 kernel thread.
5177 *
5178 * We scan the hash table for stripes which can be handled now.
5179 * During the scan, completed stripes are saved for us by the interrupt
5180 * handler, so that they will not have to wait for our next wakeup.
5181 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005182static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183{
Shaohua Li4ed87312012-10-11 13:34:00 +11005184 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005185 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005187 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Dan Williams45b42332007-07-09 11:56:43 -07005189 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
5191 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005193 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 handled = 0;
5195 spin_lock_irq(&conf->device_lock);
5196 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005197 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005198 int batch_size, released;
5199
Shaohua Li566c09c2013-11-14 15:16:17 +11005200 released = release_stripe_list(conf, conf->temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
NeilBrown0021b7b2012-07-31 09:08:14 +02005202 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005203 !list_empty(&conf->bitmap_list)) {
5204 /* Now is a good time to flush some bitmap updates */
5205 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005206 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005207 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005208 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005209 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005210 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005211 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005212 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005213
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005214 while ((bio = remove_bio_from_retry(conf))) {
5215 int ok;
5216 spin_unlock_irq(&conf->device_lock);
5217 ok = retry_aligned_read(conf, bio);
5218 spin_lock_irq(&conf->device_lock);
5219 if (!ok)
5220 break;
5221 handled++;
5222 }
5223
Shaohua Li566c09c2013-11-14 15:16:17 +11005224 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5225 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005226 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005228 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
Shaohua Li46a06402012-08-02 08:33:15 +10005230 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5231 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005232 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005233 spin_lock_irq(&conf->device_lock);
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 }
Dan Williams45b42332007-07-09 11:56:43 -07005236 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
5238 spin_unlock_irq(&conf->device_lock);
5239
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005240 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005241 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
Dan Williams45b42332007-07-09 11:56:43 -07005243 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
NeilBrown3f294f42005-11-08 21:39:25 -08005246static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005247raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005248{
NeilBrownd1688a62011-10-11 16:49:52 +11005249 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005250 if (conf)
5251 return sprintf(page, "%d\n", conf->max_nr_stripes);
5252 else
5253 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005254}
5255
NeilBrownc41d4ac2010-06-01 19:37:24 +10005256int
NeilBrownfd01b882011-10-11 16:47:53 +11005257raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005258{
NeilBrownd1688a62011-10-11 16:49:52 +11005259 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005260 int err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005261 int hash;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005262
5263 if (size <= 16 || size > 32768)
5264 return -EINVAL;
Shaohua Li566c09c2013-11-14 15:16:17 +11005265 hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005266 while (size < conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005267 if (drop_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005268 conf->max_nr_stripes--;
5269 else
5270 break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005271 hash--;
5272 if (hash < 0)
5273 hash = NR_STRIPE_HASH_LOCKS - 1;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005274 }
5275 err = md_allow_write(mddev);
5276 if (err)
5277 return err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005278 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005279 while (size > conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005280 if (grow_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005281 conf->max_nr_stripes++;
5282 else break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005283 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005284 }
5285 return 0;
5286}
5287EXPORT_SYMBOL(raid5_set_cache_size);
5288
NeilBrown3f294f42005-11-08 21:39:25 -08005289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005290raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005291{
NeilBrownd1688a62011-10-11 16:49:52 +11005292 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005293 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005294 int err;
5295
NeilBrown3f294f42005-11-08 21:39:25 -08005296 if (len >= PAGE_SIZE)
5297 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08005298 if (!conf)
5299 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08005300
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005301 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005302 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005303 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005304 if (err)
5305 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08005306 return len;
5307}
NeilBrown007583c2005-11-08 21:39:30 -08005308
NeilBrown96de1e62005-11-08 21:39:39 -08005309static struct md_sysfs_entry
5310raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5311 raid5_show_stripe_cache_size,
5312 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005313
5314static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005315raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005316{
NeilBrownd1688a62011-10-11 16:49:52 +11005317 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005318 if (conf)
5319 return sprintf(page, "%d\n", conf->bypass_threshold);
5320 else
5321 return 0;
5322}
5323
5324static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005325raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005326{
NeilBrownd1688a62011-10-11 16:49:52 +11005327 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005328 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005329 if (len >= PAGE_SIZE)
5330 return -EINVAL;
5331 if (!conf)
5332 return -ENODEV;
5333
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005334 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005335 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005336 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005337 return -EINVAL;
5338 conf->bypass_threshold = new;
5339 return len;
5340}
5341
5342static struct md_sysfs_entry
5343raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5344 S_IRUGO | S_IWUSR,
5345 raid5_show_preread_threshold,
5346 raid5_store_preread_threshold);
5347
5348static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005349stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005350{
NeilBrownd1688a62011-10-11 16:49:52 +11005351 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005352 if (conf)
5353 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5354 else
5355 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005356}
5357
NeilBrown96de1e62005-11-08 21:39:39 -08005358static struct md_sysfs_entry
5359raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005360
Shaohua Lib7214202013-08-27 17:50:42 +08005361static ssize_t
5362raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5363{
5364 struct r5conf *conf = mddev->private;
5365 if (conf)
5366 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5367 else
5368 return 0;
5369}
5370
5371static int alloc_thread_groups(struct r5conf *conf, int cnt);
5372static ssize_t
5373raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5374{
5375 struct r5conf *conf = mddev->private;
5376 unsigned long new;
5377 int err;
5378 struct r5worker_group *old_groups;
5379 int old_group_cnt;
5380
5381 if (len >= PAGE_SIZE)
5382 return -EINVAL;
5383 if (!conf)
5384 return -ENODEV;
5385
5386 if (kstrtoul(page, 10, &new))
5387 return -EINVAL;
5388
5389 if (new == conf->worker_cnt_per_group)
5390 return len;
5391
5392 mddev_suspend(mddev);
5393
5394 old_groups = conf->worker_groups;
5395 old_group_cnt = conf->worker_cnt_per_group;
5396
5397 conf->worker_groups = NULL;
5398 err = alloc_thread_groups(conf, new);
5399 if (err) {
5400 conf->worker_groups = old_groups;
5401 conf->worker_cnt_per_group = old_group_cnt;
5402 } else {
5403 if (old_groups)
5404 kfree(old_groups[0].workers);
5405 kfree(old_groups);
5406 }
5407
5408 mddev_resume(mddev);
5409
5410 if (err)
5411 return err;
5412 return len;
5413}
5414
5415static struct md_sysfs_entry
5416raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5417 raid5_show_group_thread_cnt,
5418 raid5_store_group_thread_cnt);
5419
NeilBrown007583c2005-11-08 21:39:30 -08005420static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005421 &raid5_stripecache_size.attr,
5422 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005423 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005424 &raid5_group_thread_cnt.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005425 NULL,
5426};
NeilBrown007583c2005-11-08 21:39:30 -08005427static struct attribute_group raid5_attrs_group = {
5428 .name = NULL,
5429 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005430};
5431
Shaohua Li851c30c2013-08-28 14:30:16 +08005432static int alloc_thread_groups(struct r5conf *conf, int cnt)
5433{
Shaohua Li566c09c2013-11-14 15:16:17 +11005434 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08005435 ssize_t size;
5436 struct r5worker *workers;
5437
5438 conf->worker_cnt_per_group = cnt;
5439 if (cnt == 0) {
5440 conf->worker_groups = NULL;
5441 return 0;
5442 }
5443 conf->group_cnt = num_possible_nodes();
5444 size = sizeof(struct r5worker) * cnt;
5445 workers = kzalloc(size * conf->group_cnt, GFP_NOIO);
5446 conf->worker_groups = kzalloc(sizeof(struct r5worker_group) *
5447 conf->group_cnt, GFP_NOIO);
5448 if (!conf->worker_groups || !workers) {
5449 kfree(workers);
5450 kfree(conf->worker_groups);
5451 conf->worker_groups = NULL;
5452 return -ENOMEM;
5453 }
5454
5455 for (i = 0; i < conf->group_cnt; i++) {
5456 struct r5worker_group *group;
5457
5458 group = &conf->worker_groups[i];
5459 INIT_LIST_HEAD(&group->handle_list);
5460 group->conf = conf;
5461 group->workers = workers + i * cnt;
5462
5463 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005464 struct r5worker *worker = group->workers + j;
5465 worker->group = group;
5466 INIT_WORK(&worker->work, raid5_do_work);
5467
5468 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
5469 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08005470 }
5471 }
5472
5473 return 0;
5474}
5475
5476static void free_thread_groups(struct r5conf *conf)
5477{
5478 if (conf->worker_groups)
5479 kfree(conf->worker_groups[0].workers);
5480 kfree(conf->worker_groups);
5481 conf->worker_groups = NULL;
5482}
5483
Dan Williams80c3a6c2009-03-17 18:10:40 -07005484static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005485raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005486{
NeilBrownd1688a62011-10-11 16:49:52 +11005487 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005488
5489 if (!sectors)
5490 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005491 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005492 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005493 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005494
Andre Noll9d8f0362009-06-18 08:45:01 +10005495 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005496 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005497 return sectors * (raid_disks - conf->max_degraded);
5498}
5499
NeilBrownd1688a62011-10-11 16:49:52 +11005500static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005501{
5502 struct raid5_percpu *percpu;
5503 unsigned long cpu;
5504
5505 if (!conf->percpu)
5506 return;
5507
5508 get_online_cpus();
5509 for_each_possible_cpu(cpu) {
5510 percpu = per_cpu_ptr(conf->percpu, cpu);
5511 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005512 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005513 }
5514#ifdef CONFIG_HOTPLUG_CPU
5515 unregister_cpu_notifier(&conf->cpu_notify);
5516#endif
5517 put_online_cpus();
5518
5519 free_percpu(conf->percpu);
5520}
5521
NeilBrownd1688a62011-10-11 16:49:52 +11005522static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005523{
Shaohua Li851c30c2013-08-28 14:30:16 +08005524 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005525 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005526 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005527 kfree(conf->disks);
5528 kfree(conf->stripe_hashtbl);
5529 kfree(conf);
5530}
5531
Dan Williams36d1c642009-07-14 11:48:22 -07005532#ifdef CONFIG_HOTPLUG_CPU
5533static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5534 void *hcpu)
5535{
NeilBrownd1688a62011-10-11 16:49:52 +11005536 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005537 long cpu = (long)hcpu;
5538 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5539
5540 switch (action) {
5541 case CPU_UP_PREPARE:
5542 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005543 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005544 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005545 if (!percpu->scribble)
5546 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5547
5548 if (!percpu->scribble ||
5549 (conf->level == 6 && !percpu->spare_page)) {
5550 safe_put_page(percpu->spare_page);
5551 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005552 pr_err("%s: failed memory allocation for cpu%ld\n",
5553 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005554 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005555 }
5556 break;
5557 case CPU_DEAD:
5558 case CPU_DEAD_FROZEN:
5559 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005560 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005561 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005562 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005563 break;
5564 default:
5565 break;
5566 }
5567 return NOTIFY_OK;
5568}
5569#endif
5570
NeilBrownd1688a62011-10-11 16:49:52 +11005571static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005572{
5573 unsigned long cpu;
5574 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005575 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005576 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005577 int err;
5578
Dan Williams36d1c642009-07-14 11:48:22 -07005579 allcpus = alloc_percpu(struct raid5_percpu);
5580 if (!allcpus)
5581 return -ENOMEM;
5582 conf->percpu = allcpus;
5583
5584 get_online_cpus();
5585 err = 0;
5586 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005587 if (conf->level == 6) {
5588 spare_page = alloc_page(GFP_KERNEL);
5589 if (!spare_page) {
5590 err = -ENOMEM;
5591 break;
5592 }
5593 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5594 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005595 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005596 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005597 err = -ENOMEM;
5598 break;
5599 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005600 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005601 }
5602#ifdef CONFIG_HOTPLUG_CPU
5603 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5604 conf->cpu_notify.priority = 0;
5605 if (err == 0)
5606 err = register_cpu_notifier(&conf->cpu_notify);
5607#endif
5608 put_online_cpus();
5609
5610 return err;
5611}
5612
NeilBrownd1688a62011-10-11 16:49:52 +11005613static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614{
NeilBrownd1688a62011-10-11 16:49:52 +11005615 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005616 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005617 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005619 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11005620 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
NeilBrown91adb562009-03-31 14:39:39 +11005622 if (mddev->new_level != 5
5623 && mddev->new_level != 4
5624 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005625 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005626 mdname(mddev), mddev->new_level);
5627 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
NeilBrown91adb562009-03-31 14:39:39 +11005629 if ((mddev->new_level == 5
5630 && !algorithm_valid_raid5(mddev->new_layout)) ||
5631 (mddev->new_level == 6
5632 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005633 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005634 mdname(mddev), mddev->new_layout);
5635 return ERR_PTR(-EIO);
5636 }
5637 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005638 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005639 mdname(mddev), mddev->raid_disks);
5640 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Andre Noll664e7c42009-06-18 08:45:27 +10005643 if (!mddev->new_chunk_sectors ||
5644 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5645 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005646 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5647 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005648 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005649 }
5650
NeilBrownd1688a62011-10-11 16:49:52 +11005651 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005652 if (conf == NULL)
5653 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005654 /* Don't enable multi-threading by default*/
5655 if (alloc_thread_groups(conf, 0))
5656 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005657 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10005658 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11005659 init_waitqueue_head(&conf->wait_for_stripe);
5660 init_waitqueue_head(&conf->wait_for_overlap);
5661 INIT_LIST_HEAD(&conf->handle_list);
5662 INIT_LIST_HEAD(&conf->hold_list);
5663 INIT_LIST_HEAD(&conf->delayed_list);
5664 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005665 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005666 atomic_set(&conf->active_stripes, 0);
5667 atomic_set(&conf->preread_active_stripes, 0);
5668 atomic_set(&conf->active_aligned_reads, 0);
5669 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005670 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005671
5672 conf->raid_disks = mddev->raid_disks;
5673 if (mddev->reshape_position == MaxSector)
5674 conf->previous_raid_disks = mddev->raid_disks;
5675 else
5676 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005677 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5678 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005679
NeilBrown5e5e3e72009-10-16 16:35:30 +11005680 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005681 GFP_KERNEL);
5682 if (!conf->disks)
5683 goto abort;
5684
5685 conf->mddev = mddev;
5686
5687 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5688 goto abort;
5689
Shaohua Li566c09c2013-11-14 15:16:17 +11005690 /* We init hash_locks[0] separately to that it can be used
5691 * as the reference lock in the spin_lock_nest_lock() call
5692 * in lock_all_device_hash_locks_irq in order to convince
5693 * lockdep that we know what we are doing.
5694 */
5695 spin_lock_init(conf->hash_locks);
5696 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
5697 spin_lock_init(conf->hash_locks + i);
5698
5699 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5700 INIT_LIST_HEAD(conf->inactive_list + i);
5701
5702 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5703 INIT_LIST_HEAD(conf->temp_inactive_list + i);
5704
Dan Williams36d1c642009-07-14 11:48:22 -07005705 conf->level = mddev->new_level;
5706 if (raid5_alloc_percpu(conf) != 0)
5707 goto abort;
5708
NeilBrown0c55e022010-05-03 14:09:02 +10005709 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005710
NeilBrowndafb20f2012-03-19 12:46:39 +11005711 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005712 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005713 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005714 || raid_disk < 0)
5715 continue;
5716 disk = conf->disks + raid_disk;
5717
NeilBrown17045f52011-12-23 10:17:53 +11005718 if (test_bit(Replacement, &rdev->flags)) {
5719 if (disk->replacement)
5720 goto abort;
5721 disk->replacement = rdev;
5722 } else {
5723 if (disk->rdev)
5724 goto abort;
5725 disk->rdev = rdev;
5726 }
NeilBrown91adb562009-03-31 14:39:39 +11005727
5728 if (test_bit(In_sync, &rdev->flags)) {
5729 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005730 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5731 " disk %d\n",
5732 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005733 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005734 /* Cannot rely on bitmap to complete recovery */
5735 conf->fullsync = 1;
5736 }
5737
Andre Noll09c9e5f2009-06-18 08:45:55 +10005738 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005739 conf->level = mddev->new_level;
5740 if (conf->level == 6)
5741 conf->max_degraded = 2;
5742 else
5743 conf->max_degraded = 1;
5744 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005745 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005746 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005747 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005748 conf->prev_algo = mddev->layout;
5749 }
NeilBrown91adb562009-03-31 14:39:39 +11005750
5751 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005752 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li566c09c2013-11-14 15:16:17 +11005753 if (grow_stripes(conf, NR_STRIPES)) {
NeilBrown91adb562009-03-31 14:39:39 +11005754 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005755 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5756 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005757 goto abort;
5758 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005759 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5760 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005761
NeilBrown02326052012-07-03 15:56:52 +10005762 sprintf(pers_name, "raid%d", mddev->new_level);
5763 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005764 if (!conf->thread) {
5765 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005766 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005767 mdname(mddev));
5768 goto abort;
5769 }
5770
5771 return conf;
5772
5773 abort:
5774 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005775 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005776 return ERR_PTR(-EIO);
5777 } else
5778 return ERR_PTR(-ENOMEM);
5779}
5780
NeilBrownc148ffd2009-11-13 17:47:00 +11005781
5782static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5783{
5784 switch (algo) {
5785 case ALGORITHM_PARITY_0:
5786 if (raid_disk < max_degraded)
5787 return 1;
5788 break;
5789 case ALGORITHM_PARITY_N:
5790 if (raid_disk >= raid_disks - max_degraded)
5791 return 1;
5792 break;
5793 case ALGORITHM_PARITY_0_6:
5794 if (raid_disk == 0 ||
5795 raid_disk == raid_disks - 1)
5796 return 1;
5797 break;
5798 case ALGORITHM_LEFT_ASYMMETRIC_6:
5799 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5800 case ALGORITHM_LEFT_SYMMETRIC_6:
5801 case ALGORITHM_RIGHT_SYMMETRIC_6:
5802 if (raid_disk == raid_disks - 1)
5803 return 1;
5804 }
5805 return 0;
5806}
5807
NeilBrownfd01b882011-10-11 16:47:53 +11005808static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005809{
NeilBrownd1688a62011-10-11 16:49:52 +11005810 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005811 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005812 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005813 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005814 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005815 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005816 long long min_offset_diff = 0;
5817 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005818
Andre Noll8c6ac862009-06-18 08:48:06 +10005819 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005820 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005821 " -- starting background reconstruction\n",
5822 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005823
5824 rdev_for_each(rdev, mddev) {
5825 long long diff;
5826 if (rdev->raid_disk < 0)
5827 continue;
5828 diff = (rdev->new_data_offset - rdev->data_offset);
5829 if (first) {
5830 min_offset_diff = diff;
5831 first = 0;
5832 } else if (mddev->reshape_backwards &&
5833 diff < min_offset_diff)
5834 min_offset_diff = diff;
5835 else if (!mddev->reshape_backwards &&
5836 diff > min_offset_diff)
5837 min_offset_diff = diff;
5838 }
5839
NeilBrownf6705572006-03-27 01:18:11 -08005840 if (mddev->reshape_position != MaxSector) {
5841 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005842 * Difficulties arise if the stripe we would write to
5843 * next is at or after the stripe we would read from next.
5844 * For a reshape that changes the number of devices, this
5845 * is only possible for a very short time, and mdadm makes
5846 * sure that time appears to have past before assembling
5847 * the array. So we fail if that time hasn't passed.
5848 * For a reshape that keeps the number of devices the same
5849 * mdadm must be monitoring the reshape can keeping the
5850 * critical areas read-only and backed up. It will start
5851 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005852 */
5853 sector_t here_new, here_old;
5854 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005855 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005856
NeilBrown88ce4932009-03-31 15:24:23 +11005857 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005858 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005859 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005860 mdname(mddev));
5861 return -EINVAL;
5862 }
NeilBrownf6705572006-03-27 01:18:11 -08005863 old_disks = mddev->raid_disks - mddev->delta_disks;
5864 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005865 * further up in new geometry must map after here in old
5866 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005867 */
5868 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005869 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005870 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005871 printk(KERN_ERR "md/raid:%s: reshape_position not "
5872 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005873 return -EINVAL;
5874 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005875 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005876 /* here_new is the stripe we will write to */
5877 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005878 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005879 (old_disks-max_degraded));
5880 /* here_old is the first stripe that we might need to read
5881 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005882 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005883 if ((here_new * mddev->new_chunk_sectors !=
5884 here_old * mddev->chunk_sectors)) {
5885 printk(KERN_ERR "md/raid:%s: reshape position is"
5886 " confused - aborting\n", mdname(mddev));
5887 return -EINVAL;
5888 }
NeilBrown67ac6012009-08-13 10:06:24 +10005889 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005890 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005891 * and taking constant backups.
5892 * mdadm always starts a situation like this in
5893 * readonly mode so it can take control before
5894 * allowing any writes. So just check for that.
5895 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005896 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5897 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5898 /* not really in-place - so OK */;
5899 else if (mddev->ro == 0) {
5900 printk(KERN_ERR "md/raid:%s: in-place reshape "
5901 "must be started in read-only mode "
5902 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005903 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005904 return -EINVAL;
5905 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005906 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005907 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005908 here_old * mddev->chunk_sectors)
5909 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005910 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005911 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005912 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5913 "auto-recovery - aborting.\n",
5914 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005915 return -EINVAL;
5916 }
NeilBrown0c55e022010-05-03 14:09:02 +10005917 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5918 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005919 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005920 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005921 BUG_ON(mddev->level != mddev->new_level);
5922 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005923 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005924 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005925 }
5926
NeilBrown245f46c2009-03-31 14:39:39 +11005927 if (mddev->private == NULL)
5928 conf = setup_conf(mddev);
5929 else
5930 conf = mddev->private;
5931
NeilBrown91adb562009-03-31 14:39:39 +11005932 if (IS_ERR(conf))
5933 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005934
NeilBrownb5254dd2012-05-21 09:27:01 +10005935 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005936 mddev->thread = conf->thread;
5937 conf->thread = NULL;
5938 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
NeilBrown17045f52011-12-23 10:17:53 +11005940 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5941 i++) {
5942 rdev = conf->disks[i].rdev;
5943 if (!rdev && conf->disks[i].replacement) {
5944 /* The replacement is all we have yet */
5945 rdev = conf->disks[i].replacement;
5946 conf->disks[i].replacement = NULL;
5947 clear_bit(Replacement, &rdev->flags);
5948 conf->disks[i].rdev = rdev;
5949 }
5950 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005951 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005952 if (conf->disks[i].replacement &&
5953 conf->reshape_progress != MaxSector) {
5954 /* replacements and reshape simply do not mix. */
5955 printk(KERN_ERR "md: cannot handle concurrent "
5956 "replacement and reshape.\n");
5957 goto abort;
5958 }
NeilBrown2f115882010-06-17 17:41:03 +10005959 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005960 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005961 continue;
5962 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005963 /* This disc is not fully in-sync. However if it
5964 * just stored parity (beyond the recovery_offset),
5965 * when we don't need to be concerned about the
5966 * array being dirty.
5967 * When reshape goes 'backwards', we never have
5968 * partially completed devices, so we only need
5969 * to worry about reshape going forwards.
5970 */
5971 /* Hack because v0.91 doesn't store recovery_offset properly. */
5972 if (mddev->major_version == 0 &&
5973 mddev->minor_version > 90)
5974 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005975
NeilBrownc148ffd2009-11-13 17:47:00 +11005976 if (rdev->recovery_offset < reshape_offset) {
5977 /* We need to check old and new layout */
5978 if (!only_parity(rdev->raid_disk,
5979 conf->algorithm,
5980 conf->raid_disks,
5981 conf->max_degraded))
5982 continue;
5983 }
5984 if (!only_parity(rdev->raid_disk,
5985 conf->prev_algo,
5986 conf->previous_raid_disks,
5987 conf->max_degraded))
5988 continue;
5989 dirty_parity_disks++;
5990 }
NeilBrown91adb562009-03-31 14:39:39 +11005991
NeilBrown17045f52011-12-23 10:17:53 +11005992 /*
5993 * 0 for a fully functional array, 1 or 2 for a degraded array.
5994 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005995 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
NeilBrown674806d2010-06-16 17:17:53 +10005997 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005998 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006000 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 goto abort;
6002 }
6003
NeilBrown91adb562009-03-31 14:39:39 +11006004 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006005 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006006 mddev->resync_max_sectors = mddev->dev_sectors;
6007
NeilBrownc148ffd2009-11-13 17:47:00 +11006008 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006010 if (mddev->ok_start_degraded)
6011 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006012 "md/raid:%s: starting dirty degraded array"
6013 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006014 mdname(mddev));
6015 else {
6016 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006017 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006018 mdname(mddev));
6019 goto abort;
6020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 }
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006024 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6025 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006026 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6027 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 else
NeilBrown0c55e022010-05-03 14:09:02 +10006029 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6030 " out of %d devices, algorithm %d\n",
6031 mdname(mddev), conf->level,
6032 mddev->raid_disks - mddev->degraded,
6033 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
6035 print_raid5_conf(conf);
6036
NeilBrownfef9c612009-03-31 15:16:46 +11006037 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006038 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006039 atomic_set(&conf->reshape_stripes, 0);
6040 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6041 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6042 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6043 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6044 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006045 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006046 }
6047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006050 if (mddev->to_remove == &raid5_attrs_group)
6051 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006052 else if (mddev->kobj.sd &&
6053 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006054 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006055 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006056 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006057 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6058
6059 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006060 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006061 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006062 /* read-ahead size must cover two whole stripes, which
6063 * is 2 * (datadisks) * chunksize where 'n' is the
6064 * number of raid devices
6065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6067 int stripe = data_disks *
6068 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6069 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6070 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006071
6072 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006073
6074 mddev->queue->backing_dev_info.congested_data = mddev;
6075 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10006076
6077 chunk_size = mddev->chunk_sectors << 9;
6078 blk_queue_io_min(mddev->queue, chunk_size);
6079 blk_queue_io_opt(mddev->queue, chunk_size *
6080 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11006081 /*
6082 * We can only discard a whole stripe. It doesn't make sense to
6083 * discard data disk but write parity disk
6084 */
6085 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006086 /* Round up to power of 2, as discard handling
6087 * currently assumes that */
6088 while ((stripe-1) & stripe)
6089 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006090 mddev->queue->limits.discard_alignment = stripe;
6091 mddev->queue->limits.discard_granularity = stripe;
6092 /*
6093 * unaligned part of discard request will be ignored, so can't
6094 * guarantee discard_zerors_data
6095 */
6096 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006097
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006098 blk_queue_max_write_same_sectors(mddev->queue, 0);
6099
NeilBrown05616be2012-05-21 09:27:00 +10006100 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006101 disk_stack_limits(mddev->gendisk, rdev->bdev,
6102 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006103 disk_stack_limits(mddev->gendisk, rdev->bdev,
6104 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006105 /*
6106 * discard_zeroes_data is required, otherwise data
6107 * could be lost. Consider a scenario: discard a stripe
6108 * (the stripe could be inconsistent if
6109 * discard_zeroes_data is 0); write one disk of the
6110 * stripe (the stripe could be inconsistent again
6111 * depending on which disks are used to calculate
6112 * parity); the disk is broken; The stripe data of this
6113 * disk is lost.
6114 */
6115 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6116 !bdev_get_queue(rdev->bdev)->
6117 limits.discard_zeroes_data)
6118 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10006119 }
Shaohua Li620125f2012-10-11 13:49:05 +11006120
6121 if (discard_supported &&
6122 mddev->queue->limits.max_discard_sectors >= stripe &&
6123 mddev->queue->limits.discard_granularity >= stripe)
6124 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6125 mddev->queue);
6126 else
6127 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6128 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 }
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 return 0;
6132abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006133 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006134 print_raid5_conf(conf);
6135 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006137 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 return -EIO;
6139}
6140
NeilBrownfd01b882011-10-11 16:47:53 +11006141static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142{
NeilBrownd1688a62011-10-11 16:49:52 +11006143 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
NeilBrown01f96c02011-09-21 15:30:20 +10006145 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006146 if (mddev->queue)
6147 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10006148 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006149 mddev->private = NULL;
6150 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 return 0;
6152}
6153
NeilBrownfd01b882011-10-11 16:47:53 +11006154static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
NeilBrownd1688a62011-10-11 16:49:52 +11006156 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 int i;
6158
Andre Noll9d8f0362009-06-18 08:45:01 +10006159 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6160 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006161 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 for (i = 0; i < conf->raid_disks; i++)
6163 seq_printf (seq, "%s",
6164 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08006165 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167}
6168
NeilBrownd1688a62011-10-11 16:49:52 +11006169static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170{
6171 int i;
6172 struct disk_info *tmp;
6173
NeilBrown0c55e022010-05-03 14:09:02 +10006174 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 if (!conf) {
6176 printk("(conf==NULL)\n");
6177 return;
6178 }
NeilBrown0c55e022010-05-03 14:09:02 +10006179 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6180 conf->raid_disks,
6181 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
6183 for (i = 0; i < conf->raid_disks; i++) {
6184 char b[BDEVNAME_SIZE];
6185 tmp = conf->disks + i;
6186 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10006187 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6188 i, !test_bit(Faulty, &tmp->rdev->flags),
6189 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 }
6191}
6192
NeilBrownfd01b882011-10-11 16:47:53 +11006193static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
6195 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006196 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006198 int count = 0;
6199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201 for (i = 0; i < conf->raid_disks; i++) {
6202 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006203 if (tmp->replacement
6204 && tmp->replacement->recovery_offset == MaxSector
6205 && !test_bit(Faulty, &tmp->replacement->flags)
6206 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6207 /* Replacement has just become active. */
6208 if (!tmp->rdev
6209 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6210 count++;
6211 if (tmp->rdev) {
6212 /* Replaced device not technically faulty,
6213 * but we need to be sure it gets removed
6214 * and never re-added.
6215 */
6216 set_bit(Faulty, &tmp->rdev->flags);
6217 sysfs_notify_dirent_safe(
6218 tmp->rdev->sysfs_state);
6219 }
6220 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6221 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006222 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006223 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006224 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006225 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006226 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 }
6228 }
NeilBrown6b965622010-08-18 11:56:59 +10006229 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006230 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006231 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006233 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
NeilBrownb8321b62011-12-23 10:17:51 +11006236static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
NeilBrownd1688a62011-10-11 16:49:52 +11006238 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006240 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006241 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 struct disk_info *p = conf->disks + number;
6243
6244 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006245 if (rdev == p->rdev)
6246 rdevp = &p->rdev;
6247 else if (rdev == p->replacement)
6248 rdevp = &p->replacement;
6249 else
6250 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006251
NeilBrown657e3e42011-12-23 10:17:52 +11006252 if (number >= conf->raid_disks &&
6253 conf->reshape_progress == MaxSector)
6254 clear_bit(In_sync, &rdev->flags);
6255
6256 if (test_bit(In_sync, &rdev->flags) ||
6257 atomic_read(&rdev->nr_pending)) {
6258 err = -EBUSY;
6259 goto abort;
6260 }
6261 /* Only remove non-faulty devices if recovery
6262 * isn't possible.
6263 */
6264 if (!test_bit(Faulty, &rdev->flags) &&
6265 mddev->recovery_disabled != conf->recovery_disabled &&
6266 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006267 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006268 number < conf->raid_disks) {
6269 err = -EBUSY;
6270 goto abort;
6271 }
6272 *rdevp = NULL;
6273 synchronize_rcu();
6274 if (atomic_read(&rdev->nr_pending)) {
6275 /* lost the race, try later */
6276 err = -EBUSY;
6277 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006278 } else if (p->replacement) {
6279 /* We must have just cleared 'rdev' */
6280 p->rdev = p->replacement;
6281 clear_bit(Replacement, &p->replacement->flags);
6282 smp_mb(); /* Make sure other CPUs may see both as identical
6283 * but will never see neither - if they are careful
6284 */
6285 p->replacement = NULL;
6286 clear_bit(WantReplacement, &rdev->flags);
6287 } else
6288 /* We might have just removed the Replacement as faulty-
6289 * clear the bit just in case
6290 */
6291 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292abort:
6293
6294 print_raid5_conf(conf);
6295 return err;
6296}
6297
NeilBrownfd01b882011-10-11 16:47:53 +11006298static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299{
NeilBrownd1688a62011-10-11 16:49:52 +11006300 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006301 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 int disk;
6303 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006304 int first = 0;
6305 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
NeilBrown7f0da592011-07-28 11:39:22 +10006307 if (mddev->recovery_disabled == conf->recovery_disabled)
6308 return -EBUSY;
6309
NeilBrowndc10c642012-03-19 12:46:37 +11006310 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313
Neil Brown6c2fce22008-06-28 08:31:31 +10006314 if (rdev->raid_disk >= 0)
6315 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
6317 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006318 * find the disk ... but prefer rdev->saved_raid_disk
6319 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006321 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006322 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006323 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006324 first = rdev->saved_raid_disk;
6325
6326 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006327 p = conf->disks + disk;
6328 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006329 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006331 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006332 if (rdev->saved_raid_disk != disk)
6333 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006334 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006337 }
6338 for (disk = first; disk <= last; disk++) {
6339 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006340 if (test_bit(WantReplacement, &p->rdev->flags) &&
6341 p->replacement == NULL) {
6342 clear_bit(In_sync, &rdev->flags);
6343 set_bit(Replacement, &rdev->flags);
6344 rdev->raid_disk = disk;
6345 err = 0;
6346 conf->fullsync = 1;
6347 rcu_assign_pointer(p->replacement, rdev);
6348 break;
6349 }
6350 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354}
6355
NeilBrownfd01b882011-10-11 16:47:53 +11006356static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
6358 /* no resync is happening, and there is enough space
6359 * on all devices, so we can resize.
6360 * We need to make sure resync covers any new space.
6361 * If the array is shrinking we should possibly wait until
6362 * any io in the removed space completes, but it hardly seems
6363 * worth it.
6364 */
NeilBrowna4a61252012-05-22 13:55:27 +10006365 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006366 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006367 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6368 if (mddev->external_size &&
6369 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006370 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006371 if (mddev->bitmap) {
6372 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6373 if (ret)
6374 return ret;
6375 }
6376 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006377 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006378 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006379 if (sectors > mddev->dev_sectors &&
6380 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006381 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6383 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006384 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006385 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return 0;
6387}
6388
NeilBrownfd01b882011-10-11 16:47:53 +11006389static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006390{
6391 /* Can only proceed if there are plenty of stripe_heads.
6392 * We need a minimum of one full stripe,, and for sensible progress
6393 * it is best to have about 4 times that.
6394 * If we require 4 times, then the default 256 4K stripe_heads will
6395 * allow for chunk sizes up to 256K, which is probably OK.
6396 * If the chunk size is greater, user-space should request more
6397 * stripe_heads first.
6398 */
NeilBrownd1688a62011-10-11 16:49:52 +11006399 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006400 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6401 > conf->max_nr_stripes ||
6402 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6403 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006404 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6405 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006406 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6407 / STRIPE_SIZE)*4);
6408 return 0;
6409 }
6410 return 1;
6411}
6412
NeilBrownfd01b882011-10-11 16:47:53 +11006413static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006414{
NeilBrownd1688a62011-10-11 16:49:52 +11006415 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006416
NeilBrown88ce4932009-03-31 15:24:23 +11006417 if (mddev->delta_disks == 0 &&
6418 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006419 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006420 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006421 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006422 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006423 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006424 /* We might be able to shrink, but the devices must
6425 * be made bigger first.
6426 * For raid6, 4 is the minimum size.
6427 * Otherwise 2 is the minimum
6428 */
6429 int min = 2;
6430 if (mddev->level == 6)
6431 min = 4;
6432 if (mddev->raid_disks + mddev->delta_disks < min)
6433 return -EINVAL;
6434 }
NeilBrown29269552006-03-27 01:18:10 -08006435
NeilBrown01ee22b2009-06-18 08:47:20 +10006436 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006437 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006438
NeilBrowne56108d62012-10-11 14:24:13 +11006439 return resize_stripes(conf, (conf->previous_raid_disks
6440 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006441}
6442
NeilBrownfd01b882011-10-11 16:47:53 +11006443static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006444{
NeilBrownd1688a62011-10-11 16:49:52 +11006445 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006446 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006447 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006448 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006449
NeilBrownf4168852007-02-28 20:11:53 -08006450 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006451 return -EBUSY;
6452
NeilBrown01ee22b2009-06-18 08:47:20 +10006453 if (!check_stripe_cache(mddev))
6454 return -ENOSPC;
6455
NeilBrown30b67642012-05-22 13:55:28 +10006456 if (has_failed(conf))
6457 return -EINVAL;
6458
NeilBrownc6563a82012-05-21 09:27:00 +10006459 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006460 if (!test_bit(In_sync, &rdev->flags)
6461 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006462 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006463 }
NeilBrown63c70c42006-03-27 01:18:13 -08006464
NeilBrownf4168852007-02-28 20:11:53 -08006465 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006466 /* Not enough devices even to make a degraded array
6467 * of that size
6468 */
6469 return -EINVAL;
6470
NeilBrownec32a2b2009-03-31 15:17:38 +11006471 /* Refuse to reduce size of the array. Any reductions in
6472 * array size must be through explicit setting of array_size
6473 * attribute.
6474 */
6475 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6476 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006477 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006478 "before number of disks\n", mdname(mddev));
6479 return -EINVAL;
6480 }
6481
NeilBrownf6705572006-03-27 01:18:11 -08006482 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006483 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006484 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006485 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006486 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006487 conf->prev_chunk_sectors = conf->chunk_sectors;
6488 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006489 conf->prev_algo = conf->algorithm;
6490 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006491 conf->generation++;
6492 /* Code that selects data_offset needs to see the generation update
6493 * if reshape_progress has been set - so a memory barrier needed.
6494 */
6495 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006496 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006497 conf->reshape_progress = raid5_size(mddev, 0, 0);
6498 else
6499 conf->reshape_progress = 0;
6500 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10006501 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006502 spin_unlock_irq(&conf->device_lock);
6503
NeilBrown4d77e3b2013-08-27 15:57:47 +10006504 /* Now make sure any requests that proceeded on the assumption
6505 * the reshape wasn't running - like Discard or Read - have
6506 * completed.
6507 */
6508 mddev_suspend(mddev);
6509 mddev_resume(mddev);
6510
NeilBrown29269552006-03-27 01:18:10 -08006511 /* Add some new drives, as many as will fit.
6512 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006513 * Don't add devices if we are reducing the number of
6514 * devices in the array. This is because it is not possible
6515 * to correctly record the "partially reconstructed" state of
6516 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006517 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006518 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006519 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006520 if (rdev->raid_disk < 0 &&
6521 !test_bit(Faulty, &rdev->flags)) {
6522 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006523 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006524 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006525 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006526 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006527 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006528
6529 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006530 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006531 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006532 } else if (rdev->raid_disk >= conf->previous_raid_disks
6533 && !test_bit(Faulty, &rdev->flags)) {
6534 /* This is a spare that was manually added */
6535 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006536 }
NeilBrown29269552006-03-27 01:18:10 -08006537
NeilBrown87a8dec2011-01-31 11:57:43 +11006538 /* When a reshape changes the number of devices,
6539 * ->degraded is measured against the larger of the
6540 * pre and post number of devices.
6541 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006542 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006543 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006544 spin_unlock_irqrestore(&conf->device_lock, flags);
6545 }
NeilBrown63c70c42006-03-27 01:18:13 -08006546 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006547 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006548 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006549
NeilBrown29269552006-03-27 01:18:10 -08006550 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6551 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6552 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6553 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6554 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006555 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006556 if (!mddev->sync_thread) {
6557 mddev->recovery = 0;
6558 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11006559 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006560 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11006561 mddev->new_chunk_sectors =
6562 conf->chunk_sectors = conf->prev_chunk_sectors;
6563 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10006564 rdev_for_each(rdev, mddev)
6565 rdev->new_data_offset = rdev->data_offset;
6566 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11006567 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11006568 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006569 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11006570 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006571 spin_unlock_irq(&conf->device_lock);
6572 return -EAGAIN;
6573 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006574 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006575 md_wakeup_thread(mddev->sync_thread);
6576 md_new_event(mddev);
6577 return 0;
6578}
NeilBrown29269552006-03-27 01:18:10 -08006579
NeilBrownec32a2b2009-03-31 15:17:38 +11006580/* This is called from the reshape thread and should make any
6581 * changes needed in 'conf'
6582 */
NeilBrownd1688a62011-10-11 16:49:52 +11006583static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006584{
NeilBrown29269552006-03-27 01:18:10 -08006585
NeilBrownf6705572006-03-27 01:18:11 -08006586 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006587 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006588
NeilBrownf6705572006-03-27 01:18:11 -08006589 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006590 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006591 rdev_for_each(rdev, conf->mddev)
6592 rdev->data_offset = rdev->new_data_offset;
6593 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006594 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006595 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006596 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006597
6598 /* read-ahead size must cover two whole stripes, which is
6599 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6600 */
NeilBrown4a5add42010-06-01 19:37:28 +10006601 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006602 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006603 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006604 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006605 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6606 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6607 }
NeilBrown29269552006-03-27 01:18:10 -08006608 }
NeilBrown29269552006-03-27 01:18:10 -08006609}
6610
NeilBrownec32a2b2009-03-31 15:17:38 +11006611/* This is called from the raid5d thread with mddev_lock held.
6612 * It makes config changes to the device.
6613 */
NeilBrownfd01b882011-10-11 16:47:53 +11006614static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006615{
NeilBrownd1688a62011-10-11 16:49:52 +11006616 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006617
6618 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6619
NeilBrownec32a2b2009-03-31 15:17:38 +11006620 if (mddev->delta_disks > 0) {
6621 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6622 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006623 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006624 } else {
6625 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006626 spin_lock_irq(&conf->device_lock);
6627 mddev->degraded = calc_degraded(conf);
6628 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006629 for (d = conf->raid_disks ;
6630 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006631 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006632 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006633 if (rdev)
6634 clear_bit(In_sync, &rdev->flags);
6635 rdev = conf->disks[d].replacement;
6636 if (rdev)
6637 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006638 }
NeilBrowncea9c222009-03-31 15:15:05 +11006639 }
NeilBrown88ce4932009-03-31 15:24:23 +11006640 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006641 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006642 mddev->reshape_position = MaxSector;
6643 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006644 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006645 }
6646}
6647
NeilBrownfd01b882011-10-11 16:47:53 +11006648static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006649{
NeilBrownd1688a62011-10-11 16:49:52 +11006650 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006651
6652 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006653 case 2: /* resume for a suspend */
6654 wake_up(&conf->wait_for_overlap);
6655 break;
6656
NeilBrown72626682005-09-09 16:23:54 -07006657 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006658 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006659 /* '2' tells resync/reshape to pause so that all
6660 * active stripes can drain
6661 */
6662 conf->quiesce = 2;
Shaohua Li566c09c2013-11-14 15:16:17 +11006663 wait_event_cmd(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006664 atomic_read(&conf->active_stripes) == 0 &&
6665 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11006666 unlock_all_device_hash_locks_irq(conf),
6667 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10006668 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11006669 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006670 /* allow reshape to continue */
6671 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006672 break;
6673
6674 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006675 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006676 conf->quiesce = 0;
6677 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006678 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11006679 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006680 break;
6681 }
NeilBrown72626682005-09-09 16:23:54 -07006682}
NeilBrownb15c2e52006-01-06 00:20:16 -08006683
NeilBrownd562b0c2009-03-31 14:39:39 +11006684
NeilBrownfd01b882011-10-11 16:47:53 +11006685static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006686{
NeilBrowne373ab12011-10-11 16:48:59 +11006687 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006688 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006689
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006690 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006691 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006692 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6693 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006694 return ERR_PTR(-EINVAL);
6695 }
6696
NeilBrowne373ab12011-10-11 16:48:59 +11006697 sectors = raid0_conf->strip_zone[0].zone_end;
6698 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006699 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006700 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006701 mddev->new_layout = ALGORITHM_PARITY_N;
6702 mddev->new_chunk_sectors = mddev->chunk_sectors;
6703 mddev->raid_disks += 1;
6704 mddev->delta_disks = 1;
6705 /* make sure it will be not marked as dirty */
6706 mddev->recovery_cp = MaxSector;
6707
6708 return setup_conf(mddev);
6709}
6710
6711
NeilBrownfd01b882011-10-11 16:47:53 +11006712static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006713{
6714 int chunksect;
6715
6716 if (mddev->raid_disks != 2 ||
6717 mddev->degraded > 1)
6718 return ERR_PTR(-EINVAL);
6719
6720 /* Should check if there are write-behind devices? */
6721
6722 chunksect = 64*2; /* 64K by default */
6723
6724 /* The array must be an exact multiple of chunksize */
6725 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6726 chunksect >>= 1;
6727
6728 if ((chunksect<<9) < STRIPE_SIZE)
6729 /* array size does not allow a suitable chunk size */
6730 return ERR_PTR(-EINVAL);
6731
6732 mddev->new_level = 5;
6733 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006734 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006735
6736 return setup_conf(mddev);
6737}
6738
NeilBrownfd01b882011-10-11 16:47:53 +11006739static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006740{
6741 int new_layout;
6742
6743 switch (mddev->layout) {
6744 case ALGORITHM_LEFT_ASYMMETRIC_6:
6745 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6746 break;
6747 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6748 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6749 break;
6750 case ALGORITHM_LEFT_SYMMETRIC_6:
6751 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6752 break;
6753 case ALGORITHM_RIGHT_SYMMETRIC_6:
6754 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6755 break;
6756 case ALGORITHM_PARITY_0_6:
6757 new_layout = ALGORITHM_PARITY_0;
6758 break;
6759 case ALGORITHM_PARITY_N:
6760 new_layout = ALGORITHM_PARITY_N;
6761 break;
6762 default:
6763 return ERR_PTR(-EINVAL);
6764 }
6765 mddev->new_level = 5;
6766 mddev->new_layout = new_layout;
6767 mddev->delta_disks = -1;
6768 mddev->raid_disks -= 1;
6769 return setup_conf(mddev);
6770}
6771
NeilBrownd562b0c2009-03-31 14:39:39 +11006772
NeilBrownfd01b882011-10-11 16:47:53 +11006773static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006774{
NeilBrown88ce4932009-03-31 15:24:23 +11006775 /* For a 2-drive array, the layout and chunk size can be changed
6776 * immediately as not restriping is needed.
6777 * For larger arrays we record the new value - after validation
6778 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006779 */
NeilBrownd1688a62011-10-11 16:49:52 +11006780 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006781 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006782
NeilBrown597a7112009-06-18 08:47:42 +10006783 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006784 return -EINVAL;
6785 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006786 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006787 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006788 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006789 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006790 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006791 /* not factor of array size */
6792 return -EINVAL;
6793 }
6794
6795 /* They look valid */
6796
NeilBrown88ce4932009-03-31 15:24:23 +11006797 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006798 /* can make the change immediately */
6799 if (mddev->new_layout >= 0) {
6800 conf->algorithm = mddev->new_layout;
6801 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006802 }
6803 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006804 conf->chunk_sectors = new_chunk ;
6805 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006806 }
6807 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6808 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006809 }
NeilBrown50ac1682009-06-18 08:47:55 +10006810 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006811}
6812
NeilBrownfd01b882011-10-11 16:47:53 +11006813static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006814{
NeilBrown597a7112009-06-18 08:47:42 +10006815 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006816
NeilBrown597a7112009-06-18 08:47:42 +10006817 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006818 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006819 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006820 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006821 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006822 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006823 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006824 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006825 /* not factor of array size */
6826 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006827 }
NeilBrown88ce4932009-03-31 15:24:23 +11006828
6829 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006830 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006831}
6832
NeilBrownfd01b882011-10-11 16:47:53 +11006833static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006834{
6835 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006836 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006837 * raid1 - if there are two drives. We need to know the chunk size
6838 * raid4 - trivial - just use a raid4 layout.
6839 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006840 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006841 if (mddev->level == 0)
6842 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006843 if (mddev->level == 1)
6844 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006845 if (mddev->level == 4) {
6846 mddev->new_layout = ALGORITHM_PARITY_N;
6847 mddev->new_level = 5;
6848 return setup_conf(mddev);
6849 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006850 if (mddev->level == 6)
6851 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006852
6853 return ERR_PTR(-EINVAL);
6854}
6855
NeilBrownfd01b882011-10-11 16:47:53 +11006856static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006857{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006858 /* raid4 can take over:
6859 * raid0 - if there is only one strip zone
6860 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006861 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006862 if (mddev->level == 0)
6863 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006864 if (mddev->level == 5 &&
6865 mddev->layout == ALGORITHM_PARITY_N) {
6866 mddev->new_layout = 0;
6867 mddev->new_level = 4;
6868 return setup_conf(mddev);
6869 }
6870 return ERR_PTR(-EINVAL);
6871}
NeilBrownd562b0c2009-03-31 14:39:39 +11006872
NeilBrown84fc4b52011-10-11 16:49:58 +11006873static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006874
NeilBrownfd01b882011-10-11 16:47:53 +11006875static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006876{
6877 /* Currently can only take over a raid5. We map the
6878 * personality to an equivalent raid6 personality
6879 * with the Q block at the end.
6880 */
6881 int new_layout;
6882
6883 if (mddev->pers != &raid5_personality)
6884 return ERR_PTR(-EINVAL);
6885 if (mddev->degraded > 1)
6886 return ERR_PTR(-EINVAL);
6887 if (mddev->raid_disks > 253)
6888 return ERR_PTR(-EINVAL);
6889 if (mddev->raid_disks < 3)
6890 return ERR_PTR(-EINVAL);
6891
6892 switch (mddev->layout) {
6893 case ALGORITHM_LEFT_ASYMMETRIC:
6894 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6895 break;
6896 case ALGORITHM_RIGHT_ASYMMETRIC:
6897 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6898 break;
6899 case ALGORITHM_LEFT_SYMMETRIC:
6900 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6901 break;
6902 case ALGORITHM_RIGHT_SYMMETRIC:
6903 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6904 break;
6905 case ALGORITHM_PARITY_0:
6906 new_layout = ALGORITHM_PARITY_0_6;
6907 break;
6908 case ALGORITHM_PARITY_N:
6909 new_layout = ALGORITHM_PARITY_N;
6910 break;
6911 default:
6912 return ERR_PTR(-EINVAL);
6913 }
6914 mddev->new_level = 6;
6915 mddev->new_layout = new_layout;
6916 mddev->delta_disks = 1;
6917 mddev->raid_disks += 1;
6918 return setup_conf(mddev);
6919}
6920
6921
NeilBrown84fc4b52011-10-11 16:49:58 +11006922static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006923{
6924 .name = "raid6",
6925 .level = 6,
6926 .owner = THIS_MODULE,
6927 .make_request = make_request,
6928 .run = run,
6929 .stop = stop,
6930 .status = status,
6931 .error_handler = error,
6932 .hot_add_disk = raid5_add_disk,
6933 .hot_remove_disk= raid5_remove_disk,
6934 .spare_active = raid5_spare_active,
6935 .sync_request = sync_request,
6936 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006937 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006938 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006939 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006940 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006941 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006942 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006943};
NeilBrown84fc4b52011-10-11 16:49:58 +11006944static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945{
6946 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006947 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 .owner = THIS_MODULE,
6949 .make_request = make_request,
6950 .run = run,
6951 .stop = stop,
6952 .status = status,
6953 .error_handler = error,
6954 .hot_add_disk = raid5_add_disk,
6955 .hot_remove_disk= raid5_remove_disk,
6956 .spare_active = raid5_spare_active,
6957 .sync_request = sync_request,
6958 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006959 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006960 .check_reshape = raid5_check_reshape,
6961 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006962 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006963 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006964 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965};
6966
NeilBrown84fc4b52011-10-11 16:49:58 +11006967static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968{
NeilBrown2604b702006-01-06 00:20:36 -08006969 .name = "raid4",
6970 .level = 4,
6971 .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,
NeilBrown3d378902007-03-26 21:32: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,
NeilBrown2604b702006-01-06 00:20:36 -08006986 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006987 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006988};
6989
6990static int __init raid5_init(void)
6991{
Shaohua Li851c30c2013-08-28 14:30:16 +08006992 raid5_wq = alloc_workqueue("raid5wq",
6993 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
6994 if (!raid5_wq)
6995 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07006996 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006997 register_md_personality(&raid5_personality);
6998 register_md_personality(&raid4_personality);
6999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000}
7001
NeilBrown2604b702006-01-06 00:20:36 -08007002static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003{
NeilBrown16a53ec2006-06-26 00:27:38 -07007004 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007005 unregister_md_personality(&raid5_personality);
7006 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007007 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008}
7009
7010module_init(raid5_init);
7011module_exit(raid5_exit);
7012MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007013MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007015MODULE_ALIAS("md-raid5");
7016MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007017MODULE_ALIAS("md-level-5");
7018MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007019MODULE_ALIAS("md-personality-8"); /* RAID6 */
7020MODULE_ALIAS("md-raid6");
7021MODULE_ALIAS("md-level-6");
7022
7023/* This used to be two separate modules, they were: */
7024MODULE_ALIAS("raid5");
7025MODULE_ALIAS("raid6");