backing-dev.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. char *name;
  148. va_list args;
  149. int ret = 0;
  150. struct device *dev;
  151. va_start(args, fmt);
  152. name = kvasprintf(GFP_KERNEL, fmt, args);
  153. va_end(args);
  154. if (!name)
  155. return -ENOMEM;
  156. dev = device_create(bdi_class, parent, MKDEV(0, 0), name);
  157. if (IS_ERR(dev)) {
  158. ret = PTR_ERR(dev);
  159. goto exit;
  160. }
  161. bdi->dev = dev;
  162. dev_set_drvdata(bdi->dev, bdi);
  163. bdi_debug_register(bdi, name);
  164. exit:
  165. kfree(name);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(bdi_register);
  169. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
  170. {
  171. return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
  172. }
  173. EXPORT_SYMBOL(bdi_register_dev);
  174. void bdi_unregister(struct backing_dev_info *bdi)
  175. {
  176. if (bdi->dev) {
  177. bdi_debug_unregister(bdi);
  178. device_unregister(bdi->dev);
  179. bdi->dev = NULL;
  180. }
  181. }
  182. EXPORT_SYMBOL(bdi_unregister);
  183. int bdi_init(struct backing_dev_info *bdi)
  184. {
  185. int i;
  186. int err;
  187. bdi->dev = NULL;
  188. bdi->min_ratio = 0;
  189. bdi->max_ratio = 100;
  190. bdi->max_prop_frac = PROP_FRAC_BASE;
  191. for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
  192. err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0);
  193. if (err)
  194. goto err;
  195. }
  196. bdi->dirty_exceeded = 0;
  197. err = prop_local_init_percpu(&bdi->completions);
  198. if (err) {
  199. err:
  200. while (i--)
  201. percpu_counter_destroy(&bdi->bdi_stat[i]);
  202. }
  203. return err;
  204. }
  205. EXPORT_SYMBOL(bdi_init);
  206. void bdi_destroy(struct backing_dev_info *bdi)
  207. {
  208. int i;
  209. bdi_unregister(bdi);
  210. for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
  211. percpu_counter_destroy(&bdi->bdi_stat[i]);
  212. prop_local_destroy_percpu(&bdi->completions);
  213. }
  214. EXPORT_SYMBOL(bdi_destroy);
  215. static wait_queue_head_t congestion_wqh[2] = {
  216. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  217. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  218. };
  219. void clear_bdi_congested(struct backing_dev_info *bdi, int rw)
  220. {
  221. enum bdi_state bit;
  222. wait_queue_head_t *wqh = &congestion_wqh[rw];
  223. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  224. clear_bit(bit, &bdi->state);
  225. smp_mb__after_clear_bit();
  226. if (waitqueue_active(wqh))
  227. wake_up(wqh);
  228. }
  229. EXPORT_SYMBOL(clear_bdi_congested);
  230. void set_bdi_congested(struct backing_dev_info *bdi, int rw)
  231. {
  232. enum bdi_state bit;
  233. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  234. set_bit(bit, &bdi->state);
  235. }
  236. EXPORT_SYMBOL(set_bdi_congested);
  237. /**
  238. * congestion_wait - wait for a backing_dev to become uncongested
  239. * @rw: READ or WRITE
  240. * @timeout: timeout in jiffies
  241. *
  242. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  243. * write congestion. If no backing_devs are congested then just wait for the
  244. * next write to be completed.
  245. */
  246. long congestion_wait(int rw, long timeout)
  247. {
  248. long ret;
  249. DEFINE_WAIT(wait);
  250. wait_queue_head_t *wqh = &congestion_wqh[rw];
  251. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  252. ret = io_schedule_timeout(timeout);
  253. finish_wait(wqh, &wait);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL(congestion_wait);