backing-dev.c 6.9 KB

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