locking.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/page-flags.h>
  22. #include <asm/bug.h>
  23. #include "ctree.h"
  24. #include "extent_io.h"
  25. #include "locking.h"
  26. void btrfs_assert_tree_read_locked(struct extent_buffer *eb);
  27. /*
  28. * if we currently have a spinning reader or writer lock
  29. * (indicated by the rw flag) this will bump the count
  30. * of blocking holders and drop the spinlock.
  31. */
  32. void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw)
  33. {
  34. if (rw == BTRFS_WRITE_LOCK) {
  35. if (atomic_read(&eb->blocking_writers) == 0) {
  36. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  37. atomic_dec(&eb->spinning_writers);
  38. btrfs_assert_tree_locked(eb);
  39. atomic_inc(&eb->blocking_writers);
  40. write_unlock(&eb->lock);
  41. }
  42. } else if (rw == BTRFS_READ_LOCK) {
  43. btrfs_assert_tree_read_locked(eb);
  44. atomic_inc(&eb->blocking_readers);
  45. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  46. atomic_dec(&eb->spinning_readers);
  47. read_unlock(&eb->lock);
  48. }
  49. return;
  50. }
  51. /*
  52. * if we currently have a blocking lock, take the spinlock
  53. * and drop our blocking count
  54. */
  55. void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)
  56. {
  57. if (rw == BTRFS_WRITE_LOCK_BLOCKING) {
  58. BUG_ON(atomic_read(&eb->blocking_writers) != 1);
  59. write_lock(&eb->lock);
  60. WARN_ON(atomic_read(&eb->spinning_writers));
  61. atomic_inc(&eb->spinning_writers);
  62. if (atomic_dec_and_test(&eb->blocking_writers))
  63. wake_up(&eb->write_lock_wq);
  64. } else if (rw == BTRFS_READ_LOCK_BLOCKING) {
  65. BUG_ON(atomic_read(&eb->blocking_readers) == 0);
  66. read_lock(&eb->lock);
  67. atomic_inc(&eb->spinning_readers);
  68. if (atomic_dec_and_test(&eb->blocking_readers))
  69. wake_up(&eb->read_lock_wq);
  70. }
  71. return;
  72. }
  73. /*
  74. * take a spinning read lock. This will wait for any blocking
  75. * writers
  76. */
  77. void btrfs_tree_read_lock(struct extent_buffer *eb)
  78. {
  79. again:
  80. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  81. read_lock(&eb->lock);
  82. if (atomic_read(&eb->blocking_writers)) {
  83. read_unlock(&eb->lock);
  84. wait_event(eb->write_lock_wq,
  85. atomic_read(&eb->blocking_writers) == 0);
  86. goto again;
  87. }
  88. atomic_inc(&eb->read_locks);
  89. atomic_inc(&eb->spinning_readers);
  90. }
  91. /*
  92. * returns 1 if we get the read lock and 0 if we don't
  93. * this won't wait for blocking writers
  94. */
  95. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  96. {
  97. if (atomic_read(&eb->blocking_writers))
  98. return 0;
  99. read_lock(&eb->lock);
  100. if (atomic_read(&eb->blocking_writers)) {
  101. read_unlock(&eb->lock);
  102. return 0;
  103. }
  104. atomic_inc(&eb->read_locks);
  105. atomic_inc(&eb->spinning_readers);
  106. return 1;
  107. }
  108. /*
  109. * returns 1 if we get the read lock and 0 if we don't
  110. * this won't wait for blocking writers or readers
  111. */
  112. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  113. {
  114. if (atomic_read(&eb->blocking_writers) ||
  115. atomic_read(&eb->blocking_readers))
  116. return 0;
  117. write_lock(&eb->lock);
  118. if (atomic_read(&eb->blocking_writers) ||
  119. atomic_read(&eb->blocking_readers)) {
  120. write_unlock(&eb->lock);
  121. return 0;
  122. }
  123. atomic_inc(&eb->write_locks);
  124. atomic_inc(&eb->spinning_writers);
  125. return 1;
  126. }
  127. /*
  128. * drop a spinning read lock
  129. */
  130. void btrfs_tree_read_unlock(struct extent_buffer *eb)
  131. {
  132. btrfs_assert_tree_read_locked(eb);
  133. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  134. atomic_dec(&eb->spinning_readers);
  135. atomic_dec(&eb->read_locks);
  136. read_unlock(&eb->lock);
  137. }
  138. /*
  139. * drop a blocking read lock
  140. */
  141. void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
  142. {
  143. btrfs_assert_tree_read_locked(eb);
  144. WARN_ON(atomic_read(&eb->blocking_readers) == 0);
  145. if (atomic_dec_and_test(&eb->blocking_readers))
  146. wake_up(&eb->read_lock_wq);
  147. atomic_dec(&eb->read_locks);
  148. }
  149. /*
  150. * take a spinning write lock. This will wait for both
  151. * blocking readers or writers
  152. */
  153. int btrfs_tree_lock(struct extent_buffer *eb)
  154. {
  155. again:
  156. wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
  157. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  158. write_lock(&eb->lock);
  159. if (atomic_read(&eb->blocking_readers)) {
  160. write_unlock(&eb->lock);
  161. wait_event(eb->read_lock_wq,
  162. atomic_read(&eb->blocking_readers) == 0);
  163. goto again;
  164. }
  165. if (atomic_read(&eb->blocking_writers)) {
  166. write_unlock(&eb->lock);
  167. wait_event(eb->write_lock_wq,
  168. atomic_read(&eb->blocking_writers) == 0);
  169. goto again;
  170. }
  171. WARN_ON(atomic_read(&eb->spinning_writers));
  172. atomic_inc(&eb->spinning_writers);
  173. atomic_inc(&eb->write_locks);
  174. return 0;
  175. }
  176. /*
  177. * drop a spinning or a blocking write lock.
  178. */
  179. int btrfs_tree_unlock(struct extent_buffer *eb)
  180. {
  181. int blockers = atomic_read(&eb->blocking_writers);
  182. BUG_ON(blockers > 1);
  183. btrfs_assert_tree_locked(eb);
  184. atomic_dec(&eb->write_locks);
  185. if (blockers) {
  186. WARN_ON(atomic_read(&eb->spinning_writers));
  187. atomic_dec(&eb->blocking_writers);
  188. smp_wmb();
  189. wake_up(&eb->write_lock_wq);
  190. } else {
  191. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  192. atomic_dec(&eb->spinning_writers);
  193. write_unlock(&eb->lock);
  194. }
  195. return 0;
  196. }
  197. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  198. {
  199. BUG_ON(!atomic_read(&eb->write_locks));
  200. }
  201. void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
  202. {
  203. BUG_ON(!atomic_read(&eb->read_locks));
  204. }