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