backing-dev.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. int bdi_init(struct backing_dev_info *bdi)
  7. {
  8. int i;
  9. int err;
  10. for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
  11. err = percpu_counter_init_irq(&bdi->bdi_stat[i], 0);
  12. if (err)
  13. goto err;
  14. }
  15. bdi->dirty_exceeded = 0;
  16. err = prop_local_init_percpu(&bdi->completions);
  17. if (err) {
  18. err:
  19. while (i--)
  20. percpu_counter_destroy(&bdi->bdi_stat[i]);
  21. }
  22. return err;
  23. }
  24. EXPORT_SYMBOL(bdi_init);
  25. void bdi_destroy(struct backing_dev_info *bdi)
  26. {
  27. int i;
  28. for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
  29. percpu_counter_destroy(&bdi->bdi_stat[i]);
  30. prop_local_destroy_percpu(&bdi->completions);
  31. }
  32. EXPORT_SYMBOL(bdi_destroy);
  33. static wait_queue_head_t congestion_wqh[2] = {
  34. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  35. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  36. };
  37. void clear_bdi_congested(struct backing_dev_info *bdi, int rw)
  38. {
  39. enum bdi_state bit;
  40. wait_queue_head_t *wqh = &congestion_wqh[rw];
  41. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  42. clear_bit(bit, &bdi->state);
  43. smp_mb__after_clear_bit();
  44. if (waitqueue_active(wqh))
  45. wake_up(wqh);
  46. }
  47. EXPORT_SYMBOL(clear_bdi_congested);
  48. void set_bdi_congested(struct backing_dev_info *bdi, int rw)
  49. {
  50. enum bdi_state bit;
  51. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  52. set_bit(bit, &bdi->state);
  53. }
  54. EXPORT_SYMBOL(set_bdi_congested);
  55. /**
  56. * congestion_wait - wait for a backing_dev to become uncongested
  57. * @rw: READ or WRITE
  58. * @timeout: timeout in jiffies
  59. *
  60. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  61. * write congestion. If no backing_devs are congested then just wait for the
  62. * next write to be completed.
  63. */
  64. long congestion_wait(int rw, long timeout)
  65. {
  66. long ret;
  67. DEFINE_WAIT(wait);
  68. wait_queue_head_t *wqh = &congestion_wqh[rw];
  69. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  70. ret = io_schedule_timeout(timeout);
  71. finish_wait(wqh, &wait);
  72. return ret;
  73. }
  74. EXPORT_SYMBOL(congestion_wait);