backing-dev.c 8.2 KB

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