btrfs: always initialize btrfs_bio::tgtdev_map/raid_map pointers
[linux-block.git] / fs / btrfs / raid56.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
53b381b3
DW
2/*
3 * Copyright (C) 2012 Fusion-io All rights reserved.
4 * Copyright (C) 2012 Intel Corp. All rights reserved.
53b381b3 5 */
c1d7c514 6
53b381b3 7#include <linux/sched.h>
53b381b3
DW
8#include <linux/bio.h>
9#include <linux/slab.h>
53b381b3 10#include <linux/blkdev.h>
53b381b3
DW
11#include <linux/raid/pq.h>
12#include <linux/hash.h>
13#include <linux/list_sort.h>
14#include <linux/raid/xor.h>
818e010b 15#include <linux/mm.h>
53b381b3 16#include "ctree.h"
53b381b3 17#include "disk-io.h"
53b381b3
DW
18#include "volumes.h"
19#include "raid56.h"
20#include "async-thread.h"
53b381b3
DW
21
22/* set when additional merges to this rbio are not allowed */
23#define RBIO_RMW_LOCKED_BIT 1
24
4ae10b3a
CM
25/*
26 * set when this rbio is sitting in the hash, but it is just a cache
27 * of past RMW
28 */
29#define RBIO_CACHE_BIT 2
30
31/*
32 * set when it is safe to trust the stripe_pages for caching
33 */
34#define RBIO_CACHE_READY_BIT 3
35
4ae10b3a
CM
36#define RBIO_CACHE_SIZE 1024
37
8a953348
DS
38#define BTRFS_STRIPE_HASH_TABLE_BITS 11
39
40/* Used by the raid56 code to lock stripes for read/modify/write */
41struct btrfs_stripe_hash {
42 struct list_head hash_list;
43 spinlock_t lock;
44};
45
46/* Used by the raid56 code to lock stripes for read/modify/write */
47struct btrfs_stripe_hash_table {
48 struct list_head stripe_cache;
49 spinlock_t cache_lock;
50 int cache_size;
51 struct btrfs_stripe_hash table[];
52};
53
1b94b556 54enum btrfs_rbio_ops {
b4ee1782
OS
55 BTRFS_RBIO_WRITE,
56 BTRFS_RBIO_READ_REBUILD,
57 BTRFS_RBIO_PARITY_SCRUB,
58 BTRFS_RBIO_REBUILD_MISSING,
1b94b556
MX
59};
60
53b381b3
DW
61struct btrfs_raid_bio {
62 struct btrfs_fs_info *fs_info;
63 struct btrfs_bio *bbio;
64
53b381b3
DW
65 /* while we're doing rmw on a stripe
66 * we put it into a hash table so we can
67 * lock the stripe and merge more rbios
68 * into it.
69 */
70 struct list_head hash_list;
71
4ae10b3a
CM
72 /*
73 * LRU list for the stripe cache
74 */
75 struct list_head stripe_cache;
76
53b381b3
DW
77 /*
78 * for scheduling work in the helper threads
79 */
80 struct btrfs_work work;
81
82 /*
83 * bio list and bio_list_lock are used
84 * to add more bios into the stripe
85 * in hopes of avoiding the full rmw
86 */
87 struct bio_list bio_list;
88 spinlock_t bio_list_lock;
89
6ac0f488
CM
90 /* also protected by the bio_list_lock, the
91 * plug list is used by the plugging code
92 * to collect partial bios while plugged. The
93 * stripe locking code also uses it to hand off
53b381b3
DW
94 * the stripe lock to the next pending IO
95 */
96 struct list_head plug_list;
97
98 /*
99 * flags that tell us if it is safe to
100 * merge with this bio
101 */
102 unsigned long flags;
103
104 /* size of each individual stripe on disk */
105 int stripe_len;
106
107 /* number of data stripes (no p/q) */
108 int nr_data;
109
2c8cdd6e
MX
110 int real_stripes;
111
5a6ac9ea 112 int stripe_npages;
53b381b3
DW
113 /*
114 * set if we're doing a parity rebuild
115 * for a read from higher up, which is handled
116 * differently from a parity rebuild as part of
117 * rmw
118 */
1b94b556 119 enum btrfs_rbio_ops operation;
53b381b3
DW
120
121 /* first bad stripe */
122 int faila;
123
124 /* second bad stripe (for raid6 use) */
125 int failb;
126
5a6ac9ea 127 int scrubp;
53b381b3
DW
128 /*
129 * number of pages needed to represent the full
130 * stripe
131 */
132 int nr_pages;
133
134 /*
135 * size of all the bios in the bio_list. This
136 * helps us decide if the rbio maps to a full
137 * stripe or not
138 */
139 int bio_list_bytes;
140
4245215d
MX
141 int generic_bio_cnt;
142
dec95574 143 refcount_t refs;
53b381b3 144
b89e1b01
MX
145 atomic_t stripes_pending;
146
147 atomic_t error;
53b381b3
DW
148 /*
149 * these are two arrays of pointers. We allocate the
150 * rbio big enough to hold them both and setup their
151 * locations when the rbio is allocated
152 */
153
154 /* pointers to pages that we allocated for
155 * reading/writing stripes directly from the disk (including P/Q)
156 */
157 struct page **stripe_pages;
158
159 /*
160 * pointers to the pages in the bio_list. Stored
161 * here for faster lookup
162 */
163 struct page **bio_pages;
5a6ac9ea
MX
164
165 /*
166 * bitmap to record which horizontal stripe has data
167 */
168 unsigned long *dbitmap;
1389053e
KC
169
170 /* allocated with real_stripes-many pointers for finish_*() calls */
171 void **finish_pointers;
172
173 /* allocated with stripe_npages-many bits for finish_*() calls */
174 unsigned long *finish_pbitmap;
53b381b3
DW
175};
176
177static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
178static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
179static void rmw_work(struct btrfs_work *work);
180static void read_rebuild_work(struct btrfs_work *work);
53b381b3
DW
181static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
182static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
183static void __free_raid_bio(struct btrfs_raid_bio *rbio);
184static void index_rbio_pages(struct btrfs_raid_bio *rbio);
185static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
186
5a6ac9ea
MX
187static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
188 int need_check);
a81b747d 189static void scrub_parity_work(struct btrfs_work *work);
5a6ac9ea 190
ac638859
DS
191static void start_async_work(struct btrfs_raid_bio *rbio, btrfs_func_t work_func)
192{
a0cac0ec 193 btrfs_init_work(&rbio->work, work_func, NULL, NULL);
ac638859
DS
194 btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
195}
196
53b381b3
DW
197/*
198 * the stripe hash table is used for locking, and to collect
199 * bios in hopes of making a full stripe
200 */
201int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
202{
203 struct btrfs_stripe_hash_table *table;
204 struct btrfs_stripe_hash_table *x;
205 struct btrfs_stripe_hash *cur;
206 struct btrfs_stripe_hash *h;
207 int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
208 int i;
209
210 if (info->stripe_hash_table)
211 return 0;
212
83c8266a
DS
213 /*
214 * The table is large, starting with order 4 and can go as high as
215 * order 7 in case lock debugging is turned on.
216 *
217 * Try harder to allocate and fallback to vmalloc to lower the chance
218 * of a failing mount.
219 */
ee787f95 220 table = kvzalloc(struct_size(table, table, num_entries), GFP_KERNEL);
818e010b
DS
221 if (!table)
222 return -ENOMEM;
53b381b3 223
4ae10b3a
CM
224 spin_lock_init(&table->cache_lock);
225 INIT_LIST_HEAD(&table->stripe_cache);
226
53b381b3
DW
227 h = table->table;
228
229 for (i = 0; i < num_entries; i++) {
230 cur = h + i;
231 INIT_LIST_HEAD(&cur->hash_list);
232 spin_lock_init(&cur->lock);
53b381b3
DW
233 }
234
235 x = cmpxchg(&info->stripe_hash_table, NULL, table);
f749303b
WS
236 if (x)
237 kvfree(x);
53b381b3
DW
238 return 0;
239}
240
4ae10b3a
CM
241/*
242 * caching an rbio means to copy anything from the
243 * bio_pages array into the stripe_pages array. We
244 * use the page uptodate bit in the stripe cache array
245 * to indicate if it has valid data
246 *
247 * once the caching is done, we set the cache ready
248 * bit.
249 */
250static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
251{
252 int i;
253 char *s;
254 char *d;
255 int ret;
256
257 ret = alloc_rbio_pages(rbio);
258 if (ret)
259 return;
260
261 for (i = 0; i < rbio->nr_pages; i++) {
262 if (!rbio->bio_pages[i])
263 continue;
264
265 s = kmap(rbio->bio_pages[i]);
266 d = kmap(rbio->stripe_pages[i]);
267
69d24804 268 copy_page(d, s);
4ae10b3a
CM
269
270 kunmap(rbio->bio_pages[i]);
271 kunmap(rbio->stripe_pages[i]);
272 SetPageUptodate(rbio->stripe_pages[i]);
273 }
274 set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
275}
276
53b381b3
DW
277/*
278 * we hash on the first logical address of the stripe
279 */
280static int rbio_bucket(struct btrfs_raid_bio *rbio)
281{
8e5cfb55 282 u64 num = rbio->bbio->raid_map[0];
53b381b3
DW
283
284 /*
285 * we shift down quite a bit. We're using byte
286 * addressing, and most of the lower bits are zeros.
287 * This tends to upset hash_64, and it consistently
288 * returns just one or two different values.
289 *
290 * shifting off the lower bits fixes things.
291 */
292 return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
293}
294
4ae10b3a
CM
295/*
296 * stealing an rbio means taking all the uptodate pages from the stripe
297 * array in the source rbio and putting them into the destination rbio
298 */
299static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
300{
301 int i;
302 struct page *s;
303 struct page *d;
304
305 if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
306 return;
307
308 for (i = 0; i < dest->nr_pages; i++) {
309 s = src->stripe_pages[i];
310 if (!s || !PageUptodate(s)) {
311 continue;
312 }
313
314 d = dest->stripe_pages[i];
315 if (d)
316 __free_page(d);
317
318 dest->stripe_pages[i] = s;
319 src->stripe_pages[i] = NULL;
320 }
321}
322
53b381b3
DW
323/*
324 * merging means we take the bio_list from the victim and
325 * splice it into the destination. The victim should
326 * be discarded afterwards.
327 *
328 * must be called with dest->rbio_list_lock held
329 */
330static void merge_rbio(struct btrfs_raid_bio *dest,
331 struct btrfs_raid_bio *victim)
332{
333 bio_list_merge(&dest->bio_list, &victim->bio_list);
334 dest->bio_list_bytes += victim->bio_list_bytes;
4245215d 335 dest->generic_bio_cnt += victim->generic_bio_cnt;
53b381b3
DW
336 bio_list_init(&victim->bio_list);
337}
338
339/*
4ae10b3a
CM
340 * used to prune items that are in the cache. The caller
341 * must hold the hash table lock.
342 */
343static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
344{
345 int bucket = rbio_bucket(rbio);
346 struct btrfs_stripe_hash_table *table;
347 struct btrfs_stripe_hash *h;
348 int freeit = 0;
349
350 /*
351 * check the bit again under the hash table lock.
352 */
353 if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
354 return;
355
356 table = rbio->fs_info->stripe_hash_table;
357 h = table->table + bucket;
358
359 /* hold the lock for the bucket because we may be
360 * removing it from the hash table
361 */
362 spin_lock(&h->lock);
363
364 /*
365 * hold the lock for the bio list because we need
366 * to make sure the bio list is empty
367 */
368 spin_lock(&rbio->bio_list_lock);
369
370 if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
371 list_del_init(&rbio->stripe_cache);
372 table->cache_size -= 1;
373 freeit = 1;
374
375 /* if the bio list isn't empty, this rbio is
376 * still involved in an IO. We take it out
377 * of the cache list, and drop the ref that
378 * was held for the list.
379 *
380 * If the bio_list was empty, we also remove
381 * the rbio from the hash_table, and drop
382 * the corresponding ref
383 */
384 if (bio_list_empty(&rbio->bio_list)) {
385 if (!list_empty(&rbio->hash_list)) {
386 list_del_init(&rbio->hash_list);
dec95574 387 refcount_dec(&rbio->refs);
4ae10b3a
CM
388 BUG_ON(!list_empty(&rbio->plug_list));
389 }
390 }
391 }
392
393 spin_unlock(&rbio->bio_list_lock);
394 spin_unlock(&h->lock);
395
396 if (freeit)
397 __free_raid_bio(rbio);
398}
399
400/*
401 * prune a given rbio from the cache
402 */
403static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
404{
405 struct btrfs_stripe_hash_table *table;
406 unsigned long flags;
407
408 if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
409 return;
410
411 table = rbio->fs_info->stripe_hash_table;
412
413 spin_lock_irqsave(&table->cache_lock, flags);
414 __remove_rbio_from_cache(rbio);
415 spin_unlock_irqrestore(&table->cache_lock, flags);
416}
417
418/*
419 * remove everything in the cache
420 */
48a3b636 421static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
4ae10b3a
CM
422{
423 struct btrfs_stripe_hash_table *table;
424 unsigned long flags;
425 struct btrfs_raid_bio *rbio;
426
427 table = info->stripe_hash_table;
428
429 spin_lock_irqsave(&table->cache_lock, flags);
430 while (!list_empty(&table->stripe_cache)) {
431 rbio = list_entry(table->stripe_cache.next,
432 struct btrfs_raid_bio,
433 stripe_cache);
434 __remove_rbio_from_cache(rbio);
435 }
436 spin_unlock_irqrestore(&table->cache_lock, flags);
437}
438
439/*
440 * remove all cached entries and free the hash table
441 * used by unmount
53b381b3
DW
442 */
443void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
444{
445 if (!info->stripe_hash_table)
446 return;
4ae10b3a 447 btrfs_clear_rbio_cache(info);
f749303b 448 kvfree(info->stripe_hash_table);
53b381b3
DW
449 info->stripe_hash_table = NULL;
450}
451
4ae10b3a
CM
452/*
453 * insert an rbio into the stripe cache. It
454 * must have already been prepared by calling
455 * cache_rbio_pages
456 *
457 * If this rbio was already cached, it gets
458 * moved to the front of the lru.
459 *
460 * If the size of the rbio cache is too big, we
461 * prune an item.
462 */
463static void cache_rbio(struct btrfs_raid_bio *rbio)
464{
465 struct btrfs_stripe_hash_table *table;
466 unsigned long flags;
467
468 if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
469 return;
470
471 table = rbio->fs_info->stripe_hash_table;
472
473 spin_lock_irqsave(&table->cache_lock, flags);
474 spin_lock(&rbio->bio_list_lock);
475
476 /* bump our ref if we were not in the list before */
477 if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
dec95574 478 refcount_inc(&rbio->refs);
4ae10b3a
CM
479
480 if (!list_empty(&rbio->stripe_cache)){
481 list_move(&rbio->stripe_cache, &table->stripe_cache);
482 } else {
483 list_add(&rbio->stripe_cache, &table->stripe_cache);
484 table->cache_size += 1;
485 }
486
487 spin_unlock(&rbio->bio_list_lock);
488
489 if (table->cache_size > RBIO_CACHE_SIZE) {
490 struct btrfs_raid_bio *found;
491
492 found = list_entry(table->stripe_cache.prev,
493 struct btrfs_raid_bio,
494 stripe_cache);
495
496 if (found != rbio)
497 __remove_rbio_from_cache(found);
498 }
499
500 spin_unlock_irqrestore(&table->cache_lock, flags);
4ae10b3a
CM
501}
502
53b381b3
DW
503/*
504 * helper function to run the xor_blocks api. It is only
505 * able to do MAX_XOR_BLOCKS at a time, so we need to
506 * loop through.
507 */
508static void run_xor(void **pages, int src_cnt, ssize_t len)
509{
510 int src_off = 0;
511 int xor_src_cnt = 0;
512 void *dest = pages[src_cnt];
513
514 while(src_cnt > 0) {
515 xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
516 xor_blocks(xor_src_cnt, len, dest, pages + src_off);
517
518 src_cnt -= xor_src_cnt;
519 src_off += xor_src_cnt;
520 }
521}
522
523/*
176571a1
DS
524 * Returns true if the bio list inside this rbio covers an entire stripe (no
525 * rmw required).
53b381b3 526 */
176571a1 527static int rbio_is_full(struct btrfs_raid_bio *rbio)
53b381b3 528{
176571a1 529 unsigned long flags;
53b381b3
DW
530 unsigned long size = rbio->bio_list_bytes;
531 int ret = 1;
532
176571a1 533 spin_lock_irqsave(&rbio->bio_list_lock, flags);
53b381b3
DW
534 if (size != rbio->nr_data * rbio->stripe_len)
535 ret = 0;
53b381b3 536 BUG_ON(size > rbio->nr_data * rbio->stripe_len);
53b381b3 537 spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
176571a1 538
53b381b3
DW
539 return ret;
540}
541
542/*
543 * returns 1 if it is safe to merge two rbios together.
544 * The merging is safe if the two rbios correspond to
545 * the same stripe and if they are both going in the same
546 * direction (read vs write), and if neither one is
547 * locked for final IO
548 *
549 * The caller is responsible for locking such that
550 * rmw_locked is safe to test
551 */
552static int rbio_can_merge(struct btrfs_raid_bio *last,
553 struct btrfs_raid_bio *cur)
554{
555 if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
556 test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
557 return 0;
558
4ae10b3a
CM
559 /*
560 * we can't merge with cached rbios, since the
561 * idea is that when we merge the destination
562 * rbio is going to run our IO for us. We can
01327610 563 * steal from cached rbios though, other functions
4ae10b3a
CM
564 * handle that.
565 */
566 if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
567 test_bit(RBIO_CACHE_BIT, &cur->flags))
568 return 0;
569
8e5cfb55
ZL
570 if (last->bbio->raid_map[0] !=
571 cur->bbio->raid_map[0])
53b381b3
DW
572 return 0;
573
5a6ac9ea
MX
574 /* we can't merge with different operations */
575 if (last->operation != cur->operation)
576 return 0;
577 /*
578 * We've need read the full stripe from the drive.
579 * check and repair the parity and write the new results.
580 *
581 * We're not allowed to add any new bios to the
582 * bio list here, anyone else that wants to
583 * change this stripe needs to do their own rmw.
584 */
db34be19 585 if (last->operation == BTRFS_RBIO_PARITY_SCRUB)
53b381b3 586 return 0;
53b381b3 587
db34be19 588 if (last->operation == BTRFS_RBIO_REBUILD_MISSING)
b4ee1782
OS
589 return 0;
590
cc54ff62
LB
591 if (last->operation == BTRFS_RBIO_READ_REBUILD) {
592 int fa = last->faila;
593 int fb = last->failb;
594 int cur_fa = cur->faila;
595 int cur_fb = cur->failb;
596
597 if (last->faila >= last->failb) {
598 fa = last->failb;
599 fb = last->faila;
600 }
601
602 if (cur->faila >= cur->failb) {
603 cur_fa = cur->failb;
604 cur_fb = cur->faila;
605 }
606
607 if (fa != cur_fa || fb != cur_fb)
608 return 0;
609 }
53b381b3
DW
610 return 1;
611}
612
b7178a5f
ZL
613static int rbio_stripe_page_index(struct btrfs_raid_bio *rbio, int stripe,
614 int index)
615{
616 return stripe * rbio->stripe_npages + index;
617}
618
619/*
620 * these are just the pages from the rbio array, not from anything
621 * the FS sent down to us
622 */
623static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe,
624 int index)
625{
626 return rbio->stripe_pages[rbio_stripe_page_index(rbio, stripe, index)];
627}
628
53b381b3
DW
629/*
630 * helper to index into the pstripe
631 */
632static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
633{
b7178a5f 634 return rbio_stripe_page(rbio, rbio->nr_data, index);
53b381b3
DW
635}
636
637/*
638 * helper to index into the qstripe, returns null
639 * if there is no qstripe
640 */
641static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
642{
2c8cdd6e 643 if (rbio->nr_data + 1 == rbio->real_stripes)
53b381b3 644 return NULL;
b7178a5f 645 return rbio_stripe_page(rbio, rbio->nr_data + 1, index);
53b381b3
DW
646}
647
648/*
649 * The first stripe in the table for a logical address
650 * has the lock. rbios are added in one of three ways:
651 *
652 * 1) Nobody has the stripe locked yet. The rbio is given
653 * the lock and 0 is returned. The caller must start the IO
654 * themselves.
655 *
656 * 2) Someone has the stripe locked, but we're able to merge
657 * with the lock owner. The rbio is freed and the IO will
658 * start automatically along with the existing rbio. 1 is returned.
659 *
660 * 3) Someone has the stripe locked, but we're not able to merge.
661 * The rbio is added to the lock owner's plug list, or merged into
662 * an rbio already on the plug list. When the lock owner unlocks,
663 * the next rbio on the list is run and the IO is started automatically.
664 * 1 is returned
665 *
666 * If we return 0, the caller still owns the rbio and must continue with
667 * IO submission. If we return 1, the caller must assume the rbio has
668 * already been freed.
669 */
670static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
671{
721860d5 672 struct btrfs_stripe_hash *h;
53b381b3
DW
673 struct btrfs_raid_bio *cur;
674 struct btrfs_raid_bio *pending;
675 unsigned long flags;
53b381b3 676 struct btrfs_raid_bio *freeit = NULL;
4ae10b3a 677 struct btrfs_raid_bio *cache_drop = NULL;
53b381b3 678 int ret = 0;
53b381b3 679
721860d5
JT
680 h = rbio->fs_info->stripe_hash_table->table + rbio_bucket(rbio);
681
53b381b3
DW
682 spin_lock_irqsave(&h->lock, flags);
683 list_for_each_entry(cur, &h->hash_list, hash_list) {
9d6cb1b0
JT
684 if (cur->bbio->raid_map[0] != rbio->bbio->raid_map[0])
685 continue;
4ae10b3a 686
9d6cb1b0 687 spin_lock(&cur->bio_list_lock);
4ae10b3a 688
9d6cb1b0
JT
689 /* Can we steal this cached rbio's pages? */
690 if (bio_list_empty(&cur->bio_list) &&
691 list_empty(&cur->plug_list) &&
692 test_bit(RBIO_CACHE_BIT, &cur->flags) &&
693 !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
694 list_del_init(&cur->hash_list);
695 refcount_dec(&cur->refs);
53b381b3 696
9d6cb1b0
JT
697 steal_rbio(cur, rbio);
698 cache_drop = cur;
699 spin_unlock(&cur->bio_list_lock);
4ae10b3a 700
9d6cb1b0
JT
701 goto lockit;
702 }
53b381b3 703
9d6cb1b0
JT
704 /* Can we merge into the lock owner? */
705 if (rbio_can_merge(cur, rbio)) {
706 merge_rbio(cur, rbio);
53b381b3 707 spin_unlock(&cur->bio_list_lock);
9d6cb1b0 708 freeit = rbio;
53b381b3
DW
709 ret = 1;
710 goto out;
711 }
9d6cb1b0
JT
712
713
714 /*
715 * We couldn't merge with the running rbio, see if we can merge
716 * with the pending ones. We don't have to check for rmw_locked
717 * because there is no way they are inside finish_rmw right now
718 */
719 list_for_each_entry(pending, &cur->plug_list, plug_list) {
720 if (rbio_can_merge(pending, rbio)) {
721 merge_rbio(pending, rbio);
722 spin_unlock(&cur->bio_list_lock);
723 freeit = rbio;
724 ret = 1;
725 goto out;
726 }
727 }
728
729 /*
730 * No merging, put us on the tail of the plug list, our rbio
731 * will be started with the currently running rbio unlocks
732 */
733 list_add_tail(&rbio->plug_list, &cur->plug_list);
734 spin_unlock(&cur->bio_list_lock);
735 ret = 1;
736 goto out;
53b381b3 737 }
4ae10b3a 738lockit:
dec95574 739 refcount_inc(&rbio->refs);
53b381b3
DW
740 list_add(&rbio->hash_list, &h->hash_list);
741out:
742 spin_unlock_irqrestore(&h->lock, flags);
4ae10b3a
CM
743 if (cache_drop)
744 remove_rbio_from_cache(cache_drop);
53b381b3
DW
745 if (freeit)
746 __free_raid_bio(freeit);
747 return ret;
748}
749
750/*
751 * called as rmw or parity rebuild is completed. If the plug list has more
752 * rbios waiting for this stripe, the next one on the list will be started
753 */
754static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
755{
756 int bucket;
757 struct btrfs_stripe_hash *h;
758 unsigned long flags;
4ae10b3a 759 int keep_cache = 0;
53b381b3
DW
760
761 bucket = rbio_bucket(rbio);
762 h = rbio->fs_info->stripe_hash_table->table + bucket;
763
4ae10b3a
CM
764 if (list_empty(&rbio->plug_list))
765 cache_rbio(rbio);
766
53b381b3
DW
767 spin_lock_irqsave(&h->lock, flags);
768 spin_lock(&rbio->bio_list_lock);
769
770 if (!list_empty(&rbio->hash_list)) {
4ae10b3a
CM
771 /*
772 * if we're still cached and there is no other IO
773 * to perform, just leave this rbio here for others
774 * to steal from later
775 */
776 if (list_empty(&rbio->plug_list) &&
777 test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
778 keep_cache = 1;
779 clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
780 BUG_ON(!bio_list_empty(&rbio->bio_list));
781 goto done;
782 }
53b381b3
DW
783
784 list_del_init(&rbio->hash_list);
dec95574 785 refcount_dec(&rbio->refs);
53b381b3
DW
786
787 /*
788 * we use the plug list to hold all the rbios
789 * waiting for the chance to lock this stripe.
790 * hand the lock over to one of them.
791 */
792 if (!list_empty(&rbio->plug_list)) {
793 struct btrfs_raid_bio *next;
794 struct list_head *head = rbio->plug_list.next;
795
796 next = list_entry(head, struct btrfs_raid_bio,
797 plug_list);
798
799 list_del_init(&rbio->plug_list);
800
801 list_add(&next->hash_list, &h->hash_list);
dec95574 802 refcount_inc(&next->refs);
53b381b3
DW
803 spin_unlock(&rbio->bio_list_lock);
804 spin_unlock_irqrestore(&h->lock, flags);
805
1b94b556 806 if (next->operation == BTRFS_RBIO_READ_REBUILD)
e66d8d5a 807 start_async_work(next, read_rebuild_work);
b4ee1782
OS
808 else if (next->operation == BTRFS_RBIO_REBUILD_MISSING) {
809 steal_rbio(rbio, next);
e66d8d5a 810 start_async_work(next, read_rebuild_work);
b4ee1782 811 } else if (next->operation == BTRFS_RBIO_WRITE) {
4ae10b3a 812 steal_rbio(rbio, next);
cf6a4a75 813 start_async_work(next, rmw_work);
5a6ac9ea
MX
814 } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
815 steal_rbio(rbio, next);
a81b747d 816 start_async_work(next, scrub_parity_work);
4ae10b3a 817 }
53b381b3
DW
818
819 goto done_nolock;
53b381b3
DW
820 }
821 }
4ae10b3a 822done:
53b381b3
DW
823 spin_unlock(&rbio->bio_list_lock);
824 spin_unlock_irqrestore(&h->lock, flags);
825
826done_nolock:
4ae10b3a
CM
827 if (!keep_cache)
828 remove_rbio_from_cache(rbio);
53b381b3
DW
829}
830
831static void __free_raid_bio(struct btrfs_raid_bio *rbio)
832{
833 int i;
834
dec95574 835 if (!refcount_dec_and_test(&rbio->refs))
53b381b3
DW
836 return;
837
4ae10b3a 838 WARN_ON(!list_empty(&rbio->stripe_cache));
53b381b3
DW
839 WARN_ON(!list_empty(&rbio->hash_list));
840 WARN_ON(!bio_list_empty(&rbio->bio_list));
841
842 for (i = 0; i < rbio->nr_pages; i++) {
843 if (rbio->stripe_pages[i]) {
844 __free_page(rbio->stripe_pages[i]);
845 rbio->stripe_pages[i] = NULL;
846 }
847 }
af8e2d1d 848
6e9606d2 849 btrfs_put_bbio(rbio->bbio);
53b381b3
DW
850 kfree(rbio);
851}
852
7583d8d0 853static void rbio_endio_bio_list(struct bio *cur, blk_status_t err)
53b381b3 854{
7583d8d0
LB
855 struct bio *next;
856
857 while (cur) {
858 next = cur->bi_next;
859 cur->bi_next = NULL;
860 cur->bi_status = err;
861 bio_endio(cur);
862 cur = next;
863 }
53b381b3
DW
864}
865
866/*
867 * this frees the rbio and runs through all the bios in the
868 * bio_list and calls end_io on them
869 */
4e4cbee9 870static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, blk_status_t err)
53b381b3
DW
871{
872 struct bio *cur = bio_list_get(&rbio->bio_list);
7583d8d0 873 struct bio *extra;
4245215d
MX
874
875 if (rbio->generic_bio_cnt)
876 btrfs_bio_counter_sub(rbio->fs_info, rbio->generic_bio_cnt);
877
7583d8d0
LB
878 /*
879 * At this moment, rbio->bio_list is empty, however since rbio does not
880 * always have RBIO_RMW_LOCKED_BIT set and rbio is still linked on the
881 * hash list, rbio may be merged with others so that rbio->bio_list
882 * becomes non-empty.
883 * Once unlock_stripe() is done, rbio->bio_list will not be updated any
884 * more and we can call bio_endio() on all queued bios.
885 */
886 unlock_stripe(rbio);
887 extra = bio_list_get(&rbio->bio_list);
888 __free_raid_bio(rbio);
53b381b3 889
7583d8d0
LB
890 rbio_endio_bio_list(cur, err);
891 if (extra)
892 rbio_endio_bio_list(extra, err);
53b381b3
DW
893}
894
895/*
896 * end io function used by finish_rmw. When we finally
897 * get here, we've written a full stripe
898 */
4246a0b6 899static void raid_write_end_io(struct bio *bio)
53b381b3
DW
900{
901 struct btrfs_raid_bio *rbio = bio->bi_private;
4e4cbee9 902 blk_status_t err = bio->bi_status;
a6111d11 903 int max_errors;
53b381b3
DW
904
905 if (err)
906 fail_bio_stripe(rbio, bio);
907
908 bio_put(bio);
909
b89e1b01 910 if (!atomic_dec_and_test(&rbio->stripes_pending))
53b381b3
DW
911 return;
912
58efbc9f 913 err = BLK_STS_OK;
53b381b3
DW
914
915 /* OK, we have read all the stripes we need to. */
a6111d11
ZL
916 max_errors = (rbio->operation == BTRFS_RBIO_PARITY_SCRUB) ?
917 0 : rbio->bbio->max_errors;
918 if (atomic_read(&rbio->error) > max_errors)
4e4cbee9 919 err = BLK_STS_IOERR;
53b381b3 920
4246a0b6 921 rbio_orig_end_io(rbio, err);
53b381b3
DW
922}
923
924/*
925 * the read/modify/write code wants to use the original bio for
926 * any pages it included, and then use the rbio for everything
927 * else. This function decides if a given index (stripe number)
928 * and page number in that stripe fall inside the original bio
929 * or the rbio.
930 *
931 * if you set bio_list_only, you'll get a NULL back for any ranges
932 * that are outside the bio_list
933 *
934 * This doesn't take any refs on anything, you get a bare page pointer
935 * and the caller must bump refs as required.
936 *
937 * You must call index_rbio_pages once before you can trust
938 * the answers from this function.
939 */
940static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
941 int index, int pagenr, int bio_list_only)
942{
943 int chunk_page;
944 struct page *p = NULL;
945
946 chunk_page = index * (rbio->stripe_len >> PAGE_SHIFT) + pagenr;
947
948 spin_lock_irq(&rbio->bio_list_lock);
949 p = rbio->bio_pages[chunk_page];
950 spin_unlock_irq(&rbio->bio_list_lock);
951
952 if (p || bio_list_only)
953 return p;
954
955 return rbio->stripe_pages[chunk_page];
956}
957
958/*
959 * number of pages we need for the entire stripe across all the
960 * drives
961 */
962static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
963{
09cbfeaf 964 return DIV_ROUND_UP(stripe_len, PAGE_SIZE) * nr_stripes;
53b381b3
DW
965}
966
967/*
968 * allocation and initial setup for the btrfs_raid_bio. Not
969 * this does not allocate any pages for rbio->pages.
970 */
2ff7e61e
JM
971static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
972 struct btrfs_bio *bbio,
973 u64 stripe_len)
53b381b3
DW
974{
975 struct btrfs_raid_bio *rbio;
976 int nr_data = 0;
2c8cdd6e
MX
977 int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
978 int num_pages = rbio_nr_pages(stripe_len, real_stripes);
5a6ac9ea 979 int stripe_npages = DIV_ROUND_UP(stripe_len, PAGE_SIZE);
53b381b3
DW
980 void *p;
981
1389053e
KC
982 rbio = kzalloc(sizeof(*rbio) +
983 sizeof(*rbio->stripe_pages) * num_pages +
984 sizeof(*rbio->bio_pages) * num_pages +
985 sizeof(*rbio->finish_pointers) * real_stripes +
986 sizeof(*rbio->dbitmap) * BITS_TO_LONGS(stripe_npages) +
987 sizeof(*rbio->finish_pbitmap) *
988 BITS_TO_LONGS(stripe_npages),
989 GFP_NOFS);
af8e2d1d 990 if (!rbio)
53b381b3 991 return ERR_PTR(-ENOMEM);
53b381b3
DW
992
993 bio_list_init(&rbio->bio_list);
994 INIT_LIST_HEAD(&rbio->plug_list);
995 spin_lock_init(&rbio->bio_list_lock);
4ae10b3a 996 INIT_LIST_HEAD(&rbio->stripe_cache);
53b381b3
DW
997 INIT_LIST_HEAD(&rbio->hash_list);
998 rbio->bbio = bbio;
2ff7e61e 999 rbio->fs_info = fs_info;
53b381b3
DW
1000 rbio->stripe_len = stripe_len;
1001 rbio->nr_pages = num_pages;
2c8cdd6e 1002 rbio->real_stripes = real_stripes;
5a6ac9ea 1003 rbio->stripe_npages = stripe_npages;
53b381b3
DW
1004 rbio->faila = -1;
1005 rbio->failb = -1;
dec95574 1006 refcount_set(&rbio->refs, 1);
b89e1b01
MX
1007 atomic_set(&rbio->error, 0);
1008 atomic_set(&rbio->stripes_pending, 0);
53b381b3
DW
1009
1010 /*
1389053e 1011 * the stripe_pages, bio_pages, etc arrays point to the extra
53b381b3
DW
1012 * memory we allocated past the end of the rbio
1013 */
1014 p = rbio + 1;
1389053e
KC
1015#define CONSUME_ALLOC(ptr, count) do { \
1016 ptr = p; \
1017 p = (unsigned char *)p + sizeof(*(ptr)) * (count); \
1018 } while (0)
1019 CONSUME_ALLOC(rbio->stripe_pages, num_pages);
1020 CONSUME_ALLOC(rbio->bio_pages, num_pages);
1021 CONSUME_ALLOC(rbio->finish_pointers, real_stripes);
1022 CONSUME_ALLOC(rbio->dbitmap, BITS_TO_LONGS(stripe_npages));
1023 CONSUME_ALLOC(rbio->finish_pbitmap, BITS_TO_LONGS(stripe_npages));
1024#undef CONSUME_ALLOC
53b381b3 1025
10f11900
ZL
1026 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1027 nr_data = real_stripes - 1;
1028 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
2c8cdd6e 1029 nr_data = real_stripes - 2;
53b381b3 1030 else
10f11900 1031 BUG();
53b381b3
DW
1032
1033 rbio->nr_data = nr_data;
1034 return rbio;
1035}
1036
1037/* allocate pages for all the stripes in the bio, including parity */
1038static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
1039{
1040 int i;
1041 struct page *page;
1042
1043 for (i = 0; i < rbio->nr_pages; i++) {
1044 if (rbio->stripe_pages[i])
1045 continue;
1046 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1047 if (!page)
1048 return -ENOMEM;
1049 rbio->stripe_pages[i] = page;
53b381b3
DW
1050 }
1051 return 0;
1052}
1053
b7178a5f 1054/* only allocate pages for p/q stripes */
53b381b3
DW
1055static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
1056{
1057 int i;
1058 struct page *page;
1059
b7178a5f 1060 i = rbio_stripe_page_index(rbio, rbio->nr_data, 0);
53b381b3
DW
1061
1062 for (; i < rbio->nr_pages; i++) {
1063 if (rbio->stripe_pages[i])
1064 continue;
1065 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1066 if (!page)
1067 return -ENOMEM;
1068 rbio->stripe_pages[i] = page;
1069 }
1070 return 0;
1071}
1072
1073/*
1074 * add a single page from a specific stripe into our list of bios for IO
1075 * this will try to merge into existing bios if possible, and returns
1076 * zero if all went well.
1077 */
48a3b636
ES
1078static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
1079 struct bio_list *bio_list,
1080 struct page *page,
1081 int stripe_nr,
1082 unsigned long page_index,
1083 unsigned long bio_max_len)
53b381b3
DW
1084{
1085 struct bio *last = bio_list->tail;
1086 u64 last_end = 0;
1087 int ret;
1088 struct bio *bio;
1089 struct btrfs_bio_stripe *stripe;
1090 u64 disk_start;
1091
1092 stripe = &rbio->bbio->stripes[stripe_nr];
09cbfeaf 1093 disk_start = stripe->physical + (page_index << PAGE_SHIFT);
53b381b3
DW
1094
1095 /* if the device is missing, just fail this stripe */
1096 if (!stripe->dev->bdev)
1097 return fail_rbio_index(rbio, stripe_nr);
1098
1099 /* see if we can add this page onto our existing bio */
1100 if (last) {
4f024f37
KO
1101 last_end = (u64)last->bi_iter.bi_sector << 9;
1102 last_end += last->bi_iter.bi_size;
53b381b3
DW
1103
1104 /*
1105 * we can't merge these if they are from different
1106 * devices or if they are not contiguous
1107 */
1108 if (last_end == disk_start && stripe->dev->bdev &&
4e4cbee9 1109 !last->bi_status &&
74d46992
CH
1110 last->bi_disk == stripe->dev->bdev->bd_disk &&
1111 last->bi_partno == stripe->dev->bdev->bd_partno) {
09cbfeaf
KS
1112 ret = bio_add_page(last, page, PAGE_SIZE, 0);
1113 if (ret == PAGE_SIZE)
53b381b3
DW
1114 return 0;
1115 }
1116 }
1117
1118 /* put a new bio on the list */
c5e4c3d7 1119 bio = btrfs_io_bio_alloc(bio_max_len >> PAGE_SHIFT ?: 1);
c31efbdf 1120 btrfs_io_bio(bio)->device = stripe->dev;
4f024f37 1121 bio->bi_iter.bi_size = 0;
74d46992 1122 bio_set_dev(bio, stripe->dev->bdev);
4f024f37 1123 bio->bi_iter.bi_sector = disk_start >> 9;
53b381b3 1124
09cbfeaf 1125 bio_add_page(bio, page, PAGE_SIZE, 0);
53b381b3
DW
1126 bio_list_add(bio_list, bio);
1127 return 0;
1128}
1129
1130/*
1131 * while we're doing the read/modify/write cycle, we could
1132 * have errors in reading pages off the disk. This checks
1133 * for errors and if we're not able to read the page it'll
1134 * trigger parity reconstruction. The rmw will be finished
1135 * after we've reconstructed the failed stripes
1136 */
1137static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
1138{
1139 if (rbio->faila >= 0 || rbio->failb >= 0) {
2c8cdd6e 1140 BUG_ON(rbio->faila == rbio->real_stripes - 1);
53b381b3
DW
1141 __raid56_parity_recover(rbio);
1142 } else {
1143 finish_rmw(rbio);
1144 }
1145}
1146
53b381b3
DW
1147/*
1148 * helper function to walk our bio list and populate the bio_pages array with
1149 * the result. This seems expensive, but it is faster than constantly
1150 * searching through the bio list as we setup the IO in finish_rmw or stripe
1151 * reconstruction.
1152 *
1153 * This must be called before you trust the answers from page_in_rbio
1154 */
1155static void index_rbio_pages(struct btrfs_raid_bio *rbio)
1156{
1157 struct bio *bio;
1158 u64 start;
1159 unsigned long stripe_offset;
1160 unsigned long page_index;
53b381b3
DW
1161
1162 spin_lock_irq(&rbio->bio_list_lock);
1163 bio_list_for_each(bio, &rbio->bio_list) {
6592e58c
FM
1164 struct bio_vec bvec;
1165 struct bvec_iter iter;
1166 int i = 0;
1167
4f024f37 1168 start = (u64)bio->bi_iter.bi_sector << 9;
8e5cfb55 1169 stripe_offset = start - rbio->bbio->raid_map[0];
09cbfeaf 1170 page_index = stripe_offset >> PAGE_SHIFT;
53b381b3 1171
6592e58c
FM
1172 if (bio_flagged(bio, BIO_CLONED))
1173 bio->bi_iter = btrfs_io_bio(bio)->iter;
1174
1175 bio_for_each_segment(bvec, bio, iter) {
1176 rbio->bio_pages[page_index + i] = bvec.bv_page;
1177 i++;
1178 }
53b381b3
DW
1179 }
1180 spin_unlock_irq(&rbio->bio_list_lock);
1181}
1182
1183/*
1184 * this is called from one of two situations. We either
1185 * have a full stripe from the higher layers, or we've read all
1186 * the missing bits off disk.
1187 *
1188 * This will calculate the parity and then send down any
1189 * changed blocks.
1190 */
1191static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1192{
1193 struct btrfs_bio *bbio = rbio->bbio;
1389053e 1194 void **pointers = rbio->finish_pointers;
53b381b3
DW
1195 int nr_data = rbio->nr_data;
1196 int stripe;
1197 int pagenr;
c17af965 1198 bool has_qstripe;
53b381b3
DW
1199 struct bio_list bio_list;
1200 struct bio *bio;
53b381b3
DW
1201 int ret;
1202
1203 bio_list_init(&bio_list);
1204
c17af965
DS
1205 if (rbio->real_stripes - rbio->nr_data == 1)
1206 has_qstripe = false;
1207 else if (rbio->real_stripes - rbio->nr_data == 2)
1208 has_qstripe = true;
1209 else
53b381b3 1210 BUG();
53b381b3
DW
1211
1212 /* at this point we either have a full stripe,
1213 * or we've read the full stripe from the drive.
1214 * recalculate the parity and write the new results.
1215 *
1216 * We're not allowed to add any new bios to the
1217 * bio list here, anyone else that wants to
1218 * change this stripe needs to do their own rmw.
1219 */
1220 spin_lock_irq(&rbio->bio_list_lock);
1221 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1222 spin_unlock_irq(&rbio->bio_list_lock);
1223
b89e1b01 1224 atomic_set(&rbio->error, 0);
53b381b3
DW
1225
1226 /*
1227 * now that we've set rmw_locked, run through the
1228 * bio list one last time and map the page pointers
4ae10b3a
CM
1229 *
1230 * We don't cache full rbios because we're assuming
1231 * the higher layers are unlikely to use this area of
1232 * the disk again soon. If they do use it again,
1233 * hopefully they will send another full bio.
53b381b3
DW
1234 */
1235 index_rbio_pages(rbio);
4ae10b3a
CM
1236 if (!rbio_is_full(rbio))
1237 cache_rbio_pages(rbio);
1238 else
1239 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
53b381b3 1240
915e2290 1241 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
53b381b3
DW
1242 struct page *p;
1243 /* first collect one page from each data stripe */
1244 for (stripe = 0; stripe < nr_data; stripe++) {
1245 p = page_in_rbio(rbio, stripe, pagenr, 0);
1246 pointers[stripe] = kmap(p);
1247 }
1248
1249 /* then add the parity stripe */
1250 p = rbio_pstripe_page(rbio, pagenr);
1251 SetPageUptodate(p);
1252 pointers[stripe++] = kmap(p);
1253
c17af965 1254 if (has_qstripe) {
53b381b3
DW
1255
1256 /*
1257 * raid6, add the qstripe and call the
1258 * library function to fill in our p/q
1259 */
1260 p = rbio_qstripe_page(rbio, pagenr);
1261 SetPageUptodate(p);
1262 pointers[stripe++] = kmap(p);
1263
2c8cdd6e 1264 raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
53b381b3
DW
1265 pointers);
1266 } else {
1267 /* raid5 */
69d24804 1268 copy_page(pointers[nr_data], pointers[0]);
09cbfeaf 1269 run_xor(pointers + 1, nr_data - 1, PAGE_SIZE);
53b381b3
DW
1270 }
1271
1272
2c8cdd6e 1273 for (stripe = 0; stripe < rbio->real_stripes; stripe++)
53b381b3
DW
1274 kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
1275 }
1276
1277 /*
1278 * time to start writing. Make bios for everything from the
1279 * higher layers (the bio_list in our rbio) and our p/q. Ignore
1280 * everything else.
1281 */
2c8cdd6e 1282 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
915e2290 1283 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
53b381b3
DW
1284 struct page *page;
1285 if (stripe < rbio->nr_data) {
1286 page = page_in_rbio(rbio, stripe, pagenr, 1);
1287 if (!page)
1288 continue;
1289 } else {
1290 page = rbio_stripe_page(rbio, stripe, pagenr);
1291 }
1292
1293 ret = rbio_add_io_page(rbio, &bio_list,
1294 page, stripe, pagenr, rbio->stripe_len);
1295 if (ret)
1296 goto cleanup;
1297 }
1298 }
1299
2c8cdd6e
MX
1300 if (likely(!bbio->num_tgtdevs))
1301 goto write_data;
1302
1303 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1304 if (!bbio->tgtdev_map[stripe])
1305 continue;
1306
915e2290 1307 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
2c8cdd6e
MX
1308 struct page *page;
1309 if (stripe < rbio->nr_data) {
1310 page = page_in_rbio(rbio, stripe, pagenr, 1);
1311 if (!page)
1312 continue;
1313 } else {
1314 page = rbio_stripe_page(rbio, stripe, pagenr);
1315 }
1316
1317 ret = rbio_add_io_page(rbio, &bio_list, page,
1318 rbio->bbio->tgtdev_map[stripe],
1319 pagenr, rbio->stripe_len);
1320 if (ret)
1321 goto cleanup;
1322 }
1323 }
1324
1325write_data:
b89e1b01
MX
1326 atomic_set(&rbio->stripes_pending, bio_list_size(&bio_list));
1327 BUG_ON(atomic_read(&rbio->stripes_pending) == 0);
53b381b3
DW
1328
1329 while (1) {
1330 bio = bio_list_pop(&bio_list);
1331 if (!bio)
1332 break;
1333
1334 bio->bi_private = rbio;
1335 bio->bi_end_io = raid_write_end_io;
ebcc3263 1336 bio->bi_opf = REQ_OP_WRITE;
4e49ea4a
MC
1337
1338 submit_bio(bio);
53b381b3
DW
1339 }
1340 return;
1341
1342cleanup:
58efbc9f 1343 rbio_orig_end_io(rbio, BLK_STS_IOERR);
785884fc
LB
1344
1345 while ((bio = bio_list_pop(&bio_list)))
1346 bio_put(bio);
53b381b3
DW
1347}
1348
1349/*
1350 * helper to find the stripe number for a given bio. Used to figure out which
1351 * stripe has failed. This expects the bio to correspond to a physical disk,
1352 * so it looks up based on physical sector numbers.
1353 */
1354static int find_bio_stripe(struct btrfs_raid_bio *rbio,
1355 struct bio *bio)
1356{
4f024f37 1357 u64 physical = bio->bi_iter.bi_sector;
53b381b3
DW
1358 u64 stripe_start;
1359 int i;
1360 struct btrfs_bio_stripe *stripe;
1361
1362 physical <<= 9;
1363
1364 for (i = 0; i < rbio->bbio->num_stripes; i++) {
1365 stripe = &rbio->bbio->stripes[i];
1366 stripe_start = stripe->physical;
1367 if (physical >= stripe_start &&
2c8cdd6e 1368 physical < stripe_start + rbio->stripe_len &&
047fdea6 1369 stripe->dev->bdev &&
74d46992
CH
1370 bio->bi_disk == stripe->dev->bdev->bd_disk &&
1371 bio->bi_partno == stripe->dev->bdev->bd_partno) {
53b381b3
DW
1372 return i;
1373 }
1374 }
1375 return -1;
1376}
1377
1378/*
1379 * helper to find the stripe number for a given
1380 * bio (before mapping). Used to figure out which stripe has
1381 * failed. This looks up based on logical block numbers.
1382 */
1383static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
1384 struct bio *bio)
1385{
4f024f37 1386 u64 logical = bio->bi_iter.bi_sector;
53b381b3
DW
1387 u64 stripe_start;
1388 int i;
1389
1390 logical <<= 9;
1391
1392 for (i = 0; i < rbio->nr_data; i++) {
8e5cfb55 1393 stripe_start = rbio->bbio->raid_map[i];
53b381b3
DW
1394 if (logical >= stripe_start &&
1395 logical < stripe_start + rbio->stripe_len) {
1396 return i;
1397 }
1398 }
1399 return -1;
1400}
1401
1402/*
1403 * returns -EIO if we had too many failures
1404 */
1405static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
1406{
1407 unsigned long flags;
1408 int ret = 0;
1409
1410 spin_lock_irqsave(&rbio->bio_list_lock, flags);
1411
1412 /* we already know this stripe is bad, move on */
1413 if (rbio->faila == failed || rbio->failb == failed)
1414 goto out;
1415
1416 if (rbio->faila == -1) {
1417 /* first failure on this rbio */
1418 rbio->faila = failed;
b89e1b01 1419 atomic_inc(&rbio->error);
53b381b3
DW
1420 } else if (rbio->failb == -1) {
1421 /* second failure on this rbio */
1422 rbio->failb = failed;
b89e1b01 1423 atomic_inc(&rbio->error);
53b381b3
DW
1424 } else {
1425 ret = -EIO;
1426 }
1427out:
1428 spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
1429
1430 return ret;
1431}
1432
1433/*
1434 * helper to fail a stripe based on a physical disk
1435 * bio.
1436 */
1437static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
1438 struct bio *bio)
1439{
1440 int failed = find_bio_stripe(rbio, bio);
1441
1442 if (failed < 0)
1443 return -EIO;
1444
1445 return fail_rbio_index(rbio, failed);
1446}
1447
1448/*
1449 * this sets each page in the bio uptodate. It should only be used on private
1450 * rbio pages, nothing that comes in from the higher layers
1451 */
1452static void set_bio_pages_uptodate(struct bio *bio)
1453{
0198e5b7 1454 struct bio_vec *bvec;
6dc4f100 1455 struct bvec_iter_all iter_all;
6592e58c 1456
0198e5b7 1457 ASSERT(!bio_flagged(bio, BIO_CLONED));
53b381b3 1458
2b070cfe 1459 bio_for_each_segment_all(bvec, bio, iter_all)
0198e5b7 1460 SetPageUptodate(bvec->bv_page);
53b381b3
DW
1461}
1462
1463/*
1464 * end io for the read phase of the rmw cycle. All the bios here are physical
1465 * stripe bios we've read from the disk so we can recalculate the parity of the
1466 * stripe.
1467 *
1468 * This will usually kick off finish_rmw once all the bios are read in, but it
1469 * may trigger parity reconstruction if we had any errors along the way
1470 */
4246a0b6 1471static void raid_rmw_end_io(struct bio *bio)
53b381b3
DW
1472{
1473 struct btrfs_raid_bio *rbio = bio->bi_private;
1474
4e4cbee9 1475 if (bio->bi_status)
53b381b3
DW
1476 fail_bio_stripe(rbio, bio);
1477 else
1478 set_bio_pages_uptodate(bio);
1479
1480 bio_put(bio);
1481
b89e1b01 1482 if (!atomic_dec_and_test(&rbio->stripes_pending))
53b381b3
DW
1483 return;
1484
b89e1b01 1485 if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
53b381b3
DW
1486 goto cleanup;
1487
1488 /*
1489 * this will normally call finish_rmw to start our write
1490 * but if there are any failed stripes we'll reconstruct
1491 * from parity first
1492 */
1493 validate_rbio_for_rmw(rbio);
1494 return;
1495
1496cleanup:
1497
58efbc9f 1498 rbio_orig_end_io(rbio, BLK_STS_IOERR);
53b381b3
DW
1499}
1500
53b381b3
DW
1501/*
1502 * the stripe must be locked by the caller. It will
1503 * unlock after all the writes are done
1504 */
1505static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1506{
1507 int bios_to_read = 0;
53b381b3
DW
1508 struct bio_list bio_list;
1509 int ret;
53b381b3
DW
1510 int pagenr;
1511 int stripe;
1512 struct bio *bio;
1513
1514 bio_list_init(&bio_list);
1515
1516 ret = alloc_rbio_pages(rbio);
1517 if (ret)
1518 goto cleanup;
1519
1520 index_rbio_pages(rbio);
1521
b89e1b01 1522 atomic_set(&rbio->error, 0);
53b381b3
DW
1523 /*
1524 * build a list of bios to read all the missing parts of this
1525 * stripe
1526 */
1527 for (stripe = 0; stripe < rbio->nr_data; stripe++) {
915e2290 1528 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
53b381b3
DW
1529 struct page *page;
1530 /*
1531 * we want to find all the pages missing from
1532 * the rbio and read them from the disk. If
1533 * page_in_rbio finds a page in the bio list
1534 * we don't need to read it off the stripe.
1535 */
1536 page = page_in_rbio(rbio, stripe, pagenr, 1);
1537 if (page)
1538 continue;
1539
1540 page = rbio_stripe_page(rbio, stripe, pagenr);
4ae10b3a
CM
1541 /*
1542 * the bio cache may have handed us an uptodate
1543 * page. If so, be happy and use it
1544 */
1545 if (PageUptodate(page))
1546 continue;
1547
53b381b3
DW
1548 ret = rbio_add_io_page(rbio, &bio_list, page,
1549 stripe, pagenr, rbio->stripe_len);
1550 if (ret)
1551 goto cleanup;
1552 }
1553 }
1554
1555 bios_to_read = bio_list_size(&bio_list);
1556 if (!bios_to_read) {
1557 /*
1558 * this can happen if others have merged with
1559 * us, it means there is nothing left to read.
1560 * But if there are missing devices it may not be
1561 * safe to do the full stripe write yet.
1562 */
1563 goto finish;
1564 }
1565
1566 /*
1567 * the bbio may be freed once we submit the last bio. Make sure
1568 * not to touch it after that
1569 */
b89e1b01 1570 atomic_set(&rbio->stripes_pending, bios_to_read);
53b381b3
DW
1571 while (1) {
1572 bio = bio_list_pop(&bio_list);
1573 if (!bio)
1574 break;
1575
1576 bio->bi_private = rbio;
1577 bio->bi_end_io = raid_rmw_end_io;
ebcc3263 1578 bio->bi_opf = REQ_OP_READ;
53b381b3 1579
0b246afa 1580 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
53b381b3 1581
4e49ea4a 1582 submit_bio(bio);
53b381b3
DW
1583 }
1584 /* the actual write will happen once the reads are done */
1585 return 0;
1586
1587cleanup:
58efbc9f 1588 rbio_orig_end_io(rbio, BLK_STS_IOERR);
785884fc
LB
1589
1590 while ((bio = bio_list_pop(&bio_list)))
1591 bio_put(bio);
1592
53b381b3
DW
1593 return -EIO;
1594
1595finish:
1596 validate_rbio_for_rmw(rbio);
1597 return 0;
1598}
1599
1600/*
1601 * if the upper layers pass in a full stripe, we thank them by only allocating
1602 * enough pages to hold the parity, and sending it all down quickly.
1603 */
1604static int full_stripe_write(struct btrfs_raid_bio *rbio)
1605{
1606 int ret;
1607
1608 ret = alloc_rbio_parity_pages(rbio);
3cd846d1
MX
1609 if (ret) {
1610 __free_raid_bio(rbio);
53b381b3 1611 return ret;
3cd846d1 1612 }
53b381b3
DW
1613
1614 ret = lock_stripe_add(rbio);
1615 if (ret == 0)
1616 finish_rmw(rbio);
1617 return 0;
1618}
1619
1620/*
1621 * partial stripe writes get handed over to async helpers.
1622 * We're really hoping to merge a few more writes into this
1623 * rbio before calculating new parity
1624 */
1625static int partial_stripe_write(struct btrfs_raid_bio *rbio)
1626{
1627 int ret;
1628
1629 ret = lock_stripe_add(rbio);
1630 if (ret == 0)
cf6a4a75 1631 start_async_work(rbio, rmw_work);
53b381b3
DW
1632 return 0;
1633}
1634
1635/*
1636 * sometimes while we were reading from the drive to
1637 * recalculate parity, enough new bios come into create
1638 * a full stripe. So we do a check here to see if we can
1639 * go directly to finish_rmw
1640 */
1641static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
1642{
1643 /* head off into rmw land if we don't have a full stripe */
1644 if (!rbio_is_full(rbio))
1645 return partial_stripe_write(rbio);
1646 return full_stripe_write(rbio);
1647}
1648
6ac0f488
CM
1649/*
1650 * We use plugging call backs to collect full stripes.
1651 * Any time we get a partial stripe write while plugged
1652 * we collect it into a list. When the unplug comes down,
1653 * we sort the list by logical block number and merge
1654 * everything we can into the same rbios
1655 */
1656struct btrfs_plug_cb {
1657 struct blk_plug_cb cb;
1658 struct btrfs_fs_info *info;
1659 struct list_head rbio_list;
1660 struct btrfs_work work;
1661};
1662
1663/*
1664 * rbios on the plug list are sorted for easier merging.
1665 */
1666static int plug_cmp(void *priv, struct list_head *a, struct list_head *b)
1667{
1668 struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
1669 plug_list);
1670 struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
1671 plug_list);
4f024f37
KO
1672 u64 a_sector = ra->bio_list.head->bi_iter.bi_sector;
1673 u64 b_sector = rb->bio_list.head->bi_iter.bi_sector;
6ac0f488
CM
1674
1675 if (a_sector < b_sector)
1676 return -1;
1677 if (a_sector > b_sector)
1678 return 1;
1679 return 0;
1680}
1681
1682static void run_plug(struct btrfs_plug_cb *plug)
1683{
1684 struct btrfs_raid_bio *cur;
1685 struct btrfs_raid_bio *last = NULL;
1686
1687 /*
1688 * sort our plug list then try to merge
1689 * everything we can in hopes of creating full
1690 * stripes.
1691 */
1692 list_sort(NULL, &plug->rbio_list, plug_cmp);
1693 while (!list_empty(&plug->rbio_list)) {
1694 cur = list_entry(plug->rbio_list.next,
1695 struct btrfs_raid_bio, plug_list);
1696 list_del_init(&cur->plug_list);
1697
1698 if (rbio_is_full(cur)) {
c7b562c5
DS
1699 int ret;
1700
6ac0f488 1701 /* we have a full stripe, send it down */
c7b562c5
DS
1702 ret = full_stripe_write(cur);
1703 BUG_ON(ret);
6ac0f488
CM
1704 continue;
1705 }
1706 if (last) {
1707 if (rbio_can_merge(last, cur)) {
1708 merge_rbio(last, cur);
1709 __free_raid_bio(cur);
1710 continue;
1711
1712 }
1713 __raid56_parity_write(last);
1714 }
1715 last = cur;
1716 }
1717 if (last) {
1718 __raid56_parity_write(last);
1719 }
1720 kfree(plug);
1721}
1722
1723/*
1724 * if the unplug comes from schedule, we have to push the
1725 * work off to a helper thread
1726 */
1727static void unplug_work(struct btrfs_work *work)
1728{
1729 struct btrfs_plug_cb *plug;
1730 plug = container_of(work, struct btrfs_plug_cb, work);
1731 run_plug(plug);
1732}
1733
1734static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
1735{
1736 struct btrfs_plug_cb *plug;
1737 plug = container_of(cb, struct btrfs_plug_cb, cb);
1738
1739 if (from_schedule) {
a0cac0ec 1740 btrfs_init_work(&plug->work, unplug_work, NULL, NULL);
d05a33ac
QW
1741 btrfs_queue_work(plug->info->rmw_workers,
1742 &plug->work);
6ac0f488
CM
1743 return;
1744 }
1745 run_plug(plug);
1746}
1747
53b381b3
DW
1748/*
1749 * our main entry point for writes from the rest of the FS.
1750 */
2ff7e61e 1751int raid56_parity_write(struct btrfs_fs_info *fs_info, struct bio *bio,
8e5cfb55 1752 struct btrfs_bio *bbio, u64 stripe_len)
53b381b3
DW
1753{
1754 struct btrfs_raid_bio *rbio;
6ac0f488
CM
1755 struct btrfs_plug_cb *plug = NULL;
1756 struct blk_plug_cb *cb;
4245215d 1757 int ret;
53b381b3 1758
2ff7e61e 1759 rbio = alloc_rbio(fs_info, bbio, stripe_len);
af8e2d1d 1760 if (IS_ERR(rbio)) {
6e9606d2 1761 btrfs_put_bbio(bbio);
53b381b3 1762 return PTR_ERR(rbio);
af8e2d1d 1763 }
53b381b3 1764 bio_list_add(&rbio->bio_list, bio);
4f024f37 1765 rbio->bio_list_bytes = bio->bi_iter.bi_size;
1b94b556 1766 rbio->operation = BTRFS_RBIO_WRITE;
6ac0f488 1767
0b246afa 1768 btrfs_bio_counter_inc_noblocked(fs_info);
4245215d
MX
1769 rbio->generic_bio_cnt = 1;
1770
6ac0f488
CM
1771 /*
1772 * don't plug on full rbios, just get them out the door
1773 * as quickly as we can
1774 */
4245215d
MX
1775 if (rbio_is_full(rbio)) {
1776 ret = full_stripe_write(rbio);
1777 if (ret)
0b246afa 1778 btrfs_bio_counter_dec(fs_info);
4245215d
MX
1779 return ret;
1780 }
6ac0f488 1781
0b246afa 1782 cb = blk_check_plugged(btrfs_raid_unplug, fs_info, sizeof(*plug));
6ac0f488
CM
1783 if (cb) {
1784 plug = container_of(cb, struct btrfs_plug_cb, cb);
1785 if (!plug->info) {
0b246afa 1786 plug->info = fs_info;
6ac0f488
CM
1787 INIT_LIST_HEAD(&plug->rbio_list);
1788 }
1789 list_add_tail(&rbio->plug_list, &plug->rbio_list);
4245215d 1790 ret = 0;
6ac0f488 1791 } else {
4245215d
MX
1792 ret = __raid56_parity_write(rbio);
1793 if (ret)
0b246afa 1794 btrfs_bio_counter_dec(fs_info);
6ac0f488 1795 }
4245215d 1796 return ret;
53b381b3
DW
1797}
1798
1799/*
1800 * all parity reconstruction happens here. We've read in everything
1801 * we can find from the drives and this does the heavy lifting of
1802 * sorting the good from the bad.
1803 */
1804static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
1805{
1806 int pagenr, stripe;
1807 void **pointers;
1808 int faila = -1, failb = -1;
53b381b3 1809 struct page *page;
58efbc9f 1810 blk_status_t err;
53b381b3
DW
1811 int i;
1812
31e818fe 1813 pointers = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
53b381b3 1814 if (!pointers) {
58efbc9f 1815 err = BLK_STS_RESOURCE;
53b381b3
DW
1816 goto cleanup_io;
1817 }
1818
1819 faila = rbio->faila;
1820 failb = rbio->failb;
1821
b4ee1782
OS
1822 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1823 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
53b381b3
DW
1824 spin_lock_irq(&rbio->bio_list_lock);
1825 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1826 spin_unlock_irq(&rbio->bio_list_lock);
1827 }
1828
1829 index_rbio_pages(rbio);
1830
915e2290 1831 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
5a6ac9ea
MX
1832 /*
1833 * Now we just use bitmap to mark the horizontal stripes in
1834 * which we have data when doing parity scrub.
1835 */
1836 if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB &&
1837 !test_bit(pagenr, rbio->dbitmap))
1838 continue;
1839
53b381b3
DW
1840 /* setup our array of pointers with pages
1841 * from each stripe
1842 */
2c8cdd6e 1843 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
53b381b3
DW
1844 /*
1845 * if we're rebuilding a read, we have to use
1846 * pages from the bio list
1847 */
b4ee1782
OS
1848 if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1849 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
53b381b3
DW
1850 (stripe == faila || stripe == failb)) {
1851 page = page_in_rbio(rbio, stripe, pagenr, 0);
1852 } else {
1853 page = rbio_stripe_page(rbio, stripe, pagenr);
1854 }
1855 pointers[stripe] = kmap(page);
1856 }
1857
1858 /* all raid6 handling here */
10f11900 1859 if (rbio->bbio->map_type & BTRFS_BLOCK_GROUP_RAID6) {
53b381b3
DW
1860 /*
1861 * single failure, rebuild from parity raid5
1862 * style
1863 */
1864 if (failb < 0) {
1865 if (faila == rbio->nr_data) {
1866 /*
1867 * Just the P stripe has failed, without
1868 * a bad data or Q stripe.
1869 * TODO, we should redo the xor here.
1870 */
58efbc9f 1871 err = BLK_STS_IOERR;
53b381b3
DW
1872 goto cleanup;
1873 }
1874 /*
1875 * a single failure in raid6 is rebuilt
1876 * in the pstripe code below
1877 */
1878 goto pstripe;
1879 }
1880
1881 /* make sure our ps and qs are in order */
1882 if (faila > failb) {
1883 int tmp = failb;
1884 failb = faila;
1885 faila = tmp;
1886 }
1887
1888 /* if the q stripe is failed, do a pstripe reconstruction
1889 * from the xors.
1890 * If both the q stripe and the P stripe are failed, we're
1891 * here due to a crc mismatch and we can't give them the
1892 * data they want
1893 */
8e5cfb55
ZL
1894 if (rbio->bbio->raid_map[failb] == RAID6_Q_STRIPE) {
1895 if (rbio->bbio->raid_map[faila] ==
1896 RAID5_P_STRIPE) {
58efbc9f 1897 err = BLK_STS_IOERR;
53b381b3
DW
1898 goto cleanup;
1899 }
1900 /*
1901 * otherwise we have one bad data stripe and
1902 * a good P stripe. raid5!
1903 */
1904 goto pstripe;
1905 }
1906
8e5cfb55 1907 if (rbio->bbio->raid_map[failb] == RAID5_P_STRIPE) {
2c8cdd6e 1908 raid6_datap_recov(rbio->real_stripes,
53b381b3
DW
1909 PAGE_SIZE, faila, pointers);
1910 } else {
2c8cdd6e 1911 raid6_2data_recov(rbio->real_stripes,
53b381b3
DW
1912 PAGE_SIZE, faila, failb,
1913 pointers);
1914 }
1915 } else {
1916 void *p;
1917
1918 /* rebuild from P stripe here (raid5 or raid6) */
1919 BUG_ON(failb != -1);
1920pstripe:
1921 /* Copy parity block into failed block to start with */
69d24804 1922 copy_page(pointers[faila], pointers[rbio->nr_data]);
53b381b3
DW
1923
1924 /* rearrange the pointer array */
1925 p = pointers[faila];
1926 for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
1927 pointers[stripe] = pointers[stripe + 1];
1928 pointers[rbio->nr_data - 1] = p;
1929
1930 /* xor in the rest */
09cbfeaf 1931 run_xor(pointers, rbio->nr_data - 1, PAGE_SIZE);
53b381b3
DW
1932 }
1933 /* if we're doing this rebuild as part of an rmw, go through
1934 * and set all of our private rbio pages in the
1935 * failed stripes as uptodate. This way finish_rmw will
1936 * know they can be trusted. If this was a read reconstruction,
1937 * other endio functions will fiddle the uptodate bits
1938 */
1b94b556 1939 if (rbio->operation == BTRFS_RBIO_WRITE) {
915e2290 1940 for (i = 0; i < rbio->stripe_npages; i++) {
53b381b3
DW
1941 if (faila != -1) {
1942 page = rbio_stripe_page(rbio, faila, i);
1943 SetPageUptodate(page);
1944 }
1945 if (failb != -1) {
1946 page = rbio_stripe_page(rbio, failb, i);
1947 SetPageUptodate(page);
1948 }
1949 }
1950 }
2c8cdd6e 1951 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
53b381b3
DW
1952 /*
1953 * if we're rebuilding a read, we have to use
1954 * pages from the bio list
1955 */
b4ee1782
OS
1956 if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1957 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
53b381b3
DW
1958 (stripe == faila || stripe == failb)) {
1959 page = page_in_rbio(rbio, stripe, pagenr, 0);
1960 } else {
1961 page = rbio_stripe_page(rbio, stripe, pagenr);
1962 }
1963 kunmap(page);
1964 }
1965 }
1966
58efbc9f 1967 err = BLK_STS_OK;
53b381b3
DW
1968cleanup:
1969 kfree(pointers);
1970
1971cleanup_io:
580c6efa
LB
1972 /*
1973 * Similar to READ_REBUILD, REBUILD_MISSING at this point also has a
1974 * valid rbio which is consistent with ondisk content, thus such a
1975 * valid rbio can be cached to avoid further disk reads.
1976 */
1977 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1978 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
44ac474d
LB
1979 /*
1980 * - In case of two failures, where rbio->failb != -1:
1981 *
1982 * Do not cache this rbio since the above read reconstruction
1983 * (raid6_datap_recov() or raid6_2data_recov()) may have
1984 * changed some content of stripes which are not identical to
1985 * on-disk content any more, otherwise, a later write/recover
1986 * may steal stripe_pages from this rbio and end up with
1987 * corruptions or rebuild failures.
1988 *
1989 * - In case of single failure, where rbio->failb == -1:
1990 *
1991 * Cache this rbio iff the above read reconstruction is
52042d8e 1992 * executed without problems.
44ac474d
LB
1993 */
1994 if (err == BLK_STS_OK && rbio->failb < 0)
4ae10b3a
CM
1995 cache_rbio_pages(rbio);
1996 else
1997 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
1998
4246a0b6 1999 rbio_orig_end_io(rbio, err);
58efbc9f 2000 } else if (err == BLK_STS_OK) {
53b381b3
DW
2001 rbio->faila = -1;
2002 rbio->failb = -1;
5a6ac9ea
MX
2003
2004 if (rbio->operation == BTRFS_RBIO_WRITE)
2005 finish_rmw(rbio);
2006 else if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB)
2007 finish_parity_scrub(rbio, 0);
2008 else
2009 BUG();
53b381b3 2010 } else {
4246a0b6 2011 rbio_orig_end_io(rbio, err);
53b381b3
DW
2012 }
2013}
2014
2015/*
2016 * This is called only for stripes we've read from disk to
2017 * reconstruct the parity.
2018 */
4246a0b6 2019static void raid_recover_end_io(struct bio *bio)
53b381b3
DW
2020{
2021 struct btrfs_raid_bio *rbio = bio->bi_private;
2022
2023 /*
2024 * we only read stripe pages off the disk, set them
2025 * up to date if there were no errors
2026 */
4e4cbee9 2027 if (bio->bi_status)
53b381b3
DW
2028 fail_bio_stripe(rbio, bio);
2029 else
2030 set_bio_pages_uptodate(bio);
2031 bio_put(bio);
2032
b89e1b01 2033 if (!atomic_dec_and_test(&rbio->stripes_pending))
53b381b3
DW
2034 return;
2035
b89e1b01 2036 if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
58efbc9f 2037 rbio_orig_end_io(rbio, BLK_STS_IOERR);
53b381b3
DW
2038 else
2039 __raid_recover_end_io(rbio);
2040}
2041
2042/*
2043 * reads everything we need off the disk to reconstruct
2044 * the parity. endio handlers trigger final reconstruction
2045 * when the IO is done.
2046 *
2047 * This is used both for reads from the higher layers and for
2048 * parity construction required to finish a rmw cycle.
2049 */
2050static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2051{
2052 int bios_to_read = 0;
53b381b3
DW
2053 struct bio_list bio_list;
2054 int ret;
53b381b3
DW
2055 int pagenr;
2056 int stripe;
2057 struct bio *bio;
2058
2059 bio_list_init(&bio_list);
2060
2061 ret = alloc_rbio_pages(rbio);
2062 if (ret)
2063 goto cleanup;
2064
b89e1b01 2065 atomic_set(&rbio->error, 0);
53b381b3
DW
2066
2067 /*
4ae10b3a
CM
2068 * read everything that hasn't failed. Thanks to the
2069 * stripe cache, it is possible that some or all of these
2070 * pages are going to be uptodate.
53b381b3 2071 */
2c8cdd6e 2072 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
5588383e 2073 if (rbio->faila == stripe || rbio->failb == stripe) {
b89e1b01 2074 atomic_inc(&rbio->error);
53b381b3 2075 continue;
5588383e 2076 }
53b381b3 2077
915e2290 2078 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
53b381b3
DW
2079 struct page *p;
2080
2081 /*
2082 * the rmw code may have already read this
2083 * page in
2084 */
2085 p = rbio_stripe_page(rbio, stripe, pagenr);
2086 if (PageUptodate(p))
2087 continue;
2088
2089 ret = rbio_add_io_page(rbio, &bio_list,
2090 rbio_stripe_page(rbio, stripe, pagenr),
2091 stripe, pagenr, rbio->stripe_len);
2092 if (ret < 0)
2093 goto cleanup;
2094 }
2095 }
2096
2097 bios_to_read = bio_list_size(&bio_list);
2098 if (!bios_to_read) {
2099 /*
2100 * we might have no bios to read just because the pages
2101 * were up to date, or we might have no bios to read because
2102 * the devices were gone.
2103 */
b89e1b01 2104 if (atomic_read(&rbio->error) <= rbio->bbio->max_errors) {
53b381b3
DW
2105 __raid_recover_end_io(rbio);
2106 goto out;
2107 } else {
2108 goto cleanup;
2109 }
2110 }
2111
2112 /*
2113 * the bbio may be freed once we submit the last bio. Make sure
2114 * not to touch it after that
2115 */
b89e1b01 2116 atomic_set(&rbio->stripes_pending, bios_to_read);
53b381b3
DW
2117 while (1) {
2118 bio = bio_list_pop(&bio_list);
2119 if (!bio)
2120 break;
2121
2122 bio->bi_private = rbio;
2123 bio->bi_end_io = raid_recover_end_io;
ebcc3263 2124 bio->bi_opf = REQ_OP_READ;
53b381b3 2125
0b246afa 2126 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
53b381b3 2127
4e49ea4a 2128 submit_bio(bio);
53b381b3
DW
2129 }
2130out:
2131 return 0;
2132
2133cleanup:
b4ee1782
OS
2134 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
2135 rbio->operation == BTRFS_RBIO_REBUILD_MISSING)
58efbc9f 2136 rbio_orig_end_io(rbio, BLK_STS_IOERR);
785884fc
LB
2137
2138 while ((bio = bio_list_pop(&bio_list)))
2139 bio_put(bio);
2140
53b381b3
DW
2141 return -EIO;
2142}
2143
2144/*
2145 * the main entry point for reads from the higher layers. This
2146 * is really only called when the normal read path had a failure,
2147 * so we assume the bio they send down corresponds to a failed part
2148 * of the drive.
2149 */
2ff7e61e 2150int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
8e5cfb55
ZL
2151 struct btrfs_bio *bbio, u64 stripe_len,
2152 int mirror_num, int generic_io)
53b381b3
DW
2153{
2154 struct btrfs_raid_bio *rbio;
2155 int ret;
2156
abad60c6
LB
2157 if (generic_io) {
2158 ASSERT(bbio->mirror_num == mirror_num);
2159 btrfs_io_bio(bio)->mirror_num = mirror_num;
2160 }
2161
2ff7e61e 2162 rbio = alloc_rbio(fs_info, bbio, stripe_len);
af8e2d1d 2163 if (IS_ERR(rbio)) {
6e9606d2
ZL
2164 if (generic_io)
2165 btrfs_put_bbio(bbio);
53b381b3 2166 return PTR_ERR(rbio);
af8e2d1d 2167 }
53b381b3 2168
1b94b556 2169 rbio->operation = BTRFS_RBIO_READ_REBUILD;
53b381b3 2170 bio_list_add(&rbio->bio_list, bio);
4f024f37 2171 rbio->bio_list_bytes = bio->bi_iter.bi_size;
53b381b3
DW
2172
2173 rbio->faila = find_logical_bio_stripe(rbio, bio);
2174 if (rbio->faila == -1) {
0b246afa 2175 btrfs_warn(fs_info,
e46a28ca
LB
2176 "%s could not find the bad stripe in raid56 so that we cannot recover any more (bio has logical %llu len %llu, bbio has map_type %llu)",
2177 __func__, (u64)bio->bi_iter.bi_sector << 9,
2178 (u64)bio->bi_iter.bi_size, bbio->map_type);
6e9606d2
ZL
2179 if (generic_io)
2180 btrfs_put_bbio(bbio);
53b381b3
DW
2181 kfree(rbio);
2182 return -EIO;
2183 }
2184
4245215d 2185 if (generic_io) {
0b246afa 2186 btrfs_bio_counter_inc_noblocked(fs_info);
4245215d
MX
2187 rbio->generic_bio_cnt = 1;
2188 } else {
6e9606d2 2189 btrfs_get_bbio(bbio);
4245215d
MX
2190 }
2191
53b381b3 2192 /*
8810f751
LB
2193 * Loop retry:
2194 * for 'mirror == 2', reconstruct from all other stripes.
2195 * for 'mirror_num > 2', select a stripe to fail on every retry.
53b381b3 2196 */
8810f751
LB
2197 if (mirror_num > 2) {
2198 /*
2199 * 'mirror == 3' is to fail the p stripe and
2200 * reconstruct from the q stripe. 'mirror > 3' is to
2201 * fail a data stripe and reconstruct from p+q stripe.
2202 */
2203 rbio->failb = rbio->real_stripes - (mirror_num - 1);
2204 ASSERT(rbio->failb > 0);
2205 if (rbio->failb <= rbio->faila)
2206 rbio->failb--;
2207 }
53b381b3
DW
2208
2209 ret = lock_stripe_add(rbio);
2210
2211 /*
2212 * __raid56_parity_recover will end the bio with
2213 * any errors it hits. We don't want to return
2214 * its error value up the stack because our caller
2215 * will end up calling bio_endio with any nonzero
2216 * return
2217 */
2218 if (ret == 0)
2219 __raid56_parity_recover(rbio);
2220 /*
2221 * our rbio has been added to the list of
2222 * rbios that will be handled after the
2223 * currently lock owner is done
2224 */
2225 return 0;
2226
2227}
2228
2229static void rmw_work(struct btrfs_work *work)
2230{
2231 struct btrfs_raid_bio *rbio;
2232
2233 rbio = container_of(work, struct btrfs_raid_bio, work);
2234 raid56_rmw_stripe(rbio);
2235}
2236
2237static void read_rebuild_work(struct btrfs_work *work)
2238{
2239 struct btrfs_raid_bio *rbio;
2240
2241 rbio = container_of(work, struct btrfs_raid_bio, work);
2242 __raid56_parity_recover(rbio);
2243}
5a6ac9ea
MX
2244
2245/*
2246 * The following code is used to scrub/replace the parity stripe
2247 *
ae6529c3
QW
2248 * Caller must have already increased bio_counter for getting @bbio.
2249 *
5a6ac9ea
MX
2250 * Note: We need make sure all the pages that add into the scrub/replace
2251 * raid bio are correct and not be changed during the scrub/replace. That
2252 * is those pages just hold metadata or file data with checksum.
2253 */
2254
2255struct btrfs_raid_bio *
2ff7e61e 2256raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
8e5cfb55
ZL
2257 struct btrfs_bio *bbio, u64 stripe_len,
2258 struct btrfs_device *scrub_dev,
5a6ac9ea
MX
2259 unsigned long *dbitmap, int stripe_nsectors)
2260{
2261 struct btrfs_raid_bio *rbio;
2262 int i;
2263
2ff7e61e 2264 rbio = alloc_rbio(fs_info, bbio, stripe_len);
5a6ac9ea
MX
2265 if (IS_ERR(rbio))
2266 return NULL;
2267 bio_list_add(&rbio->bio_list, bio);
2268 /*
2269 * This is a special bio which is used to hold the completion handler
2270 * and make the scrub rbio is similar to the other types
2271 */
2272 ASSERT(!bio->bi_iter.bi_size);
2273 rbio->operation = BTRFS_RBIO_PARITY_SCRUB;
2274
9cd3a7eb
LB
2275 /*
2276 * After mapping bbio with BTRFS_MAP_WRITE, parities have been sorted
2277 * to the end position, so this search can start from the first parity
2278 * stripe.
2279 */
2280 for (i = rbio->nr_data; i < rbio->real_stripes; i++) {
5a6ac9ea
MX
2281 if (bbio->stripes[i].dev == scrub_dev) {
2282 rbio->scrubp = i;
2283 break;
2284 }
2285 }
9cd3a7eb 2286 ASSERT(i < rbio->real_stripes);
5a6ac9ea
MX
2287
2288 /* Now we just support the sectorsize equals to page size */
0b246afa 2289 ASSERT(fs_info->sectorsize == PAGE_SIZE);
5a6ac9ea
MX
2290 ASSERT(rbio->stripe_npages == stripe_nsectors);
2291 bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors);
2292
ae6529c3
QW
2293 /*
2294 * We have already increased bio_counter when getting bbio, record it
2295 * so we can free it at rbio_orig_end_io().
2296 */
2297 rbio->generic_bio_cnt = 1;
2298
5a6ac9ea
MX
2299 return rbio;
2300}
2301
b4ee1782
OS
2302/* Used for both parity scrub and missing. */
2303void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
2304 u64 logical)
5a6ac9ea
MX
2305{
2306 int stripe_offset;
2307 int index;
2308
8e5cfb55
ZL
2309 ASSERT(logical >= rbio->bbio->raid_map[0]);
2310 ASSERT(logical + PAGE_SIZE <= rbio->bbio->raid_map[0] +
5a6ac9ea 2311 rbio->stripe_len * rbio->nr_data);
8e5cfb55 2312 stripe_offset = (int)(logical - rbio->bbio->raid_map[0]);
09cbfeaf 2313 index = stripe_offset >> PAGE_SHIFT;
5a6ac9ea
MX
2314 rbio->bio_pages[index] = page;
2315}
2316
2317/*
2318 * We just scrub the parity that we have correct data on the same horizontal,
2319 * so we needn't allocate all pages for all the stripes.
2320 */
2321static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
2322{
2323 int i;
2324 int bit;
2325 int index;
2326 struct page *page;
2327
2328 for_each_set_bit(bit, rbio->dbitmap, rbio->stripe_npages) {
2c8cdd6e 2329 for (i = 0; i < rbio->real_stripes; i++) {
5a6ac9ea
MX
2330 index = i * rbio->stripe_npages + bit;
2331 if (rbio->stripe_pages[index])
2332 continue;
2333
2334 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2335 if (!page)
2336 return -ENOMEM;
2337 rbio->stripe_pages[index] = page;
5a6ac9ea
MX
2338 }
2339 }
2340 return 0;
2341}
2342
5a6ac9ea
MX
2343static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
2344 int need_check)
2345{
76035976 2346 struct btrfs_bio *bbio = rbio->bbio;
1389053e
KC
2347 void **pointers = rbio->finish_pointers;
2348 unsigned long *pbitmap = rbio->finish_pbitmap;
5a6ac9ea
MX
2349 int nr_data = rbio->nr_data;
2350 int stripe;
2351 int pagenr;
c17af965 2352 bool has_qstripe;
5a6ac9ea
MX
2353 struct page *p_page = NULL;
2354 struct page *q_page = NULL;
2355 struct bio_list bio_list;
2356 struct bio *bio;
76035976 2357 int is_replace = 0;
5a6ac9ea
MX
2358 int ret;
2359
2360 bio_list_init(&bio_list);
2361
c17af965
DS
2362 if (rbio->real_stripes - rbio->nr_data == 1)
2363 has_qstripe = false;
2364 else if (rbio->real_stripes - rbio->nr_data == 2)
2365 has_qstripe = true;
2366 else
5a6ac9ea 2367 BUG();
5a6ac9ea 2368
76035976
MX
2369 if (bbio->num_tgtdevs && bbio->tgtdev_map[rbio->scrubp]) {
2370 is_replace = 1;
2371 bitmap_copy(pbitmap, rbio->dbitmap, rbio->stripe_npages);
2372 }
2373
5a6ac9ea
MX
2374 /*
2375 * Because the higher layers(scrubber) are unlikely to
2376 * use this area of the disk again soon, so don't cache
2377 * it.
2378 */
2379 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
2380
2381 if (!need_check)
2382 goto writeback;
2383
2384 p_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2385 if (!p_page)
2386 goto cleanup;
2387 SetPageUptodate(p_page);
2388
c17af965 2389 if (has_qstripe) {
5a6ac9ea
MX
2390 q_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2391 if (!q_page) {
2392 __free_page(p_page);
2393 goto cleanup;
2394 }
2395 SetPageUptodate(q_page);
2396 }
2397
2398 atomic_set(&rbio->error, 0);
2399
2400 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2401 struct page *p;
2402 void *parity;
2403 /* first collect one page from each data stripe */
2404 for (stripe = 0; stripe < nr_data; stripe++) {
2405 p = page_in_rbio(rbio, stripe, pagenr, 0);
2406 pointers[stripe] = kmap(p);
2407 }
2408
2409 /* then add the parity stripe */
2410 pointers[stripe++] = kmap(p_page);
2411
c17af965 2412 if (has_qstripe) {
5a6ac9ea
MX
2413 /*
2414 * raid6, add the qstripe and call the
2415 * library function to fill in our p/q
2416 */
2417 pointers[stripe++] = kmap(q_page);
2418
2c8cdd6e 2419 raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
5a6ac9ea
MX
2420 pointers);
2421 } else {
2422 /* raid5 */
69d24804 2423 copy_page(pointers[nr_data], pointers[0]);
09cbfeaf 2424 run_xor(pointers + 1, nr_data - 1, PAGE_SIZE);
5a6ac9ea
MX
2425 }
2426
01327610 2427 /* Check scrubbing parity and repair it */
5a6ac9ea
MX
2428 p = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2429 parity = kmap(p);
09cbfeaf 2430 if (memcmp(parity, pointers[rbio->scrubp], PAGE_SIZE))
69d24804 2431 copy_page(parity, pointers[rbio->scrubp]);
5a6ac9ea
MX
2432 else
2433 /* Parity is right, needn't writeback */
2434 bitmap_clear(rbio->dbitmap, pagenr, 1);
2435 kunmap(p);
2436
3897b6f0 2437 for (stripe = 0; stripe < nr_data; stripe++)
5a6ac9ea 2438 kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
3897b6f0 2439 kunmap(p_page);
5a6ac9ea
MX
2440 }
2441
2442 __free_page(p_page);
2443 if (q_page)
2444 __free_page(q_page);
2445
2446writeback:
2447 /*
2448 * time to start writing. Make bios for everything from the
2449 * higher layers (the bio_list in our rbio) and our p/q. Ignore
2450 * everything else.
2451 */
2452 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2453 struct page *page;
2454
2455 page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2456 ret = rbio_add_io_page(rbio, &bio_list,
2457 page, rbio->scrubp, pagenr, rbio->stripe_len);
2458 if (ret)
2459 goto cleanup;
2460 }
2461
76035976
MX
2462 if (!is_replace)
2463 goto submit_write;
2464
2465 for_each_set_bit(pagenr, pbitmap, rbio->stripe_npages) {
2466 struct page *page;
2467
2468 page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2469 ret = rbio_add_io_page(rbio, &bio_list, page,
2470 bbio->tgtdev_map[rbio->scrubp],
2471 pagenr, rbio->stripe_len);
2472 if (ret)
2473 goto cleanup;
2474 }
2475
2476submit_write:
5a6ac9ea
MX
2477 nr_data = bio_list_size(&bio_list);
2478 if (!nr_data) {
2479 /* Every parity is right */
58efbc9f 2480 rbio_orig_end_io(rbio, BLK_STS_OK);
5a6ac9ea
MX
2481 return;
2482 }
2483
2484 atomic_set(&rbio->stripes_pending, nr_data);
2485
2486 while (1) {
2487 bio = bio_list_pop(&bio_list);
2488 if (!bio)
2489 break;
2490
2491 bio->bi_private = rbio;
a6111d11 2492 bio->bi_end_io = raid_write_end_io;
ebcc3263 2493 bio->bi_opf = REQ_OP_WRITE;
4e49ea4a
MC
2494
2495 submit_bio(bio);
5a6ac9ea
MX
2496 }
2497 return;
2498
2499cleanup:
58efbc9f 2500 rbio_orig_end_io(rbio, BLK_STS_IOERR);
785884fc
LB
2501
2502 while ((bio = bio_list_pop(&bio_list)))
2503 bio_put(bio);
5a6ac9ea
MX
2504}
2505
2506static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
2507{
2508 if (stripe >= 0 && stripe < rbio->nr_data)
2509 return 1;
2510 return 0;
2511}
2512
2513/*
2514 * While we're doing the parity check and repair, we could have errors
2515 * in reading pages off the disk. This checks for errors and if we're
2516 * not able to read the page it'll trigger parity reconstruction. The
2517 * parity scrub will be finished after we've reconstructed the failed
2518 * stripes
2519 */
2520static void validate_rbio_for_parity_scrub(struct btrfs_raid_bio *rbio)
2521{
2522 if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
2523 goto cleanup;
2524
2525 if (rbio->faila >= 0 || rbio->failb >= 0) {
2526 int dfail = 0, failp = -1;
2527
2528 if (is_data_stripe(rbio, rbio->faila))
2529 dfail++;
2530 else if (is_parity_stripe(rbio->faila))
2531 failp = rbio->faila;
2532
2533 if (is_data_stripe(rbio, rbio->failb))
2534 dfail++;
2535 else if (is_parity_stripe(rbio->failb))
2536 failp = rbio->failb;
2537
2538 /*
2539 * Because we can not use a scrubbing parity to repair
2540 * the data, so the capability of the repair is declined.
2541 * (In the case of RAID5, we can not repair anything)
2542 */
2543 if (dfail > rbio->bbio->max_errors - 1)
2544 goto cleanup;
2545
2546 /*
2547 * If all data is good, only parity is correctly, just
2548 * repair the parity.
2549 */
2550 if (dfail == 0) {
2551 finish_parity_scrub(rbio, 0);
2552 return;
2553 }
2554
2555 /*
2556 * Here means we got one corrupted data stripe and one
2557 * corrupted parity on RAID6, if the corrupted parity
01327610 2558 * is scrubbing parity, luckily, use the other one to repair
5a6ac9ea
MX
2559 * the data, or we can not repair the data stripe.
2560 */
2561 if (failp != rbio->scrubp)
2562 goto cleanup;
2563
2564 __raid_recover_end_io(rbio);
2565 } else {
2566 finish_parity_scrub(rbio, 1);
2567 }
2568 return;
2569
2570cleanup:
58efbc9f 2571 rbio_orig_end_io(rbio, BLK_STS_IOERR);
5a6ac9ea
MX
2572}
2573
2574/*
2575 * end io for the read phase of the rmw cycle. All the bios here are physical
2576 * stripe bios we've read from the disk so we can recalculate the parity of the
2577 * stripe.
2578 *
2579 * This will usually kick off finish_rmw once all the bios are read in, but it
2580 * may trigger parity reconstruction if we had any errors along the way
2581 */
4246a0b6 2582static void raid56_parity_scrub_end_io(struct bio *bio)
5a6ac9ea
MX
2583{
2584 struct btrfs_raid_bio *rbio = bio->bi_private;
2585
4e4cbee9 2586 if (bio->bi_status)
5a6ac9ea
MX
2587 fail_bio_stripe(rbio, bio);
2588 else
2589 set_bio_pages_uptodate(bio);
2590
2591 bio_put(bio);
2592
2593 if (!atomic_dec_and_test(&rbio->stripes_pending))
2594 return;
2595
2596 /*
2597 * this will normally call finish_rmw to start our write
2598 * but if there are any failed stripes we'll reconstruct
2599 * from parity first
2600 */
2601 validate_rbio_for_parity_scrub(rbio);
2602}
2603
2604static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
2605{
2606 int bios_to_read = 0;
5a6ac9ea
MX
2607 struct bio_list bio_list;
2608 int ret;
2609 int pagenr;
2610 int stripe;
2611 struct bio *bio;
2612
785884fc
LB
2613 bio_list_init(&bio_list);
2614
5a6ac9ea
MX
2615 ret = alloc_rbio_essential_pages(rbio);
2616 if (ret)
2617 goto cleanup;
2618
5a6ac9ea
MX
2619 atomic_set(&rbio->error, 0);
2620 /*
2621 * build a list of bios to read all the missing parts of this
2622 * stripe
2623 */
2c8cdd6e 2624 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
5a6ac9ea
MX
2625 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2626 struct page *page;
2627 /*
2628 * we want to find all the pages missing from
2629 * the rbio and read them from the disk. If
2630 * page_in_rbio finds a page in the bio list
2631 * we don't need to read it off the stripe.
2632 */
2633 page = page_in_rbio(rbio, stripe, pagenr, 1);
2634 if (page)
2635 continue;
2636
2637 page = rbio_stripe_page(rbio, stripe, pagenr);
2638 /*
2639 * the bio cache may have handed us an uptodate
2640 * page. If so, be happy and use it
2641 */
2642 if (PageUptodate(page))
2643 continue;
2644
2645 ret = rbio_add_io_page(rbio, &bio_list, page,
2646 stripe, pagenr, rbio->stripe_len);
2647 if (ret)
2648 goto cleanup;
2649 }
2650 }
2651
2652 bios_to_read = bio_list_size(&bio_list);
2653 if (!bios_to_read) {
2654 /*
2655 * this can happen if others have merged with
2656 * us, it means there is nothing left to read.
2657 * But if there are missing devices it may not be
2658 * safe to do the full stripe write yet.
2659 */
2660 goto finish;
2661 }
2662
2663 /*
2664 * the bbio may be freed once we submit the last bio. Make sure
2665 * not to touch it after that
2666 */
2667 atomic_set(&rbio->stripes_pending, bios_to_read);
2668 while (1) {
2669 bio = bio_list_pop(&bio_list);
2670 if (!bio)
2671 break;
2672
2673 bio->bi_private = rbio;
2674 bio->bi_end_io = raid56_parity_scrub_end_io;
ebcc3263 2675 bio->bi_opf = REQ_OP_READ;
5a6ac9ea 2676
0b246afa 2677 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
5a6ac9ea 2678
4e49ea4a 2679 submit_bio(bio);
5a6ac9ea
MX
2680 }
2681 /* the actual write will happen once the reads are done */
2682 return;
2683
2684cleanup:
58efbc9f 2685 rbio_orig_end_io(rbio, BLK_STS_IOERR);
785884fc
LB
2686
2687 while ((bio = bio_list_pop(&bio_list)))
2688 bio_put(bio);
2689
5a6ac9ea
MX
2690 return;
2691
2692finish:
2693 validate_rbio_for_parity_scrub(rbio);
2694}
2695
2696static void scrub_parity_work(struct btrfs_work *work)
2697{
2698 struct btrfs_raid_bio *rbio;
2699
2700 rbio = container_of(work, struct btrfs_raid_bio, work);
2701 raid56_parity_scrub_stripe(rbio);
2702}
2703
5a6ac9ea
MX
2704void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
2705{
2706 if (!lock_stripe_add(rbio))
a81b747d 2707 start_async_work(rbio, scrub_parity_work);
5a6ac9ea 2708}
b4ee1782
OS
2709
2710/* The following code is used for dev replace of a missing RAID 5/6 device. */
2711
2712struct btrfs_raid_bio *
2ff7e61e 2713raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
b4ee1782
OS
2714 struct btrfs_bio *bbio, u64 length)
2715{
2716 struct btrfs_raid_bio *rbio;
2717
2ff7e61e 2718 rbio = alloc_rbio(fs_info, bbio, length);
b4ee1782
OS
2719 if (IS_ERR(rbio))
2720 return NULL;
2721
2722 rbio->operation = BTRFS_RBIO_REBUILD_MISSING;
2723 bio_list_add(&rbio->bio_list, bio);
2724 /*
2725 * This is a special bio which is used to hold the completion handler
2726 * and make the scrub rbio is similar to the other types
2727 */
2728 ASSERT(!bio->bi_iter.bi_size);
2729
2730 rbio->faila = find_logical_bio_stripe(rbio, bio);
2731 if (rbio->faila == -1) {
2732 BUG();
2733 kfree(rbio);
2734 return NULL;
2735 }
2736
ae6529c3
QW
2737 /*
2738 * When we get bbio, we have already increased bio_counter, record it
2739 * so we can free it at rbio_orig_end_io()
2740 */
2741 rbio->generic_bio_cnt = 1;
2742
b4ee1782
OS
2743 return rbio;
2744}
2745
b4ee1782
OS
2746void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio)
2747{
2748 if (!lock_stripe_add(rbio))
e66d8d5a 2749 start_async_work(rbio, read_rebuild_work);
b4ee1782 2750}