Skip to content

Commit ffbd517

Browse files
committed
Btrfs: use WRITE_SYNC for synchronous writes
Part of reducing fsync/O_SYNC/O_DIRECT latencies is using WRITE_SYNC for writes we plan on waiting on in the near future. This patch mirrors recent changes in other filesystems and the generic code to use WRITE_SYNC when WB_SYNC_ALL is passed and to use WRITE_SYNC for other latency critical writes. Btrfs uses async worker threads for checksumming before the write is done, and then again to actually submit the bios. The bio submission code just runs a per-device list of bios that need to be sent down the pipe. This list is split into low priority and high priority lists so the WRITE_SYNC IO happens first. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 0882e8d commit ffbd517

5 files changed

Lines changed: 141 additions & 46 deletions

File tree

‎fs/btrfs/disk-io.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,10 +2095,10 @@ static int write_dev_supers(struct btrfs_device *device,
20952095
device->barriers = 0;
20962096
get_bh(bh);
20972097
lock_buffer(bh);
2098-
ret = submit_bh(WRITE, bh);
2098+
ret = submit_bh(WRITE_SYNC, bh);
20992099
}
21002100
} else {
2101-
ret = submit_bh(WRITE, bh);
2101+
ret = submit_bh(WRITE_SYNC, bh);
21022102
}
21032103

21042104
if (!ret && wait) {

‎fs/btrfs/extent_io.c‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ struct extent_page_data {
5050
/* tells writepage not to lock the state bits for this range
5151
* it still does the unlocking
5252
*/
53-
int extent_locked;
53+
unsigned int extent_locked:1;
54+
55+
/* tells the submit_bio code to use a WRITE_SYNC */
56+
unsigned int sync_io:1;
5457
};
5558

5659
int __init extent_io_init(void)
@@ -2136,8 +2139,14 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
21362139
u64 delalloc_end;
21372140
int page_started;
21382141
int compressed;
2142+
int write_flags;
21392143
unsigned long nr_written = 0;
21402144

2145+
if (wbc->sync_mode == WB_SYNC_ALL)
2146+
write_flags = WRITE_SYNC_PLUG;
2147+
else
2148+
write_flags = WRITE;
2149+
21412150
WARN_ON(!PageLocked(page));
21422151
pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
21432152
if (page->index > end_index ||
@@ -2314,9 +2323,9 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
23142323
(unsigned long long)end);
23152324
}
23162325

2317-
ret = submit_extent_page(WRITE, tree, page, sector,
2318-
iosize, pg_offset, bdev,
2319-
&epd->bio, max_nr,
2326+
ret = submit_extent_page(write_flags, tree, page,
2327+
sector, iosize, pg_offset,
2328+
bdev, &epd->bio, max_nr,
23202329
end_bio_extent_writepage,
23212330
0, 0, 0);
23222331
if (ret)
@@ -2460,15 +2469,23 @@ static int extent_write_cache_pages(struct extent_io_tree *tree,
24602469
return ret;
24612470
}
24622471

2463-
static noinline void flush_write_bio(void *data)
2472+
static void flush_epd_write_bio(struct extent_page_data *epd)
24642473
{
2465-
struct extent_page_data *epd = data;
24662474
if (epd->bio) {
2467-
submit_one_bio(WRITE, epd->bio, 0, 0);
2475+
if (epd->sync_io)
2476+
submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2477+
else
2478+
submit_one_bio(WRITE, epd->bio, 0, 0);
24682479
epd->bio = NULL;
24692480
}
24702481
}
24712482

2483+
static noinline void flush_write_bio(void *data)
2484+
{
2485+
struct extent_page_data *epd = data;
2486+
flush_epd_write_bio(epd);
2487+
}
2488+
24722489
int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
24732490
get_extent_t *get_extent,
24742491
struct writeback_control *wbc)
@@ -2480,6 +2497,7 @@ int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
24802497
.tree = tree,
24812498
.get_extent = get_extent,
24822499
.extent_locked = 0,
2500+
.sync_io = wbc->sync_mode == WB_SYNC_ALL,
24832501
};
24842502
struct writeback_control wbc_writepages = {
24852503
.bdi = wbc->bdi,
@@ -2490,13 +2508,11 @@ int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
24902508
.range_end = (loff_t)-1,
24912509
};
24922510

2493-
24942511
ret = __extent_writepage(page, wbc, &epd);
24952512

24962513
extent_write_cache_pages(tree, mapping, &wbc_writepages,
24972514
__extent_writepage, &epd, flush_write_bio);
2498-
if (epd.bio)
2499-
submit_one_bio(WRITE, epd.bio, 0, 0);
2515+
flush_epd_write_bio(&epd);
25002516
return ret;
25012517
}
25022518

