Skip to content

Commit 9845e5d

Browse files
Christoph Hellwigkdave
authored andcommitted
btrfs: merge end_write_bio and flush_write_bio
Merge end_write_bio and flush_write_bio into a single submit_write_bio helper, that either submits the bio or ends it if a negative errno was passed in. This consolidates a lot of duplicated checks in the callers. Reviewed-by: Qu Wenruo <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 2d5ac13 commit 9845e5d

File tree

1 file changed

+29
-65
lines changed

1 file changed

+29
-65
lines changed

fs/btrfs/extent_io.c

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -201,39 +201,26 @@ static void submit_one_bio(struct bio *bio, int mirror_num,
201201
*/
202202
}
203203

204-
/* Cleanup unsubmitted bios */
205-
static void end_write_bio(struct extent_page_data *epd, int ret)
206-
{
207-
struct bio *bio = epd->bio_ctrl.bio;
208-
209-
if (bio) {
210-
bio->bi_status = errno_to_blk_status(ret);
211-
bio_endio(bio);
212-
epd->bio_ctrl.bio = NULL;
213-
}
214-
}
215-
216204
/*
217-
* Submit bio from extent page data via submit_one_bio
218-
*
219-
* Return 0 if everything is OK.
220-
* Return <0 for error.
205+
* Submit or fail the current bio in an extent_page_data structure.
221206
*/
222-
static void flush_write_bio(struct extent_page_data *epd)
207+
static void submit_write_bio(struct extent_page_data *epd, int ret)
223208
{
224209
struct bio *bio = epd->bio_ctrl.bio;
225210

226-
if (bio) {
211+
if (!bio)
212+
return;
213+
214+
if (ret) {
215+
ASSERT(ret < 0);
216+
bio->bi_status = errno_to_blk_status(ret);
217+
bio_endio(bio);
218+
} else {
227219
submit_one_bio(bio, 0, 0);
228-
/*
229-
* Clean up of epd->bio is handled by its endio function.
230-
* And endio is either triggered by successful bio execution
231-
* or the error handler of submit bio hook.
232-
* So at this point, no matter what happened, we don't need
233-
* to clean up epd->bio.
234-
*/
235-
epd->bio_ctrl.bio = NULL;
236220
}
221+
222+
/* The bio is owned by the bi_end_io handler now */
223+
epd->bio_ctrl.bio = NULL;
237224
}
238225

239226
int __init extent_state_cache_init(void)
@@ -4251,7 +4238,7 @@ static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb
42514238
int ret = 0;
42524239

42534240
if (!btrfs_try_tree_write_lock(eb)) {
4254-
flush_write_bio(epd);
4241+
submit_write_bio(epd, 0);
42554242
flush = 1;
42564243
btrfs_tree_lock(eb);
42574244
}
@@ -4261,7 +4248,7 @@ static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb
42614248
if (!epd->sync_io)
42624249
return 0;
42634250
if (!flush) {
4264-
flush_write_bio(epd);
4251+
submit_write_bio(epd, 0);
42654252
flush = 1;
42664253
}
42674254
while (1) {
@@ -4308,7 +4295,7 @@ static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb
43084295

43094296
if (!trylock_page(p)) {
43104297
if (!flush) {
4311-
flush_write_bio(epd);
4298+
submit_write_bio(epd, 0);
43124299
flush = 1;
43134300
}
43144301
lock_page(p);
@@ -4724,7 +4711,7 @@ static int submit_eb_subpage(struct page *page,
47244711

47254712
cleanup:
47264713
/* We hit error, end bio for the submitted extent buffers */
4727-
end_write_bio(epd, ret);
4714+
submit_write_bio(epd, ret);
47284715
return ret;
47294716
}
47304717

@@ -4903,10 +4890,6 @@ int btree_write_cache_pages(struct address_space *mapping,
49034890
index = 0;
49044891
goto retry;
49054892
}
4906-
if (ret < 0) {
4907-
end_write_bio(&epd, ret);
4908-
goto out;
4909-
}
49104893
/*
49114894
* If something went wrong, don't allow any metadata write bio to be
49124895
* submitted.
@@ -4933,21 +4916,17 @@ int btree_write_cache_pages(struct address_space *mapping,
49334916
* Now such dirty tree block will not be cleaned by any dirty
49344917
* extent io tree. Thus we don't want to submit such wild eb
49354918
* if the fs already has error.
4936-
*/
4937-
if (!BTRFS_FS_ERROR(fs_info)) {
4938-
flush_write_bio(&epd);
4939-
} else {
4940-
ret = -EROFS;
4941-
end_write_bio(&epd, ret);
4942-
}
4943-
out:
4944-
btrfs_zoned_meta_io_unlock(fs_info);
4945-
/*
4919+
*
49464920
* We can get ret > 0 from submit_extent_page() indicating how many ebs
49474921
* were submitted. Reset it to 0 to avoid false alerts for the caller.
49484922
*/
49494923
if (ret > 0)
49504924
ret = 0;
4925+
if (!ret && BTRFS_FS_ERROR(fs_info))
4926+
ret = -EROFS;
4927+
submit_write_bio(&epd, ret);
4928+
4929+
btrfs_zoned_meta_io_unlock(fs_info);
49514930
return ret;
49524931
}
49534932

@@ -5049,7 +5028,7 @@ static int extent_write_cache_pages(struct address_space *mapping,
50495028
* tmpfs file mapping
50505029
*/
50515030
if (!trylock_page(page)) {
5052-
flush_write_bio(epd);
5031+
submit_write_bio(epd, 0);
50535032
lock_page(page);
50545033
}
50555034

@@ -5060,7 +5039,7 @@ static int extent_write_cache_pages(struct address_space *mapping,
50605039

50615040
if (wbc->sync_mode != WB_SYNC_NONE) {
50625041
if (PageWriteback(page))
5063-
flush_write_bio(epd);
5042+
submit_write_bio(epd, 0);
50645043
wait_on_page_writeback(page);
50655044
}
50665045

@@ -5100,7 +5079,7 @@ static int extent_write_cache_pages(struct address_space *mapping,
51005079
* page in our current bio, and thus deadlock, so flush the
51015080
* write bio here.
51025081
*/
5103-
flush_write_bio(epd);
5082+
submit_write_bio(epd, 0);
51045083
goto retry;
51055084
}
51065085

@@ -5121,13 +5100,7 @@ int extent_write_full_page(struct page *page, struct writeback_control *wbc)
51215100
};
51225101

51235102
ret = __extent_writepage(page, wbc, &epd);
5124-
ASSERT(ret <= 0);
5125-
if (ret < 0) {
5126-
end_write_bio(&epd, ret);
5127-
return ret;
5128-
}
5129-
5130-
flush_write_bio(&epd);
5103+
submit_write_bio(&epd, ret);
51315104
return ret;
51325105
}
51335106

@@ -5188,10 +5161,7 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end)
51885161
cur = cur_end + 1;
51895162
}
51905163

5191-
if (!found_error)
5192-
flush_write_bio(&epd);
5193-
else
5194-
end_write_bio(&epd, ret);
5164+
submit_write_bio(&epd, found_error ? ret : 0);
51955165

51965166
wbc_detach_inode(&wbc_writepages);
51975167
if (found_error)
@@ -5216,13 +5186,7 @@ int extent_writepages(struct address_space *mapping,
52165186
*/
52175187
btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
52185188
ret = extent_write_cache_pages(mapping, wbc, &epd);
5219-
ASSERT(ret <= 0);
5220-
if (ret < 0) {
5221-
btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
5222-
end_write_bio(&epd, ret);
5223-
return ret;
5224-
}
5225-
flush_write_bio(&epd);
5189+
submit_write_bio(&epd, ret);
52265190
btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
52275191
return ret;
52285192
}

0 commit comments

Comments
 (0)