]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/locking.c
Btrfs: Only let very young transactions grow during commit
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / locking.c
CommitLineData
925baedd
CM
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/gfp.h>
20#include <linux/pagemap.h>
21#include <linux/spinlock.h>
22#include <linux/page-flags.h>
4881ee5a 23#include <asm/bug.h>
925baedd
CM
24#include "ctree.h"
25#include "extent_io.h"
26#include "locking.h"
27
b4ce94de
CM
28static inline void spin_nested(struct extent_buffer *eb)
29{
30 spin_lock(&eb->lock);
31}
d397712b 32
b4ce94de
CM
33/*
34 * Setting a lock to blocking will drop the spinlock and set the
35 * flag that forces other procs who want the lock to wait. After
36 * this you can safely schedule with the lock held.
37 */
38void btrfs_set_lock_blocking(struct extent_buffer *eb)
925baedd 39{
b4ce94de
CM
40 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
41 set_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags);
42 spin_unlock(&eb->lock);
43 }
44 /* exit with the spin lock released and the bit set */
45}
f9efa9c7 46
b4ce94de
CM
47/*
48 * clearing the blocking flag will take the spinlock again.
49 * After this you can't safely schedule
50 */
51void btrfs_clear_lock_blocking(struct extent_buffer *eb)
52{
53 if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
54 spin_nested(eb);
55 clear_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags);
56 smp_mb__after_clear_bit();
57 }
58 /* exit with the spin lock held */
59}
60
61/*
62 * unfortunately, many of the places that currently set a lock to blocking
63 * don't end up blocking for every long, and often they don't block
64 * at all. For a dbench 50 run, if we don't spin one the blocking bit
65 * at all, the context switch rate can jump up to 400,000/sec or more.
66 *
67 * So, we're still stuck with this crummy spin on the blocking bit,
68 * at least until the most common causes of the short blocks
69 * can be dealt with.
70 */
71static int btrfs_spin_on_block(struct extent_buffer *eb)
72{
73 int i;
66d7e85e 74
f9efa9c7 75 for (i = 0; i < 512; i++) {
b4ce94de
CM
76 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
77 return 1;
78 if (need_resched())
79 break;
66d7e85e 80 cpu_relax();
b4ce94de
CM
81 }
82 return 0;
83}
84
85/*
86 * This is somewhat different from trylock. It will take the
87 * spinlock but if it finds the lock is set to blocking, it will
88 * return without the lock held.
89 *
90 * returns 1 if it was able to take the lock and zero otherwise
91 *
92 * After this call, scheduling is not safe without first calling
93 * btrfs_set_lock_blocking()
94 */
95int btrfs_try_spin_lock(struct extent_buffer *eb)
96{
97 int i;
98
99 spin_nested(eb);
100 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
101 return 1;
102 spin_unlock(&eb->lock);
103
104 /* spin for a bit on the BLOCKING flag */
105 for (i = 0; i < 2; i++) {
66d7e85e 106 cpu_relax();
b4ce94de
CM
107 if (!btrfs_spin_on_block(eb))
108 break;
109
110 spin_nested(eb);
111 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
112 return 1;
113 spin_unlock(&eb->lock);
114 }
115 return 0;
116}
117
118/*
119 * the autoremove wake function will return 0 if it tried to wake up
120 * a process that was already awake, which means that process won't
121 * count as an exclusive wakeup. The waitq code will continue waking
122 * procs until it finds one that was actually sleeping.
123 *
124 * For btrfs, this isn't quite what we want. We want a single proc
125 * to be notified that the lock is ready for taking. If that proc
126 * already happen to be awake, great, it will loop around and try for
127 * the lock.
128 *
129 * So, btrfs_wake_function always returns 1, even when the proc that we
130 * tried to wake up was already awake.
131 */
132static int btrfs_wake_function(wait_queue_t *wait, unsigned mode,
133 int sync, void *key)
134{
135 autoremove_wake_function(wait, mode, sync, key);
136 return 1;
137}
138
139/*
140 * returns with the extent buffer spinlocked.
141 *
142 * This will spin and/or wait as required to take the lock, and then
143 * return with the spinlock held.
144 *
145 * After this call, scheduling is not safe without first calling
146 * btrfs_set_lock_blocking()
147 */
148int btrfs_tree_lock(struct extent_buffer *eb)
149{
150 DEFINE_WAIT(wait);
151 wait.func = btrfs_wake_function;
152
66d7e85e
CM
153 if (!btrfs_spin_on_block(eb))
154 goto sleep;
155
b4ce94de
CM
156 while(1) {
157 spin_nested(eb);
158
159 /* nobody is blocking, exit with the spinlock held */
160 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
f9efa9c7 161 return 0;
b4ce94de
CM
162
163 /*
164 * we have the spinlock, but the real owner is blocking.
165 * wait for them
166 */
167 spin_unlock(&eb->lock);
168
169 /*
170 * spin for a bit, and if the blocking flag goes away,
171 * loop around
172 */
66d7e85e 173 cpu_relax();
b4ce94de
CM
174 if (btrfs_spin_on_block(eb))
175 continue;
66d7e85e 176sleep:
b4ce94de
CM
177 prepare_to_wait_exclusive(&eb->lock_wq, &wait,
178 TASK_UNINTERRUPTIBLE);
179
180 if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
181 schedule();
182
183 finish_wait(&eb->lock_wq, &wait);
f9efa9c7 184 }
925baedd
CM
185 return 0;
186}
187
b4ce94de
CM
188/*
189 * Very quick trylock, this does not spin or schedule. It returns
190 * 1 with the spinlock held if it was able to take the lock, or it
191 * returns zero if it was unable to take the lock.
192 *
193 * After this call, scheduling is not safe without first calling
194 * btrfs_set_lock_blocking()
195 */
925baedd
CM
196int btrfs_try_tree_lock(struct extent_buffer *eb)
197{
b4ce94de
CM
198 if (spin_trylock(&eb->lock)) {
199 if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
200 /*
201 * we've got the spinlock, but the real owner is
202 * blocking. Drop the spinlock and return failure
203 */
204 spin_unlock(&eb->lock);
205 return 0;
206 }
207 return 1;
208 }
209 /* someone else has the spinlock giveup */
210 return 0;
925baedd
CM
211}
212
213int btrfs_tree_unlock(struct extent_buffer *eb)
214{
b4ce94de
CM
215 /*
216 * if we were a blocking owner, we don't have the spinlock held
217 * just clear the bit and look for waiters
218 */
219 if (test_and_clear_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
220 smp_mb__after_clear_bit();
221 else
222 spin_unlock(&eb->lock);
223
224 if (waitqueue_active(&eb->lock_wq))
225 wake_up(&eb->lock_wq);
925baedd
CM
226 return 0;
227}
228
b9447ef8 229void btrfs_assert_tree_locked(struct extent_buffer *eb)
925baedd 230{
b9447ef8
CM
231 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
232 assert_spin_locked(&eb->lock);
925baedd 233}