locking.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. }
  58. /*
  59. * if we currently have a blocking lock, take the spinlock
  60. * and drop our blocking count
  61. */
  62. void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)
  63. {
  64. /*
  65. * no lock is required. The lock owner may change if
  66. * we have a read lock, but it won't change to or away
  67. * from us. If we have the write lock, we are the owner
  68. * and it'll never change.
  69. */
  70. if (eb->lock_nested && current->pid == eb->lock_owner)
  71. return;
  72. if (rw == BTRFS_WRITE_LOCK_BLOCKING) {
  73. BUG_ON(atomic_read(&eb->blocking_writers) != 1);
  74. write_lock(&eb->lock);
  75. WARN_ON(atomic_read(&eb->spinning_writers));
  76. atomic_inc(&eb->spinning_writers);
  77. /*
  78. * atomic_dec_and_test implies a barrier for waitqueue_active
  79. */
  80. if (atomic_dec_and_test(&eb->blocking_writers) &&
  81. waitqueue_active(&eb->write_lock_wq))
  82. wake_up(&eb->write_lock_wq);
  83. } else if (rw == BTRFS_READ_LOCK_BLOCKING) {
  84. BUG_ON(atomic_read(&eb->blocking_readers) == 0);
  85. read_lock(&eb->lock);
  86. atomic_inc(&eb->spinning_readers);
  87. /*
  88. * atomic_dec_and_test implies a barrier for waitqueue_active
  89. */
  90. if (atomic_dec_and_test(&eb->blocking_readers) &&
  91. waitqueue_active(&eb->read_lock_wq))
  92. wake_up(&eb->read_lock_wq);
  93. }
  94. }
  95. /*
  96. * take a spinning read lock. This will wait for any blocking
  97. * writers
  98. */
  99. void btrfs_tree_read_lock(struct extent_buffer *eb)
  100. {
  101. again:
  102. BUG_ON(!atomic_read(&eb->blocking_writers) &&
  103. current->pid == eb->lock_owner);
  104. read_lock(&eb->lock);
  105. if (atomic_read(&eb->blocking_writers) &&
  106. current->pid == eb->lock_owner) {
  107. /*
  108. * This extent is already write-locked by our thread. We allow
  109. * an additional read lock to be added because it's for the same
  110. * thread. btrfs_find_all_roots() depends on this as it may be
  111. * called on a partly (write-)locked tree.
  112. */
  113. BUG_ON(eb->lock_nested);
  114. eb->lock_nested = 1;
  115. read_unlock(&eb->lock);
  116. return;
  117. }
  118. if (atomic_read(&eb->blocking_writers)) {
  119. read_unlock(&eb->lock);
  120. wait_event(eb->write_lock_wq,
  121. atomic_read(&eb->blocking_writers) == 0);
  122. goto again;
  123. }
  124. atomic_inc(&eb->read_locks);
  125. atomic_inc(&eb->spinning_readers);
  126. }
  127. /*
  128. * take a spinning read lock.
  129. * returns 1 if we get the read lock and 0 if we don't
  130. * this won't wait for blocking writers
  131. */
  132. int btrfs_tree_read_lock_atomic(struct extent_buffer *eb)
  133. {
  134. if (atomic_read(&eb->blocking_writers))
  135. return 0;
  136. read_lock(&eb->lock);
  137. if (atomic_read(&eb->blocking_writers)) {
  138. read_unlock(&eb->lock);
  139. return 0;
  140. }
  141. atomic_inc(&eb->read_locks);
  142. atomic_inc(&eb->spinning_readers);
  143. return 1;
  144. }
  145. /*
  146. * returns 1 if we get the read lock and 0 if we don't
  147. * this won't wait for blocking writers
  148. */
  149. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  150. {
  151. if (atomic_read(&eb->blocking_writers))
  152. return 0;
  153. if (!read_trylock(&eb->lock))
  154. return 0;
  155. if (atomic_read(&eb->blocking_writers)) {
  156. read_unlock(&eb->lock);
  157. return 0;
  158. }
  159. atomic_inc(&eb->read_locks);
  160. atomic_inc(&eb->spinning_readers);
  161. return 1;
  162. }
  163. /*
  164. * returns 1 if we get the read lock and 0 if we don't
  165. * this won't wait for blocking writers or readers
  166. */
  167. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  168. {
  169. if (atomic_read(&eb->blocking_writers) ||
  170. atomic_read(&eb->blocking_readers))
  171. return 0;
  172. write_lock(&eb->lock);
  173. if (atomic_read(&eb->blocking_writers) ||
  174. atomic_read(&eb->blocking_readers)) {
  175. write_unlock(&eb->lock);
  176. return 0;
  177. }
  178. atomic_inc(&eb->write_locks);
  179. atomic_inc(&eb->spinning_writers);
  180. eb->lock_owner = current->pid;
  181. return 1;
  182. }
  183. /*
  184. * drop a spinning read lock
  185. */
  186. void btrfs_tree_read_unlock(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->spinning_readers) == 0);
  200. atomic_dec(&eb->spinning_readers);
  201. atomic_dec(&eb->read_locks);
  202. read_unlock(&eb->lock);
  203. }
  204. /*
  205. * drop a blocking read lock
  206. */
  207. void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
  208. {
  209. /*
  210. * if we're nested, we have the write lock. No new locking
  211. * is needed as long as we are the lock owner.
  212. * The write unlock will do a barrier for us, and the lock_nested
  213. * field only matters to the lock owner.
  214. */
  215. if (eb->lock_nested && current->pid == eb->lock_owner) {
  216. eb->lock_nested = 0;
  217. return;
  218. }
  219. btrfs_assert_tree_read_locked(eb);
  220. WARN_ON(atomic_read(&eb->blocking_readers) == 0);
  221. /*
  222. * atomic_dec_and_test implies a barrier for waitqueue_active
  223. */
  224. if (atomic_dec_and_test(&eb->blocking_readers) &&
  225. waitqueue_active(&eb->read_lock_wq))
  226. wake_up(&eb->read_lock_wq);
  227. atomic_dec(&eb->read_locks);
  228. }
  229. /*
  230. * take a spinning write lock. This will wait for both
  231. * blocking readers or writers
  232. */
  233. void btrfs_tree_lock(struct extent_buffer *eb)
  234. {
  235. WARN_ON(eb->lock_owner == current->pid);
  236. again:
  237. wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
  238. wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
  239. write_lock(&eb->lock);
  240. if (atomic_read(&eb->blocking_readers)) {
  241. write_unlock(&eb->lock);
  242. wait_event(eb->read_lock_wq,
  243. atomic_read(&eb->blocking_readers) == 0);
  244. goto again;
  245. }
  246. if (atomic_read(&eb->blocking_writers)) {
  247. write_unlock(&eb->lock);
  248. wait_event(eb->write_lock_wq,
  249. atomic_read(&eb->blocking_writers) == 0);
  250. goto again;
  251. }
  252. WARN_ON(atomic_read(&eb->spinning_writers));
  253. atomic_inc(&eb->spinning_writers);
  254. atomic_inc(&eb->write_locks);
  255. eb->lock_owner = current->pid;
  256. }
  257. /*
  258. * drop a spinning or a blocking write lock.
  259. */
  260. void btrfs_tree_unlock(struct extent_buffer *eb)
  261. {
  262. int blockers = atomic_read(&eb->blocking_writers);
  263. BUG_ON(blockers > 1);
  264. btrfs_assert_tree_locked(eb);
  265. eb->lock_owner = 0;
  266. atomic_dec(&eb->write_locks);
  267. if (blockers) {
  268. WARN_ON(atomic_read(&eb->spinning_writers));
  269. atomic_dec(&eb->blocking_writers);
  270. /*
  271. * Make sure counter is updated before we wake up waiters.
  272. */
  273. smp_mb();
  274. if (waitqueue_active(&eb->write_lock_wq))
  275. wake_up(&eb->write_lock_wq);
  276. } else {
  277. WARN_ON(atomic_read(&eb->spinning_writers) != 1);
  278. atomic_dec(&eb->spinning_writers);
  279. write_unlock(&eb->lock);
  280. }
  281. }
  282. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  283. {
  284. BUG_ON(!atomic_read(&eb->write_locks));
  285. }
  286. static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
  287. {
  288. BUG_ON(!atomic_read(&eb->read_locks));
  289. }