locking.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 (eb->lock_nested) {
  35. read_lock(&eb->lock);
  36. if (eb->lock_nested && current->pid == eb->lock_owner) {
  37. read_unlock(&eb->lock);
  38. return;
  39. }
  40. read_unlock(&eb->lock);
  41. }
  42. if (rw == BTRFS_WRITE_LOCK) {
  43. if (atomic_read(&eb->blocking_writers) == 0) {
  44. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  45. atomic_dec(&eb->spinning_writers);
  46. btrfs_assert_tree_locked(eb);
  47. atomic_inc(&eb->blocking_writers);
  48. write_unlock(&eb->lock);
  49. }
  50. } else if (rw == BTRFS_READ_LOCK) {
  51. btrfs_assert_tree_read_locked(eb);
  52. atomic_inc(&eb->blocking_readers);
  53. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  54. atomic_dec(&eb->spinning_readers);
  55. read_unlock(&eb->lock);
  56. }
  57. return;
  58. }
  59. /*
  60. * if we currently have a blocking lock, take the spinlock
  61. * and drop our blocking count
  62. */
  63. void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)
  64. {
  65. if (eb->lock_nested) {
  66. read_lock(&eb->lock);
  67. if (&eb->lock_nested && current->pid == eb->lock_owner) {
  68. read_unlock(&eb->lock);
  69. return;
  70. }
  71. read_unlock(&eb->lock);
  72. }
  73. if (rw == BTRFS_WRITE_LOCK_BLOCKING) {
  74. BUG_ON(atomic_read(&eb->blocking_writers) != 1);
  75. write_lock(&eb->lock);
  76. WARN_ON(atomic_read(&eb->spinning_writers));
  77. atomic_inc(&eb->spinning_writers);
  78. if (atomic_dec_and_test(&eb->blocking_writers))
  79. wake_up(&eb->write_lock_wq);
  80. } else if (rw == BTRFS_READ_LOCK_BLOCKING) {
  81. BUG_ON(atomic_read(&eb->blocking_readers) == 0);
  82. read_lock(&eb->lock);
  83. atomic_inc(&eb->spinning_readers);
  84. if (atomic_dec_and_test(&eb->blocking_readers))
  85. wake_up(&eb->read_lock_wq);
  86. }
  87. return;
  88. }
  89. /*
  90. * take a spinning read lock. This will wait for any blocking
  91. * writers
  92. */
  93. void btrfs_tree_read_lock(struct extent_buffer *eb)
  94. {
  95. again:
  96. read_lock(&eb->lock);
  97. if (atomic_read(&eb->blocking_writers) &&
  98. current->pid == eb->lock_owner) {
  99. /*
  100. * This extent is already write-locked by our thread. We allow
  101. * an additional read lock to be added because it's for the same
  102. * thread. btrfs_find_all_roots() depends on this as it may be
  103. * called on a partly (write-)locked tree.
  104. */
  105. BUG_ON(eb->lock_nested);
  106. eb->lock_nested = 1;
  107. read_unlock(&eb->lock);
  108. return;
  109. }
  110. read_unlock(&eb->lock);
  111. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  112. read_lock(&eb->lock);
  113. if (atomic_read(&eb->blocking_writers)) {
  114. read_unlock(&eb->lock);
  115. goto again;
  116. }
  117. atomic_inc(&eb->read_locks);
  118. atomic_inc(&eb->spinning_readers);
  119. }
  120. /*
  121. * returns 1 if we get the read lock and 0 if we don't
  122. * this won't wait for blocking writers
  123. */
  124. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  125. {
  126. if (atomic_read(&eb->blocking_writers))
  127. return 0;
  128. read_lock(&eb->lock);
  129. if (atomic_read(&eb->blocking_writers)) {
  130. read_unlock(&eb->lock);
  131. return 0;
  132. }
  133. atomic_inc(&eb->read_locks);
  134. atomic_inc(&eb->spinning_readers);
  135. return 1;
  136. }
  137. /*
  138. * returns 1 if we get the read lock and 0 if we don't
  139. * this won't wait for blocking writers or readers
  140. */
  141. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  142. {
  143. if (atomic_read(&eb->blocking_writers) ||
  144. atomic_read(&eb->blocking_readers))
  145. return 0;
  146. write_lock(&eb->lock);
  147. if (atomic_read(&eb->blocking_writers) ||
  148. atomic_read(&eb->blocking_readers)) {
  149. write_unlock(&eb->lock);
  150. return 0;
  151. }
  152. atomic_inc(&eb->write_locks);
  153. atomic_inc(&eb->spinning_writers);
  154. eb->lock_owner = current->pid;
  155. return 1;
  156. }
  157. /*
  158. * drop a spinning read lock
  159. */
  160. void btrfs_tree_read_unlock(struct extent_buffer *eb)
  161. {
  162. if (eb->lock_nested) {
  163. read_lock(&eb->lock);
  164. if (eb->lock_nested && current->pid == eb->lock_owner) {
  165. eb->lock_nested = 0;
  166. read_unlock(&eb->lock);
  167. return;
  168. }
  169. read_unlock(&eb->lock);
  170. }
  171. btrfs_assert_tree_read_locked(eb);
  172. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  173. atomic_dec(&eb->spinning_readers);
  174. atomic_dec(&eb->read_locks);
  175. read_unlock(&eb->lock);
  176. }
  177. /*
  178. * drop a blocking read lock
  179. */
  180. void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
  181. {
  182. if (eb->lock_nested) {
  183. read_lock(&eb->lock);
  184. if (eb->lock_nested && current->pid == eb->lock_owner) {
  185. eb->lock_nested = 0;
  186. read_unlock(&eb->lock);
  187. return;
  188. }
  189. read_unlock(&eb->lock);
  190. }
  191. btrfs_assert_tree_read_locked(eb);
  192. WARN_ON(atomic_read(&eb->blocking_readers) == 0);
  193. if (atomic_dec_and_test(&eb->blocking_readers))
  194. wake_up(&eb->read_lock_wq);
  195. atomic_dec(&eb->read_locks);
  196. }
  197. /*
  198. * take a spinning write lock. This will wait for both
  199. * blocking readers or writers
  200. */
  201. void btrfs_tree_lock(struct extent_buffer *eb)
  202. {
  203. again:
  204. wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
  205. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  206. write_lock(&eb->lock);
  207. if (atomic_read(&eb->blocking_readers)) {
  208. write_unlock(&eb->lock);
  209. wait_event(eb->read_lock_wq,
  210. atomic_read(&eb->blocking_readers) == 0);
  211. goto again;
  212. }
  213. if (atomic_read(&eb->blocking_writers)) {
  214. write_unlock(&eb->lock);
  215. wait_event(eb->write_lock_wq,
  216. atomic_read(&eb->blocking_writers) == 0);
  217. goto again;
  218. }
  219. WARN_ON(atomic_read(&eb->spinning_writers));
  220. atomic_inc(&eb->spinning_writers);
  221. atomic_inc(&eb->write_locks);
  222. eb->lock_owner = current->pid;
  223. }
  224. /*
  225. * drop a spinning or a blocking write lock.
  226. */
  227. void btrfs_tree_unlock(struct extent_buffer *eb)
  228. {
  229. int blockers = atomic_read(&eb->blocking_writers);
  230. BUG_ON(blockers > 1);
  231. btrfs_assert_tree_locked(eb);
  232. atomic_dec(&eb->write_locks);
  233. if (blockers) {
  234. WARN_ON(atomic_read(&eb->spinning_writers));
  235. atomic_dec(&eb->blocking_writers);
  236. smp_wmb();
  237. wake_up(&eb->write_lock_wq);
  238. } else {
  239. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  240. atomic_dec(&eb->spinning_writers);
  241. write_unlock(&eb->lock);
  242. }
  243. }
  244. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  245. {
  246. BUG_ON(!atomic_read(&eb->write_locks));
  247. }
  248. void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
  249. {
  250. BUG_ON(!atomic_read(&eb->read_locks));
  251. }