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