locking.c 8.4 KB

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