Commit | Line | Data |
---|---|---|
3fcfab16 AM |
1 | |
2 | #include <linux/wait.h> | |
3 | #include <linux/backing-dev.h> | |
03ba3782 JA |
4 | #include <linux/kthread.h> |
5 | #include <linux/freezer.h> | |
3fcfab16 | 6 | #include <linux/fs.h> |
26160158 | 7 | #include <linux/pagemap.h> |
03ba3782 | 8 | #include <linux/mm.h> |
3fcfab16 AM |
9 | #include <linux/sched.h> |
10 | #include <linux/module.h> | |
cf0ca9fe PZ |
11 | #include <linux/writeback.h> |
12 | #include <linux/device.h> | |
455b2864 | 13 | #include <trace/events/writeback.h> |
cf0ca9fe | 14 | |
c3c53206 JA |
15 | static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0); |
16 | ||
5129a469 JE |
17 | struct backing_dev_info noop_backing_dev_info = { |
18 | .name = "noop", | |
976e48f8 | 19 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
5129a469 | 20 | }; |
a212b105 | 21 | EXPORT_SYMBOL_GPL(noop_backing_dev_info); |
5129a469 | 22 | |
cf0ca9fe | 23 | static struct class *bdi_class; |
cfc4ba53 JA |
24 | |
25 | /* | |
181387da | 26 | * bdi_lock protects updates to bdi_list. bdi_list has RCU reader side |
cfc4ba53 JA |
27 | * locking. |
28 | */ | |
03ba3782 | 29 | DEFINE_SPINLOCK(bdi_lock); |
66f3b8e2 | 30 | LIST_HEAD(bdi_list); |
03ba3782 | 31 | |
839a8e86 TH |
32 | /* bdi_wq serves all asynchronous writeback tasks */ |
33 | struct workqueue_struct *bdi_wq; | |
34 | ||
76f1418b MS |
35 | #ifdef CONFIG_DEBUG_FS |
36 | #include <linux/debugfs.h> | |
37 | #include <linux/seq_file.h> | |
38 | ||
39 | static struct dentry *bdi_debug_root; | |
40 | ||
41 | static void bdi_debug_init(void) | |
42 | { | |
43 | bdi_debug_root = debugfs_create_dir("bdi", NULL); | |
44 | } | |
45 | ||
46 | static int bdi_debug_stats_show(struct seq_file *m, void *v) | |
47 | { | |
48 | struct backing_dev_info *bdi = m->private; | |
c1955ce3 | 49 | struct bdi_writeback *wb = &bdi->wb; |
364aeb28 DR |
50 | unsigned long background_thresh; |
51 | unsigned long dirty_thresh; | |
52 | unsigned long bdi_thresh; | |
0ae45f63 | 53 | unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time; |
f09b00d3 JA |
54 | struct inode *inode; |
55 | ||
0ae45f63 | 56 | nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0; |
f758eeab | 57 | spin_lock(&wb->list_lock); |
7ccf19a8 | 58 | list_for_each_entry(inode, &wb->b_dirty, i_wb_list) |
c1955ce3 | 59 | nr_dirty++; |
7ccf19a8 | 60 | list_for_each_entry(inode, &wb->b_io, i_wb_list) |
c1955ce3 | 61 | nr_io++; |
7ccf19a8 | 62 | list_for_each_entry(inode, &wb->b_more_io, i_wb_list) |
c1955ce3 | 63 | nr_more_io++; |
0ae45f63 TT |
64 | list_for_each_entry(inode, &wb->b_dirty_time, i_wb_list) |
65 | if (inode->i_state & I_DIRTY_TIME) | |
66 | nr_dirty_time++; | |
f758eeab | 67 | spin_unlock(&wb->list_lock); |
76f1418b | 68 | |
16c4042f | 69 | global_dirty_limits(&background_thresh, &dirty_thresh); |
a88a341a | 70 | bdi_thresh = wb_dirty_limit(wb, dirty_thresh); |
76f1418b MS |
71 | |
72 | #define K(x) ((x) << (PAGE_SHIFT - 10)) | |
73 | seq_printf(m, | |
00821b00 WF |
74 | "BdiWriteback: %10lu kB\n" |
75 | "BdiReclaimable: %10lu kB\n" | |
76 | "BdiDirtyThresh: %10lu kB\n" | |
77 | "DirtyThresh: %10lu kB\n" | |
78 | "BackgroundThresh: %10lu kB\n" | |
c8e28ce0 | 79 | "BdiDirtied: %10lu kB\n" |
00821b00 WF |
80 | "BdiWritten: %10lu kB\n" |
81 | "BdiWriteBandwidth: %10lu kBps\n" | |
82 | "b_dirty: %10lu\n" | |
83 | "b_io: %10lu\n" | |
84 | "b_more_io: %10lu\n" | |
0ae45f63 | 85 | "b_dirty_time: %10lu\n" |
00821b00 WF |
86 | "bdi_list: %10u\n" |
87 | "state: %10lx\n", | |
93f78d88 TH |
88 | (unsigned long) K(wb_stat(wb, WB_WRITEBACK)), |
89 | (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)), | |
f7d2b1ec JK |
90 | K(bdi_thresh), |
91 | K(dirty_thresh), | |
92 | K(background_thresh), | |
93f78d88 TH |
93 | (unsigned long) K(wb_stat(wb, WB_DIRTIED)), |
94 | (unsigned long) K(wb_stat(wb, WB_WRITTEN)), | |
a88a341a | 95 | (unsigned long) K(wb->write_bandwidth), |
f7d2b1ec JK |
96 | nr_dirty, |
97 | nr_io, | |
98 | nr_more_io, | |
0ae45f63 | 99 | nr_dirty_time, |
4452226e | 100 | !list_empty(&bdi->bdi_list), bdi->wb.state); |
76f1418b MS |
101 | #undef K |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | static int bdi_debug_stats_open(struct inode *inode, struct file *file) | |
107 | { | |
108 | return single_open(file, bdi_debug_stats_show, inode->i_private); | |
109 | } | |
110 | ||
111 | static const struct file_operations bdi_debug_stats_fops = { | |
112 | .open = bdi_debug_stats_open, | |
113 | .read = seq_read, | |
114 | .llseek = seq_lseek, | |
115 | .release = single_release, | |
116 | }; | |
117 | ||
118 | static void bdi_debug_register(struct backing_dev_info *bdi, const char *name) | |
119 | { | |
120 | bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root); | |
121 | bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir, | |
122 | bdi, &bdi_debug_stats_fops); | |
123 | } | |
124 | ||
125 | static void bdi_debug_unregister(struct backing_dev_info *bdi) | |
126 | { | |
127 | debugfs_remove(bdi->debug_stats); | |
128 | debugfs_remove(bdi->debug_dir); | |
129 | } | |
130 | #else | |
131 | static inline void bdi_debug_init(void) | |
132 | { | |
133 | } | |
134 | static inline void bdi_debug_register(struct backing_dev_info *bdi, | |
135 | const char *name) | |
136 | { | |
137 | } | |
138 | static inline void bdi_debug_unregister(struct backing_dev_info *bdi) | |
139 | { | |
140 | } | |
141 | #endif | |
142 | ||
cf0ca9fe PZ |
143 | static ssize_t read_ahead_kb_store(struct device *dev, |
144 | struct device_attribute *attr, | |
145 | const char *buf, size_t count) | |
146 | { | |
147 | struct backing_dev_info *bdi = dev_get_drvdata(dev); | |
cf0ca9fe | 148 | unsigned long read_ahead_kb; |
7034ed13 | 149 | ssize_t ret; |
cf0ca9fe | 150 | |
7034ed13 NJ |
151 | ret = kstrtoul(buf, 10, &read_ahead_kb); |
152 | if (ret < 0) | |
153 | return ret; | |
154 | ||
155 | bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10); | |
156 | ||
157 | return count; | |
cf0ca9fe PZ |
158 | } |
159 | ||
160 | #define K(pages) ((pages) << (PAGE_SHIFT - 10)) | |
161 | ||
162 | #define BDI_SHOW(name, expr) \ | |
163 | static ssize_t name##_show(struct device *dev, \ | |
164 | struct device_attribute *attr, char *page) \ | |
165 | { \ | |
166 | struct backing_dev_info *bdi = dev_get_drvdata(dev); \ | |
167 | \ | |
168 | return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \ | |
d9e1241e GKH |
169 | } \ |
170 | static DEVICE_ATTR_RW(name); | |
cf0ca9fe PZ |
171 | |
172 | BDI_SHOW(read_ahead_kb, K(bdi->ra_pages)) | |
173 | ||
189d3c4a PZ |
174 | static ssize_t min_ratio_store(struct device *dev, |
175 | struct device_attribute *attr, const char *buf, size_t count) | |
176 | { | |
177 | struct backing_dev_info *bdi = dev_get_drvdata(dev); | |
189d3c4a | 178 | unsigned int ratio; |
7034ed13 NJ |
179 | ssize_t ret; |
180 | ||
181 | ret = kstrtouint(buf, 10, &ratio); | |
182 | if (ret < 0) | |
183 | return ret; | |
184 | ||
185 | ret = bdi_set_min_ratio(bdi, ratio); | |
186 | if (!ret) | |
187 | ret = count; | |
189d3c4a | 188 | |
189d3c4a PZ |
189 | return ret; |
190 | } | |
191 | BDI_SHOW(min_ratio, bdi->min_ratio) | |
192 | ||
a42dde04 PZ |
193 | static ssize_t max_ratio_store(struct device *dev, |
194 | struct device_attribute *attr, const char *buf, size_t count) | |
195 | { | |
196 | struct backing_dev_info *bdi = dev_get_drvdata(dev); | |
a42dde04 | 197 | unsigned int ratio; |
7034ed13 NJ |
198 | ssize_t ret; |
199 | ||
200 | ret = kstrtouint(buf, 10, &ratio); | |
201 | if (ret < 0) | |
202 | return ret; | |
203 | ||
204 | ret = bdi_set_max_ratio(bdi, ratio); | |
205 | if (!ret) | |
206 | ret = count; | |
a42dde04 | 207 | |
a42dde04 PZ |
208 | return ret; |
209 | } | |
210 | BDI_SHOW(max_ratio, bdi->max_ratio) | |
211 | ||
7d311cda DW |
212 | static ssize_t stable_pages_required_show(struct device *dev, |
213 | struct device_attribute *attr, | |
214 | char *page) | |
215 | { | |
216 | struct backing_dev_info *bdi = dev_get_drvdata(dev); | |
217 | ||
218 | return snprintf(page, PAGE_SIZE-1, "%d\n", | |
219 | bdi_cap_stable_pages_required(bdi) ? 1 : 0); | |
220 | } | |
d9e1241e GKH |
221 | static DEVICE_ATTR_RO(stable_pages_required); |
222 | ||
223 | static struct attribute *bdi_dev_attrs[] = { | |
224 | &dev_attr_read_ahead_kb.attr, | |
225 | &dev_attr_min_ratio.attr, | |
226 | &dev_attr_max_ratio.attr, | |
227 | &dev_attr_stable_pages_required.attr, | |
228 | NULL, | |
cf0ca9fe | 229 | }; |
d9e1241e | 230 | ATTRIBUTE_GROUPS(bdi_dev); |
cf0ca9fe PZ |
231 | |
232 | static __init int bdi_class_init(void) | |
233 | { | |
234 | bdi_class = class_create(THIS_MODULE, "bdi"); | |
14421453 AB |
235 | if (IS_ERR(bdi_class)) |
236 | return PTR_ERR(bdi_class); | |
237 | ||
d9e1241e | 238 | bdi_class->dev_groups = bdi_dev_groups; |
76f1418b | 239 | bdi_debug_init(); |
cf0ca9fe PZ |
240 | return 0; |
241 | } | |
76f1418b | 242 | postcore_initcall(bdi_class_init); |
cf0ca9fe | 243 | |
26160158 JA |
244 | static int __init default_bdi_init(void) |
245 | { | |
246 | int err; | |
247 | ||
839a8e86 | 248 | bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_FREEZABLE | |
b5c872dd | 249 | WQ_UNBOUND | WQ_SYSFS, 0); |
839a8e86 TH |
250 | if (!bdi_wq) |
251 | return -ENOMEM; | |
252 | ||
976e48f8 | 253 | err = bdi_init(&noop_backing_dev_info); |
26160158 JA |
254 | |
255 | return err; | |
256 | } | |
257 | subsys_initcall(default_bdi_init); | |
258 | ||
03ba3782 JA |
259 | int bdi_has_dirty_io(struct backing_dev_info *bdi) |
260 | { | |
261 | return wb_has_dirty_io(&bdi->wb); | |
262 | } | |
263 | ||
6467716a | 264 | /* |
f0054bb1 | 265 | * This function is used when the first inode for this wb is marked dirty. It |
6467716a AB |
266 | * wakes-up the corresponding bdi thread which should then take care of the |
267 | * periodic background write-out of dirty inodes. Since the write-out would | |
268 | * starts only 'dirty_writeback_interval' centisecs from now anyway, we just | |
269 | * set up a timer which wakes the bdi thread up later. | |
270 | * | |
271 | * Note, we wouldn't bother setting up the timer, but this function is on the | |
272 | * fast-path (used by '__mark_inode_dirty()'), so we save few context switches | |
273 | * by delaying the wake-up. | |
6ca738d6 DB |
274 | * |
275 | * We have to be careful not to postpone flush work if it is scheduled for | |
276 | * earlier. Thus we use queue_delayed_work(). | |
6467716a | 277 | */ |
f0054bb1 | 278 | void wb_wakeup_delayed(struct bdi_writeback *wb) |
6467716a AB |
279 | { |
280 | unsigned long timeout; | |
281 | ||
282 | timeout = msecs_to_jiffies(dirty_writeback_interval * 10); | |
f0054bb1 TH |
283 | spin_lock_bh(&wb->work_lock); |
284 | if (test_bit(WB_registered, &wb->state)) | |
285 | queue_delayed_work(bdi_wq, &wb->dwork, timeout); | |
286 | spin_unlock_bh(&wb->work_lock); | |
03ba3782 JA |
287 | } |
288 | ||
a88a341a TH |
289 | /* |
290 | * Initial write bandwidth: 100 MB/s | |
291 | */ | |
292 | #define INIT_BW (100 << (20 - PAGE_SHIFT)) | |
293 | ||
8395cd9f TH |
294 | static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi, |
295 | gfp_t gfp) | |
6467716a | 296 | { |
93f78d88 TH |
297 | int i, err; |
298 | ||
6467716a AB |
299 | memset(wb, 0, sizeof(*wb)); |
300 | ||
301 | wb->bdi = bdi; | |
302 | wb->last_old_flush = jiffies; | |
303 | INIT_LIST_HEAD(&wb->b_dirty); | |
304 | INIT_LIST_HEAD(&wb->b_io); | |
305 | INIT_LIST_HEAD(&wb->b_more_io); | |
0ae45f63 | 306 | INIT_LIST_HEAD(&wb->b_dirty_time); |
f758eeab | 307 | spin_lock_init(&wb->list_lock); |
93f78d88 | 308 | |
a88a341a TH |
309 | wb->bw_time_stamp = jiffies; |
310 | wb->balanced_dirty_ratelimit = INIT_BW; | |
311 | wb->dirty_ratelimit = INIT_BW; | |
312 | wb->write_bandwidth = INIT_BW; | |
313 | wb->avg_write_bandwidth = INIT_BW; | |
314 | ||
f0054bb1 TH |
315 | spin_lock_init(&wb->work_lock); |
316 | INIT_LIST_HEAD(&wb->work_list); | |
317 | INIT_DELAYED_WORK(&wb->dwork, wb_workfn); | |
318 | ||
8395cd9f | 319 | err = fprop_local_init_percpu(&wb->completions, gfp); |
a88a341a TH |
320 | if (err) |
321 | return err; | |
322 | ||
93f78d88 | 323 | for (i = 0; i < NR_WB_STAT_ITEMS; i++) { |
8395cd9f | 324 | err = percpu_counter_init(&wb->stat[i], 0, gfp); |
93f78d88 TH |
325 | if (err) { |
326 | while (--i) | |
327 | percpu_counter_destroy(&wb->stat[i]); | |
a88a341a | 328 | fprop_local_destroy_percpu(&wb->completions); |
93f78d88 TH |
329 | return err; |
330 | } | |
331 | } | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
46100071 TH |
336 | /* |
337 | * Remove bdi from the global list and shutdown any threads we have running | |
338 | */ | |
339 | static void wb_shutdown(struct bdi_writeback *wb) | |
340 | { | |
341 | /* Make sure nobody queues further work */ | |
342 | spin_lock_bh(&wb->work_lock); | |
343 | if (!test_and_clear_bit(WB_registered, &wb->state)) { | |
344 | spin_unlock_bh(&wb->work_lock); | |
345 | return; | |
346 | } | |
347 | spin_unlock_bh(&wb->work_lock); | |
348 | ||
349 | /* | |
350 | * Drain work list and shutdown the delayed_work. !WB_registered | |
351 | * tells wb_workfn() that @wb is dying and its work_list needs to | |
352 | * be drained no matter what. | |
353 | */ | |
354 | mod_delayed_work(bdi_wq, &wb->dwork, 0); | |
355 | flush_delayed_work(&wb->dwork); | |
356 | WARN_ON(!list_empty(&wb->work_list)); | |
357 | } | |
358 | ||
f0054bb1 | 359 | static void wb_exit(struct bdi_writeback *wb) |
93f78d88 TH |
360 | { |
361 | int i; | |
362 | ||
363 | WARN_ON(delayed_work_pending(&wb->dwork)); | |
364 | ||
365 | for (i = 0; i < NR_WB_STAT_ITEMS; i++) | |
366 | percpu_counter_destroy(&wb->stat[i]); | |
6467716a | 367 | |
a88a341a TH |
368 | fprop_local_destroy_percpu(&wb->completions); |
369 | } | |
e98be2d5 | 370 | |
b2e8fb6e PZ |
371 | int bdi_init(struct backing_dev_info *bdi) |
372 | { | |
93f78d88 | 373 | int err; |
b2e8fb6e | 374 | |
cf0ca9fe PZ |
375 | bdi->dev = NULL; |
376 | ||
189d3c4a | 377 | bdi->min_ratio = 0; |
a42dde04 | 378 | bdi->max_ratio = 100; |
eb608e3a | 379 | bdi->max_prop_frac = FPROP_FRAC_BASE; |
66f3b8e2 | 380 | INIT_LIST_HEAD(&bdi->bdi_list); |
03ba3782 | 381 | |
8395cd9f | 382 | err = wb_init(&bdi->wb, bdi, GFP_KERNEL); |
93f78d88 TH |
383 | if (err) |
384 | return err; | |
04fbfdc1 | 385 | |
4aa9c692 TH |
386 | bdi->wb_congested.state = 0; |
387 | bdi->wb.congested = &bdi->wb_congested; | |
388 | ||
93f78d88 | 389 | return 0; |
b2e8fb6e PZ |
390 | } |
391 | EXPORT_SYMBOL(bdi_init); | |
392 | ||
46100071 TH |
393 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
394 | const char *fmt, ...) | |
395 | { | |
396 | va_list args; | |
397 | struct device *dev; | |
398 | ||
399 | if (bdi->dev) /* The driver needs to use separate queues per device */ | |
400 | return 0; | |
401 | ||
402 | va_start(args, fmt); | |
403 | dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args); | |
404 | va_end(args); | |
405 | if (IS_ERR(dev)) | |
406 | return PTR_ERR(dev); | |
407 | ||
408 | bdi->dev = dev; | |
409 | ||
410 | bdi_debug_register(bdi, dev_name(dev)); | |
411 | set_bit(WB_registered, &bdi->wb.state); | |
412 | ||
413 | spin_lock_bh(&bdi_lock); | |
414 | list_add_tail_rcu(&bdi->bdi_list, &bdi_list); | |
415 | spin_unlock_bh(&bdi_lock); | |
416 | ||
417 | trace_writeback_bdi_register(bdi); | |
418 | return 0; | |
419 | } | |
420 | EXPORT_SYMBOL(bdi_register); | |
421 | ||
422 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev) | |
423 | { | |
424 | return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev)); | |
425 | } | |
426 | EXPORT_SYMBOL(bdi_register_dev); | |
427 | ||
428 | /* | |
429 | * Remove bdi from bdi_list, and ensure that it is no longer visible | |
430 | */ | |
431 | static void bdi_remove_from_list(struct backing_dev_info *bdi) | |
432 | { | |
433 | spin_lock_bh(&bdi_lock); | |
434 | list_del_rcu(&bdi->bdi_list); | |
435 | spin_unlock_bh(&bdi_lock); | |
436 | ||
437 | synchronize_rcu_expedited(); | |
438 | } | |
439 | ||
440 | /* | |
441 | * Called when the device behind @bdi has been removed or ejected. | |
442 | * | |
443 | * We can't really do much here except for reducing the dirty ratio at | |
444 | * the moment. In the future we should be able to set a flag so that | |
445 | * the filesystem can handle errors at mark_inode_dirty time instead | |
446 | * of only at writeback time. | |
447 | */ | |
448 | void bdi_unregister(struct backing_dev_info *bdi) | |
449 | { | |
450 | if (WARN_ON_ONCE(!bdi->dev)) | |
451 | return; | |
452 | ||
453 | bdi_set_min_ratio(bdi, 0); | |
454 | } | |
455 | EXPORT_SYMBOL(bdi_unregister); | |
456 | ||
b2e8fb6e PZ |
457 | void bdi_destroy(struct backing_dev_info *bdi) |
458 | { | |
f0054bb1 TH |
459 | /* make sure nobody finds us on the bdi_list anymore */ |
460 | bdi_remove_from_list(bdi); | |
461 | wb_shutdown(&bdi->wb); | |
7a401a97 | 462 | |
c4db59d3 CH |
463 | if (bdi->dev) { |
464 | bdi_debug_unregister(bdi); | |
465 | device_unregister(bdi->dev); | |
466 | bdi->dev = NULL; | |
467 | } | |
468 | ||
f0054bb1 | 469 | wb_exit(&bdi->wb); |
b2e8fb6e PZ |
470 | } |
471 | EXPORT_SYMBOL(bdi_destroy); | |
472 | ||
c3c53206 JA |
473 | /* |
474 | * For use from filesystems to quickly init and register a bdi associated | |
475 | * with dirty writeback | |
476 | */ | |
b4caecd4 | 477 | int bdi_setup_and_register(struct backing_dev_info *bdi, char *name) |
c3c53206 | 478 | { |
c3c53206 JA |
479 | int err; |
480 | ||
481 | bdi->name = name; | |
b4caecd4 | 482 | bdi->capabilities = 0; |
c3c53206 JA |
483 | err = bdi_init(bdi); |
484 | if (err) | |
485 | return err; | |
486 | ||
02aa2a37 KC |
487 | err = bdi_register(bdi, NULL, "%.28s-%ld", name, |
488 | atomic_long_inc_return(&bdi_seq)); | |
c3c53206 JA |
489 | if (err) { |
490 | bdi_destroy(bdi); | |
491 | return err; | |
492 | } | |
493 | ||
494 | return 0; | |
495 | } | |
496 | EXPORT_SYMBOL(bdi_setup_and_register); | |
497 | ||
3fcfab16 AM |
498 | static wait_queue_head_t congestion_wqh[2] = { |
499 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]), | |
500 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1]) | |
501 | }; | |
0e093d99 | 502 | static atomic_t nr_bdi_congested[2]; |
3fcfab16 | 503 | |
1faa16d2 | 504 | void clear_bdi_congested(struct backing_dev_info *bdi, int sync) |
3fcfab16 | 505 | { |
4452226e | 506 | enum wb_state bit; |
1faa16d2 | 507 | wait_queue_head_t *wqh = &congestion_wqh[sync]; |
3fcfab16 | 508 | |
4452226e | 509 | bit = sync ? WB_sync_congested : WB_async_congested; |
4aa9c692 | 510 | if (test_and_clear_bit(bit, &bdi->wb.congested->state)) |
0e093d99 | 511 | atomic_dec(&nr_bdi_congested[sync]); |
4e857c58 | 512 | smp_mb__after_atomic(); |
3fcfab16 AM |
513 | if (waitqueue_active(wqh)) |
514 | wake_up(wqh); | |
515 | } | |
516 | EXPORT_SYMBOL(clear_bdi_congested); | |
517 | ||
1faa16d2 | 518 | void set_bdi_congested(struct backing_dev_info *bdi, int sync) |
3fcfab16 | 519 | { |
4452226e | 520 | enum wb_state bit; |
3fcfab16 | 521 | |
4452226e | 522 | bit = sync ? WB_sync_congested : WB_async_congested; |
4aa9c692 | 523 | if (!test_and_set_bit(bit, &bdi->wb.congested->state)) |
0e093d99 | 524 | atomic_inc(&nr_bdi_congested[sync]); |
3fcfab16 AM |
525 | } |
526 | EXPORT_SYMBOL(set_bdi_congested); | |
527 | ||
528 | /** | |
529 | * congestion_wait - wait for a backing_dev to become uncongested | |
8aa7e847 | 530 | * @sync: SYNC or ASYNC IO |
3fcfab16 AM |
531 | * @timeout: timeout in jiffies |
532 | * | |
533 | * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit | |
534 | * write congestion. If no backing_devs are congested then just wait for the | |
535 | * next write to be completed. | |
536 | */ | |
8aa7e847 | 537 | long congestion_wait(int sync, long timeout) |
3fcfab16 AM |
538 | { |
539 | long ret; | |
52bb9198 | 540 | unsigned long start = jiffies; |
3fcfab16 | 541 | DEFINE_WAIT(wait); |
8aa7e847 | 542 | wait_queue_head_t *wqh = &congestion_wqh[sync]; |
3fcfab16 AM |
543 | |
544 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
545 | ret = io_schedule_timeout(timeout); | |
546 | finish_wait(wqh, &wait); | |
52bb9198 MG |
547 | |
548 | trace_writeback_congestion_wait(jiffies_to_usecs(timeout), | |
549 | jiffies_to_usecs(jiffies - start)); | |
550 | ||
3fcfab16 AM |
551 | return ret; |
552 | } | |
553 | EXPORT_SYMBOL(congestion_wait); | |
04fbfdc1 | 554 | |
0e093d99 MG |
555 | /** |
556 | * wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a zone to complete writes | |
557 | * @zone: A zone to check if it is heavily congested | |
558 | * @sync: SYNC or ASYNC IO | |
559 | * @timeout: timeout in jiffies | |
560 | * | |
561 | * In the event of a congested backing_dev (any backing_dev) and the given | |
562 | * @zone has experienced recent congestion, this waits for up to @timeout | |
563 | * jiffies for either a BDI to exit congestion of the given @sync queue | |
564 | * or a write to complete. | |
565 | * | |
25985edc | 566 | * In the absence of zone congestion, cond_resched() is called to yield |
0e093d99 MG |
567 | * the processor if necessary but otherwise does not sleep. |
568 | * | |
569 | * The return value is 0 if the sleep is for the full timeout. Otherwise, | |
570 | * it is the number of jiffies that were still remaining when the function | |
571 | * returned. return_value == timeout implies the function did not sleep. | |
572 | */ | |
573 | long wait_iff_congested(struct zone *zone, int sync, long timeout) | |
574 | { | |
575 | long ret; | |
576 | unsigned long start = jiffies; | |
577 | DEFINE_WAIT(wait); | |
578 | wait_queue_head_t *wqh = &congestion_wqh[sync]; | |
579 | ||
580 | /* | |
581 | * If there is no congestion, or heavy congestion is not being | |
582 | * encountered in the current zone, yield if necessary instead | |
583 | * of sleeping on the congestion queue | |
584 | */ | |
585 | if (atomic_read(&nr_bdi_congested[sync]) == 0 || | |
57054651 | 586 | !test_bit(ZONE_CONGESTED, &zone->flags)) { |
0e093d99 MG |
587 | cond_resched(); |
588 | ||
589 | /* In case we scheduled, work out time remaining */ | |
590 | ret = timeout - (jiffies - start); | |
591 | if (ret < 0) | |
592 | ret = 0; | |
593 | ||
594 | goto out; | |
595 | } | |
596 | ||
597 | /* Sleep until uncongested or a write happens */ | |
598 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
599 | ret = io_schedule_timeout(timeout); | |
600 | finish_wait(wqh, &wait); | |
601 | ||
602 | out: | |
603 | trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout), | |
604 | jiffies_to_usecs(jiffies - start)); | |
605 | ||
606 | return ret; | |
607 | } | |
608 | EXPORT_SYMBOL(wait_iff_congested); | |
3965c9ae WL |
609 | |
610 | int pdflush_proc_obsolete(struct ctl_table *table, int write, | |
611 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
612 | { | |
613 | char kbuf[] = "0\n"; | |
614 | ||
4c3bffc2 | 615 | if (*ppos || *lenp < sizeof(kbuf)) { |
3965c9ae WL |
616 | *lenp = 0; |
617 | return 0; | |
618 | } | |
619 | ||
620 | if (copy_to_user(buffer, kbuf, sizeof(kbuf))) | |
621 | return -EFAULT; | |
622 | printk_once(KERN_WARNING "%s exported in /proc is scheduled for removal\n", | |
623 | table->procname); | |
624 | ||
625 | *lenp = 2; | |
626 | *ppos += *lenp; | |
627 | return 2; | |
628 | } |