locking.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. static 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. if (atomic_read(&eb->blocking_writers)) {
  113. read_unlock(&eb->lock);
  114. wait_event(eb->write_lock_wq,
  115. atomic_read(&eb->blocking_writers) == 0);
  116. goto again;
  117. }
  118. atomic_inc(&eb->read_locks);
  119. atomic_inc(&eb->spinning_readers);
  120. }
  121. /*
  122. * returns 1 if we get the read lock and 0 if we don't
  123. * this won't wait for blocking writers
  124. */
  125. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  126. {
  127. if (atomic_read(&eb->blocking_writers))
  128. return 0;
  129. read_lock(&eb->lock);
  130. if (atomic_read(&eb->blocking_writers)) {
  131. read_unlock(&eb->lock);
  132. return 0;
  133. }
  134. atomic_inc(&eb->read_locks);
  135. atomic_inc(&eb->spinning_readers);
  136. return 1;
  137. }
  138. /*
  139. * returns 1 if we get the read lock and 0 if we don't
  140. * this won't wait for blocking writers or readers
  141. */
  142. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  143. {
  144. if (atomic_read(&eb->blocking_writers) ||
  145. atomic_read(&eb->blocking_readers))
  146. return 0;
  147. write_lock(&eb->lock);
  148. if (atomic_read(&eb->blocking_writers) ||
  149. atomic_read(&eb->blocking_readers)) {
  150. write_unlock(&eb->lock);
  151. return 0;
  152. }
  153. atomic_inc(&eb->write_locks);
  154. atomic_inc(&eb->spinning_writers);
  155. eb->lock_owner = current->pid;
  156. return 1;
  157. }
  158. /*
  159. * drop a spinning read lock
  160. */
  161. void btrfs_tree_read_unlock(struct extent_buffer *eb)
  162. {
  163. if (eb->lock_nested) {
  164. read_lock(&eb->lock);
  165. if (eb->lock_nested && current->pid == eb->lock_owner) {
  166. eb->lock_nested = 0;
  167. read_unlock(&eb->lock);
  168. return;
  169. }
  170. read_unlock(&eb->lock);
  171. }
  172. btrfs_assert_tree_read_locked(eb);
  173. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  174. atomic_dec(&eb->spinning_readers);
  175. atomic_dec(&eb->read_locks);
  176. read_unlock(&eb->lock);
  177. }
  178. /*
  179. * drop a blocking read lock
  180. */
  181. void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
  182. {
  183. if (eb->lock_nested) {
  184. read_lock(&eb->lock);
  185. if (eb->lock_nested && current->pid == eb->lock_owner) {
  186. eb->lock_nested = 0;
  187. read_unlock(&eb->lock);
  188. return;
  189. }
  190. read_unlock(&eb->lock);
  191. }
  192. btrfs_assert_tree_read_locked(eb);
  193. WARN_ON(atomic_read(&eb->blocking_readers) == 0);
  194. if (atomic_dec_and_test(&eb->blocking_readers) &&
  195. waitqueue_active(&eb->read_lock_wq))
  196. wake_up(&eb->read_lock_wq);
  197. atomic_dec(&eb->read_locks);
  198. }
  199. /*
  200. * take a spinning write lock. This will wait for both
  201. * blocking readers or writers
  202. */
  203. void btrfs_tree_lock(struct extent_buffer *eb)
  204. {
  205. again:
  206. wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
  207. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  208. write_lock(&eb->lock);
  209. if (atomic_read(&eb->blocking_readers)) {
  210. write_unlock(&eb->lock);
  211. wait_event(eb->read_lock_wq,
  212. atomic_read(&eb->blocking_readers) == 0);
  213. goto again;
  214. }
  215. if (atomic_read(&eb->blocking_writers)) {
  216. write_unlock(&eb->lock);
  217. wait_event(eb->write_lock_wq,
  218. atomic_read(&eb->blocking_writers) == 0);
  219. goto again;
  220. }
  221. WARN_ON(atomic_read(&eb->spinning_writers));
  222. atomic_inc(&eb->spinning_writers);
  223. atomic_inc(&eb->write_locks);
  224. eb->lock_owner = current->pid;
  225. }
  226. /*
  227. * drop a spinning or a blocking write lock.
  228. */
  229. void btrfs_tree_unlock(struct extent_buffer *eb)
  230. {
  231. int blockers = atomic_read(&eb->blocking_writers);
  232. BUG_ON(blockers > 1);
  233. btrfs_assert_tree_locked(eb);
  234. atomic_dec(&eb->write_locks);
  235. if (blockers) {
  236. WARN_ON(atomic_read(&eb->spinning_writers));
  237. atomic_dec(&eb->blocking_writers);
  238. smp_mb();
  239. if (waitqueue_active(&eb->write_lock_wq))
  240. wake_up(&eb->write_lock_wq);
  241. } else {
  242. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  243. atomic_dec(&eb->spinning_writers);
  244. write_unlock(&eb->lock);
  245. }
  246. }
  247. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  248. {
  249. BUG_ON(!atomic_read(&eb->write_locks));
  250. }
  251. static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
  252. {
  253. BUG_ON(!atomic_read(&eb->read_locks));
  254. }