backing-dev.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. static wait_queue_head_t congestion_wqh[2] = {
  7. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  8. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  9. };
  10. void clear_bdi_congested(struct backing_dev_info *bdi, int rw)
  11. {
  12. enum bdi_state bit;
  13. wait_queue_head_t *wqh = &congestion_wqh[rw];
  14. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  15. clear_bit(bit, &bdi->state);
  16. smp_mb__after_clear_bit();
  17. if (waitqueue_active(wqh))
  18. wake_up(wqh);
  19. }
  20. EXPORT_SYMBOL(clear_bdi_congested);
  21. void set_bdi_congested(struct backing_dev_info *bdi, int rw)
  22. {
  23. enum bdi_state bit;
  24. bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
  25. set_bit(bit, &bdi->state);
  26. }
  27. EXPORT_SYMBOL(set_bdi_congested);
  28. /**
  29. * congestion_wait - wait for a backing_dev to become uncongested
  30. * @rw: READ or WRITE
  31. * @timeout: timeout in jiffies
  32. *
  33. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  34. * write congestion. If no backing_devs are congested then just wait for the
  35. * next write to be completed.
  36. */
  37. long congestion_wait(int rw, long timeout)
  38. {
  39. long ret;
  40. DEFINE_WAIT(wait);
  41. wait_queue_head_t *wqh = &congestion_wqh[rw];
  42. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  43. ret = io_schedule_timeout(timeout);
  44. finish_wait(wqh, &wait);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL(congestion_wait);
  48. /**
  49. * congestion_end - wake up sleepers on a congested backing_dev_info
  50. * @rw: READ or WRITE
  51. */
  52. void congestion_end(int rw)
  53. {
  54. wait_queue_head_t *wqh = &congestion_wqh[rw];
  55. if (waitqueue_active(wqh))
  56. wake_up(wqh);
  57. }
  58. EXPORT_SYMBOL(congestion_end);