locking.c 7.1 KB

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