locking.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /*
  35. * no lock is required. The lock owner may change if
  36. * we have a read lock, but it won't change to or away
  37. * from us. If we have the write lock, we are the owner
  38. * and it'll never change.
  39. */
  40. if (eb->lock_nested && current->pid == eb->lock_owner)
  41. return;
  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. /*
  66. * no lock is required. The lock owner may change if
  67. * we have a read lock, but it won't change to or away
  68. * from us. If we have the write lock, we are the owner
  69. * and it'll never change.
  70. */
  71. if (eb->lock_nested && current->pid == eb->lock_owner)
  72. return;
  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. BUG_ON(!atomic_read(&eb->blocking_writers) &&
  99. current->pid == eb->lock_owner);
  100. read_lock(&eb->lock);
  101. if (atomic_read(&eb->blocking_writers) &&
  102. current->pid == eb->lock_owner) {
  103. /*
  104. * This extent is already write-locked by our thread. We allow
  105. * an additional read lock to be added because it's for the same
  106. * thread. btrfs_find_all_roots() depends on this as it may be
  107. * called on a partly (write-)locked tree.
  108. */
  109. BUG_ON(eb->lock_nested);
  110. eb->lock_nested = 1;
  111. read_unlock(&eb->lock);
  112. return;
  113. }
  114. if (atomic_read(&eb->blocking_writers)) {
  115. read_unlock(&eb->lock);
  116. wait_event(eb->write_lock_wq,
  117. atomic_read(&eb->blocking_writers) == 0);
  118. goto again;
  119. }
  120. atomic_inc(&eb->read_locks);
  121. atomic_inc(&eb->spinning_readers);
  122. }
  123. /*
  124. * returns 1 if we get the read lock and 0 if we don't
  125. * this won't wait for blocking writers
  126. */
  127. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  128. {
  129. if (atomic_read(&eb->blocking_writers))
  130. return 0;
  131. if (!read_trylock(&eb->lock))
  132. return 0;
  133. if (atomic_read(&eb->blocking_writers)) {
  134. read_unlock(&eb->lock);
  135. return 0;
  136. }
  137. atomic_inc(&eb->read_locks);
  138. atomic_inc(&eb->spinning_readers);
  139. return 1;
  140. }
  141. /*
  142. * returns 1 if we get the read lock and 0 if we don't
  143. * this won't wait for blocking writers or readers
  144. */
  145. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  146. {
  147. if (atomic_read(&eb->blocking_writers) ||
  148. atomic_read(&eb->blocking_readers))
  149. return 0;
  150. if (!write_trylock(&eb->lock))
  151. return 0;
  152. if (atomic_read(&eb->blocking_writers) ||
  153. atomic_read(&eb->blocking_readers)) {
  154. write_unlock(&eb->lock);
  155. return 0;
  156. }
  157. atomic_inc(&eb->write_locks);
  158. atomic_inc(&eb->spinning_writers);
  159. eb->lock_owner = current->pid;
  160. return 1;
  161. }
  162. /*
  163. * drop a spinning read lock
  164. */
  165. void btrfs_tree_read_unlock(struct extent_buffer *eb)
  166. {
  167. /*
  168. * if we're nested, we have the write lock. No new locking
  169. * is needed as long as we are the lock owner.
  170. * The write unlock will do a barrier for us, and the lock_nested
  171. * field only matters to the lock owner.
  172. */
  173. if (eb->lock_nested && current->pid == eb->lock_owner) {
  174. eb->lock_nested = 0;
  175. return;
  176. }
  177. btrfs_assert_tree_read_locked(eb);
  178. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  179. atomic_dec(&eb->spinning_readers);
  180. atomic_dec(&eb->read_locks);
  181. read_unlock(&eb->lock);
  182. }
  183. /*
  184. * drop a blocking read lock
  185. */
  186. void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
  187. {
  188. /*
  189. * if we're nested, we have the write lock. No new locking
  190. * is needed as long as we are the lock owner.
  191. * The write unlock will do a barrier for us, and the lock_nested
  192. * field only matters to the lock owner.
  193. */
  194. if (eb->lock_nested && current->pid == eb->lock_owner) {
  195. eb->lock_nested = 0;
  196. return;
  197. }
  198. btrfs_assert_tree_read_locked(eb);
  199. WARN_ON(atomic_read(&eb->blocking_readers) == 0);
  200. if (atomic_dec_and_test(&eb->blocking_readers) &&
  201. waitqueue_active(&eb->read_lock_wq))
  202. wake_up(&eb->read_lock_wq);
  203. atomic_dec(&eb->read_locks);
  204. }
  205. /*
  206. * take a spinning write lock. This will wait for both
  207. * blocking readers or writers
  208. */
  209. void btrfs_tree_lock(struct extent_buffer *eb)
  210. {
  211. again:
  212. wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
  213. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  214. write_lock(&eb->lock);
  215. if (atomic_read(&eb->blocking_readers)) {
  216. write_unlock(&eb->lock);
  217. wait_event(eb->read_lock_wq,
  218. atomic_read(&eb->blocking_readers) == 0);
  219. goto again;
  220. }
  221. if (atomic_read(&eb->blocking_writers)) {
  222. write_unlock(&eb->lock);
  223. wait_event(eb->write_lock_wq,
  224. atomic_read(&eb->blocking_writers) == 0);
  225. goto again;
  226. }
  227. WARN_ON(atomic_read(&eb->spinning_writers));
  228. atomic_inc(&eb->spinning_writers);
  229. atomic_inc(&eb->write_locks);
  230. eb->lock_owner = current->pid;
  231. }
  232. /*
  233. * drop a spinning or a blocking write lock.
  234. */
  235. void btrfs_tree_unlock(struct extent_buffer *eb)
  236. {
  237. int blockers = atomic_read(&eb->blocking_writers);
  238. BUG_ON(blockers > 1);
  239. btrfs_assert_tree_locked(eb);
  240. eb->lock_owner = 0;
  241. atomic_dec(&eb->write_locks);
  242. if (blockers) {
  243. WARN_ON(atomic_read(&eb->spinning_writers));
  244. atomic_dec(&eb->blocking_writers);
  245. smp_mb();
  246. if (waitqueue_active(&eb->write_lock_wq))
  247. wake_up(&eb->write_lock_wq);
  248. } else {
  249. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  250. atomic_dec(&eb->spinning_writers);
  251. write_unlock(&eb->lock);
  252. }
  253. }
  254. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  255. {
  256. BUG_ON(!atomic_read(&eb->write_locks));
  257. }
  258. static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
  259. {
  260. BUG_ON(!atomic_read(&eb->read_locks));
  261. }