diff options
author | Kent Overstreet <kmo@daterainc.com> | 2013-11-23 21:34:15 -0500 |
---|---|---|
committer | Kent Overstreet <kmo@daterainc.com> | 2013-11-24 01:33:56 -0500 |
commit | 196d38bccfcfa32faed8c561868336fdfa0fe8e4 (patch) | |
tree | 9dee2bc174436072c9e90ffebbe71f47fde63aaf /fs/bio.c | |
parent | e90abc8ec323c1fd2a25600097ef7ae1e91f39b0 (diff) |
block: Generic bio chaining
This adds a generic mechanism for chaining bio completions. This is
going to be used for a bio_split() replacement, and it turns out to be
very useful in a fair amount of driver code - a fair number of drivers
were implementing this in their own roundabout ways, often painfully.
Note that this means it's no longer to call bio_endio() more than once
on the same bio! This can cause problems for drivers that save/restore
bi_end_io. Arguably they shouldn't be saving/restoring bi_end_io at all
- in all but the simplest cases they'd be better off just cloning the
bio, and immutable biovecs is making bio cloning cheaper. But for now,
we add a bio_endio_nodec() for these cases.
Signed-off-by: Kent Overstreet <kmo@daterainc.com>
Cc: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'fs/bio.c')
-rw-r--r-- | fs/bio.c | 76 |
1 files changed, 70 insertions, 6 deletions
@@ -273,6 +273,7 @@ void bio_init(struct bio *bio) | |||
273 | { | 273 | { |
274 | memset(bio, 0, sizeof(*bio)); | 274 | memset(bio, 0, sizeof(*bio)); |
275 | bio->bi_flags = 1 << BIO_UPTODATE; | 275 | bio->bi_flags = 1 << BIO_UPTODATE; |
276 | atomic_set(&bio->bi_remaining, 1); | ||
276 | atomic_set(&bio->bi_cnt, 1); | 277 | atomic_set(&bio->bi_cnt, 1); |
277 | } | 278 | } |
278 | EXPORT_SYMBOL(bio_init); | 279 | EXPORT_SYMBOL(bio_init); |
@@ -295,9 +296,35 @@ void bio_reset(struct bio *bio) | |||
295 | 296 | ||
296 | memset(bio, 0, BIO_RESET_BYTES); | 297 | memset(bio, 0, BIO_RESET_BYTES); |
297 | bio->bi_flags = flags|(1 << BIO_UPTODATE); | 298 | bio->bi_flags = flags|(1 << BIO_UPTODATE); |
299 | atomic_set(&bio->bi_remaining, 1); | ||
298 | } | 300 | } |
299 | EXPORT_SYMBOL(bio_reset); | 301 | EXPORT_SYMBOL(bio_reset); |
300 | 302 | ||
303 | static void bio_chain_endio(struct bio *bio, int error) | ||
304 | { | ||
305 | bio_endio(bio->bi_private, error); | ||
306 | bio_put(bio); | ||
307 | } | ||
308 | |||
309 | /** | ||
310 | * bio_chain - chain bio completions | ||
311 | * | ||
312 | * The caller won't have a bi_end_io called when @bio completes - instead, | ||
313 | * @parent's bi_end_io won't be called until both @parent and @bio have | ||
314 | * completed; the chained bio will also be freed when it completes. | ||
315 | * | ||
316 | * The caller must not set bi_private or bi_end_io in @bio. | ||
317 | */ | ||
318 | void bio_chain(struct bio *bio, struct bio *parent) | ||
319 | { | ||
320 | BUG_ON(bio->bi_private || bio->bi_end_io); | ||
321 | |||
322 | bio->bi_private = parent; | ||
323 | bio->bi_end_io = bio_chain_endio; | ||
324 | atomic_inc(&parent->bi_remaining); | ||
325 | } | ||
326 | EXPORT_SYMBOL(bio_chain); | ||
327 | |||
301 | static void bio_alloc_rescue(struct work_struct *work) | 328 | static void bio_alloc_rescue(struct work_struct *work) |
302 | { | 329 | { |
303 | struct bio_set *bs = container_of(work, struct bio_set, rescue_work); | 330 | struct bio_set *bs = container_of(work, struct bio_set, rescue_work); |
@@ -1719,16 +1746,53 @@ EXPORT_SYMBOL(bio_flush_dcache_pages); | |||
1719 | **/ | 1746 | **/ |
1720 | void bio_endio(struct bio *bio, int error) | 1747 | void bio_endio(struct bio *bio, int error) |
1721 | { | 1748 | { |
1722 | if (error) | 1749 | while (bio) { |
1723 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | 1750 | BUG_ON(atomic_read(&bio->bi_remaining) <= 0); |
1724 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | 1751 | |
1725 | error = -EIO; | 1752 | if (error) |
1753 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | ||
1754 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | ||
1755 | error = -EIO; | ||
1756 | |||
1757 | if (!atomic_dec_and_test(&bio->bi_remaining)) | ||
1758 | return; | ||
1726 | 1759 | ||
1727 | if (bio->bi_end_io) | 1760 | /* |
1728 | bio->bi_end_io(bio, error); | 1761 | * Need to have a real endio function for chained bios, |
1762 | * otherwise various corner cases will break (like stacking | ||
1763 | * block devices that save/restore bi_end_io) - however, we want | ||
1764 | * to avoid unbounded recursion and blowing the stack. Tail call | ||
1765 | * optimization would handle this, but compiling with frame | ||
1766 | * pointers also disables gcc's sibling call optimization. | ||
1767 | */ | ||
1768 | if (bio->bi_end_io == bio_chain_endio) { | ||
1769 | struct bio *parent = bio->bi_private; | ||
1770 | bio_put(bio); | ||
1771 | bio = parent; | ||
1772 | } else { | ||
1773 | if (bio->bi_end_io) | ||
1774 | bio->bi_end_io(bio, error); | ||
1775 | bio = NULL; | ||
1776 | } | ||
1777 | } | ||
1729 | } | 1778 | } |
1730 | EXPORT_SYMBOL(bio_endio); | 1779 | EXPORT_SYMBOL(bio_endio); |
1731 | 1780 | ||
1781 | /** | ||
1782 | * bio_endio_nodec - end I/O on a bio, without decrementing bi_remaining | ||
1783 | * @bio: bio | ||
1784 | * @error: error, if any | ||
1785 | * | ||
1786 | * For code that has saved and restored bi_end_io; thing hard before using this | ||
1787 | * function, probably you should've cloned the entire bio. | ||
1788 | **/ | ||
1789 | void bio_endio_nodec(struct bio *bio, int error) | ||
1790 | { | ||
1791 | atomic_inc(&bio->bi_remaining); | ||
1792 | bio_endio(bio, error); | ||
1793 | } | ||
1794 | EXPORT_SYMBOL(bio_endio_nodec); | ||
1795 | |||
1732 | void bio_pair_release(struct bio_pair *bp) | 1796 | void bio_pair_release(struct bio_pair *bp) |
1733 | { | 1797 | { |
1734 | if (atomic_dec_and_test(&bp->cnt)) { | 1798 | if (atomic_dec_and_test(&bp->cnt)) { |