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