backing-dev.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include <linux/wait.h>
  2. #include <linux/backing-dev.h>
  3. #include <linux/fs.h>
  4. #include <linux/pagemap.h>
  5. #include <linux/sched.h>
  6. #include <linux/module.h>
  7. #include <linux/writeback.h>
  8. #include <linux/device.h>
  9. void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  10. {
  11. }
  12. EXPORT_SYMBOL(default_unplug_io_fn);
  13. struct backing_dev_info default_backing_dev_info = {
  14. .ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_CACHE_SIZE,
  15. .state = 0,
  16. .capabilities = BDI_CAP_MAP_COPY,
  17. .unplug_io_fn = default_unplug_io_fn,
  18. };
  19. EXPORT_SYMBOL_GPL(default_backing_dev_info);
  20. static struct class *bdi_class;
  21. #ifdef CONFIG_DEBUG_FS
  22. #include <linux/debugfs.h>
  23. #include <linux/seq_file.h>
  24. static struct dentry *bdi_debug_root;
  25. static void bdi_debug_init(void)
  26. {
  27. bdi_debug_root = debugfs_create_dir("bdi", NULL);
  28. }
  29. static int bdi_debug_stats_show(struct seq_file *m, void *v)
  30. {
  31. struct backing_dev_info *bdi = m->private;
  32. unsigned long background_thresh;
  33. unsigned long dirty_thresh;
  34. unsigned long bdi_thresh;
  35. get_dirty_limits(&background_thresh, &dirty_thresh, &bdi_thresh, bdi);
  36. #define K(x) ((x) << (PAGE_SHIFT - 10))
  37. seq_printf(m,
  38. "BdiWriteback: %8lu kB\n"
  39. "BdiReclaimable: %8lu kB\n"
  40. "BdiDirtyThresh: %8lu kB\n"
  41. "DirtyThresh: %8lu kB\n"
  42. "BackgroundThresh: %8lu kB\n",
  43. (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)),
  44. (unsigned long) K(bdi_stat(bdi, BDI_RECLAIMABLE)),
  45. K(bdi_thresh),
  46. K(dirty_thresh),
  47. K(background_thresh));
  48. #undef K
  49. return 0;
  50. }
  51. static int bdi_debug_stats_open(struct inode *inode, struct file *file)
  52. {
  53. return single_open(file, bdi_debug_stats_show, inode->i_private);
  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. static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
  62. {
  63. bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
  64. bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir,
  65. bdi, &bdi_debug_stats_fops);
  66. }
  67. static void bdi_debug_unregister(struct backing_dev_info *bdi)
  68. {
  69. debugfs_remove(bdi->debug_stats);
  70. debugfs_remove(bdi->debug_dir);
  71. }
  72. #else
  73. static inline void bdi_debug_init(void)
  74. {
  75. }
  76. static inline void bdi_debug_register(struct backing_dev_info *bdi,
  77. const char *name)
  78. {
  79. }
  80. static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
  81. {
  82. }
  83. #endif
  84. static ssize_t read_ahead_kb_store(struct device *dev,
  85. struct device_attribute *attr,
  86. const char *buf, size_t count)
  87. {
  88. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  89. char *end;
  90. unsigned long read_ahead_kb;
  91. ssize_t ret = -EINVAL;
  92. read_ahead_kb = simple_strtoul(buf, &end, 10);
  93. if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
  94. bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
  95. ret = count;
  96. }
  97. return ret;
  98. }
  99. #define K(pages) ((pages) << (PAGE_SHIFT - 10))
  100. #define BDI_SHOW(name, expr) \
  101. static ssize_t name##_show(struct device *dev, \
  102. struct device_attribute *attr, char *page) \
  103. { \
  104. struct backing_dev_info *bdi = dev_get_drvdata(dev); \
  105. \
  106. return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
  107. }
  108. BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
  109. static ssize_t min_ratio_store(struct device *dev,
  110. struct device_attribute *attr, const char *buf, size_t count)
  111. {
  112. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  113. char *end;
  114. unsigned int ratio;
  115. ssize_t ret = -EINVAL;
  116. ratio = simple_strtoul(buf, &end, 10);
  117. if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
  118. ret = bdi_set_min_ratio(bdi, ratio);
  119. if (!ret)
  120. ret = count;
  121. }
  122. return ret;
  123. }
  124. BDI_SHOW(min_ratio, bdi->min_ratio)
  125. static ssize_t max_ratio_store(struct device *dev,
  126. struct device_attribute *attr, const char *buf, size_t count)
  127. {
  128. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  129. char *end;
  130. unsigned int ratio;
  131. ssize_t ret = -EINVAL;
  132. ratio = simple_strtoul(buf, &end, 10);
  133. if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
  134. ret = bdi_set_max_ratio(bdi, ratio);
  135. if (!ret)
  136. ret = count;
  137. }
  138. return ret;
  139. }
  140. BDI_SHOW(max_ratio, bdi->max_ratio)
  141. #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
  142. static struct device_attribute bdi_dev_attrs[] = {
  143. __ATTR_RW(read_ahead_kb),
  144. __ATTR_RW(min_ratio),
  145. __ATTR_RW(max_ratio),
  146. __ATTR_NULL,
  147. };
  148. static __init int bdi_class_init(void)
  149. {
  150. bdi_class = class_create(THIS_MODULE, "bdi");
  151. bdi_class->dev_attrs = bdi_dev_attrs;
  152. bdi_debug_init();
  153. return 0;
  154. }
  155. postcore_initcall(bdi_class_init);
  156. static int __init default_bdi_init(void)
  157. {
  158. int err;
  159. err = bdi_init(&default_backing_dev_info);
  160. if (!err)
  161. bdi_register(&default_backing_dev_info, NULL, "default");
  162. return err;
  163. }
  164. subsys_initcall(default_bdi_init);
  165. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  166. const char *fmt, ...)
  167. {
  168. va_list args;
  169. int ret = 0;
  170. struct device *dev;
  171. if (bdi->dev) /* The driver needs to use separate queues per device */
  172. goto exit;
  173. va_start(args, fmt);
  174. dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
  175. va_end(args);
  176. if (IS_ERR(dev)) {
  177. ret = PTR_ERR(dev);
  178. goto exit;
  179. }
  180. bdi->dev = dev;
  181. bdi_debug_register(bdi, dev_name(dev));
  182. exit:
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(bdi_register);
  186. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
  187. {
  188. return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
  189. }
  190. EXPORT_SYMBOL(bdi_register_dev);
  191. void bdi_unregister(struct backing_dev_info *bdi)
  192. {
  193. if (bdi->dev) {
  194. bdi_debug_unregister(bdi);
  195. device_unregister(bdi->dev);
  196. bdi->dev = NULL;
  197. }
  198. }
  199. EXPORT_SYMBOL(bdi_unregister);
  200. int bdi_init(struct backing_dev_info *bdi)
  201. {
  202. int i;
  203. int err;
  204. bdi->dev = NULL;
  205. bdi->min_ratio = 0;
  206. bdi->max_ratio = 100;
  207. bdi->max_prop_frac = PROP_FRAC_BASE;
  208. for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
  209. err = percpu_counter_init(&bdi->bdi_stat[i], 0);
  210. if (err)
  211. goto err;
  212. }
  213. bdi->dirty_exceeded = 0;
  214. err = prop_local_init_percpu(&bdi->completions);
  215. if (err) {
  216. err:
  217. while (i--)
  218. percpu_counter_destroy(&bdi->bdi_stat[i]);
  219. }
  220. return err;
  221. }
  222. EXPORT_SYMBOL(bdi_init);
  223. void bdi_destroy(struct backing_dev_info *bdi)
  224. {
  225. int i;
  226. bdi_unregister(bdi);
  227. for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
  228. percpu_counter_destroy(&bdi->bdi_stat[i]);
  229. prop_local_destroy_percpu(&bdi->completions);
  230. }
  231. EXPORT_SYMBOL(bdi_destroy);
  232. static wait_queue_head_t congestion_wqh[2] = {
  233. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  234. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  235. };
  236. void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
  237. {
  238. enum bdi_state bit;
  239. wait_queue_head_t *wqh = &congestion_wqh[sync];
  240. bit = sync ? BDI_sync_congested : BDI_async_congested;
  241. clear_bit(bit, &bdi->state);
  242. smp_mb__after_clear_bit();
  243. if (waitqueue_active(wqh))
  244. wake_up(wqh);
  245. }
  246. EXPORT_SYMBOL(clear_bdi_congested);
  247. void set_bdi_congested(struct backing_dev_info *bdi, int sync)
  248. {
  249. enum bdi_state bit;
  250. bit = sync ? BDI_sync_congested : BDI_async_congested;
  251. set_bit(bit, &bdi->state);
  252. }
  253. EXPORT_SYMBOL(set_bdi_congested);
  254. /**
  255. * congestion_wait - wait for a backing_dev to become uncongested
  256. * @rw: READ or WRITE
  257. * @timeout: timeout in jiffies
  258. *
  259. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  260. * write congestion. If no backing_devs are congested then just wait for the
  261. * next write to be completed.
  262. */
  263. long congestion_wait(int rw, long timeout)
  264. {
  265. long ret;
  266. DEFINE_WAIT(wait);
  267. wait_queue_head_t *wqh = &congestion_wqh[rw];
  268. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  269. ret = io_schedule_timeout(timeout);
  270. finish_wait(wqh, &wait);
  271. return ret;
  272. }
  273. EXPORT_SYMBOL(congestion_wait);