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