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 | |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 13 | #ifdef CONFIG_DEBUG_FS |
| 14 | #include <linux/debugfs.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | |
| 17 | static struct dentry *bdi_debug_root; |
| 18 | |
| 19 | static void bdi_debug_init(void) |
| 20 | { |
| 21 | bdi_debug_root = debugfs_create_dir("bdi", NULL); |
| 22 | } |
| 23 | |
| 24 | static int bdi_debug_stats_show(struct seq_file *m, void *v) |
| 25 | { |
| 26 | struct backing_dev_info *bdi = m->private; |
| 27 | long background_thresh; |
| 28 | long dirty_thresh; |
| 29 | long bdi_thresh; |
| 30 | |
| 31 | get_dirty_limits(&background_thresh, &dirty_thresh, &bdi_thresh, bdi); |
| 32 | |
| 33 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
| 34 | seq_printf(m, |
| 35 | "BdiWriteback: %8lu kB\n" |
| 36 | "BdiReclaimable: %8lu kB\n" |
| 37 | "BdiDirtyThresh: %8lu kB\n" |
| 38 | "DirtyThresh: %8lu kB\n" |
| 39 | "BackgroundThresh: %8lu kB\n", |
| 40 | (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)), |
| 41 | (unsigned long) K(bdi_stat(bdi, BDI_RECLAIMABLE)), |
| 42 | K(bdi_thresh), |
| 43 | K(dirty_thresh), |
| 44 | K(background_thresh)); |
| 45 | #undef K |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int bdi_debug_stats_open(struct inode *inode, struct file *file) |
| 51 | { |
| 52 | return single_open(file, bdi_debug_stats_show, inode->i_private); |
| 53 | } |
| 54 | |
| 55 | static const struct file_operations bdi_debug_stats_fops = { |
| 56 | .open = bdi_debug_stats_open, |
| 57 | .read = seq_read, |
| 58 | .llseek = seq_lseek, |
| 59 | .release = single_release, |
| 60 | }; |
| 61 | |
| 62 | static void bdi_debug_register(struct backing_dev_info *bdi, const char *name) |
| 63 | { |
| 64 | bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root); |
| 65 | bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir, |
| 66 | bdi, &bdi_debug_stats_fops); |
| 67 | } |
| 68 | |
| 69 | static void bdi_debug_unregister(struct backing_dev_info *bdi) |
| 70 | { |
| 71 | debugfs_remove(bdi->debug_stats); |
| 72 | debugfs_remove(bdi->debug_dir); |
| 73 | } |
| 74 | #else |
| 75 | static inline void bdi_debug_init(void) |
| 76 | { |
| 77 | } |
| 78 | static inline void bdi_debug_register(struct backing_dev_info *bdi, |
| 79 | const char *name) |
| 80 | { |
| 81 | } |
| 82 | static inline void bdi_debug_unregister(struct backing_dev_info *bdi) |
| 83 | { |
| 84 | } |
| 85 | #endif |
| 86 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 87 | static ssize_t read_ahead_kb_store(struct device *dev, |
| 88 | struct device_attribute *attr, |
| 89 | const char *buf, size_t count) |
| 90 | { |
| 91 | struct backing_dev_info *bdi = dev_get_drvdata(dev); |
| 92 | char *end; |
| 93 | unsigned long read_ahead_kb; |
| 94 | ssize_t ret = -EINVAL; |
| 95 | |
| 96 | read_ahead_kb = simple_strtoul(buf, &end, 10); |
| 97 | if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) { |
| 98 | bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10); |
| 99 | ret = count; |
| 100 | } |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | #define K(pages) ((pages) << (PAGE_SHIFT - 10)) |
| 105 | |
| 106 | #define BDI_SHOW(name, expr) \ |
| 107 | static ssize_t name##_show(struct device *dev, \ |
| 108 | struct device_attribute *attr, char *page) \ |
| 109 | { \ |
| 110 | struct backing_dev_info *bdi = dev_get_drvdata(dev); \ |
| 111 | \ |
| 112 | return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \ |
| 113 | } |
| 114 | |
| 115 | BDI_SHOW(read_ahead_kb, K(bdi->ra_pages)) |
| 116 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 117 | static ssize_t min_ratio_store(struct device *dev, |
| 118 | struct device_attribute *attr, const char *buf, size_t count) |
| 119 | { |
| 120 | struct backing_dev_info *bdi = dev_get_drvdata(dev); |
| 121 | char *end; |
| 122 | unsigned int ratio; |
| 123 | ssize_t ret = -EINVAL; |
| 124 | |
| 125 | ratio = simple_strtoul(buf, &end, 10); |
| 126 | if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) { |
| 127 | ret = bdi_set_min_ratio(bdi, ratio); |
| 128 | if (!ret) |
| 129 | ret = count; |
| 130 | } |
| 131 | return ret; |
| 132 | } |
| 133 | BDI_SHOW(min_ratio, bdi->min_ratio) |
| 134 | |
Peter Zijlstra | a42dde0 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 135 | static ssize_t max_ratio_store(struct device *dev, |
| 136 | struct device_attribute *attr, const char *buf, size_t count) |
| 137 | { |
| 138 | struct backing_dev_info *bdi = dev_get_drvdata(dev); |
| 139 | char *end; |
| 140 | unsigned int ratio; |
| 141 | ssize_t ret = -EINVAL; |
| 142 | |
| 143 | ratio = simple_strtoul(buf, &end, 10); |
| 144 | if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) { |
| 145 | ret = bdi_set_max_ratio(bdi, ratio); |
| 146 | if (!ret) |
| 147 | ret = count; |
| 148 | } |
| 149 | return ret; |
| 150 | } |
| 151 | BDI_SHOW(max_ratio, bdi->max_ratio) |
| 152 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 153 | #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store) |
| 154 | |
| 155 | static struct device_attribute bdi_dev_attrs[] = { |
| 156 | __ATTR_RW(read_ahead_kb), |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 157 | __ATTR_RW(min_ratio), |
Peter Zijlstra | a42dde0 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 158 | __ATTR_RW(max_ratio), |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 159 | __ATTR_NULL, |
| 160 | }; |
| 161 | |
| 162 | static __init int bdi_class_init(void) |
| 163 | { |
| 164 | bdi_class = class_create(THIS_MODULE, "bdi"); |
| 165 | bdi_class->dev_attrs = bdi_dev_attrs; |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 166 | bdi_debug_init(); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 170 | postcore_initcall(bdi_class_init); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 171 | |
| 172 | int bdi_register(struct backing_dev_info *bdi, struct device *parent, |
| 173 | const char *fmt, ...) |
| 174 | { |
| 175 | char *name; |
| 176 | va_list args; |
| 177 | int ret = 0; |
| 178 | struct device *dev; |
| 179 | |
| 180 | va_start(args, fmt); |
| 181 | name = kvasprintf(GFP_KERNEL, fmt, args); |
| 182 | va_end(args); |
| 183 | |
| 184 | if (!name) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | dev = device_create(bdi_class, parent, MKDEV(0, 0), name); |
| 188 | if (IS_ERR(dev)) { |
| 189 | ret = PTR_ERR(dev); |
| 190 | goto exit; |
| 191 | } |
| 192 | |
| 193 | bdi->dev = dev; |
| 194 | dev_set_drvdata(bdi->dev, bdi); |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 195 | bdi_debug_register(bdi, name); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 196 | |
| 197 | exit: |
| 198 | kfree(name); |
| 199 | return ret; |
| 200 | } |
| 201 | EXPORT_SYMBOL(bdi_register); |
| 202 | |
| 203 | int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev) |
| 204 | { |
| 205 | return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev)); |
| 206 | } |
| 207 | EXPORT_SYMBOL(bdi_register_dev); |
| 208 | |
| 209 | void bdi_unregister(struct backing_dev_info *bdi) |
| 210 | { |
| 211 | if (bdi->dev) { |
Miklos Szeredi | 76f1418 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 212 | bdi_debug_unregister(bdi); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 213 | device_unregister(bdi->dev); |
| 214 | bdi->dev = NULL; |
| 215 | } |
| 216 | } |
| 217 | EXPORT_SYMBOL(bdi_unregister); |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 218 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 219 | int bdi_init(struct backing_dev_info *bdi) |
| 220 | { |
Denis Cheng | 4b01a0b | 2007-12-04 23:45:07 -0800 | [diff] [blame] | 221 | int i; |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 222 | int err; |
| 223 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 224 | bdi->dev = NULL; |
| 225 | |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 226 | bdi->min_ratio = 0; |
Peter Zijlstra | a42dde0 | 2008-04-30 00:54:36 -0700 | [diff] [blame] | 227 | bdi->max_ratio = 100; |
| 228 | bdi->max_prop_frac = PROP_FRAC_BASE; |
Peter Zijlstra | 189d3c4 | 2008-04-30 00:54:35 -0700 | [diff] [blame] | 229 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 230 | for (i = 0; i < NR_BDI_STAT_ITEMS; i++) { |
| 231 | err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 232 | if (err) |
| 233 | goto err; |
| 234 | } |
| 235 | |
| 236 | bdi->dirty_exceeded = 0; |
| 237 | err = prop_local_init_percpu(&bdi->completions); |
| 238 | |
| 239 | if (err) { |
| 240 | err: |
Denis Cheng | 4b01a0b | 2007-12-04 23:45:07 -0800 | [diff] [blame] | 241 | while (i--) |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 242 | percpu_counter_destroy(&bdi->bdi_stat[i]); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return err; |
| 246 | } |
| 247 | EXPORT_SYMBOL(bdi_init); |
| 248 | |
| 249 | void bdi_destroy(struct backing_dev_info *bdi) |
| 250 | { |
| 251 | int i; |
| 252 | |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 253 | bdi_unregister(bdi); |
| 254 | |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 255 | for (i = 0; i < NR_BDI_STAT_ITEMS; i++) |
| 256 | percpu_counter_destroy(&bdi->bdi_stat[i]); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 257 | |
| 258 | prop_local_destroy_percpu(&bdi->completions); |
Peter Zijlstra | b2e8fb6 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL(bdi_destroy); |
| 261 | |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 262 | static wait_queue_head_t congestion_wqh[2] = { |
| 263 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]), |
| 264 | __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1]) |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | void clear_bdi_congested(struct backing_dev_info *bdi, int rw) |
| 269 | { |
| 270 | enum bdi_state bit; |
| 271 | wait_queue_head_t *wqh = &congestion_wqh[rw]; |
| 272 | |
| 273 | bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested; |
| 274 | clear_bit(bit, &bdi->state); |
| 275 | smp_mb__after_clear_bit(); |
| 276 | if (waitqueue_active(wqh)) |
| 277 | wake_up(wqh); |
| 278 | } |
| 279 | EXPORT_SYMBOL(clear_bdi_congested); |
| 280 | |
| 281 | void set_bdi_congested(struct backing_dev_info *bdi, int rw) |
| 282 | { |
| 283 | enum bdi_state bit; |
| 284 | |
| 285 | bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested; |
| 286 | set_bit(bit, &bdi->state); |
| 287 | } |
| 288 | EXPORT_SYMBOL(set_bdi_congested); |
| 289 | |
| 290 | /** |
| 291 | * congestion_wait - wait for a backing_dev to become uncongested |
| 292 | * @rw: READ or WRITE |
| 293 | * @timeout: timeout in jiffies |
| 294 | * |
| 295 | * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit |
| 296 | * write congestion. If no backing_devs are congested then just wait for the |
| 297 | * next write to be completed. |
| 298 | */ |
| 299 | long congestion_wait(int rw, long timeout) |
| 300 | { |
| 301 | long ret; |
| 302 | DEFINE_WAIT(wait); |
| 303 | wait_queue_head_t *wqh = &congestion_wqh[rw]; |
| 304 | |
| 305 | prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE); |
| 306 | ret = io_schedule_timeout(timeout); |
| 307 | finish_wait(wqh, &wait); |
| 308 | return ret; |
| 309 | } |
| 310 | EXPORT_SYMBOL(congestion_wait); |
Peter Zijlstra | 04fbfdc | 2007-10-16 23:25:50 -0700 | [diff] [blame] | 311 | |