backing-dev.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. static ssize_t read_ahead_kb_store(struct device *dev,
  10. struct device_attribute *attr,
  11. const char *buf, size_t count)
  12. {
  13. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  14. char *end;
  15. unsigned long read_ahead_kb;
  16. ssize_t ret = -EINVAL;
  17. read_ahead_kb = simple_strtoul(buf, &end, 10);
  18. if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
  19. bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
  20. ret = count;
  21. }
  22. return ret;
  23. }
  24. #define K(pages) ((pages) << (PAGE_SHIFT - 10))
  25. #define BDI_SHOW(name, expr) \
  26. static ssize_t name##_show(struct device *dev, \
  27. struct device_attribute *attr, char *page) \
  28. { \
  29. struct backing_dev_info *bdi = dev_get_drvdata(dev); \
  30. \
  31. return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
  32. }
  33. BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
  34. BDI_SHOW(reclaimable_kb, K(bdi_stat(bdi, BDI_RECLAIMABLE)))
  35. BDI_SHOW(writeback_kb, K(bdi_stat(bdi, BDI_WRITEBACK)))
  36. static inline unsigned long get_dirty(struct backing_dev_info *bdi, int i)
  37. {
  38. unsigned long thresh[3];
  39. get_dirty_limits(&thresh[0], &thresh[1], &thresh[2], bdi);
  40. return thresh[i];
  41. }
  42. BDI_SHOW(dirty_kb, K(get_dirty(bdi, 1)))
  43. BDI_SHOW(bdi_dirty_kb, K(get_dirty(bdi, 2)))
  44. static ssize_t min_ratio_store(struct device *dev,
  45. struct device_attribute *attr, const char *buf, size_t count)
  46. {
  47. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  48. char *end;
  49. unsigned int ratio;
  50. ssize_t ret = -EINVAL;
  51. ratio = simple_strtoul(buf, &end, 10);
  52. if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
  53. ret = bdi_set_min_ratio(bdi, ratio);
  54. if (!ret)
  55. ret = count;
  56. }
  57. return ret;
  58. }
  59. BDI_SHOW(min_ratio, bdi->min_ratio)
  60. #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
  61. static struct device_attribute bdi_dev_attrs[] = {
  62. __ATTR_RW(read_ahead_kb),
  63. __ATTR_RO(reclaimable_kb),
  64. __ATTR_RO(writeback_kb),
  65. __ATTR_RO(dirty_kb),
  66. __ATTR_RO(bdi_dirty_kb),
  67. __ATTR_RW(min_ratio),
  68. __ATTR_NULL,
  69. };
  70. static __init int bdi_class_init(void)
  71. {
  72. bdi_class = class_create(THIS_MODULE, "bdi");
  73. bdi_class->dev_attrs = bdi_dev_attrs;
  74. return 0;
  75. }
  76. core_initcall(bdi_class_init);
  77. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  78. const char *fmt, ...)
  79. {
  80. char *name;
  81. va_list args;
  82. int ret = 0;
  83. struct device *dev;
  84. va_start(args, fmt);
  85. name = kvasprintf(GFP_KERNEL, fmt, args);
  86. va_end(args);
  87. if (!name)
  88. return -ENOMEM;
  89. dev = device_create(bdi_class, parent, MKDEV(0, 0), name);
  90. if (IS_ERR(dev)) {
  91. ret = PTR_ERR(dev);
  92. goto exit;
  93. }
  94. bdi->dev = dev;
  95. dev_set_drvdata(bdi->dev, bdi);
  96. exit:
  97. kfree(name);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL(bdi_register);
  101. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
  102. {
  103. return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
  104. }
  105. EXPORT_SYMBOL(bdi_register_dev);
  106. void bdi_unregister(struct backing_dev_info *bdi)
  107. {
  108. if (bdi->dev) {
  109. device_unregister(bdi->dev);
  110. bdi->dev = NULL;
  111. }
  112. }
  113. EXPORT_SYMBOL(bdi_unregister);
  114. int bdi_init(struct backing_dev_info *bdi)
  115. {
  116. int i;
  117. int err;
  118. bdi->dev = NULL;
  119. bdi->min_ratio = 0;
  120. for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
  121. err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0);
  122. if (err)
  123. goto err;
  124. }
  125. bdi->dirty_exceeded = 0;
  126. err = prop_local_init_percpu(&bdi->completions);
  127. if (err) {
  128. err:
  129. while (i--)
  130. percpu_counter_destroy(&bdi->bdi_stat[i]);
  131. }
  132. return err;
  133. }
  134. EXPORT_SYMBOL(bdi_init);
  135. void bdi_destroy(struct backing_dev_info *bdi)
  136. {
  137. int i;
  138. bdi_unregister(bdi);
  139. for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
  140. percpu_counter_destroy(&bdi->bdi_stat[i]);
  141. prop_local_destroy_percpu(&bdi->completions);
  142. }
  143. EXPORT_SYMBOL(bdi_destroy);
  144. static wait_queue_head_t congestion_wqh[2] = {
  145. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  146. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  147. };
  148. void clear_bdi_congested(struct backing_dev_info *bdi, int rw)
  149. {
  150. enum bdi_state bit;
  151. wait_queue_head_t *wqh = &congestion_wqh[rw];
  152. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  153. clear_bit(bit, &bdi->state);
  154. smp_mb__after_clear_bit();
  155. if (waitqueue_active(wqh))
  156. wake_up(wqh);
  157. }
  158. EXPORT_SYMBOL(clear_bdi_congested);
  159. void set_bdi_congested(struct backing_dev_info *bdi, int rw)
  160. {
  161. enum bdi_state bit;
  162. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  163. set_bit(bit, &bdi->state);
  164. }
  165. EXPORT_SYMBOL(set_bdi_congested);
  166. /**
  167. * congestion_wait - wait for a backing_dev to become uncongested
  168. * @rw: READ or WRITE
  169. * @timeout: timeout in jiffies
  170. *
  171. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  172. * write congestion. If no backing_devs are congested then just wait for the
  173. * next write to be completed.
  174. */
  175. long congestion_wait(int rw, long timeout)
  176. {
  177. long ret;
  178. DEFINE_WAIT(wait);
  179. wait_queue_head_t *wqh = &congestion_wqh[rw];
  180. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  181. ret = io_schedule_timeout(timeout);
  182. finish_wait(wqh, &wait);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(congestion_wait);