2 * Copyright (C) 2012 Fusion-io All rights reserved.
3 * Copyright (C) 2012 Intel Corp. All rights reserved.
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.
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.
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.
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>
34 #include <asm/div64.h>
37 #include "extent_map.h"
39 #include "transaction.h"
40 #include "print-tree.h"
43 #include "async-thread.h"
44 #include "check-integrity.h"
45 #include "rcu-string.h"
47 /* set when additional merges to this rbio are not allowed */
48 #define RBIO_RMW_LOCKED_BIT 1
51 * set when this rbio is sitting in the hash, but it is just a cache
54 #define RBIO_CACHE_BIT 2
57 * set when it is safe to trust the stripe_pages for caching
59 #define RBIO_CACHE_READY_BIT 3
62 #define RBIO_CACHE_SIZE 1024
64 struct btrfs_raid_bio
{
65 struct btrfs_fs_info
*fs_info
;
66 struct btrfs_bio
*bbio
;
69 * logical block numbers for the start of each stripe
70 * The last one or two are p/q. These are sorted,
71 * so raid_map[0] is the start of our full stripe
75 /* while we're doing rmw on a stripe
76 * we put it into a hash table so we can
77 * lock the stripe and merge more rbios
80 struct list_head hash_list
;
83 * LRU list for the stripe cache
85 struct list_head stripe_cache
;
88 * for scheduling work in the helper threads
90 struct btrfs_work work
;
93 * bio list and bio_list_lock are used
94 * to add more bios into the stripe
95 * in hopes of avoiding the full rmw
97 struct bio_list bio_list
;
98 spinlock_t bio_list_lock
;
101 * also protected by the bio_list_lock, the
102 * stripe locking code uses plug_list to hand off
103 * the stripe lock to the next pending IO
105 struct list_head plug_list
;
108 * flags that tell us if it is safe to
109 * merge with this bio
113 /* size of each individual stripe on disk */
116 /* number of data stripes (no p/q) */
120 * set if we're doing a parity rebuild
121 * for a read from higher up, which is handled
122 * differently from a parity rebuild as part of
127 /* first bad stripe */
130 /* second bad stripe (for raid6 use) */
134 * number of pages needed to represent the full
140 * size of all the bios in the bio_list. This
141 * helps us decide if the rbio maps to a full
149 * these are two arrays of pointers. We allocate the
150 * rbio big enough to hold them both and setup their
151 * locations when the rbio is allocated
154 /* pointers to pages that we allocated for
155 * reading/writing stripes directly from the disk (including P/Q)
157 struct page
**stripe_pages
;
160 * pointers to the pages in the bio_list. Stored
161 * here for faster lookup
163 struct page
**bio_pages
;
166 static int __raid56_parity_recover(struct btrfs_raid_bio
*rbio
);
167 static noinline
void finish_rmw(struct btrfs_raid_bio
*rbio
);
168 static void rmw_work(struct btrfs_work
*work
);
169 static void read_rebuild_work(struct btrfs_work
*work
);
170 static void async_rmw_stripe(struct btrfs_raid_bio
*rbio
);
171 static void async_read_rebuild(struct btrfs_raid_bio
*rbio
);
172 static int fail_bio_stripe(struct btrfs_raid_bio
*rbio
, struct bio
*bio
);
173 static int fail_rbio_index(struct btrfs_raid_bio
*rbio
, int failed
);
174 static void __free_raid_bio(struct btrfs_raid_bio
*rbio
);
175 static void index_rbio_pages(struct btrfs_raid_bio
*rbio
);
176 static int alloc_rbio_pages(struct btrfs_raid_bio
*rbio
);
179 * the stripe hash table is used for locking, and to collect
180 * bios in hopes of making a full stripe
182 int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info
*info
)
184 struct btrfs_stripe_hash_table
*table
;
185 struct btrfs_stripe_hash_table
*x
;
186 struct btrfs_stripe_hash
*cur
;
187 struct btrfs_stripe_hash
*h
;
188 int num_entries
= 1 << BTRFS_STRIPE_HASH_TABLE_BITS
;
191 if (info
->stripe_hash_table
)
194 table
= kzalloc(sizeof(*table
) + sizeof(*h
) * num_entries
, GFP_NOFS
);
198 spin_lock_init(&table
->cache_lock
);
199 INIT_LIST_HEAD(&table
->stripe_cache
);
203 for (i
= 0; i
< num_entries
; i
++) {
205 INIT_LIST_HEAD(&cur
->hash_list
);
206 spin_lock_init(&cur
->lock
);
207 init_waitqueue_head(&cur
->wait
);
210 x
= cmpxchg(&info
->stripe_hash_table
, NULL
, table
);
217 * caching an rbio means to copy anything from the
218 * bio_pages array into the stripe_pages array. We
219 * use the page uptodate bit in the stripe cache array
220 * to indicate if it has valid data
222 * once the caching is done, we set the cache ready
225 static void cache_rbio_pages(struct btrfs_raid_bio
*rbio
)
232 ret
= alloc_rbio_pages(rbio
);
236 for (i
= 0; i
< rbio
->nr_pages
; i
++) {
237 if (!rbio
->bio_pages
[i
])
240 s
= kmap(rbio
->bio_pages
[i
]);
241 d
= kmap(rbio
->stripe_pages
[i
]);
243 memcpy(d
, s
, PAGE_CACHE_SIZE
);
245 kunmap(rbio
->bio_pages
[i
]);
246 kunmap(rbio
->stripe_pages
[i
]);
247 SetPageUptodate(rbio
->stripe_pages
[i
]);
249 set_bit(RBIO_CACHE_READY_BIT
, &rbio
->flags
);
253 * we hash on the first logical address of the stripe
255 static int rbio_bucket(struct btrfs_raid_bio
*rbio
)
257 u64 num
= rbio
->raid_map
[0];
260 * we shift down quite a bit. We're using byte
261 * addressing, and most of the lower bits are zeros.
262 * This tends to upset hash_64, and it consistently
263 * returns just one or two different values.
265 * shifting off the lower bits fixes things.
267 return hash_64(num
>> 16, BTRFS_STRIPE_HASH_TABLE_BITS
);
271 * stealing an rbio means taking all the uptodate pages from the stripe
272 * array in the source rbio and putting them into the destination rbio
274 static void steal_rbio(struct btrfs_raid_bio
*src
, struct btrfs_raid_bio
*dest
)
280 if (!test_bit(RBIO_CACHE_READY_BIT
, &src
->flags
))
283 for (i
= 0; i
< dest
->nr_pages
; i
++) {
284 s
= src
->stripe_pages
[i
];
285 if (!s
|| !PageUptodate(s
)) {
289 d
= dest
->stripe_pages
[i
];
293 dest
->stripe_pages
[i
] = s
;
294 src
->stripe_pages
[i
] = NULL
;
299 * merging means we take the bio_list from the victim and
300 * splice it into the destination. The victim should
301 * be discarded afterwards.
303 * must be called with dest->rbio_list_lock held
305 static void merge_rbio(struct btrfs_raid_bio
*dest
,
306 struct btrfs_raid_bio
*victim
)
308 bio_list_merge(&dest
->bio_list
, &victim
->bio_list
);
309 dest
->bio_list_bytes
+= victim
->bio_list_bytes
;
310 bio_list_init(&victim
->bio_list
);
314 * used to prune items that are in the cache. The caller
315 * must hold the hash table lock.
317 static void __remove_rbio_from_cache(struct btrfs_raid_bio
*rbio
)
319 int bucket
= rbio_bucket(rbio
);
320 struct btrfs_stripe_hash_table
*table
;
321 struct btrfs_stripe_hash
*h
;
325 * check the bit again under the hash table lock.
327 if (!test_bit(RBIO_CACHE_BIT
, &rbio
->flags
))
330 table
= rbio
->fs_info
->stripe_hash_table
;
331 h
= table
->table
+ bucket
;
333 /* hold the lock for the bucket because we may be
334 * removing it from the hash table
339 * hold the lock for the bio list because we need
340 * to make sure the bio list is empty
342 spin_lock(&rbio
->bio_list_lock
);
344 if (test_and_clear_bit(RBIO_CACHE_BIT
, &rbio
->flags
)) {
345 list_del_init(&rbio
->stripe_cache
);
346 table
->cache_size
-= 1;
349 /* if the bio list isn't empty, this rbio is
350 * still involved in an IO. We take it out
351 * of the cache list, and drop the ref that
352 * was held for the list.
354 * If the bio_list was empty, we also remove
355 * the rbio from the hash_table, and drop
356 * the corresponding ref
358 if (bio_list_empty(&rbio
->bio_list
)) {
359 if (!list_empty(&rbio
->hash_list
)) {
360 list_del_init(&rbio
->hash_list
);
361 atomic_dec(&rbio
->refs
);
362 BUG_ON(!list_empty(&rbio
->plug_list
));
367 spin_unlock(&rbio
->bio_list_lock
);
368 spin_unlock(&h
->lock
);
371 __free_raid_bio(rbio
);
375 * prune a given rbio from the cache
377 static void remove_rbio_from_cache(struct btrfs_raid_bio
*rbio
)
379 struct btrfs_stripe_hash_table
*table
;
382 if (!test_bit(RBIO_CACHE_BIT
, &rbio
->flags
))
385 table
= rbio
->fs_info
->stripe_hash_table
;
387 spin_lock_irqsave(&table
->cache_lock
, flags
);
388 __remove_rbio_from_cache(rbio
);
389 spin_unlock_irqrestore(&table
->cache_lock
, flags
);
393 * remove everything in the cache
395 void btrfs_clear_rbio_cache(struct btrfs_fs_info
*info
)
397 struct btrfs_stripe_hash_table
*table
;
399 struct btrfs_raid_bio
*rbio
;
401 table
= info
->stripe_hash_table
;
403 spin_lock_irqsave(&table
->cache_lock
, flags
);
404 while (!list_empty(&table
->stripe_cache
)) {
405 rbio
= list_entry(table
->stripe_cache
.next
,
406 struct btrfs_raid_bio
,
408 __remove_rbio_from_cache(rbio
);
410 spin_unlock_irqrestore(&table
->cache_lock
, flags
);
414 * remove all cached entries and free the hash table
417 void btrfs_free_stripe_hash_table(struct btrfs_fs_info
*info
)
419 if (!info
->stripe_hash_table
)
421 btrfs_clear_rbio_cache(info
);
422 kfree(info
->stripe_hash_table
);
423 info
->stripe_hash_table
= NULL
;
427 * insert an rbio into the stripe cache. It
428 * must have already been prepared by calling
431 * If this rbio was already cached, it gets
432 * moved to the front of the lru.
434 * If the size of the rbio cache is too big, we
437 static void cache_rbio(struct btrfs_raid_bio
*rbio
)
439 struct btrfs_stripe_hash_table
*table
;
442 if (!test_bit(RBIO_CACHE_READY_BIT
, &rbio
->flags
))
445 table
= rbio
->fs_info
->stripe_hash_table
;
447 spin_lock_irqsave(&table
->cache_lock
, flags
);
448 spin_lock(&rbio
->bio_list_lock
);
450 /* bump our ref if we were not in the list before */
451 if (!test_and_set_bit(RBIO_CACHE_BIT
, &rbio
->flags
))
452 atomic_inc(&rbio
->refs
);
454 if (!list_empty(&rbio
->stripe_cache
)){
455 list_move(&rbio
->stripe_cache
, &table
->stripe_cache
);
457 list_add(&rbio
->stripe_cache
, &table
->stripe_cache
);
458 table
->cache_size
+= 1;
461 spin_unlock(&rbio
->bio_list_lock
);
463 if (table
->cache_size
> RBIO_CACHE_SIZE
) {
464 struct btrfs_raid_bio
*found
;
466 found
= list_entry(table
->stripe_cache
.prev
,
467 struct btrfs_raid_bio
,
471 __remove_rbio_from_cache(found
);
474 spin_unlock_irqrestore(&table
->cache_lock
, flags
);
479 * helper function to run the xor_blocks api. It is only
480 * able to do MAX_XOR_BLOCKS at a time, so we need to
483 static void run_xor(void **pages
, int src_cnt
, ssize_t len
)
487 void *dest
= pages
[src_cnt
];
490 xor_src_cnt
= min(src_cnt
, MAX_XOR_BLOCKS
);
491 xor_blocks(xor_src_cnt
, len
, dest
, pages
+ src_off
);
493 src_cnt
-= xor_src_cnt
;
494 src_off
+= xor_src_cnt
;
499 * returns true if the bio list inside this rbio
500 * covers an entire stripe (no rmw required).
501 * Must be called with the bio list lock held, or
502 * at a time when you know it is impossible to add
503 * new bios into the list
505 static int __rbio_is_full(struct btrfs_raid_bio
*rbio
)
507 unsigned long size
= rbio
->bio_list_bytes
;
510 if (size
!= rbio
->nr_data
* rbio
->stripe_len
)
513 BUG_ON(size
> rbio
->nr_data
* rbio
->stripe_len
);
517 static int rbio_is_full(struct btrfs_raid_bio
*rbio
)
522 spin_lock_irqsave(&rbio
->bio_list_lock
, flags
);
523 ret
= __rbio_is_full(rbio
);
524 spin_unlock_irqrestore(&rbio
->bio_list_lock
, flags
);
529 * returns 1 if it is safe to merge two rbios together.
530 * The merging is safe if the two rbios correspond to
531 * the same stripe and if they are both going in the same
532 * direction (read vs write), and if neither one is
533 * locked for final IO
535 * The caller is responsible for locking such that
536 * rmw_locked is safe to test
538 static int rbio_can_merge(struct btrfs_raid_bio
*last
,
539 struct btrfs_raid_bio
*cur
)
541 if (test_bit(RBIO_RMW_LOCKED_BIT
, &last
->flags
) ||
542 test_bit(RBIO_RMW_LOCKED_BIT
, &cur
->flags
))
546 * we can't merge with cached rbios, since the
547 * idea is that when we merge the destination
548 * rbio is going to run our IO for us. We can
549 * steal from cached rbio's though, other functions
552 if (test_bit(RBIO_CACHE_BIT
, &last
->flags
) ||
553 test_bit(RBIO_CACHE_BIT
, &cur
->flags
))
556 if (last
->raid_map
[0] !=
560 /* reads can't merge with writes */
561 if (last
->read_rebuild
!=
570 * helper to index into the pstripe
572 static struct page
*rbio_pstripe_page(struct btrfs_raid_bio
*rbio
, int index
)
574 index
+= (rbio
->nr_data
* rbio
->stripe_len
) >> PAGE_CACHE_SHIFT
;
575 return rbio
->stripe_pages
[index
];
579 * helper to index into the qstripe, returns null
580 * if there is no qstripe
582 static struct page
*rbio_qstripe_page(struct btrfs_raid_bio
*rbio
, int index
)
584 if (rbio
->nr_data
+ 1 == rbio
->bbio
->num_stripes
)
587 index
+= ((rbio
->nr_data
+ 1) * rbio
->stripe_len
) >>
589 return rbio
->stripe_pages
[index
];
593 * The first stripe in the table for a logical address
594 * has the lock. rbios are added in one of three ways:
596 * 1) Nobody has the stripe locked yet. The rbio is given
597 * the lock and 0 is returned. The caller must start the IO
600 * 2) Someone has the stripe locked, but we're able to merge
601 * with the lock owner. The rbio is freed and the IO will
602 * start automatically along with the existing rbio. 1 is returned.
604 * 3) Someone has the stripe locked, but we're not able to merge.
605 * The rbio is added to the lock owner's plug list, or merged into
606 * an rbio already on the plug list. When the lock owner unlocks,
607 * the next rbio on the list is run and the IO is started automatically.
610 * If we return 0, the caller still owns the rbio and must continue with
611 * IO submission. If we return 1, the caller must assume the rbio has
612 * already been freed.
614 static noinline
int lock_stripe_add(struct btrfs_raid_bio
*rbio
)
616 int bucket
= rbio_bucket(rbio
);
617 struct btrfs_stripe_hash
*h
= rbio
->fs_info
->stripe_hash_table
->table
+ bucket
;
618 struct btrfs_raid_bio
*cur
;
619 struct btrfs_raid_bio
*pending
;
622 struct btrfs_raid_bio
*freeit
= NULL
;
623 struct btrfs_raid_bio
*cache_drop
= NULL
;
627 spin_lock_irqsave(&h
->lock
, flags
);
628 list_for_each_entry(cur
, &h
->hash_list
, hash_list
) {
630 if (cur
->raid_map
[0] == rbio
->raid_map
[0]) {
631 spin_lock(&cur
->bio_list_lock
);
633 /* can we steal this cached rbio's pages? */
634 if (bio_list_empty(&cur
->bio_list
) &&
635 list_empty(&cur
->plug_list
) &&
636 test_bit(RBIO_CACHE_BIT
, &cur
->flags
) &&
637 !test_bit(RBIO_RMW_LOCKED_BIT
, &cur
->flags
)) {
638 list_del_init(&cur
->hash_list
);
639 atomic_dec(&cur
->refs
);
641 steal_rbio(cur
, rbio
);
643 spin_unlock(&cur
->bio_list_lock
);
648 /* can we merge into the lock owner? */
649 if (rbio_can_merge(cur
, rbio
)) {
650 merge_rbio(cur
, rbio
);
651 spin_unlock(&cur
->bio_list_lock
);
659 * we couldn't merge with the running
660 * rbio, see if we can merge with the
661 * pending ones. We don't have to
662 * check for rmw_locked because there
663 * is no way they are inside finish_rmw
666 list_for_each_entry(pending
, &cur
->plug_list
,
668 if (rbio_can_merge(pending
, rbio
)) {
669 merge_rbio(pending
, rbio
);
670 spin_unlock(&cur
->bio_list_lock
);
677 /* no merging, put us on the tail of the plug list,
678 * our rbio will be started with the currently
679 * running rbio unlocks
681 list_add_tail(&rbio
->plug_list
, &cur
->plug_list
);
682 spin_unlock(&cur
->bio_list_lock
);
688 atomic_inc(&rbio
->refs
);
689 list_add(&rbio
->hash_list
, &h
->hash_list
);
691 spin_unlock_irqrestore(&h
->lock
, flags
);
693 remove_rbio_from_cache(cache_drop
);
695 __free_raid_bio(freeit
);
700 * called as rmw or parity rebuild is completed. If the plug list has more
701 * rbios waiting for this stripe, the next one on the list will be started
703 static noinline
void unlock_stripe(struct btrfs_raid_bio
*rbio
)
706 struct btrfs_stripe_hash
*h
;
710 bucket
= rbio_bucket(rbio
);
711 h
= rbio
->fs_info
->stripe_hash_table
->table
+ bucket
;
713 if (list_empty(&rbio
->plug_list
))
716 spin_lock_irqsave(&h
->lock
, flags
);
717 spin_lock(&rbio
->bio_list_lock
);
719 if (!list_empty(&rbio
->hash_list
)) {
721 * if we're still cached and there is no other IO
722 * to perform, just leave this rbio here for others
723 * to steal from later
725 if (list_empty(&rbio
->plug_list
) &&
726 test_bit(RBIO_CACHE_BIT
, &rbio
->flags
)) {
728 clear_bit(RBIO_RMW_LOCKED_BIT
, &rbio
->flags
);
729 BUG_ON(!bio_list_empty(&rbio
->bio_list
));
733 list_del_init(&rbio
->hash_list
);
734 atomic_dec(&rbio
->refs
);
737 * we use the plug list to hold all the rbios
738 * waiting for the chance to lock this stripe.
739 * hand the lock over to one of them.
741 if (!list_empty(&rbio
->plug_list
)) {
742 struct btrfs_raid_bio
*next
;
743 struct list_head
*head
= rbio
->plug_list
.next
;
745 next
= list_entry(head
, struct btrfs_raid_bio
,
748 list_del_init(&rbio
->plug_list
);
750 list_add(&next
->hash_list
, &h
->hash_list
);
751 atomic_inc(&next
->refs
);
752 spin_unlock(&rbio
->bio_list_lock
);
753 spin_unlock_irqrestore(&h
->lock
, flags
);
755 if (next
->read_rebuild
)
756 async_read_rebuild(next
);
758 steal_rbio(rbio
, next
);
759 async_rmw_stripe(next
);
763 } else if (waitqueue_active(&h
->wait
)) {
764 spin_unlock(&rbio
->bio_list_lock
);
765 spin_unlock_irqrestore(&h
->lock
, flags
);
771 spin_unlock(&rbio
->bio_list_lock
);
772 spin_unlock_irqrestore(&h
->lock
, flags
);
776 remove_rbio_from_cache(rbio
);
779 static void __free_raid_bio(struct btrfs_raid_bio
*rbio
)
783 WARN_ON(atomic_read(&rbio
->refs
) < 0);
784 if (!atomic_dec_and_test(&rbio
->refs
))
787 WARN_ON(!list_empty(&rbio
->stripe_cache
));
788 WARN_ON(!list_empty(&rbio
->hash_list
));
789 WARN_ON(!bio_list_empty(&rbio
->bio_list
));
791 for (i
= 0; i
< rbio
->nr_pages
; i
++) {
792 if (rbio
->stripe_pages
[i
]) {
793 __free_page(rbio
->stripe_pages
[i
]);
794 rbio
->stripe_pages
[i
] = NULL
;
797 kfree(rbio
->raid_map
);
802 static void free_raid_bio(struct btrfs_raid_bio
*rbio
)
805 __free_raid_bio(rbio
);
809 * this frees the rbio and runs through all the bios in the
810 * bio_list and calls end_io on them
812 static void rbio_orig_end_io(struct btrfs_raid_bio
*rbio
, int err
, int uptodate
)
814 struct bio
*cur
= bio_list_get(&rbio
->bio_list
);
822 set_bit(BIO_UPTODATE
, &cur
->bi_flags
);
829 * end io function used by finish_rmw. When we finally
830 * get here, we've written a full stripe
832 static void raid_write_end_io(struct bio
*bio
, int err
)
834 struct btrfs_raid_bio
*rbio
= bio
->bi_private
;
837 fail_bio_stripe(rbio
, bio
);
841 if (!atomic_dec_and_test(&rbio
->bbio
->stripes_pending
))
846 /* OK, we have read all the stripes we need to. */
847 if (atomic_read(&rbio
->bbio
->error
) > rbio
->bbio
->max_errors
)
850 rbio_orig_end_io(rbio
, err
, 0);
855 * the read/modify/write code wants to use the original bio for
856 * any pages it included, and then use the rbio for everything
857 * else. This function decides if a given index (stripe number)
858 * and page number in that stripe fall inside the original bio
861 * if you set bio_list_only, you'll get a NULL back for any ranges
862 * that are outside the bio_list
864 * This doesn't take any refs on anything, you get a bare page pointer
865 * and the caller must bump refs as required.
867 * You must call index_rbio_pages once before you can trust
868 * the answers from this function.
870 static struct page
*page_in_rbio(struct btrfs_raid_bio
*rbio
,
871 int index
, int pagenr
, int bio_list_only
)
874 struct page
*p
= NULL
;
876 chunk_page
= index
* (rbio
->stripe_len
>> PAGE_SHIFT
) + pagenr
;
878 spin_lock_irq(&rbio
->bio_list_lock
);
879 p
= rbio
->bio_pages
[chunk_page
];
880 spin_unlock_irq(&rbio
->bio_list_lock
);
882 if (p
|| bio_list_only
)
885 return rbio
->stripe_pages
[chunk_page
];
889 * number of pages we need for the entire stripe across all the
892 static unsigned long rbio_nr_pages(unsigned long stripe_len
, int nr_stripes
)
894 unsigned long nr
= stripe_len
* nr_stripes
;
895 return (nr
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
899 * allocation and initial setup for the btrfs_raid_bio. Not
900 * this does not allocate any pages for rbio->pages.
902 static struct btrfs_raid_bio
*alloc_rbio(struct btrfs_root
*root
,
903 struct btrfs_bio
*bbio
, u64
*raid_map
,
906 struct btrfs_raid_bio
*rbio
;
908 int num_pages
= rbio_nr_pages(stripe_len
, bbio
->num_stripes
);
911 rbio
= kzalloc(sizeof(*rbio
) + num_pages
* sizeof(struct page
*) * 2,
916 return ERR_PTR(-ENOMEM
);
919 bio_list_init(&rbio
->bio_list
);
920 INIT_LIST_HEAD(&rbio
->plug_list
);
921 spin_lock_init(&rbio
->bio_list_lock
);
922 INIT_LIST_HEAD(&rbio
->stripe_cache
);
923 INIT_LIST_HEAD(&rbio
->hash_list
);
925 rbio
->raid_map
= raid_map
;
926 rbio
->fs_info
= root
->fs_info
;
927 rbio
->stripe_len
= stripe_len
;
928 rbio
->nr_pages
= num_pages
;
931 atomic_set(&rbio
->refs
, 1);
934 * the stripe_pages and bio_pages array point to the extra
935 * memory we allocated past the end of the rbio
938 rbio
->stripe_pages
= p
;
939 rbio
->bio_pages
= p
+ sizeof(struct page
*) * num_pages
;
941 if (raid_map
[bbio
->num_stripes
- 1] == RAID6_Q_STRIPE
)
942 nr_data
= bbio
->num_stripes
- 2;
944 nr_data
= bbio
->num_stripes
- 1;
946 rbio
->nr_data
= nr_data
;
950 /* allocate pages for all the stripes in the bio, including parity */
951 static int alloc_rbio_pages(struct btrfs_raid_bio
*rbio
)
956 for (i
= 0; i
< rbio
->nr_pages
; i
++) {
957 if (rbio
->stripe_pages
[i
])
959 page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
962 rbio
->stripe_pages
[i
] = page
;
963 ClearPageUptodate(page
);
968 /* allocate pages for just the p/q stripes */
969 static int alloc_rbio_parity_pages(struct btrfs_raid_bio
*rbio
)
974 i
= (rbio
->nr_data
* rbio
->stripe_len
) >> PAGE_CACHE_SHIFT
;
976 for (; i
< rbio
->nr_pages
; i
++) {
977 if (rbio
->stripe_pages
[i
])
979 page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
982 rbio
->stripe_pages
[i
] = page
;
988 * add a single page from a specific stripe into our list of bios for IO
989 * this will try to merge into existing bios if possible, and returns
990 * zero if all went well.
992 int rbio_add_io_page(struct btrfs_raid_bio
*rbio
,
993 struct bio_list
*bio_list
,
996 unsigned long page_index
,
997 unsigned long bio_max_len
)
999 struct bio
*last
= bio_list
->tail
;
1003 struct btrfs_bio_stripe
*stripe
;
1006 stripe
= &rbio
->bbio
->stripes
[stripe_nr
];
1007 disk_start
= stripe
->physical
+ (page_index
<< PAGE_CACHE_SHIFT
);
1009 /* if the device is missing, just fail this stripe */
1010 if (!stripe
->dev
->bdev
)
1011 return fail_rbio_index(rbio
, stripe_nr
);
1013 /* see if we can add this page onto our existing bio */
1015 last_end
= (u64
)last
->bi_sector
<< 9;
1016 last_end
+= last
->bi_size
;
1019 * we can't merge these if they are from different
1020 * devices or if they are not contiguous
1022 if (last_end
== disk_start
&& stripe
->dev
->bdev
&&
1023 test_bit(BIO_UPTODATE
, &last
->bi_flags
) &&
1024 last
->bi_bdev
== stripe
->dev
->bdev
) {
1025 ret
= bio_add_page(last
, page
, PAGE_CACHE_SIZE
, 0);
1026 if (ret
== PAGE_CACHE_SIZE
)
1031 /* put a new bio on the list */
1032 bio
= bio_alloc(GFP_NOFS
, bio_max_len
>> PAGE_SHIFT
?:1);
1037 bio
->bi_bdev
= stripe
->dev
->bdev
;
1038 bio
->bi_sector
= disk_start
>> 9;
1039 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1041 bio_add_page(bio
, page
, PAGE_CACHE_SIZE
, 0);
1042 bio_list_add(bio_list
, bio
);
1047 * while we're doing the read/modify/write cycle, we could
1048 * have errors in reading pages off the disk. This checks
1049 * for errors and if we're not able to read the page it'll
1050 * trigger parity reconstruction. The rmw will be finished
1051 * after we've reconstructed the failed stripes
1053 static void validate_rbio_for_rmw(struct btrfs_raid_bio
*rbio
)
1055 if (rbio
->faila
>= 0 || rbio
->failb
>= 0) {
1056 BUG_ON(rbio
->faila
== rbio
->bbio
->num_stripes
- 1);
1057 __raid56_parity_recover(rbio
);
1064 * these are just the pages from the rbio array, not from anything
1065 * the FS sent down to us
1067 static struct page
*rbio_stripe_page(struct btrfs_raid_bio
*rbio
, int stripe
, int page
)
1070 index
= stripe
* (rbio
->stripe_len
>> PAGE_CACHE_SHIFT
);
1072 return rbio
->stripe_pages
[index
];
1076 * helper function to walk our bio list and populate the bio_pages array with
1077 * the result. This seems expensive, but it is faster than constantly
1078 * searching through the bio list as we setup the IO in finish_rmw or stripe
1081 * This must be called before you trust the answers from page_in_rbio
1083 static void index_rbio_pages(struct btrfs_raid_bio
*rbio
)
1087 unsigned long stripe_offset
;
1088 unsigned long page_index
;
1092 spin_lock_irq(&rbio
->bio_list_lock
);
1093 bio_list_for_each(bio
, &rbio
->bio_list
) {
1094 start
= (u64
)bio
->bi_sector
<< 9;
1095 stripe_offset
= start
- rbio
->raid_map
[0];
1096 page_index
= stripe_offset
>> PAGE_CACHE_SHIFT
;
1098 for (i
= 0; i
< bio
->bi_vcnt
; i
++) {
1099 p
= bio
->bi_io_vec
[i
].bv_page
;
1100 rbio
->bio_pages
[page_index
+ i
] = p
;
1103 spin_unlock_irq(&rbio
->bio_list_lock
);
1107 * this is called from one of two situations. We either
1108 * have a full stripe from the higher layers, or we've read all
1109 * the missing bits off disk.
1111 * This will calculate the parity and then send down any
1114 static noinline
void finish_rmw(struct btrfs_raid_bio
*rbio
)
1116 struct btrfs_bio
*bbio
= rbio
->bbio
;
1117 void *pointers
[bbio
->num_stripes
];
1118 int stripe_len
= rbio
->stripe_len
;
1119 int nr_data
= rbio
->nr_data
;
1124 struct bio_list bio_list
;
1126 int pages_per_stripe
= stripe_len
>> PAGE_CACHE_SHIFT
;
1129 bio_list_init(&bio_list
);
1131 if (bbio
->num_stripes
- rbio
->nr_data
== 1) {
1132 p_stripe
= bbio
->num_stripes
- 1;
1133 } else if (bbio
->num_stripes
- rbio
->nr_data
== 2) {
1134 p_stripe
= bbio
->num_stripes
- 2;
1135 q_stripe
= bbio
->num_stripes
- 1;
1140 /* at this point we either have a full stripe,
1141 * or we've read the full stripe from the drive.
1142 * recalculate the parity and write the new results.
1144 * We're not allowed to add any new bios to the
1145 * bio list here, anyone else that wants to
1146 * change this stripe needs to do their own rmw.
1148 spin_lock_irq(&rbio
->bio_list_lock
);
1149 set_bit(RBIO_RMW_LOCKED_BIT
, &rbio
->flags
);
1150 spin_unlock_irq(&rbio
->bio_list_lock
);
1152 atomic_set(&rbio
->bbio
->error
, 0);
1155 * now that we've set rmw_locked, run through the
1156 * bio list one last time and map the page pointers
1158 * We don't cache full rbios because we're assuming
1159 * the higher layers are unlikely to use this area of
1160 * the disk again soon. If they do use it again,
1161 * hopefully they will send another full bio.
1163 index_rbio_pages(rbio
);
1164 if (!rbio_is_full(rbio
))
1165 cache_rbio_pages(rbio
);
1167 clear_bit(RBIO_CACHE_READY_BIT
, &rbio
->flags
);
1169 for (pagenr
= 0; pagenr
< pages_per_stripe
; pagenr
++) {
1171 /* first collect one page from each data stripe */
1172 for (stripe
= 0; stripe
< nr_data
; stripe
++) {
1173 p
= page_in_rbio(rbio
, stripe
, pagenr
, 0);
1174 pointers
[stripe
] = kmap(p
);
1177 /* then add the parity stripe */
1178 p
= rbio_pstripe_page(rbio
, pagenr
);
1180 pointers
[stripe
++] = kmap(p
);
1182 if (q_stripe
!= -1) {
1185 * raid6, add the qstripe and call the
1186 * library function to fill in our p/q
1188 p
= rbio_qstripe_page(rbio
, pagenr
);
1190 pointers
[stripe
++] = kmap(p
);
1192 raid6_call
.gen_syndrome(bbio
->num_stripes
, PAGE_SIZE
,
1196 memcpy(pointers
[nr_data
], pointers
[0], PAGE_SIZE
);
1197 run_xor(pointers
+ 1, nr_data
- 1, PAGE_CACHE_SIZE
);
1201 for (stripe
= 0; stripe
< bbio
->num_stripes
; stripe
++)
1202 kunmap(page_in_rbio(rbio
, stripe
, pagenr
, 0));
1206 * time to start writing. Make bios for everything from the
1207 * higher layers (the bio_list in our rbio) and our p/q. Ignore
1210 for (stripe
= 0; stripe
< bbio
->num_stripes
; stripe
++) {
1211 for (pagenr
= 0; pagenr
< pages_per_stripe
; pagenr
++) {
1213 if (stripe
< rbio
->nr_data
) {
1214 page
= page_in_rbio(rbio
, stripe
, pagenr
, 1);
1218 page
= rbio_stripe_page(rbio
, stripe
, pagenr
);
1221 ret
= rbio_add_io_page(rbio
, &bio_list
,
1222 page
, stripe
, pagenr
, rbio
->stripe_len
);
1228 atomic_set(&bbio
->stripes_pending
, bio_list_size(&bio_list
));
1229 BUG_ON(atomic_read(&bbio
->stripes_pending
) == 0);
1232 bio
= bio_list_pop(&bio_list
);
1236 bio
->bi_private
= rbio
;
1237 bio
->bi_end_io
= raid_write_end_io
;
1238 BUG_ON(!test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
1239 submit_bio(WRITE
, bio
);
1244 rbio_orig_end_io(rbio
, -EIO
, 0);
1248 * helper to find the stripe number for a given bio. Used to figure out which
1249 * stripe has failed. This expects the bio to correspond to a physical disk,
1250 * so it looks up based on physical sector numbers.
1252 static int find_bio_stripe(struct btrfs_raid_bio
*rbio
,
1255 u64 physical
= bio
->bi_sector
;
1258 struct btrfs_bio_stripe
*stripe
;
1262 for (i
= 0; i
< rbio
->bbio
->num_stripes
; i
++) {
1263 stripe
= &rbio
->bbio
->stripes
[i
];
1264 stripe_start
= stripe
->physical
;
1265 if (physical
>= stripe_start
&&
1266 physical
< stripe_start
+ rbio
->stripe_len
) {
1274 * helper to find the stripe number for a given
1275 * bio (before mapping). Used to figure out which stripe has
1276 * failed. This looks up based on logical block numbers.
1278 static int find_logical_bio_stripe(struct btrfs_raid_bio
*rbio
,
1281 u64 logical
= bio
->bi_sector
;
1287 for (i
= 0; i
< rbio
->nr_data
; i
++) {
1288 stripe_start
= rbio
->raid_map
[i
];
1289 if (logical
>= stripe_start
&&
1290 logical
< stripe_start
+ rbio
->stripe_len
) {
1298 * returns -EIO if we had too many failures
1300 static int fail_rbio_index(struct btrfs_raid_bio
*rbio
, int failed
)
1302 unsigned long flags
;
1305 spin_lock_irqsave(&rbio
->bio_list_lock
, flags
);
1307 /* we already know this stripe is bad, move on */
1308 if (rbio
->faila
== failed
|| rbio
->failb
== failed
)
1311 if (rbio
->faila
== -1) {
1312 /* first failure on this rbio */
1313 rbio
->faila
= failed
;
1314 atomic_inc(&rbio
->bbio
->error
);
1315 } else if (rbio
->failb
== -1) {
1316 /* second failure on this rbio */
1317 rbio
->failb
= failed
;
1318 atomic_inc(&rbio
->bbio
->error
);
1323 spin_unlock_irqrestore(&rbio
->bio_list_lock
, flags
);
1329 * helper to fail a stripe based on a physical disk
1332 static int fail_bio_stripe(struct btrfs_raid_bio
*rbio
,
1335 int failed
= find_bio_stripe(rbio
, bio
);
1340 return fail_rbio_index(rbio
, failed
);
1344 * this sets each page in the bio uptodate. It should only be used on private
1345 * rbio pages, nothing that comes in from the higher layers
1347 static void set_bio_pages_uptodate(struct bio
*bio
)
1352 for (i
= 0; i
< bio
->bi_vcnt
; i
++) {
1353 p
= bio
->bi_io_vec
[i
].bv_page
;
1359 * end io for the read phase of the rmw cycle. All the bios here are physical
1360 * stripe bios we've read from the disk so we can recalculate the parity of the
1363 * This will usually kick off finish_rmw once all the bios are read in, but it
1364 * may trigger parity reconstruction if we had any errors along the way
1366 static void raid_rmw_end_io(struct bio
*bio
, int err
)
1368 struct btrfs_raid_bio
*rbio
= bio
->bi_private
;
1371 fail_bio_stripe(rbio
, bio
);
1373 set_bio_pages_uptodate(bio
);
1377 if (!atomic_dec_and_test(&rbio
->bbio
->stripes_pending
))
1381 if (atomic_read(&rbio
->bbio
->error
) > rbio
->bbio
->max_errors
)
1385 * this will normally call finish_rmw to start our write
1386 * but if there are any failed stripes we'll reconstruct
1389 validate_rbio_for_rmw(rbio
);
1394 rbio_orig_end_io(rbio
, -EIO
, 0);
1397 static void async_rmw_stripe(struct btrfs_raid_bio
*rbio
)
1399 rbio
->work
.flags
= 0;
1400 rbio
->work
.func
= rmw_work
;
1402 btrfs_queue_worker(&rbio
->fs_info
->rmw_workers
,
1406 static void async_read_rebuild(struct btrfs_raid_bio
*rbio
)
1408 rbio
->work
.flags
= 0;
1409 rbio
->work
.func
= read_rebuild_work
;
1411 btrfs_queue_worker(&rbio
->fs_info
->rmw_workers
,
1416 * the stripe must be locked by the caller. It will
1417 * unlock after all the writes are done
1419 static int raid56_rmw_stripe(struct btrfs_raid_bio
*rbio
)
1421 int bios_to_read
= 0;
1422 struct btrfs_bio
*bbio
= rbio
->bbio
;
1423 struct bio_list bio_list
;
1425 int nr_pages
= (rbio
->stripe_len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1430 bio_list_init(&bio_list
);
1432 ret
= alloc_rbio_pages(rbio
);
1436 index_rbio_pages(rbio
);
1438 atomic_set(&rbio
->bbio
->error
, 0);
1440 * build a list of bios to read all the missing parts of this
1443 for (stripe
= 0; stripe
< rbio
->nr_data
; stripe
++) {
1444 for (pagenr
= 0; pagenr
< nr_pages
; pagenr
++) {
1447 * we want to find all the pages missing from
1448 * the rbio and read them from the disk. If
1449 * page_in_rbio finds a page in the bio list
1450 * we don't need to read it off the stripe.
1452 page
= page_in_rbio(rbio
, stripe
, pagenr
, 1);
1456 page
= rbio_stripe_page(rbio
, stripe
, pagenr
);
1458 * the bio cache may have handed us an uptodate
1459 * page. If so, be happy and use it
1461 if (PageUptodate(page
))
1464 ret
= rbio_add_io_page(rbio
, &bio_list
, page
,
1465 stripe
, pagenr
, rbio
->stripe_len
);
1471 bios_to_read
= bio_list_size(&bio_list
);
1472 if (!bios_to_read
) {
1474 * this can happen if others have merged with
1475 * us, it means there is nothing left to read.
1476 * But if there are missing devices it may not be
1477 * safe to do the full stripe write yet.
1483 * the bbio may be freed once we submit the last bio. Make sure
1484 * not to touch it after that
1486 atomic_set(&bbio
->stripes_pending
, bios_to_read
);
1488 bio
= bio_list_pop(&bio_list
);
1492 bio
->bi_private
= rbio
;
1493 bio
->bi_end_io
= raid_rmw_end_io
;
1495 btrfs_bio_wq_end_io(rbio
->fs_info
, bio
,
1496 BTRFS_WQ_ENDIO_RAID56
);
1498 BUG_ON(!test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
1499 submit_bio(READ
, bio
);
1501 /* the actual write will happen once the reads are done */
1505 rbio_orig_end_io(rbio
, -EIO
, 0);
1509 validate_rbio_for_rmw(rbio
);
1514 * if the upper layers pass in a full stripe, we thank them by only allocating
1515 * enough pages to hold the parity, and sending it all down quickly.
1517 static int full_stripe_write(struct btrfs_raid_bio
*rbio
)
1521 ret
= alloc_rbio_parity_pages(rbio
);
1525 ret
= lock_stripe_add(rbio
);
1532 * partial stripe writes get handed over to async helpers.
1533 * We're really hoping to merge a few more writes into this
1534 * rbio before calculating new parity
1536 static int partial_stripe_write(struct btrfs_raid_bio
*rbio
)
1540 ret
= lock_stripe_add(rbio
);
1542 async_rmw_stripe(rbio
);
1547 * sometimes while we were reading from the drive to
1548 * recalculate parity, enough new bios come into create
1549 * a full stripe. So we do a check here to see if we can
1550 * go directly to finish_rmw
1552 static int __raid56_parity_write(struct btrfs_raid_bio
*rbio
)
1554 /* head off into rmw land if we don't have a full stripe */
1555 if (!rbio_is_full(rbio
))
1556 return partial_stripe_write(rbio
);
1557 return full_stripe_write(rbio
);
1561 * our main entry point for writes from the rest of the FS.
1563 int raid56_parity_write(struct btrfs_root
*root
, struct bio
*bio
,
1564 struct btrfs_bio
*bbio
, u64
*raid_map
,
1567 struct btrfs_raid_bio
*rbio
;
1569 rbio
= alloc_rbio(root
, bbio
, raid_map
, stripe_len
);
1573 return PTR_ERR(rbio
);
1575 bio_list_add(&rbio
->bio_list
, bio
);
1576 rbio
->bio_list_bytes
= bio
->bi_size
;
1577 return __raid56_parity_write(rbio
);
1581 * all parity reconstruction happens here. We've read in everything
1582 * we can find from the drives and this does the heavy lifting of
1583 * sorting the good from the bad.
1585 static void __raid_recover_end_io(struct btrfs_raid_bio
*rbio
)
1589 int faila
= -1, failb
= -1;
1590 int nr_pages
= (rbio
->stripe_len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1595 pointers
= kzalloc(rbio
->bbio
->num_stripes
* sizeof(void *),
1602 faila
= rbio
->faila
;
1603 failb
= rbio
->failb
;
1605 if (rbio
->read_rebuild
) {
1606 spin_lock_irq(&rbio
->bio_list_lock
);
1607 set_bit(RBIO_RMW_LOCKED_BIT
, &rbio
->flags
);
1608 spin_unlock_irq(&rbio
->bio_list_lock
);
1611 index_rbio_pages(rbio
);
1613 for (pagenr
= 0; pagenr
< nr_pages
; pagenr
++) {
1614 /* setup our array of pointers with pages
1617 for (stripe
= 0; stripe
< rbio
->bbio
->num_stripes
; stripe
++) {
1619 * if we're rebuilding a read, we have to use
1620 * pages from the bio list
1622 if (rbio
->read_rebuild
&&
1623 (stripe
== faila
|| stripe
== failb
)) {
1624 page
= page_in_rbio(rbio
, stripe
, pagenr
, 0);
1626 page
= rbio_stripe_page(rbio
, stripe
, pagenr
);
1628 pointers
[stripe
] = kmap(page
);
1631 /* all raid6 handling here */
1632 if (rbio
->raid_map
[rbio
->bbio
->num_stripes
- 1] ==
1636 * single failure, rebuild from parity raid5
1640 if (faila
== rbio
->nr_data
) {
1642 * Just the P stripe has failed, without
1643 * a bad data or Q stripe.
1644 * TODO, we should redo the xor here.
1650 * a single failure in raid6 is rebuilt
1651 * in the pstripe code below
1656 /* make sure our ps and qs are in order */
1657 if (faila
> failb
) {
1663 /* if the q stripe is failed, do a pstripe reconstruction
1665 * If both the q stripe and the P stripe are failed, we're
1666 * here due to a crc mismatch and we can't give them the
1669 if (rbio
->raid_map
[failb
] == RAID6_Q_STRIPE
) {
1670 if (rbio
->raid_map
[faila
] == RAID5_P_STRIPE
) {
1675 * otherwise we have one bad data stripe and
1676 * a good P stripe. raid5!
1681 if (rbio
->raid_map
[failb
] == RAID5_P_STRIPE
) {
1682 raid6_datap_recov(rbio
->bbio
->num_stripes
,
1683 PAGE_SIZE
, faila
, pointers
);
1685 raid6_2data_recov(rbio
->bbio
->num_stripes
,
1686 PAGE_SIZE
, faila
, failb
,
1692 /* rebuild from P stripe here (raid5 or raid6) */
1693 BUG_ON(failb
!= -1);
1695 /* Copy parity block into failed block to start with */
1696 memcpy(pointers
[faila
],
1697 pointers
[rbio
->nr_data
],
1700 /* rearrange the pointer array */
1701 p
= pointers
[faila
];
1702 for (stripe
= faila
; stripe
< rbio
->nr_data
- 1; stripe
++)
1703 pointers
[stripe
] = pointers
[stripe
+ 1];
1704 pointers
[rbio
->nr_data
- 1] = p
;
1706 /* xor in the rest */
1707 run_xor(pointers
, rbio
->nr_data
- 1, PAGE_CACHE_SIZE
);
1709 /* if we're doing this rebuild as part of an rmw, go through
1710 * and set all of our private rbio pages in the
1711 * failed stripes as uptodate. This way finish_rmw will
1712 * know they can be trusted. If this was a read reconstruction,
1713 * other endio functions will fiddle the uptodate bits
1715 if (!rbio
->read_rebuild
) {
1716 for (i
= 0; i
< nr_pages
; i
++) {
1718 page
= rbio_stripe_page(rbio
, faila
, i
);
1719 SetPageUptodate(page
);
1722 page
= rbio_stripe_page(rbio
, failb
, i
);
1723 SetPageUptodate(page
);
1727 for (stripe
= 0; stripe
< rbio
->bbio
->num_stripes
; stripe
++) {
1729 * if we're rebuilding a read, we have to use
1730 * pages from the bio list
1732 if (rbio
->read_rebuild
&&
1733 (stripe
== faila
|| stripe
== failb
)) {
1734 page
= page_in_rbio(rbio
, stripe
, pagenr
, 0);
1736 page
= rbio_stripe_page(rbio
, stripe
, pagenr
);
1748 if (rbio
->read_rebuild
) {
1750 cache_rbio_pages(rbio
);
1752 clear_bit(RBIO_CACHE_READY_BIT
, &rbio
->flags
);
1754 rbio_orig_end_io(rbio
, err
, err
== 0);
1755 } else if (err
== 0) {
1760 rbio_orig_end_io(rbio
, err
, 0);
1765 * This is called only for stripes we've read from disk to
1766 * reconstruct the parity.
1768 static void raid_recover_end_io(struct bio
*bio
, int err
)
1770 struct btrfs_raid_bio
*rbio
= bio
->bi_private
;
1773 * we only read stripe pages off the disk, set them
1774 * up to date if there were no errors
1777 fail_bio_stripe(rbio
, bio
);
1779 set_bio_pages_uptodate(bio
);
1782 if (!atomic_dec_and_test(&rbio
->bbio
->stripes_pending
))
1785 if (atomic_read(&rbio
->bbio
->error
) > rbio
->bbio
->max_errors
)
1786 rbio_orig_end_io(rbio
, -EIO
, 0);
1788 __raid_recover_end_io(rbio
);
1792 * reads everything we need off the disk to reconstruct
1793 * the parity. endio handlers trigger final reconstruction
1794 * when the IO is done.
1796 * This is used both for reads from the higher layers and for
1797 * parity construction required to finish a rmw cycle.
1799 static int __raid56_parity_recover(struct btrfs_raid_bio
*rbio
)
1801 int bios_to_read
= 0;
1802 struct btrfs_bio
*bbio
= rbio
->bbio
;
1803 struct bio_list bio_list
;
1805 int nr_pages
= (rbio
->stripe_len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1810 bio_list_init(&bio_list
);
1812 ret
= alloc_rbio_pages(rbio
);
1816 atomic_set(&rbio
->bbio
->error
, 0);
1819 * read everything that hasn't failed. Thanks to the
1820 * stripe cache, it is possible that some or all of these
1821 * pages are going to be uptodate.
1823 for (stripe
= 0; stripe
< bbio
->num_stripes
; stripe
++) {
1824 if (rbio
->faila
== stripe
||
1825 rbio
->failb
== stripe
)
1828 for (pagenr
= 0; pagenr
< nr_pages
; pagenr
++) {
1832 * the rmw code may have already read this
1835 p
= rbio_stripe_page(rbio
, stripe
, pagenr
);
1836 if (PageUptodate(p
))
1839 ret
= rbio_add_io_page(rbio
, &bio_list
,
1840 rbio_stripe_page(rbio
, stripe
, pagenr
),
1841 stripe
, pagenr
, rbio
->stripe_len
);
1847 bios_to_read
= bio_list_size(&bio_list
);
1848 if (!bios_to_read
) {
1850 * we might have no bios to read just because the pages
1851 * were up to date, or we might have no bios to read because
1852 * the devices were gone.
1854 if (atomic_read(&rbio
->bbio
->error
) <= rbio
->bbio
->max_errors
) {
1855 __raid_recover_end_io(rbio
);
1863 * the bbio may be freed once we submit the last bio. Make sure
1864 * not to touch it after that
1866 atomic_set(&bbio
->stripes_pending
, bios_to_read
);
1868 bio
= bio_list_pop(&bio_list
);
1872 bio
->bi_private
= rbio
;
1873 bio
->bi_end_io
= raid_recover_end_io
;
1875 btrfs_bio_wq_end_io(rbio
->fs_info
, bio
,
1876 BTRFS_WQ_ENDIO_RAID56
);
1878 BUG_ON(!test_bit(BIO_UPTODATE
, &bio
->bi_flags
));
1879 submit_bio(READ
, bio
);
1885 if (rbio
->read_rebuild
)
1886 rbio_orig_end_io(rbio
, -EIO
, 0);
1891 * the main entry point for reads from the higher layers. This
1892 * is really only called when the normal read path had a failure,
1893 * so we assume the bio they send down corresponds to a failed part
1896 int raid56_parity_recover(struct btrfs_root
*root
, struct bio
*bio
,
1897 struct btrfs_bio
*bbio
, u64
*raid_map
,
1898 u64 stripe_len
, int mirror_num
)
1900 struct btrfs_raid_bio
*rbio
;
1903 rbio
= alloc_rbio(root
, bbio
, raid_map
, stripe_len
);
1905 return PTR_ERR(rbio
);
1908 rbio
->read_rebuild
= 1;
1909 bio_list_add(&rbio
->bio_list
, bio
);
1910 rbio
->bio_list_bytes
= bio
->bi_size
;
1912 rbio
->faila
= find_logical_bio_stripe(rbio
, bio
);
1913 if (rbio
->faila
== -1) {
1920 * reconstruct from the q stripe if they are
1921 * asking for mirror 3
1923 if (mirror_num
== 3)
1924 rbio
->failb
= bbio
->num_stripes
- 2;
1926 ret
= lock_stripe_add(rbio
);
1929 * __raid56_parity_recover will end the bio with
1930 * any errors it hits. We don't want to return
1931 * its error value up the stack because our caller
1932 * will end up calling bio_endio with any nonzero
1936 __raid56_parity_recover(rbio
);
1938 * our rbio has been added to the list of
1939 * rbios that will be handled after the
1940 * currently lock owner is done
1946 static void rmw_work(struct btrfs_work
*work
)
1948 struct btrfs_raid_bio
*rbio
;
1950 rbio
= container_of(work
, struct btrfs_raid_bio
, work
);
1951 raid56_rmw_stripe(rbio
);
1954 static void read_rebuild_work(struct btrfs_work
*work
)
1956 struct btrfs_raid_bio
*rbio
;
1958 rbio
= container_of(work
, struct btrfs_raid_bio
, work
);
1959 __raid56_parity_recover(rbio
);