locking.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * take a spinning read lock.
  125. * returns 1 if we get the read lock and 0 if we don't
  126. * this won't wait for blocking writers
  127. */
  128. int btrfs_tree_read_lock_atomic(struct extent_buffer *eb)
  129. {
  130. if (atomic_read(&eb->blocking_writers))
  131. return 0;
  132. read_lock(&eb->lock);
  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
  144. */
  145. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  146. {
  147. if (atomic_read(&eb->blocking_writers))
  148. return 0;
  149. if (!read_trylock(&eb->lock))
  150. return 0;
  151. if (atomic_read(&eb->blocking_writers)) {
  152. read_unlock(&eb->lock);
  153. return 0;
  154. }
  155. atomic_inc(&eb->read_locks);
  156. atomic_inc(&eb->spinning_readers);
  157. return 1;
  158. }
  159. /*
  160. * returns 1 if we get the read lock and 0 if we don't
  161. * this won't wait for blocking writers or readers
  162. */
  163. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  164. {
  165. if (atomic_read(&eb->blocking_writers) ||
  166. atomic_read(&eb->blocking_readers))
  167. return 0;
  168. write_lock(&eb->lock);
  169. if (atomic_read(&eb->blocking_writers) ||
  170. atomic_read(&eb->blocking_readers)) {
  171. write_unlock(&eb->lock);
  172. return 0;
  173. }
  174. atomic_inc(&eb->write_locks);
  175. atomic_inc(&eb->spinning_writers);
  176. eb->lock_owner = current->pid;
  177. return 1;
  178. }
  179. /*
  180. * drop a spinning read lock
  181. */
  182. void btrfs_tree_read_unlock(struct extent_buffer *eb)
  183. {
  184. /*
  185. * if we're nested, we have the write lock. No new locking
  186. * is needed as long as we are the lock owner.
  187. * The write unlock will do a barrier for us, and the lock_nested
  188. * field only matters to the lock owner.
  189. */
  190. if (eb->lock_nested && current->pid == eb->lock_owner) {
  191. eb->lock_nested = 0;
  192. return;
  193. }
  194. btrfs_assert_tree_read_locked(eb);
  195. WARN_ON(atomic_read(&eb->spinning_readers) == 0);
  196. atomic_dec(&eb->spinning_readers);
  197. atomic_dec(&eb->read_locks);
  198. read_unlock(&eb->lock);
  199. }
  200. /*
  201. * drop a blocking read lock
  202. */
  203. void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
  204. {
  205. /*
  206. * if we're nested, we have the write lock. No new locking
  207. * is needed as long as we are the lock owner.
  208. * The write unlock will do a barrier for us, and the lock_nested
  209. * field only matters to the lock owner.
  210. */
  211. if (eb->lock_nested && current->pid == eb->lock_owner) {
  212. eb->lock_nested = 0;
  213. return;
  214. }
  215. btrfs_assert_tree_read_locked(eb);
  216. WARN_ON(atomic_read(&eb->blocking_readers) == 0);
  217. if (atomic_dec_and_test(&eb->blocking_readers) &&
  218. waitqueue_active(&eb->read_lock_wq))
  219. wake_up(&eb->read_lock_wq);
  220. atomic_dec(&eb->read_locks);
  221. }
  222. /*
  223. * take a spinning write lock. This will wait for both
  224. * blocking readers or writers
  225. */
  226. void btrfs_tree_lock(struct extent_buffer *eb)
  227. {
  228. again:
  229. wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
  230. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  231. write_lock(&eb->lock);
  232. if (atomic_read(&eb->blocking_readers)) {
  233. write_unlock(&eb->lock);
  234. wait_event(eb->read_lock_wq,
  235. atomic_read(&eb->blocking_readers) == 0);
  236. goto again;
  237. }
  238. if (atomic_read(&eb->blocking_writers)) {
  239. write_unlock(&eb->lock);
  240. wait_event(eb->write_lock_wq,
  241. atomic_read(&eb->blocking_writers) == 0);
  242. goto again;
  243. }
  244. WARN_ON(atomic_read(&eb->spinning_writers));
  245. atomic_inc(&eb->spinning_writers);
  246. atomic_inc(&eb->write_locks);
  247. eb->lock_owner = current->pid;
  248. }
  249. /*
  250. * drop a spinning or a blocking write lock.
  251. */
  252. void btrfs_tree_unlock(struct extent_buffer *eb)
  253. {
  254. int blockers = atomic_read(&eb->blocking_writers);
  255. BUG_ON(blockers > 1);
  256. btrfs_assert_tree_locked(eb);
  257. eb->lock_owner = 0;
  258. atomic_dec(&eb->write_locks);
  259. if (blockers) {
  260. WARN_ON(atomic_read(&eb->spinning_writers));
  261. atomic_dec(&eb->blocking_writers);
  262. smp_mb();
  263. if (waitqueue_active(&eb->write_lock_wq))
  264. wake_up(&eb->write_lock_wq);
  265. } else {
  266. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  267. atomic_dec(&eb->spinning_writers);
  268. write_unlock(&eb->lock);
  269. }
  270. }
  271. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  272. {
  273. BUG_ON(!atomic_read(&eb->write_locks));
  274. }
  275. static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
  276. {
  277. BUG_ON(!atomic_read(&eb->read_locks));
  278. }