Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * fs/fs-writeback.c | |
4 | * | |
5 | * Copyright (C) 2002, Linus Torvalds. | |
6 | * | |
7 | * Contains all the functions related to writing back and waiting | |
8 | * upon dirty inodes against superblocks, and writing back dirty | |
9 | * pages against inodes. ie: data writeback. Writeout of the | |
10 | * inode itself is not handled here. | |
11 | * | |
e1f8e874 | 12 | * 10Apr2002 Andrew Morton |
1da177e4 LT |
13 | * Split out of fs/inode.c |
14 | * Additions for address_space-based writeback | |
15 | */ | |
16 | ||
17 | #include <linux/kernel.h> | |
630d9c47 | 18 | #include <linux/export.h> |
1da177e4 | 19 | #include <linux/spinlock.h> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
1da177e4 LT |
21 | #include <linux/sched.h> |
22 | #include <linux/fs.h> | |
23 | #include <linux/mm.h> | |
bc31b86a | 24 | #include <linux/pagemap.h> |
03ba3782 | 25 | #include <linux/kthread.h> |
1da177e4 LT |
26 | #include <linux/writeback.h> |
27 | #include <linux/blkdev.h> | |
28 | #include <linux/backing-dev.h> | |
455b2864 | 29 | #include <linux/tracepoint.h> |
719ea2fb | 30 | #include <linux/device.h> |
21c6321f | 31 | #include <linux/memcontrol.h> |
07f3f05c | 32 | #include "internal.h" |
1da177e4 | 33 | |
bc31b86a WF |
34 | /* |
35 | * 4MB minimal write chunk size | |
36 | */ | |
09cbfeaf | 37 | #define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10)) |
bc31b86a | 38 | |
cc395d7f TH |
39 | struct wb_completion { |
40 | atomic_t cnt; | |
41 | }; | |
42 | ||
c4a77a6c JA |
43 | /* |
44 | * Passed into wb_writeback(), essentially a subset of writeback_control | |
45 | */ | |
83ba7b07 | 46 | struct wb_writeback_work { |
c4a77a6c JA |
47 | long nr_pages; |
48 | struct super_block *sb; | |
0dc83bd3 | 49 | unsigned long *older_than_this; |
c4a77a6c | 50 | enum writeback_sync_modes sync_mode; |
6e6938b6 | 51 | unsigned int tagged_writepages:1; |
52957fe1 HS |
52 | unsigned int for_kupdate:1; |
53 | unsigned int range_cyclic:1; | |
54 | unsigned int for_background:1; | |
7747bd4b | 55 | unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ |
ac7b19a3 | 56 | unsigned int auto_free:1; /* free on completion */ |
0e175a18 | 57 | enum wb_reason reason; /* why was writeback initiated? */ |
c4a77a6c | 58 | |
8010c3b6 | 59 | struct list_head list; /* pending work list */ |
cc395d7f | 60 | struct wb_completion *done; /* set if the caller waits */ |
03ba3782 JA |
61 | }; |
62 | ||
cc395d7f TH |
63 | /* |
64 | * If one wants to wait for one or more wb_writeback_works, each work's | |
65 | * ->done should be set to a wb_completion defined using the following | |
66 | * macro. Once all work items are issued with wb_queue_work(), the caller | |
67 | * can wait for the completion of all using wb_wait_for_completion(). Work | |
68 | * items which are waited upon aren't freed automatically on completion. | |
69 | */ | |
70 | #define DEFINE_WB_COMPLETION_ONSTACK(cmpl) \ | |
71 | struct wb_completion cmpl = { \ | |
72 | .cnt = ATOMIC_INIT(1), \ | |
73 | } | |
74 | ||
75 | ||
a2f48706 TT |
76 | /* |
77 | * If an inode is constantly having its pages dirtied, but then the | |
78 | * updates stop dirtytime_expire_interval seconds in the past, it's | |
79 | * possible for the worst case time between when an inode has its | |
80 | * timestamps updated and when they finally get written out to be two | |
81 | * dirtytime_expire_intervals. We set the default to 12 hours (in | |
82 | * seconds), which means most of the time inodes will have their | |
83 | * timestamps written to disk after 12 hours, but in the worst case a | |
84 | * few inodes might not their timestamps updated for 24 hours. | |
85 | */ | |
86 | unsigned int dirtytime_expire_interval = 12 * 60 * 60; | |
87 | ||
7ccf19a8 NP |
88 | static inline struct inode *wb_inode(struct list_head *head) |
89 | { | |
c7f54084 | 90 | return list_entry(head, struct inode, i_io_list); |
7ccf19a8 NP |
91 | } |
92 | ||
15eb77a0 WF |
93 | /* |
94 | * Include the creation of the trace points after defining the | |
95 | * wb_writeback_work structure and inline functions so that the definition | |
96 | * remains local to this file. | |
97 | */ | |
98 | #define CREATE_TRACE_POINTS | |
99 | #include <trace/events/writeback.h> | |
100 | ||
774016b2 SW |
101 | EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); |
102 | ||
d6c10f1f TH |
103 | static bool wb_io_lists_populated(struct bdi_writeback *wb) |
104 | { | |
105 | if (wb_has_dirty_io(wb)) { | |
106 | return false; | |
107 | } else { | |
108 | set_bit(WB_has_dirty_io, &wb->state); | |
95a46c65 | 109 | WARN_ON_ONCE(!wb->avg_write_bandwidth); |
766a9d6e TH |
110 | atomic_long_add(wb->avg_write_bandwidth, |
111 | &wb->bdi->tot_write_bandwidth); | |
d6c10f1f TH |
112 | return true; |
113 | } | |
114 | } | |
115 | ||
116 | static void wb_io_lists_depopulated(struct bdi_writeback *wb) | |
117 | { | |
118 | if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && | |
766a9d6e | 119 | list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { |
d6c10f1f | 120 | clear_bit(WB_has_dirty_io, &wb->state); |
95a46c65 TH |
121 | WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, |
122 | &wb->bdi->tot_write_bandwidth) < 0); | |
766a9d6e | 123 | } |
d6c10f1f TH |
124 | } |
125 | ||
126 | /** | |
c7f54084 | 127 | * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list |
d6c10f1f TH |
128 | * @inode: inode to be moved |
129 | * @wb: target bdi_writeback | |
bbbc3c1c | 130 | * @head: one of @wb->b_{dirty|io|more_io|dirty_time} |
d6c10f1f | 131 | * |
c7f54084 | 132 | * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. |
d6c10f1f TH |
133 | * Returns %true if @inode is the first occupant of the !dirty_time IO |
134 | * lists; otherwise, %false. | |
135 | */ | |
c7f54084 | 136 | static bool inode_io_list_move_locked(struct inode *inode, |
d6c10f1f TH |
137 | struct bdi_writeback *wb, |
138 | struct list_head *head) | |
139 | { | |
140 | assert_spin_locked(&wb->list_lock); | |
141 | ||
c7f54084 | 142 | list_move(&inode->i_io_list, head); |
d6c10f1f TH |
143 | |
144 | /* dirty_time doesn't count as dirty_io until expiration */ | |
145 | if (head != &wb->b_dirty_time) | |
146 | return wb_io_lists_populated(wb); | |
147 | ||
148 | wb_io_lists_depopulated(wb); | |
149 | return false; | |
150 | } | |
151 | ||
152 | /** | |
c7f54084 | 153 | * inode_io_list_del_locked - remove an inode from its bdi_writeback IO list |
d6c10f1f TH |
154 | * @inode: inode to be removed |
155 | * @wb: bdi_writeback @inode is being removed from | |
156 | * | |
157 | * Remove @inode which may be on one of @wb->b_{dirty|io|more_io} lists and | |
158 | * clear %WB_has_dirty_io if all are empty afterwards. | |
159 | */ | |
c7f54084 | 160 | static void inode_io_list_del_locked(struct inode *inode, |
d6c10f1f TH |
161 | struct bdi_writeback *wb) |
162 | { | |
163 | assert_spin_locked(&wb->list_lock); | |
164 | ||
c7f54084 | 165 | list_del_init(&inode->i_io_list); |
d6c10f1f TH |
166 | wb_io_lists_depopulated(wb); |
167 | } | |
168 | ||
f0054bb1 | 169 | static void wb_wakeup(struct bdi_writeback *wb) |
5acda9d1 | 170 | { |
f0054bb1 TH |
171 | spin_lock_bh(&wb->work_lock); |
172 | if (test_bit(WB_registered, &wb->state)) | |
173 | mod_delayed_work(bdi_wq, &wb->dwork, 0); | |
174 | spin_unlock_bh(&wb->work_lock); | |
5acda9d1 JK |
175 | } |
176 | ||
4a3a485b TE |
177 | static void finish_writeback_work(struct bdi_writeback *wb, |
178 | struct wb_writeback_work *work) | |
179 | { | |
180 | struct wb_completion *done = work->done; | |
181 | ||
182 | if (work->auto_free) | |
183 | kfree(work); | |
184 | if (done && atomic_dec_and_test(&done->cnt)) | |
185 | wake_up_all(&wb->bdi->wb_waitq); | |
186 | } | |
187 | ||
f0054bb1 TH |
188 | static void wb_queue_work(struct bdi_writeback *wb, |
189 | struct wb_writeback_work *work) | |
6585027a | 190 | { |
5634cc2a | 191 | trace_writeback_queue(wb, work); |
6585027a | 192 | |
cc395d7f TH |
193 | if (work->done) |
194 | atomic_inc(&work->done->cnt); | |
4a3a485b TE |
195 | |
196 | spin_lock_bh(&wb->work_lock); | |
197 | ||
198 | if (test_bit(WB_registered, &wb->state)) { | |
199 | list_add_tail(&work->list, &wb->work_list); | |
200 | mod_delayed_work(bdi_wq, &wb->dwork, 0); | |
201 | } else | |
202 | finish_writeback_work(wb, work); | |
203 | ||
f0054bb1 | 204 | spin_unlock_bh(&wb->work_lock); |
1da177e4 LT |
205 | } |
206 | ||
cc395d7f TH |
207 | /** |
208 | * wb_wait_for_completion - wait for completion of bdi_writeback_works | |
209 | * @bdi: bdi work items were issued to | |
210 | * @done: target wb_completion | |
211 | * | |
212 | * Wait for one or more work items issued to @bdi with their ->done field | |
213 | * set to @done, which should have been defined with | |
214 | * DEFINE_WB_COMPLETION_ONSTACK(). This function returns after all such | |
215 | * work items are completed. Work items which are waited upon aren't freed | |
216 | * automatically on completion. | |
217 | */ | |
218 | static void wb_wait_for_completion(struct backing_dev_info *bdi, | |
219 | struct wb_completion *done) | |
220 | { | |
221 | atomic_dec(&done->cnt); /* put down the initial count */ | |
222 | wait_event(bdi->wb_waitq, !atomic_read(&done->cnt)); | |
223 | } | |
224 | ||
703c2708 TH |
225 | #ifdef CONFIG_CGROUP_WRITEBACK |
226 | ||
55a694df TH |
227 | /* |
228 | * Parameters for foreign inode detection, see wbc_detach_inode() to see | |
229 | * how they're used. | |
230 | * | |
231 | * These paramters are inherently heuristical as the detection target | |
232 | * itself is fuzzy. All we want to do is detaching an inode from the | |
233 | * current owner if it's being written to by some other cgroups too much. | |
234 | * | |
235 | * The current cgroup writeback is built on the assumption that multiple | |
236 | * cgroups writing to the same inode concurrently is very rare and a mode | |
237 | * of operation which isn't well supported. As such, the goal is not | |
238 | * taking too long when a different cgroup takes over an inode while | |
239 | * avoiding too aggressive flip-flops from occasional foreign writes. | |
240 | * | |
241 | * We record, very roughly, 2s worth of IO time history and if more than | |
242 | * half of that is foreign, trigger the switch. The recording is quantized | |
243 | * to 16 slots. To avoid tiny writes from swinging the decision too much, | |
244 | * writes smaller than 1/8 of avg size are ignored. | |
245 | */ | |
2a814908 TH |
246 | #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */ |
247 | #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ | |
55a694df | 248 | #define WB_FRN_TIME_CUT_DIV 8 /* ignore rounds < avg / 8 */ |
2a814908 TH |
249 | #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ |
250 | ||
251 | #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ | |
252 | #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) | |
253 | /* each slot's duration is 2s / 16 */ | |
254 | #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) | |
255 | /* if foreign slots >= 8, switch */ | |
256 | #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) | |
257 | /* one round can affect upto 5 slots */ | |
258 | ||
a1a0e23e TH |
259 | static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); |
260 | static struct workqueue_struct *isw_wq; | |
261 | ||
21c6321f TH |
262 | void __inode_attach_wb(struct inode *inode, struct page *page) |
263 | { | |
264 | struct backing_dev_info *bdi = inode_to_bdi(inode); | |
265 | struct bdi_writeback *wb = NULL; | |
266 | ||
267 | if (inode_cgwb_enabled(inode)) { | |
268 | struct cgroup_subsys_state *memcg_css; | |
269 | ||
270 | if (page) { | |
271 | memcg_css = mem_cgroup_css_from_page(page); | |
272 | wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); | |
273 | } else { | |
274 | /* must pin memcg_css, see wb_get_create() */ | |
275 | memcg_css = task_get_css(current, memory_cgrp_id); | |
276 | wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); | |
277 | css_put(memcg_css); | |
278 | } | |
279 | } | |
280 | ||
281 | if (!wb) | |
282 | wb = &bdi->wb; | |
283 | ||
284 | /* | |
285 | * There may be multiple instances of this function racing to | |
286 | * update the same inode. Use cmpxchg() to tell the winner. | |
287 | */ | |
288 | if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) | |
289 | wb_put(wb); | |
290 | } | |
9b0eb69b | 291 | EXPORT_SYMBOL_GPL(__inode_attach_wb); |
21c6321f | 292 | |
87e1d789 TH |
293 | /** |
294 | * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it | |
295 | * @inode: inode of interest with i_lock held | |
296 | * | |
297 | * Returns @inode's wb with its list_lock held. @inode->i_lock must be | |
298 | * held on entry and is released on return. The returned wb is guaranteed | |
299 | * to stay @inode's associated wb until its list_lock is released. | |
300 | */ | |
301 | static struct bdi_writeback * | |
302 | locked_inode_to_wb_and_lock_list(struct inode *inode) | |
303 | __releases(&inode->i_lock) | |
304 | __acquires(&wb->list_lock) | |
305 | { | |
306 | while (true) { | |
307 | struct bdi_writeback *wb = inode_to_wb(inode); | |
308 | ||
309 | /* | |
310 | * inode_to_wb() association is protected by both | |
311 | * @inode->i_lock and @wb->list_lock but list_lock nests | |
312 | * outside i_lock. Drop i_lock and verify that the | |
313 | * association hasn't changed after acquiring list_lock. | |
314 | */ | |
315 | wb_get(wb); | |
316 | spin_unlock(&inode->i_lock); | |
317 | spin_lock(&wb->list_lock); | |
87e1d789 | 318 | |
aaa2cacf | 319 | /* i_wb may have changed inbetween, can't use inode_to_wb() */ |
614a4e37 TH |
320 | if (likely(wb == inode->i_wb)) { |
321 | wb_put(wb); /* @inode already has ref */ | |
322 | return wb; | |
323 | } | |
87e1d789 TH |
324 | |
325 | spin_unlock(&wb->list_lock); | |
614a4e37 | 326 | wb_put(wb); |
87e1d789 TH |
327 | cpu_relax(); |
328 | spin_lock(&inode->i_lock); | |
329 | } | |
330 | } | |
331 | ||
332 | /** | |
333 | * inode_to_wb_and_lock_list - determine an inode's wb and lock it | |
334 | * @inode: inode of interest | |
335 | * | |
336 | * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held | |
337 | * on entry. | |
338 | */ | |
339 | static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) | |
340 | __acquires(&wb->list_lock) | |
341 | { | |
342 | spin_lock(&inode->i_lock); | |
343 | return locked_inode_to_wb_and_lock_list(inode); | |
344 | } | |
345 | ||
682aa8e1 TH |
346 | struct inode_switch_wbs_context { |
347 | struct inode *inode; | |
348 | struct bdi_writeback *new_wb; | |
349 | ||
350 | struct rcu_head rcu_head; | |
351 | struct work_struct work; | |
352 | }; | |
353 | ||
7fc5854f TH |
354 | static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) |
355 | { | |
356 | down_write(&bdi->wb_switch_rwsem); | |
357 | } | |
358 | ||
359 | static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) | |
360 | { | |
361 | up_write(&bdi->wb_switch_rwsem); | |
362 | } | |
363 | ||
682aa8e1 TH |
364 | static void inode_switch_wbs_work_fn(struct work_struct *work) |
365 | { | |
366 | struct inode_switch_wbs_context *isw = | |
367 | container_of(work, struct inode_switch_wbs_context, work); | |
368 | struct inode *inode = isw->inode; | |
7fc5854f | 369 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
d10c8095 TH |
370 | struct address_space *mapping = inode->i_mapping; |
371 | struct bdi_writeback *old_wb = inode->i_wb; | |
682aa8e1 | 372 | struct bdi_writeback *new_wb = isw->new_wb; |
04edf02c MW |
373 | XA_STATE(xas, &mapping->i_pages, 0); |
374 | struct page *page; | |
d10c8095 | 375 | bool switched = false; |
682aa8e1 | 376 | |
7fc5854f TH |
377 | /* |
378 | * If @inode switches cgwb membership while sync_inodes_sb() is | |
379 | * being issued, sync_inodes_sb() might miss it. Synchronize. | |
380 | */ | |
381 | down_read(&bdi->wb_switch_rwsem); | |
382 | ||
682aa8e1 TH |
383 | /* |
384 | * By the time control reaches here, RCU grace period has passed | |
385 | * since I_WB_SWITCH assertion and all wb stat update transactions | |
386 | * between unlocked_inode_to_wb_begin/end() are guaranteed to be | |
b93b0163 | 387 | * synchronizing against the i_pages lock. |
d10c8095 | 388 | * |
b93b0163 | 389 | * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock |
d10c8095 TH |
390 | * gives us exclusion against all wb related operations on @inode |
391 | * including IO list manipulations and stat updates. | |
682aa8e1 | 392 | */ |
d10c8095 TH |
393 | if (old_wb < new_wb) { |
394 | spin_lock(&old_wb->list_lock); | |
395 | spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); | |
396 | } else { | |
397 | spin_lock(&new_wb->list_lock); | |
398 | spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); | |
399 | } | |
682aa8e1 | 400 | spin_lock(&inode->i_lock); |
b93b0163 | 401 | xa_lock_irq(&mapping->i_pages); |
d10c8095 TH |
402 | |
403 | /* | |
404 | * Once I_FREEING is visible under i_lock, the eviction path owns | |
c7f54084 | 405 | * the inode and we shouldn't modify ->i_io_list. |
d10c8095 TH |
406 | */ |
407 | if (unlikely(inode->i_state & I_FREEING)) | |
408 | goto skip_switch; | |
409 | ||
410 | /* | |
411 | * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points | |
412 | * to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to | |
b93b0163 | 413 | * pages actually under writeback. |
d10c8095 | 414 | */ |
04edf02c MW |
415 | xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_DIRTY) { |
416 | if (PageDirty(page)) { | |
3e8f399d NB |
417 | dec_wb_stat(old_wb, WB_RECLAIMABLE); |
418 | inc_wb_stat(new_wb, WB_RECLAIMABLE); | |
d10c8095 TH |
419 | } |
420 | } | |
421 | ||
04edf02c MW |
422 | xas_set(&xas, 0); |
423 | xas_for_each_marked(&xas, page, ULONG_MAX, PAGECACHE_TAG_WRITEBACK) { | |
424 | WARN_ON_ONCE(!PageWriteback(page)); | |
425 | dec_wb_stat(old_wb, WB_WRITEBACK); | |
426 | inc_wb_stat(new_wb, WB_WRITEBACK); | |
d10c8095 TH |
427 | } |
428 | ||
429 | wb_get(new_wb); | |
430 | ||
431 | /* | |
432 | * Transfer to @new_wb's IO list if necessary. The specific list | |
433 | * @inode was on is ignored and the inode is put on ->b_dirty which | |
434 | * is always correct including from ->b_dirty_time. The transfer | |
435 | * preserves @inode->dirtied_when ordering. | |
436 | */ | |
c7f54084 | 437 | if (!list_empty(&inode->i_io_list)) { |
d10c8095 TH |
438 | struct inode *pos; |
439 | ||
c7f54084 | 440 | inode_io_list_del_locked(inode, old_wb); |
d10c8095 | 441 | inode->i_wb = new_wb; |
c7f54084 | 442 | list_for_each_entry(pos, &new_wb->b_dirty, i_io_list) |
d10c8095 TH |
443 | if (time_after_eq(inode->dirtied_when, |
444 | pos->dirtied_when)) | |
445 | break; | |
c7f54084 | 446 | inode_io_list_move_locked(inode, new_wb, pos->i_io_list.prev); |
d10c8095 TH |
447 | } else { |
448 | inode->i_wb = new_wb; | |
449 | } | |
682aa8e1 | 450 | |
d10c8095 | 451 | /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ |
682aa8e1 TH |
452 | inode->i_wb_frn_winner = 0; |
453 | inode->i_wb_frn_avg_time = 0; | |
454 | inode->i_wb_frn_history = 0; | |
d10c8095 TH |
455 | switched = true; |
456 | skip_switch: | |
682aa8e1 TH |
457 | /* |
458 | * Paired with load_acquire in unlocked_inode_to_wb_begin() and | |
459 | * ensures that the new wb is visible if they see !I_WB_SWITCH. | |
460 | */ | |
461 | smp_store_release(&inode->i_state, inode->i_state & ~I_WB_SWITCH); | |
462 | ||
b93b0163 | 463 | xa_unlock_irq(&mapping->i_pages); |
682aa8e1 | 464 | spin_unlock(&inode->i_lock); |
d10c8095 TH |
465 | spin_unlock(&new_wb->list_lock); |
466 | spin_unlock(&old_wb->list_lock); | |
682aa8e1 | 467 | |
7fc5854f TH |
468 | up_read(&bdi->wb_switch_rwsem); |
469 | ||
d10c8095 TH |
470 | if (switched) { |
471 | wb_wakeup(new_wb); | |
472 | wb_put(old_wb); | |
473 | } | |
682aa8e1 | 474 | wb_put(new_wb); |
d10c8095 TH |
475 | |
476 | iput(inode); | |
682aa8e1 | 477 | kfree(isw); |
a1a0e23e TH |
478 | |
479 | atomic_dec(&isw_nr_in_flight); | |
682aa8e1 TH |
480 | } |
481 | ||
482 | static void inode_switch_wbs_rcu_fn(struct rcu_head *rcu_head) | |
483 | { | |
484 | struct inode_switch_wbs_context *isw = container_of(rcu_head, | |
485 | struct inode_switch_wbs_context, rcu_head); | |
486 | ||
487 | /* needs to grab bh-unsafe locks, bounce to work item */ | |
488 | INIT_WORK(&isw->work, inode_switch_wbs_work_fn); | |
a1a0e23e | 489 | queue_work(isw_wq, &isw->work); |
682aa8e1 TH |
490 | } |
491 | ||
492 | /** | |
493 | * inode_switch_wbs - change the wb association of an inode | |
494 | * @inode: target inode | |
495 | * @new_wb_id: ID of the new wb | |
496 | * | |
497 | * Switch @inode's wb association to the wb identified by @new_wb_id. The | |
498 | * switching is performed asynchronously and may fail silently. | |
499 | */ | |
500 | static void inode_switch_wbs(struct inode *inode, int new_wb_id) | |
501 | { | |
502 | struct backing_dev_info *bdi = inode_to_bdi(inode); | |
503 | struct cgroup_subsys_state *memcg_css; | |
504 | struct inode_switch_wbs_context *isw; | |
505 | ||
506 | /* noop if seems to be already in progress */ | |
507 | if (inode->i_state & I_WB_SWITCH) | |
508 | return; | |
509 | ||
7fc5854f TH |
510 | /* |
511 | * Avoid starting new switches while sync_inodes_sb() is in | |
512 | * progress. Otherwise, if the down_write protected issue path | |
513 | * blocks heavily, we might end up starting a large number of | |
514 | * switches which will block on the rwsem. | |
515 | */ | |
516 | if (!down_read_trylock(&bdi->wb_switch_rwsem)) | |
517 | return; | |
518 | ||
682aa8e1 TH |
519 | isw = kzalloc(sizeof(*isw), GFP_ATOMIC); |
520 | if (!isw) | |
7fc5854f | 521 | goto out_unlock; |
682aa8e1 TH |
522 | |
523 | /* find and pin the new wb */ | |
524 | rcu_read_lock(); | |
525 | memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); | |
526 | if (memcg_css) | |
527 | isw->new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); | |
528 | rcu_read_unlock(); | |
529 | if (!isw->new_wb) | |
530 | goto out_free; | |
531 | ||
532 | /* while holding I_WB_SWITCH, no one else can update the association */ | |
533 | spin_lock(&inode->i_lock); | |
1751e8a6 | 534 | if (!(inode->i_sb->s_flags & SB_ACTIVE) || |
a1a0e23e TH |
535 | inode->i_state & (I_WB_SWITCH | I_FREEING) || |
536 | inode_to_wb(inode) == isw->new_wb) { | |
537 | spin_unlock(&inode->i_lock); | |
538 | goto out_free; | |
539 | } | |
682aa8e1 | 540 | inode->i_state |= I_WB_SWITCH; |
74524955 | 541 | __iget(inode); |
682aa8e1 TH |
542 | spin_unlock(&inode->i_lock); |
543 | ||
682aa8e1 TH |
544 | isw->inode = inode; |
545 | ||
546 | /* | |
547 | * In addition to synchronizing among switchers, I_WB_SWITCH tells | |
b93b0163 MW |
548 | * the RCU protected stat update paths to grab the i_page |
549 | * lock so that stat transfer can synchronize against them. | |
682aa8e1 TH |
550 | * Let's continue after I_WB_SWITCH is guaranteed to be visible. |
551 | */ | |
552 | call_rcu(&isw->rcu_head, inode_switch_wbs_rcu_fn); | |
ec084de9 JX |
553 | |
554 | atomic_inc(&isw_nr_in_flight); | |
555 | ||
7fc5854f | 556 | goto out_unlock; |
682aa8e1 TH |
557 | |
558 | out_free: | |
559 | if (isw->new_wb) | |
560 | wb_put(isw->new_wb); | |
561 | kfree(isw); | |
7fc5854f TH |
562 | out_unlock: |
563 | up_read(&bdi->wb_switch_rwsem); | |
682aa8e1 TH |
564 | } |
565 | ||
b16b1deb TH |
566 | /** |
567 | * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it | |
568 | * @wbc: writeback_control of interest | |
569 | * @inode: target inode | |
570 | * | |
571 | * @inode is locked and about to be written back under the control of @wbc. | |
572 | * Record @inode's writeback context into @wbc and unlock the i_lock. On | |
573 | * writeback completion, wbc_detach_inode() should be called. This is used | |
574 | * to track the cgroup writeback context. | |
575 | */ | |
576 | void wbc_attach_and_unlock_inode(struct writeback_control *wbc, | |
577 | struct inode *inode) | |
578 | { | |
dd73e4b7 TH |
579 | if (!inode_cgwb_enabled(inode)) { |
580 | spin_unlock(&inode->i_lock); | |
581 | return; | |
582 | } | |
583 | ||
b16b1deb | 584 | wbc->wb = inode_to_wb(inode); |
2a814908 TH |
585 | wbc->inode = inode; |
586 | ||
587 | wbc->wb_id = wbc->wb->memcg_css->id; | |
588 | wbc->wb_lcand_id = inode->i_wb_frn_winner; | |
589 | wbc->wb_tcand_id = 0; | |
590 | wbc->wb_bytes = 0; | |
591 | wbc->wb_lcand_bytes = 0; | |
592 | wbc->wb_tcand_bytes = 0; | |
593 | ||
b16b1deb TH |
594 | wb_get(wbc->wb); |
595 | spin_unlock(&inode->i_lock); | |
e8a7abf5 TH |
596 | |
597 | /* | |
598 | * A dying wb indicates that the memcg-blkcg mapping has changed | |
599 | * and a new wb is already serving the memcg. Switch immediately. | |
600 | */ | |
601 | if (unlikely(wb_dying(wbc->wb))) | |
602 | inode_switch_wbs(inode, wbc->wb_id); | |
b16b1deb | 603 | } |
9b0eb69b | 604 | EXPORT_SYMBOL_GPL(wbc_attach_and_unlock_inode); |
b16b1deb TH |
605 | |
606 | /** | |
2a814908 TH |
607 | * wbc_detach_inode - disassociate wbc from inode and perform foreign detection |
608 | * @wbc: writeback_control of the just finished writeback | |
b16b1deb TH |
609 | * |
610 | * To be called after a writeback attempt of an inode finishes and undoes | |
611 | * wbc_attach_and_unlock_inode(). Can be called under any context. | |
2a814908 TH |
612 | * |
613 | * As concurrent write sharing of an inode is expected to be very rare and | |
614 | * memcg only tracks page ownership on first-use basis severely confining | |
615 | * the usefulness of such sharing, cgroup writeback tracks ownership | |
616 | * per-inode. While the support for concurrent write sharing of an inode | |
617 | * is deemed unnecessary, an inode being written to by different cgroups at | |
618 | * different points in time is a lot more common, and, more importantly, | |
619 | * charging only by first-use can too readily lead to grossly incorrect | |
620 | * behaviors (single foreign page can lead to gigabytes of writeback to be | |
621 | * incorrectly attributed). | |
622 | * | |
623 | * To resolve this issue, cgroup writeback detects the majority dirtier of | |
624 | * an inode and transfers the ownership to it. To avoid unnnecessary | |
625 | * oscillation, the detection mechanism keeps track of history and gives | |
626 | * out the switch verdict only if the foreign usage pattern is stable over | |
627 | * a certain amount of time and/or writeback attempts. | |
628 | * | |
629 | * On each writeback attempt, @wbc tries to detect the majority writer | |
630 | * using Boyer-Moore majority vote algorithm. In addition to the byte | |
631 | * count from the majority voting, it also counts the bytes written for the | |
632 | * current wb and the last round's winner wb (max of last round's current | |
633 | * wb, the winner from two rounds ago, and the last round's majority | |
634 | * candidate). Keeping track of the historical winner helps the algorithm | |
635 | * to semi-reliably detect the most active writer even when it's not the | |
636 | * absolute majority. | |
637 | * | |
638 | * Once the winner of the round is determined, whether the winner is | |
639 | * foreign or not and how much IO time the round consumed is recorded in | |
640 | * inode->i_wb_frn_history. If the amount of recorded foreign IO time is | |
641 | * over a certain threshold, the switch verdict is given. | |
b16b1deb TH |
642 | */ |
643 | void wbc_detach_inode(struct writeback_control *wbc) | |
644 | { | |
2a814908 TH |
645 | struct bdi_writeback *wb = wbc->wb; |
646 | struct inode *inode = wbc->inode; | |
dd73e4b7 TH |
647 | unsigned long avg_time, max_bytes, max_time; |
648 | u16 history; | |
2a814908 TH |
649 | int max_id; |
650 | ||
dd73e4b7 TH |
651 | if (!wb) |
652 | return; | |
653 | ||
654 | history = inode->i_wb_frn_history; | |
655 | avg_time = inode->i_wb_frn_avg_time; | |
656 | ||
2a814908 TH |
657 | /* pick the winner of this round */ |
658 | if (wbc->wb_bytes >= wbc->wb_lcand_bytes && | |
659 | wbc->wb_bytes >= wbc->wb_tcand_bytes) { | |
660 | max_id = wbc->wb_id; | |
661 | max_bytes = wbc->wb_bytes; | |
662 | } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { | |
663 | max_id = wbc->wb_lcand_id; | |
664 | max_bytes = wbc->wb_lcand_bytes; | |
665 | } else { | |
666 | max_id = wbc->wb_tcand_id; | |
667 | max_bytes = wbc->wb_tcand_bytes; | |
668 | } | |
669 | ||
670 | /* | |
671 | * Calculate the amount of IO time the winner consumed and fold it | |
672 | * into the running average kept per inode. If the consumed IO | |
673 | * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for | |
674 | * deciding whether to switch or not. This is to prevent one-off | |
675 | * small dirtiers from skewing the verdict. | |
676 | */ | |
677 | max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, | |
678 | wb->avg_write_bandwidth); | |
679 | if (avg_time) | |
680 | avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - | |
681 | (avg_time >> WB_FRN_TIME_AVG_SHIFT); | |
682 | else | |
683 | avg_time = max_time; /* immediate catch up on first run */ | |
684 | ||
685 | if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { | |
686 | int slots; | |
687 | ||
688 | /* | |
689 | * The switch verdict is reached if foreign wb's consume | |
690 | * more than a certain proportion of IO time in a | |
691 | * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot | |
692 | * history mask where each bit represents one sixteenth of | |
693 | * the period. Determine the number of slots to shift into | |
694 | * history from @max_time. | |
695 | */ | |
696 | slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), | |
697 | (unsigned long)WB_FRN_HIST_MAX_SLOTS); | |
698 | history <<= slots; | |
699 | if (wbc->wb_id != max_id) | |
700 | history |= (1U << slots) - 1; | |
701 | ||
702 | /* | |
703 | * Switch if the current wb isn't the consistent winner. | |
704 | * If there are multiple closely competing dirtiers, the | |
705 | * inode may switch across them repeatedly over time, which | |
706 | * is okay. The main goal is avoiding keeping an inode on | |
707 | * the wrong wb for an extended period of time. | |
708 | */ | |
682aa8e1 TH |
709 | if (hweight32(history) > WB_FRN_HIST_THR_SLOTS) |
710 | inode_switch_wbs(inode, max_id); | |
2a814908 TH |
711 | } |
712 | ||
713 | /* | |
714 | * Multiple instances of this function may race to update the | |
715 | * following fields but we don't mind occassional inaccuracies. | |
716 | */ | |
717 | inode->i_wb_frn_winner = max_id; | |
718 | inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); | |
719 | inode->i_wb_frn_history = history; | |
720 | ||
b16b1deb TH |
721 | wb_put(wbc->wb); |
722 | wbc->wb = NULL; | |
723 | } | |
9b0eb69b | 724 | EXPORT_SYMBOL_GPL(wbc_detach_inode); |
b16b1deb | 725 | |
2a814908 | 726 | /** |
34e51a5e | 727 | * wbc_account_cgroup_owner - account writeback to update inode cgroup ownership |
2a814908 TH |
728 | * @wbc: writeback_control of the writeback in progress |
729 | * @page: page being written out | |
730 | * @bytes: number of bytes being written out | |
731 | * | |
732 | * @bytes from @page are about to written out during the writeback | |
733 | * controlled by @wbc. Keep the book for foreign inode detection. See | |
734 | * wbc_detach_inode(). | |
735 | */ | |
34e51a5e TH |
736 | void wbc_account_cgroup_owner(struct writeback_control *wbc, struct page *page, |
737 | size_t bytes) | |
2a814908 | 738 | { |
66311422 | 739 | struct cgroup_subsys_state *css; |
2a814908 TH |
740 | int id; |
741 | ||
742 | /* | |
743 | * pageout() path doesn't attach @wbc to the inode being written | |
744 | * out. This is intentional as we don't want the function to block | |
745 | * behind a slow cgroup. Ultimately, we want pageout() to kick off | |
746 | * regular writeback instead of writing things out itself. | |
747 | */ | |
27b36d8f | 748 | if (!wbc->wb || wbc->no_cgroup_owner) |
2a814908 TH |
749 | return; |
750 | ||
66311422 TH |
751 | css = mem_cgroup_css_from_page(page); |
752 | /* dead cgroups shouldn't contribute to inode ownership arbitration */ | |
753 | if (!(css->flags & CSS_ONLINE)) | |
754 | return; | |
755 | ||
756 | id = css->id; | |
2a814908 TH |
757 | |
758 | if (id == wbc->wb_id) { | |
759 | wbc->wb_bytes += bytes; | |
760 | return; | |
761 | } | |
762 | ||
763 | if (id == wbc->wb_lcand_id) | |
764 | wbc->wb_lcand_bytes += bytes; | |
765 | ||
766 | /* Boyer-Moore majority vote algorithm */ | |
767 | if (!wbc->wb_tcand_bytes) | |
768 | wbc->wb_tcand_id = id; | |
769 | if (id == wbc->wb_tcand_id) | |
770 | wbc->wb_tcand_bytes += bytes; | |
771 | else | |
772 | wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); | |
773 | } | |
34e51a5e | 774 | EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner); |
2a814908 | 775 | |
703c2708 TH |
776 | /** |
777 | * inode_congested - test whether an inode is congested | |
60292bcc | 778 | * @inode: inode to test for congestion (may be NULL) |
703c2708 TH |
779 | * @cong_bits: mask of WB_[a]sync_congested bits to test |
780 | * | |
781 | * Tests whether @inode is congested. @cong_bits is the mask of congestion | |
782 | * bits to test and the return value is the mask of set bits. | |
783 | * | |
784 | * If cgroup writeback is enabled for @inode, the congestion state is | |
785 | * determined by whether the cgwb (cgroup bdi_writeback) for the blkcg | |
786 | * associated with @inode is congested; otherwise, the root wb's congestion | |
787 | * state is used. | |
60292bcc TH |
788 | * |
789 | * @inode is allowed to be NULL as this function is often called on | |
790 | * mapping->host which is NULL for the swapper space. | |
703c2708 TH |
791 | */ |
792 | int inode_congested(struct inode *inode, int cong_bits) | |
793 | { | |
5cb8b824 TH |
794 | /* |
795 | * Once set, ->i_wb never becomes NULL while the inode is alive. | |
796 | * Start transaction iff ->i_wb is visible. | |
797 | */ | |
aaa2cacf | 798 | if (inode && inode_to_wb_is_valid(inode)) { |
5cb8b824 | 799 | struct bdi_writeback *wb; |
2e898e4c GT |
800 | struct wb_lock_cookie lock_cookie = {}; |
801 | bool congested; | |
5cb8b824 | 802 | |
2e898e4c | 803 | wb = unlocked_inode_to_wb_begin(inode, &lock_cookie); |
5cb8b824 | 804 | congested = wb_congested(wb, cong_bits); |
2e898e4c | 805 | unlocked_inode_to_wb_end(inode, &lock_cookie); |
5cb8b824 | 806 | return congested; |
703c2708 TH |
807 | } |
808 | ||
809 | return wb_congested(&inode_to_bdi(inode)->wb, cong_bits); | |
810 | } | |
811 | EXPORT_SYMBOL_GPL(inode_congested); | |
812 | ||
f2b65121 TH |
813 | /** |
814 | * wb_split_bdi_pages - split nr_pages to write according to bandwidth | |
815 | * @wb: target bdi_writeback to split @nr_pages to | |
816 | * @nr_pages: number of pages to write for the whole bdi | |
817 | * | |
818 | * Split @wb's portion of @nr_pages according to @wb's write bandwidth in | |
819 | * relation to the total write bandwidth of all wb's w/ dirty inodes on | |
820 | * @wb->bdi. | |
821 | */ | |
822 | static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) | |
823 | { | |
824 | unsigned long this_bw = wb->avg_write_bandwidth; | |
825 | unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); | |
826 | ||
827 | if (nr_pages == LONG_MAX) | |
828 | return LONG_MAX; | |
829 | ||
830 | /* | |
831 | * This may be called on clean wb's and proportional distribution | |
832 | * may not make sense, just use the original @nr_pages in those | |
833 | * cases. In general, we wanna err on the side of writing more. | |
834 | */ | |
835 | if (!tot_bw || this_bw >= tot_bw) | |
836 | return nr_pages; | |
837 | else | |
838 | return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); | |
839 | } | |
840 | ||
db125360 TH |
841 | /** |
842 | * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi | |
843 | * @bdi: target backing_dev_info | |
844 | * @base_work: wb_writeback_work to issue | |
845 | * @skip_if_busy: skip wb's which already have writeback in progress | |
846 | * | |
847 | * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which | |
848 | * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's | |
849 | * distributed to the busy wbs according to each wb's proportion in the | |
850 | * total active write bandwidth of @bdi. | |
851 | */ | |
852 | static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, | |
853 | struct wb_writeback_work *base_work, | |
854 | bool skip_if_busy) | |
855 | { | |
b817525a | 856 | struct bdi_writeback *last_wb = NULL; |
b33e18f6 TH |
857 | struct bdi_writeback *wb = list_entry(&bdi->wb_list, |
858 | struct bdi_writeback, bdi_node); | |
db125360 TH |
859 | |
860 | might_sleep(); | |
db125360 TH |
861 | restart: |
862 | rcu_read_lock(); | |
b817525a | 863 | list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { |
8a1270cd TH |
864 | DEFINE_WB_COMPLETION_ONSTACK(fallback_work_done); |
865 | struct wb_writeback_work fallback_work; | |
866 | struct wb_writeback_work *work; | |
867 | long nr_pages; | |
868 | ||
b817525a TH |
869 | if (last_wb) { |
870 | wb_put(last_wb); | |
871 | last_wb = NULL; | |
872 | } | |
873 | ||
006a0973 TH |
874 | /* SYNC_ALL writes out I_DIRTY_TIME too */ |
875 | if (!wb_has_dirty_io(wb) && | |
876 | (base_work->sync_mode == WB_SYNC_NONE || | |
877 | list_empty(&wb->b_dirty_time))) | |
878 | continue; | |
879 | if (skip_if_busy && writeback_in_progress(wb)) | |
db125360 TH |
880 | continue; |
881 | ||
8a1270cd TH |
882 | nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); |
883 | ||
884 | work = kmalloc(sizeof(*work), GFP_ATOMIC); | |
885 | if (work) { | |
886 | *work = *base_work; | |
887 | work->nr_pages = nr_pages; | |
888 | work->auto_free = 1; | |
889 | wb_queue_work(wb, work); | |
890 | continue; | |
db125360 | 891 | } |
8a1270cd TH |
892 | |
893 | /* alloc failed, execute synchronously using on-stack fallback */ | |
894 | work = &fallback_work; | |
895 | *work = *base_work; | |
896 | work->nr_pages = nr_pages; | |
897 | work->auto_free = 0; | |
898 | work->done = &fallback_work_done; | |
899 | ||
900 | wb_queue_work(wb, work); | |
901 | ||
b817525a TH |
902 | /* |
903 | * Pin @wb so that it stays on @bdi->wb_list. This allows | |
904 | * continuing iteration from @wb after dropping and | |
905 | * regrabbing rcu read lock. | |
906 | */ | |
907 | wb_get(wb); | |
908 | last_wb = wb; | |
909 | ||
8a1270cd TH |
910 | rcu_read_unlock(); |
911 | wb_wait_for_completion(bdi, &fallback_work_done); | |
912 | goto restart; | |
db125360 TH |
913 | } |
914 | rcu_read_unlock(); | |
b817525a TH |
915 | |
916 | if (last_wb) | |
917 | wb_put(last_wb); | |
db125360 TH |
918 | } |
919 | ||
a1a0e23e TH |
920 | /** |
921 | * cgroup_writeback_umount - flush inode wb switches for umount | |
922 | * | |
923 | * This function is called when a super_block is about to be destroyed and | |
924 | * flushes in-flight inode wb switches. An inode wb switch goes through | |
925 | * RCU and then workqueue, so the two need to be flushed in order to ensure | |
926 | * that all previously scheduled switches are finished. As wb switches are | |
927 | * rare occurrences and synchronize_rcu() can take a while, perform | |
928 | * flushing iff wb switches are in flight. | |
929 | */ | |
930 | void cgroup_writeback_umount(void) | |
931 | { | |
932 | if (atomic_read(&isw_nr_in_flight)) { | |
ec084de9 JX |
933 | /* |
934 | * Use rcu_barrier() to wait for all pending callbacks to | |
935 | * ensure that all in-flight wb switches are in the workqueue. | |
936 | */ | |
937 | rcu_barrier(); | |
a1a0e23e TH |
938 | flush_workqueue(isw_wq); |
939 | } | |
940 | } | |
941 | ||
942 | static int __init cgroup_writeback_init(void) | |
943 | { | |
944 | isw_wq = alloc_workqueue("inode_switch_wbs", 0, 0); | |
945 | if (!isw_wq) | |
946 | return -ENOMEM; | |
947 | return 0; | |
948 | } | |
949 | fs_initcall(cgroup_writeback_init); | |
950 | ||
f2b65121 TH |
951 | #else /* CONFIG_CGROUP_WRITEBACK */ |
952 | ||
7fc5854f TH |
953 | static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } |
954 | static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } | |
955 | ||
87e1d789 TH |
956 | static struct bdi_writeback * |
957 | locked_inode_to_wb_and_lock_list(struct inode *inode) | |
958 | __releases(&inode->i_lock) | |
959 | __acquires(&wb->list_lock) | |
960 | { | |
961 | struct bdi_writeback *wb = inode_to_wb(inode); | |
962 | ||
963 | spin_unlock(&inode->i_lock); | |
964 | spin_lock(&wb->list_lock); | |
965 | return wb; | |
966 | } | |
967 | ||
968 | static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) | |
969 | __acquires(&wb->list_lock) | |
970 | { | |
971 | struct bdi_writeback *wb = inode_to_wb(inode); | |
972 | ||
973 | spin_lock(&wb->list_lock); | |
974 | return wb; | |
975 | } | |
976 | ||
f2b65121 TH |
977 | static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) |
978 | { | |
979 | return nr_pages; | |
980 | } | |
981 | ||
db125360 TH |
982 | static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, |
983 | struct wb_writeback_work *base_work, | |
984 | bool skip_if_busy) | |
985 | { | |
986 | might_sleep(); | |
987 | ||
006a0973 | 988 | if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { |
db125360 | 989 | base_work->auto_free = 0; |
db125360 TH |
990 | wb_queue_work(&bdi->wb, base_work); |
991 | } | |
992 | } | |
993 | ||
703c2708 TH |
994 | #endif /* CONFIG_CGROUP_WRITEBACK */ |
995 | ||
e8e8a0c6 JA |
996 | /* |
997 | * Add in the number of potentially dirty inodes, because each inode | |
998 | * write can dirty pagecache in the underlying blockdev. | |
999 | */ | |
1000 | static unsigned long get_nr_dirty_pages(void) | |
1001 | { | |
1002 | return global_node_page_state(NR_FILE_DIRTY) + | |
1003 | global_node_page_state(NR_UNSTABLE_NFS) + | |
1004 | get_nr_dirty_inodes(); | |
1005 | } | |
1006 | ||
1007 | static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason) | |
b6e51316 | 1008 | { |
c00ddad3 TH |
1009 | if (!wb_has_dirty_io(wb)) |
1010 | return; | |
1011 | ||
aac8d41c JA |
1012 | /* |
1013 | * All callers of this function want to start writeback of all | |
1014 | * dirty pages. Places like vmscan can call this at a very | |
1015 | * high frequency, causing pointless allocations of tons of | |
1016 | * work items and keeping the flusher threads busy retrieving | |
1017 | * that work. Ensure that we only allow one of them pending and | |
85009b4f | 1018 | * inflight at the time. |
aac8d41c | 1019 | */ |
85009b4f JA |
1020 | if (test_bit(WB_start_all, &wb->state) || |
1021 | test_and_set_bit(WB_start_all, &wb->state)) | |
aac8d41c JA |
1022 | return; |
1023 | ||
85009b4f JA |
1024 | wb->start_all_reason = reason; |
1025 | wb_wakeup(wb); | |
c5444198 | 1026 | } |
d3ddec76 | 1027 | |
c5444198 | 1028 | /** |
9ecf4866 TH |
1029 | * wb_start_background_writeback - start background writeback |
1030 | * @wb: bdi_writback to write from | |
c5444198 CH |
1031 | * |
1032 | * Description: | |
6585027a | 1033 | * This makes sure WB_SYNC_NONE background writeback happens. When |
9ecf4866 | 1034 | * this function returns, it is only guaranteed that for given wb |
6585027a JK |
1035 | * some IO is happening if we are over background dirty threshold. |
1036 | * Caller need not hold sb s_umount semaphore. | |
c5444198 | 1037 | */ |
9ecf4866 | 1038 | void wb_start_background_writeback(struct bdi_writeback *wb) |
c5444198 | 1039 | { |
6585027a JK |
1040 | /* |
1041 | * We just wake up the flusher thread. It will perform background | |
1042 | * writeback as soon as there is no other work to do. | |
1043 | */ | |
5634cc2a | 1044 | trace_writeback_wake_background(wb); |
9ecf4866 | 1045 | wb_wakeup(wb); |
1da177e4 LT |
1046 | } |
1047 | ||
a66979ab DC |
1048 | /* |
1049 | * Remove the inode from the writeback list it is on. | |
1050 | */ | |
c7f54084 | 1051 | void inode_io_list_del(struct inode *inode) |
a66979ab | 1052 | { |
87e1d789 | 1053 | struct bdi_writeback *wb; |
f758eeab | 1054 | |
87e1d789 | 1055 | wb = inode_to_wb_and_lock_list(inode); |
c7f54084 | 1056 | inode_io_list_del_locked(inode, wb); |
52ebea74 | 1057 | spin_unlock(&wb->list_lock); |
a66979ab DC |
1058 | } |
1059 | ||
6c60d2b5 DC |
1060 | /* |
1061 | * mark an inode as under writeback on the sb | |
1062 | */ | |
1063 | void sb_mark_inode_writeback(struct inode *inode) | |
1064 | { | |
1065 | struct super_block *sb = inode->i_sb; | |
1066 | unsigned long flags; | |
1067 | ||
1068 | if (list_empty(&inode->i_wb_list)) { | |
1069 | spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); | |
9a46b04f | 1070 | if (list_empty(&inode->i_wb_list)) { |
6c60d2b5 | 1071 | list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb); |
9a46b04f BF |
1072 | trace_sb_mark_inode_writeback(inode); |
1073 | } | |
6c60d2b5 DC |
1074 | spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); |
1075 | } | |
1076 | } | |
1077 | ||
1078 | /* | |
1079 | * clear an inode as under writeback on the sb | |
1080 | */ | |
1081 | void sb_clear_inode_writeback(struct inode *inode) | |
1082 | { | |
1083 | struct super_block *sb = inode->i_sb; | |
1084 | unsigned long flags; | |
1085 | ||
1086 | if (!list_empty(&inode->i_wb_list)) { | |
1087 | spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); | |
9a46b04f BF |
1088 | if (!list_empty(&inode->i_wb_list)) { |
1089 | list_del_init(&inode->i_wb_list); | |
1090 | trace_sb_clear_inode_writeback(inode); | |
1091 | } | |
6c60d2b5 DC |
1092 | spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); |
1093 | } | |
1094 | } | |
1095 | ||
6610a0bc AM |
1096 | /* |
1097 | * Redirty an inode: set its when-it-was dirtied timestamp and move it to the | |
1098 | * furthest end of its superblock's dirty-inode list. | |
1099 | * | |
1100 | * Before stamping the inode's ->dirtied_when, we check to see whether it is | |
66f3b8e2 | 1101 | * already the most-recently-dirtied inode on the b_dirty list. If that is |
6610a0bc AM |
1102 | * the case then the inode must have been redirtied while it was being written |
1103 | * out and we don't reset its dirtied_when. | |
1104 | */ | |
f758eeab | 1105 | static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) |
6610a0bc | 1106 | { |
03ba3782 | 1107 | if (!list_empty(&wb->b_dirty)) { |
66f3b8e2 | 1108 | struct inode *tail; |
6610a0bc | 1109 | |
7ccf19a8 | 1110 | tail = wb_inode(wb->b_dirty.next); |
66f3b8e2 | 1111 | if (time_before(inode->dirtied_when, tail->dirtied_when)) |
6610a0bc AM |
1112 | inode->dirtied_when = jiffies; |
1113 | } | |
c7f54084 | 1114 | inode_io_list_move_locked(inode, wb, &wb->b_dirty); |
6610a0bc AM |
1115 | } |
1116 | ||
c986d1e2 | 1117 | /* |
66f3b8e2 | 1118 | * requeue inode for re-scanning after bdi->b_io list is exhausted. |
c986d1e2 | 1119 | */ |
f758eeab | 1120 | static void requeue_io(struct inode *inode, struct bdi_writeback *wb) |
c986d1e2 | 1121 | { |
c7f54084 | 1122 | inode_io_list_move_locked(inode, wb, &wb->b_more_io); |
c986d1e2 AM |
1123 | } |
1124 | ||
1c0eeaf5 JE |
1125 | static void inode_sync_complete(struct inode *inode) |
1126 | { | |
365b94ae | 1127 | inode->i_state &= ~I_SYNC; |
4eff96dd JK |
1128 | /* If inode is clean an unused, put it into LRU now... */ |
1129 | inode_add_lru(inode); | |
365b94ae | 1130 | /* Waiters must see I_SYNC cleared before being woken up */ |
1c0eeaf5 JE |
1131 | smp_mb(); |
1132 | wake_up_bit(&inode->i_state, __I_SYNC); | |
1133 | } | |
1134 | ||
d2caa3c5 JL |
1135 | static bool inode_dirtied_after(struct inode *inode, unsigned long t) |
1136 | { | |
1137 | bool ret = time_after(inode->dirtied_when, t); | |
1138 | #ifndef CONFIG_64BIT | |
1139 | /* | |
1140 | * For inodes being constantly redirtied, dirtied_when can get stuck. | |
1141 | * It _appears_ to be in the future, but is actually in distant past. | |
1142 | * This test is necessary to prevent such wrapped-around relative times | |
5b0830cb | 1143 | * from permanently stopping the whole bdi writeback. |
d2caa3c5 JL |
1144 | */ |
1145 | ret = ret && time_before_eq(inode->dirtied_when, jiffies); | |
1146 | #endif | |
1147 | return ret; | |
1148 | } | |
1149 | ||
0ae45f63 TT |
1150 | #define EXPIRE_DIRTY_ATIME 0x0001 |
1151 | ||
2c136579 | 1152 | /* |
0e2f2b23 | 1153 | * Move expired (dirtied before work->older_than_this) dirty inodes from |
697e6fed | 1154 | * @delaying_queue to @dispatch_queue. |
2c136579 | 1155 | */ |
e84d0a4f | 1156 | static int move_expired_inodes(struct list_head *delaying_queue, |
2c136579 | 1157 | struct list_head *dispatch_queue, |
0ae45f63 | 1158 | int flags, |
ad4e38dd | 1159 | struct wb_writeback_work *work) |
2c136579 | 1160 | { |
0ae45f63 TT |
1161 | unsigned long *older_than_this = NULL; |
1162 | unsigned long expire_time; | |
5c03449d SL |
1163 | LIST_HEAD(tmp); |
1164 | struct list_head *pos, *node; | |
cf137307 | 1165 | struct super_block *sb = NULL; |
5c03449d | 1166 | struct inode *inode; |
cf137307 | 1167 | int do_sb_sort = 0; |
e84d0a4f | 1168 | int moved = 0; |
5c03449d | 1169 | |
0ae45f63 TT |
1170 | if ((flags & EXPIRE_DIRTY_ATIME) == 0) |
1171 | older_than_this = work->older_than_this; | |
a2f48706 TT |
1172 | else if (!work->for_sync) { |
1173 | expire_time = jiffies - (dirtytime_expire_interval * HZ); | |
0ae45f63 TT |
1174 | older_than_this = &expire_time; |
1175 | } | |
2c136579 | 1176 | while (!list_empty(delaying_queue)) { |
7ccf19a8 | 1177 | inode = wb_inode(delaying_queue->prev); |
0ae45f63 TT |
1178 | if (older_than_this && |
1179 | inode_dirtied_after(inode, *older_than_this)) | |
2c136579 | 1180 | break; |
c7f54084 | 1181 | list_move(&inode->i_io_list, &tmp); |
a8855990 | 1182 | moved++; |
0ae45f63 TT |
1183 | if (flags & EXPIRE_DIRTY_ATIME) |
1184 | set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state); | |
a8855990 JK |
1185 | if (sb_is_blkdev_sb(inode->i_sb)) |
1186 | continue; | |
cf137307 JA |
1187 | if (sb && sb != inode->i_sb) |
1188 | do_sb_sort = 1; | |
1189 | sb = inode->i_sb; | |
5c03449d SL |
1190 | } |
1191 | ||
cf137307 JA |
1192 | /* just one sb in list, splice to dispatch_queue and we're done */ |
1193 | if (!do_sb_sort) { | |
1194 | list_splice(&tmp, dispatch_queue); | |
e84d0a4f | 1195 | goto out; |
cf137307 JA |
1196 | } |
1197 | ||
5c03449d SL |
1198 | /* Move inodes from one superblock together */ |
1199 | while (!list_empty(&tmp)) { | |
7ccf19a8 | 1200 | sb = wb_inode(tmp.prev)->i_sb; |
5c03449d | 1201 | list_for_each_prev_safe(pos, node, &tmp) { |
7ccf19a8 | 1202 | inode = wb_inode(pos); |
5c03449d | 1203 | if (inode->i_sb == sb) |
c7f54084 | 1204 | list_move(&inode->i_io_list, dispatch_queue); |
5c03449d | 1205 | } |
2c136579 | 1206 | } |
e84d0a4f WF |
1207 | out: |
1208 | return moved; | |
2c136579 FW |
1209 | } |
1210 | ||
1211 | /* | |
1212 | * Queue all expired dirty inodes for io, eldest first. | |
4ea879b9 WF |
1213 | * Before |
1214 | * newly dirtied b_dirty b_io b_more_io | |
1215 | * =============> gf edc BA | |
1216 | * After | |
1217 | * newly dirtied b_dirty b_io b_more_io | |
1218 | * =============> g fBAedc | |
1219 | * | | |
1220 | * +--> dequeue for IO | |
2c136579 | 1221 | */ |
ad4e38dd | 1222 | static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work) |
66f3b8e2 | 1223 | { |
e84d0a4f | 1224 | int moved; |
0ae45f63 | 1225 | |
f758eeab | 1226 | assert_spin_locked(&wb->list_lock); |
4ea879b9 | 1227 | list_splice_init(&wb->b_more_io, &wb->b_io); |
0ae45f63 TT |
1228 | moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work); |
1229 | moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, | |
1230 | EXPIRE_DIRTY_ATIME, work); | |
d6c10f1f TH |
1231 | if (moved) |
1232 | wb_io_lists_populated(wb); | |
ad4e38dd | 1233 | trace_writeback_queue_io(wb, work, moved); |
66f3b8e2 JA |
1234 | } |
1235 | ||
a9185b41 | 1236 | static int write_inode(struct inode *inode, struct writeback_control *wbc) |
08d8e974 | 1237 | { |
9fb0a7da TH |
1238 | int ret; |
1239 | ||
1240 | if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { | |
1241 | trace_writeback_write_inode_start(inode, wbc); | |
1242 | ret = inode->i_sb->s_op->write_inode(inode, wbc); | |
1243 | trace_writeback_write_inode(inode, wbc); | |
1244 | return ret; | |
1245 | } | |
03ba3782 | 1246 | return 0; |
08d8e974 | 1247 | } |
08d8e974 | 1248 | |
1da177e4 | 1249 | /* |
169ebd90 JK |
1250 | * Wait for writeback on an inode to complete. Called with i_lock held. |
1251 | * Caller must make sure inode cannot go away when we drop i_lock. | |
01c03194 | 1252 | */ |
169ebd90 JK |
1253 | static void __inode_wait_for_writeback(struct inode *inode) |
1254 | __releases(inode->i_lock) | |
1255 | __acquires(inode->i_lock) | |
01c03194 CH |
1256 | { |
1257 | DEFINE_WAIT_BIT(wq, &inode->i_state, __I_SYNC); | |
1258 | wait_queue_head_t *wqh; | |
1259 | ||
1260 | wqh = bit_waitqueue(&inode->i_state, __I_SYNC); | |
250df6ed DC |
1261 | while (inode->i_state & I_SYNC) { |
1262 | spin_unlock(&inode->i_lock); | |
74316201 N |
1263 | __wait_on_bit(wqh, &wq, bit_wait, |
1264 | TASK_UNINTERRUPTIBLE); | |
250df6ed | 1265 | spin_lock(&inode->i_lock); |
58a9d3d8 | 1266 | } |
01c03194 CH |
1267 | } |
1268 | ||
169ebd90 JK |
1269 | /* |
1270 | * Wait for writeback on an inode to complete. Caller must have inode pinned. | |
1271 | */ | |
1272 | void inode_wait_for_writeback(struct inode *inode) | |
1273 | { | |
1274 | spin_lock(&inode->i_lock); | |
1275 | __inode_wait_for_writeback(inode); | |
1276 | spin_unlock(&inode->i_lock); | |
1277 | } | |
1278 | ||
1279 | /* | |
1280 | * Sleep until I_SYNC is cleared. This function must be called with i_lock | |
1281 | * held and drops it. It is aimed for callers not holding any inode reference | |
1282 | * so once i_lock is dropped, inode can go away. | |
1283 | */ | |
1284 | static void inode_sleep_on_writeback(struct inode *inode) | |
1285 | __releases(inode->i_lock) | |
1286 | { | |
1287 | DEFINE_WAIT(wait); | |
1288 | wait_queue_head_t *wqh = bit_waitqueue(&inode->i_state, __I_SYNC); | |
1289 | int sleep; | |
1290 | ||
1291 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
1292 | sleep = inode->i_state & I_SYNC; | |
1293 | spin_unlock(&inode->i_lock); | |
1294 | if (sleep) | |
1295 | schedule(); | |
1296 | finish_wait(wqh, &wait); | |
1297 | } | |
1298 | ||
ccb26b5a JK |
1299 | /* |
1300 | * Find proper writeback list for the inode depending on its current state and | |
1301 | * possibly also change of its state while we were doing writeback. Here we | |
1302 | * handle things such as livelock prevention or fairness of writeback among | |
1303 | * inodes. This function can be called only by flusher thread - noone else | |
1304 | * processes all inodes in writeback lists and requeueing inodes behind flusher | |
1305 | * thread's back can have unexpected consequences. | |
1306 | */ | |
1307 | static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, | |
1308 | struct writeback_control *wbc) | |
1309 | { | |
1310 | if (inode->i_state & I_FREEING) | |
1311 | return; | |
1312 | ||
1313 | /* | |
1314 | * Sync livelock prevention. Each inode is tagged and synced in one | |
1315 | * shot. If still dirty, it will be redirty_tail()'ed below. Update | |
1316 | * the dirty time to prevent enqueue and sync it again. | |
1317 | */ | |
1318 | if ((inode->i_state & I_DIRTY) && | |
1319 | (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) | |
1320 | inode->dirtied_when = jiffies; | |
1321 | ||
4f8ad655 JK |
1322 | if (wbc->pages_skipped) { |
1323 | /* | |
1324 | * writeback is not making progress due to locked | |
1325 | * buffers. Skip this inode for now. | |
1326 | */ | |
1327 | redirty_tail(inode, wb); | |
1328 | return; | |
1329 | } | |
1330 | ||
ccb26b5a JK |
1331 | if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { |
1332 | /* | |
1333 | * We didn't write back all the pages. nfs_writepages() | |
1334 | * sometimes bales out without doing anything. | |
1335 | */ | |
1336 | if (wbc->nr_to_write <= 0) { | |
1337 | /* Slice used up. Queue for next turn. */ | |
1338 | requeue_io(inode, wb); | |
1339 | } else { | |
1340 | /* | |
1341 | * Writeback blocked by something other than | |
1342 | * congestion. Delay the inode for some time to | |
1343 | * avoid spinning on the CPU (100% iowait) | |
1344 | * retrying writeback of the dirty page/inode | |
1345 | * that cannot be performed immediately. | |
1346 | */ | |
1347 | redirty_tail(inode, wb); | |
1348 | } | |
1349 | } else if (inode->i_state & I_DIRTY) { | |
1350 | /* | |
1351 | * Filesystems can dirty the inode during writeback operations, | |
1352 | * such as delayed allocation during submission or metadata | |
1353 | * updates after data IO completion. | |
1354 | */ | |
1355 | redirty_tail(inode, wb); | |
0ae45f63 | 1356 | } else if (inode->i_state & I_DIRTY_TIME) { |
a2f48706 | 1357 | inode->dirtied_when = jiffies; |
c7f54084 | 1358 | inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); |
ccb26b5a JK |
1359 | } else { |
1360 | /* The inode is clean. Remove from writeback lists. */ | |
c7f54084 | 1361 | inode_io_list_del_locked(inode, wb); |
ccb26b5a JK |
1362 | } |
1363 | } | |
1364 | ||
01c03194 | 1365 | /* |
4f8ad655 JK |
1366 | * Write out an inode and its dirty pages. Do not update the writeback list |
1367 | * linkage. That is left to the caller. The caller is also responsible for | |
1368 | * setting I_SYNC flag and calling inode_sync_complete() to clear it. | |
1da177e4 LT |
1369 | */ |
1370 | static int | |
cd8ed2a4 | 1371 | __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) |
1da177e4 | 1372 | { |
1da177e4 | 1373 | struct address_space *mapping = inode->i_mapping; |
251d6a47 | 1374 | long nr_to_write = wbc->nr_to_write; |
01c03194 | 1375 | unsigned dirty; |
1da177e4 LT |
1376 | int ret; |
1377 | ||
4f8ad655 | 1378 | WARN_ON(!(inode->i_state & I_SYNC)); |
1da177e4 | 1379 | |
9fb0a7da TH |
1380 | trace_writeback_single_inode_start(inode, wbc, nr_to_write); |
1381 | ||
1da177e4 LT |
1382 | ret = do_writepages(mapping, wbc); |
1383 | ||
26821ed4 CH |
1384 | /* |
1385 | * Make sure to wait on the data before writing out the metadata. | |
1386 | * This is important for filesystems that modify metadata on data | |
7747bd4b DC |
1387 | * I/O completion. We don't do it for sync(2) writeback because it has a |
1388 | * separate, external IO completion path and ->sync_fs for guaranteeing | |
1389 | * inode metadata is written back correctly. | |
26821ed4 | 1390 | */ |
7747bd4b | 1391 | if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { |
26821ed4 | 1392 | int err = filemap_fdatawait(mapping); |
1da177e4 LT |
1393 | if (ret == 0) |
1394 | ret = err; | |
1395 | } | |
1396 | ||
5547e8aa DM |
1397 | /* |
1398 | * Some filesystems may redirty the inode during the writeback | |
1399 | * due to delalloc, clear dirty metadata flags right before | |
1400 | * write_inode() | |
1401 | */ | |
250df6ed | 1402 | spin_lock(&inode->i_lock); |
9c6ac78e | 1403 | |
5547e8aa | 1404 | dirty = inode->i_state & I_DIRTY; |
a2f48706 | 1405 | if (inode->i_state & I_DIRTY_TIME) { |
0e11f644 | 1406 | if ((dirty & I_DIRTY_INODE) || |
dc5ff2b1 | 1407 | wbc->sync_mode == WB_SYNC_ALL || |
a2f48706 TT |
1408 | unlikely(inode->i_state & I_DIRTY_TIME_EXPIRED) || |
1409 | unlikely(time_after(jiffies, | |
1410 | (inode->dirtied_time_when + | |
1411 | dirtytime_expire_interval * HZ)))) { | |
1412 | dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED; | |
1413 | trace_writeback_lazytime(inode); | |
1414 | } | |
1415 | } else | |
1416 | inode->i_state &= ~I_DIRTY_TIME_EXPIRED; | |
0ae45f63 | 1417 | inode->i_state &= ~dirty; |
9c6ac78e TH |
1418 | |
1419 | /* | |
1420 | * Paired with smp_mb() in __mark_inode_dirty(). This allows | |
1421 | * __mark_inode_dirty() to test i_state without grabbing i_lock - | |
1422 | * either they see the I_DIRTY bits cleared or we see the dirtied | |
1423 | * inode. | |
1424 | * | |
1425 | * I_DIRTY_PAGES is always cleared together above even if @mapping | |
1426 | * still has dirty pages. The flag is reinstated after smp_mb() if | |
1427 | * necessary. This guarantees that either __mark_inode_dirty() | |
1428 | * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. | |
1429 | */ | |
1430 | smp_mb(); | |
1431 | ||
1432 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | |
1433 | inode->i_state |= I_DIRTY_PAGES; | |
1434 | ||
250df6ed | 1435 | spin_unlock(&inode->i_lock); |
9c6ac78e | 1436 | |
0ae45f63 TT |
1437 | if (dirty & I_DIRTY_TIME) |
1438 | mark_inode_dirty_sync(inode); | |
26821ed4 | 1439 | /* Don't write the inode if only I_DIRTY_PAGES was set */ |
0ae45f63 | 1440 | if (dirty & ~I_DIRTY_PAGES) { |
a9185b41 | 1441 | int err = write_inode(inode, wbc); |
1da177e4 LT |
1442 | if (ret == 0) |
1443 | ret = err; | |
1444 | } | |
4f8ad655 JK |
1445 | trace_writeback_single_inode(inode, wbc, nr_to_write); |
1446 | return ret; | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Write out an inode's dirty pages. Either the caller has an active reference | |
1451 | * on the inode or the inode has I_WILL_FREE set. | |
1452 | * | |
1453 | * This function is designed to be called for writing back one inode which | |
1454 | * we go e.g. from filesystem. Flusher thread uses __writeback_single_inode() | |
1455 | * and does more profound writeback list handling in writeback_sb_inodes(). | |
1456 | */ | |
aaf25593 TH |
1457 | static int writeback_single_inode(struct inode *inode, |
1458 | struct writeback_control *wbc) | |
4f8ad655 | 1459 | { |
aaf25593 | 1460 | struct bdi_writeback *wb; |
4f8ad655 JK |
1461 | int ret = 0; |
1462 | ||
1463 | spin_lock(&inode->i_lock); | |
1464 | if (!atomic_read(&inode->i_count)) | |
1465 | WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); | |
1466 | else | |
1467 | WARN_ON(inode->i_state & I_WILL_FREE); | |
1468 | ||
1469 | if (inode->i_state & I_SYNC) { | |
1470 | if (wbc->sync_mode != WB_SYNC_ALL) | |
1471 | goto out; | |
1472 | /* | |
169ebd90 JK |
1473 | * It's a data-integrity sync. We must wait. Since callers hold |
1474 | * inode reference or inode has I_WILL_FREE set, it cannot go | |
1475 | * away under us. | |
4f8ad655 | 1476 | */ |
169ebd90 | 1477 | __inode_wait_for_writeback(inode); |
4f8ad655 JK |
1478 | } |
1479 | WARN_ON(inode->i_state & I_SYNC); | |
1480 | /* | |
f9b0e058 JK |
1481 | * Skip inode if it is clean and we have no outstanding writeback in |
1482 | * WB_SYNC_ALL mode. We don't want to mess with writeback lists in this | |
1483 | * function since flusher thread may be doing for example sync in | |
1484 | * parallel and if we move the inode, it could get skipped. So here we | |
1485 | * make sure inode is on some writeback list and leave it there unless | |
1486 | * we have completely cleaned the inode. | |
4f8ad655 | 1487 | */ |
0ae45f63 | 1488 | if (!(inode->i_state & I_DIRTY_ALL) && |
f9b0e058 JK |
1489 | (wbc->sync_mode != WB_SYNC_ALL || |
1490 | !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) | |
4f8ad655 JK |
1491 | goto out; |
1492 | inode->i_state |= I_SYNC; | |
b16b1deb | 1493 | wbc_attach_and_unlock_inode(wbc, inode); |
4f8ad655 | 1494 | |
cd8ed2a4 | 1495 | ret = __writeback_single_inode(inode, wbc); |
1da177e4 | 1496 | |
b16b1deb | 1497 | wbc_detach_inode(wbc); |
aaf25593 TH |
1498 | |
1499 | wb = inode_to_wb_and_lock_list(inode); | |
250df6ed | 1500 | spin_lock(&inode->i_lock); |
4f8ad655 JK |
1501 | /* |
1502 | * If inode is clean, remove it from writeback lists. Otherwise don't | |
1503 | * touch it. See comment above for explanation. | |
1504 | */ | |
0ae45f63 | 1505 | if (!(inode->i_state & I_DIRTY_ALL)) |
c7f54084 | 1506 | inode_io_list_del_locked(inode, wb); |
4f8ad655 | 1507 | spin_unlock(&wb->list_lock); |
1c0eeaf5 | 1508 | inode_sync_complete(inode); |
4f8ad655 JK |
1509 | out: |
1510 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
1511 | return ret; |
1512 | } | |
1513 | ||
a88a341a | 1514 | static long writeback_chunk_size(struct bdi_writeback *wb, |
1a12d8bd | 1515 | struct wb_writeback_work *work) |
d46db3d5 WF |
1516 | { |
1517 | long pages; | |
1518 | ||
1519 | /* | |
1520 | * WB_SYNC_ALL mode does livelock avoidance by syncing dirty | |
1521 | * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX | |
1522 | * here avoids calling into writeback_inodes_wb() more than once. | |
1523 | * | |
1524 | * The intended call sequence for WB_SYNC_ALL writeback is: | |
1525 | * | |
1526 | * wb_writeback() | |
1527 | * writeback_sb_inodes() <== called only once | |
1528 | * write_cache_pages() <== called once for each inode | |
1529 | * (quickly) tag currently dirty pages | |
1530 | * (maybe slowly) sync all tagged pages | |
1531 | */ | |
1532 | if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) | |
1533 | pages = LONG_MAX; | |
1a12d8bd | 1534 | else { |
a88a341a | 1535 | pages = min(wb->avg_write_bandwidth / 2, |
dcc25ae7 | 1536 | global_wb_domain.dirty_limit / DIRTY_SCOPE); |
1a12d8bd WF |
1537 | pages = min(pages, work->nr_pages); |
1538 | pages = round_down(pages + MIN_WRITEBACK_PAGES, | |
1539 | MIN_WRITEBACK_PAGES); | |
1540 | } | |
d46db3d5 WF |
1541 | |
1542 | return pages; | |
1543 | } | |
1544 | ||
f11c9c5c ES |
1545 | /* |
1546 | * Write a portion of b_io inodes which belong to @sb. | |
edadfb10 | 1547 | * |
d46db3d5 | 1548 | * Return the number of pages and/or inodes written. |
0ba13fd1 LT |
1549 | * |
1550 | * NOTE! This is called with wb->list_lock held, and will | |
1551 | * unlock and relock that for each inode it ends up doing | |
1552 | * IO for. | |
f11c9c5c | 1553 | */ |
d46db3d5 WF |
1554 | static long writeback_sb_inodes(struct super_block *sb, |
1555 | struct bdi_writeback *wb, | |
1556 | struct wb_writeback_work *work) | |
1da177e4 | 1557 | { |
d46db3d5 WF |
1558 | struct writeback_control wbc = { |
1559 | .sync_mode = work->sync_mode, | |
1560 | .tagged_writepages = work->tagged_writepages, | |
1561 | .for_kupdate = work->for_kupdate, | |
1562 | .for_background = work->for_background, | |
7747bd4b | 1563 | .for_sync = work->for_sync, |
d46db3d5 WF |
1564 | .range_cyclic = work->range_cyclic, |
1565 | .range_start = 0, | |
1566 | .range_end = LLONG_MAX, | |
1567 | }; | |
1568 | unsigned long start_time = jiffies; | |
1569 | long write_chunk; | |
1570 | long wrote = 0; /* count both pages and inodes */ | |
1571 | ||
03ba3782 | 1572 | while (!list_empty(&wb->b_io)) { |
7ccf19a8 | 1573 | struct inode *inode = wb_inode(wb->b_io.prev); |
aaf25593 | 1574 | struct bdi_writeback *tmp_wb; |
edadfb10 CH |
1575 | |
1576 | if (inode->i_sb != sb) { | |
d46db3d5 | 1577 | if (work->sb) { |
edadfb10 CH |
1578 | /* |
1579 | * We only want to write back data for this | |
1580 | * superblock, move all inodes not belonging | |
1581 | * to it back onto the dirty list. | |
1582 | */ | |
f758eeab | 1583 | redirty_tail(inode, wb); |
edadfb10 CH |
1584 | continue; |
1585 | } | |
1586 | ||
1587 | /* | |
1588 | * The inode belongs to a different superblock. | |
1589 | * Bounce back to the caller to unpin this and | |
1590 | * pin the next superblock. | |
1591 | */ | |
d46db3d5 | 1592 | break; |
edadfb10 CH |
1593 | } |
1594 | ||
9843b76a | 1595 | /* |
331cbdee WL |
1596 | * Don't bother with new inodes or inodes being freed, first |
1597 | * kind does not need periodic writeout yet, and for the latter | |
9843b76a CH |
1598 | * kind writeout is handled by the freer. |
1599 | */ | |
250df6ed | 1600 | spin_lock(&inode->i_lock); |
9843b76a | 1601 | if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { |
250df6ed | 1602 | spin_unlock(&inode->i_lock); |
fcc5c222 | 1603 | redirty_tail(inode, wb); |
7ef0d737 NP |
1604 | continue; |
1605 | } | |
cc1676d9 JK |
1606 | if ((inode->i_state & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { |
1607 | /* | |
1608 | * If this inode is locked for writeback and we are not | |
1609 | * doing writeback-for-data-integrity, move it to | |
1610 | * b_more_io so that writeback can proceed with the | |
1611 | * other inodes on s_io. | |
1612 | * | |
1613 | * We'll have another go at writing back this inode | |
1614 | * when we completed a full scan of b_io. | |
1615 | */ | |
1616 | spin_unlock(&inode->i_lock); | |
1617 | requeue_io(inode, wb); | |
1618 | trace_writeback_sb_inodes_requeue(inode); | |
1619 | continue; | |
1620 | } | |
f0d07b7f JK |
1621 | spin_unlock(&wb->list_lock); |
1622 | ||
4f8ad655 JK |
1623 | /* |
1624 | * We already requeued the inode if it had I_SYNC set and we | |
1625 | * are doing WB_SYNC_NONE writeback. So this catches only the | |
1626 | * WB_SYNC_ALL case. | |
1627 | */ | |
169ebd90 JK |
1628 | if (inode->i_state & I_SYNC) { |
1629 | /* Wait for I_SYNC. This function drops i_lock... */ | |
1630 | inode_sleep_on_writeback(inode); | |
1631 | /* Inode may be gone, start again */ | |
ead188f9 | 1632 | spin_lock(&wb->list_lock); |
169ebd90 JK |
1633 | continue; |
1634 | } | |
4f8ad655 | 1635 | inode->i_state |= I_SYNC; |
b16b1deb | 1636 | wbc_attach_and_unlock_inode(&wbc, inode); |
169ebd90 | 1637 | |
a88a341a | 1638 | write_chunk = writeback_chunk_size(wb, work); |
d46db3d5 WF |
1639 | wbc.nr_to_write = write_chunk; |
1640 | wbc.pages_skipped = 0; | |
250df6ed | 1641 | |
169ebd90 JK |
1642 | /* |
1643 | * We use I_SYNC to pin the inode in memory. While it is set | |
1644 | * evict_inode() will wait so the inode cannot be freed. | |
1645 | */ | |
cd8ed2a4 | 1646 | __writeback_single_inode(inode, &wbc); |
250df6ed | 1647 | |
b16b1deb | 1648 | wbc_detach_inode(&wbc); |
d46db3d5 WF |
1649 | work->nr_pages -= write_chunk - wbc.nr_to_write; |
1650 | wrote += write_chunk - wbc.nr_to_write; | |
590dca3a CM |
1651 | |
1652 | if (need_resched()) { | |
1653 | /* | |
1654 | * We're trying to balance between building up a nice | |
1655 | * long list of IOs to improve our merge rate, and | |
1656 | * getting those IOs out quickly for anyone throttling | |
1657 | * in balance_dirty_pages(). cond_resched() doesn't | |
1658 | * unplug, so get our IOs out the door before we | |
1659 | * give up the CPU. | |
1660 | */ | |
1661 | blk_flush_plug(current); | |
1662 | cond_resched(); | |
1663 | } | |
1664 | ||
aaf25593 TH |
1665 | /* |
1666 | * Requeue @inode if still dirty. Be careful as @inode may | |
1667 | * have been switched to another wb in the meantime. | |
1668 | */ | |
1669 | tmp_wb = inode_to_wb_and_lock_list(inode); | |
4f8ad655 | 1670 | spin_lock(&inode->i_lock); |
0ae45f63 | 1671 | if (!(inode->i_state & I_DIRTY_ALL)) |
d46db3d5 | 1672 | wrote++; |
aaf25593 | 1673 | requeue_inode(inode, tmp_wb, &wbc); |
4f8ad655 | 1674 | inode_sync_complete(inode); |
0f1b1fd8 | 1675 | spin_unlock(&inode->i_lock); |
590dca3a | 1676 | |
aaf25593 TH |
1677 | if (unlikely(tmp_wb != wb)) { |
1678 | spin_unlock(&tmp_wb->list_lock); | |
1679 | spin_lock(&wb->list_lock); | |
1680 | } | |
1681 | ||
d46db3d5 WF |
1682 | /* |
1683 | * bail out to wb_writeback() often enough to check | |
1684 | * background threshold and other termination conditions. | |
1685 | */ | |
1686 | if (wrote) { | |
1687 | if (time_is_before_jiffies(start_time + HZ / 10UL)) | |
1688 | break; | |
1689 | if (work->nr_pages <= 0) | |
1690 | break; | |
8bc3be27 | 1691 | } |
1da177e4 | 1692 | } |
d46db3d5 | 1693 | return wrote; |
f11c9c5c ES |
1694 | } |
1695 | ||
d46db3d5 WF |
1696 | static long __writeback_inodes_wb(struct bdi_writeback *wb, |
1697 | struct wb_writeback_work *work) | |
f11c9c5c | 1698 | { |
d46db3d5 WF |
1699 | unsigned long start_time = jiffies; |
1700 | long wrote = 0; | |
38f21977 | 1701 | |
f11c9c5c | 1702 | while (!list_empty(&wb->b_io)) { |
7ccf19a8 | 1703 | struct inode *inode = wb_inode(wb->b_io.prev); |
f11c9c5c | 1704 | struct super_block *sb = inode->i_sb; |
9ecc2738 | 1705 | |
eb6ef3df | 1706 | if (!trylock_super(sb)) { |
0e995816 | 1707 | /* |
eb6ef3df | 1708 | * trylock_super() may fail consistently due to |
0e995816 WF |
1709 | * s_umount being grabbed by someone else. Don't use |
1710 | * requeue_io() to avoid busy retrying the inode/sb. | |
1711 | */ | |
1712 | redirty_tail(inode, wb); | |
edadfb10 | 1713 | continue; |
f11c9c5c | 1714 | } |
d46db3d5 | 1715 | wrote += writeback_sb_inodes(sb, wb, work); |
eb6ef3df | 1716 | up_read(&sb->s_umount); |
f11c9c5c | 1717 | |
d46db3d5 WF |
1718 | /* refer to the same tests at the end of writeback_sb_inodes */ |
1719 | if (wrote) { | |
1720 | if (time_is_before_jiffies(start_time + HZ / 10UL)) | |
1721 | break; | |
1722 | if (work->nr_pages <= 0) | |
1723 | break; | |
1724 | } | |
f11c9c5c | 1725 | } |
66f3b8e2 | 1726 | /* Leave any unwritten inodes on b_io */ |
d46db3d5 | 1727 | return wrote; |
66f3b8e2 JA |
1728 | } |
1729 | ||
7d9f073b | 1730 | static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, |
0e175a18 | 1731 | enum wb_reason reason) |
edadfb10 | 1732 | { |
d46db3d5 WF |
1733 | struct wb_writeback_work work = { |
1734 | .nr_pages = nr_pages, | |
1735 | .sync_mode = WB_SYNC_NONE, | |
1736 | .range_cyclic = 1, | |
0e175a18 | 1737 | .reason = reason, |
d46db3d5 | 1738 | }; |
505a666e | 1739 | struct blk_plug plug; |
edadfb10 | 1740 | |
505a666e | 1741 | blk_start_plug(&plug); |
f758eeab | 1742 | spin_lock(&wb->list_lock); |
424b351f | 1743 | if (list_empty(&wb->b_io)) |
ad4e38dd | 1744 | queue_io(wb, &work); |
d46db3d5 | 1745 | __writeback_inodes_wb(wb, &work); |
f758eeab | 1746 | spin_unlock(&wb->list_lock); |
505a666e | 1747 | blk_finish_plug(&plug); |
edadfb10 | 1748 | |
d46db3d5 WF |
1749 | return nr_pages - work.nr_pages; |
1750 | } | |
03ba3782 | 1751 | |
03ba3782 JA |
1752 | /* |
1753 | * Explicit flushing or periodic writeback of "old" data. | |
66f3b8e2 | 1754 | * |
03ba3782 JA |
1755 | * Define "old": the first time one of an inode's pages is dirtied, we mark the |
1756 | * dirtying-time in the inode's address_space. So this periodic writeback code | |
1757 | * just walks the superblock inode list, writing back any inodes which are | |
1758 | * older than a specific point in time. | |
66f3b8e2 | 1759 | * |
03ba3782 JA |
1760 | * Try to run once per dirty_writeback_interval. But if a writeback event |
1761 | * takes longer than a dirty_writeback_interval interval, then leave a | |
1762 | * one-second gap. | |
66f3b8e2 | 1763 | * |
03ba3782 JA |
1764 | * older_than_this takes precedence over nr_to_write. So we'll only write back |
1765 | * all dirty pages if they are all attached to "old" mappings. | |
66f3b8e2 | 1766 | */ |
c4a77a6c | 1767 | static long wb_writeback(struct bdi_writeback *wb, |
83ba7b07 | 1768 | struct wb_writeback_work *work) |
66f3b8e2 | 1769 | { |
e98be2d5 | 1770 | unsigned long wb_start = jiffies; |
d46db3d5 | 1771 | long nr_pages = work->nr_pages; |
0dc83bd3 | 1772 | unsigned long oldest_jif; |
a5989bdc | 1773 | struct inode *inode; |
d46db3d5 | 1774 | long progress; |
505a666e | 1775 | struct blk_plug plug; |
66f3b8e2 | 1776 | |
0dc83bd3 JK |
1777 | oldest_jif = jiffies; |
1778 | work->older_than_this = &oldest_jif; | |
38f21977 | 1779 | |
505a666e | 1780 | blk_start_plug(&plug); |
e8dfc305 | 1781 | spin_lock(&wb->list_lock); |
03ba3782 JA |
1782 | for (;;) { |
1783 | /* | |
d3ddec76 | 1784 | * Stop writeback when nr_pages has been consumed |
03ba3782 | 1785 | */ |
83ba7b07 | 1786 | if (work->nr_pages <= 0) |
03ba3782 | 1787 | break; |
66f3b8e2 | 1788 | |
aa373cf5 JK |
1789 | /* |
1790 | * Background writeout and kupdate-style writeback may | |
1791 | * run forever. Stop them if there is other work to do | |
1792 | * so that e.g. sync can proceed. They'll be restarted | |
1793 | * after the other works are all done. | |
1794 | */ | |
1795 | if ((work->for_background || work->for_kupdate) && | |
f0054bb1 | 1796 | !list_empty(&wb->work_list)) |
aa373cf5 JK |
1797 | break; |
1798 | ||
38f21977 | 1799 | /* |
d3ddec76 WF |
1800 | * For background writeout, stop when we are below the |
1801 | * background dirty threshold | |
38f21977 | 1802 | */ |
aa661bbe | 1803 | if (work->for_background && !wb_over_bg_thresh(wb)) |
03ba3782 | 1804 | break; |
38f21977 | 1805 | |
1bc36b64 JK |
1806 | /* |
1807 | * Kupdate and background works are special and we want to | |
1808 | * include all inodes that need writing. Livelock avoidance is | |
1809 | * handled by these works yielding to any other work so we are | |
1810 | * safe. | |
1811 | */ | |
ba9aa839 | 1812 | if (work->for_kupdate) { |
0dc83bd3 | 1813 | oldest_jif = jiffies - |
ba9aa839 | 1814 | msecs_to_jiffies(dirty_expire_interval * 10); |
1bc36b64 | 1815 | } else if (work->for_background) |
0dc83bd3 | 1816 | oldest_jif = jiffies; |
028c2dd1 | 1817 | |
5634cc2a | 1818 | trace_writeback_start(wb, work); |
e8dfc305 | 1819 | if (list_empty(&wb->b_io)) |
ad4e38dd | 1820 | queue_io(wb, work); |
83ba7b07 | 1821 | if (work->sb) |
d46db3d5 | 1822 | progress = writeback_sb_inodes(work->sb, wb, work); |
edadfb10 | 1823 | else |
d46db3d5 | 1824 | progress = __writeback_inodes_wb(wb, work); |
5634cc2a | 1825 | trace_writeback_written(wb, work); |
028c2dd1 | 1826 | |
e98be2d5 | 1827 | wb_update_bandwidth(wb, wb_start); |
03ba3782 JA |
1828 | |
1829 | /* | |
e6fb6da2 WF |
1830 | * Did we write something? Try for more |
1831 | * | |
1832 | * Dirty inodes are moved to b_io for writeback in batches. | |
1833 | * The completion of the current batch does not necessarily | |
1834 | * mean the overall work is done. So we keep looping as long | |
1835 | * as made some progress on cleaning pages or inodes. | |
03ba3782 | 1836 | */ |
d46db3d5 | 1837 | if (progress) |
71fd05a8 JA |
1838 | continue; |
1839 | /* | |
e6fb6da2 | 1840 | * No more inodes for IO, bail |
71fd05a8 | 1841 | */ |
b7a2441f | 1842 | if (list_empty(&wb->b_more_io)) |
03ba3782 | 1843 | break; |
71fd05a8 JA |
1844 | /* |
1845 | * Nothing written. Wait for some inode to | |
1846 | * become available for writeback. Otherwise | |
1847 | * we'll just busyloop. | |
1848 | */ | |
bace9248 TE |
1849 | trace_writeback_wait(wb, work); |
1850 | inode = wb_inode(wb->b_more_io.prev); | |
1851 | spin_lock(&inode->i_lock); | |
1852 | spin_unlock(&wb->list_lock); | |
1853 | /* This function drops i_lock... */ | |
1854 | inode_sleep_on_writeback(inode); | |
1855 | spin_lock(&wb->list_lock); | |
03ba3782 | 1856 | } |
e8dfc305 | 1857 | spin_unlock(&wb->list_lock); |
505a666e | 1858 | blk_finish_plug(&plug); |
03ba3782 | 1859 | |
d46db3d5 | 1860 | return nr_pages - work->nr_pages; |
03ba3782 JA |
1861 | } |
1862 | ||
1863 | /* | |
83ba7b07 | 1864 | * Return the next wb_writeback_work struct that hasn't been processed yet. |
03ba3782 | 1865 | */ |
f0054bb1 | 1866 | static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) |
03ba3782 | 1867 | { |
83ba7b07 | 1868 | struct wb_writeback_work *work = NULL; |
03ba3782 | 1869 | |
f0054bb1 TH |
1870 | spin_lock_bh(&wb->work_lock); |
1871 | if (!list_empty(&wb->work_list)) { | |
1872 | work = list_entry(wb->work_list.next, | |
83ba7b07 CH |
1873 | struct wb_writeback_work, list); |
1874 | list_del_init(&work->list); | |
03ba3782 | 1875 | } |
f0054bb1 | 1876 | spin_unlock_bh(&wb->work_lock); |
83ba7b07 | 1877 | return work; |
03ba3782 JA |
1878 | } |
1879 | ||
6585027a JK |
1880 | static long wb_check_background_flush(struct bdi_writeback *wb) |
1881 | { | |
aa661bbe | 1882 | if (wb_over_bg_thresh(wb)) { |
6585027a JK |
1883 | |
1884 | struct wb_writeback_work work = { | |
1885 | .nr_pages = LONG_MAX, | |
1886 | .sync_mode = WB_SYNC_NONE, | |
1887 | .for_background = 1, | |
1888 | .range_cyclic = 1, | |
0e175a18 | 1889 | .reason = WB_REASON_BACKGROUND, |
6585027a JK |
1890 | }; |
1891 | ||
1892 | return wb_writeback(wb, &work); | |
1893 | } | |
1894 | ||
1895 | return 0; | |
1896 | } | |
1897 | ||
03ba3782 JA |
1898 | static long wb_check_old_data_flush(struct bdi_writeback *wb) |
1899 | { | |
1900 | unsigned long expired; | |
1901 | long nr_pages; | |
1902 | ||
69b62d01 JA |
1903 | /* |
1904 | * When set to zero, disable periodic writeback | |
1905 | */ | |
1906 | if (!dirty_writeback_interval) | |
1907 | return 0; | |
1908 | ||
03ba3782 JA |
1909 | expired = wb->last_old_flush + |
1910 | msecs_to_jiffies(dirty_writeback_interval * 10); | |
1911 | if (time_before(jiffies, expired)) | |
1912 | return 0; | |
1913 | ||
1914 | wb->last_old_flush = jiffies; | |
cdf01dd5 | 1915 | nr_pages = get_nr_dirty_pages(); |
03ba3782 | 1916 | |
c4a77a6c | 1917 | if (nr_pages) { |
83ba7b07 | 1918 | struct wb_writeback_work work = { |
c4a77a6c JA |
1919 | .nr_pages = nr_pages, |
1920 | .sync_mode = WB_SYNC_NONE, | |
1921 | .for_kupdate = 1, | |
1922 | .range_cyclic = 1, | |
0e175a18 | 1923 | .reason = WB_REASON_PERIODIC, |
c4a77a6c JA |
1924 | }; |
1925 | ||
83ba7b07 | 1926 | return wb_writeback(wb, &work); |
c4a77a6c | 1927 | } |
03ba3782 JA |
1928 | |
1929 | return 0; | |
1930 | } | |
1931 | ||
85009b4f JA |
1932 | static long wb_check_start_all(struct bdi_writeback *wb) |
1933 | { | |
1934 | long nr_pages; | |
1935 | ||
1936 | if (!test_bit(WB_start_all, &wb->state)) | |
1937 | return 0; | |
1938 | ||
1939 | nr_pages = get_nr_dirty_pages(); | |
1940 | if (nr_pages) { | |
1941 | struct wb_writeback_work work = { | |
1942 | .nr_pages = wb_split_bdi_pages(wb, nr_pages), | |
1943 | .sync_mode = WB_SYNC_NONE, | |
1944 | .range_cyclic = 1, | |
1945 | .reason = wb->start_all_reason, | |
1946 | }; | |
1947 | ||
1948 | nr_pages = wb_writeback(wb, &work); | |
1949 | } | |
1950 | ||
1951 | clear_bit(WB_start_all, &wb->state); | |
1952 | return nr_pages; | |
1953 | } | |
1954 | ||
1955 | ||
03ba3782 JA |
1956 | /* |
1957 | * Retrieve work items and do the writeback they describe | |
1958 | */ | |
25d130ba | 1959 | static long wb_do_writeback(struct bdi_writeback *wb) |
03ba3782 | 1960 | { |
83ba7b07 | 1961 | struct wb_writeback_work *work; |
c4a77a6c | 1962 | long wrote = 0; |
03ba3782 | 1963 | |
4452226e | 1964 | set_bit(WB_writeback_running, &wb->state); |
f0054bb1 | 1965 | while ((work = get_next_work_item(wb)) != NULL) { |
5634cc2a | 1966 | trace_writeback_exec(wb, work); |
83ba7b07 | 1967 | wrote += wb_writeback(wb, work); |
4a3a485b | 1968 | finish_writeback_work(wb, work); |
03ba3782 JA |
1969 | } |
1970 | ||
85009b4f JA |
1971 | /* |
1972 | * Check for a flush-everything request | |
1973 | */ | |
1974 | wrote += wb_check_start_all(wb); | |
1975 | ||
03ba3782 JA |
1976 | /* |
1977 | * Check for periodic writeback, kupdated() style | |
1978 | */ | |
1979 | wrote += wb_check_old_data_flush(wb); | |
6585027a | 1980 | wrote += wb_check_background_flush(wb); |
4452226e | 1981 | clear_bit(WB_writeback_running, &wb->state); |
03ba3782 JA |
1982 | |
1983 | return wrote; | |
1984 | } | |
1985 | ||
1986 | /* | |
1987 | * Handle writeback of dirty data for the device backed by this bdi. Also | |
839a8e86 | 1988 | * reschedules periodically and does kupdated style flushing. |
03ba3782 | 1989 | */ |
f0054bb1 | 1990 | void wb_workfn(struct work_struct *work) |
03ba3782 | 1991 | { |
839a8e86 TH |
1992 | struct bdi_writeback *wb = container_of(to_delayed_work(work), |
1993 | struct bdi_writeback, dwork); | |
03ba3782 JA |
1994 | long pages_written; |
1995 | ||
f0054bb1 | 1996 | set_worker_desc("flush-%s", dev_name(wb->bdi->dev)); |
766f9164 | 1997 | current->flags |= PF_SWAPWRITE; |
455b2864 | 1998 | |
839a8e86 | 1999 | if (likely(!current_is_workqueue_rescuer() || |
4452226e | 2000 | !test_bit(WB_registered, &wb->state))) { |
6467716a | 2001 | /* |
f0054bb1 | 2002 | * The normal path. Keep writing back @wb until its |
839a8e86 | 2003 | * work_list is empty. Note that this path is also taken |
f0054bb1 | 2004 | * if @wb is shutting down even when we're running off the |
839a8e86 | 2005 | * rescuer as work_list needs to be drained. |
6467716a | 2006 | */ |
839a8e86 | 2007 | do { |
25d130ba | 2008 | pages_written = wb_do_writeback(wb); |
839a8e86 | 2009 | trace_writeback_pages_written(pages_written); |
f0054bb1 | 2010 | } while (!list_empty(&wb->work_list)); |
839a8e86 TH |
2011 | } else { |
2012 | /* | |
2013 | * bdi_wq can't get enough workers and we're running off | |
2014 | * the emergency worker. Don't hog it. Hopefully, 1024 is | |
2015 | * enough for efficient IO. | |
2016 | */ | |
f0054bb1 | 2017 | pages_written = writeback_inodes_wb(wb, 1024, |
839a8e86 | 2018 | WB_REASON_FORKER_THREAD); |
455b2864 | 2019 | trace_writeback_pages_written(pages_written); |
03ba3782 JA |
2020 | } |
2021 | ||
f0054bb1 | 2022 | if (!list_empty(&wb->work_list)) |
b8b78495 | 2023 | wb_wakeup(wb); |
6ca738d6 | 2024 | else if (wb_has_dirty_io(wb) && dirty_writeback_interval) |
f0054bb1 | 2025 | wb_wakeup_delayed(wb); |
455b2864 | 2026 | |
839a8e86 | 2027 | current->flags &= ~PF_SWAPWRITE; |
03ba3782 JA |
2028 | } |
2029 | ||
595043e5 JA |
2030 | /* |
2031 | * Start writeback of `nr_pages' pages on this bdi. If `nr_pages' is zero, | |
2032 | * write back the whole world. | |
2033 | */ | |
2034 | static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, | |
e8e8a0c6 | 2035 | enum wb_reason reason) |
595043e5 JA |
2036 | { |
2037 | struct bdi_writeback *wb; | |
2038 | ||
2039 | if (!bdi_has_dirty_io(bdi)) | |
2040 | return; | |
2041 | ||
2042 | list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) | |
e8e8a0c6 | 2043 | wb_start_writeback(wb, reason); |
595043e5 JA |
2044 | } |
2045 | ||
2046 | void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, | |
2047 | enum wb_reason reason) | |
2048 | { | |
595043e5 | 2049 | rcu_read_lock(); |
e8e8a0c6 | 2050 | __wakeup_flusher_threads_bdi(bdi, reason); |
595043e5 JA |
2051 | rcu_read_unlock(); |
2052 | } | |
2053 | ||
03ba3782 | 2054 | /* |
9ba4b2df | 2055 | * Wakeup the flusher threads to start writeback of all currently dirty pages |
03ba3782 | 2056 | */ |
9ba4b2df | 2057 | void wakeup_flusher_threads(enum wb_reason reason) |
03ba3782 | 2058 | { |
b8c2f347 | 2059 | struct backing_dev_info *bdi; |
03ba3782 | 2060 | |
51350ea0 KK |
2061 | /* |
2062 | * If we are expecting writeback progress we must submit plugged IO. | |
2063 | */ | |
2064 | if (blk_needs_flush_plug(current)) | |
2065 | blk_schedule_flush_plug(current); | |
2066 | ||
b8c2f347 | 2067 | rcu_read_lock(); |
595043e5 | 2068 | list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) |
e8e8a0c6 | 2069 | __wakeup_flusher_threads_bdi(bdi, reason); |
cfc4ba53 | 2070 | rcu_read_unlock(); |
1da177e4 LT |
2071 | } |
2072 | ||
a2f48706 TT |
2073 | /* |
2074 | * Wake up bdi's periodically to make sure dirtytime inodes gets | |
2075 | * written back periodically. We deliberately do *not* check the | |
2076 | * b_dirtytime list in wb_has_dirty_io(), since this would cause the | |
2077 | * kernel to be constantly waking up once there are any dirtytime | |
2078 | * inodes on the system. So instead we define a separate delayed work | |
2079 | * function which gets called much more rarely. (By default, only | |
2080 | * once every 12 hours.) | |
2081 | * | |
2082 | * If there is any other write activity going on in the file system, | |
2083 | * this function won't be necessary. But if the only thing that has | |
2084 | * happened on the file system is a dirtytime inode caused by an atime | |
2085 | * update, we need this infrastructure below to make sure that inode | |
2086 | * eventually gets pushed out to disk. | |
2087 | */ | |
2088 | static void wakeup_dirtytime_writeback(struct work_struct *w); | |
2089 | static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); | |
2090 | ||
2091 | static void wakeup_dirtytime_writeback(struct work_struct *w) | |
2092 | { | |
2093 | struct backing_dev_info *bdi; | |
2094 | ||
2095 | rcu_read_lock(); | |
2096 | list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { | |
001fe6f6 | 2097 | struct bdi_writeback *wb; |
001fe6f6 | 2098 | |
b817525a | 2099 | list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) |
6fdf860f TH |
2100 | if (!list_empty(&wb->b_dirty_time)) |
2101 | wb_wakeup(wb); | |
a2f48706 TT |
2102 | } |
2103 | rcu_read_unlock(); | |
2104 | schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); | |
2105 | } | |
2106 | ||
2107 | static int __init start_dirtytime_writeback(void) | |
2108 | { | |
2109 | schedule_delayed_work(&dirtytime_work, dirtytime_expire_interval * HZ); | |
2110 | return 0; | |
2111 | } | |
2112 | __initcall(start_dirtytime_writeback); | |
2113 | ||
1efff914 TT |
2114 | int dirtytime_interval_handler(struct ctl_table *table, int write, |
2115 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
2116 | { | |
2117 | int ret; | |
2118 | ||
2119 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); | |
2120 | if (ret == 0 && write) | |
2121 | mod_delayed_work(system_wq, &dirtytime_work, 0); | |
2122 | return ret; | |
2123 | } | |
2124 | ||
03ba3782 JA |
2125 | static noinline void block_dump___mark_inode_dirty(struct inode *inode) |
2126 | { | |
2127 | if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) { | |
2128 | struct dentry *dentry; | |
2129 | const char *name = "?"; | |
2130 | ||
2131 | dentry = d_find_alias(inode); | |
2132 | if (dentry) { | |
2133 | spin_lock(&dentry->d_lock); | |
2134 | name = (const char *) dentry->d_name.name; | |
2135 | } | |
2136 | printk(KERN_DEBUG | |
2137 | "%s(%d): dirtied inode %lu (%s) on %s\n", | |
2138 | current->comm, task_pid_nr(current), inode->i_ino, | |
2139 | name, inode->i_sb->s_id); | |
2140 | if (dentry) { | |
2141 | spin_unlock(&dentry->d_lock); | |
2142 | dput(dentry); | |
2143 | } | |
2144 | } | |
2145 | } | |
2146 | ||
2147 | /** | |
0117d427 MCC |
2148 | * __mark_inode_dirty - internal function |
2149 | * | |
2150 | * @inode: inode to mark | |
2151 | * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) | |
2152 | * | |
2153 | * Mark an inode as dirty. Callers should use mark_inode_dirty or | |
2154 | * mark_inode_dirty_sync. | |
1da177e4 | 2155 | * |
03ba3782 JA |
2156 | * Put the inode on the super block's dirty list. |
2157 | * | |
2158 | * CAREFUL! We mark it dirty unconditionally, but move it onto the | |
2159 | * dirty list only if it is hashed or if it refers to a blockdev. | |
2160 | * If it was not hashed, it will never be added to the dirty list | |
2161 | * even if it is later hashed, as it will have been marked dirty already. | |
2162 | * | |
2163 | * In short, make sure you hash any inodes _before_ you start marking | |
2164 | * them dirty. | |
1da177e4 | 2165 | * |
03ba3782 JA |
2166 | * Note that for blockdevs, inode->dirtied_when represents the dirtying time of |
2167 | * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of | |
2168 | * the kernel-internal blockdev inode represents the dirtying time of the | |
2169 | * blockdev's pages. This is why for I_DIRTY_PAGES we always use | |
2170 | * page->mapping->host, so the page-dirtying time is recorded in the internal | |
2171 | * blockdev inode. | |
1da177e4 | 2172 | */ |
03ba3782 | 2173 | void __mark_inode_dirty(struct inode *inode, int flags) |
1da177e4 | 2174 | { |
03ba3782 | 2175 | struct super_block *sb = inode->i_sb; |
0ae45f63 TT |
2176 | int dirtytime; |
2177 | ||
2178 | trace_writeback_mark_inode_dirty(inode, flags); | |
1da177e4 | 2179 | |
03ba3782 JA |
2180 | /* |
2181 | * Don't do this for I_DIRTY_PAGES - that doesn't actually | |
2182 | * dirty the inode itself | |
2183 | */ | |
0e11f644 | 2184 | if (flags & (I_DIRTY_INODE | I_DIRTY_TIME)) { |
9fb0a7da TH |
2185 | trace_writeback_dirty_inode_start(inode, flags); |
2186 | ||
03ba3782 | 2187 | if (sb->s_op->dirty_inode) |
aa385729 | 2188 | sb->s_op->dirty_inode(inode, flags); |
9fb0a7da TH |
2189 | |
2190 | trace_writeback_dirty_inode(inode, flags); | |
03ba3782 | 2191 | } |
0ae45f63 TT |
2192 | if (flags & I_DIRTY_INODE) |
2193 | flags &= ~I_DIRTY_TIME; | |
2194 | dirtytime = flags & I_DIRTY_TIME; | |
03ba3782 JA |
2195 | |
2196 | /* | |
9c6ac78e TH |
2197 | * Paired with smp_mb() in __writeback_single_inode() for the |
2198 | * following lockless i_state test. See there for details. | |
03ba3782 JA |
2199 | */ |
2200 | smp_mb(); | |
2201 | ||
0ae45f63 TT |
2202 | if (((inode->i_state & flags) == flags) || |
2203 | (dirtytime && (inode->i_state & I_DIRTY_INODE))) | |
03ba3782 JA |
2204 | return; |
2205 | ||
2206 | if (unlikely(block_dump)) | |
2207 | block_dump___mark_inode_dirty(inode); | |
2208 | ||
250df6ed | 2209 | spin_lock(&inode->i_lock); |
0ae45f63 TT |
2210 | if (dirtytime && (inode->i_state & I_DIRTY_INODE)) |
2211 | goto out_unlock_inode; | |
03ba3782 JA |
2212 | if ((inode->i_state & flags) != flags) { |
2213 | const int was_dirty = inode->i_state & I_DIRTY; | |
2214 | ||
52ebea74 TH |
2215 | inode_attach_wb(inode, NULL); |
2216 | ||
0ae45f63 TT |
2217 | if (flags & I_DIRTY_INODE) |
2218 | inode->i_state &= ~I_DIRTY_TIME; | |
03ba3782 JA |
2219 | inode->i_state |= flags; |
2220 | ||
2221 | /* | |
2222 | * If the inode is being synced, just update its dirty state. | |
2223 | * The unlocker will place the inode on the appropriate | |
2224 | * superblock list, based upon its state. | |
2225 | */ | |
2226 | if (inode->i_state & I_SYNC) | |
250df6ed | 2227 | goto out_unlock_inode; |
03ba3782 JA |
2228 | |
2229 | /* | |
2230 | * Only add valid (hashed) inodes to the superblock's | |
2231 | * dirty list. Add blockdev inodes as well. | |
2232 | */ | |
2233 | if (!S_ISBLK(inode->i_mode)) { | |
1d3382cb | 2234 | if (inode_unhashed(inode)) |
250df6ed | 2235 | goto out_unlock_inode; |
03ba3782 | 2236 | } |
a4ffdde6 | 2237 | if (inode->i_state & I_FREEING) |
250df6ed | 2238 | goto out_unlock_inode; |
03ba3782 JA |
2239 | |
2240 | /* | |
2241 | * If the inode was already on b_dirty/b_io/b_more_io, don't | |
2242 | * reposition it (that would break b_dirty time-ordering). | |
2243 | */ | |
2244 | if (!was_dirty) { | |
87e1d789 | 2245 | struct bdi_writeback *wb; |
d6c10f1f | 2246 | struct list_head *dirty_list; |
a66979ab | 2247 | bool wakeup_bdi = false; |
253c34e9 | 2248 | |
87e1d789 | 2249 | wb = locked_inode_to_wb_and_lock_list(inode); |
253c34e9 | 2250 | |
0747259d TH |
2251 | WARN(bdi_cap_writeback_dirty(wb->bdi) && |
2252 | !test_bit(WB_registered, &wb->state), | |
2253 | "bdi-%s not registered\n", wb->bdi->name); | |
03ba3782 JA |
2254 | |
2255 | inode->dirtied_when = jiffies; | |
a2f48706 TT |
2256 | if (dirtytime) |
2257 | inode->dirtied_time_when = jiffies; | |
d6c10f1f | 2258 | |
0e11f644 | 2259 | if (inode->i_state & I_DIRTY) |
0747259d | 2260 | dirty_list = &wb->b_dirty; |
a2f48706 | 2261 | else |
0747259d | 2262 | dirty_list = &wb->b_dirty_time; |
d6c10f1f | 2263 | |
c7f54084 | 2264 | wakeup_bdi = inode_io_list_move_locked(inode, wb, |
d6c10f1f TH |
2265 | dirty_list); |
2266 | ||
0747259d | 2267 | spin_unlock(&wb->list_lock); |
0ae45f63 | 2268 | trace_writeback_dirty_inode_enqueue(inode); |
a66979ab | 2269 | |
d6c10f1f TH |
2270 | /* |
2271 | * If this is the first dirty inode for this bdi, | |
2272 | * we have to wake-up the corresponding bdi thread | |
2273 | * to make sure background write-back happens | |
2274 | * later. | |
2275 | */ | |
0747259d TH |
2276 | if (bdi_cap_writeback_dirty(wb->bdi) && wakeup_bdi) |
2277 | wb_wakeup_delayed(wb); | |
a66979ab | 2278 | return; |
1da177e4 | 2279 | } |
1da177e4 | 2280 | } |
250df6ed DC |
2281 | out_unlock_inode: |
2282 | spin_unlock(&inode->i_lock); | |
03ba3782 JA |
2283 | } |
2284 | EXPORT_SYMBOL(__mark_inode_dirty); | |
2285 | ||
e97fedb9 DC |
2286 | /* |
2287 | * The @s_sync_lock is used to serialise concurrent sync operations | |
2288 | * to avoid lock contention problems with concurrent wait_sb_inodes() calls. | |
2289 | * Concurrent callers will block on the s_sync_lock rather than doing contending | |
2290 | * walks. The queueing maintains sync(2) required behaviour as all the IO that | |
2291 | * has been issued up to the time this function is enter is guaranteed to be | |
2292 | * completed by the time we have gained the lock and waited for all IO that is | |
2293 | * in progress regardless of the order callers are granted the lock. | |
2294 | */ | |
b6e51316 | 2295 | static void wait_sb_inodes(struct super_block *sb) |
03ba3782 | 2296 | { |
6c60d2b5 | 2297 | LIST_HEAD(sync_list); |
03ba3782 JA |
2298 | |
2299 | /* | |
2300 | * We need to be protected against the filesystem going from | |
2301 | * r/o to r/w or vice versa. | |
2302 | */ | |
b6e51316 | 2303 | WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
03ba3782 | 2304 | |
e97fedb9 | 2305 | mutex_lock(&sb->s_sync_lock); |
03ba3782 JA |
2306 | |
2307 | /* | |
6c60d2b5 DC |
2308 | * Splice the writeback list onto a temporary list to avoid waiting on |
2309 | * inodes that have started writeback after this point. | |
2310 | * | |
2311 | * Use rcu_read_lock() to keep the inodes around until we have a | |
2312 | * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as | |
2313 | * the local list because inodes can be dropped from either by writeback | |
2314 | * completion. | |
2315 | */ | |
2316 | rcu_read_lock(); | |
2317 | spin_lock_irq(&sb->s_inode_wblist_lock); | |
2318 | list_splice_init(&sb->s_inodes_wb, &sync_list); | |
2319 | ||
2320 | /* | |
2321 | * Data integrity sync. Must wait for all pages under writeback, because | |
2322 | * there may have been pages dirtied before our sync call, but which had | |
2323 | * writeout started before we write it out. In which case, the inode | |
2324 | * may not be on the dirty list, but we still have to wait for that | |
2325 | * writeout. | |
03ba3782 | 2326 | */ |
6c60d2b5 DC |
2327 | while (!list_empty(&sync_list)) { |
2328 | struct inode *inode = list_first_entry(&sync_list, struct inode, | |
2329 | i_wb_list); | |
250df6ed | 2330 | struct address_space *mapping = inode->i_mapping; |
03ba3782 | 2331 | |
6c60d2b5 DC |
2332 | /* |
2333 | * Move each inode back to the wb list before we drop the lock | |
2334 | * to preserve consistency between i_wb_list and the mapping | |
2335 | * writeback tag. Writeback completion is responsible to remove | |
2336 | * the inode from either list once the writeback tag is cleared. | |
2337 | */ | |
2338 | list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb); | |
2339 | ||
2340 | /* | |
2341 | * The mapping can appear untagged while still on-list since we | |
2342 | * do not have the mapping lock. Skip it here, wb completion | |
2343 | * will remove it. | |
2344 | */ | |
2345 | if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) | |
2346 | continue; | |
2347 | ||
2348 | spin_unlock_irq(&sb->s_inode_wblist_lock); | |
2349 | ||
250df6ed | 2350 | spin_lock(&inode->i_lock); |
6c60d2b5 | 2351 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { |
250df6ed | 2352 | spin_unlock(&inode->i_lock); |
6c60d2b5 DC |
2353 | |
2354 | spin_lock_irq(&sb->s_inode_wblist_lock); | |
03ba3782 | 2355 | continue; |
250df6ed | 2356 | } |
03ba3782 | 2357 | __iget(inode); |
250df6ed | 2358 | spin_unlock(&inode->i_lock); |
6c60d2b5 | 2359 | rcu_read_unlock(); |
03ba3782 | 2360 | |
aa750fd7 JN |
2361 | /* |
2362 | * We keep the error status of individual mapping so that | |
2363 | * applications can catch the writeback error using fsync(2). | |
2364 | * See filemap_fdatawait_keep_errors() for details. | |
2365 | */ | |
2366 | filemap_fdatawait_keep_errors(mapping); | |
03ba3782 JA |
2367 | |
2368 | cond_resched(); | |
2369 | ||
6c60d2b5 DC |
2370 | iput(inode); |
2371 | ||
2372 | rcu_read_lock(); | |
2373 | spin_lock_irq(&sb->s_inode_wblist_lock); | |
03ba3782 | 2374 | } |
6c60d2b5 DC |
2375 | spin_unlock_irq(&sb->s_inode_wblist_lock); |
2376 | rcu_read_unlock(); | |
e97fedb9 | 2377 | mutex_unlock(&sb->s_sync_lock); |
1da177e4 LT |
2378 | } |
2379 | ||
f30a7d0c TH |
2380 | static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, |
2381 | enum wb_reason reason, bool skip_if_busy) | |
1da177e4 | 2382 | { |
cc395d7f | 2383 | DEFINE_WB_COMPLETION_ONSTACK(done); |
83ba7b07 | 2384 | struct wb_writeback_work work = { |
6e6938b6 WF |
2385 | .sb = sb, |
2386 | .sync_mode = WB_SYNC_NONE, | |
2387 | .tagged_writepages = 1, | |
2388 | .done = &done, | |
2389 | .nr_pages = nr, | |
0e175a18 | 2390 | .reason = reason, |
3c4d7165 | 2391 | }; |
e7972912 | 2392 | struct backing_dev_info *bdi = sb->s_bdi; |
d8a8559c | 2393 | |
e7972912 | 2394 | if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) |
6eedc701 | 2395 | return; |
cf37e972 | 2396 | WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
f30a7d0c | 2397 | |
db125360 | 2398 | bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); |
cc395d7f | 2399 | wb_wait_for_completion(bdi, &done); |
e913fc82 | 2400 | } |
f30a7d0c TH |
2401 | |
2402 | /** | |
2403 | * writeback_inodes_sb_nr - writeback dirty inodes from given super_block | |
2404 | * @sb: the superblock | |
2405 | * @nr: the number of pages to write | |
2406 | * @reason: reason why some writeback work initiated | |
2407 | * | |
2408 | * Start writeback on some inodes on this super_block. No guarantees are made | |
2409 | * on how many (if any) will be written, and this function does not wait | |
2410 | * for IO completion of submitted IO. | |
2411 | */ | |
2412 | void writeback_inodes_sb_nr(struct super_block *sb, | |
2413 | unsigned long nr, | |
2414 | enum wb_reason reason) | |
2415 | { | |
2416 | __writeback_inodes_sb_nr(sb, nr, reason, false); | |
2417 | } | |
3259f8be CM |
2418 | EXPORT_SYMBOL(writeback_inodes_sb_nr); |
2419 | ||
2420 | /** | |
2421 | * writeback_inodes_sb - writeback dirty inodes from given super_block | |
2422 | * @sb: the superblock | |
786228ab | 2423 | * @reason: reason why some writeback work was initiated |
3259f8be CM |
2424 | * |
2425 | * Start writeback on some inodes on this super_block. No guarantees are made | |
2426 | * on how many (if any) will be written, and this function does not wait | |
2427 | * for IO completion of submitted IO. | |
2428 | */ | |
0e175a18 | 2429 | void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) |
3259f8be | 2430 | { |
0e175a18 | 2431 | return writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); |
3259f8be | 2432 | } |
0e3c9a22 | 2433 | EXPORT_SYMBOL(writeback_inodes_sb); |
e913fc82 | 2434 | |
17bd55d0 | 2435 | /** |
8264c321 | 2436 | * try_to_writeback_inodes_sb - try to start writeback if none underway |
17bd55d0 | 2437 | * @sb: the superblock |
8264c321 | 2438 | * @reason: reason why some writeback work was initiated |
17bd55d0 | 2439 | * |
8264c321 | 2440 | * Invoke __writeback_inodes_sb_nr if no writeback is currently underway. |
17bd55d0 | 2441 | */ |
8264c321 | 2442 | void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) |
17bd55d0 | 2443 | { |
10ee27a0 | 2444 | if (!down_read_trylock(&sb->s_umount)) |
8264c321 | 2445 | return; |
10ee27a0 | 2446 | |
8264c321 | 2447 | __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true); |
10ee27a0 | 2448 | up_read(&sb->s_umount); |
3259f8be | 2449 | } |
10ee27a0 | 2450 | EXPORT_SYMBOL(try_to_writeback_inodes_sb); |
3259f8be | 2451 | |
d8a8559c JA |
2452 | /** |
2453 | * sync_inodes_sb - sync sb inode pages | |
0dc83bd3 | 2454 | * @sb: the superblock |
d8a8559c JA |
2455 | * |
2456 | * This function writes and waits on any dirty inode belonging to this | |
0dc83bd3 | 2457 | * super_block. |
d8a8559c | 2458 | */ |
0dc83bd3 | 2459 | void sync_inodes_sb(struct super_block *sb) |
d8a8559c | 2460 | { |
cc395d7f | 2461 | DEFINE_WB_COMPLETION_ONSTACK(done); |
83ba7b07 | 2462 | struct wb_writeback_work work = { |
3c4d7165 CH |
2463 | .sb = sb, |
2464 | .sync_mode = WB_SYNC_ALL, | |
2465 | .nr_pages = LONG_MAX, | |
2466 | .range_cyclic = 0, | |
83ba7b07 | 2467 | .done = &done, |
0e175a18 | 2468 | .reason = WB_REASON_SYNC, |
7747bd4b | 2469 | .for_sync = 1, |
3c4d7165 | 2470 | }; |
e7972912 | 2471 | struct backing_dev_info *bdi = sb->s_bdi; |
3c4d7165 | 2472 | |
006a0973 TH |
2473 | /* |
2474 | * Can't skip on !bdi_has_dirty() because we should wait for !dirty | |
2475 | * inodes under writeback and I_DIRTY_TIME inodes ignored by | |
2476 | * bdi_has_dirty() need to be written out too. | |
2477 | */ | |
2478 | if (bdi == &noop_backing_dev_info) | |
6eedc701 | 2479 | return; |
cf37e972 CH |
2480 | WARN_ON(!rwsem_is_locked(&sb->s_umount)); |
2481 | ||
7fc5854f TH |
2482 | /* protect against inode wb switch, see inode_switch_wbs_work_fn() */ |
2483 | bdi_down_write_wb_switch_rwsem(bdi); | |
db125360 | 2484 | bdi_split_work_to_wbs(bdi, &work, false); |
cc395d7f | 2485 | wb_wait_for_completion(bdi, &done); |
7fc5854f | 2486 | bdi_up_write_wb_switch_rwsem(bdi); |
83ba7b07 | 2487 | |
b6e51316 | 2488 | wait_sb_inodes(sb); |
1da177e4 | 2489 | } |
d8a8559c | 2490 | EXPORT_SYMBOL(sync_inodes_sb); |
1da177e4 | 2491 | |
1da177e4 | 2492 | /** |
7f04c26d AA |
2493 | * write_inode_now - write an inode to disk |
2494 | * @inode: inode to write to disk | |
2495 | * @sync: whether the write should be synchronous or not | |
2496 | * | |
2497 | * This function commits an inode to disk immediately if it is dirty. This is | |
2498 | * primarily needed by knfsd. | |
1da177e4 | 2499 | * |
7f04c26d | 2500 | * The caller must either have a ref on the inode or must have set I_WILL_FREE. |
1da177e4 | 2501 | */ |
1da177e4 LT |
2502 | int write_inode_now(struct inode *inode, int sync) |
2503 | { | |
1da177e4 LT |
2504 | struct writeback_control wbc = { |
2505 | .nr_to_write = LONG_MAX, | |
18914b18 | 2506 | .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, |
111ebb6e OH |
2507 | .range_start = 0, |
2508 | .range_end = LLONG_MAX, | |
1da177e4 LT |
2509 | }; |
2510 | ||
2511 | if (!mapping_cap_writeback_dirty(inode->i_mapping)) | |
49364ce2 | 2512 | wbc.nr_to_write = 0; |
1da177e4 LT |
2513 | |
2514 | might_sleep(); | |
aaf25593 | 2515 | return writeback_single_inode(inode, &wbc); |
1da177e4 LT |
2516 | } |
2517 | EXPORT_SYMBOL(write_inode_now); | |
2518 | ||
2519 | /** | |
2520 | * sync_inode - write an inode and its pages to disk. | |
2521 | * @inode: the inode to sync | |
2522 | * @wbc: controls the writeback mode | |
2523 | * | |
2524 | * sync_inode() will write an inode and its pages to disk. It will also | |
2525 | * correctly update the inode on its superblock's dirty inode lists and will | |
2526 | * update inode->i_state. | |
2527 | * | |
2528 | * The caller must have a ref on the inode. | |
2529 | */ | |
2530 | int sync_inode(struct inode *inode, struct writeback_control *wbc) | |
2531 | { | |
aaf25593 | 2532 | return writeback_single_inode(inode, wbc); |
1da177e4 LT |
2533 | } |
2534 | EXPORT_SYMBOL(sync_inode); | |
c3765016 CH |
2535 | |
2536 | /** | |
c691b9d9 | 2537 | * sync_inode_metadata - write an inode to disk |
c3765016 CH |
2538 | * @inode: the inode to sync |
2539 | * @wait: wait for I/O to complete. | |
2540 | * | |
c691b9d9 | 2541 | * Write an inode to disk and adjust its dirty state after completion. |
c3765016 CH |
2542 | * |
2543 | * Note: only writes the actual inode, no associated data or other metadata. | |
2544 | */ | |
2545 | int sync_inode_metadata(struct inode *inode, int wait) | |
2546 | { | |
2547 | struct writeback_control wbc = { | |
2548 | .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, | |
2549 | .nr_to_write = 0, /* metadata-only */ | |
2550 | }; | |
2551 | ||
2552 | return sync_inode(inode, &wbc); | |
2553 | } | |
2554 | EXPORT_SYMBOL(sync_inode_metadata); |