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 | ||
f0054bb1 | 294 | static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi) |
6467716a | 295 | { |
93f78d88 TH |
296 | int i, err; |
297 | ||
6467716a AB |
298 | memset(wb, 0, sizeof(*wb)); |
299 | ||
300 | wb->bdi = bdi; | |
301 | wb->last_old_flush = jiffies; | |
302 | INIT_LIST_HEAD(&wb->b_dirty); | |
303 | INIT_LIST_HEAD(&wb->b_io); | |
304 | INIT_LIST_HEAD(&wb->b_more_io); | |
0ae45f63 | 305 | INIT_LIST_HEAD(&wb->b_dirty_time); |
f758eeab | 306 | spin_lock_init(&wb->list_lock); |
93f78d88 | 307 | |
a88a341a TH |
308 | wb->bw_time_stamp = jiffies; |
309 | wb->balanced_dirty_ratelimit = INIT_BW; | |
310 | wb->dirty_ratelimit = INIT_BW; | |
311 | wb->write_bandwidth = INIT_BW; | |
312 | wb->avg_write_bandwidth = INIT_BW; | |
313 | ||
f0054bb1 TH |
314 | spin_lock_init(&wb->work_lock); |
315 | INIT_LIST_HEAD(&wb->work_list); | |
316 | INIT_DELAYED_WORK(&wb->dwork, wb_workfn); | |
317 | ||
a88a341a TH |
318 | err = fprop_local_init_percpu(&wb->completions, GFP_KERNEL); |
319 | if (err) | |
320 | return err; | |
321 | ||
93f78d88 TH |
322 | for (i = 0; i < NR_WB_STAT_ITEMS; i++) { |
323 | err = percpu_counter_init(&wb->stat[i], 0, GFP_KERNEL); | |
324 | if (err) { | |
325 | while (--i) | |
326 | percpu_counter_destroy(&wb->stat[i]); | |
a88a341a | 327 | fprop_local_destroy_percpu(&wb->completions); |
93f78d88 TH |
328 | return err; |
329 | } | |
330 | } | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
46100071 TH |
335 | /* |
336 | * Remove bdi from the global list and shutdown any threads we have running | |
337 | */ | |
338 | static void wb_shutdown(struct bdi_writeback *wb) | |
339 | { | |
340 | /* Make sure nobody queues further work */ | |
341 | spin_lock_bh(&wb->work_lock); | |
342 | if (!test_and_clear_bit(WB_registered, &wb->state)) { | |
343 | spin_unlock_bh(&wb->work_lock); | |
344 | return; | |
345 | } | |
346 | spin_unlock_bh(&wb->work_lock); | |
347 | ||
348 | /* | |
349 | * Drain work list and shutdown the delayed_work. !WB_registered | |
350 | * tells wb_workfn() that @wb is dying and its work_list needs to | |
351 | * be drained no matter what. | |
352 | */ | |
353 | mod_delayed_work(bdi_wq, &wb->dwork, 0); | |
354 | flush_delayed_work(&wb->dwork); | |
355 | WARN_ON(!list_empty(&wb->work_list)); | |
356 | } | |
357 | ||
f0054bb1 | 358 | static void wb_exit(struct bdi_writeback *wb) |
93f78d88 TH |
359 | { |
360 | int i; | |
361 | ||
362 | WARN_ON(delayed_work_pending(&wb->dwork)); | |
363 | ||
364 | for (i = 0; i < NR_WB_STAT_ITEMS; i++) | |
365 | percpu_counter_destroy(&wb->stat[i]); | |
6467716a | 366 | |
a88a341a TH |
367 | fprop_local_destroy_percpu(&wb->completions); |
368 | } | |
e98be2d5 | 369 | |
b2e8fb6e PZ |
370 | int bdi_init(struct backing_dev_info *bdi) |
371 | { | |
93f78d88 | 372 | int err; |
b2e8fb6e | 373 | |
cf0ca9fe PZ |
374 | bdi->dev = NULL; |
375 | ||
189d3c4a | 376 | bdi->min_ratio = 0; |
a42dde04 | 377 | bdi->max_ratio = 100; |
eb608e3a | 378 | bdi->max_prop_frac = FPROP_FRAC_BASE; |
66f3b8e2 | 379 | INIT_LIST_HEAD(&bdi->bdi_list); |
03ba3782 | 380 | |
f0054bb1 | 381 | err = wb_init(&bdi->wb, bdi); |
93f78d88 TH |
382 | if (err) |
383 | return err; | |
04fbfdc1 | 384 | |
93f78d88 | 385 | return 0; |
b2e8fb6e PZ |
386 | } |
387 | EXPORT_SYMBOL(bdi_init); | |
388 | ||
46100071 TH |
389 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
390 | const char *fmt, ...) | |
391 | { | |
392 | va_list args; | |
393 | struct device *dev; | |
394 | ||
395 | if (bdi->dev) /* The driver needs to use separate queues per device */ | |
396 | return 0; | |
397 | ||
398 | va_start(args, fmt); | |
399 | dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args); | |
400 | va_end(args); | |
401 | if (IS_ERR(dev)) | |
402 | return PTR_ERR(dev); | |
403 | ||
404 | bdi->dev = dev; | |
405 | ||
406 | bdi_debug_register(bdi, dev_name(dev)); | |
407 | set_bit(WB_registered, &bdi->wb.state); | |
408 | ||
409 | spin_lock_bh(&bdi_lock); | |
410 | list_add_tail_rcu(&bdi->bdi_list, &bdi_list); | |
411 | spin_unlock_bh(&bdi_lock); | |
412 | ||
413 | trace_writeback_bdi_register(bdi); | |
414 | return 0; | |
415 | } | |
416 | EXPORT_SYMBOL(bdi_register); | |
417 | ||
418 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev) | |
419 | { | |
420 | return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev)); | |
421 | } | |
422 | EXPORT_SYMBOL(bdi_register_dev); | |
423 | ||
424 | /* | |
425 | * Remove bdi from bdi_list, and ensure that it is no longer visible | |
426 | */ | |
427 | static void bdi_remove_from_list(struct backing_dev_info *bdi) | |
428 | { | |
429 | spin_lock_bh(&bdi_lock); | |
430 | list_del_rcu(&bdi->bdi_list); | |
431 | spin_unlock_bh(&bdi_lock); | |
432 | ||
433 | synchronize_rcu_expedited(); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Called when the device behind @bdi has been removed or ejected. | |
438 | * | |
439 | * We can't really do much here except for reducing the dirty ratio at | |
440 | * the moment. In the future we should be able to set a flag so that | |
441 | * the filesystem can handle errors at mark_inode_dirty time instead | |
442 | * of only at writeback time. | |
443 | */ | |
444 | void bdi_unregister(struct backing_dev_info *bdi) | |
445 | { | |
446 | if (WARN_ON_ONCE(!bdi->dev)) | |
447 | return; | |
448 | ||
449 | bdi_set_min_ratio(bdi, 0); | |
450 | } | |
451 | EXPORT_SYMBOL(bdi_unregister); | |
452 | ||
b2e8fb6e PZ |
453 | void bdi_destroy(struct backing_dev_info *bdi) |
454 | { | |
f0054bb1 TH |
455 | /* make sure nobody finds us on the bdi_list anymore */ |
456 | bdi_remove_from_list(bdi); | |
457 | wb_shutdown(&bdi->wb); | |
7a401a97 | 458 | |
c4db59d3 CH |
459 | if (bdi->dev) { |
460 | bdi_debug_unregister(bdi); | |
461 | device_unregister(bdi->dev); | |
462 | bdi->dev = NULL; | |
463 | } | |
464 | ||
f0054bb1 | 465 | wb_exit(&bdi->wb); |
b2e8fb6e PZ |
466 | } |
467 | EXPORT_SYMBOL(bdi_destroy); | |
468 | ||
c3c53206 JA |
469 | /* |
470 | * For use from filesystems to quickly init and register a bdi associated | |
471 | * with dirty writeback | |
472 | */ | |
b4caecd4 | 473 | int bdi_setup_and_register(struct backing_dev_info *bdi, char *name) |
c3c53206 | 474 | { |
c3c53206 JA |
475 | int err; |
476 | ||
477 | bdi->name = name; | |
b4caecd4 | 478 | bdi->capabilities = 0; |
c3c53206 JA |
479 | err = bdi_init(bdi); |
480 | if (err) | |
481 | return err; | |
482 | ||
02aa2a37 KC |
483 | err = bdi_register(bdi, NULL, "%.28s-%ld", name, |
484 | atomic_long_inc_return(&bdi_seq)); | |
c3c53206 JA |
485 | if (err) { |
486 | bdi_destroy(bdi); | |
487 | return err; | |
488 | } | |
489 | ||
490 | return 0; | |
491 | } | |
492 | EXPORT_SYMBOL(bdi_setup_and_register); | |
493 | ||
3fcfab16 AM |
494 | static wait_queue_head_t congestion_wqh[2] = { |
495 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]), | |
496 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1]) | |
497 | }; | |
0e093d99 | 498 | static atomic_t nr_bdi_congested[2]; |
3fcfab16 | 499 | |
1faa16d2 | 500 | void clear_bdi_congested(struct backing_dev_info *bdi, int sync) |
3fcfab16 | 501 | { |
4452226e | 502 | enum wb_state bit; |
1faa16d2 | 503 | wait_queue_head_t *wqh = &congestion_wqh[sync]; |
3fcfab16 | 504 | |
4452226e TH |
505 | bit = sync ? WB_sync_congested : WB_async_congested; |
506 | if (test_and_clear_bit(bit, &bdi->wb.state)) | |
0e093d99 | 507 | atomic_dec(&nr_bdi_congested[sync]); |
4e857c58 | 508 | smp_mb__after_atomic(); |
3fcfab16 AM |
509 | if (waitqueue_active(wqh)) |
510 | wake_up(wqh); | |
511 | } | |
512 | EXPORT_SYMBOL(clear_bdi_congested); | |
513 | ||
1faa16d2 | 514 | void set_bdi_congested(struct backing_dev_info *bdi, int sync) |
3fcfab16 | 515 | { |
4452226e | 516 | enum wb_state bit; |
3fcfab16 | 517 | |
4452226e TH |
518 | bit = sync ? WB_sync_congested : WB_async_congested; |
519 | if (!test_and_set_bit(bit, &bdi->wb.state)) | |
0e093d99 | 520 | atomic_inc(&nr_bdi_congested[sync]); |
3fcfab16 AM |
521 | } |
522 | EXPORT_SYMBOL(set_bdi_congested); | |
523 | ||
524 | /** | |
525 | * congestion_wait - wait for a backing_dev to become uncongested | |
8aa7e847 | 526 | * @sync: SYNC or ASYNC IO |
3fcfab16 AM |
527 | * @timeout: timeout in jiffies |
528 | * | |
529 | * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit | |
530 | * write congestion. If no backing_devs are congested then just wait for the | |
531 | * next write to be completed. | |
532 | */ | |
8aa7e847 | 533 | long congestion_wait(int sync, long timeout) |
3fcfab16 AM |
534 | { |
535 | long ret; | |
52bb9198 | 536 | unsigned long start = jiffies; |
3fcfab16 | 537 | DEFINE_WAIT(wait); |
8aa7e847 | 538 | wait_queue_head_t *wqh = &congestion_wqh[sync]; |
3fcfab16 AM |
539 | |
540 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
541 | ret = io_schedule_timeout(timeout); | |
542 | finish_wait(wqh, &wait); | |
52bb9198 MG |
543 | |
544 | trace_writeback_congestion_wait(jiffies_to_usecs(timeout), | |
545 | jiffies_to_usecs(jiffies - start)); | |
546 | ||
3fcfab16 AM |
547 | return ret; |
548 | } | |
549 | EXPORT_SYMBOL(congestion_wait); | |
04fbfdc1 | 550 | |
0e093d99 MG |
551 | /** |
552 | * wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a zone to complete writes | |
553 | * @zone: A zone to check if it is heavily congested | |
554 | * @sync: SYNC or ASYNC IO | |
555 | * @timeout: timeout in jiffies | |
556 | * | |
557 | * In the event of a congested backing_dev (any backing_dev) and the given | |
558 | * @zone has experienced recent congestion, this waits for up to @timeout | |
559 | * jiffies for either a BDI to exit congestion of the given @sync queue | |
560 | * or a write to complete. | |
561 | * | |
25985edc | 562 | * In the absence of zone congestion, cond_resched() is called to yield |
0e093d99 MG |
563 | * the processor if necessary but otherwise does not sleep. |
564 | * | |
565 | * The return value is 0 if the sleep is for the full timeout. Otherwise, | |
566 | * it is the number of jiffies that were still remaining when the function | |
567 | * returned. return_value == timeout implies the function did not sleep. | |
568 | */ | |
569 | long wait_iff_congested(struct zone *zone, int sync, long timeout) | |
570 | { | |
571 | long ret; | |
572 | unsigned long start = jiffies; | |
573 | DEFINE_WAIT(wait); | |
574 | wait_queue_head_t *wqh = &congestion_wqh[sync]; | |
575 | ||
576 | /* | |
577 | * If there is no congestion, or heavy congestion is not being | |
578 | * encountered in the current zone, yield if necessary instead | |
579 | * of sleeping on the congestion queue | |
580 | */ | |
581 | if (atomic_read(&nr_bdi_congested[sync]) == 0 || | |
57054651 | 582 | !test_bit(ZONE_CONGESTED, &zone->flags)) { |
0e093d99 MG |
583 | cond_resched(); |
584 | ||
585 | /* In case we scheduled, work out time remaining */ | |
586 | ret = timeout - (jiffies - start); | |
587 | if (ret < 0) | |
588 | ret = 0; | |
589 | ||
590 | goto out; | |
591 | } | |
592 | ||
593 | /* Sleep until uncongested or a write happens */ | |
594 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); | |
595 | ret = io_schedule_timeout(timeout); | |
596 | finish_wait(wqh, &wait); | |
597 | ||
598 | out: | |
599 | trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout), | |
600 | jiffies_to_usecs(jiffies - start)); | |
601 | ||
602 | return ret; | |
603 | } | |
604 | EXPORT_SYMBOL(wait_iff_congested); | |
3965c9ae WL |
605 | |
606 | int pdflush_proc_obsolete(struct ctl_table *table, int write, | |
607 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
608 | { | |
609 | char kbuf[] = "0\n"; | |
610 | ||
4c3bffc2 | 611 | if (*ppos || *lenp < sizeof(kbuf)) { |
3965c9ae WL |
612 | *lenp = 0; |
613 | return 0; | |
614 | } | |
615 | ||
616 | if (copy_to_user(buffer, kbuf, sizeof(kbuf))) | |
617 | return -EFAULT; | |
618 | printk_once(KERN_WARNING "%s exported in /proc is scheduled for removal\n", | |
619 | table->procname); | |
620 | ||
621 | *lenp = 2; | |
622 | *ppos += *lenp; | |
623 | return 2; | |
624 | } |