@@ -2515,6 +2531,7 @@ int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
25152531
.tree = tree,
25162532
.get_extent = get_extent,
25172533
.extent_locked = 1,
2534+
.sync_io = mode == WB_SYNC_ALL,
25182535
};
25192536
struct writeback_control wbc_writepages = {
25202537
.bdi = inode->i_mapping->backing_dev_info,
@@ -2540,8 +2557,7 @@ int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
25402557
start += PAGE_CACHE_SIZE;
25412558
}
25422559

2543-
if (epd.bio)
2544-
submit_one_bio(WRITE, epd.bio, 0, 0);
2560+
flush_epd_write_bio(&epd);
25452561
return ret;
25462562
}
25472563

@@ -2556,13 +2572,13 @@ int extent_writepages(struct extent_io_tree *tree,
25562572
.tree = tree,
25572573
.get_extent = get_extent,
25582574
.extent_locked = 0,
2575+
.sync_io = wbc->sync_mode == WB_SYNC_ALL,
25592576
};
25602577

25612578
ret = extent_write_cache_pages(tree, mapping, wbc,
25622579
__extent_writepage, &epd,
25632580
flush_write_bio);
2564-
if (epd.bio)
2565-
submit_one_bio(WRITE, epd.bio, 0, 0);
2581+
flush_epd_write_bio(&epd);
25662582
return ret;
25672583
}
25682584

‎fs/btrfs/ordered-data.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
489489
/* start IO across the range first to instantiate any delalloc
490490
* extents
491491
*/
492-
btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_NONE);
492+
btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_ALL);
493493

494494
/* The compression code will leave pages locked but return from
495495
* writepage without setting the page writeback. Starting again

‎fs/btrfs/volumes.c‎

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
125125
return NULL;
126126
}
127127

128+
static void requeue_list(struct btrfs_pending_bios *pending_bios,
129+
struct bio *head, struct bio *tail)
130+
{
131+
132+
struct bio *old_head;
133+
134+
old_head = pending_bios->head;
135+
pending_bios->head = head;
136+
if (pending_bios->tail)
137+
tail->bi_next = old_head;
138+
else
139+
pending_bios->tail = tail;
140+
}
141+
128142
/*
129143
* we try to collect pending bios for a device so we don't get a large
130144
* number of procs sending bios down to the same device. This greatly
@@ -141,10 +155,12 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
141155
struct bio *pending;
142156
struct backing_dev_info *bdi;
143157
struct btrfs_fs_info *fs_info;
158+
struct btrfs_pending_bios *pending_bios;
144159
struct bio *tail;
145160
struct bio *cur;
146161
int again = 0;
147-
unsigned long num_run = 0;
162+
unsigned long num_run;
163+
unsigned long num_sync_run;
148164
unsigned long limit;
149165
unsigned long last_waited = 0;
150166

@@ -153,20 +169,30 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
153169
limit = btrfs_async_submit_limit(fs_info);
154170
limit = limit * 2 / 3;
155171

172+
/* we want to make sure that every time we switch from the sync
173+
* list to the normal list, we unplug
174+
*/
175+
num_sync_run = 0;
176+
156177
loop:
157178
spin_lock(&device->io_lock);
179+
num_run = 0;
158180

159181
loop_lock:
182+
160183
/* take all the bios off the list at once and process them
161184
* later on (without the lock held). But, remember the
162185
* tail and other pointers so the bios can be properly reinserted
163186
* into the list if we hit congestion
164187
*/
165-
pending = device->pending_bios;
166-
tail = device->pending_bio_tail;
188+
if (device->pending_sync_bios.head)
189+
pending_bios = &device->pending_sync_bios;
190+
else
191+
pending_bios = &device->pending_bios;
192+
193+
pending = pending_bios->head;
194+
tail = pending_bios->tail;
167195
WARN_ON(pending && !tail);
168-
device->pending_bios = NULL;
169-
device->pending_bio_tail = NULL;
170196

