blob: 7ff4f252ca1a42943252a9e19b975da5b2f8ce9b [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
88/* bio's attached to a stripe+device for I/O are linked together in bi_sector
89 * order without overlap. There may be several bio's per stripe+device, and
90 * a bio could span several devices.
91 * When walking this list for a particular stripe+device, we must never proceed
92 * beyond a bio that extends past this device, as the next bio might no longer
93 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110094 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * of the current stripe+device
96 */
NeilBrowndb298e12011-10-07 14:23:00 +110097static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
98{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -080099 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +1100100 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
101 return bio->bi_next;
102 else
103 return NULL;
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jens Axboe960e7392008-08-15 10:41:18 +0200106/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200107 * We maintain a biased count of active stripes in the bottom 16 bits of
108 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200109 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200111{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200117{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200120}
121
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200123{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000124 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
125 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200126}
127
Shaohua Lie7836bd62012-07-19 16:01:31 +1000128static inline void raid5_set_bi_processed_stripes(struct bio *bio,
129 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200130{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000131 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
132 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200133
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 do {
135 old = atomic_read(segments);
136 new = (old & 0xffff) | (cnt << 16);
137 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200138}
139
Shaohua Lie7836bd62012-07-19 16:01:31 +1000140static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200141{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000142 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
143 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200144}
145
NeilBrownd0dabf72009-03-31 14:39:38 +1100146/* Find first data disk in a raid6 stripe */
147static inline int raid6_d0(struct stripe_head *sh)
148{
NeilBrown67cc2b82009-03-31 14:39:38 +1100149 if (sh->ddf_layout)
150 /* ddf always start from first device */
151 return 0;
152 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100153 if (sh->qd_idx == sh->disks - 1)
154 return 0;
155 else
156 return sh->qd_idx + 1;
157}
NeilBrown16a53ec2006-06-26 00:27:38 -0700158static inline int raid6_next_disk(int disk, int raid_disks)
159{
160 disk++;
161 return (disk < raid_disks) ? disk : 0;
162}
Dan Williamsa4456852007-07-09 11:56:43 -0700163
NeilBrownd0dabf72009-03-31 14:39:38 +1100164/* When walking through the disks in a raid5, starting at raid6_d0,
165 * We need to map each disk to a 'slot', where the data disks are slot
166 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
167 * is raid_disks-1. This help does that mapping.
168 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100169static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
170 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100171{
Dan Williams66295422009-10-19 18:09:32 -0700172 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100173
NeilBrowne4424fe2009-10-16 16:27:34 +1100174 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700175 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100177 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100178 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100179 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100180 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700181 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100182 return slot;
183}
184
Dan Williamsa4456852007-07-09 11:56:43 -0700185static void return_io(struct bio *return_bi)
186{
187 struct bio *bi = return_bi;
188 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700189
190 return_bi = bi->bi_next;
191 bi->bi_next = NULL;
192 bi->bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700193 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
194 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000195 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700196 bi = return_bi;
197 }
198}
199
NeilBrownd1688a62011-10-11 16:49:52 +1100200static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Dan Williams600aa102008-06-28 08:32:05 +1000202static int stripe_operations_active(struct stripe_head *sh)
203{
204 return sh->check_state || sh->reconstruct_state ||
205 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
206 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
207}
208
Shaohua Li851c30c2013-08-28 14:30:16 +0800209static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
210{
211 struct r5conf *conf = sh->raid_conf;
212 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800213 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800214 int i, cpu = sh->cpu;
215
216 if (!cpu_online(cpu)) {
217 cpu = cpumask_any(cpu_online_mask);
218 sh->cpu = cpu;
219 }
220
221 if (list_empty(&sh->lru)) {
222 struct r5worker_group *group;
223 group = conf->worker_groups + cpu_to_group(cpu);
224 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800225 group->stripes_cnt++;
226 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800227 }
228
229 if (conf->worker_cnt_per_group == 0) {
230 md_wakeup_thread(conf->mddev->thread);
231 return;
232 }
233
234 group = conf->worker_groups + cpu_to_group(sh->cpu);
235
Shaohua Libfc90cb2013-08-29 15:40:32 +0800236 group->workers[0].working = true;
237 /* at least one worker should run to avoid race */
238 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
239
240 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
241 /* wakeup more workers */
242 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
243 if (group->workers[i].working == false) {
244 group->workers[i].working = true;
245 queue_work_on(sh->cpu, raid5_wq,
246 &group->workers[i].work);
247 thread_cnt--;
248 }
249 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800250}
251
Shaohua Li4eb788d2012-07-19 16:01:31 +1000252static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000254 BUG_ON(!list_empty(&sh->lru));
255 BUG_ON(atomic_read(&conf->active_stripes)==0);
256 if (test_bit(STRIPE_HANDLE, &sh->state)) {
257 if (test_bit(STRIPE_DELAYED, &sh->state) &&
258 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
259 list_add_tail(&sh->lru, &conf->delayed_list);
260 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
261 sh->bm_seq - conf->seq_write > 0)
262 list_add_tail(&sh->lru, &conf->bitmap_list);
263 else {
264 clear_bit(STRIPE_DELAYED, &sh->state);
265 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800266 if (conf->worker_cnt_per_group == 0) {
267 list_add_tail(&sh->lru, &conf->handle_list);
268 } else {
269 raid5_wakeup_stripe_thread(sh);
270 return;
271 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000272 }
273 md_wakeup_thread(conf->mddev->thread);
274 } else {
275 BUG_ON(stripe_operations_active(sh));
276 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
277 if (atomic_dec_return(&conf->preread_active_stripes)
278 < IO_THRESHOLD)
279 md_wakeup_thread(conf->mddev->thread);
280 atomic_dec(&conf->active_stripes);
281 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
282 list_add_tail(&sh->lru, &conf->inactive_list);
283 wake_up(&conf->wait_for_stripe);
284 if (conf->retry_read_aligned)
285 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
288}
NeilBrownd0dabf72009-03-31 14:39:38 +1100289
Shaohua Li4eb788d2012-07-19 16:01:31 +1000290static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
291{
292 if (atomic_dec_and_test(&sh->count))
293 do_release_stripe(conf, sh);
294}
295
Shaohua Lid265d9d2013-08-28 14:29:05 +0800296static struct llist_node *llist_reverse_order(struct llist_node *head)
297{
298 struct llist_node *new_head = NULL;
299
300 while (head) {
301 struct llist_node *tmp = head;
302 head = head->next;
303 tmp->next = new_head;
304 new_head = tmp;
305 }
306
307 return new_head;
308}
309
Shaohua Li773ca822013-08-27 17:50:39 +0800310/* should hold conf->device_lock already */
311static int release_stripe_list(struct r5conf *conf)
312{
313 struct stripe_head *sh;
314 int count = 0;
315 struct llist_node *head;
316
317 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800318 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800319 while (head) {
320 sh = llist_entry(head, struct stripe_head, release_list);
321 head = llist_next(head);
322 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
323 smp_mb();
324 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
325 /*
326 * Don't worry the bit is set here, because if the bit is set
327 * again, the count is always > 1. This is true for
328 * STRIPE_ON_UNPLUG_LIST bit too.
329 */
330 __release_stripe(conf, sh);
331 count++;
332 }
333
334 return count;
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static void release_stripe(struct stripe_head *sh)
338{
NeilBrownd1688a62011-10-11 16:49:52 +1100339 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long flags;
Shaohua Li773ca822013-08-27 17:50:39 +0800341 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700342
Shaohua Li773ca822013-08-27 17:50:39 +0800343 if (test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
344 goto slow_path;
345 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
346 if (wakeup)
347 md_wakeup_thread(conf->mddev->thread);
348 return;
349slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000350 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800351 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000352 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
353 do_release_stripe(conf, sh);
354 spin_unlock(&conf->device_lock);
355 }
356 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
NeilBrownfccddba2006-01-06 00:20:33 -0800359static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Dan Williams45b42332007-07-09 11:56:43 -0700361 pr_debug("remove_hash(), stripe %llu\n",
362 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
NeilBrownfccddba2006-01-06 00:20:33 -0800364 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
NeilBrownd1688a62011-10-11 16:49:52 +1100367static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
NeilBrownfccddba2006-01-06 00:20:33 -0800369 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Dan Williams45b42332007-07-09 11:56:43 -0700371 pr_debug("insert_hash(), stripe %llu\n",
372 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
NeilBrownfccddba2006-01-06 00:20:33 -0800374 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377
378/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100379static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct stripe_head *sh = NULL;
382 struct list_head *first;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (list_empty(&conf->inactive_list))
385 goto out;
386 first = conf->inactive_list.next;
387 sh = list_entry(first, struct stripe_head, lru);
388 list_del_init(first);
389 remove_hash(sh);
390 atomic_inc(&conf->active_stripes);
391out:
392 return sh;
393}
394
NeilBrowne4e11e32010-06-16 16:45:16 +1000395static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct page *p;
398 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000399 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
NeilBrowne4e11e32010-06-16 16:45:16 +1000401 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 p = sh->dev[i].page;
403 if (!p)
404 continue;
405 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800406 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408}
409
NeilBrowne4e11e32010-06-16 16:45:16 +1000410static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000413 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
NeilBrowne4e11e32010-06-16 16:45:16 +1000415 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct page *page;
417
418 if (!(page = alloc_page(GFP_KERNEL))) {
419 return 1;
420 }
421 sh->dev[i].page = page;
422 }
423 return 0;
424}
425
NeilBrown784052e2009-03-31 15:19:07 +1100426static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100427static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100428 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
NeilBrownb5663ba2009-03-31 14:39:38 +1100430static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
NeilBrownd1688a62011-10-11 16:49:52 +1100432 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800433 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200435 BUG_ON(atomic_read(&sh->count) != 0);
436 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000437 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700438
Dan Williams45b42332007-07-09 11:56:43 -0700439 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 (unsigned long long)sh->sector);
441
442 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700443
NeilBrown86b42c72009-03-31 15:19:03 +1100444 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100445 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100447 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 sh->state = 0;
449
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800450
451 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct r5dev *dev = &sh->dev[i];
453
Dan Williamsd84e0f12007-01-02 13:52:30 -0700454 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700456 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700458 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000460 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100463 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800466 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
NeilBrownd1688a62011-10-11 16:49:52 +1100469static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100470 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct stripe_head *sh;
473
Dan Williams45b42332007-07-09 11:56:43 -0700474 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800475 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100476 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700478 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return NULL;
480}
481
NeilBrown674806d2010-06-16 17:17:53 +1000482/*
483 * Need to check if array has failed when deciding whether to:
484 * - start an array
485 * - remove non-faulty devices
486 * - add a spare
487 * - allow a reshape
488 * This determination is simple when no reshape is happening.
489 * However if there is a reshape, we need to carefully check
490 * both the before and after sections.
491 * This is because some failed devices may only affect one
492 * of the two sections, and some non-in_sync devices may
493 * be insync in the section most affected by failed devices.
494 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100495static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000496{
NeilBrown908f4fb2011-12-23 10:17:50 +1100497 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000498 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000499
500 rcu_read_lock();
501 degraded = 0;
502 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100503 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000504 if (rdev && test_bit(Faulty, &rdev->flags))
505 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000506 if (!rdev || test_bit(Faulty, &rdev->flags))
507 degraded++;
508 else if (test_bit(In_sync, &rdev->flags))
509 ;
510 else
511 /* not in-sync or faulty.
512 * If the reshape increases the number of devices,
513 * this is being recovered by the reshape, so
514 * this 'previous' section is not in_sync.
515 * If the number of devices is being reduced however,
516 * the device can only be part of the array if
517 * we are reverting a reshape, so this section will
518 * be in-sync.
519 */
520 if (conf->raid_disks >= conf->previous_raid_disks)
521 degraded++;
522 }
523 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100524 if (conf->raid_disks == conf->previous_raid_disks)
525 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000526 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100527 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000528 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100529 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000530 if (rdev && test_bit(Faulty, &rdev->flags))
531 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000532 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100533 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000534 else if (test_bit(In_sync, &rdev->flags))
535 ;
536 else
537 /* not in-sync or faulty.
538 * If reshape increases the number of devices, this
539 * section has already been recovered, else it
540 * almost certainly hasn't.
541 */
542 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100543 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000544 }
545 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100546 if (degraded2 > degraded)
547 return degraded2;
548 return degraded;
549}
550
551static int has_failed(struct r5conf *conf)
552{
553 int degraded;
554
555 if (conf->mddev->reshape_position == MaxSector)
556 return conf->mddev->degraded > conf->max_degraded;
557
558 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000559 if (degraded > conf->max_degraded)
560 return 1;
561 return 0;
562}
563
NeilBrownb5663ba2009-03-31 14:39:38 +1100564static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100565get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000566 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 struct stripe_head *sh;
569
Dan Williams45b42332007-07-09 11:56:43 -0700570 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 spin_lock_irq(&conf->device_lock);
573
574 do {
NeilBrown72626682005-09-09 16:23:54 -0700575 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000576 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100577 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100578 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!sh) {
580 if (!conf->inactive_blocked)
581 sh = get_free_stripe(conf);
582 if (noblock && sh == NULL)
583 break;
584 if (!sh) {
585 conf->inactive_blocked = 1;
586 wait_event_lock_irq(conf->wait_for_stripe,
587 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800588 (atomic_read(&conf->active_stripes)
589 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100591 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 conf->inactive_blocked = 0;
593 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100594 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 } else {
596 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100597 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000598 && !test_bit(STRIPE_EXPANDING, &sh->state)
Shaohua Li773ca822013-08-27 17:50:39 +0800599 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
600 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else {
602 if (!test_bit(STRIPE_HANDLE, &sh->state))
603 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700604 if (list_empty(&sh->lru) &&
605 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700606 BUG();
607 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800608 if (sh->group) {
609 sh->group->stripes_cnt--;
610 sh->group = NULL;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 }
614 } while (sh == NULL);
615
616 if (sh)
617 atomic_inc(&sh->count);
618
619 spin_unlock_irq(&conf->device_lock);
620 return sh;
621}
622
NeilBrown05616be2012-05-21 09:27:00 +1000623/* Determine if 'data_offset' or 'new_data_offset' should be used
624 * in this stripe_head.
625 */
626static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
627{
628 sector_t progress = conf->reshape_progress;
629 /* Need a memory barrier to make sure we see the value
630 * of conf->generation, or ->data_offset that was set before
631 * reshape_progress was updated.
632 */
633 smp_rmb();
634 if (progress == MaxSector)
635 return 0;
636 if (sh->generation == conf->generation - 1)
637 return 0;
638 /* We are in a reshape, and this is a new-generation stripe,
639 * so use new_data_offset.
640 */
641 return 1;
642}
643
NeilBrown6712ecf2007-09-27 12:47:43 +0200644static void
645raid5_end_read_request(struct bio *bi, int error);
646static void
647raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700648
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000649static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700650{
NeilBrownd1688a62011-10-11 16:49:52 +1100651 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700652 int i, disks = sh->disks;
653
654 might_sleep();
655
656 for (i = disks; i--; ) {
657 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100658 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100659 struct bio *bi, *rbi;
660 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200661 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
662 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
663 rw = WRITE_FUA;
664 else
665 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100666 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100667 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200668 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700669 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100670 else if (test_and_clear_bit(R5_WantReplace,
671 &sh->dev[i].flags)) {
672 rw = WRITE;
673 replace_only = 1;
674 } else
Dan Williams91c00922007-01-02 13:52:30 -0700675 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000676 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
677 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700678
679 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100680 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700681
Dan Williams91c00922007-01-02 13:52:30 -0700682 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100683 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100684 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
685 rdev = rcu_dereference(conf->disks[i].rdev);
686 if (!rdev) {
687 rdev = rrdev;
688 rrdev = NULL;
689 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100690 if (rw & WRITE) {
691 if (replace_only)
692 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100693 if (rdev == rrdev)
694 /* We raced and saw duplicates */
695 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100696 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100697 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100698 rdev = rrdev;
699 rrdev = NULL;
700 }
NeilBrown977df362011-12-23 10:17:53 +1100701
Dan Williams91c00922007-01-02 13:52:30 -0700702 if (rdev && test_bit(Faulty, &rdev->flags))
703 rdev = NULL;
704 if (rdev)
705 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100706 if (rrdev && test_bit(Faulty, &rrdev->flags))
707 rrdev = NULL;
708 if (rrdev)
709 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700710 rcu_read_unlock();
711
NeilBrown73e92e52011-07-28 11:39:22 +1000712 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100713 * need to check for writes. We never accept write errors
714 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000715 */
716 while ((rw & WRITE) && rdev &&
717 test_bit(WriteErrorSeen, &rdev->flags)) {
718 sector_t first_bad;
719 int bad_sectors;
720 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
721 &first_bad, &bad_sectors);
722 if (!bad)
723 break;
724
725 if (bad < 0) {
726 set_bit(BlockedBadBlocks, &rdev->flags);
727 if (!conf->mddev->external &&
728 conf->mddev->flags) {
729 /* It is very unlikely, but we might
730 * still need to write out the
731 * bad block log - better give it
732 * a chance*/
733 md_check_recovery(conf->mddev);
734 }
majianpeng18507532012-07-03 12:11:54 +1000735 /*
736 * Because md_wait_for_blocked_rdev
737 * will dec nr_pending, we must
738 * increment it first.
739 */
740 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000741 md_wait_for_blocked_rdev(rdev, conf->mddev);
742 } else {
743 /* Acknowledged bad block - skip the write */
744 rdev_dec_pending(rdev, conf->mddev);
745 rdev = NULL;
746 }
747 }
748
Dan Williams91c00922007-01-02 13:52:30 -0700749 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100750 if (s->syncing || s->expanding || s->expanded
751 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700752 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
753
Dan Williams2b7497f2008-06-28 08:31:52 +1000754 set_bit(STRIPE_IO_STARTED, &sh->state);
755
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700756 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700757 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700758 bi->bi_rw = rw;
759 bi->bi_end_io = (rw & WRITE)
760 ? raid5_end_write_request
761 : raid5_end_read_request;
762 bi->bi_private = sh;
763
Dan Williams91c00922007-01-02 13:52:30 -0700764 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700765 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700766 bi->bi_rw, i);
767 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000768 if (use_new_offset(conf, sh))
769 bi->bi_sector = (sh->sector
770 + rdev->new_data_offset);
771 else
772 bi->bi_sector = (sh->sector
773 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000774 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
775 bi->bi_rw |= REQ_FLUSH;
776
Kent Overstreet4997b722013-05-30 08:44:39 +0200777 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700778 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
779 bi->bi_io_vec[0].bv_offset = 0;
780 bi->bi_size = STRIPE_SIZE;
NeilBrown977df362011-12-23 10:17:53 +1100781 if (rrdev)
782 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600783
784 if (conf->mddev->gendisk)
785 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
786 bi, disk_devt(conf->mddev->gendisk),
787 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700788 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100789 }
790 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100791 if (s->syncing || s->expanding || s->expanded
792 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100793 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
794
795 set_bit(STRIPE_IO_STARTED, &sh->state);
796
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700797 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100798 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700799 rbi->bi_rw = rw;
800 BUG_ON(!(rw & WRITE));
801 rbi->bi_end_io = raid5_end_write_request;
802 rbi->bi_private = sh;
803
NeilBrown977df362011-12-23 10:17:53 +1100804 pr_debug("%s: for %llu schedule op %ld on "
805 "replacement disc %d\n",
806 __func__, (unsigned long long)sh->sector,
807 rbi->bi_rw, i);
808 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000809 if (use_new_offset(conf, sh))
810 rbi->bi_sector = (sh->sector
811 + rrdev->new_data_offset);
812 else
813 rbi->bi_sector = (sh->sector
814 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200815 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100816 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
817 rbi->bi_io_vec[0].bv_offset = 0;
818 rbi->bi_size = STRIPE_SIZE;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600819 if (conf->mddev->gendisk)
820 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
821 rbi, disk_devt(conf->mddev->gendisk),
822 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100823 generic_make_request(rbi);
824 }
825 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000826 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700827 set_bit(STRIPE_DEGRADED, &sh->state);
828 pr_debug("skip op %ld on disc %d for sector %llu\n",
829 bi->bi_rw, i, (unsigned long long)sh->sector);
830 clear_bit(R5_LOCKED, &sh->dev[i].flags);
831 set_bit(STRIPE_HANDLE, &sh->state);
832 }
833 }
834}
835
836static struct dma_async_tx_descriptor *
837async_copy_data(int frombio, struct bio *bio, struct page *page,
838 sector_t sector, struct dma_async_tx_descriptor *tx)
839{
840 struct bio_vec *bvl;
841 struct page *bio_page;
842 int i;
843 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700844 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700845 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700846
847 if (bio->bi_sector >= sector)
848 page_offset = (signed)(bio->bi_sector - sector) * 512;
849 else
850 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700851
Dan Williams0403e382009-09-08 17:42:50 -0700852 if (frombio)
853 flags |= ASYNC_TX_FENCE;
854 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
855
Dan Williams91c00922007-01-02 13:52:30 -0700856 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000857 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700858 int clen;
859 int b_offset = 0;
860
861 if (page_offset < 0) {
862 b_offset = -page_offset;
863 page_offset += b_offset;
864 len -= b_offset;
865 }
866
867 if (len > 0 && page_offset + len > STRIPE_SIZE)
868 clen = STRIPE_SIZE - page_offset;
869 else
870 clen = len;
871
872 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000873 b_offset += bvl->bv_offset;
874 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700875 if (frombio)
876 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700877 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700878 else
879 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700880 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700881 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700882 /* chain the operations */
883 submit.depend_tx = tx;
884
Dan Williams91c00922007-01-02 13:52:30 -0700885 if (clen < len) /* hit end of page */
886 break;
887 page_offset += len;
888 }
889
890 return tx;
891}
892
893static void ops_complete_biofill(void *stripe_head_ref)
894{
895 struct stripe_head *sh = stripe_head_ref;
896 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700897 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700898
Harvey Harrisone46b2722008-04-28 02:15:50 -0700899 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700900 (unsigned long long)sh->sector);
901
902 /* clear completed biofills */
903 for (i = sh->disks; i--; ) {
904 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700905
906 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700907 /* and check if we need to reply to a read request,
908 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000909 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700910 */
911 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700912 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700913
Dan Williams91c00922007-01-02 13:52:30 -0700914 BUG_ON(!dev->read);
915 rbi = dev->read;
916 dev->read = NULL;
917 while (rbi && rbi->bi_sector <
918 dev->sector + STRIPE_SECTORS) {
919 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000920 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700921 rbi->bi_next = return_bi;
922 return_bi = rbi;
923 }
Dan Williams91c00922007-01-02 13:52:30 -0700924 rbi = rbi2;
925 }
926 }
927 }
Dan Williams83de75c2008-06-28 08:31:58 +1000928 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700929
930 return_io(return_bi);
931
Dan Williamse4d84902007-09-24 10:06:13 -0700932 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700933 release_stripe(sh);
934}
935
936static void ops_run_biofill(struct stripe_head *sh)
937{
938 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700939 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700940 int i;
941
Harvey Harrisone46b2722008-04-28 02:15:50 -0700942 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700943 (unsigned long long)sh->sector);
944
945 for (i = sh->disks; i--; ) {
946 struct r5dev *dev = &sh->dev[i];
947 if (test_bit(R5_Wantfill, &dev->flags)) {
948 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000949 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700950 dev->read = rbi = dev->toread;
951 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000952 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700953 while (rbi && rbi->bi_sector <
954 dev->sector + STRIPE_SECTORS) {
955 tx = async_copy_data(0, rbi, dev->page,
956 dev->sector, tx);
957 rbi = r5_next_bio(rbi, dev->sector);
958 }
959 }
960 }
961
962 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700963 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
964 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700965}
966
Dan Williams4e7d2c02009-08-29 19:13:11 -0700967static void mark_target_uptodate(struct stripe_head *sh, int target)
968{
969 struct r5dev *tgt;
970
971 if (target < 0)
972 return;
973
974 tgt = &sh->dev[target];
975 set_bit(R5_UPTODATE, &tgt->flags);
976 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
977 clear_bit(R5_Wantcompute, &tgt->flags);
978}
979
Dan Williamsac6b53b2009-07-14 13:40:19 -0700980static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700981{
982 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700983
Harvey Harrisone46b2722008-04-28 02:15:50 -0700984 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700985 (unsigned long long)sh->sector);
986
Dan Williamsac6b53b2009-07-14 13:40:19 -0700987 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700988 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700989 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700990
Dan Williamsecc65c92008-06-28 08:31:57 +1000991 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
992 if (sh->check_state == check_state_compute_run)
993 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700994 set_bit(STRIPE_HANDLE, &sh->state);
995 release_stripe(sh);
996}
997
Dan Williamsd6f38f32009-07-14 11:50:52 -0700998/* return a pointer to the address conversion region of the scribble buffer */
999static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1000 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001001{
Dan Williamsd6f38f32009-07-14 11:50:52 -07001002 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
1003}
1004
1005static struct dma_async_tx_descriptor *
1006ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1007{
Dan Williams91c00922007-01-02 13:52:30 -07001008 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001009 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001010 int target = sh->ops.target;
1011 struct r5dev *tgt = &sh->dev[target];
1012 struct page *xor_dest = tgt->page;
1013 int count = 0;
1014 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001015 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001016 int i;
1017
1018 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001019 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001020 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1021
1022 for (i = disks; i--; )
1023 if (i != target)
1024 xor_srcs[count++] = sh->dev[i].page;
1025
1026 atomic_inc(&sh->count);
1027
Dan Williams0403e382009-09-08 17:42:50 -07001028 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001029 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -07001030 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001031 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001032 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001033 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001034
Dan Williams91c00922007-01-02 13:52:30 -07001035 return tx;
1036}
1037
Dan Williamsac6b53b2009-07-14 13:40:19 -07001038/* set_syndrome_sources - populate source buffers for gen_syndrome
1039 * @srcs - (struct page *) array of size sh->disks
1040 * @sh - stripe_head to parse
1041 *
1042 * Populates srcs in proper layout order for the stripe and returns the
1043 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1044 * destination buffer is recorded in srcs[count] and the Q destination
1045 * is recorded in srcs[count+1]].
1046 */
1047static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1048{
1049 int disks = sh->disks;
1050 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1051 int d0_idx = raid6_d0(sh);
1052 int count;
1053 int i;
1054
1055 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001056 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057
1058 count = 0;
1059 i = d0_idx;
1060 do {
1061 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1062
1063 srcs[slot] = sh->dev[i].page;
1064 i = raid6_next_disk(i, disks);
1065 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001066
NeilBrowne4424fe2009-10-16 16:27:34 +11001067 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001068}
1069
1070static struct dma_async_tx_descriptor *
1071ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1072{
1073 int disks = sh->disks;
1074 struct page **blocks = percpu->scribble;
1075 int target;
1076 int qd_idx = sh->qd_idx;
1077 struct dma_async_tx_descriptor *tx;
1078 struct async_submit_ctl submit;
1079 struct r5dev *tgt;
1080 struct page *dest;
1081 int i;
1082 int count;
1083
1084 if (sh->ops.target < 0)
1085 target = sh->ops.target2;
1086 else if (sh->ops.target2 < 0)
1087 target = sh->ops.target;
1088 else
1089 /* we should only have one valid target */
1090 BUG();
1091 BUG_ON(target < 0);
1092 pr_debug("%s: stripe %llu block: %d\n",
1093 __func__, (unsigned long long)sh->sector, target);
1094
1095 tgt = &sh->dev[target];
1096 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1097 dest = tgt->page;
1098
1099 atomic_inc(&sh->count);
1100
1101 if (target == qd_idx) {
1102 count = set_syndrome_sources(blocks, sh);
1103 blocks[count] = NULL; /* regenerating p is not necessary */
1104 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001105 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1106 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001107 to_addr_conv(sh, percpu));
1108 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1109 } else {
1110 /* Compute any data- or p-drive using XOR */
1111 count = 0;
1112 for (i = disks; i-- ; ) {
1113 if (i == target || i == qd_idx)
1114 continue;
1115 blocks[count++] = sh->dev[i].page;
1116 }
1117
Dan Williams0403e382009-09-08 17:42:50 -07001118 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1119 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001120 to_addr_conv(sh, percpu));
1121 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1122 }
1123
1124 return tx;
1125}
1126
1127static struct dma_async_tx_descriptor *
1128ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1129{
1130 int i, count, disks = sh->disks;
1131 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1132 int d0_idx = raid6_d0(sh);
1133 int faila = -1, failb = -1;
1134 int target = sh->ops.target;
1135 int target2 = sh->ops.target2;
1136 struct r5dev *tgt = &sh->dev[target];
1137 struct r5dev *tgt2 = &sh->dev[target2];
1138 struct dma_async_tx_descriptor *tx;
1139 struct page **blocks = percpu->scribble;
1140 struct async_submit_ctl submit;
1141
1142 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1143 __func__, (unsigned long long)sh->sector, target, target2);
1144 BUG_ON(target < 0 || target2 < 0);
1145 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1146 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1147
Dan Williams6c910a72009-09-16 12:24:54 -07001148 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001149 * slot number conversion for 'faila' and 'failb'
1150 */
1151 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001152 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001153 count = 0;
1154 i = d0_idx;
1155 do {
1156 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1157
1158 blocks[slot] = sh->dev[i].page;
1159
1160 if (i == target)
1161 faila = slot;
1162 if (i == target2)
1163 failb = slot;
1164 i = raid6_next_disk(i, disks);
1165 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001166
1167 BUG_ON(faila == failb);
1168 if (failb < faila)
1169 swap(faila, failb);
1170 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1171 __func__, (unsigned long long)sh->sector, faila, failb);
1172
1173 atomic_inc(&sh->count);
1174
1175 if (failb == syndrome_disks+1) {
1176 /* Q disk is one of the missing disks */
1177 if (faila == syndrome_disks) {
1178 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001179 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1180 ops_complete_compute, sh,
1181 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001182 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001183 STRIPE_SIZE, &submit);
1184 } else {
1185 struct page *dest;
1186 int data_target;
1187 int qd_idx = sh->qd_idx;
1188
1189 /* Missing D+Q: recompute D from P, then recompute Q */
1190 if (target == qd_idx)
1191 data_target = target2;
1192 else
1193 data_target = target;
1194
1195 count = 0;
1196 for (i = disks; i-- ; ) {
1197 if (i == data_target || i == qd_idx)
1198 continue;
1199 blocks[count++] = sh->dev[i].page;
1200 }
1201 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001202 init_async_submit(&submit,
1203 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1204 NULL, NULL, NULL,
1205 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001206 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1207 &submit);
1208
1209 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001210 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1211 ops_complete_compute, sh,
1212 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001213 return async_gen_syndrome(blocks, 0, count+2,
1214 STRIPE_SIZE, &submit);
1215 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001216 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001217 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1218 ops_complete_compute, sh,
1219 to_addr_conv(sh, percpu));
1220 if (failb == syndrome_disks) {
1221 /* We're missing D+P. */
1222 return async_raid6_datap_recov(syndrome_disks+2,
1223 STRIPE_SIZE, faila,
1224 blocks, &submit);
1225 } else {
1226 /* We're missing D+D. */
1227 return async_raid6_2data_recov(syndrome_disks+2,
1228 STRIPE_SIZE, faila, failb,
1229 blocks, &submit);
1230 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001231 }
1232}
1233
1234
Dan Williams91c00922007-01-02 13:52:30 -07001235static void ops_complete_prexor(void *stripe_head_ref)
1236{
1237 struct stripe_head *sh = stripe_head_ref;
1238
Harvey Harrisone46b2722008-04-28 02:15:50 -07001239 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001240 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001241}
1242
1243static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001244ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1245 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001246{
Dan Williams91c00922007-01-02 13:52:30 -07001247 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001248 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001249 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001250 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001251
1252 /* existing parity data subtracted */
1253 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1254
Harvey Harrisone46b2722008-04-28 02:15:50 -07001255 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001256 (unsigned long long)sh->sector);
1257
1258 for (i = disks; i--; ) {
1259 struct r5dev *dev = &sh->dev[i];
1260 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001261 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001262 xor_srcs[count++] = dev->page;
1263 }
1264
Dan Williams0403e382009-09-08 17:42:50 -07001265 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001266 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001267 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001268
1269 return tx;
1270}
1271
1272static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001273ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001274{
1275 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001276 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001277
Harvey Harrisone46b2722008-04-28 02:15:50 -07001278 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001279 (unsigned long long)sh->sector);
1280
1281 for (i = disks; i--; ) {
1282 struct r5dev *dev = &sh->dev[i];
1283 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001284
Dan Williamsd8ee0722008-06-28 08:32:06 +10001285 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001286 struct bio *wbi;
1287
Shaohua Lib17459c2012-07-19 16:01:31 +10001288 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001289 chosen = dev->towrite;
1290 dev->towrite = NULL;
1291 BUG_ON(dev->written);
1292 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001293 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001294
1295 while (wbi && wbi->bi_sector <
1296 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001297 if (wbi->bi_rw & REQ_FUA)
1298 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001299 if (wbi->bi_rw & REQ_SYNC)
1300 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001301 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001302 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001303 else
Shaohua Li620125f2012-10-11 13:49:05 +11001304 tx = async_copy_data(1, wbi, dev->page,
1305 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001306 wbi = r5_next_bio(wbi, dev->sector);
1307 }
1308 }
1309 }
1310
1311 return tx;
1312}
1313
Dan Williamsac6b53b2009-07-14 13:40:19 -07001314static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001315{
1316 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001317 int disks = sh->disks;
1318 int pd_idx = sh->pd_idx;
1319 int qd_idx = sh->qd_idx;
1320 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001321 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001322
Harvey Harrisone46b2722008-04-28 02:15:50 -07001323 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001324 (unsigned long long)sh->sector);
1325
Shaohua Libc0934f2012-05-22 13:55:05 +10001326 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001327 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001328 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001329 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001330 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001331
Dan Williams91c00922007-01-02 13:52:30 -07001332 for (i = disks; i--; ) {
1333 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001334
Tejun Heoe9c74692010-09-03 11:56:18 +02001335 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001336 if (!discard)
1337 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001338 if (fua)
1339 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001340 if (sync)
1341 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001342 }
Dan Williams91c00922007-01-02 13:52:30 -07001343 }
1344
Dan Williamsd8ee0722008-06-28 08:32:06 +10001345 if (sh->reconstruct_state == reconstruct_state_drain_run)
1346 sh->reconstruct_state = reconstruct_state_drain_result;
1347 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1348 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1349 else {
1350 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1351 sh->reconstruct_state = reconstruct_state_result;
1352 }
Dan Williams91c00922007-01-02 13:52:30 -07001353
1354 set_bit(STRIPE_HANDLE, &sh->state);
1355 release_stripe(sh);
1356}
1357
1358static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001359ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1360 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001361{
Dan Williams91c00922007-01-02 13:52:30 -07001362 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001363 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001364 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001365 int count = 0, pd_idx = sh->pd_idx, i;
1366 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001367 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001368 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001369
Harvey Harrisone46b2722008-04-28 02:15:50 -07001370 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001371 (unsigned long long)sh->sector);
1372
Shaohua Li620125f2012-10-11 13:49:05 +11001373 for (i = 0; i < sh->disks; i++) {
1374 if (pd_idx == i)
1375 continue;
1376 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1377 break;
1378 }
1379 if (i >= sh->disks) {
1380 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001381 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1382 ops_complete_reconstruct(sh);
1383 return;
1384 }
Dan Williams91c00922007-01-02 13:52:30 -07001385 /* check if prexor is active which means only process blocks
1386 * that are part of a read-modify-write (written)
1387 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001388 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1389 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001390 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1391 for (i = disks; i--; ) {
1392 struct r5dev *dev = &sh->dev[i];
1393 if (dev->written)
1394 xor_srcs[count++] = dev->page;
1395 }
1396 } else {
1397 xor_dest = sh->dev[pd_idx].page;
1398 for (i = disks; i--; ) {
1399 struct r5dev *dev = &sh->dev[i];
1400 if (i != pd_idx)
1401 xor_srcs[count++] = dev->page;
1402 }
1403 }
1404
Dan Williams91c00922007-01-02 13:52:30 -07001405 /* 1/ if we prexor'd then the dest is reused as a source
1406 * 2/ if we did not prexor then we are redoing the parity
1407 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1408 * for the synchronous xor case
1409 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001410 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001411 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1412
1413 atomic_inc(&sh->count);
1414
Dan Williamsac6b53b2009-07-14 13:40:19 -07001415 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001416 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001417 if (unlikely(count == 1))
1418 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1419 else
1420 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001421}
1422
Dan Williamsac6b53b2009-07-14 13:40:19 -07001423static void
1424ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1425 struct dma_async_tx_descriptor *tx)
1426{
1427 struct async_submit_ctl submit;
1428 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001429 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001430
1431 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1432
Shaohua Li620125f2012-10-11 13:49:05 +11001433 for (i = 0; i < sh->disks; i++) {
1434 if (sh->pd_idx == i || sh->qd_idx == i)
1435 continue;
1436 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1437 break;
1438 }
1439 if (i >= sh->disks) {
1440 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001441 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1442 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1443 ops_complete_reconstruct(sh);
1444 return;
1445 }
1446
Dan Williamsac6b53b2009-07-14 13:40:19 -07001447 count = set_syndrome_sources(blocks, sh);
1448
1449 atomic_inc(&sh->count);
1450
1451 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1452 sh, to_addr_conv(sh, percpu));
1453 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001454}
1455
1456static void ops_complete_check(void *stripe_head_ref)
1457{
1458 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001459
Harvey Harrisone46b2722008-04-28 02:15:50 -07001460 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001461 (unsigned long long)sh->sector);
1462
Dan Williamsecc65c92008-06-28 08:31:57 +10001463 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001464 set_bit(STRIPE_HANDLE, &sh->state);
1465 release_stripe(sh);
1466}
1467
Dan Williamsac6b53b2009-07-14 13:40:19 -07001468static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001469{
Dan Williams91c00922007-01-02 13:52:30 -07001470 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001471 int pd_idx = sh->pd_idx;
1472 int qd_idx = sh->qd_idx;
1473 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001474 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001475 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001476 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001477 int count;
1478 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001479
Harvey Harrisone46b2722008-04-28 02:15:50 -07001480 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001481 (unsigned long long)sh->sector);
1482
Dan Williamsac6b53b2009-07-14 13:40:19 -07001483 count = 0;
1484 xor_dest = sh->dev[pd_idx].page;
1485 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001486 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487 if (i == pd_idx || i == qd_idx)
1488 continue;
1489 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001490 }
1491
Dan Williamsd6f38f32009-07-14 11:50:52 -07001492 init_async_submit(&submit, 0, NULL, NULL, NULL,
1493 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001494 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001495 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001496
Dan Williams91c00922007-01-02 13:52:30 -07001497 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001498 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1499 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001500}
1501
Dan Williamsac6b53b2009-07-14 13:40:19 -07001502static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1503{
1504 struct page **srcs = percpu->scribble;
1505 struct async_submit_ctl submit;
1506 int count;
1507
1508 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1509 (unsigned long long)sh->sector, checkp);
1510
1511 count = set_syndrome_sources(srcs, sh);
1512 if (!checkp)
1513 srcs[count] = NULL;
1514
1515 atomic_inc(&sh->count);
1516 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1517 sh, to_addr_conv(sh, percpu));
1518 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1519 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1520}
1521
NeilBrown51acbce2013-02-28 09:08:34 +11001522static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001523{
1524 int overlap_clear = 0, i, disks = sh->disks;
1525 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001526 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001527 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001528 struct raid5_percpu *percpu;
1529 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001530
Dan Williamsd6f38f32009-07-14 11:50:52 -07001531 cpu = get_cpu();
1532 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001533 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001534 ops_run_biofill(sh);
1535 overlap_clear++;
1536 }
1537
Dan Williams7b3a8712008-06-28 08:32:09 +10001538 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001539 if (level < 6)
1540 tx = ops_run_compute5(sh, percpu);
1541 else {
1542 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1543 tx = ops_run_compute6_1(sh, percpu);
1544 else
1545 tx = ops_run_compute6_2(sh, percpu);
1546 }
1547 /* terminate the chain if reconstruct is not set to be run */
1548 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001549 async_tx_ack(tx);
1550 }
Dan Williams91c00922007-01-02 13:52:30 -07001551
Dan Williams600aa102008-06-28 08:32:05 +10001552 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001553 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001554
Dan Williams600aa102008-06-28 08:32:05 +10001555 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001556 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001557 overlap_clear++;
1558 }
1559
Dan Williamsac6b53b2009-07-14 13:40:19 -07001560 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1561 if (level < 6)
1562 ops_run_reconstruct5(sh, percpu, tx);
1563 else
1564 ops_run_reconstruct6(sh, percpu, tx);
1565 }
Dan Williams91c00922007-01-02 13:52:30 -07001566
Dan Williamsac6b53b2009-07-14 13:40:19 -07001567 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1568 if (sh->check_state == check_state_run)
1569 ops_run_check_p(sh, percpu);
1570 else if (sh->check_state == check_state_run_q)
1571 ops_run_check_pq(sh, percpu, 0);
1572 else if (sh->check_state == check_state_run_pq)
1573 ops_run_check_pq(sh, percpu, 1);
1574 else
1575 BUG();
1576 }
Dan Williams91c00922007-01-02 13:52:30 -07001577
Dan Williams91c00922007-01-02 13:52:30 -07001578 if (overlap_clear)
1579 for (i = disks; i--; ) {
1580 struct r5dev *dev = &sh->dev[i];
1581 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1582 wake_up(&sh->raid_conf->wait_for_overlap);
1583 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001584 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001585}
1586
NeilBrownd1688a62011-10-11 16:49:52 +11001587static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001590 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001591 if (!sh)
1592 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001593
NeilBrown3f294f42005-11-08 21:39:25 -08001594 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001595
Shaohua Lib17459c2012-07-19 16:01:31 +10001596 spin_lock_init(&sh->stripe_lock);
1597
NeilBrowne4e11e32010-06-16 16:45:16 +10001598 if (grow_buffers(sh)) {
1599 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001600 kmem_cache_free(conf->slab_cache, sh);
1601 return 0;
1602 }
1603 /* we just created an active stripe so... */
1604 atomic_set(&sh->count, 1);
1605 atomic_inc(&conf->active_stripes);
1606 INIT_LIST_HEAD(&sh->lru);
1607 release_stripe(sh);
1608 return 1;
1609}
1610
NeilBrownd1688a62011-10-11 16:49:52 +11001611static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001612{
Christoph Lametere18b8902006-12-06 20:33:20 -08001613 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001614 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
NeilBrownf4be6b42010-06-01 19:37:25 +10001616 if (conf->mddev->gendisk)
1617 sprintf(conf->cache_name[0],
1618 "raid%d-%s", conf->level, mdname(conf->mddev));
1619 else
1620 sprintf(conf->cache_name[0],
1621 "raid%d-%p", conf->level, conf->mddev);
1622 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1623
NeilBrownad01c9e2006-03-27 01:18:07 -08001624 conf->active_name = 0;
1625 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001627 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (!sc)
1629 return 1;
1630 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001631 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001632 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001633 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
NeilBrown29269552006-03-27 01:18:10 -08001637
Dan Williamsd6f38f32009-07-14 11:50:52 -07001638/**
1639 * scribble_len - return the required size of the scribble region
1640 * @num - total number of disks in the array
1641 *
1642 * The size must be enough to contain:
1643 * 1/ a struct page pointer for each device in the array +2
1644 * 2/ room to convert each entry in (1) to its corresponding dma
1645 * (dma_map_page()) or page (page_address()) address.
1646 *
1647 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1648 * calculate over all devices (not just the data blocks), using zeros in place
1649 * of the P and Q blocks.
1650 */
1651static size_t scribble_len(int num)
1652{
1653 size_t len;
1654
1655 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1656
1657 return len;
1658}
1659
NeilBrownd1688a62011-10-11 16:49:52 +11001660static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001661{
1662 /* Make all the stripes able to hold 'newsize' devices.
1663 * New slots in each stripe get 'page' set to a new page.
1664 *
1665 * This happens in stages:
1666 * 1/ create a new kmem_cache and allocate the required number of
1667 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001668 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001669 * to the new stripe_heads. This will have the side effect of
1670 * freezing the array as once all stripe_heads have been collected,
1671 * no IO will be possible. Old stripe heads are freed once their
1672 * pages have been transferred over, and the old kmem_cache is
1673 * freed when all stripes are done.
1674 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1675 * we simple return a failre status - no need to clean anything up.
1676 * 4/ allocate new pages for the new slots in the new stripe_heads.
1677 * If this fails, we don't bother trying the shrink the
1678 * stripe_heads down again, we just leave them as they are.
1679 * As each stripe_head is processed the new one is released into
1680 * active service.
1681 *
1682 * Once step2 is started, we cannot afford to wait for a write,
1683 * so we use GFP_NOIO allocations.
1684 */
1685 struct stripe_head *osh, *nsh;
1686 LIST_HEAD(newstripes);
1687 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001688 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001689 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001690 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001691 int i;
1692
1693 if (newsize <= conf->pool_size)
1694 return 0; /* never bother to shrink */
1695
Dan Williamsb5470dc2008-06-27 21:44:04 -07001696 err = md_allow_write(conf->mddev);
1697 if (err)
1698 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001699
NeilBrownad01c9e2006-03-27 01:18:07 -08001700 /* Step 1 */
1701 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1702 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001703 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001704 if (!sc)
1705 return -ENOMEM;
1706
1707 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001708 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001709 if (!nsh)
1710 break;
1711
NeilBrownad01c9e2006-03-27 01:18:07 -08001712 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001713 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001714
1715 list_add(&nsh->lru, &newstripes);
1716 }
1717 if (i) {
1718 /* didn't get enough, give up */
1719 while (!list_empty(&newstripes)) {
1720 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1721 list_del(&nsh->lru);
1722 kmem_cache_free(sc, nsh);
1723 }
1724 kmem_cache_destroy(sc);
1725 return -ENOMEM;
1726 }
1727 /* Step 2 - Must use GFP_NOIO now.
1728 * OK, we have enough stripes, start collecting inactive
1729 * stripes and copying them over
1730 */
1731 list_for_each_entry(nsh, &newstripes, lru) {
1732 spin_lock_irq(&conf->device_lock);
1733 wait_event_lock_irq(conf->wait_for_stripe,
1734 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001735 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001736 osh = get_free_stripe(conf);
1737 spin_unlock_irq(&conf->device_lock);
1738 atomic_set(&nsh->count, 1);
1739 for(i=0; i<conf->pool_size; i++)
1740 nsh->dev[i].page = osh->dev[i].page;
1741 for( ; i<newsize; i++)
1742 nsh->dev[i].page = NULL;
1743 kmem_cache_free(conf->slab_cache, osh);
1744 }
1745 kmem_cache_destroy(conf->slab_cache);
1746
1747 /* Step 3.
1748 * At this point, we are holding all the stripes so the array
1749 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001750 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001751 */
1752 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1753 if (ndisks) {
1754 for (i=0; i<conf->raid_disks; i++)
1755 ndisks[i] = conf->disks[i];
1756 kfree(conf->disks);
1757 conf->disks = ndisks;
1758 } else
1759 err = -ENOMEM;
1760
Dan Williamsd6f38f32009-07-14 11:50:52 -07001761 get_online_cpus();
1762 conf->scribble_len = scribble_len(newsize);
1763 for_each_present_cpu(cpu) {
1764 struct raid5_percpu *percpu;
1765 void *scribble;
1766
1767 percpu = per_cpu_ptr(conf->percpu, cpu);
1768 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1769
1770 if (scribble) {
1771 kfree(percpu->scribble);
1772 percpu->scribble = scribble;
1773 } else {
1774 err = -ENOMEM;
1775 break;
1776 }
1777 }
1778 put_online_cpus();
1779
NeilBrownad01c9e2006-03-27 01:18:07 -08001780 /* Step 4, return new stripes to service */
1781 while(!list_empty(&newstripes)) {
1782 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1783 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001784
NeilBrownad01c9e2006-03-27 01:18:07 -08001785 for (i=conf->raid_disks; i < newsize; i++)
1786 if (nsh->dev[i].page == NULL) {
1787 struct page *p = alloc_page(GFP_NOIO);
1788 nsh->dev[i].page = p;
1789 if (!p)
1790 err = -ENOMEM;
1791 }
1792 release_stripe(nsh);
1793 }
1794 /* critical section pass, GFP_NOIO no longer needed */
1795
1796 conf->slab_cache = sc;
1797 conf->active_name = 1-conf->active_name;
1798 conf->pool_size = newsize;
1799 return err;
1800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
NeilBrownd1688a62011-10-11 16:49:52 +11001802static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 struct stripe_head *sh;
1805
NeilBrown3f294f42005-11-08 21:39:25 -08001806 spin_lock_irq(&conf->device_lock);
1807 sh = get_free_stripe(conf);
1808 spin_unlock_irq(&conf->device_lock);
1809 if (!sh)
1810 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001811 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001812 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001813 kmem_cache_free(conf->slab_cache, sh);
1814 atomic_dec(&conf->active_stripes);
1815 return 1;
1816}
1817
NeilBrownd1688a62011-10-11 16:49:52 +11001818static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001819{
1820 while (drop_one_stripe(conf))
1821 ;
1822
NeilBrown29fc7e32006-02-03 03:03:41 -08001823 if (conf->slab_cache)
1824 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 conf->slab_cache = NULL;
1826}
1827
NeilBrown6712ecf2007-09-27 12:47:43 +02001828static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
NeilBrown99c0fb52009-03-31 14:39:38 +11001830 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001831 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001832 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001834 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001835 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001836 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 for (i=0 ; i<disks; i++)
1839 if (bi == &sh->dev[i].req)
1840 break;
1841
Dan Williams45b42332007-07-09 11:56:43 -07001842 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1843 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 uptodate);
1845 if (i == disks) {
1846 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001847 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
NeilBrown14a75d32011-12-23 10:17:52 +11001849 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001850 /* If replacement finished while this request was outstanding,
1851 * 'replacement' might be NULL already.
1852 * In that case it moved down to 'rdev'.
1853 * rdev is not removed until all requests are finished.
1854 */
NeilBrown14a75d32011-12-23 10:17:52 +11001855 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001856 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001857 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
NeilBrown05616be2012-05-21 09:27:00 +10001859 if (use_new_offset(conf, sh))
1860 s = sh->sector + rdev->new_data_offset;
1861 else
1862 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001865 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001866 /* Note that this cannot happen on a
1867 * replacement device. We just fail those on
1868 * any error
1869 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001870 printk_ratelimited(
1871 KERN_INFO
1872 "md/raid:%s: read error corrected"
1873 " (%lu sectors at %llu on %s)\n",
1874 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001875 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001876 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001877 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001878 clear_bit(R5_ReadError, &sh->dev[i].flags);
1879 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001880 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1881 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1882
NeilBrown14a75d32011-12-23 10:17:52 +11001883 if (atomic_read(&rdev->read_errors))
1884 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001886 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001887 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001888 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001891 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001892 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1893 printk_ratelimited(
1894 KERN_WARNING
1895 "md/raid:%s: read error on replacement device "
1896 "(sector %llu on %s).\n",
1897 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001898 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001899 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001900 else if (conf->mddev->degraded >= conf->max_degraded) {
1901 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001902 printk_ratelimited(
1903 KERN_WARNING
1904 "md/raid:%s: read error not correctable "
1905 "(sector %llu on %s).\n",
1906 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001907 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001908 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001909 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001910 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001911 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001912 printk_ratelimited(
1913 KERN_WARNING
1914 "md/raid:%s: read error NOT corrected!! "
1915 "(sector %llu on %s).\n",
1916 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001917 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001918 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001919 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001920 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001921 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001922 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001923 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001924 else
1925 retry = 1;
1926 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001927 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1928 set_bit(R5_ReadError, &sh->dev[i].flags);
1929 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1930 } else
1931 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001932 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001933 clear_bit(R5_ReadError, &sh->dev[i].flags);
1934 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001935 if (!(set_bad
1936 && test_bit(In_sync, &rdev->flags)
1937 && rdev_set_badblocks(
1938 rdev, sh->sector, STRIPE_SECTORS, 0)))
1939 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
NeilBrown14a75d32011-12-23 10:17:52 +11001942 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1944 set_bit(STRIPE_HANDLE, &sh->state);
1945 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
NeilBrownd710e132008-10-13 11:55:12 +11001948static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
NeilBrown99c0fb52009-03-31 14:39:38 +11001950 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001951 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001952 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001953 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001955 sector_t first_bad;
1956 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001957 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
NeilBrown977df362011-12-23 10:17:53 +11001959 for (i = 0 ; i < disks; i++) {
1960 if (bi == &sh->dev[i].req) {
1961 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 break;
NeilBrown977df362011-12-23 10:17:53 +11001963 }
1964 if (bi == &sh->dev[i].rreq) {
1965 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001966 if (rdev)
1967 replacement = 1;
1968 else
1969 /* rdev was removed and 'replacement'
1970 * replaced it. rdev is not removed
1971 * until all requests are finished.
1972 */
1973 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001974 break;
1975 }
1976 }
Dan Williams45b42332007-07-09 11:56:43 -07001977 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1979 uptodate);
1980 if (i == disks) {
1981 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
NeilBrown977df362011-12-23 10:17:53 +11001985 if (replacement) {
1986 if (!uptodate)
1987 md_error(conf->mddev, rdev);
1988 else if (is_badblock(rdev, sh->sector,
1989 STRIPE_SECTORS,
1990 &first_bad, &bad_sectors))
1991 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1992 } else {
1993 if (!uptodate) {
1994 set_bit(WriteErrorSeen, &rdev->flags);
1995 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001996 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1997 set_bit(MD_RECOVERY_NEEDED,
1998 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001999 } else if (is_badblock(rdev, sh->sector,
2000 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002001 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002002 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002003 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2004 /* That was a successful write so make
2005 * sure it looks like we already did
2006 * a re-write.
2007 */
2008 set_bit(R5_ReWrite, &sh->dev[i].flags);
2009 }
NeilBrown977df362011-12-23 10:17:53 +11002010 }
2011 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
NeilBrown977df362011-12-23 10:17:53 +11002013 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2014 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002016 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
NeilBrown784052e2009-03-31 15:19:07 +11002019static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
NeilBrown784052e2009-03-31 15:19:07 +11002021static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 struct r5dev *dev = &sh->dev[i];
2024
2025 bio_init(&dev->req);
2026 dev->req.bi_io_vec = &dev->vec;
2027 dev->req.bi_vcnt++;
2028 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11002030 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
NeilBrown977df362011-12-23 10:17:53 +11002032 bio_init(&dev->rreq);
2033 dev->rreq.bi_io_vec = &dev->rvec;
2034 dev->rreq.bi_vcnt++;
2035 dev->rreq.bi_max_vecs++;
2036 dev->rreq.bi_private = sh;
2037 dev->rvec.bv_page = dev->page;
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002040 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
NeilBrownfd01b882011-10-11 16:47:53 +11002043static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002046 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002047 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002048 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
NeilBrown908f4fb2011-12-23 10:17:50 +11002050 spin_lock_irqsave(&conf->device_lock, flags);
2051 clear_bit(In_sync, &rdev->flags);
2052 mddev->degraded = calc_degraded(conf);
2053 spin_unlock_irqrestore(&conf->device_lock, flags);
2054 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2055
NeilBrownde393cd2011-07-28 11:31:48 +10002056 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002057 set_bit(Faulty, &rdev->flags);
2058 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2059 printk(KERN_ALERT
2060 "md/raid:%s: Disk failure on %s, disabling device.\n"
2061 "md/raid:%s: Operation continuing on %d devices.\n",
2062 mdname(mddev),
2063 bdevname(rdev->bdev, b),
2064 mdname(mddev),
2065 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068/*
2069 * Input: a 'big' sector number,
2070 * Output: index of the data and parity disk, and the sector # in them.
2071 */
NeilBrownd1688a62011-10-11 16:49:52 +11002072static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002073 int previous, int *dd_idx,
2074 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002076 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002077 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002079 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002080 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002082 int algorithm = previous ? conf->prev_algo
2083 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002084 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2085 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002086 int raid_disks = previous ? conf->previous_raid_disks
2087 : conf->raid_disks;
2088 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* First compute the information on this sector */
2091
2092 /*
2093 * Compute the chunk number and the sector offset inside the chunk
2094 */
2095 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2096 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /*
2099 * Compute the stripe number
2100 */
NeilBrown35f2a592010-04-20 14:13:34 +10002101 stripe = chunk_number;
2102 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002103 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /*
2105 * Select the parity disk based on the user selected algorithm.
2106 */
NeilBrown84789552011-07-27 11:00:36 +10002107 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002108 switch(conf->level) {
2109 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002110 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002111 break;
2112 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002113 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002115 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002116 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 (*dd_idx)++;
2118 break;
2119 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002120 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002121 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 (*dd_idx)++;
2123 break;
2124 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002125 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002126 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 break;
2128 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002129 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002130 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002132 case ALGORITHM_PARITY_0:
2133 pd_idx = 0;
2134 (*dd_idx)++;
2135 break;
2136 case ALGORITHM_PARITY_N:
2137 pd_idx = data_disks;
2138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002140 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002141 }
2142 break;
2143 case 6:
2144
NeilBrowne183eae2009-03-31 15:20:22 +11002145 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002146 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002147 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002148 qd_idx = pd_idx + 1;
2149 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002150 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002151 qd_idx = 0;
2152 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002153 (*dd_idx) += 2; /* D D P Q D */
2154 break;
2155 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002156 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002157 qd_idx = pd_idx + 1;
2158 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002159 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002160 qd_idx = 0;
2161 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002162 (*dd_idx) += 2; /* D D P Q D */
2163 break;
2164 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002165 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002166 qd_idx = (pd_idx + 1) % raid_disks;
2167 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002168 break;
2169 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002170 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002171 qd_idx = (pd_idx + 1) % raid_disks;
2172 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002174
2175 case ALGORITHM_PARITY_0:
2176 pd_idx = 0;
2177 qd_idx = 1;
2178 (*dd_idx) += 2;
2179 break;
2180 case ALGORITHM_PARITY_N:
2181 pd_idx = data_disks;
2182 qd_idx = data_disks + 1;
2183 break;
2184
2185 case ALGORITHM_ROTATING_ZERO_RESTART:
2186 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2187 * of blocks for computing Q is different.
2188 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002189 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002190 qd_idx = pd_idx + 1;
2191 if (pd_idx == raid_disks-1) {
2192 (*dd_idx)++; /* Q D D D P */
2193 qd_idx = 0;
2194 } else if (*dd_idx >= pd_idx)
2195 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002196 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002197 break;
2198
2199 case ALGORITHM_ROTATING_N_RESTART:
2200 /* Same a left_asymmetric, by first stripe is
2201 * D D D P Q rather than
2202 * Q D D D P
2203 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002204 stripe2 += 1;
2205 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002206 qd_idx = pd_idx + 1;
2207 if (pd_idx == raid_disks-1) {
2208 (*dd_idx)++; /* Q D D D P */
2209 qd_idx = 0;
2210 } else if (*dd_idx >= pd_idx)
2211 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002212 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002213 break;
2214
2215 case ALGORITHM_ROTATING_N_CONTINUE:
2216 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002217 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002218 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2219 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002220 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002221 break;
2222
2223 case ALGORITHM_LEFT_ASYMMETRIC_6:
2224 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002225 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002226 if (*dd_idx >= pd_idx)
2227 (*dd_idx)++;
2228 qd_idx = raid_disks - 1;
2229 break;
2230
2231 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002232 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002233 if (*dd_idx >= pd_idx)
2234 (*dd_idx)++;
2235 qd_idx = raid_disks - 1;
2236 break;
2237
2238 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002239 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002240 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2241 qd_idx = raid_disks - 1;
2242 break;
2243
2244 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002245 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002246 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2247 qd_idx = raid_disks - 1;
2248 break;
2249
2250 case ALGORITHM_PARITY_0_6:
2251 pd_idx = 0;
2252 (*dd_idx)++;
2253 qd_idx = raid_disks - 1;
2254 break;
2255
NeilBrown16a53ec2006-06-26 00:27:38 -07002256 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002257 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002258 }
2259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
NeilBrown911d4ee2009-03-31 14:39:38 +11002262 if (sh) {
2263 sh->pd_idx = pd_idx;
2264 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002265 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /*
2268 * Finally, compute the new sector number
2269 */
2270 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2271 return new_sector;
2272}
2273
2274
NeilBrown784052e2009-03-31 15:19:07 +11002275static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
NeilBrownd1688a62011-10-11 16:49:52 +11002277 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002278 int raid_disks = sh->disks;
2279 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002281 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2282 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002283 int algorithm = previous ? conf->prev_algo
2284 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 sector_t stripe;
2286 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002287 sector_t chunk_number;
2288 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002290 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
NeilBrown16a53ec2006-06-26 00:27:38 -07002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2294 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
NeilBrown16a53ec2006-06-26 00:27:38 -07002296 if (i == sh->pd_idx)
2297 return 0;
2298 switch(conf->level) {
2299 case 4: break;
2300 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002301 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 case ALGORITHM_LEFT_ASYMMETRIC:
2303 case ALGORITHM_RIGHT_ASYMMETRIC:
2304 if (i > sh->pd_idx)
2305 i--;
2306 break;
2307 case ALGORITHM_LEFT_SYMMETRIC:
2308 case ALGORITHM_RIGHT_SYMMETRIC:
2309 if (i < sh->pd_idx)
2310 i += raid_disks;
2311 i -= (sh->pd_idx + 1);
2312 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002313 case ALGORITHM_PARITY_0:
2314 i -= 1;
2315 break;
2316 case ALGORITHM_PARITY_N:
2317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002319 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002320 }
2321 break;
2322 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002323 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002324 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002325 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002326 case ALGORITHM_LEFT_ASYMMETRIC:
2327 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002328 case ALGORITHM_ROTATING_ZERO_RESTART:
2329 case ALGORITHM_ROTATING_N_RESTART:
2330 if (sh->pd_idx == raid_disks-1)
2331 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002332 else if (i > sh->pd_idx)
2333 i -= 2; /* D D P Q D */
2334 break;
2335 case ALGORITHM_LEFT_SYMMETRIC:
2336 case ALGORITHM_RIGHT_SYMMETRIC:
2337 if (sh->pd_idx == raid_disks-1)
2338 i--; /* Q D D D P */
2339 else {
2340 /* D D P Q D */
2341 if (i < sh->pd_idx)
2342 i += raid_disks;
2343 i -= (sh->pd_idx + 2);
2344 }
2345 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002346 case ALGORITHM_PARITY_0:
2347 i -= 2;
2348 break;
2349 case ALGORITHM_PARITY_N:
2350 break;
2351 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002352 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002353 if (sh->pd_idx == 0)
2354 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002355 else {
2356 /* D D Q P D */
2357 if (i < sh->pd_idx)
2358 i += raid_disks;
2359 i -= (sh->pd_idx + 1);
2360 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002361 break;
2362 case ALGORITHM_LEFT_ASYMMETRIC_6:
2363 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2364 if (i > sh->pd_idx)
2365 i--;
2366 break;
2367 case ALGORITHM_LEFT_SYMMETRIC_6:
2368 case ALGORITHM_RIGHT_SYMMETRIC_6:
2369 if (i < sh->pd_idx)
2370 i += data_disks + 1;
2371 i -= (sh->pd_idx + 1);
2372 break;
2373 case ALGORITHM_PARITY_0_6:
2374 i -= 1;
2375 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002376 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002377 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002378 }
2379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381
2382 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002383 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
NeilBrown112bf892009-03-31 14:39:38 +11002385 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002386 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002387 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2388 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002389 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2390 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return 0;
2392 }
2393 return r_sector;
2394}
2395
2396
Dan Williams600aa102008-06-28 08:32:05 +10002397static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002398schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002399 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002400{
2401 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002402 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002403 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002404
Dan Williamse33129d2007-01-02 13:52:30 -07002405 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002406
2407 for (i = disks; i--; ) {
2408 struct r5dev *dev = &sh->dev[i];
2409
2410 if (dev->towrite) {
2411 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002412 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002413 if (!expand)
2414 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002415 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002416 }
2417 }
NeilBrownce7d3632013-03-04 12:37:14 +11002418 /* if we are not expanding this is a proper write request, and
2419 * there will be bios with new data to be drained into the
2420 * stripe cache
2421 */
2422 if (!expand) {
2423 if (!s->locked)
2424 /* False alarm, nothing to do */
2425 return;
2426 sh->reconstruct_state = reconstruct_state_drain_run;
2427 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2428 } else
2429 sh->reconstruct_state = reconstruct_state_run;
2430
2431 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2432
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002433 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002434 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002435 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002436 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002437 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002438 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2439 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2440
Dan Williamse33129d2007-01-02 13:52:30 -07002441 for (i = disks; i--; ) {
2442 struct r5dev *dev = &sh->dev[i];
2443 if (i == pd_idx)
2444 continue;
2445
Dan Williamse33129d2007-01-02 13:52:30 -07002446 if (dev->towrite &&
2447 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002448 test_bit(R5_Wantcompute, &dev->flags))) {
2449 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002450 set_bit(R5_LOCKED, &dev->flags);
2451 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002452 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002453 }
2454 }
NeilBrownce7d3632013-03-04 12:37:14 +11002455 if (!s->locked)
2456 /* False alarm - nothing to do */
2457 return;
2458 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2459 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2460 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2461 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002462 }
2463
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002464 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002465 * are in flight
2466 */
2467 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2468 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002469 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002470
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002471 if (level == 6) {
2472 int qd_idx = sh->qd_idx;
2473 struct r5dev *dev = &sh->dev[qd_idx];
2474
2475 set_bit(R5_LOCKED, &dev->flags);
2476 clear_bit(R5_UPTODATE, &dev->flags);
2477 s->locked++;
2478 }
2479
Dan Williams600aa102008-06-28 08:32:05 +10002480 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002481 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002482 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002483}
NeilBrown16a53ec2006-06-26 00:27:38 -07002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485/*
2486 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002487 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 * The bi_next chain must be in order.
2489 */
2490static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2491{
2492 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002493 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002494 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
NeilBrowncbe47ec2011-07-26 11:20:35 +10002496 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 (unsigned long long)bi->bi_sector,
2498 (unsigned long long)sh->sector);
2499
Shaohua Lib17459c2012-07-19 16:01:31 +10002500 /*
2501 * If several bio share a stripe. The bio bi_phys_segments acts as a
2502 * reference count to avoid race. The reference count should already be
2503 * increased before this function is called (for example, in
2504 * make_request()), so other bio sharing this stripe will not free the
2505 * stripe. If a stripe is owned by one stripe, the stripe lock will
2506 * protect it.
2507 */
2508 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002509 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002511 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002512 firstwrite = 1;
2513 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 bip = &sh->dev[dd_idx].toread;
2515 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002516 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 goto overlap;
2518 bip = & (*bip)->bi_next;
2519 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002520 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 goto overlap;
2522
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002523 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (*bip)
2525 bi->bi_next = *bip;
2526 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002527 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (forwrite) {
2530 /* check if page is covered */
2531 sector_t sector = sh->dev[dd_idx].sector;
2532 for (bi=sh->dev[dd_idx].towrite;
2533 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2534 bi && bi->bi_sector <= sector;
2535 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002536 if (bio_end_sector(bi) >= sector)
2537 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2540 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2541 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002542
2543 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2544 (unsigned long long)(*bip)->bi_sector,
2545 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002546 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002547
2548 if (conf->mddev->bitmap && firstwrite) {
2549 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2550 STRIPE_SECTORS, 0);
2551 sh->bm_seq = conf->seq_flush+1;
2552 set_bit(STRIPE_BIT_DELAY, &sh->state);
2553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return 1;
2555
2556 overlap:
2557 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002558 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return 0;
2560}
2561
NeilBrownd1688a62011-10-11 16:49:52 +11002562static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002563
NeilBrownd1688a62011-10-11 16:49:52 +11002564static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002565 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002566{
NeilBrown784052e2009-03-31 15:19:07 +11002567 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002568 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002569 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002570 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002571 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002572
NeilBrown112bf892009-03-31 14:39:38 +11002573 raid5_compute_sector(conf,
2574 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002575 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002576 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002577 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002578}
2579
Dan Williamsa4456852007-07-09 11:56:43 -07002580static void
NeilBrownd1688a62011-10-11 16:49:52 +11002581handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002582 struct stripe_head_state *s, int disks,
2583 struct bio **return_bi)
2584{
2585 int i;
2586 for (i = disks; i--; ) {
2587 struct bio *bi;
2588 int bitmap_end = 0;
2589
2590 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002591 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002592 rcu_read_lock();
2593 rdev = rcu_dereference(conf->disks[i].rdev);
2594 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002595 atomic_inc(&rdev->nr_pending);
2596 else
2597 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002598 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002599 if (rdev) {
2600 if (!rdev_set_badblocks(
2601 rdev,
2602 sh->sector,
2603 STRIPE_SECTORS, 0))
2604 md_error(conf->mddev, rdev);
2605 rdev_dec_pending(rdev, conf->mddev);
2606 }
Dan Williamsa4456852007-07-09 11:56:43 -07002607 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002608 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002609 /* fail all writes first */
2610 bi = sh->dev[i].towrite;
2611 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002612 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002613 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002614 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002615
2616 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2617 wake_up(&conf->wait_for_overlap);
2618
2619 while (bi && bi->bi_sector <
2620 sh->dev[i].sector + STRIPE_SECTORS) {
2621 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2622 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002623 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002624 md_write_end(conf->mddev);
2625 bi->bi_next = *return_bi;
2626 *return_bi = bi;
2627 }
2628 bi = nextbi;
2629 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002630 if (bitmap_end)
2631 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2632 STRIPE_SECTORS, 0, 0);
2633 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002634 /* and fail all 'written' */
2635 bi = sh->dev[i].written;
2636 sh->dev[i].written = NULL;
2637 if (bi) bitmap_end = 1;
2638 while (bi && bi->bi_sector <
2639 sh->dev[i].sector + STRIPE_SECTORS) {
2640 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2641 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002642 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002643 md_write_end(conf->mddev);
2644 bi->bi_next = *return_bi;
2645 *return_bi = bi;
2646 }
2647 bi = bi2;
2648 }
2649
Dan Williamsb5e98d62007-01-02 13:52:31 -07002650 /* fail any reads if this device is non-operational and
2651 * the data has not reached the cache yet.
2652 */
2653 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2654 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2655 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002656 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002657 bi = sh->dev[i].toread;
2658 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002659 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002660 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2661 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002662 while (bi && bi->bi_sector <
2663 sh->dev[i].sector + STRIPE_SECTORS) {
2664 struct bio *nextbi =
2665 r5_next_bio(bi, sh->dev[i].sector);
2666 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002667 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002668 bi->bi_next = *return_bi;
2669 *return_bi = bi;
2670 }
2671 bi = nextbi;
2672 }
2673 }
Dan Williamsa4456852007-07-09 11:56:43 -07002674 if (bitmap_end)
2675 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2676 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002677 /* If we were in the middle of a write the parity block might
2678 * still be locked - so just clear all R5_LOCKED flags
2679 */
2680 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002681 }
2682
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002683 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2684 if (atomic_dec_and_test(&conf->pending_full_writes))
2685 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002686}
2687
NeilBrown7f0da592011-07-28 11:39:22 +10002688static void
NeilBrownd1688a62011-10-11 16:49:52 +11002689handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002690 struct stripe_head_state *s)
2691{
2692 int abort = 0;
2693 int i;
2694
NeilBrown7f0da592011-07-28 11:39:22 +10002695 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002696 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2697 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002698 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002699 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002700 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002701 * Don't even need to abort as that is handled elsewhere
2702 * if needed, and not always wanted e.g. if there is a known
2703 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002704 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002705 * non-sync devices, or abort the recovery
2706 */
NeilBrown18b98372012-04-01 23:48:38 +10002707 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2708 /* During recovery devices cannot be removed, so
2709 * locking and refcounting of rdevs is not needed
2710 */
2711 for (i = 0; i < conf->raid_disks; i++) {
2712 struct md_rdev *rdev = conf->disks[i].rdev;
2713 if (rdev
2714 && !test_bit(Faulty, &rdev->flags)
2715 && !test_bit(In_sync, &rdev->flags)
2716 && !rdev_set_badblocks(rdev, sh->sector,
2717 STRIPE_SECTORS, 0))
2718 abort = 1;
2719 rdev = conf->disks[i].replacement;
2720 if (rdev
2721 && !test_bit(Faulty, &rdev->flags)
2722 && !test_bit(In_sync, &rdev->flags)
2723 && !rdev_set_badblocks(rdev, sh->sector,
2724 STRIPE_SECTORS, 0))
2725 abort = 1;
2726 }
2727 if (abort)
2728 conf->recovery_disabled =
2729 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002730 }
NeilBrown18b98372012-04-01 23:48:38 +10002731 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002732}
2733
NeilBrown9a3e1102011-12-23 10:17:53 +11002734static int want_replace(struct stripe_head *sh, int disk_idx)
2735{
2736 struct md_rdev *rdev;
2737 int rv = 0;
2738 /* Doing recovery so rcu locking not required */
2739 rdev = sh->raid_conf->disks[disk_idx].replacement;
2740 if (rdev
2741 && !test_bit(Faulty, &rdev->flags)
2742 && !test_bit(In_sync, &rdev->flags)
2743 && (rdev->recovery_offset <= sh->sector
2744 || rdev->mddev->recovery_cp <= sh->sector))
2745 rv = 1;
2746
2747 return rv;
2748}
2749
NeilBrown93b3dbc2011-07-27 11:00:36 +10002750/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002751 * to be read or computed to satisfy a request.
2752 *
2753 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002754 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002755 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002756static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2757 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002758{
2759 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002760 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2761 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002762
Dan Williamsf38e1212007-01-02 13:52:30 -07002763 /* is the data in this block needed, and can we get it? */
2764 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002765 !test_bit(R5_UPTODATE, &dev->flags) &&
2766 (dev->toread ||
2767 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2768 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002769 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002770 (s->failed >= 1 && fdev[0]->toread) ||
2771 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002772 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2773 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2774 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002775 /* we would like to get this block, possibly by computing it,
2776 * otherwise read it if the backing disk is insync
2777 */
2778 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2779 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2780 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002781 (s->failed && (disk_idx == s->failed_num[0] ||
2782 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002783 /* have disk failed, and we're requested to fetch it;
2784 * do compute it
2785 */
2786 pr_debug("Computing stripe %llu block %d\n",
2787 (unsigned long long)sh->sector, disk_idx);
2788 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2789 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2790 set_bit(R5_Wantcompute, &dev->flags);
2791 sh->ops.target = disk_idx;
2792 sh->ops.target2 = -1; /* no 2nd target */
2793 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002794 /* Careful: from this point on 'uptodate' is in the eye
2795 * of raid_run_ops which services 'compute' operations
2796 * before writes. R5_Wantcompute flags a block that will
2797 * be R5_UPTODATE by the time it is needed for a
2798 * subsequent operation.
2799 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002800 s->uptodate++;
2801 return 1;
2802 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2803 /* Computing 2-failure is *very* expensive; only
2804 * do it if failed >= 2
2805 */
2806 int other;
2807 for (other = disks; other--; ) {
2808 if (other == disk_idx)
2809 continue;
2810 if (!test_bit(R5_UPTODATE,
2811 &sh->dev[other].flags))
2812 break;
2813 }
2814 BUG_ON(other < 0);
2815 pr_debug("Computing stripe %llu blocks %d,%d\n",
2816 (unsigned long long)sh->sector,
2817 disk_idx, other);
2818 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2819 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2820 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2821 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2822 sh->ops.target = disk_idx;
2823 sh->ops.target2 = other;
2824 s->uptodate += 2;
2825 s->req_compute = 1;
2826 return 1;
2827 } else if (test_bit(R5_Insync, &dev->flags)) {
2828 set_bit(R5_LOCKED, &dev->flags);
2829 set_bit(R5_Wantread, &dev->flags);
2830 s->locked++;
2831 pr_debug("Reading block %d (sync=%d)\n",
2832 disk_idx, s->syncing);
2833 }
2834 }
2835
2836 return 0;
2837}
2838
2839/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002840 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002841 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002842static void handle_stripe_fill(struct stripe_head *sh,
2843 struct stripe_head_state *s,
2844 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002845{
2846 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002847
2848 /* look for blocks to read/compute, skip this if a compute
2849 * is already in flight, or if the stripe contents are in the
2850 * midst of changing due to a write
2851 */
2852 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2853 !sh->reconstruct_state)
2854 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002855 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002856 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002857 set_bit(STRIPE_HANDLE, &sh->state);
2858}
2859
2860
Dan Williams1fe797e2008-06-28 09:16:30 +10002861/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002862 * any written block on an uptodate or failed drive can be returned.
2863 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2864 * never LOCKED, so we don't need to test 'failed' directly.
2865 */
NeilBrownd1688a62011-10-11 16:49:52 +11002866static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002867 struct stripe_head *sh, int disks, struct bio **return_bi)
2868{
2869 int i;
2870 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002871 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002872
2873 for (i = disks; i--; )
2874 if (sh->dev[i].written) {
2875 dev = &sh->dev[i];
2876 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002877 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002878 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002879 /* We can return any write requests */
2880 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002881 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002882 if (test_and_clear_bit(R5_Discard, &dev->flags))
2883 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002884 wbi = dev->written;
2885 dev->written = NULL;
2886 while (wbi && wbi->bi_sector <
2887 dev->sector + STRIPE_SECTORS) {
2888 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002889 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002890 md_write_end(conf->mddev);
2891 wbi->bi_next = *return_bi;
2892 *return_bi = wbi;
2893 }
2894 wbi = wbi2;
2895 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002896 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2897 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002898 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002899 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002900 } else if (test_bit(R5_Discard, &dev->flags))
2901 discard_pending = 1;
2902 }
2903 if (!discard_pending &&
2904 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2905 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2906 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2907 if (sh->qd_idx >= 0) {
2908 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2909 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2910 }
2911 /* now that discard is done we can proceed with any sync */
2912 clear_bit(STRIPE_DISCARD, &sh->state);
2913 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2914 set_bit(STRIPE_HANDLE, &sh->state);
2915
2916 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002917
2918 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2919 if (atomic_dec_and_test(&conf->pending_full_writes))
2920 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002921}
2922
NeilBrownd1688a62011-10-11 16:49:52 +11002923static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002924 struct stripe_head *sh,
2925 struct stripe_head_state *s,
2926 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002927{
2928 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002929 sector_t recovery_cp = conf->mddev->recovery_cp;
2930
2931 /* RAID6 requires 'rcw' in current implementation.
2932 * Otherwise, check whether resync is now happening or should start.
2933 * If yes, then the array is dirty (after unclean shutdown or
2934 * initial creation), so parity in some stripes might be inconsistent.
2935 * In this case, we need to always do reconstruct-write, to ensure
2936 * that in case of drive failure or read-error correction, we
2937 * generate correct data from the parity.
2938 */
2939 if (conf->max_degraded == 2 ||
2940 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2941 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002942 * look like rcw is cheaper
2943 */
2944 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002945 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2946 conf->max_degraded, (unsigned long long)recovery_cp,
2947 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002948 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002949 /* would I have to read this buffer for read_modify_write */
2950 struct r5dev *dev = &sh->dev[i];
2951 if ((dev->towrite || i == sh->pd_idx) &&
2952 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002953 !(test_bit(R5_UPTODATE, &dev->flags) ||
2954 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002955 if (test_bit(R5_Insync, &dev->flags))
2956 rmw++;
2957 else
2958 rmw += 2*disks; /* cannot read it */
2959 }
2960 /* Would I have to read this buffer for reconstruct_write */
2961 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2962 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002963 !(test_bit(R5_UPTODATE, &dev->flags) ||
2964 test_bit(R5_Wantcompute, &dev->flags))) {
2965 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002966 else
2967 rcw += 2*disks;
2968 }
2969 }
Dan Williams45b42332007-07-09 11:56:43 -07002970 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002971 (unsigned long long)sh->sector, rmw, rcw);
2972 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002973 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002974 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002975 if (conf->mddev->queue)
2976 blk_add_trace_msg(conf->mddev->queue,
2977 "raid5 rmw %llu %d",
2978 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002979 for (i = disks; i--; ) {
2980 struct r5dev *dev = &sh->dev[i];
2981 if ((dev->towrite || i == sh->pd_idx) &&
2982 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002983 !(test_bit(R5_UPTODATE, &dev->flags) ||
2984 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002985 test_bit(R5_Insync, &dev->flags)) {
2986 if (
2987 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002988 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002989 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002990 set_bit(R5_LOCKED, &dev->flags);
2991 set_bit(R5_Wantread, &dev->flags);
2992 s->locked++;
2993 } else {
2994 set_bit(STRIPE_DELAYED, &sh->state);
2995 set_bit(STRIPE_HANDLE, &sh->state);
2996 }
2997 }
2998 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002999 }
NeilBrownc8ac1802011-07-27 11:00:36 +10003000 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003001 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003002 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003003 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003004 for (i = disks; i--; ) {
3005 struct r5dev *dev = &sh->dev[i];
3006 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003007 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003008 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003009 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003010 test_bit(R5_Wantcompute, &dev->flags))) {
3011 rcw++;
3012 if (!test_bit(R5_Insync, &dev->flags))
3013 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07003014 if (
3015 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003016 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003017 "%d for Reconstruct\n", i);
3018 set_bit(R5_LOCKED, &dev->flags);
3019 set_bit(R5_Wantread, &dev->flags);
3020 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003021 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003022 } else {
3023 set_bit(STRIPE_DELAYED, &sh->state);
3024 set_bit(STRIPE_HANDLE, &sh->state);
3025 }
3026 }
3027 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003028 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003029 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3030 (unsigned long long)sh->sector,
3031 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003032 }
Dan Williamsa4456852007-07-09 11:56:43 -07003033 /* now if nothing is locked, and if we have enough data,
3034 * we can start a write request
3035 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003036 /* since handle_stripe can be called at any time we need to handle the
3037 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003038 * subsequent call wants to start a write request. raid_run_ops only
3039 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003040 * simultaneously. If this is not the case then new writes need to be
3041 * held off until the compute completes.
3042 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003043 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3044 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3045 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003046 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003047}
3048
NeilBrownd1688a62011-10-11 16:49:52 +11003049static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003050 struct stripe_head_state *s, int disks)
3051{
Dan Williamsecc65c92008-06-28 08:31:57 +10003052 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003053
Dan Williamsbd2ab672008-04-10 21:29:27 -07003054 set_bit(STRIPE_HANDLE, &sh->state);
3055
Dan Williamsecc65c92008-06-28 08:31:57 +10003056 switch (sh->check_state) {
3057 case check_state_idle:
3058 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003059 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003060 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003061 sh->check_state = check_state_run;
3062 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003063 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003064 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003065 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003066 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003067 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003068 /* fall through */
3069 case check_state_compute_result:
3070 sh->check_state = check_state_idle;
3071 if (!dev)
3072 dev = &sh->dev[sh->pd_idx];
3073
3074 /* check that a write has not made the stripe insync */
3075 if (test_bit(STRIPE_INSYNC, &sh->state))
3076 break;
3077
3078 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003079 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3080 BUG_ON(s->uptodate != disks);
3081
3082 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003083 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003084 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003085
Dan Williamsa4456852007-07-09 11:56:43 -07003086 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003087 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003088 break;
3089 case check_state_run:
3090 break; /* we will be called again upon completion */
3091 case check_state_check_result:
3092 sh->check_state = check_state_idle;
3093
3094 /* if a failure occurred during the check operation, leave
3095 * STRIPE_INSYNC not set and let the stripe be handled again
3096 */
3097 if (s->failed)
3098 break;
3099
3100 /* handle a successful check operation, if parity is correct
3101 * we are done. Otherwise update the mismatch count and repair
3102 * parity if !MD_RECOVERY_CHECK
3103 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003104 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003105 /* parity is correct (on disc,
3106 * not in buffer any more)
3107 */
3108 set_bit(STRIPE_INSYNC, &sh->state);
3109 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003110 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003111 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3112 /* don't try to repair!! */
3113 set_bit(STRIPE_INSYNC, &sh->state);
3114 else {
3115 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003116 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003117 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3118 set_bit(R5_Wantcompute,
3119 &sh->dev[sh->pd_idx].flags);
3120 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003121 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003122 s->uptodate++;
3123 }
3124 }
3125 break;
3126 case check_state_compute_run:
3127 break;
3128 default:
3129 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3130 __func__, sh->check_state,
3131 (unsigned long long) sh->sector);
3132 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003133 }
3134}
3135
3136
NeilBrownd1688a62011-10-11 16:49:52 +11003137static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003138 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003139 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003140{
Dan Williamsa4456852007-07-09 11:56:43 -07003141 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003142 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003143 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003144
3145 set_bit(STRIPE_HANDLE, &sh->state);
3146
3147 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003148
Dan Williamsa4456852007-07-09 11:56:43 -07003149 /* Want to check and possibly repair P and Q.
3150 * However there could be one 'failed' device, in which
3151 * case we can only check one of them, possibly using the
3152 * other to generate missing data
3153 */
3154
Dan Williamsd82dfee2009-07-14 13:40:57 -07003155 switch (sh->check_state) {
3156 case check_state_idle:
3157 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003158 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003159 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003160 * makes sense to check P (If anything else were failed,
3161 * we would have used P to recreate it).
3162 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003163 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003164 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003165 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003166 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003167 * anything, so it makes sense to check it
3168 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003169 if (sh->check_state == check_state_run)
3170 sh->check_state = check_state_run_pq;
3171 else
3172 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003173 }
Dan Williams36d1c642009-07-14 11:48:22 -07003174
Dan Williamsd82dfee2009-07-14 13:40:57 -07003175 /* discard potentially stale zero_sum_result */
3176 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003177
Dan Williamsd82dfee2009-07-14 13:40:57 -07003178 if (sh->check_state == check_state_run) {
3179 /* async_xor_zero_sum destroys the contents of P */
3180 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3181 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003182 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003183 if (sh->check_state >= check_state_run &&
3184 sh->check_state <= check_state_run_pq) {
3185 /* async_syndrome_zero_sum preserves P and Q, so
3186 * no need to mark them !uptodate here
3187 */
3188 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3189 break;
3190 }
Dan Williams36d1c642009-07-14 11:48:22 -07003191
Dan Williamsd82dfee2009-07-14 13:40:57 -07003192 /* we have 2-disk failure */
3193 BUG_ON(s->failed != 2);
3194 /* fall through */
3195 case check_state_compute_result:
3196 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003197
Dan Williamsd82dfee2009-07-14 13:40:57 -07003198 /* check that a write has not made the stripe insync */
3199 if (test_bit(STRIPE_INSYNC, &sh->state))
3200 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003201
3202 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003203 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003204 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003205 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003206 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003207 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003208 s->locked++;
3209 set_bit(R5_LOCKED, &dev->flags);
3210 set_bit(R5_Wantwrite, &dev->flags);
3211 }
3212 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003213 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003214 s->locked++;
3215 set_bit(R5_LOCKED, &dev->flags);
3216 set_bit(R5_Wantwrite, &dev->flags);
3217 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003218 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003219 dev = &sh->dev[pd_idx];
3220 s->locked++;
3221 set_bit(R5_LOCKED, &dev->flags);
3222 set_bit(R5_Wantwrite, &dev->flags);
3223 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003224 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003225 dev = &sh->dev[qd_idx];
3226 s->locked++;
3227 set_bit(R5_LOCKED, &dev->flags);
3228 set_bit(R5_Wantwrite, &dev->flags);
3229 }
3230 clear_bit(STRIPE_DEGRADED, &sh->state);
3231
3232 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003233 break;
3234 case check_state_run:
3235 case check_state_run_q:
3236 case check_state_run_pq:
3237 break; /* we will be called again upon completion */
3238 case check_state_check_result:
3239 sh->check_state = check_state_idle;
3240
3241 /* handle a successful check operation, if parity is correct
3242 * we are done. Otherwise update the mismatch count and repair
3243 * parity if !MD_RECOVERY_CHECK
3244 */
3245 if (sh->ops.zero_sum_result == 0) {
3246 /* both parities are correct */
3247 if (!s->failed)
3248 set_bit(STRIPE_INSYNC, &sh->state);
3249 else {
3250 /* in contrast to the raid5 case we can validate
3251 * parity, but still have a failure to write
3252 * back
3253 */
3254 sh->check_state = check_state_compute_result;
3255 /* Returning at this point means that we may go
3256 * off and bring p and/or q uptodate again so
3257 * we make sure to check zero_sum_result again
3258 * to verify if p or q need writeback
3259 */
3260 }
3261 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003262 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003263 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3264 /* don't try to repair!! */
3265 set_bit(STRIPE_INSYNC, &sh->state);
3266 else {
3267 int *target = &sh->ops.target;
3268
3269 sh->ops.target = -1;
3270 sh->ops.target2 = -1;
3271 sh->check_state = check_state_compute_run;
3272 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3273 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3274 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3275 set_bit(R5_Wantcompute,
3276 &sh->dev[pd_idx].flags);
3277 *target = pd_idx;
3278 target = &sh->ops.target2;
3279 s->uptodate++;
3280 }
3281 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3282 set_bit(R5_Wantcompute,
3283 &sh->dev[qd_idx].flags);
3284 *target = qd_idx;
3285 s->uptodate++;
3286 }
3287 }
3288 }
3289 break;
3290 case check_state_compute_run:
3291 break;
3292 default:
3293 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3294 __func__, sh->check_state,
3295 (unsigned long long) sh->sector);
3296 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003297 }
3298}
3299
NeilBrownd1688a62011-10-11 16:49:52 +11003300static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003301{
3302 int i;
3303
3304 /* We have read all the blocks in this stripe and now we need to
3305 * copy some of them into a target stripe for expand.
3306 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003307 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003308 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3309 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003310 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003311 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003312 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003313 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003314
NeilBrown784052e2009-03-31 15:19:07 +11003315 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003316 sector_t s = raid5_compute_sector(conf, bn, 0,
3317 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003318 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003319 if (sh2 == NULL)
3320 /* so far only the early blocks of this stripe
3321 * have been requested. When later blocks
3322 * get requested, we will try again
3323 */
3324 continue;
3325 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3326 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3327 /* must have already done this block */
3328 release_stripe(sh2);
3329 continue;
3330 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003331
3332 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003333 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003334 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003335 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003336 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003337
Dan Williamsa4456852007-07-09 11:56:43 -07003338 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3339 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3340 for (j = 0; j < conf->raid_disks; j++)
3341 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003342 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003343 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3344 break;
3345 if (j == conf->raid_disks) {
3346 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3347 set_bit(STRIPE_HANDLE, &sh2->state);
3348 }
3349 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003350
Dan Williamsa4456852007-07-09 11:56:43 -07003351 }
NeilBrowna2e08552007-09-11 15:23:36 -07003352 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003353 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356/*
3357 * handle_stripe - do things to a stripe.
3358 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003359 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3360 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003362 * return some read requests which now have data
3363 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 * schedule a read on some buffers
3365 * schedule a write of some buffers
3366 * return confirmation of parity correctness
3367 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 */
Dan Williamsa4456852007-07-09 11:56:43 -07003369
NeilBrownacfe7262011-07-27 11:00:36 +10003370static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003371{
NeilBrownd1688a62011-10-11 16:49:52 +11003372 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003373 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003374 struct r5dev *dev;
3375 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003376 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003377
NeilBrownacfe7262011-07-27 11:00:36 +10003378 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003379
NeilBrownacfe7262011-07-27 11:00:36 +10003380 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3381 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3382 s->failed_num[0] = -1;
3383 s->failed_num[1] = -1;
3384
3385 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003386 rcu_read_lock();
3387 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003388 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003389 sector_t first_bad;
3390 int bad_sectors;
3391 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003392
NeilBrown16a53ec2006-06-26 00:27:38 -07003393 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003394
Dan Williams45b42332007-07-09 11:56:43 -07003395 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003396 i, dev->flags,
3397 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003398 /* maybe we can reply to a read
3399 *
3400 * new wantfill requests are only permitted while
3401 * ops_complete_biofill is guaranteed to be inactive
3402 */
3403 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3404 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3405 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003406
3407 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003408 if (test_bit(R5_LOCKED, &dev->flags))
3409 s->locked++;
3410 if (test_bit(R5_UPTODATE, &dev->flags))
3411 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003412 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003413 s->compute++;
3414 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003415 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003416
NeilBrownacfe7262011-07-27 11:00:36 +10003417 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003418 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003419 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003420 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003421 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003422 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003423 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003424 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003425 }
Dan Williamsa4456852007-07-09 11:56:43 -07003426 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003427 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003428 /* Prefer to use the replacement for reads, but only
3429 * if it is recovered enough and has no bad blocks.
3430 */
3431 rdev = rcu_dereference(conf->disks[i].replacement);
3432 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3433 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3434 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3435 &first_bad, &bad_sectors))
3436 set_bit(R5_ReadRepl, &dev->flags);
3437 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003438 if (rdev)
3439 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003440 rdev = rcu_dereference(conf->disks[i].rdev);
3441 clear_bit(R5_ReadRepl, &dev->flags);
3442 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003443 if (rdev && test_bit(Faulty, &rdev->flags))
3444 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003445 if (rdev) {
3446 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3447 &first_bad, &bad_sectors);
3448 if (s->blocked_rdev == NULL
3449 && (test_bit(Blocked, &rdev->flags)
3450 || is_bad < 0)) {
3451 if (is_bad < 0)
3452 set_bit(BlockedBadBlocks,
3453 &rdev->flags);
3454 s->blocked_rdev = rdev;
3455 atomic_inc(&rdev->nr_pending);
3456 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003457 }
NeilBrown415e72d2010-06-17 17:25:21 +10003458 clear_bit(R5_Insync, &dev->flags);
3459 if (!rdev)
3460 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003461 else if (is_bad) {
3462 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003463 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3464 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003465 /* treat as in-sync, but with a read error
3466 * which we can now try to correct
3467 */
3468 set_bit(R5_Insync, &dev->flags);
3469 set_bit(R5_ReadError, &dev->flags);
3470 }
3471 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003472 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003473 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003474 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003475 set_bit(R5_Insync, &dev->flags);
3476 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3477 test_bit(R5_Expanded, &dev->flags))
3478 /* If we've reshaped into here, we assume it is Insync.
3479 * We will shortly update recovery_offset to make
3480 * it official.
3481 */
3482 set_bit(R5_Insync, &dev->flags);
3483
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003484 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003485 /* This flag does not apply to '.replacement'
3486 * only to .rdev, so make sure to check that*/
3487 struct md_rdev *rdev2 = rcu_dereference(
3488 conf->disks[i].rdev);
3489 if (rdev2 == rdev)
3490 clear_bit(R5_Insync, &dev->flags);
3491 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003492 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003493 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003494 } else
3495 clear_bit(R5_WriteError, &dev->flags);
3496 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003497 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003498 /* This flag does not apply to '.replacement'
3499 * only to .rdev, so make sure to check that*/
3500 struct md_rdev *rdev2 = rcu_dereference(
3501 conf->disks[i].rdev);
3502 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003503 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003504 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003505 } else
3506 clear_bit(R5_MadeGood, &dev->flags);
3507 }
NeilBrown977df362011-12-23 10:17:53 +11003508 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3509 struct md_rdev *rdev2 = rcu_dereference(
3510 conf->disks[i].replacement);
3511 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3512 s->handle_bad_blocks = 1;
3513 atomic_inc(&rdev2->nr_pending);
3514 } else
3515 clear_bit(R5_MadeGoodRepl, &dev->flags);
3516 }
NeilBrown415e72d2010-06-17 17:25:21 +10003517 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003518 /* The ReadError flag will just be confusing now */
3519 clear_bit(R5_ReadError, &dev->flags);
3520 clear_bit(R5_ReWrite, &dev->flags);
3521 }
NeilBrown415e72d2010-06-17 17:25:21 +10003522 if (test_bit(R5_ReadError, &dev->flags))
3523 clear_bit(R5_Insync, &dev->flags);
3524 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003525 if (s->failed < 2)
3526 s->failed_num[s->failed] = i;
3527 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003528 if (rdev && !test_bit(Faulty, &rdev->flags))
3529 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003530 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003531 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003532 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3533 /* If there is a failed device being replaced,
3534 * we must be recovering.
3535 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003536 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003537 * else we can only be replacing
3538 * sync and recovery both need to read all devices, and so
3539 * use the same flag.
3540 */
3541 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003542 sh->sector >= conf->mddev->recovery_cp ||
3543 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003544 s->syncing = 1;
3545 else
3546 s->replacing = 1;
3547 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003548 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003549}
NeilBrownf4168852007-02-28 20:11:53 -08003550
NeilBrowncc940152011-07-26 11:35:35 +10003551static void handle_stripe(struct stripe_head *sh)
3552{
3553 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003554 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003555 int i;
NeilBrown84789552011-07-27 11:00:36 +10003556 int prexor;
3557 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003558 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003559
3560 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003561 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003562 /* already being handled, ensure it gets handled
3563 * again when current action finishes */
3564 set_bit(STRIPE_HANDLE, &sh->state);
3565 return;
3566 }
3567
NeilBrownf8dfcff2013-03-12 12:18:06 +11003568 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3569 spin_lock(&sh->stripe_lock);
3570 /* Cannot process 'sync' concurrently with 'discard' */
3571 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3572 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3573 set_bit(STRIPE_SYNCING, &sh->state);
3574 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003575 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003576 }
3577 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003578 }
3579 clear_bit(STRIPE_DELAYED, &sh->state);
3580
3581 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3582 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3583 (unsigned long long)sh->sector, sh->state,
3584 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3585 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003586
NeilBrownacfe7262011-07-27 11:00:36 +10003587 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003588
NeilBrownbc2607f2011-07-28 11:39:22 +10003589 if (s.handle_bad_blocks) {
3590 set_bit(STRIPE_HANDLE, &sh->state);
3591 goto finish;
3592 }
3593
NeilBrown474af965fe2011-07-27 11:00:36 +10003594 if (unlikely(s.blocked_rdev)) {
3595 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003596 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003597 set_bit(STRIPE_HANDLE, &sh->state);
3598 goto finish;
3599 }
3600 /* There is nothing for the blocked_rdev to block */
3601 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3602 s.blocked_rdev = NULL;
3603 }
3604
3605 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3606 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3607 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3608 }
3609
3610 pr_debug("locked=%d uptodate=%d to_read=%d"
3611 " to_write=%d failed=%d failed_num=%d,%d\n",
3612 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3613 s.failed_num[0], s.failed_num[1]);
3614 /* check if the array has lost more than max_degraded devices and,
3615 * if so, some requests might need to be failed.
3616 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003617 if (s.failed > conf->max_degraded) {
3618 sh->check_state = 0;
3619 sh->reconstruct_state = 0;
3620 if (s.to_read+s.to_write+s.written)
3621 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003622 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003623 handle_failed_sync(conf, sh, &s);
3624 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003625
NeilBrown84789552011-07-27 11:00:36 +10003626 /* Now we check to see if any write operations have recently
3627 * completed
3628 */
3629 prexor = 0;
3630 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3631 prexor = 1;
3632 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3633 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3634 sh->reconstruct_state = reconstruct_state_idle;
3635
3636 /* All the 'written' buffers and the parity block are ready to
3637 * be written back to disk
3638 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003639 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3640 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003641 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003642 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3643 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003644 for (i = disks; i--; ) {
3645 struct r5dev *dev = &sh->dev[i];
3646 if (test_bit(R5_LOCKED, &dev->flags) &&
3647 (i == sh->pd_idx || i == sh->qd_idx ||
3648 dev->written)) {
3649 pr_debug("Writing block %d\n", i);
3650 set_bit(R5_Wantwrite, &dev->flags);
3651 if (prexor)
3652 continue;
3653 if (!test_bit(R5_Insync, &dev->flags) ||
3654 ((i == sh->pd_idx || i == sh->qd_idx) &&
3655 s.failed == 0))
3656 set_bit(STRIPE_INSYNC, &sh->state);
3657 }
3658 }
3659 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3660 s.dec_preread_active = 1;
3661 }
3662
NeilBrownef5b7c62012-11-22 09:13:36 +11003663 /*
3664 * might be able to return some write requests if the parity blocks
3665 * are safe, or on a failed drive
3666 */
3667 pdev = &sh->dev[sh->pd_idx];
3668 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3669 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3670 qdev = &sh->dev[sh->qd_idx];
3671 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3672 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3673 || conf->level < 6;
3674
3675 if (s.written &&
3676 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3677 && !test_bit(R5_LOCKED, &pdev->flags)
3678 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3679 test_bit(R5_Discard, &pdev->flags))))) &&
3680 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3681 && !test_bit(R5_LOCKED, &qdev->flags)
3682 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3683 test_bit(R5_Discard, &qdev->flags))))))
3684 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3685
3686 /* Now we might consider reading some blocks, either to check/generate
3687 * parity, or to satisfy requests
3688 * or to load a block that is being partially written.
3689 */
3690 if (s.to_read || s.non_overwrite
3691 || (conf->level == 6 && s.to_write && s.failed)
3692 || (s.syncing && (s.uptodate + s.compute < disks))
3693 || s.replacing
3694 || s.expanding)
3695 handle_stripe_fill(sh, &s, disks);
3696
NeilBrown84789552011-07-27 11:00:36 +10003697 /* Now to consider new write requests and what else, if anything
3698 * should be read. We do not handle new writes when:
3699 * 1/ A 'write' operation (copy+xor) is already in flight.
3700 * 2/ A 'check' operation is in flight, as it may clobber the parity
3701 * block.
3702 */
3703 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3704 handle_stripe_dirtying(conf, sh, &s, disks);
3705
3706 /* maybe we need to check and possibly fix the parity for this stripe
3707 * Any reads will already have been scheduled, so we just see if enough
3708 * data is available. The parity check is held off while parity
3709 * dependent operations are in flight.
3710 */
3711 if (sh->check_state ||
3712 (s.syncing && s.locked == 0 &&
3713 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3714 !test_bit(STRIPE_INSYNC, &sh->state))) {
3715 if (conf->level == 6)
3716 handle_parity_checks6(conf, sh, &s, disks);
3717 else
3718 handle_parity_checks5(conf, sh, &s, disks);
3719 }
NeilBrownc5a31002011-07-27 11:00:36 +10003720
NeilBrownf94c0b62013-07-22 12:57:21 +10003721 if ((s.replacing || s.syncing) && s.locked == 0
3722 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3723 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003724 /* Write out to replacement devices where possible */
3725 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003726 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3727 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003728 set_bit(R5_WantReplace, &sh->dev[i].flags);
3729 set_bit(R5_LOCKED, &sh->dev[i].flags);
3730 s.locked++;
3731 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003732 if (s.replacing)
3733 set_bit(STRIPE_INSYNC, &sh->state);
3734 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003735 }
3736 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003737 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003738 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003739 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3740 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003741 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3742 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003743 }
3744
3745 /* If the failed drives are just a ReadError, then we might need
3746 * to progress the repair/check process
3747 */
3748 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3749 for (i = 0; i < s.failed; i++) {
3750 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3751 if (test_bit(R5_ReadError, &dev->flags)
3752 && !test_bit(R5_LOCKED, &dev->flags)
3753 && test_bit(R5_UPTODATE, &dev->flags)
3754 ) {
3755 if (!test_bit(R5_ReWrite, &dev->flags)) {
3756 set_bit(R5_Wantwrite, &dev->flags);
3757 set_bit(R5_ReWrite, &dev->flags);
3758 set_bit(R5_LOCKED, &dev->flags);
3759 s.locked++;
3760 } else {
3761 /* let's read it back */
3762 set_bit(R5_Wantread, &dev->flags);
3763 set_bit(R5_LOCKED, &dev->flags);
3764 s.locked++;
3765 }
3766 }
3767 }
3768
3769
NeilBrown3687c062011-07-27 11:00:36 +10003770 /* Finish reconstruct operations initiated by the expansion process */
3771 if (sh->reconstruct_state == reconstruct_state_result) {
3772 struct stripe_head *sh_src
3773 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3774 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3775 /* sh cannot be written until sh_src has been read.
3776 * so arrange for sh to be delayed a little
3777 */
3778 set_bit(STRIPE_DELAYED, &sh->state);
3779 set_bit(STRIPE_HANDLE, &sh->state);
3780 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3781 &sh_src->state))
3782 atomic_inc(&conf->preread_active_stripes);
3783 release_stripe(sh_src);
3784 goto finish;
3785 }
3786 if (sh_src)
3787 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003788
NeilBrown3687c062011-07-27 11:00:36 +10003789 sh->reconstruct_state = reconstruct_state_idle;
3790 clear_bit(STRIPE_EXPANDING, &sh->state);
3791 for (i = conf->raid_disks; i--; ) {
3792 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3793 set_bit(R5_LOCKED, &sh->dev[i].flags);
3794 s.locked++;
3795 }
3796 }
3797
3798 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3799 !sh->reconstruct_state) {
3800 /* Need to write out all blocks after computing parity */
3801 sh->disks = conf->raid_disks;
3802 stripe_set_idx(sh->sector, conf, 0, sh);
3803 schedule_reconstruction(sh, &s, 1, 1);
3804 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3805 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3806 atomic_dec(&conf->reshape_stripes);
3807 wake_up(&conf->wait_for_overlap);
3808 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3809 }
3810
3811 if (s.expanding && s.locked == 0 &&
3812 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3813 handle_stripe_expansion(conf, sh);
3814
3815finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003816 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003817 if (unlikely(s.blocked_rdev)) {
3818 if (conf->mddev->external)
3819 md_wait_for_blocked_rdev(s.blocked_rdev,
3820 conf->mddev);
3821 else
3822 /* Internal metadata will immediately
3823 * be written by raid5d, so we don't
3824 * need to wait here.
3825 */
3826 rdev_dec_pending(s.blocked_rdev,
3827 conf->mddev);
3828 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003829
NeilBrownbc2607f2011-07-28 11:39:22 +10003830 if (s.handle_bad_blocks)
3831 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003832 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003833 struct r5dev *dev = &sh->dev[i];
3834 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3835 /* We own a safe reference to the rdev */
3836 rdev = conf->disks[i].rdev;
3837 if (!rdev_set_badblocks(rdev, sh->sector,
3838 STRIPE_SECTORS, 0))
3839 md_error(conf->mddev, rdev);
3840 rdev_dec_pending(rdev, conf->mddev);
3841 }
NeilBrownb84db562011-07-28 11:39:23 +10003842 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3843 rdev = conf->disks[i].rdev;
3844 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003845 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003846 rdev_dec_pending(rdev, conf->mddev);
3847 }
NeilBrown977df362011-12-23 10:17:53 +11003848 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3849 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003850 if (!rdev)
3851 /* rdev have been moved down */
3852 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003853 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003854 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003855 rdev_dec_pending(rdev, conf->mddev);
3856 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003857 }
3858
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003859 if (s.ops_request)
3860 raid_run_ops(sh, s.ops_request);
3861
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003862 ops_run_io(sh, &s);
3863
NeilBrownc5709ef2011-07-26 11:35:20 +10003864 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003865 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003866 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003867 * have actually been submitted.
3868 */
3869 atomic_dec(&conf->preread_active_stripes);
3870 if (atomic_read(&conf->preread_active_stripes) <
3871 IO_THRESHOLD)
3872 md_wakeup_thread(conf->mddev->thread);
3873 }
3874
NeilBrownc5709ef2011-07-26 11:35:20 +10003875 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003876
Dan Williams257a4b42011-11-08 16:22:06 +11003877 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003878}
3879
NeilBrownd1688a62011-10-11 16:49:52 +11003880static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
3882 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3883 while (!list_empty(&conf->delayed_list)) {
3884 struct list_head *l = conf->delayed_list.next;
3885 struct stripe_head *sh;
3886 sh = list_entry(l, struct stripe_head, lru);
3887 list_del_init(l);
3888 clear_bit(STRIPE_DELAYED, &sh->state);
3889 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3890 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003891 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08003892 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
NeilBrown482c0832011-04-18 18:25:42 +10003894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
3896
NeilBrownd1688a62011-10-11 16:49:52 +11003897static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003898{
3899 /* device_lock is held */
3900 struct list_head head;
3901 list_add(&head, &conf->bitmap_list);
3902 list_del_init(&conf->bitmap_list);
3903 while (!list_empty(&head)) {
3904 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3905 list_del_init(&sh->lru);
3906 atomic_inc(&sh->count);
3907 __release_stripe(conf, sh);
3908 }
3909}
3910
NeilBrownfd01b882011-10-11 16:47:53 +11003911int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003912{
NeilBrownd1688a62011-10-11 16:49:52 +11003913 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003914
3915 /* No difference between reads and writes. Just check
3916 * how busy the stripe_cache is
3917 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003918
NeilBrownf022b2f2006-10-03 01:15:56 -07003919 if (conf->inactive_blocked)
3920 return 1;
3921 if (conf->quiesce)
3922 return 1;
3923 if (list_empty_careful(&conf->inactive_list))
3924 return 1;
3925
3926 return 0;
3927}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003928EXPORT_SYMBOL_GPL(md_raid5_congested);
3929
3930static int raid5_congested(void *data, int bits)
3931{
NeilBrownfd01b882011-10-11 16:47:53 +11003932 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003933
3934 return mddev_congested(mddev, bits) ||
3935 md_raid5_congested(mddev, bits);
3936}
NeilBrownf022b2f2006-10-03 01:15:56 -07003937
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003938/* We want read requests to align with chunks where possible,
3939 * but write requests don't need to.
3940 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003941static int raid5_mergeable_bvec(struct request_queue *q,
3942 struct bvec_merge_data *bvm,
3943 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003944{
NeilBrownfd01b882011-10-11 16:47:53 +11003945 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003946 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003947 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003948 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003949 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003950
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003951 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003952 return biovec->bv_len; /* always allow writes to be mergeable */
3953
Andre Noll664e7c42009-06-18 08:45:27 +10003954 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3955 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003956 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3957 if (max < 0) max = 0;
3958 if (max <= biovec->bv_len && bio_sectors == 0)
3959 return biovec->bv_len;
3960 else
3961 return max;
3962}
3963
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003964
NeilBrownfd01b882011-10-11 16:47:53 +11003965static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003966{
3967 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003968 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003969 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003970
Andre Noll664e7c42009-06-18 08:45:27 +10003971 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3972 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003973 return chunk_sectors >=
3974 ((sector & (chunk_sectors - 1)) + bio_sectors);
3975}
3976
3977/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003978 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3979 * later sampled by raid5d.
3980 */
NeilBrownd1688a62011-10-11 16:49:52 +11003981static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003982{
3983 unsigned long flags;
3984
3985 spin_lock_irqsave(&conf->device_lock, flags);
3986
3987 bi->bi_next = conf->retry_read_aligned_list;
3988 conf->retry_read_aligned_list = bi;
3989
3990 spin_unlock_irqrestore(&conf->device_lock, flags);
3991 md_wakeup_thread(conf->mddev->thread);
3992}
3993
3994
NeilBrownd1688a62011-10-11 16:49:52 +11003995static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003996{
3997 struct bio *bi;
3998
3999 bi = conf->retry_read_aligned;
4000 if (bi) {
4001 conf->retry_read_aligned = NULL;
4002 return bi;
4003 }
4004 bi = conf->retry_read_aligned_list;
4005 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004006 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004007 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004008 /*
4009 * this sets the active strip count to 1 and the processed
4010 * strip count to zero (upper 8 bits)
4011 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004012 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004013 }
4014
4015 return bi;
4016}
4017
4018
4019/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004020 * The "raid5_align_endio" should check if the read succeeded and if it
4021 * did, call bio_endio on the original bio (having bio_put the new bio
4022 * first).
4023 * If the read failed..
4024 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004025static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004026{
4027 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004028 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004029 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004030 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004031 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004032
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004033 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004034
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004035 rdev = (void*)raid_bi->bi_next;
4036 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004037 mddev = rdev->mddev;
4038 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004039
4040 rdev_dec_pending(rdev, conf->mddev);
4041
4042 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004043 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4044 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004045 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004046 if (atomic_dec_and_test(&conf->active_aligned_reads))
4047 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004048 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004049 }
4050
4051
Dan Williams45b42332007-07-09 11:56:43 -07004052 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004053
4054 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004055}
4056
Neil Brown387bb172007-02-08 14:20:29 -08004057static int bio_fits_rdev(struct bio *bi)
4058{
Jens Axboe165125e2007-07-24 09:28:11 +02004059 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004060
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004061 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004062 return 0;
4063 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004064 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004065 return 0;
4066
4067 if (q->merge_bvec_fn)
4068 /* it's too hard to apply the merge_bvec_fn at this stage,
4069 * just just give up
4070 */
4071 return 0;
4072
4073 return 1;
4074}
4075
4076
NeilBrownfd01b882011-10-11 16:47:53 +11004077static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004078{
NeilBrownd1688a62011-10-11 16:49:52 +11004079 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004080 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004081 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004082 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004083 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004084
4085 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004086 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004087 return 0;
4088 }
4089 /*
NeilBrowna167f662010-10-26 18:31:13 +11004090 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004091 */
NeilBrowna167f662010-10-26 18:31:13 +11004092 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004093 if (!align_bi)
4094 return 0;
4095 /*
4096 * set bi_end_io to a new function, and set bi_private to the
4097 * original bio.
4098 */
4099 align_bi->bi_end_io = raid5_align_endio;
4100 align_bi->bi_private = raid_bio;
4101 /*
4102 * compute position
4103 */
NeilBrown112bf892009-03-31 14:39:38 +11004104 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4105 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004106 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004107
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004108 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004109 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004110 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4111 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4112 rdev->recovery_offset < end_sector) {
4113 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4114 if (rdev &&
4115 (test_bit(Faulty, &rdev->flags) ||
4116 !(test_bit(In_sync, &rdev->flags) ||
4117 rdev->recovery_offset >= end_sector)))
4118 rdev = NULL;
4119 }
4120 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004121 sector_t first_bad;
4122 int bad_sectors;
4123
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004124 atomic_inc(&rdev->nr_pending);
4125 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004126 raid_bio->bi_next = (void*)rdev;
4127 align_bi->bi_bdev = rdev->bdev;
4128 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004129
NeilBrown31c176e2011-07-28 11:39:22 +10004130 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004131 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004132 &first_bad, &bad_sectors)) {
4133 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004134 bio_put(align_bi);
4135 rdev_dec_pending(rdev, mddev);
4136 return 0;
4137 }
4138
majianpeng6c0544e2012-06-12 08:31:10 +08004139 /* No reshape active, so we can trust rdev->data_offset */
4140 align_bi->bi_sector += rdev->data_offset;
4141
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004142 spin_lock_irq(&conf->device_lock);
4143 wait_event_lock_irq(conf->wait_for_stripe,
4144 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004145 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004146 atomic_inc(&conf->active_aligned_reads);
4147 spin_unlock_irq(&conf->device_lock);
4148
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004149 if (mddev->gendisk)
4150 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4151 align_bi, disk_devt(mddev->gendisk),
4152 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004153 generic_make_request(align_bi);
4154 return 1;
4155 } else {
4156 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004157 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004158 return 0;
4159 }
4160}
4161
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004162/* __get_priority_stripe - get the next stripe to process
4163 *
4164 * Full stripe writes are allowed to pass preread active stripes up until
4165 * the bypass_threshold is exceeded. In general the bypass_count
4166 * increments when the handle_list is handled before the hold_list; however, it
4167 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4168 * stripe with in flight i/o. The bypass_count will be reset when the
4169 * head of the hold_list has changed, i.e. the head was promoted to the
4170 * handle_list.
4171 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004172static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004173{
Shaohua Li851c30c2013-08-28 14:30:16 +08004174 struct stripe_head *sh = NULL, *tmp;
4175 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004176 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004177
4178 if (conf->worker_cnt_per_group == 0) {
4179 handle_list = &conf->handle_list;
4180 } else if (group != ANY_GROUP) {
4181 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004182 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004183 } else {
4184 int i;
4185 for (i = 0; i < conf->group_cnt; i++) {
4186 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004187 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004188 if (!list_empty(handle_list))
4189 break;
4190 }
4191 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004192
4193 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4194 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004195 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004196 list_empty(&conf->hold_list) ? "empty" : "busy",
4197 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4198
Shaohua Li851c30c2013-08-28 14:30:16 +08004199 if (!list_empty(handle_list)) {
4200 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004201
4202 if (list_empty(&conf->hold_list))
4203 conf->bypass_count = 0;
4204 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4205 if (conf->hold_list.next == conf->last_hold)
4206 conf->bypass_count++;
4207 else {
4208 conf->last_hold = conf->hold_list.next;
4209 conf->bypass_count -= conf->bypass_threshold;
4210 if (conf->bypass_count < 0)
4211 conf->bypass_count = 0;
4212 }
4213 }
4214 } else if (!list_empty(&conf->hold_list) &&
4215 ((conf->bypass_threshold &&
4216 conf->bypass_count > conf->bypass_threshold) ||
4217 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004218
4219 list_for_each_entry(tmp, &conf->hold_list, lru) {
4220 if (conf->worker_cnt_per_group == 0 ||
4221 group == ANY_GROUP ||
4222 !cpu_online(tmp->cpu) ||
4223 cpu_to_group(tmp->cpu) == group) {
4224 sh = tmp;
4225 break;
4226 }
4227 }
4228
4229 if (sh) {
4230 conf->bypass_count -= conf->bypass_threshold;
4231 if (conf->bypass_count < 0)
4232 conf->bypass_count = 0;
4233 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004234 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004235 }
4236
4237 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004238 return NULL;
4239
Shaohua Libfc90cb2013-08-29 15:40:32 +08004240 if (wg) {
4241 wg->stripes_cnt--;
4242 sh->group = NULL;
4243 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004244 list_del_init(&sh->lru);
4245 atomic_inc(&sh->count);
4246 BUG_ON(atomic_read(&sh->count) != 1);
4247 return sh;
4248}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004249
Shaohua Li8811b592012-08-02 08:33:00 +10004250struct raid5_plug_cb {
4251 struct blk_plug_cb cb;
4252 struct list_head list;
4253};
4254
4255static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4256{
4257 struct raid5_plug_cb *cb = container_of(
4258 blk_cb, struct raid5_plug_cb, cb);
4259 struct stripe_head *sh;
4260 struct mddev *mddev = cb->cb.data;
4261 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004262 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004263
4264 if (cb->list.next && !list_empty(&cb->list)) {
4265 spin_lock_irq(&conf->device_lock);
4266 while (!list_empty(&cb->list)) {
4267 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4268 list_del_init(&sh->lru);
4269 /*
4270 * avoid race release_stripe_plug() sees
4271 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4272 * is still in our list
4273 */
4274 smp_mb__before_clear_bit();
4275 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004276 /*
4277 * STRIPE_ON_RELEASE_LIST could be set here. In that
4278 * case, the count is always > 1 here
4279 */
Shaohua Li8811b592012-08-02 08:33:00 +10004280 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004281 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004282 }
4283 spin_unlock_irq(&conf->device_lock);
4284 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004285 if (mddev->queue)
4286 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004287 kfree(cb);
4288}
4289
4290static void release_stripe_plug(struct mddev *mddev,
4291 struct stripe_head *sh)
4292{
4293 struct blk_plug_cb *blk_cb = blk_check_plugged(
4294 raid5_unplug, mddev,
4295 sizeof(struct raid5_plug_cb));
4296 struct raid5_plug_cb *cb;
4297
4298 if (!blk_cb) {
4299 release_stripe(sh);
4300 return;
4301 }
4302
4303 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4304
4305 if (cb->list.next == NULL)
4306 INIT_LIST_HEAD(&cb->list);
4307
4308 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4309 list_add_tail(&sh->lru, &cb->list);
4310 else
4311 release_stripe(sh);
4312}
4313
Shaohua Li620125f2012-10-11 13:49:05 +11004314static void make_discard_request(struct mddev *mddev, struct bio *bi)
4315{
4316 struct r5conf *conf = mddev->private;
4317 sector_t logical_sector, last_sector;
4318 struct stripe_head *sh;
4319 int remaining;
4320 int stripe_sectors;
4321
4322 if (mddev->reshape_position != MaxSector)
4323 /* Skip discard while reshape is happening */
4324 return;
4325
4326 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4327 last_sector = bi->bi_sector + (bi->bi_size>>9);
4328
4329 bi->bi_next = NULL;
4330 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4331
4332 stripe_sectors = conf->chunk_sectors *
4333 (conf->raid_disks - conf->max_degraded);
4334 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4335 stripe_sectors);
4336 sector_div(last_sector, stripe_sectors);
4337
4338 logical_sector *= conf->chunk_sectors;
4339 last_sector *= conf->chunk_sectors;
4340
4341 for (; logical_sector < last_sector;
4342 logical_sector += STRIPE_SECTORS) {
4343 DEFINE_WAIT(w);
4344 int d;
4345 again:
4346 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4347 prepare_to_wait(&conf->wait_for_overlap, &w,
4348 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004349 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4350 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4351 release_stripe(sh);
4352 schedule();
4353 goto again;
4354 }
4355 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004356 spin_lock_irq(&sh->stripe_lock);
4357 for (d = 0; d < conf->raid_disks; d++) {
4358 if (d == sh->pd_idx || d == sh->qd_idx)
4359 continue;
4360 if (sh->dev[d].towrite || sh->dev[d].toread) {
4361 set_bit(R5_Overlap, &sh->dev[d].flags);
4362 spin_unlock_irq(&sh->stripe_lock);
4363 release_stripe(sh);
4364 schedule();
4365 goto again;
4366 }
4367 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004368 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004369 finish_wait(&conf->wait_for_overlap, &w);
4370 for (d = 0; d < conf->raid_disks; d++) {
4371 if (d == sh->pd_idx || d == sh->qd_idx)
4372 continue;
4373 sh->dev[d].towrite = bi;
4374 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4375 raid5_inc_bi_active_stripes(bi);
4376 }
4377 spin_unlock_irq(&sh->stripe_lock);
4378 if (conf->mddev->bitmap) {
4379 for (d = 0;
4380 d < conf->raid_disks - conf->max_degraded;
4381 d++)
4382 bitmap_startwrite(mddev->bitmap,
4383 sh->sector,
4384 STRIPE_SECTORS,
4385 0);
4386 sh->bm_seq = conf->seq_flush + 1;
4387 set_bit(STRIPE_BIT_DELAY, &sh->state);
4388 }
4389
4390 set_bit(STRIPE_HANDLE, &sh->state);
4391 clear_bit(STRIPE_DELAYED, &sh->state);
4392 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4393 atomic_inc(&conf->preread_active_stripes);
4394 release_stripe_plug(mddev, sh);
4395 }
4396
4397 remaining = raid5_dec_bi_active_stripes(bi);
4398 if (remaining == 0) {
4399 md_write_end(mddev);
4400 bio_endio(bi, 0);
4401 }
4402}
4403
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004404static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
NeilBrownd1688a62011-10-11 16:49:52 +11004406 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004407 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 sector_t new_sector;
4409 sector_t logical_sector, last_sector;
4410 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004411 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004412 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Tejun Heoe9c74692010-09-03 11:56:18 +02004414 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4415 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004416 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004417 }
4418
NeilBrown3d310eb2005-06-21 17:17:26 -07004419 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004420
NeilBrown802ba062006-12-13 00:34:13 -08004421 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004422 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004423 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004424 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004425
Shaohua Li620125f2012-10-11 13:49:05 +11004426 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4427 make_discard_request(mddev, bi);
4428 return;
4429 }
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004432 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 bi->bi_next = NULL;
4434 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4437 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004438 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10004439 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08004440
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004441 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10004442 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11004443 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004444 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004445 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004446 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004447 * 64bit on a 32bit platform, and so it might be
4448 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004449 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004450 * the lock is dropped, so once we get a reference
4451 * to the stripe that we think it is, we will have
4452 * to check again.
4453 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004454 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004455 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004456 ? logical_sector < conf->reshape_progress
4457 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004458 previous = 1;
4459 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004460 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004461 ? logical_sector < conf->reshape_safe
4462 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004463 spin_unlock_irq(&conf->device_lock);
4464 schedule();
4465 goto retry;
4466 }
4467 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004468 spin_unlock_irq(&conf->device_lock);
4469 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004470
NeilBrown112bf892009-03-31 14:39:38 +11004471 new_sector = raid5_compute_sector(conf, logical_sector,
4472 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004473 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004474 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10004475 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 (unsigned long long)logical_sector);
4477
NeilBrownb5663ba2009-03-31 14:39:38 +11004478 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004479 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004481 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004482 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004483 * stripe, so we must do the range check again.
4484 * Expansion could still move past after this
4485 * test, but as we are holding a reference to
4486 * 'sh', we know that if that happens,
4487 * STRIPE_EXPANDING will get set and the expansion
4488 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004489 */
4490 int must_retry = 0;
4491 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004492 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004493 ? logical_sector >= conf->reshape_progress
4494 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004495 /* mismatch, need to try again */
4496 must_retry = 1;
4497 spin_unlock_irq(&conf->device_lock);
4498 if (must_retry) {
4499 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004500 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004501 goto retry;
4502 }
4503 }
NeilBrownc46501b2013-08-27 15:52:13 +10004504 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4505 /* Might have got the wrong stripe_head
4506 * by accident
4507 */
4508 release_stripe(sh);
4509 goto retry;
4510 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004511
Namhyung Kimffd96e32011-07-18 17:38:51 +10004512 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004513 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004514 logical_sector < mddev->suspend_hi) {
4515 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004516 /* As the suspend_* range is controlled by
4517 * userspace, we want an interruptible
4518 * wait.
4519 */
4520 flush_signals(current);
4521 prepare_to_wait(&conf->wait_for_overlap,
4522 &w, TASK_INTERRUPTIBLE);
4523 if (logical_sector >= mddev->suspend_lo &&
4524 logical_sector < mddev->suspend_hi)
4525 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004526 goto retry;
4527 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004528
4529 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004530 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004531 /* Stripe is busy expanding or
4532 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 * and wait a while
4534 */
NeilBrown482c0832011-04-18 18:25:42 +10004535 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 release_stripe(sh);
4537 schedule();
4538 goto retry;
4539 }
4540 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004541 set_bit(STRIPE_HANDLE, &sh->state);
4542 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004543 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004544 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4545 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004546 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 } else {
4548 /* cannot get stripe for read-ahead, just give-up */
4549 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4550 finish_wait(&conf->wait_for_overlap, &w);
4551 break;
4552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004554
Shaohua Lie7836bd62012-07-19 16:01:31 +10004555 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004556 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
NeilBrown16a53ec2006-06-26 00:27:38 -07004558 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004560
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004561 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4562 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004563 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
NeilBrownfd01b882011-10-11 16:47:53 +11004567static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004568
NeilBrownfd01b882011-10-11 16:47:53 +11004569static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
NeilBrown52c03292006-06-26 00:27:43 -07004571 /* reshaping is quite different to recovery/resync so it is
4572 * handled quite separately ... here.
4573 *
4574 * On each call to sync_request, we gather one chunk worth of
4575 * destination stripes and flag them as expanding.
4576 * Then we find all the source stripes and request reads.
4577 * As the reads complete, handle_stripe will copy the data
4578 * into the destination stripe and release that stripe.
4579 */
NeilBrownd1688a62011-10-11 16:49:52 +11004580 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004582 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004583 int raid_disks = conf->previous_raid_disks;
4584 int data_disks = raid_disks - conf->max_degraded;
4585 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004586 int i;
4587 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004588 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004589 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004590 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004591 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004592
NeilBrownfef9c612009-03-31 15:16:46 +11004593 if (sector_nr == 0) {
4594 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004595 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004596 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4597 sector_nr = raid5_size(mddev, 0, 0)
4598 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004599 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004600 conf->reshape_progress > 0)
4601 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004602 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004603 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004604 mddev->curr_resync_completed = sector_nr;
4605 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004606 *skipped = 1;
4607 return sector_nr;
4608 }
NeilBrown52c03292006-06-26 00:27:43 -07004609 }
4610
NeilBrown7a661382009-03-31 15:21:40 +11004611 /* We need to process a full chunk at a time.
4612 * If old and new chunk sizes differ, we need to process the
4613 * largest of these
4614 */
Andre Noll664e7c42009-06-18 08:45:27 +10004615 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4616 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004617 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004618 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004619
NeilBrownb5254dd2012-05-21 09:27:01 +10004620 /* We update the metadata at least every 10 seconds, or when
4621 * the data about to be copied would over-write the source of
4622 * the data at the front of the range. i.e. one new_stripe
4623 * along from reshape_progress new_maps to after where
4624 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004625 */
NeilBrownfef9c612009-03-31 15:16:46 +11004626 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004627 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004628 readpos = conf->reshape_progress;
4629 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004630 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004631 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004632 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004633 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004634 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004635 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004636 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004637 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004638 readpos -= min_t(sector_t, reshape_sectors, readpos);
4639 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004640 }
NeilBrown52c03292006-06-26 00:27:43 -07004641
NeilBrownb5254dd2012-05-21 09:27:01 +10004642 /* Having calculated the 'writepos' possibly use it
4643 * to set 'stripe_addr' which is where we will write to.
4644 */
4645 if (mddev->reshape_backwards) {
4646 BUG_ON(conf->reshape_progress == 0);
4647 stripe_addr = writepos;
4648 BUG_ON((mddev->dev_sectors &
4649 ~((sector_t)reshape_sectors - 1))
4650 - reshape_sectors - stripe_addr
4651 != sector_nr);
4652 } else {
4653 BUG_ON(writepos != sector_nr + reshape_sectors);
4654 stripe_addr = sector_nr;
4655 }
4656
NeilBrownc8f517c2009-03-31 15:28:40 +11004657 /* 'writepos' is the most advanced device address we might write.
4658 * 'readpos' is the least advanced device address we might read.
4659 * 'safepos' is the least address recorded in the metadata as having
4660 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004661 * If there is a min_offset_diff, these are adjusted either by
4662 * increasing the safepos/readpos if diff is negative, or
4663 * increasing writepos if diff is positive.
4664 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004665 * ensure safety in the face of a crash - that must be done by userspace
4666 * making a backup of the data. So in that case there is no particular
4667 * rush to update metadata.
4668 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4669 * update the metadata to advance 'safepos' to match 'readpos' so that
4670 * we can be safe in the event of a crash.
4671 * So we insist on updating metadata if safepos is behind writepos and
4672 * readpos is beyond writepos.
4673 * In any case, update the metadata every 10 seconds.
4674 * Maybe that number should be configurable, but I'm not sure it is
4675 * worth it.... maybe it could be a multiple of safemode_delay???
4676 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004677 if (conf->min_offset_diff < 0) {
4678 safepos += -conf->min_offset_diff;
4679 readpos += -conf->min_offset_diff;
4680 } else
4681 writepos += conf->min_offset_diff;
4682
NeilBrown2c810cd2012-05-21 09:27:00 +10004683 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004684 ? (safepos > writepos && readpos < writepos)
4685 : (safepos < writepos && readpos > writepos)) ||
4686 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004687 /* Cannot proceed until we've updated the superblock... */
4688 wait_event(conf->wait_for_overlap,
4689 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004690 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004691 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004692 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004693 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004694 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004695 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004696 kthread_should_stop());
4697 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004698 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004699 spin_unlock_irq(&conf->device_lock);
4700 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004701 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004702 }
4703
NeilBrownab69ae12009-03-31 15:26:47 +11004704 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004705 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004706 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004707 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004708 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004709 set_bit(STRIPE_EXPANDING, &sh->state);
4710 atomic_inc(&conf->reshape_stripes);
4711 /* If any of this stripe is beyond the end of the old
4712 * array, then we need to zero those blocks
4713 */
4714 for (j=sh->disks; j--;) {
4715 sector_t s;
4716 if (j == sh->pd_idx)
4717 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004718 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004719 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004720 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004721 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004722 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004723 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004724 continue;
4725 }
4726 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4727 set_bit(R5_Expanded, &sh->dev[j].flags);
4728 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4729 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004730 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004731 set_bit(STRIPE_EXPAND_READY, &sh->state);
4732 set_bit(STRIPE_HANDLE, &sh->state);
4733 }
NeilBrownab69ae12009-03-31 15:26:47 +11004734 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004735 }
4736 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004737 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004738 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004739 else
NeilBrown7a661382009-03-31 15:21:40 +11004740 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004741 spin_unlock_irq(&conf->device_lock);
4742 /* Ok, those stripe are ready. We can start scheduling
4743 * reads on the source stripes.
4744 * The source stripes are determined by mapping the first and last
4745 * block on the destination stripes.
4746 */
NeilBrown52c03292006-06-26 00:27:43 -07004747 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004748 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004749 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004750 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004751 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004752 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004753 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004754 if (last_sector >= mddev->dev_sectors)
4755 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004756 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004757 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004758 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4759 set_bit(STRIPE_HANDLE, &sh->state);
4760 release_stripe(sh);
4761 first_sector += STRIPE_SECTORS;
4762 }
NeilBrownab69ae12009-03-31 15:26:47 +11004763 /* Now that the sources are clearly marked, we can release
4764 * the destination stripes
4765 */
4766 while (!list_empty(&stripes)) {
4767 sh = list_entry(stripes.next, struct stripe_head, lru);
4768 list_del_init(&sh->lru);
4769 release_stripe(sh);
4770 }
NeilBrownc6207272008-02-06 01:39:52 -08004771 /* If this takes us to the resync_max point where we have to pause,
4772 * then we need to write out the superblock.
4773 */
NeilBrown7a661382009-03-31 15:21:40 +11004774 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004775 if ((sector_nr - mddev->curr_resync_completed) * 2
4776 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004777 /* Cannot proceed until we've updated the superblock... */
4778 wait_event(conf->wait_for_overlap,
4779 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004780 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004781 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004782 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004783 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4784 md_wakeup_thread(mddev->thread);
4785 wait_event(mddev->sb_wait,
4786 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4787 || kthread_should_stop());
4788 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004789 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004790 spin_unlock_irq(&conf->device_lock);
4791 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004792 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004793 }
NeilBrown7a661382009-03-31 15:21:40 +11004794 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004795}
4796
4797/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004798static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004799{
NeilBrownd1688a62011-10-11 16:49:52 +11004800 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004801 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004802 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004803 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004804 int still_degraded = 0;
4805 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
NeilBrown72626682005-09-09 16:23:54 -07004807 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004809
NeilBrown29269552006-03-27 01:18:10 -08004810 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4811 end_reshape(conf);
4812 return 0;
4813 }
NeilBrown72626682005-09-09 16:23:54 -07004814
4815 if (mddev->curr_resync < max_sector) /* aborted */
4816 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4817 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004818 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004819 conf->fullsync = 0;
4820 bitmap_close_sync(mddev->bitmap);
4821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return 0;
4823 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004824
NeilBrown64bd6602009-08-03 10:59:58 +10004825 /* Allow raid5_quiesce to complete */
4826 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4827
NeilBrown52c03292006-06-26 00:27:43 -07004828 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4829 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004830
NeilBrownc6207272008-02-06 01:39:52 -08004831 /* No need to check resync_max as we never do more than one
4832 * stripe, and as resync_max will always be on a chunk boundary,
4833 * if the check in md_do_sync didn't fire, there is no chance
4834 * of overstepping resync_max here
4835 */
4836
NeilBrown16a53ec2006-06-26 00:27:38 -07004837 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 * to resync, then assert that we are finished, because there is
4839 * nothing we can do.
4840 */
NeilBrown3285edf2006-06-26 00:27:55 -07004841 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004842 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004843 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004844 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 return rv;
4846 }
majianpeng6f608042013-04-24 11:42:41 +10004847 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4848 !conf->fullsync &&
4849 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4850 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004851 /* we can skip this block, and probably more */
4852 sync_blocks /= STRIPE_SECTORS;
4853 *skipped = 1;
4854 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
NeilBrownb47490c2008-02-06 01:39:50 -08004857 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4858
NeilBrowna8c906c2009-06-09 14:39:59 +10004859 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004861 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004863 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004865 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004867 /* Need to check if array will still be degraded after recovery/resync
4868 * We don't need to check the 'failed' flag as when that gets set,
4869 * recovery aborts.
4870 */
NeilBrownf001a702009-06-09 14:30:31 +10004871 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004872 if (conf->disks[i].rdev == NULL)
4873 still_degraded = 1;
4874
4875 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4876
NeilBrown83206d62011-07-26 11:19:49 +10004877 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
NeilBrown14425772009-10-16 15:55:25 +11004879 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 release_stripe(sh);
4881
4882 return STRIPE_SECTORS;
4883}
4884
NeilBrownd1688a62011-10-11 16:49:52 +11004885static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004886{
4887 /* We may not be able to submit a whole bio at once as there
4888 * may not be enough stripe_heads available.
4889 * We cannot pre-allocate enough stripe_heads as we may need
4890 * more than exist in the cache (if we allow ever large chunks).
4891 * So we do one stripe head at a time and record in
4892 * ->bi_hw_segments how many have been done.
4893 *
4894 * We *know* that this entire raid_bio is in one chunk, so
4895 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4896 */
4897 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004898 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004899 sector_t sector, logical_sector, last_sector;
4900 int scnt = 0;
4901 int remaining;
4902 int handled = 0;
4903
4904 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004905 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004906 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004907 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004908
4909 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004910 logical_sector += STRIPE_SECTORS,
4911 sector += STRIPE_SECTORS,
4912 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004913
Shaohua Lie7836bd62012-07-19 16:01:31 +10004914 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004915 /* already done this stripe */
4916 continue;
4917
NeilBrowna8c906c2009-06-09 14:39:59 +10004918 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004919
4920 if (!sh) {
4921 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004922 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004923 conf->retry_read_aligned = raid_bio;
4924 return handled;
4925 }
4926
Neil Brown387bb172007-02-08 14:20:29 -08004927 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4928 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004929 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004930 conf->retry_read_aligned = raid_bio;
4931 return handled;
4932 }
4933
majianpeng3f9e7c12012-07-31 10:04:21 +10004934 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004935 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004936 release_stripe(sh);
4937 handled++;
4938 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004939 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004940 if (remaining == 0) {
4941 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4942 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004943 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004944 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004945 if (atomic_dec_and_test(&conf->active_aligned_reads))
4946 wake_up(&conf->wait_for_stripe);
4947 return handled;
4948}
4949
Shaohua Libfc90cb2013-08-29 15:40:32 +08004950static int handle_active_stripes(struct r5conf *conf, int group,
4951 struct r5worker *worker)
Shaohua Li46a06402012-08-02 08:33:15 +10004952{
4953 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4954 int i, batch_size = 0;
4955
4956 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08004957 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10004958 batch[batch_size++] = sh;
4959
4960 if (batch_size == 0)
4961 return batch_size;
4962 spin_unlock_irq(&conf->device_lock);
4963
4964 for (i = 0; i < batch_size; i++)
4965 handle_stripe(batch[i]);
4966
4967 cond_resched();
4968
4969 spin_lock_irq(&conf->device_lock);
4970 for (i = 0; i < batch_size; i++)
4971 __release_stripe(conf, batch[i]);
4972 return batch_size;
4973}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004974
Shaohua Li851c30c2013-08-28 14:30:16 +08004975static void raid5_do_work(struct work_struct *work)
4976{
4977 struct r5worker *worker = container_of(work, struct r5worker, work);
4978 struct r5worker_group *group = worker->group;
4979 struct r5conf *conf = group->conf;
4980 int group_id = group - conf->worker_groups;
4981 int handled;
4982 struct blk_plug plug;
4983
4984 pr_debug("+++ raid5worker active\n");
4985
4986 blk_start_plug(&plug);
4987 handled = 0;
4988 spin_lock_irq(&conf->device_lock);
4989 while (1) {
4990 int batch_size, released;
4991
4992 released = release_stripe_list(conf);
4993
Shaohua Libfc90cb2013-08-29 15:40:32 +08004994 batch_size = handle_active_stripes(conf, group_id, worker);
4995 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08004996 if (!batch_size && !released)
4997 break;
4998 handled += batch_size;
4999 }
5000 pr_debug("%d stripes handled\n", handled);
5001
5002 spin_unlock_irq(&conf->device_lock);
5003 blk_finish_plug(&plug);
5004
5005 pr_debug("--- raid5worker inactive\n");
5006}
5007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008/*
5009 * This is our raid5 kernel thread.
5010 *
5011 * We scan the hash table for stripes which can be handled now.
5012 * During the scan, completed stripes are saved for us by the interrupt
5013 * handler, so that they will not have to wait for our next wakeup.
5014 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005015static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
Shaohua Li4ed87312012-10-11 13:34:00 +11005017 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005018 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005020 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Dan Williams45b42332007-07-09 11:56:43 -07005022 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
5024 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005026 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 handled = 0;
5028 spin_lock_irq(&conf->device_lock);
5029 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005030 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005031 int batch_size, released;
5032
5033 released = release_stripe_list(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
NeilBrown0021b7b2012-07-31 09:08:14 +02005035 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005036 !list_empty(&conf->bitmap_list)) {
5037 /* Now is a good time to flush some bitmap updates */
5038 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005039 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005040 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005041 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005042 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07005043 activate_bit_delay(conf);
5044 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005045 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005046
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005047 while ((bio = remove_bio_from_retry(conf))) {
5048 int ok;
5049 spin_unlock_irq(&conf->device_lock);
5050 ok = retry_aligned_read(conf, bio);
5051 spin_lock_irq(&conf->device_lock);
5052 if (!ok)
5053 break;
5054 handled++;
5055 }
5056
Shaohua Libfc90cb2013-08-29 15:40:32 +08005057 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL);
Shaohua Li773ca822013-08-27 17:50:39 +08005058 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005060 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Shaohua Li46a06402012-08-02 08:33:15 +10005062 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5063 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005064 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005065 spin_lock_irq(&conf->device_lock);
5066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 }
Dan Williams45b42332007-07-09 11:56:43 -07005068 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
5070 spin_unlock_irq(&conf->device_lock);
5071
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005072 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005073 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Dan Williams45b42332007-07-09 11:56:43 -07005075 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076}
5077
NeilBrown3f294f42005-11-08 21:39:25 -08005078static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005079raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005080{
NeilBrownd1688a62011-10-11 16:49:52 +11005081 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005082 if (conf)
5083 return sprintf(page, "%d\n", conf->max_nr_stripes);
5084 else
5085 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005086}
5087
NeilBrownc41d4ac2010-06-01 19:37:24 +10005088int
NeilBrownfd01b882011-10-11 16:47:53 +11005089raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005090{
NeilBrownd1688a62011-10-11 16:49:52 +11005091 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005092 int err;
5093
5094 if (size <= 16 || size > 32768)
5095 return -EINVAL;
5096 while (size < conf->max_nr_stripes) {
5097 if (drop_one_stripe(conf))
5098 conf->max_nr_stripes--;
5099 else
5100 break;
5101 }
5102 err = md_allow_write(mddev);
5103 if (err)
5104 return err;
5105 while (size > conf->max_nr_stripes) {
5106 if (grow_one_stripe(conf))
5107 conf->max_nr_stripes++;
5108 else break;
5109 }
5110 return 0;
5111}
5112EXPORT_SYMBOL(raid5_set_cache_size);
5113
NeilBrown3f294f42005-11-08 21:39:25 -08005114static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005115raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005116{
NeilBrownd1688a62011-10-11 16:49:52 +11005117 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005118 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005119 int err;
5120
NeilBrown3f294f42005-11-08 21:39:25 -08005121 if (len >= PAGE_SIZE)
5122 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08005123 if (!conf)
5124 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08005125
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005126 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005127 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005128 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005129 if (err)
5130 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08005131 return len;
5132}
NeilBrown007583c2005-11-08 21:39:30 -08005133
NeilBrown96de1e62005-11-08 21:39:39 -08005134static struct md_sysfs_entry
5135raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5136 raid5_show_stripe_cache_size,
5137 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005138
5139static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005140raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005141{
NeilBrownd1688a62011-10-11 16:49:52 +11005142 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005143 if (conf)
5144 return sprintf(page, "%d\n", conf->bypass_threshold);
5145 else
5146 return 0;
5147}
5148
5149static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005150raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005151{
NeilBrownd1688a62011-10-11 16:49:52 +11005152 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005153 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005154 if (len >= PAGE_SIZE)
5155 return -EINVAL;
5156 if (!conf)
5157 return -ENODEV;
5158
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005159 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005160 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005161 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005162 return -EINVAL;
5163 conf->bypass_threshold = new;
5164 return len;
5165}
5166
5167static struct md_sysfs_entry
5168raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5169 S_IRUGO | S_IWUSR,
5170 raid5_show_preread_threshold,
5171 raid5_store_preread_threshold);
5172
5173static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005174stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005175{
NeilBrownd1688a62011-10-11 16:49:52 +11005176 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005177 if (conf)
5178 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5179 else
5180 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005181}
5182
NeilBrown96de1e62005-11-08 21:39:39 -08005183static struct md_sysfs_entry
5184raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005185
Shaohua Lib7214202013-08-27 17:50:42 +08005186static ssize_t
5187raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5188{
5189 struct r5conf *conf = mddev->private;
5190 if (conf)
5191 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5192 else
5193 return 0;
5194}
5195
5196static int alloc_thread_groups(struct r5conf *conf, int cnt);
5197static ssize_t
5198raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5199{
5200 struct r5conf *conf = mddev->private;
5201 unsigned long new;
5202 int err;
5203 struct r5worker_group *old_groups;
5204 int old_group_cnt;
5205
5206 if (len >= PAGE_SIZE)
5207 return -EINVAL;
5208 if (!conf)
5209 return -ENODEV;
5210
5211 if (kstrtoul(page, 10, &new))
5212 return -EINVAL;
5213
5214 if (new == conf->worker_cnt_per_group)
5215 return len;
5216
5217 mddev_suspend(mddev);
5218
5219 old_groups = conf->worker_groups;
5220 old_group_cnt = conf->worker_cnt_per_group;
5221
5222 conf->worker_groups = NULL;
5223 err = alloc_thread_groups(conf, new);
5224 if (err) {
5225 conf->worker_groups = old_groups;
5226 conf->worker_cnt_per_group = old_group_cnt;
5227 } else {
5228 if (old_groups)
5229 kfree(old_groups[0].workers);
5230 kfree(old_groups);
5231 }
5232
5233 mddev_resume(mddev);
5234
5235 if (err)
5236 return err;
5237 return len;
5238}
5239
5240static struct md_sysfs_entry
5241raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5242 raid5_show_group_thread_cnt,
5243 raid5_store_group_thread_cnt);
5244
NeilBrown007583c2005-11-08 21:39:30 -08005245static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005246 &raid5_stripecache_size.attr,
5247 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005248 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005249 &raid5_group_thread_cnt.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005250 NULL,
5251};
NeilBrown007583c2005-11-08 21:39:30 -08005252static struct attribute_group raid5_attrs_group = {
5253 .name = NULL,
5254 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005255};
5256
Shaohua Li851c30c2013-08-28 14:30:16 +08005257static int alloc_thread_groups(struct r5conf *conf, int cnt)
5258{
5259 int i, j;
5260 ssize_t size;
5261 struct r5worker *workers;
5262
5263 conf->worker_cnt_per_group = cnt;
5264 if (cnt == 0) {
5265 conf->worker_groups = NULL;
5266 return 0;
5267 }
5268 conf->group_cnt = num_possible_nodes();
5269 size = sizeof(struct r5worker) * cnt;
5270 workers = kzalloc(size * conf->group_cnt, GFP_NOIO);
5271 conf->worker_groups = kzalloc(sizeof(struct r5worker_group) *
5272 conf->group_cnt, GFP_NOIO);
5273 if (!conf->worker_groups || !workers) {
5274 kfree(workers);
5275 kfree(conf->worker_groups);
5276 conf->worker_groups = NULL;
5277 return -ENOMEM;
5278 }
5279
5280 for (i = 0; i < conf->group_cnt; i++) {
5281 struct r5worker_group *group;
5282
5283 group = &conf->worker_groups[i];
5284 INIT_LIST_HEAD(&group->handle_list);
5285 group->conf = conf;
5286 group->workers = workers + i * cnt;
5287
5288 for (j = 0; j < cnt; j++) {
5289 group->workers[j].group = group;
5290 INIT_WORK(&group->workers[j].work, raid5_do_work);
5291 }
5292 }
5293
5294 return 0;
5295}
5296
5297static void free_thread_groups(struct r5conf *conf)
5298{
5299 if (conf->worker_groups)
5300 kfree(conf->worker_groups[0].workers);
5301 kfree(conf->worker_groups);
5302 conf->worker_groups = NULL;
5303}
5304
Dan Williams80c3a6c2009-03-17 18:10:40 -07005305static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005306raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005307{
NeilBrownd1688a62011-10-11 16:49:52 +11005308 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005309
5310 if (!sectors)
5311 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005312 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005313 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005314 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005315
Andre Noll9d8f0362009-06-18 08:45:01 +10005316 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005317 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005318 return sectors * (raid_disks - conf->max_degraded);
5319}
5320
NeilBrownd1688a62011-10-11 16:49:52 +11005321static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005322{
5323 struct raid5_percpu *percpu;
5324 unsigned long cpu;
5325
5326 if (!conf->percpu)
5327 return;
5328
5329 get_online_cpus();
5330 for_each_possible_cpu(cpu) {
5331 percpu = per_cpu_ptr(conf->percpu, cpu);
5332 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005333 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005334 }
5335#ifdef CONFIG_HOTPLUG_CPU
5336 unregister_cpu_notifier(&conf->cpu_notify);
5337#endif
5338 put_online_cpus();
5339
5340 free_percpu(conf->percpu);
5341}
5342
NeilBrownd1688a62011-10-11 16:49:52 +11005343static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005344{
Shaohua Li851c30c2013-08-28 14:30:16 +08005345 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005346 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005347 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005348 kfree(conf->disks);
5349 kfree(conf->stripe_hashtbl);
5350 kfree(conf);
5351}
5352
Dan Williams36d1c642009-07-14 11:48:22 -07005353#ifdef CONFIG_HOTPLUG_CPU
5354static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5355 void *hcpu)
5356{
NeilBrownd1688a62011-10-11 16:49:52 +11005357 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005358 long cpu = (long)hcpu;
5359 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5360
5361 switch (action) {
5362 case CPU_UP_PREPARE:
5363 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005364 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005365 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005366 if (!percpu->scribble)
5367 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5368
5369 if (!percpu->scribble ||
5370 (conf->level == 6 && !percpu->spare_page)) {
5371 safe_put_page(percpu->spare_page);
5372 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005373 pr_err("%s: failed memory allocation for cpu%ld\n",
5374 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005375 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005376 }
5377 break;
5378 case CPU_DEAD:
5379 case CPU_DEAD_FROZEN:
5380 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005381 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005382 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005383 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005384 break;
5385 default:
5386 break;
5387 }
5388 return NOTIFY_OK;
5389}
5390#endif
5391
NeilBrownd1688a62011-10-11 16:49:52 +11005392static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005393{
5394 unsigned long cpu;
5395 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005396 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005397 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005398 int err;
5399
Dan Williams36d1c642009-07-14 11:48:22 -07005400 allcpus = alloc_percpu(struct raid5_percpu);
5401 if (!allcpus)
5402 return -ENOMEM;
5403 conf->percpu = allcpus;
5404
5405 get_online_cpus();
5406 err = 0;
5407 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005408 if (conf->level == 6) {
5409 spare_page = alloc_page(GFP_KERNEL);
5410 if (!spare_page) {
5411 err = -ENOMEM;
5412 break;
5413 }
5414 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5415 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005416 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005417 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005418 err = -ENOMEM;
5419 break;
5420 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005421 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005422 }
5423#ifdef CONFIG_HOTPLUG_CPU
5424 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5425 conf->cpu_notify.priority = 0;
5426 if (err == 0)
5427 err = register_cpu_notifier(&conf->cpu_notify);
5428#endif
5429 put_online_cpus();
5430
5431 return err;
5432}
5433
NeilBrownd1688a62011-10-11 16:49:52 +11005434static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
NeilBrownd1688a62011-10-11 16:49:52 +11005436 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005437 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005438 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005440 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
NeilBrown91adb562009-03-31 14:39:39 +11005442 if (mddev->new_level != 5
5443 && mddev->new_level != 4
5444 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005445 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005446 mdname(mddev), mddev->new_level);
5447 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
NeilBrown91adb562009-03-31 14:39:39 +11005449 if ((mddev->new_level == 5
5450 && !algorithm_valid_raid5(mddev->new_layout)) ||
5451 (mddev->new_level == 6
5452 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005453 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005454 mdname(mddev), mddev->new_layout);
5455 return ERR_PTR(-EIO);
5456 }
5457 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005458 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005459 mdname(mddev), mddev->raid_disks);
5460 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Andre Noll664e7c42009-06-18 08:45:27 +10005463 if (!mddev->new_chunk_sectors ||
5464 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5465 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005466 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5467 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005468 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005469 }
5470
NeilBrownd1688a62011-10-11 16:49:52 +11005471 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005472 if (conf == NULL)
5473 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005474 /* Don't enable multi-threading by default*/
5475 if (alloc_thread_groups(conf, 0))
5476 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005477 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10005478 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11005479 init_waitqueue_head(&conf->wait_for_stripe);
5480 init_waitqueue_head(&conf->wait_for_overlap);
5481 INIT_LIST_HEAD(&conf->handle_list);
5482 INIT_LIST_HEAD(&conf->hold_list);
5483 INIT_LIST_HEAD(&conf->delayed_list);
5484 INIT_LIST_HEAD(&conf->bitmap_list);
5485 INIT_LIST_HEAD(&conf->inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005486 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005487 atomic_set(&conf->active_stripes, 0);
5488 atomic_set(&conf->preread_active_stripes, 0);
5489 atomic_set(&conf->active_aligned_reads, 0);
5490 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005491 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005492
5493 conf->raid_disks = mddev->raid_disks;
5494 if (mddev->reshape_position == MaxSector)
5495 conf->previous_raid_disks = mddev->raid_disks;
5496 else
5497 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005498 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5499 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005500
NeilBrown5e5e3e72009-10-16 16:35:30 +11005501 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005502 GFP_KERNEL);
5503 if (!conf->disks)
5504 goto abort;
5505
5506 conf->mddev = mddev;
5507
5508 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5509 goto abort;
5510
Dan Williams36d1c642009-07-14 11:48:22 -07005511 conf->level = mddev->new_level;
5512 if (raid5_alloc_percpu(conf) != 0)
5513 goto abort;
5514
NeilBrown0c55e022010-05-03 14:09:02 +10005515 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005516
NeilBrowndafb20f2012-03-19 12:46:39 +11005517 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005518 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005519 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005520 || raid_disk < 0)
5521 continue;
5522 disk = conf->disks + raid_disk;
5523
NeilBrown17045f52011-12-23 10:17:53 +11005524 if (test_bit(Replacement, &rdev->flags)) {
5525 if (disk->replacement)
5526 goto abort;
5527 disk->replacement = rdev;
5528 } else {
5529 if (disk->rdev)
5530 goto abort;
5531 disk->rdev = rdev;
5532 }
NeilBrown91adb562009-03-31 14:39:39 +11005533
5534 if (test_bit(In_sync, &rdev->flags)) {
5535 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005536 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5537 " disk %d\n",
5538 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005539 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005540 /* Cannot rely on bitmap to complete recovery */
5541 conf->fullsync = 1;
5542 }
5543
Andre Noll09c9e5f2009-06-18 08:45:55 +10005544 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005545 conf->level = mddev->new_level;
5546 if (conf->level == 6)
5547 conf->max_degraded = 2;
5548 else
5549 conf->max_degraded = 1;
5550 conf->algorithm = mddev->new_layout;
5551 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005552 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005553 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005554 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005555 conf->prev_algo = mddev->layout;
5556 }
NeilBrown91adb562009-03-31 14:39:39 +11005557
5558 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005559 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005560 if (grow_stripes(conf, conf->max_nr_stripes)) {
5561 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005562 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5563 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005564 goto abort;
5565 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005566 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5567 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005568
NeilBrown02326052012-07-03 15:56:52 +10005569 sprintf(pers_name, "raid%d", mddev->new_level);
5570 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005571 if (!conf->thread) {
5572 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005573 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005574 mdname(mddev));
5575 goto abort;
5576 }
5577
5578 return conf;
5579
5580 abort:
5581 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005582 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005583 return ERR_PTR(-EIO);
5584 } else
5585 return ERR_PTR(-ENOMEM);
5586}
5587
NeilBrownc148ffd2009-11-13 17:47:00 +11005588
5589static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5590{
5591 switch (algo) {
5592 case ALGORITHM_PARITY_0:
5593 if (raid_disk < max_degraded)
5594 return 1;
5595 break;
5596 case ALGORITHM_PARITY_N:
5597 if (raid_disk >= raid_disks - max_degraded)
5598 return 1;
5599 break;
5600 case ALGORITHM_PARITY_0_6:
5601 if (raid_disk == 0 ||
5602 raid_disk == raid_disks - 1)
5603 return 1;
5604 break;
5605 case ALGORITHM_LEFT_ASYMMETRIC_6:
5606 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5607 case ALGORITHM_LEFT_SYMMETRIC_6:
5608 case ALGORITHM_RIGHT_SYMMETRIC_6:
5609 if (raid_disk == raid_disks - 1)
5610 return 1;
5611 }
5612 return 0;
5613}
5614
NeilBrownfd01b882011-10-11 16:47:53 +11005615static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005616{
NeilBrownd1688a62011-10-11 16:49:52 +11005617 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005618 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005619 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005620 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005621 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005622 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005623 long long min_offset_diff = 0;
5624 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005625
Andre Noll8c6ac862009-06-18 08:48:06 +10005626 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005627 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005628 " -- starting background reconstruction\n",
5629 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005630
5631 rdev_for_each(rdev, mddev) {
5632 long long diff;
5633 if (rdev->raid_disk < 0)
5634 continue;
5635 diff = (rdev->new_data_offset - rdev->data_offset);
5636 if (first) {
5637 min_offset_diff = diff;
5638 first = 0;
5639 } else if (mddev->reshape_backwards &&
5640 diff < min_offset_diff)
5641 min_offset_diff = diff;
5642 else if (!mddev->reshape_backwards &&
5643 diff > min_offset_diff)
5644 min_offset_diff = diff;
5645 }
5646
NeilBrownf6705572006-03-27 01:18:11 -08005647 if (mddev->reshape_position != MaxSector) {
5648 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005649 * Difficulties arise if the stripe we would write to
5650 * next is at or after the stripe we would read from next.
5651 * For a reshape that changes the number of devices, this
5652 * is only possible for a very short time, and mdadm makes
5653 * sure that time appears to have past before assembling
5654 * the array. So we fail if that time hasn't passed.
5655 * For a reshape that keeps the number of devices the same
5656 * mdadm must be monitoring the reshape can keeping the
5657 * critical areas read-only and backed up. It will start
5658 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005659 */
5660 sector_t here_new, here_old;
5661 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005662 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005663
NeilBrown88ce4932009-03-31 15:24:23 +11005664 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005665 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005666 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005667 mdname(mddev));
5668 return -EINVAL;
5669 }
NeilBrownf6705572006-03-27 01:18:11 -08005670 old_disks = mddev->raid_disks - mddev->delta_disks;
5671 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005672 * further up in new geometry must map after here in old
5673 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005674 */
5675 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005676 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005677 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005678 printk(KERN_ERR "md/raid:%s: reshape_position not "
5679 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005680 return -EINVAL;
5681 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005682 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005683 /* here_new is the stripe we will write to */
5684 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005685 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005686 (old_disks-max_degraded));
5687 /* here_old is the first stripe that we might need to read
5688 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005689 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005690 if ((here_new * mddev->new_chunk_sectors !=
5691 here_old * mddev->chunk_sectors)) {
5692 printk(KERN_ERR "md/raid:%s: reshape position is"
5693 " confused - aborting\n", mdname(mddev));
5694 return -EINVAL;
5695 }
NeilBrown67ac6012009-08-13 10:06:24 +10005696 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005697 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005698 * and taking constant backups.
5699 * mdadm always starts a situation like this in
5700 * readonly mode so it can take control before
5701 * allowing any writes. So just check for that.
5702 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005703 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5704 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5705 /* not really in-place - so OK */;
5706 else if (mddev->ro == 0) {
5707 printk(KERN_ERR "md/raid:%s: in-place reshape "
5708 "must be started in read-only mode "
5709 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005710 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005711 return -EINVAL;
5712 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005713 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005714 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005715 here_old * mddev->chunk_sectors)
5716 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005717 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005718 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005719 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5720 "auto-recovery - aborting.\n",
5721 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005722 return -EINVAL;
5723 }
NeilBrown0c55e022010-05-03 14:09:02 +10005724 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5725 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005726 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005727 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005728 BUG_ON(mddev->level != mddev->new_level);
5729 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005730 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005731 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005732 }
5733
NeilBrown245f46c2009-03-31 14:39:39 +11005734 if (mddev->private == NULL)
5735 conf = setup_conf(mddev);
5736 else
5737 conf = mddev->private;
5738
NeilBrown91adb562009-03-31 14:39:39 +11005739 if (IS_ERR(conf))
5740 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005741
NeilBrownb5254dd2012-05-21 09:27:01 +10005742 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005743 mddev->thread = conf->thread;
5744 conf->thread = NULL;
5745 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
NeilBrown17045f52011-12-23 10:17:53 +11005747 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5748 i++) {
5749 rdev = conf->disks[i].rdev;
5750 if (!rdev && conf->disks[i].replacement) {
5751 /* The replacement is all we have yet */
5752 rdev = conf->disks[i].replacement;
5753 conf->disks[i].replacement = NULL;
5754 clear_bit(Replacement, &rdev->flags);
5755 conf->disks[i].rdev = rdev;
5756 }
5757 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005758 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005759 if (conf->disks[i].replacement &&
5760 conf->reshape_progress != MaxSector) {
5761 /* replacements and reshape simply do not mix. */
5762 printk(KERN_ERR "md: cannot handle concurrent "
5763 "replacement and reshape.\n");
5764 goto abort;
5765 }
NeilBrown2f115882010-06-17 17:41:03 +10005766 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005767 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005768 continue;
5769 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005770 /* This disc is not fully in-sync. However if it
5771 * just stored parity (beyond the recovery_offset),
5772 * when we don't need to be concerned about the
5773 * array being dirty.
5774 * When reshape goes 'backwards', we never have
5775 * partially completed devices, so we only need
5776 * to worry about reshape going forwards.
5777 */
5778 /* Hack because v0.91 doesn't store recovery_offset properly. */
5779 if (mddev->major_version == 0 &&
5780 mddev->minor_version > 90)
5781 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005782
NeilBrownc148ffd2009-11-13 17:47:00 +11005783 if (rdev->recovery_offset < reshape_offset) {
5784 /* We need to check old and new layout */
5785 if (!only_parity(rdev->raid_disk,
5786 conf->algorithm,
5787 conf->raid_disks,
5788 conf->max_degraded))
5789 continue;
5790 }
5791 if (!only_parity(rdev->raid_disk,
5792 conf->prev_algo,
5793 conf->previous_raid_disks,
5794 conf->max_degraded))
5795 continue;
5796 dirty_parity_disks++;
5797 }
NeilBrown91adb562009-03-31 14:39:39 +11005798
NeilBrown17045f52011-12-23 10:17:53 +11005799 /*
5800 * 0 for a fully functional array, 1 or 2 for a degraded array.
5801 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005802 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
NeilBrown674806d2010-06-16 17:17:53 +10005804 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005805 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005807 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 goto abort;
5809 }
5810
NeilBrown91adb562009-03-31 14:39:39 +11005811 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005812 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005813 mddev->resync_max_sectors = mddev->dev_sectors;
5814
NeilBrownc148ffd2009-11-13 17:47:00 +11005815 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005817 if (mddev->ok_start_degraded)
5818 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005819 "md/raid:%s: starting dirty degraded array"
5820 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005821 mdname(mddev));
5822 else {
5823 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005824 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005825 mdname(mddev));
5826 goto abort;
5827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
5829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005831 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5832 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005833 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5834 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 else
NeilBrown0c55e022010-05-03 14:09:02 +10005836 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5837 " out of %d devices, algorithm %d\n",
5838 mdname(mddev), conf->level,
5839 mddev->raid_disks - mddev->degraded,
5840 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
5842 print_raid5_conf(conf);
5843
NeilBrownfef9c612009-03-31 15:16:46 +11005844 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005845 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005846 atomic_set(&conf->reshape_stripes, 0);
5847 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5848 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5849 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5850 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5851 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005852 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005853 }
5854
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
5856 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005857 if (mddev->to_remove == &raid5_attrs_group)
5858 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005859 else if (mddev->kobj.sd &&
5860 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005861 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005862 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005863 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005864 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5865
5866 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005867 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005868 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005869 /* read-ahead size must cover two whole stripes, which
5870 * is 2 * (datadisks) * chunksize where 'n' is the
5871 * number of raid devices
5872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5874 int stripe = data_disks *
5875 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5876 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5877 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005878
5879 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005880
5881 mddev->queue->backing_dev_info.congested_data = mddev;
5882 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005883
5884 chunk_size = mddev->chunk_sectors << 9;
5885 blk_queue_io_min(mddev->queue, chunk_size);
5886 blk_queue_io_opt(mddev->queue, chunk_size *
5887 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005888 /*
5889 * We can only discard a whole stripe. It doesn't make sense to
5890 * discard data disk but write parity disk
5891 */
5892 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005893 /* Round up to power of 2, as discard handling
5894 * currently assumes that */
5895 while ((stripe-1) & stripe)
5896 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005897 mddev->queue->limits.discard_alignment = stripe;
5898 mddev->queue->limits.discard_granularity = stripe;
5899 /*
5900 * unaligned part of discard request will be ignored, so can't
5901 * guarantee discard_zerors_data
5902 */
5903 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005904
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005905 blk_queue_max_write_same_sectors(mddev->queue, 0);
5906
NeilBrown05616be2012-05-21 09:27:00 +10005907 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005908 disk_stack_limits(mddev->gendisk, rdev->bdev,
5909 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005910 disk_stack_limits(mddev->gendisk, rdev->bdev,
5911 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005912 /*
5913 * discard_zeroes_data is required, otherwise data
5914 * could be lost. Consider a scenario: discard a stripe
5915 * (the stripe could be inconsistent if
5916 * discard_zeroes_data is 0); write one disk of the
5917 * stripe (the stripe could be inconsistent again
5918 * depending on which disks are used to calculate
5919 * parity); the disk is broken; The stripe data of this
5920 * disk is lost.
5921 */
5922 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5923 !bdev_get_queue(rdev->bdev)->
5924 limits.discard_zeroes_data)
5925 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005926 }
Shaohua Li620125f2012-10-11 13:49:05 +11005927
5928 if (discard_supported &&
5929 mddev->queue->limits.max_discard_sectors >= stripe &&
5930 mddev->queue->limits.discard_granularity >= stripe)
5931 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5932 mddev->queue);
5933 else
5934 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5935 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 }
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 return 0;
5939abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005940 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005941 print_raid5_conf(conf);
5942 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005944 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 return -EIO;
5946}
5947
NeilBrownfd01b882011-10-11 16:47:53 +11005948static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949{
NeilBrownd1688a62011-10-11 16:49:52 +11005950 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
NeilBrown01f96c02011-09-21 15:30:20 +10005952 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005953 if (mddev->queue)
5954 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005955 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005956 mddev->private = NULL;
5957 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 return 0;
5959}
5960
NeilBrownfd01b882011-10-11 16:47:53 +11005961static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962{
NeilBrownd1688a62011-10-11 16:49:52 +11005963 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 int i;
5965
Andre Noll9d8f0362009-06-18 08:45:01 +10005966 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5967 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005968 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 for (i = 0; i < conf->raid_disks; i++)
5970 seq_printf (seq, "%s",
5971 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005972 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974}
5975
NeilBrownd1688a62011-10-11 16:49:52 +11005976static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
5978 int i;
5979 struct disk_info *tmp;
5980
NeilBrown0c55e022010-05-03 14:09:02 +10005981 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 if (!conf) {
5983 printk("(conf==NULL)\n");
5984 return;
5985 }
NeilBrown0c55e022010-05-03 14:09:02 +10005986 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5987 conf->raid_disks,
5988 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
5990 for (i = 0; i < conf->raid_disks; i++) {
5991 char b[BDEVNAME_SIZE];
5992 tmp = conf->disks + i;
5993 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005994 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5995 i, !test_bit(Faulty, &tmp->rdev->flags),
5996 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 }
5998}
5999
NeilBrownfd01b882011-10-11 16:47:53 +11006000static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001{
6002 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006003 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006005 int count = 0;
6006 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
6008 for (i = 0; i < conf->raid_disks; i++) {
6009 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006010 if (tmp->replacement
6011 && tmp->replacement->recovery_offset == MaxSector
6012 && !test_bit(Faulty, &tmp->replacement->flags)
6013 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6014 /* Replacement has just become active. */
6015 if (!tmp->rdev
6016 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6017 count++;
6018 if (tmp->rdev) {
6019 /* Replaced device not technically faulty,
6020 * but we need to be sure it gets removed
6021 * and never re-added.
6022 */
6023 set_bit(Faulty, &tmp->rdev->flags);
6024 sysfs_notify_dirent_safe(
6025 tmp->rdev->sysfs_state);
6026 }
6027 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6028 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006029 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006030 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006031 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006032 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006033 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 }
6035 }
NeilBrown6b965622010-08-18 11:56:59 +10006036 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006037 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006038 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006040 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041}
6042
NeilBrownb8321b62011-12-23 10:17:51 +11006043static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
NeilBrownd1688a62011-10-11 16:49:52 +11006045 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006047 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006048 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 struct disk_info *p = conf->disks + number;
6050
6051 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006052 if (rdev == p->rdev)
6053 rdevp = &p->rdev;
6054 else if (rdev == p->replacement)
6055 rdevp = &p->replacement;
6056 else
6057 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006058
NeilBrown657e3e42011-12-23 10:17:52 +11006059 if (number >= conf->raid_disks &&
6060 conf->reshape_progress == MaxSector)
6061 clear_bit(In_sync, &rdev->flags);
6062
6063 if (test_bit(In_sync, &rdev->flags) ||
6064 atomic_read(&rdev->nr_pending)) {
6065 err = -EBUSY;
6066 goto abort;
6067 }
6068 /* Only remove non-faulty devices if recovery
6069 * isn't possible.
6070 */
6071 if (!test_bit(Faulty, &rdev->flags) &&
6072 mddev->recovery_disabled != conf->recovery_disabled &&
6073 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006074 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006075 number < conf->raid_disks) {
6076 err = -EBUSY;
6077 goto abort;
6078 }
6079 *rdevp = NULL;
6080 synchronize_rcu();
6081 if (atomic_read(&rdev->nr_pending)) {
6082 /* lost the race, try later */
6083 err = -EBUSY;
6084 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006085 } else if (p->replacement) {
6086 /* We must have just cleared 'rdev' */
6087 p->rdev = p->replacement;
6088 clear_bit(Replacement, &p->replacement->flags);
6089 smp_mb(); /* Make sure other CPUs may see both as identical
6090 * but will never see neither - if they are careful
6091 */
6092 p->replacement = NULL;
6093 clear_bit(WantReplacement, &rdev->flags);
6094 } else
6095 /* We might have just removed the Replacement as faulty-
6096 * clear the bit just in case
6097 */
6098 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099abort:
6100
6101 print_raid5_conf(conf);
6102 return err;
6103}
6104
NeilBrownfd01b882011-10-11 16:47:53 +11006105static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
NeilBrownd1688a62011-10-11 16:49:52 +11006107 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006108 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 int disk;
6110 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006111 int first = 0;
6112 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
NeilBrown7f0da592011-07-28 11:39:22 +10006114 if (mddev->recovery_disabled == conf->recovery_disabled)
6115 return -EBUSY;
6116
NeilBrowndc10c642012-03-19 12:46:37 +11006117 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Neil Brown6c2fce22008-06-28 08:31:31 +10006121 if (rdev->raid_disk >= 0)
6122 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
6124 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006125 * find the disk ... but prefer rdev->saved_raid_disk
6126 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006128 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006129 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006130 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006131 first = rdev->saved_raid_disk;
6132
6133 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006134 p = conf->disks + disk;
6135 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006136 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006138 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006139 if (rdev->saved_raid_disk != disk)
6140 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006141 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006144 }
6145 for (disk = first; disk <= last; disk++) {
6146 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006147 if (test_bit(WantReplacement, &p->rdev->flags) &&
6148 p->replacement == NULL) {
6149 clear_bit(In_sync, &rdev->flags);
6150 set_bit(Replacement, &rdev->flags);
6151 rdev->raid_disk = disk;
6152 err = 0;
6153 conf->fullsync = 1;
6154 rcu_assign_pointer(p->replacement, rdev);
6155 break;
6156 }
6157 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006158out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161}
6162
NeilBrownfd01b882011-10-11 16:47:53 +11006163static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164{
6165 /* no resync is happening, and there is enough space
6166 * on all devices, so we can resize.
6167 * We need to make sure resync covers any new space.
6168 * If the array is shrinking we should possibly wait until
6169 * any io in the removed space completes, but it hardly seems
6170 * worth it.
6171 */
NeilBrowna4a61252012-05-22 13:55:27 +10006172 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006173 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006174 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6175 if (mddev->external_size &&
6176 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006177 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006178 if (mddev->bitmap) {
6179 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6180 if (ret)
6181 return ret;
6182 }
6183 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006184 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006185 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006186 if (sectors > mddev->dev_sectors &&
6187 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006188 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6190 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006191 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006192 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 return 0;
6194}
6195
NeilBrownfd01b882011-10-11 16:47:53 +11006196static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006197{
6198 /* Can only proceed if there are plenty of stripe_heads.
6199 * We need a minimum of one full stripe,, and for sensible progress
6200 * it is best to have about 4 times that.
6201 * If we require 4 times, then the default 256 4K stripe_heads will
6202 * allow for chunk sizes up to 256K, which is probably OK.
6203 * If the chunk size is greater, user-space should request more
6204 * stripe_heads first.
6205 */
NeilBrownd1688a62011-10-11 16:49:52 +11006206 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006207 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6208 > conf->max_nr_stripes ||
6209 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6210 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006211 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6212 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006213 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6214 / STRIPE_SIZE)*4);
6215 return 0;
6216 }
6217 return 1;
6218}
6219
NeilBrownfd01b882011-10-11 16:47:53 +11006220static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006221{
NeilBrownd1688a62011-10-11 16:49:52 +11006222 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006223
NeilBrown88ce4932009-03-31 15:24:23 +11006224 if (mddev->delta_disks == 0 &&
6225 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006226 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006227 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006228 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006229 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006230 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006231 /* We might be able to shrink, but the devices must
6232 * be made bigger first.
6233 * For raid6, 4 is the minimum size.
6234 * Otherwise 2 is the minimum
6235 */
6236 int min = 2;
6237 if (mddev->level == 6)
6238 min = 4;
6239 if (mddev->raid_disks + mddev->delta_disks < min)
6240 return -EINVAL;
6241 }
NeilBrown29269552006-03-27 01:18:10 -08006242
NeilBrown01ee22b2009-06-18 08:47:20 +10006243 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006244 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006245
NeilBrowne56108d62012-10-11 14:24:13 +11006246 return resize_stripes(conf, (conf->previous_raid_disks
6247 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006248}
6249
NeilBrownfd01b882011-10-11 16:47:53 +11006250static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006251{
NeilBrownd1688a62011-10-11 16:49:52 +11006252 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006253 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006254 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006255 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006256
NeilBrownf4168852007-02-28 20:11:53 -08006257 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006258 return -EBUSY;
6259
NeilBrown01ee22b2009-06-18 08:47:20 +10006260 if (!check_stripe_cache(mddev))
6261 return -ENOSPC;
6262
NeilBrown30b67642012-05-22 13:55:28 +10006263 if (has_failed(conf))
6264 return -EINVAL;
6265
NeilBrownc6563a82012-05-21 09:27:00 +10006266 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006267 if (!test_bit(In_sync, &rdev->flags)
6268 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006269 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006270 }
NeilBrown63c70c42006-03-27 01:18:13 -08006271
NeilBrownf4168852007-02-28 20:11:53 -08006272 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006273 /* Not enough devices even to make a degraded array
6274 * of that size
6275 */
6276 return -EINVAL;
6277
NeilBrownec32a2b2009-03-31 15:17:38 +11006278 /* Refuse to reduce size of the array. Any reductions in
6279 * array size must be through explicit setting of array_size
6280 * attribute.
6281 */
6282 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6283 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006284 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006285 "before number of disks\n", mdname(mddev));
6286 return -EINVAL;
6287 }
6288
NeilBrownf6705572006-03-27 01:18:11 -08006289 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006290 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006291 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006292 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006293 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006294 conf->prev_chunk_sectors = conf->chunk_sectors;
6295 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006296 conf->prev_algo = conf->algorithm;
6297 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006298 conf->generation++;
6299 /* Code that selects data_offset needs to see the generation update
6300 * if reshape_progress has been set - so a memory barrier needed.
6301 */
6302 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006303 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006304 conf->reshape_progress = raid5_size(mddev, 0, 0);
6305 else
6306 conf->reshape_progress = 0;
6307 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10006308 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006309 spin_unlock_irq(&conf->device_lock);
6310
NeilBrown4d77e3b2013-08-27 15:57:47 +10006311 /* Now make sure any requests that proceeded on the assumption
6312 * the reshape wasn't running - like Discard or Read - have
6313 * completed.
6314 */
6315 mddev_suspend(mddev);
6316 mddev_resume(mddev);
6317
NeilBrown29269552006-03-27 01:18:10 -08006318 /* Add some new drives, as many as will fit.
6319 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006320 * Don't add devices if we are reducing the number of
6321 * devices in the array. This is because it is not possible
6322 * to correctly record the "partially reconstructed" state of
6323 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006324 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006325 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006326 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006327 if (rdev->raid_disk < 0 &&
6328 !test_bit(Faulty, &rdev->flags)) {
6329 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006330 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006331 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006332 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006333 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006334 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006335
6336 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006337 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006338 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006339 } else if (rdev->raid_disk >= conf->previous_raid_disks
6340 && !test_bit(Faulty, &rdev->flags)) {
6341 /* This is a spare that was manually added */
6342 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006343 }
NeilBrown29269552006-03-27 01:18:10 -08006344
NeilBrown87a8dec2011-01-31 11:57:43 +11006345 /* When a reshape changes the number of devices,
6346 * ->degraded is measured against the larger of the
6347 * pre and post number of devices.
6348 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006349 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006350 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006351 spin_unlock_irqrestore(&conf->device_lock, flags);
6352 }
NeilBrown63c70c42006-03-27 01:18:13 -08006353 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006354 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006355 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006356
NeilBrown29269552006-03-27 01:18:10 -08006357 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6358 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6359 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6360 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6361 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006362 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006363 if (!mddev->sync_thread) {
6364 mddev->recovery = 0;
6365 spin_lock_irq(&conf->device_lock);
6366 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006367 rdev_for_each(rdev, mddev)
6368 rdev->new_data_offset = rdev->data_offset;
6369 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006370 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006371 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006372 spin_unlock_irq(&conf->device_lock);
6373 return -EAGAIN;
6374 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006375 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006376 md_wakeup_thread(mddev->sync_thread);
6377 md_new_event(mddev);
6378 return 0;
6379}
NeilBrown29269552006-03-27 01:18:10 -08006380
NeilBrownec32a2b2009-03-31 15:17:38 +11006381/* This is called from the reshape thread and should make any
6382 * changes needed in 'conf'
6383 */
NeilBrownd1688a62011-10-11 16:49:52 +11006384static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006385{
NeilBrown29269552006-03-27 01:18:10 -08006386
NeilBrownf6705572006-03-27 01:18:11 -08006387 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006388 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006389
NeilBrownf6705572006-03-27 01:18:11 -08006390 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006391 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006392 rdev_for_each(rdev, conf->mddev)
6393 rdev->data_offset = rdev->new_data_offset;
6394 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006395 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006396 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006397 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006398
6399 /* read-ahead size must cover two whole stripes, which is
6400 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6401 */
NeilBrown4a5add42010-06-01 19:37:28 +10006402 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006403 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006404 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006405 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006406 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6407 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6408 }
NeilBrown29269552006-03-27 01:18:10 -08006409 }
NeilBrown29269552006-03-27 01:18:10 -08006410}
6411
NeilBrownec32a2b2009-03-31 15:17:38 +11006412/* This is called from the raid5d thread with mddev_lock held.
6413 * It makes config changes to the device.
6414 */
NeilBrownfd01b882011-10-11 16:47:53 +11006415static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006416{
NeilBrownd1688a62011-10-11 16:49:52 +11006417 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006418
6419 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6420
NeilBrownec32a2b2009-03-31 15:17:38 +11006421 if (mddev->delta_disks > 0) {
6422 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6423 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006424 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006425 } else {
6426 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006427 spin_lock_irq(&conf->device_lock);
6428 mddev->degraded = calc_degraded(conf);
6429 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006430 for (d = conf->raid_disks ;
6431 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006432 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006433 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006434 if (rdev)
6435 clear_bit(In_sync, &rdev->flags);
6436 rdev = conf->disks[d].replacement;
6437 if (rdev)
6438 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006439 }
NeilBrowncea9c222009-03-31 15:15:05 +11006440 }
NeilBrown88ce4932009-03-31 15:24:23 +11006441 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006442 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006443 mddev->reshape_position = MaxSector;
6444 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006445 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006446 }
6447}
6448
NeilBrownfd01b882011-10-11 16:47:53 +11006449static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006450{
NeilBrownd1688a62011-10-11 16:49:52 +11006451 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006452
6453 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006454 case 2: /* resume for a suspend */
6455 wake_up(&conf->wait_for_overlap);
6456 break;
6457
NeilBrown72626682005-09-09 16:23:54 -07006458 case 1: /* stop all writes */
6459 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006460 /* '2' tells resync/reshape to pause so that all
6461 * active stripes can drain
6462 */
6463 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006464 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006465 atomic_read(&conf->active_stripes) == 0 &&
6466 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006467 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006468 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006469 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006470 /* allow reshape to continue */
6471 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006472 break;
6473
6474 case 0: /* re-enable writes */
6475 spin_lock_irq(&conf->device_lock);
6476 conf->quiesce = 0;
6477 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006478 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006479 spin_unlock_irq(&conf->device_lock);
6480 break;
6481 }
NeilBrown72626682005-09-09 16:23:54 -07006482}
NeilBrownb15c2e52006-01-06 00:20:16 -08006483
NeilBrownd562b0c2009-03-31 14:39:39 +11006484
NeilBrownfd01b882011-10-11 16:47:53 +11006485static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006486{
NeilBrowne373ab12011-10-11 16:48:59 +11006487 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006488 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006489
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006490 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006491 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006492 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6493 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006494 return ERR_PTR(-EINVAL);
6495 }
6496
NeilBrowne373ab12011-10-11 16:48:59 +11006497 sectors = raid0_conf->strip_zone[0].zone_end;
6498 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006499 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006500 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006501 mddev->new_layout = ALGORITHM_PARITY_N;
6502 mddev->new_chunk_sectors = mddev->chunk_sectors;
6503 mddev->raid_disks += 1;
6504 mddev->delta_disks = 1;
6505 /* make sure it will be not marked as dirty */
6506 mddev->recovery_cp = MaxSector;
6507
6508 return setup_conf(mddev);
6509}
6510
6511
NeilBrownfd01b882011-10-11 16:47:53 +11006512static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006513{
6514 int chunksect;
6515
6516 if (mddev->raid_disks != 2 ||
6517 mddev->degraded > 1)
6518 return ERR_PTR(-EINVAL);
6519
6520 /* Should check if there are write-behind devices? */
6521
6522 chunksect = 64*2; /* 64K by default */
6523
6524 /* The array must be an exact multiple of chunksize */
6525 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6526 chunksect >>= 1;
6527
6528 if ((chunksect<<9) < STRIPE_SIZE)
6529 /* array size does not allow a suitable chunk size */
6530 return ERR_PTR(-EINVAL);
6531
6532 mddev->new_level = 5;
6533 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006534 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006535
6536 return setup_conf(mddev);
6537}
6538
NeilBrownfd01b882011-10-11 16:47:53 +11006539static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006540{
6541 int new_layout;
6542
6543 switch (mddev->layout) {
6544 case ALGORITHM_LEFT_ASYMMETRIC_6:
6545 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6546 break;
6547 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6548 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6549 break;
6550 case ALGORITHM_LEFT_SYMMETRIC_6:
6551 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6552 break;
6553 case ALGORITHM_RIGHT_SYMMETRIC_6:
6554 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6555 break;
6556 case ALGORITHM_PARITY_0_6:
6557 new_layout = ALGORITHM_PARITY_0;
6558 break;
6559 case ALGORITHM_PARITY_N:
6560 new_layout = ALGORITHM_PARITY_N;
6561 break;
6562 default:
6563 return ERR_PTR(-EINVAL);
6564 }
6565 mddev->new_level = 5;
6566 mddev->new_layout = new_layout;
6567 mddev->delta_disks = -1;
6568 mddev->raid_disks -= 1;
6569 return setup_conf(mddev);
6570}
6571
NeilBrownd562b0c2009-03-31 14:39:39 +11006572
NeilBrownfd01b882011-10-11 16:47:53 +11006573static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006574{
NeilBrown88ce4932009-03-31 15:24:23 +11006575 /* For a 2-drive array, the layout and chunk size can be changed
6576 * immediately as not restriping is needed.
6577 * For larger arrays we record the new value - after validation
6578 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006579 */
NeilBrownd1688a62011-10-11 16:49:52 +11006580 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006581 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006582
NeilBrown597a7112009-06-18 08:47:42 +10006583 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006584 return -EINVAL;
6585 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006586 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006587 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006588 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006589 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006590 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006591 /* not factor of array size */
6592 return -EINVAL;
6593 }
6594
6595 /* They look valid */
6596
NeilBrown88ce4932009-03-31 15:24:23 +11006597 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006598 /* can make the change immediately */
6599 if (mddev->new_layout >= 0) {
6600 conf->algorithm = mddev->new_layout;
6601 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006602 }
6603 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006604 conf->chunk_sectors = new_chunk ;
6605 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006606 }
6607 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6608 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006609 }
NeilBrown50ac1682009-06-18 08:47:55 +10006610 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006611}
6612
NeilBrownfd01b882011-10-11 16:47:53 +11006613static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006614{
NeilBrown597a7112009-06-18 08:47:42 +10006615 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006616
NeilBrown597a7112009-06-18 08:47:42 +10006617 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006618 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006619 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006620 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006621 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006622 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006623 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006624 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006625 /* not factor of array size */
6626 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006627 }
NeilBrown88ce4932009-03-31 15:24:23 +11006628
6629 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006630 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006631}
6632
NeilBrownfd01b882011-10-11 16:47:53 +11006633static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006634{
6635 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006636 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006637 * raid1 - if there are two drives. We need to know the chunk size
6638 * raid4 - trivial - just use a raid4 layout.
6639 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006640 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006641 if (mddev->level == 0)
6642 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006643 if (mddev->level == 1)
6644 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006645 if (mddev->level == 4) {
6646 mddev->new_layout = ALGORITHM_PARITY_N;
6647 mddev->new_level = 5;
6648 return setup_conf(mddev);
6649 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006650 if (mddev->level == 6)
6651 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006652
6653 return ERR_PTR(-EINVAL);
6654}
6655
NeilBrownfd01b882011-10-11 16:47:53 +11006656static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006657{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006658 /* raid4 can take over:
6659 * raid0 - if there is only one strip zone
6660 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006661 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006662 if (mddev->level == 0)
6663 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006664 if (mddev->level == 5 &&
6665 mddev->layout == ALGORITHM_PARITY_N) {
6666 mddev->new_layout = 0;
6667 mddev->new_level = 4;
6668 return setup_conf(mddev);
6669 }
6670 return ERR_PTR(-EINVAL);
6671}
NeilBrownd562b0c2009-03-31 14:39:39 +11006672
NeilBrown84fc4b52011-10-11 16:49:58 +11006673static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006674
NeilBrownfd01b882011-10-11 16:47:53 +11006675static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006676{
6677 /* Currently can only take over a raid5. We map the
6678 * personality to an equivalent raid6 personality
6679 * with the Q block at the end.
6680 */
6681 int new_layout;
6682
6683 if (mddev->pers != &raid5_personality)
6684 return ERR_PTR(-EINVAL);
6685 if (mddev->degraded > 1)
6686 return ERR_PTR(-EINVAL);
6687 if (mddev->raid_disks > 253)
6688 return ERR_PTR(-EINVAL);
6689 if (mddev->raid_disks < 3)
6690 return ERR_PTR(-EINVAL);
6691
6692 switch (mddev->layout) {
6693 case ALGORITHM_LEFT_ASYMMETRIC:
6694 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6695 break;
6696 case ALGORITHM_RIGHT_ASYMMETRIC:
6697 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6698 break;
6699 case ALGORITHM_LEFT_SYMMETRIC:
6700 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6701 break;
6702 case ALGORITHM_RIGHT_SYMMETRIC:
6703 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6704 break;
6705 case ALGORITHM_PARITY_0:
6706 new_layout = ALGORITHM_PARITY_0_6;
6707 break;
6708 case ALGORITHM_PARITY_N:
6709 new_layout = ALGORITHM_PARITY_N;
6710 break;
6711 default:
6712 return ERR_PTR(-EINVAL);
6713 }
6714 mddev->new_level = 6;
6715 mddev->new_layout = new_layout;
6716 mddev->delta_disks = 1;
6717 mddev->raid_disks += 1;
6718 return setup_conf(mddev);
6719}
6720
6721
NeilBrown84fc4b52011-10-11 16:49:58 +11006722static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006723{
6724 .name = "raid6",
6725 .level = 6,
6726 .owner = THIS_MODULE,
6727 .make_request = make_request,
6728 .run = run,
6729 .stop = stop,
6730 .status = status,
6731 .error_handler = error,
6732 .hot_add_disk = raid5_add_disk,
6733 .hot_remove_disk= raid5_remove_disk,
6734 .spare_active = raid5_spare_active,
6735 .sync_request = sync_request,
6736 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006737 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006738 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006739 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006740 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006741 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006742 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006743};
NeilBrown84fc4b52011-10-11 16:49:58 +11006744static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745{
6746 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006747 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 .owner = THIS_MODULE,
6749 .make_request = make_request,
6750 .run = run,
6751 .stop = stop,
6752 .status = status,
6753 .error_handler = error,
6754 .hot_add_disk = raid5_add_disk,
6755 .hot_remove_disk= raid5_remove_disk,
6756 .spare_active = raid5_spare_active,
6757 .sync_request = sync_request,
6758 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006759 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006760 .check_reshape = raid5_check_reshape,
6761 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006762 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006763 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006764 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765};
6766
NeilBrown84fc4b52011-10-11 16:49:58 +11006767static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768{
NeilBrown2604b702006-01-06 00:20:36 -08006769 .name = "raid4",
6770 .level = 4,
6771 .owner = THIS_MODULE,
6772 .make_request = make_request,
6773 .run = run,
6774 .stop = stop,
6775 .status = status,
6776 .error_handler = error,
6777 .hot_add_disk = raid5_add_disk,
6778 .hot_remove_disk= raid5_remove_disk,
6779 .spare_active = raid5_spare_active,
6780 .sync_request = sync_request,
6781 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006782 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006783 .check_reshape = raid5_check_reshape,
6784 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006785 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006786 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006787 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006788};
6789
6790static int __init raid5_init(void)
6791{
Shaohua Li851c30c2013-08-28 14:30:16 +08006792 raid5_wq = alloc_workqueue("raid5wq",
6793 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
6794 if (!raid5_wq)
6795 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07006796 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006797 register_md_personality(&raid5_personality);
6798 register_md_personality(&raid4_personality);
6799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800}
6801
NeilBrown2604b702006-01-06 00:20:36 -08006802static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803{
NeilBrown16a53ec2006-06-26 00:27:38 -07006804 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006805 unregister_md_personality(&raid5_personality);
6806 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08006807 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808}
6809
6810module_init(raid5_init);
6811module_exit(raid5_exit);
6812MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006813MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006815MODULE_ALIAS("md-raid5");
6816MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006817MODULE_ALIAS("md-level-5");
6818MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006819MODULE_ALIAS("md-personality-8"); /* RAID6 */
6820MODULE_ALIAS("md-raid6");
6821MODULE_ALIAS("md-level-6");
6822
6823/* This used to be two separate modules, they were: */
6824MODULE_ALIAS("raid5");
6825MODULE_ALIAS("raid6");