Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 1 | |
| 2 | #include <linux/wait.h> |
| 3 | #include <linux/backing-dev.h> |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/sched.h> |
| 6 | #include <linux/module.h> |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 7 | #include <linux/writeback.h> |
| 8 | #include <linux/device.h> |
| 9 | |
| 10 | |
| 11 | static struct class *bdi_class; |
| 12 | |
| 13 | static ssize_t read_ahead_kb_store(struct device *dev, |
| 14 | struct device_attribute *attr, |
| 15 | const char *buf, size_t count) |
| 16 | { |
| 17 | struct backing_dev_info *bdi = dev_get_drvdata(dev); |
| 18 | char *end; |
| 19 | unsigned long read_ahead_kb; |
| 20 | ssize_t ret = -EINVAL; |
| 21 | |
| 22 | read_ahead_kb = simple_strtoul(buf, &end, 10); |
| 23 | if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) { |
| 24 | bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10); |
| 25 | ret = count; |
| 26 | } |
| 27 | return ret; |
| 28 | } |
| 29 | |
| 30 | #define K(pages) ((pages) << (PAGE_SHIFT - 10)) |
| 31 | |
| 32 | #define BDI_SHOW(name, expr) \ |
| 33 | static ssize_t name##_show(struct device *dev, \ |
| 34 | struct device_attribute *attr, char *page) \ |
| 35 | { \ |
| 36 | struct backing_dev_info *bdi = dev_get_drvdata(dev); \ |
| 37 | \ |
| 38 | return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \ |
| 39 | } |
| 40 | |
| 41 | BDI_SHOW(read_ahead_kb, K(bdi->ra_pages)) |
| 42 | |
| 43 | BDI_SHOW(reclaimable_kb, K(bdi_stat(bdi, BDI_RECLAIMABLE))) |
| 44 | BDI_SHOW(writeback_kb, K(bdi_stat(bdi, BDI_WRITEBACK))) |
| 45 | |
| 46 | static inline unsigned long get_dirty(struct backing_dev_info *bdi, int i) |
| 47 | { |
| 48 | unsigned long thresh[3]; |
| 49 | |
| 50 | get_dirty_limits(&thresh[0], &thresh[1], &thresh[2], bdi); |
| 51 | |
| 52 | return thresh[i]; |
| 53 | } |
| 54 | |
| 55 | BDI_SHOW(dirty_kb, K(get_dirty(bdi, 1))) |
| 56 | BDI_SHOW(bdi_dirty_kb, K(get_dirty(bdi, 2))) |
| 57 | |
| 58 | #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store) |
| 59 | |
| 60 | static struct device_attribute bdi_dev_attrs[] = { |
| 61 | __ATTR_RW(read_ahead_kb), |
| 62 | __ATTR_RO(reclaimable_kb), |
| 63 | __ATTR_RO(writeback_kb), |
| 64 | __ATTR_RO(dirty_kb), |
| 65 | __ATTR_RO(bdi_dirty_kb), |
| 66 | __ATTR_NULL, |
| 67 | }; |
| 68 | |
| 69 | static __init int bdi_class_init(void) |
| 70 | { |
| 71 | bdi_class = class_create(THIS_MODULE, "bdi"); |
| 72 | bdi_class->dev_attrs = bdi_dev_attrs; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | core_initcall(bdi_class_init); |
| 77 | |
| 78 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
| 79 | const char *fmt, ...) |
| 80 | { |
| 81 | char *name; |
| 82 | va_list args; |
| 83 | int ret = 0; |
| 84 | struct device *dev; |
| 85 | |
| 86 | va_start(args, fmt); |
| 87 | name = kvasprintf(GFP_KERNEL, fmt, args); |
| 88 | va_end(args); |
| 89 | |
| 90 | if (!name) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | dev = device_create(bdi_class, parent, MKDEV(0, 0), name); |
| 94 | if (IS_ERR(dev)) { |
| 95 | ret = PTR_ERR(dev); |
| 96 | goto exit; |
| 97 | } |
| 98 | |
| 99 | bdi->dev = dev; |
| 100 | dev_set_drvdata(bdi->dev, bdi); |
| 101 | |
| 102 | exit: |
| 103 | kfree(name); |
| 104 | return ret; |
| 105 | } |
| 106 | EXPORT_SYMBOL(bdi_register); |
| 107 | |
| 108 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev) |
| 109 | { |
| 110 | return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev)); |
| 111 | } |
| 112 | EXPORT_SYMBOL(bdi_register_dev); |
| 113 | |
| 114 | void bdi_unregister(struct backing_dev_info *bdi) |
| 115 | { |
| 116 | if (bdi->dev) { |
| 117 | device_unregister(bdi->dev); |
| 118 | bdi->dev = NULL; |
| 119 | } |
| 120 | } |
| 121 | EXPORT_SYMBOL(bdi_unregister); |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 122 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 123 | int bdi_init(struct backing_dev_info *bdi) |
| 124 | { |
Denis Cheng | 4b01a0b | 2007-12-04 23:45:07 -0800 | [diff] [blame] | 125 | int i; |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 126 | int err; |
| 127 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 128 | bdi->dev = NULL; |
| 129 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 130 | for (i = 0; i < NR_BDI_STAT_ITEMS; i++) { |
| 131 | err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 132 | if (err) |
| 133 | goto err; |
| 134 | } |
| 135 | |
| 136 | bdi->dirty_exceeded = 0; |
| 137 | err = prop_local_init_percpu(&bdi->completions); |
| 138 | |
| 139 | if (err) { |
| 140 | err: |
Denis Cheng | 4b01a0b | 2007-12-04 23:45:07 -0800 | [diff] [blame] | 141 | while (i--) |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 142 | percpu_counter_destroy(&bdi->bdi_stat[i]); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return err; |
| 146 | } |
| 147 | EXPORT_SYMBOL(bdi_init); |
| 148 | |
| 149 | void bdi_destroy(struct backing_dev_info *bdi) |
| 150 | { |
| 151 | int i; |
| 152 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 153 | bdi_unregister(bdi); |
| 154 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 155 | for (i = 0; i < NR_BDI_STAT_ITEMS; i++) |
| 156 | percpu_counter_destroy(&bdi->bdi_stat[i]); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 157 | |
| 158 | prop_local_destroy_percpu(&bdi->completions); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 159 | } |
| 160 | EXPORT_SYMBOL(bdi_destroy); |
| 161 | |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 162 | static wait_queue_head_t congestion_wqh[2] = { |
| 163 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]), |
| 164 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1]) |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | void clear_bdi_congested(struct backing_dev_info *bdi, int rw) |
| 169 | { |
| 170 | enum bdi_state bit; |
| 171 | wait_queue_head_t *wqh = &congestion_wqh[rw]; |
| 172 | |
| 173 | bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested; |
| 174 | clear_bit(bit, &bdi->state); |
| 175 | smp_mb__after_clear_bit(); |
| 176 | if (waitqueue_active(wqh)) |
| 177 | wake_up(wqh); |
| 178 | } |
| 179 | EXPORT_SYMBOL(clear_bdi_congested); |
| 180 | |
| 181 | void set_bdi_congested(struct backing_dev_info *bdi, int rw) |
| 182 | { |
| 183 | enum bdi_state bit; |
| 184 | |
| 185 | bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested; |
| 186 | set_bit(bit, &bdi->state); |
| 187 | } |
| 188 | EXPORT_SYMBOL(set_bdi_congested); |
| 189 | |
| 190 | /** |
| 191 | * congestion_wait - wait for a backing_dev to become uncongested |
| 192 | * @rw: READ or WRITE |
| 193 | * @timeout: timeout in jiffies |
| 194 | * |
| 195 | * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit |
| 196 | * write congestion. If no backing_devs are congested then just wait for the |
| 197 | * next write to be completed. |
| 198 | */ |
| 199 | long congestion_wait(int rw, long timeout) |
| 200 | { |
| 201 | long ret; |
| 202 | DEFINE_WAIT(wait); |
| 203 | wait_queue_head_t *wqh = &congestion_wqh[rw]; |
| 204 | |
| 205 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); |
| 206 | ret = io_schedule_timeout(timeout); |
| 207 | finish_wait(wqh, &wait); |
| 208 | return ret; |
| 209 | } |
| 210 | EXPORT_SYMBOL(congestion_wait); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 211 | |