171197
/*
172198
* if pending was null this time around, no bios need processing
@@ -176,16 +202,41 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
176202
* device->running_pending is used to synchronize with the
177203
* schedule_bio code.
178204
*/
179-
if (pending) {
180-
again = 1;
181-
device->running_pending = 1;
182-
} else {
205+
if (device->pending_sync_bios.head == NULL &&
206+
device->pending_bios.head == NULL) {
183207
again = 0;
184208
device->running_pending = 0;
209+
} else {
210+
again = 1;
211+
device->running_pending = 1;
185212
}
213+
214+
pending_bios->head = NULL;
215+
pending_bios->tail = NULL;
216+
186217
spin_unlock(&device->io_lock);
187218

219+
/*
220+
* if we're doing the regular priority list, make sure we unplug
221+
* for any high prio bios we've sent down
222+
*/
223+
if (pending_bios == &device->pending_bios && num_sync_run > 0) {
224+
num_sync_run = 0;
225+
blk_run_backing_dev(bdi, NULL);
226+
}
227+
188228
while (pending) {
229+
230+
rmb();
231+
if (pending_bios != &device->pending_sync_bios &&
232+
device->pending_sync_bios.head &&
233+
num_run > 16) {
234+
cond_resched();
235+
spin_lock(&device->io_lock);
236+
requeue_list(pending_bios, pending, tail);
237+
goto loop_lock;
238+
}
239+
189240
cur = pending;
190241
pending = pending->bi_next;
191242
cur->bi_next = NULL;
@@ -196,10 +247,18 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
196247
wake_up(&fs_info->async_submit_wait);
197248

198249
BUG_ON(atomic_read(&cur->bi_cnt) == 0);
199-
bio_get(cur);
200250
submit_bio(cur->bi_rw, cur);
201-
bio_put(cur);
202251
num_run++;
252+
if (bio_sync(cur))
253+
num_sync_run++;
254+
255+
if (need_resched()) {
256+
if (num_sync_run) {
257+
blk_run_backing_dev(bdi, NULL);
258+
num_sync_run = 0;
259+
}
260+
cond_resched();
261+
}
203262

204263
/*
205264
* we made progress, there is more work to do and the bdi
@@ -208,7 +267,6 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
208267
*/
209268
if (pending && bdi_write_congested(bdi) && num_run > 16 &&
210269
fs_info->fs_devices->open_devices > 1) {
211-
struct bio *old_head;
212270
struct io_context *ioc;
213271

214272
ioc = current->io_context;
@@ -233,29 +291,36 @@ static noinline int run_scheduled_bios(struct btrfs_device *device)
233291
* against it before looping
234292
*/
235293
last_waited = ioc->last_waited;
294+
if (need_resched()) {
295+
if (num_sync_run) {
296+
blk_run_backing_dev(bdi, NULL);
297+
num_sync_run = 0;
298+
}
299+
cond_resched();
300+
}
236301
continue;
237302
}
238303
spin_lock(&device->io_lock);
239-
240-
old_head = device->pending_bios;
241-
device->pending_bios = pending;
242-
if (device->pending_bio_tail)
243-
tail->bi_next = old_head;
244-
else
245-
device->pending_bio_tail = tail;
246-
304+
requeue_list(pending_bios, pending, tail);
247305
device->running_pending = 1;
248306

249307
spin_unlock(&device->io_lock);
250308
btrfs_requeue_work(&device->work);
251309
goto done;
252310
}
253311
}
312+
313+
if (num_sync_run) {
314+
num_sync_run = 0;
315+
blk_run_backing_dev(bdi, NULL);
316+
}
317+
318+
cond_resched();
254319
if (again)
255320
goto loop;
256321

257322
spin_lock(&device->io_lock);
258-
if (device->pending_bios)
323+
if (device->pending_bios.head || device->pending_sync_bios.head)
259324
goto loop_lock;
260325
spin_unlock(&device->io_lock);
261326

@@ -2497,7 +2562,7 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
24972562
max_errors = 1;
24982563
}
24992564
}
2500-
if (multi_ret && rw == WRITE &&
2565+
if (multi_ret && (rw & (1 << BIO_RW)) &&
25012566
stripes_allocated < stripes_required) {
25022567
stripes_allocated = map->num_stripes;
25032568
free_extent_map(em);
@@ -2762,6 +2827,7 @@ static noinline int schedule_bio(struct btrfs_root *root,
27622827
int rw, struct bio *bio)
27632828
{
27642829
int should_queue = 1;
2830+
struct btrfs_pending_bios *pending_bios;
27652831

27662832
/* don't bother with additional async steps for reads, right now */
27672833
if (!(rw & (1 << BIO_RW))) {
@@ -2783,13 +2849,17 @@ static noinline int schedule_bio(struct btrfs_root *root,
27832849
bio->bi_rw |= rw;
27842850

27852851
spin_lock(&device->io_lock);
2852+
if (bio_sync(bio))
2853+
pending_bios = &device->pending_sync_bios;
2854+
else
2855+
pending_bios = &device->pending_bios;
27862856

2787-
if (device->pending_bio_tail)
2788-
device->pending_bio_tail->bi_next = bio;
2857+
if (pending_bios->tail)
2858+
pending_bios->tail->bi_next = bio;
27892859

2790-
device->pending_bio_tail = bio;
2791-
if (!device->pending_bios)
2792-
device->pending_bios = bio;
2860+
pending_bios->tail = bio;
2861+
if (!pending_bios->head)
2862+
pending_bios->head = bio;
27932863
if (device->running_pending)
27942864
should_queue = 0;
27952865

0 commit comments

Comments
 (0)