Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
cf45b752 | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
d77d1b58 JP |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
b3b94faa DT |
12 | #include <linux/sched.h> |
13 | #include <linux/slab.h> | |
14 | #include <linux/spinlock.h> | |
b3b94faa DT |
15 | #include <linux/buffer_head.h> |
16 | #include <linux/delay.h> | |
17 | #include <linux/sort.h> | |
18 | #include <linux/jhash.h> | |
d0dc80db | 19 | #include <linux/kallsyms.h> |
5c676f6d | 20 | #include <linux/gfs2_ondisk.h> |
24264434 | 21 | #include <linux/list.h> |
fee852e3 | 22 | #include <linux/wait.h> |
95d97b7d | 23 | #include <linux/module.h> |
7c0f6ba6 | 24 | #include <linux/uaccess.h> |
7c52b166 RP |
25 | #include <linux/seq_file.h> |
26 | #include <linux/debugfs.h> | |
8fbbfd21 SW |
27 | #include <linux/kthread.h> |
28 | #include <linux/freezer.h> | |
c4f68a13 BM |
29 | #include <linux/workqueue.h> |
30 | #include <linux/jiffies.h> | |
bc015cb8 SW |
31 | #include <linux/rcupdate.h> |
32 | #include <linux/rculist_bl.h> | |
33 | #include <linux/bit_spinlock.h> | |
a245769f | 34 | #include <linux/percpu.h> |
4506a519 | 35 | #include <linux/list_sort.h> |
e66cf161 | 36 | #include <linux/lockref.h> |
88ffbf3e | 37 | #include <linux/rhashtable.h> |
b3b94faa DT |
38 | |
39 | #include "gfs2.h" | |
5c676f6d | 40 | #include "incore.h" |
b3b94faa DT |
41 | #include "glock.h" |
42 | #include "glops.h" | |
43 | #include "inode.h" | |
b3b94faa DT |
44 | #include "lops.h" |
45 | #include "meta_io.h" | |
46 | #include "quota.h" | |
47 | #include "super.h" | |
5c676f6d | 48 | #include "util.h" |
813e0c46 | 49 | #include "bmap.h" |
63997775 SW |
50 | #define CREATE_TRACE_POINTS |
51 | #include "trace_gfs2.h" | |
b3b94faa | 52 | |
6802e340 | 53 | struct gfs2_glock_iter { |
ba1ddcb6 | 54 | struct gfs2_sbd *sdp; /* incore superblock */ |
88ffbf3e | 55 | struct rhashtable_iter hti; /* rhashtable iterator */ |
ba1ddcb6 SW |
56 | struct gfs2_glock *gl; /* current glock struct */ |
57 | loff_t last_pos; /* last position */ | |
7c52b166 RP |
58 | }; |
59 | ||
b3b94faa DT |
60 | typedef void (*glock_examiner) (struct gfs2_glock * gl); |
61 | ||
6802e340 | 62 | static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target); |
c4f68a13 | 63 | |
7c52b166 | 64 | static struct dentry *gfs2_root; |
c4f68a13 | 65 | static struct workqueue_struct *glock_workqueue; |
b94a170e | 66 | struct workqueue_struct *gfs2_delete_workqueue; |
97cc1025 SW |
67 | static LIST_HEAD(lru_list); |
68 | static atomic_t lru_count = ATOMIC_INIT(0); | |
eb8374e7 | 69 | static DEFINE_SPINLOCK(lru_lock); |
08bc2dbc | 70 | |
b6397893 | 71 | #define GFS2_GL_HASH_SHIFT 15 |
47a9a527 | 72 | #define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT) |
087efdd3 | 73 | |
88ffbf3e BP |
74 | static struct rhashtable_params ht_parms = { |
75 | .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4, | |
972b044e | 76 | .key_len = offsetofend(struct lm_lockname, ln_type), |
88ffbf3e BP |
77 | .key_offset = offsetof(struct gfs2_glock, gl_name), |
78 | .head_offset = offsetof(struct gfs2_glock, gl_node), | |
79 | }; | |
b3b94faa | 80 | |
88ffbf3e | 81 | static struct rhashtable gl_hash_table; |
b3b94faa | 82 | |
961ae1d8 | 83 | static void gfs2_glock_dealloc(struct rcu_head *rcu) |
b3b94faa | 84 | { |
961ae1d8 | 85 | struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu); |
b3b94faa | 86 | |
dba2d70c | 87 | if (gl->gl_ops->go_flags & GLOF_ASPACE) { |
bc015cb8 | 88 | kmem_cache_free(gfs2_glock_aspace_cachep, gl); |
dba2d70c | 89 | } else { |
4e2f8849 | 90 | kfree(gl->gl_lksb.sb_lvbptr); |
bc015cb8 | 91 | kmem_cache_free(gfs2_glock_cachep, gl); |
dba2d70c | 92 | } |
961ae1d8 AG |
93 | } |
94 | ||
95 | void gfs2_glock_free(struct gfs2_glock *gl) | |
96 | { | |
97 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; | |
98 | ||
99 | call_rcu(&gl->gl_rcu, gfs2_glock_dealloc); | |
bc015cb8 SW |
100 | if (atomic_dec_and_test(&sdp->sd_glock_disposal)) |
101 | wake_up(&sdp->sd_glock_wait); | |
b3b94faa DT |
102 | } |
103 | ||
104 | /** | |
105 | * gfs2_glock_hold() - increment reference count on glock | |
106 | * @gl: The glock to hold | |
107 | * | |
108 | */ | |
109 | ||
e66cf161 | 110 | static void gfs2_glock_hold(struct gfs2_glock *gl) |
b3b94faa | 111 | { |
e66cf161 SW |
112 | GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref)); |
113 | lockref_get(&gl->gl_lockref); | |
b3b94faa DT |
114 | } |
115 | ||
8ff22a6f BM |
116 | /** |
117 | * demote_ok - Check to see if it's ok to unlock a glock | |
118 | * @gl: the glock | |
119 | * | |
120 | * Returns: 1 if it's ok | |
121 | */ | |
122 | ||
123 | static int demote_ok(const struct gfs2_glock *gl) | |
124 | { | |
125 | const struct gfs2_glock_operations *glops = gl->gl_ops; | |
126 | ||
127 | if (gl->gl_state == LM_ST_UNLOCKED) | |
128 | return 0; | |
f42ab085 | 129 | if (!list_empty(&gl->gl_holders)) |
8ff22a6f BM |
130 | return 0; |
131 | if (glops->go_demote_ok) | |
132 | return glops->go_demote_ok(gl); | |
133 | return 1; | |
134 | } | |
135 | ||
bc015cb8 | 136 | |
29687a2a SW |
137 | void gfs2_glock_add_to_lru(struct gfs2_glock *gl) |
138 | { | |
139 | spin_lock(&lru_lock); | |
140 | ||
141 | if (!list_empty(&gl->gl_lru)) | |
142 | list_del_init(&gl->gl_lru); | |
143 | else | |
144 | atomic_inc(&lru_count); | |
145 | ||
146 | list_add_tail(&gl->gl_lru, &lru_list); | |
627c10b7 | 147 | set_bit(GLF_LRU, &gl->gl_flags); |
29687a2a SW |
148 | spin_unlock(&lru_lock); |
149 | } | |
150 | ||
8f6cb409 | 151 | static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl) |
f42ab085 | 152 | { |
645ebd49 BP |
153 | if (!(gl->gl_ops->go_flags & GLOF_LRU)) |
154 | return; | |
155 | ||
8f6cb409 | 156 | spin_lock(&lru_lock); |
f42ab085 SW |
157 | if (!list_empty(&gl->gl_lru)) { |
158 | list_del_init(&gl->gl_lru); | |
159 | atomic_dec(&lru_count); | |
160 | clear_bit(GLF_LRU, &gl->gl_flags); | |
161 | } | |
162 | spin_unlock(&lru_lock); | |
163 | } | |
164 | ||
6b0c7440 AG |
165 | /* |
166 | * Enqueue the glock on the work queue. Passes one glock reference on to the | |
167 | * work queue. | |
b3b94faa | 168 | */ |
6b0c7440 AG |
169 | static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) { |
170 | if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) { | |
171 | /* | |
172 | * We are holding the lockref spinlock, and the work was still | |
173 | * queued above. The queued work (glock_work_func) takes that | |
174 | * spinlock before dropping its glock reference(s), so it | |
175 | * cannot have dropped them in the meantime. | |
176 | */ | |
177 | GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2); | |
178 | gl->gl_lockref.count--; | |
179 | } | |
180 | } | |
b3b94faa | 181 | |
6b0c7440 AG |
182 | static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) { |
183 | spin_lock(&gl->gl_lockref.lock); | |
184 | __gfs2_glock_queue_work(gl, delay); | |
185 | spin_unlock(&gl->gl_lockref.lock); | |
186 | } | |
187 | ||
188 | static void __gfs2_glock_put(struct gfs2_glock *gl) | |
b3b94faa | 189 | { |
15562c43 | 190 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
bc015cb8 | 191 | struct address_space *mapping = gfs2_glock2aspace(gl); |
b3b94faa | 192 | |
e66cf161 SW |
193 | lockref_mark_dead(&gl->gl_lockref); |
194 | ||
8f6cb409 | 195 | gfs2_glock_remove_from_lru(gl); |
e66cf161 | 196 | spin_unlock(&gl->gl_lockref.lock); |
88ffbf3e | 197 | rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms); |
e66cf161 SW |
198 | GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders)); |
199 | GLOCK_BUG_ON(gl, mapping && mapping->nrpages); | |
200 | trace_gfs2_glock_put(gl); | |
201 | sdp->sd_lockstruct.ls_ops->lm_put_lock(gl); | |
b3b94faa DT |
202 | } |
203 | ||
6b0c7440 AG |
204 | /** |
205 | * gfs2_glock_put() - Decrement reference count on glock | |
206 | * @gl: The glock to put | |
207 | * | |
208 | */ | |
209 | ||
210 | void gfs2_glock_put(struct gfs2_glock *gl) | |
211 | { | |
212 | if (lockref_put_or_lock(&gl->gl_lockref)) | |
213 | return; | |
214 | ||
215 | __gfs2_glock_put(gl); | |
216 | } | |
217 | ||
6802e340 SW |
218 | /** |
219 | * may_grant - check if its ok to grant a new lock | |
220 | * @gl: The glock | |
221 | * @gh: The lock request which we wish to grant | |
222 | * | |
223 | * Returns: true if its ok to grant the lock | |
224 | */ | |
225 | ||
226 | static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh) | |
227 | { | |
228 | const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list); | |
229 | if ((gh->gh_state == LM_ST_EXCLUSIVE || | |
230 | gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head) | |
231 | return 0; | |
232 | if (gl->gl_state == gh->gh_state) | |
233 | return 1; | |
234 | if (gh->gh_flags & GL_EXACT) | |
235 | return 0; | |
209806ab SW |
236 | if (gl->gl_state == LM_ST_EXCLUSIVE) { |
237 | if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED) | |
238 | return 1; | |
239 | if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED) | |
240 | return 1; | |
241 | } | |
6802e340 SW |
242 | if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY)) |
243 | return 1; | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static void gfs2_holder_wake(struct gfs2_holder *gh) | |
248 | { | |
249 | clear_bit(HIF_WAIT, &gh->gh_iflags); | |
4e857c58 | 250 | smp_mb__after_atomic(); |
6802e340 SW |
251 | wake_up_bit(&gh->gh_iflags, HIF_WAIT); |
252 | } | |
253 | ||
d5341a92 SW |
254 | /** |
255 | * do_error - Something unexpected has happened during a lock request | |
256 | * | |
257 | */ | |
258 | ||
a527b38e | 259 | static void do_error(struct gfs2_glock *gl, const int ret) |
d5341a92 SW |
260 | { |
261 | struct gfs2_holder *gh, *tmp; | |
262 | ||
263 | list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) { | |
264 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) | |
265 | continue; | |
266 | if (ret & LM_OUT_ERROR) | |
267 | gh->gh_error = -EIO; | |
268 | else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) | |
269 | gh->gh_error = GLR_TRYFAILED; | |
270 | else | |
271 | continue; | |
272 | list_del_init(&gh->gh_list); | |
273 | trace_gfs2_glock_queue(gh, 0); | |
274 | gfs2_holder_wake(gh); | |
275 | } | |
276 | } | |
277 | ||
6802e340 SW |
278 | /** |
279 | * do_promote - promote as many requests as possible on the current queue | |
280 | * @gl: The glock | |
281 | * | |
813e0c46 SW |
282 | * Returns: 1 if there is a blocked holder at the head of the list, or 2 |
283 | * if a type specific operation is underway. | |
6802e340 SW |
284 | */ |
285 | ||
286 | static int do_promote(struct gfs2_glock *gl) | |
f3dd1649 AG |
287 | __releases(&gl->gl_lockref.lock) |
288 | __acquires(&gl->gl_lockref.lock) | |
6802e340 SW |
289 | { |
290 | const struct gfs2_glock_operations *glops = gl->gl_ops; | |
291 | struct gfs2_holder *gh, *tmp; | |
292 | int ret; | |
293 | ||
294 | restart: | |
295 | list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) { | |
296 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) | |
297 | continue; | |
298 | if (may_grant(gl, gh)) { | |
299 | if (gh->gh_list.prev == &gl->gl_holders && | |
300 | glops->go_lock) { | |
f3dd1649 | 301 | spin_unlock(&gl->gl_lockref.lock); |
6802e340 SW |
302 | /* FIXME: eliminate this eventually */ |
303 | ret = glops->go_lock(gh); | |
f3dd1649 | 304 | spin_lock(&gl->gl_lockref.lock); |
6802e340 | 305 | if (ret) { |
813e0c46 SW |
306 | if (ret == 1) |
307 | return 2; | |
6802e340 SW |
308 | gh->gh_error = ret; |
309 | list_del_init(&gh->gh_list); | |
63997775 | 310 | trace_gfs2_glock_queue(gh, 0); |
6802e340 SW |
311 | gfs2_holder_wake(gh); |
312 | goto restart; | |
313 | } | |
314 | set_bit(HIF_HOLDER, &gh->gh_iflags); | |
63997775 | 315 | trace_gfs2_promote(gh, 1); |
6802e340 SW |
316 | gfs2_holder_wake(gh); |
317 | goto restart; | |
318 | } | |
319 | set_bit(HIF_HOLDER, &gh->gh_iflags); | |
63997775 | 320 | trace_gfs2_promote(gh, 0); |
6802e340 SW |
321 | gfs2_holder_wake(gh); |
322 | continue; | |
323 | } | |
324 | if (gh->gh_list.prev == &gl->gl_holders) | |
325 | return 1; | |
d5341a92 | 326 | do_error(gl, 0); |
6802e340 SW |
327 | break; |
328 | } | |
329 | return 0; | |
330 | } | |
331 | ||
6802e340 SW |
332 | /** |
333 | * find_first_waiter - find the first gh that's waiting for the glock | |
334 | * @gl: the glock | |
335 | */ | |
336 | ||
337 | static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl) | |
338 | { | |
339 | struct gfs2_holder *gh; | |
340 | ||
341 | list_for_each_entry(gh, &gl->gl_holders, gh_list) { | |
342 | if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) | |
343 | return gh; | |
344 | } | |
345 | return NULL; | |
346 | } | |
347 | ||
348 | /** | |
349 | * state_change - record that the glock is now in a different state | |
350 | * @gl: the glock | |
351 | * @new_state the new state | |
352 | * | |
353 | */ | |
354 | ||
355 | static void state_change(struct gfs2_glock *gl, unsigned int new_state) | |
356 | { | |
357 | int held1, held2; | |
358 | ||
359 | held1 = (gl->gl_state != LM_ST_UNLOCKED); | |
360 | held2 = (new_state != LM_ST_UNLOCKED); | |
361 | ||
362 | if (held1 != held2) { | |
e66cf161 | 363 | GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref)); |
6802e340 | 364 | if (held2) |
e66cf161 | 365 | gl->gl_lockref.count++; |
6802e340 | 366 | else |
e66cf161 | 367 | gl->gl_lockref.count--; |
6802e340 | 368 | } |
7b5e3d5f SW |
369 | if (held1 && held2 && list_empty(&gl->gl_holders)) |
370 | clear_bit(GLF_QUEUED, &gl->gl_flags); | |
6802e340 | 371 | |
7cf8dcd3 BP |
372 | if (new_state != gl->gl_target) |
373 | /* shorten our minimum hold time */ | |
374 | gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR, | |
375 | GL_GLOCK_MIN_HOLD); | |
6802e340 SW |
376 | gl->gl_state = new_state; |
377 | gl->gl_tchange = jiffies; | |
378 | } | |
379 | ||
380 | static void gfs2_demote_wake(struct gfs2_glock *gl) | |
381 | { | |
382 | gl->gl_demote_state = LM_ST_EXCLUSIVE; | |
383 | clear_bit(GLF_DEMOTE, &gl->gl_flags); | |
4e857c58 | 384 | smp_mb__after_atomic(); |
6802e340 SW |
385 | wake_up_bit(&gl->gl_flags, GLF_DEMOTE); |
386 | } | |
387 | ||
388 | /** | |
389 | * finish_xmote - The DLM has replied to one of our lock requests | |
390 | * @gl: The glock | |
391 | * @ret: The status from the DLM | |
392 | * | |
393 | */ | |
394 | ||
395 | static void finish_xmote(struct gfs2_glock *gl, unsigned int ret) | |
396 | { | |
397 | const struct gfs2_glock_operations *glops = gl->gl_ops; | |
398 | struct gfs2_holder *gh; | |
399 | unsigned state = ret & LM_OUT_ST_MASK; | |
813e0c46 | 400 | int rv; |
6802e340 | 401 | |
f3dd1649 | 402 | spin_lock(&gl->gl_lockref.lock); |
63997775 | 403 | trace_gfs2_glock_state_change(gl, state); |
6802e340 SW |
404 | state_change(gl, state); |
405 | gh = find_first_waiter(gl); | |
406 | ||
407 | /* Demote to UN request arrived during demote to SH or DF */ | |
408 | if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) && | |
409 | state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED) | |
410 | gl->gl_target = LM_ST_UNLOCKED; | |
411 | ||
412 | /* Check for state != intended state */ | |
413 | if (unlikely(state != gl->gl_target)) { | |
414 | if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) { | |
415 | /* move to back of queue and try next entry */ | |
416 | if (ret & LM_OUT_CANCELED) { | |
417 | if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0) | |
418 | list_move_tail(&gh->gh_list, &gl->gl_holders); | |
419 | gh = find_first_waiter(gl); | |
420 | gl->gl_target = gh->gh_state; | |
421 | goto retry; | |
422 | } | |
423 | /* Some error or failed "try lock" - report it */ | |
424 | if ((ret & LM_OUT_ERROR) || | |
425 | (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) { | |
426 | gl->gl_target = gl->gl_state; | |
427 | do_error(gl, ret); | |
428 | goto out; | |
429 | } | |
430 | } | |
431 | switch(state) { | |
432 | /* Unlocked due to conversion deadlock, try again */ | |
433 | case LM_ST_UNLOCKED: | |
434 | retry: | |
435 | do_xmote(gl, gh, gl->gl_target); | |
436 | break; | |
437 | /* Conversion fails, unlock and try again */ | |
438 | case LM_ST_SHARED: | |
439 | case LM_ST_DEFERRED: | |
440 | do_xmote(gl, gh, LM_ST_UNLOCKED); | |
441 | break; | |
442 | default: /* Everything else */ | |
d77d1b58 | 443 | pr_err("wanted %u got %u\n", gl->gl_target, state); |
6802e340 SW |
444 | GLOCK_BUG_ON(gl, 1); |
445 | } | |
f3dd1649 | 446 | spin_unlock(&gl->gl_lockref.lock); |
6802e340 SW |
447 | return; |
448 | } | |
449 | ||
450 | /* Fast path - we got what we asked for */ | |
451 | if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) | |
452 | gfs2_demote_wake(gl); | |
453 | if (state != LM_ST_UNLOCKED) { | |
454 | if (glops->go_xmote_bh) { | |
f3dd1649 | 455 | spin_unlock(&gl->gl_lockref.lock); |
6802e340 | 456 | rv = glops->go_xmote_bh(gl, gh); |
f3dd1649 | 457 | spin_lock(&gl->gl_lockref.lock); |
6802e340 SW |
458 | if (rv) { |
459 | do_error(gl, rv); | |
460 | goto out; | |
461 | } | |
462 | } | |
813e0c46 SW |
463 | rv = do_promote(gl); |
464 | if (rv == 2) | |
465 | goto out_locked; | |
6802e340 SW |
466 | } |
467 | out: | |
468 | clear_bit(GLF_LOCK, &gl->gl_flags); | |
813e0c46 | 469 | out_locked: |
f3dd1649 | 470 | spin_unlock(&gl->gl_lockref.lock); |
6802e340 SW |
471 | } |
472 | ||
6802e340 SW |
473 | /** |
474 | * do_xmote - Calls the DLM to change the state of a lock | |
475 | * @gl: The lock state | |
476 | * @gh: The holder (only for promotes) | |
477 | * @target: The target lock state | |
478 | * | |
479 | */ | |
480 | ||
481 | static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target) | |
f3dd1649 AG |
482 | __releases(&gl->gl_lockref.lock) |
483 | __acquires(&gl->gl_lockref.lock) | |
6802e340 SW |
484 | { |
485 | const struct gfs2_glock_operations *glops = gl->gl_ops; | |
15562c43 | 486 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
b58bf407 | 487 | unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0); |
6802e340 SW |
488 | int ret; |
489 | ||
ed17545d BP |
490 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) && |
491 | target != LM_ST_UNLOCKED) | |
0d1c7ae9 | 492 | return; |
6802e340 SW |
493 | lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP | |
494 | LM_FLAG_PRIORITY); | |
921169ca SW |
495 | GLOCK_BUG_ON(gl, gl->gl_state == target); |
496 | GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target); | |
6802e340 SW |
497 | if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) && |
498 | glops->go_inval) { | |
499 | set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); | |
500 | do_error(gl, 0); /* Fail queued try locks */ | |
501 | } | |
47a25380 | 502 | gl->gl_req = target; |
a245769f SW |
503 | set_bit(GLF_BLOCKING, &gl->gl_flags); |
504 | if ((gl->gl_req == LM_ST_UNLOCKED) || | |
505 | (gl->gl_state == LM_ST_EXCLUSIVE) || | |
506 | (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB))) | |
507 | clear_bit(GLF_BLOCKING, &gl->gl_flags); | |
f3dd1649 | 508 | spin_unlock(&gl->gl_lockref.lock); |
06dfc306 BP |
509 | if (glops->go_sync) |
510 | glops->go_sync(gl); | |
6802e340 SW |
511 | if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) |
512 | glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA); | |
513 | clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); | |
514 | ||
515 | gfs2_glock_hold(gl); | |
921169ca SW |
516 | if (sdp->sd_lockstruct.ls_ops->lm_lock) { |
517 | /* lock_dlm */ | |
518 | ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags); | |
3e11e530 BM |
519 | if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED && |
520 | target == LM_ST_UNLOCKED && | |
521 | test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) { | |
522 | finish_xmote(gl, target); | |
6b0c7440 | 523 | gfs2_glock_queue_work(gl, 0); |
3e11e530 BM |
524 | } |
525 | else if (ret) { | |
d77d1b58 | 526 | pr_err("lm_lock ret %d\n", ret); |
ed17545d BP |
527 | GLOCK_BUG_ON(gl, !test_bit(SDF_SHUTDOWN, |
528 | &sdp->sd_flags)); | |
dba2d70c | 529 | } |
921169ca SW |
530 | } else { /* lock_nolock */ |
531 | finish_xmote(gl, target); | |
6b0c7440 | 532 | gfs2_glock_queue_work(gl, 0); |
6802e340 | 533 | } |
921169ca | 534 | |
f3dd1649 | 535 | spin_lock(&gl->gl_lockref.lock); |
6802e340 SW |
536 | } |
537 | ||
538 | /** | |
539 | * find_first_holder - find the first "holder" gh | |
540 | * @gl: the glock | |
541 | */ | |
542 | ||
543 | static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl) | |
544 | { | |
545 | struct gfs2_holder *gh; | |
546 | ||
547 | if (!list_empty(&gl->gl_holders)) { | |
548 | gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list); | |
549 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) | |
550 | return gh; | |
551 | } | |
552 | return NULL; | |
553 | } | |
554 | ||
555 | /** | |
556 | * run_queue - do all outstanding tasks related to a glock | |
557 | * @gl: The glock in question | |
558 | * @nonblock: True if we must not block in run_queue | |
559 | * | |
560 | */ | |
561 | ||
562 | static void run_queue(struct gfs2_glock *gl, const int nonblock) | |
f3dd1649 AG |
563 | __releases(&gl->gl_lockref.lock) |
564 | __acquires(&gl->gl_lockref.lock) | |
6802e340 SW |
565 | { |
566 | struct gfs2_holder *gh = NULL; | |
813e0c46 | 567 | int ret; |
6802e340 SW |
568 | |
569 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) | |
570 | return; | |
571 | ||
572 | GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)); | |
573 | ||
574 | if (test_bit(GLF_DEMOTE, &gl->gl_flags) && | |
575 | gl->gl_demote_state != gl->gl_state) { | |
576 | if (find_first_holder(gl)) | |
d8348de0 | 577 | goto out_unlock; |
6802e340 SW |
578 | if (nonblock) |
579 | goto out_sched; | |
580 | set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags); | |
265d529c | 581 | GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE); |
6802e340 SW |
582 | gl->gl_target = gl->gl_demote_state; |
583 | } else { | |
584 | if (test_bit(GLF_DEMOTE, &gl->gl_flags)) | |
585 | gfs2_demote_wake(gl); | |
813e0c46 SW |
586 | ret = do_promote(gl); |
587 | if (ret == 0) | |
d8348de0 | 588 | goto out_unlock; |
813e0c46 | 589 | if (ret == 2) |
a228df63 | 590 | goto out; |
6802e340 SW |
591 | gh = find_first_waiter(gl); |
592 | gl->gl_target = gh->gh_state; | |
593 | if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) | |
594 | do_error(gl, 0); /* Fail queued try locks */ | |
595 | } | |
596 | do_xmote(gl, gh, gl->gl_target); | |
a228df63 | 597 | out: |
6802e340 SW |
598 | return; |
599 | ||
600 | out_sched: | |
7e71c55e | 601 | clear_bit(GLF_LOCK, &gl->gl_flags); |
4e857c58 | 602 | smp_mb__after_atomic(); |
e66cf161 | 603 | gl->gl_lockref.count++; |
6b0c7440 | 604 | __gfs2_glock_queue_work(gl, 0); |
7e71c55e SW |
605 | return; |
606 | ||
d8348de0 | 607 | out_unlock: |
6802e340 | 608 | clear_bit(GLF_LOCK, &gl->gl_flags); |
4e857c58 | 609 | smp_mb__after_atomic(); |
7e71c55e | 610 | return; |
6802e340 SW |
611 | } |
612 | ||
b94a170e BM |
613 | static void delete_work_func(struct work_struct *work) |
614 | { | |
615 | struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete); | |
15562c43 | 616 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
ec5ec66b | 617 | struct inode *inode; |
044b9414 SW |
618 | u64 no_addr = gl->gl_name.ln_number; |
619 | ||
a4923865 BP |
620 | /* If someone's using this glock to create a new dinode, the block must |
621 | have been freed by another node, then re-used, in which case our | |
622 | iopen callback is too late after the fact. Ignore it. */ | |
623 | if (test_bit(GLF_INODE_CREATING, &gl->gl_flags)) | |
624 | goto out; | |
625 | ||
ec5ec66b | 626 | inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED); |
044b9414 SW |
627 | if (inode && !IS_ERR(inode)) { |
628 | d_prune_aliases(inode); | |
629 | iput(inode); | |
b94a170e | 630 | } |
a4923865 | 631 | out: |
b94a170e BM |
632 | gfs2_glock_put(gl); |
633 | } | |
634 | ||
c4f68a13 BM |
635 | static void glock_work_func(struct work_struct *work) |
636 | { | |
6802e340 | 637 | unsigned long delay = 0; |
c4f68a13 | 638 | struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work); |
6b0c7440 | 639 | unsigned int drop_refs = 1; |
c4f68a13 | 640 | |
26bb7505 | 641 | if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) { |
6802e340 | 642 | finish_xmote(gl, gl->gl_reply); |
6b0c7440 | 643 | drop_refs++; |
26bb7505 | 644 | } |
f3dd1649 | 645 | spin_lock(&gl->gl_lockref.lock); |
f90e5b5b | 646 | if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && |
265d529c SW |
647 | gl->gl_state != LM_ST_UNLOCKED && |
648 | gl->gl_demote_state != LM_ST_EXCLUSIVE) { | |
6802e340 | 649 | unsigned long holdtime, now = jiffies; |
f90e5b5b | 650 | |
7cf8dcd3 | 651 | holdtime = gl->gl_tchange + gl->gl_hold_time; |
6802e340 SW |
652 | if (time_before(now, holdtime)) |
653 | delay = holdtime - now; | |
f90e5b5b BP |
654 | |
655 | if (!delay) { | |
656 | clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags); | |
657 | set_bit(GLF_DEMOTE, &gl->gl_flags); | |
658 | } | |
6802e340 SW |
659 | } |
660 | run_queue(gl, 0); | |
6b0c7440 AG |
661 | if (delay) { |
662 | /* Keep one glock reference for the work we requeue. */ | |
663 | drop_refs--; | |
7cf8dcd3 BP |
664 | if (gl->gl_name.ln_type != LM_TYPE_INODE) |
665 | delay = 0; | |
6b0c7440 | 666 | __gfs2_glock_queue_work(gl, delay); |
7cf8dcd3 | 667 | } |
6b0c7440 AG |
668 | |
669 | /* | |
670 | * Drop the remaining glock references manually here. (Mind that | |
671 | * __gfs2_glock_queue_work depends on the lockref spinlock begin held | |
672 | * here as well.) | |
673 | */ | |
674 | gl->gl_lockref.count -= drop_refs; | |
675 | if (!gl->gl_lockref.count) { | |
676 | __gfs2_glock_put(gl); | |
677 | return; | |
678 | } | |
679 | spin_unlock(&gl->gl_lockref.lock); | |
c4f68a13 BM |
680 | } |
681 | ||
b3b94faa DT |
682 | /** |
683 | * gfs2_glock_get() - Get a glock, or create one if one doesn't exist | |
684 | * @sdp: The GFS2 superblock | |
685 | * @number: the lock number | |
686 | * @glops: The glock_operations to use | |
687 | * @create: If 0, don't create the glock if it doesn't exist | |
688 | * @glp: the glock is returned here | |
689 | * | |
690 | * This does not lock a glock, just finds/creates structures for one. | |
691 | * | |
692 | * Returns: errno | |
693 | */ | |
694 | ||
cd915493 | 695 | int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number, |
8fb4b536 | 696 | const struct gfs2_glock_operations *glops, int create, |
b3b94faa DT |
697 | struct gfs2_glock **glp) |
698 | { | |
009d8518 | 699 | struct super_block *s = sdp->sd_vfs; |
15562c43 BP |
700 | struct lm_lockname name = { .ln_number = number, |
701 | .ln_type = glops->go_type, | |
702 | .ln_sbd = sdp }; | |
0a52aba7 | 703 | struct gfs2_glock *gl, *tmp; |
009d8518 | 704 | struct address_space *mapping; |
bc015cb8 | 705 | struct kmem_cache *cachep; |
0a52aba7 | 706 | int ret = 0; |
b3b94faa | 707 | |
f38e5fb9 | 708 | rcu_read_lock(); |
88ffbf3e BP |
709 | gl = rhashtable_lookup_fast(&gl_hash_table, &name, ht_parms); |
710 | if (gl && !lockref_get_not_dead(&gl->gl_lockref)) | |
711 | gl = NULL; | |
f38e5fb9 | 712 | rcu_read_unlock(); |
b3b94faa | 713 | |
64d576ba SW |
714 | *glp = gl; |
715 | if (gl) | |
b3b94faa | 716 | return 0; |
64d576ba SW |
717 | if (!create) |
718 | return -ENOENT; | |
b3b94faa | 719 | |
009d8518 | 720 | if (glops->go_flags & GLOF_ASPACE) |
bc015cb8 | 721 | cachep = gfs2_glock_aspace_cachep; |
009d8518 | 722 | else |
bc015cb8 | 723 | cachep = gfs2_glock_cachep; |
fe0bbd29 | 724 | gl = kmem_cache_alloc(cachep, GFP_NOFS); |
b3b94faa DT |
725 | if (!gl) |
726 | return -ENOMEM; | |
727 | ||
dba2d70c | 728 | memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb)); |
dba2d70c DT |
729 | |
730 | if (glops->go_flags & GLOF_LVB) { | |
fe0bbd29 | 731 | gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS); |
4e2f8849 | 732 | if (!gl->gl_lksb.sb_lvbptr) { |
dba2d70c DT |
733 | kmem_cache_free(cachep, gl); |
734 | return -ENOMEM; | |
735 | } | |
dba2d70c DT |
736 | } |
737 | ||
8f05228e | 738 | atomic_inc(&sdp->sd_glock_disposal); |
88ffbf3e | 739 | gl->gl_node.next = NULL; |
ec45d9f5 | 740 | gl->gl_flags = 0; |
b3b94faa | 741 | gl->gl_name = name; |
e66cf161 | 742 | gl->gl_lockref.count = 1; |
b3b94faa | 743 | gl->gl_state = LM_ST_UNLOCKED; |
6802e340 | 744 | gl->gl_target = LM_ST_UNLOCKED; |
c4f68a13 | 745 | gl->gl_demote_state = LM_ST_EXCLUSIVE; |
b3b94faa | 746 | gl->gl_ops = glops; |
8b0e1953 | 747 | gl->gl_dstamp = 0; |
a245769f SW |
748 | preempt_disable(); |
749 | /* We use the global stats to estimate the initial per-glock stats */ | |
750 | gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type]; | |
751 | preempt_enable(); | |
752 | gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0; | |
753 | gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0; | |
c4f68a13 | 754 | gl->gl_tchange = jiffies; |
ec45d9f5 | 755 | gl->gl_object = NULL; |
7cf8dcd3 | 756 | gl->gl_hold_time = GL_GLOCK_DFT_HOLD; |
c4f68a13 | 757 | INIT_DELAYED_WORK(&gl->gl_work, glock_work_func); |
b94a170e | 758 | INIT_WORK(&gl->gl_delete, delete_work_func); |
b3b94faa | 759 | |
009d8518 SW |
760 | mapping = gfs2_glock2aspace(gl); |
761 | if (mapping) { | |
762 | mapping->a_ops = &gfs2_meta_aops; | |
763 | mapping->host = s->s_bdev->bd_inode; | |
764 | mapping->flags = 0; | |
765 | mapping_set_gfp_mask(mapping, GFP_NOFS); | |
252aa6f5 | 766 | mapping->private_data = NULL; |
009d8518 | 767 | mapping->writeback_index = 0; |
b3b94faa DT |
768 | } |
769 | ||
88ffbf3e | 770 | again: |
0a52aba7 AG |
771 | rcu_read_lock(); |
772 | tmp = rhashtable_lookup_get_insert_fast(&gl_hash_table, &gl->gl_node, | |
773 | ht_parms); | |
774 | if (!tmp) { | |
88ffbf3e | 775 | *glp = gl; |
0a52aba7 | 776 | goto out; |
b3b94faa | 777 | } |
0a52aba7 AG |
778 | if (IS_ERR(tmp)) { |
779 | ret = PTR_ERR(tmp); | |
780 | goto out_free; | |
781 | } | |
782 | if (lockref_get_not_dead(&tmp->gl_lockref)) { | |
783 | *glp = tmp; | |
784 | goto out_free; | |
88ffbf3e | 785 | } |
0a52aba7 AG |
786 | rcu_read_unlock(); |
787 | cond_resched(); | |
788 | goto again; | |
789 | ||
790 | out_free: | |
88ffbf3e BP |
791 | kfree(gl->gl_lksb.sb_lvbptr); |
792 | kmem_cache_free(cachep, gl); | |
793 | atomic_dec(&sdp->sd_glock_disposal); | |
b3b94faa | 794 | |
0a52aba7 AG |
795 | out: |
796 | rcu_read_unlock(); | |
88ffbf3e | 797 | return ret; |
b3b94faa DT |
798 | } |
799 | ||
800 | /** | |
801 | * gfs2_holder_init - initialize a struct gfs2_holder in the default way | |
802 | * @gl: the glock | |
803 | * @state: the state we're requesting | |
804 | * @flags: the modifier flags | |
805 | * @gh: the holder structure | |
806 | * | |
807 | */ | |
808 | ||
b58bf407 | 809 | void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags, |
b3b94faa DT |
810 | struct gfs2_holder *gh) |
811 | { | |
812 | INIT_LIST_HEAD(&gh->gh_list); | |
813 | gh->gh_gl = gl; | |
d29c0afe | 814 | gh->gh_ip = _RET_IP_; |
b1e058da | 815 | gh->gh_owner_pid = get_pid(task_pid(current)); |
b3b94faa DT |
816 | gh->gh_state = state; |
817 | gh->gh_flags = flags; | |
818 | gh->gh_error = 0; | |
819 | gh->gh_iflags = 0; | |
b3b94faa DT |
820 | gfs2_glock_hold(gl); |
821 | } | |
822 | ||
823 | /** | |
824 | * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it | |
825 | * @state: the state we're requesting | |
826 | * @flags: the modifier flags | |
827 | * @gh: the holder structure | |
828 | * | |
829 | * Don't mess with the glock. | |
830 | * | |
831 | */ | |
832 | ||
b58bf407 | 833 | void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh) |
b3b94faa DT |
834 | { |
835 | gh->gh_state = state; | |
579b78a4 | 836 | gh->gh_flags = flags; |
3b8249f6 | 837 | gh->gh_iflags = 0; |
d29c0afe | 838 | gh->gh_ip = _RET_IP_; |
30badc95 | 839 | put_pid(gh->gh_owner_pid); |
1a0eae88 | 840 | gh->gh_owner_pid = get_pid(task_pid(current)); |
b3b94faa DT |
841 | } |
842 | ||
843 | /** | |
844 | * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference) | |
845 | * @gh: the holder structure | |
846 | * | |
847 | */ | |
848 | ||
849 | void gfs2_holder_uninit(struct gfs2_holder *gh) | |
850 | { | |
b1e058da | 851 | put_pid(gh->gh_owner_pid); |
b3b94faa | 852 | gfs2_glock_put(gh->gh_gl); |
6df9f9a2 | 853 | gfs2_holder_mark_uninitialized(gh); |
d0dc80db | 854 | gh->gh_ip = 0; |
b3b94faa DT |
855 | } |
856 | ||
07a79049 BP |
857 | /** |
858 | * gfs2_glock_wait - wait on a glock acquisition | |
859 | * @gh: the glock holder | |
860 | * | |
861 | * Returns: 0 on success | |
862 | */ | |
863 | ||
864 | int gfs2_glock_wait(struct gfs2_holder *gh) | |
da755fdb | 865 | { |
7cf8dcd3 BP |
866 | unsigned long time1 = jiffies; |
867 | ||
6802e340 | 868 | might_sleep(); |
74316201 | 869 | wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE); |
7cf8dcd3 BP |
870 | if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */ |
871 | /* Lengthen the minimum hold time. */ | |
872 | gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time + | |
873 | GL_GLOCK_HOLD_INCR, | |
874 | GL_GLOCK_MAX_HOLD); | |
07a79049 | 875 | return gh->gh_error; |
da755fdb SW |
876 | } |
877 | ||
b3b94faa | 878 | /** |
6802e340 SW |
879 | * handle_callback - process a demote request |
880 | * @gl: the glock | |
881 | * @state: the state the caller wants us to change to | |
b3b94faa | 882 | * |
6802e340 SW |
883 | * There are only two requests that we are going to see in actual |
884 | * practise: LM_ST_SHARED and LM_ST_UNLOCKED | |
b3b94faa DT |
885 | */ |
886 | ||
6802e340 | 887 | static void handle_callback(struct gfs2_glock *gl, unsigned int state, |
81ffbf65 | 888 | unsigned long delay, bool remote) |
b3b94faa | 889 | { |
6802e340 | 890 | int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE; |
b3b94faa | 891 | |
6802e340 SW |
892 | set_bit(bit, &gl->gl_flags); |
893 | if (gl->gl_demote_state == LM_ST_EXCLUSIVE) { | |
894 | gl->gl_demote_state = state; | |
895 | gl->gl_demote_time = jiffies; | |
6802e340 SW |
896 | } else if (gl->gl_demote_state != LM_ST_UNLOCKED && |
897 | gl->gl_demote_state != state) { | |
898 | gl->gl_demote_state = LM_ST_UNLOCKED; | |
b3b94faa | 899 | } |
b94a170e | 900 | if (gl->gl_ops->go_callback) |
81ffbf65 | 901 | gl->gl_ops->go_callback(gl, remote); |
7bd8b2eb | 902 | trace_gfs2_demote_rq(gl, remote); |
b3b94faa DT |
903 | } |
904 | ||
6802e340 | 905 | void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...) |
7c52b166 | 906 | { |
5e69069c | 907 | struct va_format vaf; |
7c52b166 RP |
908 | va_list args; |
909 | ||
910 | va_start(args, fmt); | |
5e69069c | 911 | |
6802e340 | 912 | if (seq) { |
1bb49303 | 913 | seq_vprintf(seq, fmt, args); |
6802e340 | 914 | } else { |
5e69069c JP |
915 | vaf.fmt = fmt; |
916 | vaf.va = &args; | |
917 | ||
d77d1b58 | 918 | pr_err("%pV", &vaf); |
6802e340 | 919 | } |
5e69069c | 920 | |
7c52b166 RP |
921 | va_end(args); |
922 | } | |
923 | ||
b3b94faa DT |
924 | /** |
925 | * add_to_queue - Add a holder to the wait queue (but look for recursion) | |
926 | * @gh: the holder structure to add | |
927 | * | |
6802e340 SW |
928 | * Eventually we should move the recursive locking trap to a |
929 | * debugging option or something like that. This is the fast | |
930 | * path and needs to have the minimum number of distractions. | |
931 | * | |
b3b94faa DT |
932 | */ |
933 | ||
6802e340 | 934 | static inline void add_to_queue(struct gfs2_holder *gh) |
f3dd1649 AG |
935 | __releases(&gl->gl_lockref.lock) |
936 | __acquires(&gl->gl_lockref.lock) | |
b3b94faa DT |
937 | { |
938 | struct gfs2_glock *gl = gh->gh_gl; | |
15562c43 | 939 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
6802e340 SW |
940 | struct list_head *insert_pt = NULL; |
941 | struct gfs2_holder *gh2; | |
e5dc76b9 | 942 | int try_futile = 0; |
b3b94faa | 943 | |
b1e058da | 944 | BUG_ON(gh->gh_owner_pid == NULL); |
fee852e3 SW |
945 | if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags)) |
946 | BUG(); | |
190562bd | 947 | |
6802e340 SW |
948 | if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { |
949 | if (test_bit(GLF_LOCK, &gl->gl_flags)) | |
e5dc76b9 | 950 | try_futile = !may_grant(gl, gh); |
6802e340 SW |
951 | if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) |
952 | goto fail; | |
953 | } | |
954 | ||
955 | list_for_each_entry(gh2, &gl->gl_holders, gh_list) { | |
956 | if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid && | |
957 | (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK))) | |
958 | goto trap_recursive; | |
e5dc76b9 BP |
959 | if (try_futile && |
960 | !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) { | |
6802e340 SW |
961 | fail: |
962 | gh->gh_error = GLR_TRYFAILED; | |
963 | gfs2_holder_wake(gh); | |
964 | return; | |
b4c20166 | 965 | } |
6802e340 SW |
966 | if (test_bit(HIF_HOLDER, &gh2->gh_iflags)) |
967 | continue; | |
968 | if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt)) | |
969 | insert_pt = &gh2->gh_list; | |
970 | } | |
7b5e3d5f | 971 | set_bit(GLF_QUEUED, &gl->gl_flags); |
edae38a6 | 972 | trace_gfs2_glock_queue(gh, 1); |
a245769f SW |
973 | gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT); |
974 | gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT); | |
6802e340 SW |
975 | if (likely(insert_pt == NULL)) { |
976 | list_add_tail(&gh->gh_list, &gl->gl_holders); | |
977 | if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY)) | |
978 | goto do_cancel; | |
979 | return; | |
980 | } | |
981 | list_add_tail(&gh->gh_list, insert_pt); | |
982 | do_cancel: | |
983 | gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list); | |
984 | if (!(gh->gh_flags & LM_FLAG_PRIORITY)) { | |
f3dd1649 | 985 | spin_unlock(&gl->gl_lockref.lock); |
048bca22 | 986 | if (sdp->sd_lockstruct.ls_ops->lm_cancel) |
f057f6cd | 987 | sdp->sd_lockstruct.ls_ops->lm_cancel(gl); |
f3dd1649 | 988 | spin_lock(&gl->gl_lockref.lock); |
b3b94faa | 989 | } |
6802e340 | 990 | return; |
b3b94faa | 991 | |
6802e340 | 992 | trap_recursive: |
fc554ed3 FF |
993 | pr_err("original: %pSR\n", (void *)gh2->gh_ip); |
994 | pr_err("pid: %d\n", pid_nr(gh2->gh_owner_pid)); | |
995 | pr_err("lock type: %d req lock state : %d\n", | |
6802e340 | 996 | gh2->gh_gl->gl_name.ln_type, gh2->gh_state); |
fc554ed3 FF |
997 | pr_err("new: %pSR\n", (void *)gh->gh_ip); |
998 | pr_err("pid: %d\n", pid_nr(gh->gh_owner_pid)); | |
999 | pr_err("lock type: %d req lock state : %d\n", | |
6802e340 | 1000 | gh->gh_gl->gl_name.ln_type, gh->gh_state); |
8eae1ca0 | 1001 | gfs2_dump_glock(NULL, gl); |
6802e340 | 1002 | BUG(); |
b3b94faa DT |
1003 | } |
1004 | ||
1005 | /** | |
1006 | * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock) | |
1007 | * @gh: the holder structure | |
1008 | * | |
1009 | * if (gh->gh_flags & GL_ASYNC), this never returns an error | |
1010 | * | |
1011 | * Returns: 0, GLR_TRYFAILED, or errno on failure | |
1012 | */ | |
1013 | ||
1014 | int gfs2_glock_nq(struct gfs2_holder *gh) | |
1015 | { | |
1016 | struct gfs2_glock *gl = gh->gh_gl; | |
15562c43 | 1017 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
b3b94faa DT |
1018 | int error = 0; |
1019 | ||
6802e340 | 1020 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
b3b94faa | 1021 | return -EIO; |
b3b94faa | 1022 | |
f42ab085 SW |
1023 | if (test_bit(GLF_LRU, &gl->gl_flags)) |
1024 | gfs2_glock_remove_from_lru(gl); | |
1025 | ||
f3dd1649 | 1026 | spin_lock(&gl->gl_lockref.lock); |
b3b94faa | 1027 | add_to_queue(gh); |
01b172b7 BP |
1028 | if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) && |
1029 | test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) { | |
0809f6ec | 1030 | set_bit(GLF_REPLY_PENDING, &gl->gl_flags); |
01b172b7 | 1031 | gl->gl_lockref.count++; |
6b0c7440 | 1032 | __gfs2_glock_queue_work(gl, 0); |
01b172b7 | 1033 | } |
6802e340 | 1034 | run_queue(gl, 1); |
f3dd1649 | 1035 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa | 1036 | |
6802e340 SW |
1037 | if (!(gh->gh_flags & GL_ASYNC)) |
1038 | error = gfs2_glock_wait(gh); | |
b3b94faa | 1039 | |
b3b94faa DT |
1040 | return error; |
1041 | } | |
1042 | ||
1043 | /** | |
1044 | * gfs2_glock_poll - poll to see if an async request has been completed | |
1045 | * @gh: the holder | |
1046 | * | |
1047 | * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on | |
1048 | */ | |
1049 | ||
1050 | int gfs2_glock_poll(struct gfs2_holder *gh) | |
1051 | { | |
6802e340 | 1052 | return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1; |
b3b94faa DT |
1053 | } |
1054 | ||
1055 | /** | |
1056 | * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock) | |
1057 | * @gh: the glock holder | |
1058 | * | |
1059 | */ | |
1060 | ||
1061 | void gfs2_glock_dq(struct gfs2_holder *gh) | |
1062 | { | |
1063 | struct gfs2_glock *gl = gh->gh_gl; | |
8fb4b536 | 1064 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
c4f68a13 | 1065 | unsigned delay = 0; |
6802e340 | 1066 | int fast_path = 0; |
b3b94faa | 1067 | |
f3dd1649 | 1068 | spin_lock(&gl->gl_lockref.lock); |
b3b94faa | 1069 | if (gh->gh_flags & GL_NOCACHE) |
81ffbf65 | 1070 | handle_callback(gl, LM_ST_UNLOCKED, 0, false); |
b3b94faa | 1071 | |
b3b94faa | 1072 | list_del_init(&gh->gh_list); |
7508abc4 | 1073 | clear_bit(HIF_HOLDER, &gh->gh_iflags); |
6802e340 | 1074 | if (find_first_holder(gl) == NULL) { |
3042a2cc | 1075 | if (glops->go_unlock) { |
6802e340 | 1076 | GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags)); |
f3dd1649 | 1077 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa | 1078 | glops->go_unlock(gh); |
f3dd1649 | 1079 | spin_lock(&gl->gl_lockref.lock); |
6802e340 | 1080 | clear_bit(GLF_LOCK, &gl->gl_flags); |
3042a2cc | 1081 | } |
6802e340 SW |
1082 | if (list_empty(&gl->gl_holders) && |
1083 | !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && | |
1084 | !test_bit(GLF_DEMOTE, &gl->gl_flags)) | |
1085 | fast_path = 1; | |
b3b94faa | 1086 | } |
e7ccaf5f BP |
1087 | if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl) && |
1088 | (glops->go_flags & GLOF_LRU)) | |
4abb6ad9 BP |
1089 | gfs2_glock_add_to_lru(gl); |
1090 | ||
63997775 | 1091 | trace_gfs2_glock_queue(gh, 0); |
6b0c7440 AG |
1092 | if (unlikely(!fast_path)) { |
1093 | gl->gl_lockref.count++; | |
1094 | if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && | |
1095 | !test_bit(GLF_DEMOTE, &gl->gl_flags) && | |
1096 | gl->gl_name.ln_type == LM_TYPE_INODE) | |
1097 | delay = gl->gl_hold_time; | |
1098 | __gfs2_glock_queue_work(gl, delay); | |
1099 | } | |
f3dd1649 | 1100 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa DT |
1101 | } |
1102 | ||
d93cfa98 AD |
1103 | void gfs2_glock_dq_wait(struct gfs2_holder *gh) |
1104 | { | |
1105 | struct gfs2_glock *gl = gh->gh_gl; | |
1106 | gfs2_glock_dq(gh); | |
81e1d450 | 1107 | might_sleep(); |
74316201 | 1108 | wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE); |
d93cfa98 AD |
1109 | } |
1110 | ||
b3b94faa DT |
1111 | /** |
1112 | * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it | |
1113 | * @gh: the holder structure | |
1114 | * | |
1115 | */ | |
1116 | ||
1117 | void gfs2_glock_dq_uninit(struct gfs2_holder *gh) | |
1118 | { | |
1119 | gfs2_glock_dq(gh); | |
1120 | gfs2_holder_uninit(gh); | |
1121 | } | |
1122 | ||
1123 | /** | |
1124 | * gfs2_glock_nq_num - acquire a glock based on lock number | |
1125 | * @sdp: the filesystem | |
1126 | * @number: the lock number | |
1127 | * @glops: the glock operations for the type of glock | |
1128 | * @state: the state to acquire the glock in | |
25985edc | 1129 | * @flags: modifier flags for the acquisition |
b3b94faa DT |
1130 | * @gh: the struct gfs2_holder |
1131 | * | |
1132 | * Returns: errno | |
1133 | */ | |
1134 | ||
cd915493 | 1135 | int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number, |
8fb4b536 | 1136 | const struct gfs2_glock_operations *glops, |
b58bf407 | 1137 | unsigned int state, u16 flags, struct gfs2_holder *gh) |
b3b94faa DT |
1138 | { |
1139 | struct gfs2_glock *gl; | |
1140 | int error; | |
1141 | ||
1142 | error = gfs2_glock_get(sdp, number, glops, CREATE, &gl); | |
1143 | if (!error) { | |
1144 | error = gfs2_glock_nq_init(gl, state, flags, gh); | |
1145 | gfs2_glock_put(gl); | |
1146 | } | |
1147 | ||
1148 | return error; | |
1149 | } | |
1150 | ||
1151 | /** | |
1152 | * glock_compare - Compare two struct gfs2_glock structures for sorting | |
1153 | * @arg_a: the first structure | |
1154 | * @arg_b: the second structure | |
1155 | * | |
1156 | */ | |
1157 | ||
1158 | static int glock_compare(const void *arg_a, const void *arg_b) | |
1159 | { | |
a5e08a9e SW |
1160 | const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; |
1161 | const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; | |
1162 | const struct lm_lockname *a = &gh_a->gh_gl->gl_name; | |
1163 | const struct lm_lockname *b = &gh_b->gh_gl->gl_name; | |
b3b94faa DT |
1164 | |
1165 | if (a->ln_number > b->ln_number) | |
a5e08a9e SW |
1166 | return 1; |
1167 | if (a->ln_number < b->ln_number) | |
1168 | return -1; | |
1c0f4872 | 1169 | BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type); |
a5e08a9e | 1170 | return 0; |
b3b94faa DT |
1171 | } |
1172 | ||
1173 | /** | |
1174 | * nq_m_sync - synchonously acquire more than one glock in deadlock free order | |
1175 | * @num_gh: the number of structures | |
1176 | * @ghs: an array of struct gfs2_holder structures | |
1177 | * | |
1178 | * Returns: 0 on success (all glocks acquired), | |
1179 | * errno on failure (no glocks acquired) | |
1180 | */ | |
1181 | ||
1182 | static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs, | |
1183 | struct gfs2_holder **p) | |
1184 | { | |
1185 | unsigned int x; | |
1186 | int error = 0; | |
1187 | ||
1188 | for (x = 0; x < num_gh; x++) | |
1189 | p[x] = &ghs[x]; | |
1190 | ||
1191 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL); | |
1192 | ||
1193 | for (x = 0; x < num_gh; x++) { | |
1194 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); | |
1195 | ||
1196 | error = gfs2_glock_nq(p[x]); | |
1197 | if (error) { | |
1198 | while (x--) | |
1199 | gfs2_glock_dq(p[x]); | |
1200 | break; | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | return error; | |
1205 | } | |
1206 | ||
1207 | /** | |
1208 | * gfs2_glock_nq_m - acquire multiple glocks | |
1209 | * @num_gh: the number of structures | |
1210 | * @ghs: an array of struct gfs2_holder structures | |
1211 | * | |
b3b94faa DT |
1212 | * |
1213 | * Returns: 0 on success (all glocks acquired), | |
1214 | * errno on failure (no glocks acquired) | |
1215 | */ | |
1216 | ||
1217 | int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs) | |
1218 | { | |
eaf5bd3c SW |
1219 | struct gfs2_holder *tmp[4]; |
1220 | struct gfs2_holder **pph = tmp; | |
b3b94faa DT |
1221 | int error = 0; |
1222 | ||
eaf5bd3c SW |
1223 | switch(num_gh) { |
1224 | case 0: | |
b3b94faa | 1225 | return 0; |
eaf5bd3c | 1226 | case 1: |
b3b94faa DT |
1227 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
1228 | return gfs2_glock_nq(ghs); | |
eaf5bd3c SW |
1229 | default: |
1230 | if (num_gh <= 4) | |
b3b94faa | 1231 | break; |
eaf5bd3c SW |
1232 | pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS); |
1233 | if (!pph) | |
1234 | return -ENOMEM; | |
b3b94faa DT |
1235 | } |
1236 | ||
eaf5bd3c | 1237 | error = nq_m_sync(num_gh, ghs, pph); |
b3b94faa | 1238 | |
eaf5bd3c SW |
1239 | if (pph != tmp) |
1240 | kfree(pph); | |
b3b94faa DT |
1241 | |
1242 | return error; | |
1243 | } | |
1244 | ||
1245 | /** | |
1246 | * gfs2_glock_dq_m - release multiple glocks | |
1247 | * @num_gh: the number of structures | |
1248 | * @ghs: an array of struct gfs2_holder structures | |
1249 | * | |
1250 | */ | |
1251 | ||
1252 | void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs) | |
1253 | { | |
fa1bbdea BP |
1254 | while (num_gh--) |
1255 | gfs2_glock_dq(&ghs[num_gh]); | |
b3b94faa DT |
1256 | } |
1257 | ||
f057f6cd | 1258 | void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state) |
da755fdb | 1259 | { |
c4f68a13 BM |
1260 | unsigned long delay = 0; |
1261 | unsigned long holdtime; | |
1262 | unsigned long now = jiffies; | |
b3b94faa | 1263 | |
f057f6cd | 1264 | gfs2_glock_hold(gl); |
7cf8dcd3 BP |
1265 | holdtime = gl->gl_tchange + gl->gl_hold_time; |
1266 | if (test_bit(GLF_QUEUED, &gl->gl_flags) && | |
1267 | gl->gl_name.ln_type == LM_TYPE_INODE) { | |
7b5e3d5f SW |
1268 | if (time_before(now, holdtime)) |
1269 | delay = holdtime - now; | |
1270 | if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags)) | |
7cf8dcd3 | 1271 | delay = gl->gl_hold_time; |
7b5e3d5f | 1272 | } |
b3b94faa | 1273 | |
f3dd1649 | 1274 | spin_lock(&gl->gl_lockref.lock); |
81ffbf65 | 1275 | handle_callback(gl, state, delay, true); |
6b0c7440 | 1276 | __gfs2_glock_queue_work(gl, delay); |
f3dd1649 | 1277 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa DT |
1278 | } |
1279 | ||
0809f6ec SW |
1280 | /** |
1281 | * gfs2_should_freeze - Figure out if glock should be frozen | |
1282 | * @gl: The glock in question | |
1283 | * | |
1284 | * Glocks are not frozen if (a) the result of the dlm operation is | |
1285 | * an error, (b) the locking operation was an unlock operation or | |
1286 | * (c) if there is a "noexp" flagged request anywhere in the queue | |
1287 | * | |
1288 | * Returns: 1 if freezing should occur, 0 otherwise | |
1289 | */ | |
1290 | ||
1291 | static int gfs2_should_freeze(const struct gfs2_glock *gl) | |
1292 | { | |
1293 | const struct gfs2_holder *gh; | |
1294 | ||
1295 | if (gl->gl_reply & ~LM_OUT_ST_MASK) | |
1296 | return 0; | |
1297 | if (gl->gl_target == LM_ST_UNLOCKED) | |
1298 | return 0; | |
1299 | ||
1300 | list_for_each_entry(gh, &gl->gl_holders, gh_list) { | |
1301 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) | |
1302 | continue; | |
1303 | if (LM_FLAG_NOEXP & gh->gh_flags) | |
1304 | return 0; | |
1305 | } | |
1306 | ||
1307 | return 1; | |
1308 | } | |
1309 | ||
b3b94faa | 1310 | /** |
f057f6cd SW |
1311 | * gfs2_glock_complete - Callback used by locking |
1312 | * @gl: Pointer to the glock | |
1313 | * @ret: The return value from the dlm | |
b3b94faa | 1314 | * |
f3dd1649 | 1315 | * The gl_reply field is under the gl_lockref.lock lock so that it is ok |
47a25380 | 1316 | * to use a bitfield shared with other glock state fields. |
b3b94faa DT |
1317 | */ |
1318 | ||
f057f6cd | 1319 | void gfs2_glock_complete(struct gfs2_glock *gl, int ret) |
b3b94faa | 1320 | { |
15562c43 | 1321 | struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct; |
0809f6ec | 1322 | |
f3dd1649 | 1323 | spin_lock(&gl->gl_lockref.lock); |
f057f6cd | 1324 | gl->gl_reply = ret; |
0809f6ec | 1325 | |
e0c2a9aa | 1326 | if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) { |
0809f6ec | 1327 | if (gfs2_should_freeze(gl)) { |
f057f6cd | 1328 | set_bit(GLF_FROZEN, &gl->gl_flags); |
f3dd1649 | 1329 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa | 1330 | return; |
0809f6ec | 1331 | } |
b3b94faa | 1332 | } |
47a25380 | 1333 | |
e66cf161 | 1334 | gl->gl_lockref.count++; |
f057f6cd | 1335 | set_bit(GLF_REPLY_PENDING, &gl->gl_flags); |
6b0c7440 | 1336 | __gfs2_glock_queue_work(gl, 0); |
f3dd1649 | 1337 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa DT |
1338 | } |
1339 | ||
4506a519 SW |
1340 | static int glock_cmp(void *priv, struct list_head *a, struct list_head *b) |
1341 | { | |
1342 | struct gfs2_glock *gla, *glb; | |
1343 | ||
1344 | gla = list_entry(a, struct gfs2_glock, gl_lru); | |
1345 | glb = list_entry(b, struct gfs2_glock, gl_lru); | |
1346 | ||
1347 | if (gla->gl_name.ln_number > glb->gl_name.ln_number) | |
1348 | return 1; | |
1349 | if (gla->gl_name.ln_number < glb->gl_name.ln_number) | |
1350 | return -1; | |
1351 | ||
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | /** | |
1356 | * gfs2_dispose_glock_lru - Demote a list of glocks | |
1357 | * @list: The list to dispose of | |
1358 | * | |
1359 | * Disposing of glocks may involve disk accesses, so that here we sort | |
1360 | * the glocks by number (i.e. disk location of the inodes) so that if | |
1361 | * there are any such accesses, they'll be sent in order (mostly). | |
1362 | * | |
1363 | * Must be called under the lru_lock, but may drop and retake this | |
1364 | * lock. While the lru_lock is dropped, entries may vanish from the | |
1365 | * list, but no new entries will appear on the list (since it is | |
1366 | * private) | |
1367 | */ | |
1368 | ||
1369 | static void gfs2_dispose_glock_lru(struct list_head *list) | |
1370 | __releases(&lru_lock) | |
1371 | __acquires(&lru_lock) | |
1372 | { | |
1373 | struct gfs2_glock *gl; | |
1374 | ||
1375 | list_sort(NULL, list, glock_cmp); | |
1376 | ||
1377 | while(!list_empty(list)) { | |
1378 | gl = list_entry(list->next, struct gfs2_glock, gl_lru); | |
1379 | list_del_init(&gl->gl_lru); | |
f3dd1649 | 1380 | if (!spin_trylock(&gl->gl_lockref.lock)) { |
94a09a39 | 1381 | add_back_to_lru: |
e66cf161 SW |
1382 | list_add(&gl->gl_lru, &lru_list); |
1383 | atomic_inc(&lru_count); | |
1384 | continue; | |
1385 | } | |
94a09a39 | 1386 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) { |
f3dd1649 | 1387 | spin_unlock(&gl->gl_lockref.lock); |
94a09a39 SW |
1388 | goto add_back_to_lru; |
1389 | } | |
4506a519 | 1390 | clear_bit(GLF_LRU, &gl->gl_flags); |
e66cf161 | 1391 | gl->gl_lockref.count++; |
4506a519 | 1392 | if (demote_ok(gl)) |
81ffbf65 | 1393 | handle_callback(gl, LM_ST_UNLOCKED, 0, false); |
4506a519 | 1394 | WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags)); |
6b0c7440 | 1395 | __gfs2_glock_queue_work(gl, 0); |
f3dd1649 | 1396 | spin_unlock(&gl->gl_lockref.lock); |
94a09a39 | 1397 | cond_resched_lock(&lru_lock); |
4506a519 SW |
1398 | } |
1399 | } | |
1400 | ||
2a005855 SW |
1401 | /** |
1402 | * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote | |
1403 | * @nr: The number of entries to scan | |
1404 | * | |
4506a519 SW |
1405 | * This function selects the entries on the LRU which are able to |
1406 | * be demoted, and then kicks off the process by calling | |
1407 | * gfs2_dispose_glock_lru() above. | |
2a005855 | 1408 | */ |
b3b94faa | 1409 | |
1ab6c499 | 1410 | static long gfs2_scan_glock_lru(int nr) |
b3b94faa DT |
1411 | { |
1412 | struct gfs2_glock *gl; | |
97cc1025 | 1413 | LIST_HEAD(skipped); |
4506a519 | 1414 | LIST_HEAD(dispose); |
1ab6c499 | 1415 | long freed = 0; |
b3b94faa | 1416 | |
97cc1025 | 1417 | spin_lock(&lru_lock); |
1ab6c499 | 1418 | while ((nr-- >= 0) && !list_empty(&lru_list)) { |
97cc1025 | 1419 | gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru); |
97cc1025 SW |
1420 | |
1421 | /* Test for being demotable */ | |
94a09a39 | 1422 | if (!test_bit(GLF_LOCK, &gl->gl_flags)) { |
4506a519 SW |
1423 | list_move(&gl->gl_lru, &dispose); |
1424 | atomic_dec(&lru_count); | |
1ab6c499 | 1425 | freed++; |
2163b1e6 | 1426 | continue; |
97cc1025 | 1427 | } |
4506a519 SW |
1428 | |
1429 | list_move(&gl->gl_lru, &skipped); | |
b3b94faa | 1430 | } |
97cc1025 | 1431 | list_splice(&skipped, &lru_list); |
4506a519 SW |
1432 | if (!list_empty(&dispose)) |
1433 | gfs2_dispose_glock_lru(&dispose); | |
97cc1025 | 1434 | spin_unlock(&lru_lock); |
1ab6c499 DC |
1435 | |
1436 | return freed; | |
2a005855 SW |
1437 | } |
1438 | ||
1ab6c499 DC |
1439 | static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink, |
1440 | struct shrink_control *sc) | |
2a005855 | 1441 | { |
1ab6c499 DC |
1442 | if (!(sc->gfp_mask & __GFP_FS)) |
1443 | return SHRINK_STOP; | |
1444 | return gfs2_scan_glock_lru(sc->nr_to_scan); | |
1445 | } | |
2a005855 | 1446 | |
1ab6c499 DC |
1447 | static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink, |
1448 | struct shrink_control *sc) | |
1449 | { | |
55f841ce | 1450 | return vfs_pressure_ratio(atomic_read(&lru_count)); |
b3b94faa DT |
1451 | } |
1452 | ||
97cc1025 | 1453 | static struct shrinker glock_shrinker = { |
97cc1025 | 1454 | .seeks = DEFAULT_SEEKS, |
1ab6c499 DC |
1455 | .count_objects = gfs2_glock_shrink_count, |
1456 | .scan_objects = gfs2_glock_shrink_scan, | |
97cc1025 SW |
1457 | }; |
1458 | ||
b3b94faa DT |
1459 | /** |
1460 | * examine_bucket - Call a function for glock in a hash bucket | |
1461 | * @examiner: the function | |
1462 | * @sdp: the filesystem | |
1463 | * @bucket: the bucket | |
1464 | * | |
98687f42 HX |
1465 | * Note that the function can be called multiple times on the same |
1466 | * object. So the user must ensure that the function can cope with | |
1467 | * that. | |
b3b94faa DT |
1468 | */ |
1469 | ||
88ffbf3e | 1470 | static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp) |
b3b94faa | 1471 | { |
bc015cb8 | 1472 | struct gfs2_glock *gl; |
98687f42 HX |
1473 | struct rhashtable_iter iter; |
1474 | ||
1475 | rhashtable_walk_enter(&gl_hash_table, &iter); | |
1476 | ||
1477 | do { | |
1478 | gl = ERR_PTR(rhashtable_walk_start(&iter)); | |
1479 | if (gl) | |
1480 | continue; | |
1481 | ||
1482 | while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) | |
88ffbf3e BP |
1483 | if ((gl->gl_name.ln_sbd == sdp) && |
1484 | lockref_get_not_dead(&gl->gl_lockref)) | |
1485 | examiner(gl); | |
98687f42 HX |
1486 | |
1487 | rhashtable_walk_stop(&iter); | |
1488 | } while (cond_resched(), gl == ERR_PTR(-EAGAIN)); | |
1489 | ||
1490 | rhashtable_walk_exit(&iter); | |
bc015cb8 SW |
1491 | } |
1492 | ||
f057f6cd SW |
1493 | /** |
1494 | * thaw_glock - thaw out a glock which has an unprocessed reply waiting | |
1495 | * @gl: The glock to thaw | |
1496 | * | |
f057f6cd SW |
1497 | */ |
1498 | ||
1499 | static void thaw_glock(struct gfs2_glock *gl) | |
1500 | { | |
6b0c7440 | 1501 | if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) { |
f057f6cd | 1502 | gfs2_glock_put(gl); |
6b0c7440 | 1503 | return; |
7286b31e | 1504 | } |
6b0c7440 AG |
1505 | set_bit(GLF_REPLY_PENDING, &gl->gl_flags); |
1506 | gfs2_glock_queue_work(gl, 0); | |
f057f6cd SW |
1507 | } |
1508 | ||
b3b94faa DT |
1509 | /** |
1510 | * clear_glock - look at a glock and see if we can free it from glock cache | |
1511 | * @gl: the glock to look at | |
1512 | * | |
1513 | */ | |
1514 | ||
1515 | static void clear_glock(struct gfs2_glock *gl) | |
1516 | { | |
f42ab085 | 1517 | gfs2_glock_remove_from_lru(gl); |
b3b94faa | 1518 | |
f3dd1649 | 1519 | spin_lock(&gl->gl_lockref.lock); |
c741c455 | 1520 | if (gl->gl_state != LM_ST_UNLOCKED) |
81ffbf65 | 1521 | handle_callback(gl, LM_ST_UNLOCKED, 0, false); |
6b0c7440 | 1522 | __gfs2_glock_queue_work(gl, 0); |
f3dd1649 | 1523 | spin_unlock(&gl->gl_lockref.lock); |
b3b94faa DT |
1524 | } |
1525 | ||
f057f6cd SW |
1526 | /** |
1527 | * gfs2_glock_thaw - Thaw any frozen glocks | |
1528 | * @sdp: The super block | |
1529 | * | |
1530 | */ | |
1531 | ||
1532 | void gfs2_glock_thaw(struct gfs2_sbd *sdp) | |
1533 | { | |
bc015cb8 SW |
1534 | glock_hash_walk(thaw_glock, sdp); |
1535 | } | |
f057f6cd | 1536 | |
ac3beb6a | 1537 | static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl) |
bc015cb8 | 1538 | { |
f3dd1649 | 1539 | spin_lock(&gl->gl_lockref.lock); |
ac3beb6a | 1540 | gfs2_dump_glock(seq, gl); |
f3dd1649 | 1541 | spin_unlock(&gl->gl_lockref.lock); |
bc015cb8 SW |
1542 | } |
1543 | ||
1544 | static void dump_glock_func(struct gfs2_glock *gl) | |
1545 | { | |
1546 | dump_glock(NULL, gl); | |
f057f6cd SW |
1547 | } |
1548 | ||
b3b94faa DT |
1549 | /** |
1550 | * gfs2_gl_hash_clear - Empty out the glock hash table | |
1551 | * @sdp: the filesystem | |
1552 | * @wait: wait until it's all gone | |
1553 | * | |
1bdad606 | 1554 | * Called when unmounting the filesystem. |
b3b94faa DT |
1555 | */ |
1556 | ||
fefc03bf | 1557 | void gfs2_gl_hash_clear(struct gfs2_sbd *sdp) |
b3b94faa | 1558 | { |
fb6791d1 | 1559 | set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags); |
222cb538 | 1560 | flush_workqueue(glock_workqueue); |
bc015cb8 | 1561 | glock_hash_walk(clear_glock, sdp); |
8f05228e | 1562 | flush_workqueue(glock_workqueue); |
2aba1b5b BP |
1563 | wait_event_timeout(sdp->sd_glock_wait, |
1564 | atomic_read(&sdp->sd_glock_disposal) == 0, | |
1565 | HZ * 600); | |
bc015cb8 | 1566 | glock_hash_walk(dump_glock_func, sdp); |
b3b94faa DT |
1567 | } |
1568 | ||
813e0c46 SW |
1569 | void gfs2_glock_finish_truncate(struct gfs2_inode *ip) |
1570 | { | |
1571 | struct gfs2_glock *gl = ip->i_gl; | |
1572 | int ret; | |
1573 | ||
1574 | ret = gfs2_truncatei_resume(ip); | |
15562c43 | 1575 | gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0); |
813e0c46 | 1576 | |
f3dd1649 | 1577 | spin_lock(&gl->gl_lockref.lock); |
813e0c46 SW |
1578 | clear_bit(GLF_LOCK, &gl->gl_flags); |
1579 | run_queue(gl, 1); | |
f3dd1649 | 1580 | spin_unlock(&gl->gl_lockref.lock); |
813e0c46 SW |
1581 | } |
1582 | ||
6802e340 | 1583 | static const char *state2str(unsigned state) |
04b933f2 | 1584 | { |
6802e340 SW |
1585 | switch(state) { |
1586 | case LM_ST_UNLOCKED: | |
1587 | return "UN"; | |
1588 | case LM_ST_SHARED: | |
1589 | return "SH"; | |
1590 | case LM_ST_DEFERRED: | |
1591 | return "DF"; | |
1592 | case LM_ST_EXCLUSIVE: | |
1593 | return "EX"; | |
1594 | } | |
1595 | return "??"; | |
1596 | } | |
1597 | ||
b58bf407 | 1598 | static const char *hflags2str(char *buf, u16 flags, unsigned long iflags) |
6802e340 SW |
1599 | { |
1600 | char *p = buf; | |
1601 | if (flags & LM_FLAG_TRY) | |
1602 | *p++ = 't'; | |
1603 | if (flags & LM_FLAG_TRY_1CB) | |
1604 | *p++ = 'T'; | |
1605 | if (flags & LM_FLAG_NOEXP) | |
1606 | *p++ = 'e'; | |
1607 | if (flags & LM_FLAG_ANY) | |
f057f6cd | 1608 | *p++ = 'A'; |
6802e340 SW |
1609 | if (flags & LM_FLAG_PRIORITY) |
1610 | *p++ = 'p'; | |
1611 | if (flags & GL_ASYNC) | |
1612 | *p++ = 'a'; | |
1613 | if (flags & GL_EXACT) | |
1614 | *p++ = 'E'; | |
6802e340 SW |
1615 | if (flags & GL_NOCACHE) |
1616 | *p++ = 'c'; | |
1617 | if (test_bit(HIF_HOLDER, &iflags)) | |
1618 | *p++ = 'H'; | |
1619 | if (test_bit(HIF_WAIT, &iflags)) | |
1620 | *p++ = 'W'; | |
1621 | if (test_bit(HIF_FIRST, &iflags)) | |
1622 | *p++ = 'F'; | |
1623 | *p = 0; | |
1624 | return buf; | |
04b933f2 RP |
1625 | } |
1626 | ||
b3b94faa DT |
1627 | /** |
1628 | * dump_holder - print information about a glock holder | |
6802e340 | 1629 | * @seq: the seq_file struct |
b3b94faa DT |
1630 | * @gh: the glock holder |
1631 | * | |
b3b94faa DT |
1632 | */ |
1633 | ||
ac3beb6a | 1634 | static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh) |
b3b94faa | 1635 | { |
6802e340 | 1636 | struct task_struct *gh_owner = NULL; |
6802e340 | 1637 | char flags_buf[32]; |
b3b94faa | 1638 | |
0b3a2c99 | 1639 | rcu_read_lock(); |
6802e340 | 1640 | if (gh->gh_owner_pid) |
b1e058da | 1641 | gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID); |
cc18152e JP |
1642 | gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n", |
1643 | state2str(gh->gh_state), | |
1644 | hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags), | |
1645 | gh->gh_error, | |
1646 | gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1, | |
1647 | gh_owner ? gh_owner->comm : "(ended)", | |
1648 | (void *)gh->gh_ip); | |
0b3a2c99 | 1649 | rcu_read_unlock(); |
b3b94faa DT |
1650 | } |
1651 | ||
627c10b7 | 1652 | static const char *gflags2str(char *buf, const struct gfs2_glock *gl) |
6802e340 | 1653 | { |
627c10b7 | 1654 | const unsigned long *gflags = &gl->gl_flags; |
6802e340 | 1655 | char *p = buf; |
627c10b7 | 1656 | |
6802e340 SW |
1657 | if (test_bit(GLF_LOCK, gflags)) |
1658 | *p++ = 'l'; | |
6802e340 SW |
1659 | if (test_bit(GLF_DEMOTE, gflags)) |
1660 | *p++ = 'D'; | |
1661 | if (test_bit(GLF_PENDING_DEMOTE, gflags)) | |
1662 | *p++ = 'd'; | |
1663 | if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags)) | |
1664 | *p++ = 'p'; | |
1665 | if (test_bit(GLF_DIRTY, gflags)) | |
1666 | *p++ = 'y'; | |
1667 | if (test_bit(GLF_LFLUSH, gflags)) | |
1668 | *p++ = 'f'; | |
1669 | if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags)) | |
1670 | *p++ = 'i'; | |
1671 | if (test_bit(GLF_REPLY_PENDING, gflags)) | |
1672 | *p++ = 'r'; | |
f057f6cd | 1673 | if (test_bit(GLF_INITIAL, gflags)) |
d8348de0 | 1674 | *p++ = 'I'; |
f057f6cd SW |
1675 | if (test_bit(GLF_FROZEN, gflags)) |
1676 | *p++ = 'F'; | |
7b5e3d5f SW |
1677 | if (test_bit(GLF_QUEUED, gflags)) |
1678 | *p++ = 'q'; | |
627c10b7 SW |
1679 | if (test_bit(GLF_LRU, gflags)) |
1680 | *p++ = 'L'; | |
1681 | if (gl->gl_object) | |
1682 | *p++ = 'o'; | |
a245769f SW |
1683 | if (test_bit(GLF_BLOCKING, gflags)) |
1684 | *p++ = 'b'; | |
6802e340 SW |
1685 | *p = 0; |
1686 | return buf; | |
b3b94faa DT |
1687 | } |
1688 | ||
1689 | /** | |
8eae1ca0 | 1690 | * gfs2_dump_glock - print information about a glock |
6802e340 | 1691 | * @seq: The seq_file struct |
b3b94faa | 1692 | * @gl: the glock |
6802e340 SW |
1693 | * |
1694 | * The file format is as follows: | |
1695 | * One line per object, capital letters are used to indicate objects | |
1696 | * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented, | |
1697 | * other objects are indented by a single space and follow the glock to | |
1698 | * which they are related. Fields are indicated by lower case letters | |
1699 | * followed by a colon and the field value, except for strings which are in | |
1700 | * [] so that its possible to see if they are composed of spaces for | |
1701 | * example. The field's are n = number (id of the object), f = flags, | |
1702 | * t = type, s = state, r = refcount, e = error, p = pid. | |
b3b94faa | 1703 | * |
b3b94faa DT |
1704 | */ |
1705 | ||
ac3beb6a | 1706 | void gfs2_dump_glock(struct seq_file *seq, const struct gfs2_glock *gl) |
b3b94faa | 1707 | { |
6802e340 SW |
1708 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
1709 | unsigned long long dtime; | |
1710 | const struct gfs2_holder *gh; | |
1711 | char gflags_buf[32]; | |
b3b94faa | 1712 | |
6802e340 SW |
1713 | dtime = jiffies - gl->gl_demote_time; |
1714 | dtime *= 1000000/HZ; /* demote time in uSec */ | |
1715 | if (!test_bit(GLF_DEMOTE, &gl->gl_flags)) | |
1716 | dtime = 0; | |
7cf8dcd3 | 1717 | gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d m:%ld\n", |
6802e340 SW |
1718 | state2str(gl->gl_state), |
1719 | gl->gl_name.ln_type, | |
1720 | (unsigned long long)gl->gl_name.ln_number, | |
627c10b7 | 1721 | gflags2str(gflags_buf, gl), |
6802e340 SW |
1722 | state2str(gl->gl_target), |
1723 | state2str(gl->gl_demote_state), dtime, | |
6802e340 | 1724 | atomic_read(&gl->gl_ail_count), |
f42ab085 | 1725 | atomic_read(&gl->gl_revokes), |
e66cf161 | 1726 | (int)gl->gl_lockref.count, gl->gl_hold_time); |
b3b94faa | 1727 | |
ac3beb6a SW |
1728 | list_for_each_entry(gh, &gl->gl_holders, gh_list) |
1729 | dump_holder(seq, gh); | |
1730 | ||
6802e340 | 1731 | if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump) |
ac3beb6a | 1732 | glops->go_dump(seq, gl); |
b3b94faa DT |
1733 | } |
1734 | ||
a245769f SW |
1735 | static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr) |
1736 | { | |
1737 | struct gfs2_glock *gl = iter_ptr; | |
1738 | ||
4d207133 | 1739 | seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n", |
a245769f SW |
1740 | gl->gl_name.ln_type, |
1741 | (unsigned long long)gl->gl_name.ln_number, | |
4d207133 BH |
1742 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT], |
1743 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR], | |
1744 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB], | |
1745 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB], | |
1746 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT], | |
1747 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR], | |
1748 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT], | |
1749 | (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]); | |
a245769f SW |
1750 | return 0; |
1751 | } | |
1752 | ||
1753 | static const char *gfs2_gltype[] = { | |
1754 | "type", | |
1755 | "reserved", | |
1756 | "nondisk", | |
1757 | "inode", | |
1758 | "rgrp", | |
1759 | "meta", | |
1760 | "iopen", | |
1761 | "flock", | |
1762 | "plock", | |
1763 | "quota", | |
1764 | "journal", | |
1765 | }; | |
1766 | ||
1767 | static const char *gfs2_stype[] = { | |
1768 | [GFS2_LKS_SRTT] = "srtt", | |
1769 | [GFS2_LKS_SRTTVAR] = "srttvar", | |
1770 | [GFS2_LKS_SRTTB] = "srttb", | |
1771 | [GFS2_LKS_SRTTVARB] = "srttvarb", | |
1772 | [GFS2_LKS_SIRT] = "sirt", | |
1773 | [GFS2_LKS_SIRTVAR] = "sirtvar", | |
1774 | [GFS2_LKS_DCOUNT] = "dlm", | |
1775 | [GFS2_LKS_QCOUNT] = "queue", | |
1776 | }; | |
1777 | ||
1778 | #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype)) | |
1779 | ||
1780 | static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr) | |
1781 | { | |
81648d04 AG |
1782 | struct gfs2_sbd *sdp = seq->private; |
1783 | loff_t pos = *(loff_t *)iter_ptr; | |
1784 | unsigned index = pos >> 3; | |
1785 | unsigned subindex = pos & 0x07; | |
a245769f SW |
1786 | int i; |
1787 | ||
1788 | if (index == 0 && subindex != 0) | |
1789 | return 0; | |
6802e340 | 1790 | |
a245769f SW |
1791 | seq_printf(seq, "%-10s %8s:", gfs2_gltype[index], |
1792 | (index == 0) ? "cpu": gfs2_stype[subindex]); | |
b3b94faa | 1793 | |
a245769f SW |
1794 | for_each_possible_cpu(i) { |
1795 | const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i); | |
8f7e0a80 AG |
1796 | |
1797 | if (index == 0) | |
1798 | seq_printf(seq, " %15u", i); | |
1799 | else | |
1800 | seq_printf(seq, " %15llu", (unsigned long long)lkstats-> | |
1801 | lkstats[index - 1].stats[subindex]); | |
a245769f SW |
1802 | } |
1803 | seq_putc(seq, '\n'); | |
1804 | return 0; | |
1805 | } | |
8fbbfd21 | 1806 | |
85d1da67 SW |
1807 | int __init gfs2_glock_init(void) |
1808 | { | |
88ffbf3e BP |
1809 | int ret; |
1810 | ||
1811 | ret = rhashtable_init(&gl_hash_table, &ht_parms); | |
1812 | if (ret < 0) | |
1813 | return ret; | |
8fbbfd21 | 1814 | |
d2115778 | 1815 | glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM | |
58a69cb4 | 1816 | WQ_HIGHPRI | WQ_FREEZABLE, 0); |
88ffbf3e BP |
1817 | if (!glock_workqueue) { |
1818 | rhashtable_destroy(&gl_hash_table); | |
dfc4616d | 1819 | return -ENOMEM; |
88ffbf3e | 1820 | } |
d2115778 | 1821 | gfs2_delete_workqueue = alloc_workqueue("delete_workqueue", |
58a69cb4 | 1822 | WQ_MEM_RECLAIM | WQ_FREEZABLE, |
d2115778 | 1823 | 0); |
dfc4616d | 1824 | if (!gfs2_delete_workqueue) { |
b94a170e | 1825 | destroy_workqueue(glock_workqueue); |
88ffbf3e | 1826 | rhashtable_destroy(&gl_hash_table); |
dfc4616d | 1827 | return -ENOMEM; |
b94a170e | 1828 | } |
97cc1025 | 1829 | |
e0d735c1 CY |
1830 | ret = register_shrinker(&glock_shrinker); |
1831 | if (ret) { | |
1832 | destroy_workqueue(gfs2_delete_workqueue); | |
1833 | destroy_workqueue(glock_workqueue); | |
1834 | rhashtable_destroy(&gl_hash_table); | |
1835 | return ret; | |
1836 | } | |
c4f68a13 | 1837 | |
85d1da67 SW |
1838 | return 0; |
1839 | } | |
1840 | ||
8fbbfd21 SW |
1841 | void gfs2_glock_exit(void) |
1842 | { | |
97cc1025 | 1843 | unregister_shrinker(&glock_shrinker); |
88ffbf3e | 1844 | rhashtable_destroy(&gl_hash_table); |
c4f68a13 | 1845 | destroy_workqueue(glock_workqueue); |
b94a170e | 1846 | destroy_workqueue(gfs2_delete_workqueue); |
8fbbfd21 SW |
1847 | } |
1848 | ||
88ffbf3e | 1849 | static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi) |
bc015cb8 | 1850 | { |
14d37564 | 1851 | while ((gi->gl = rhashtable_walk_next(&gi->hti))) { |
88ffbf3e BP |
1852 | if (IS_ERR(gi->gl)) { |
1853 | if (PTR_ERR(gi->gl) == -EAGAIN) | |
1854 | continue; | |
1855 | gi->gl = NULL; | |
14d37564 | 1856 | return; |
bc015cb8 | 1857 | } |
14d37564 DC |
1858 | /* Skip entries for other sb and dead entries */ |
1859 | if (gi->sdp == gi->gl->gl_name.ln_sbd && | |
1860 | !__lockref_is_dead(&gi->gl->gl_lockref)) | |
1861 | return; | |
1862 | } | |
7c52b166 RP |
1863 | } |
1864 | ||
6802e340 | 1865 | static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos) |
7c52b166 | 1866 | { |
6802e340 | 1867 | struct gfs2_glock_iter *gi = seq->private; |
7c52b166 | 1868 | loff_t n = *pos; |
88ffbf3e | 1869 | int ret; |
7c52b166 | 1870 | |
ba1ddcb6 | 1871 | if (gi->last_pos <= *pos) |
88ffbf3e | 1872 | n = (*pos - gi->last_pos); |
ba1ddcb6 | 1873 | |
88ffbf3e BP |
1874 | ret = rhashtable_walk_start(&gi->hti); |
1875 | if (ret) | |
1876 | return NULL; | |
7c52b166 | 1877 | |
6802e340 | 1878 | do { |
88ffbf3e BP |
1879 | gfs2_glock_iter_next(gi); |
1880 | } while (gi->gl && n--); | |
7c52b166 | 1881 | |
ba1ddcb6 | 1882 | gi->last_pos = *pos; |
6802e340 | 1883 | return gi->gl; |
7c52b166 RP |
1884 | } |
1885 | ||
6802e340 | 1886 | static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr, |
7c52b166 RP |
1887 | loff_t *pos) |
1888 | { | |
6802e340 | 1889 | struct gfs2_glock_iter *gi = seq->private; |
7c52b166 RP |
1890 | |
1891 | (*pos)++; | |
ba1ddcb6 | 1892 | gi->last_pos = *pos; |
88ffbf3e | 1893 | gfs2_glock_iter_next(gi); |
6802e340 | 1894 | return gi->gl; |
7c52b166 RP |
1895 | } |
1896 | ||
6802e340 | 1897 | static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr) |
7c52b166 | 1898 | { |
6802e340 | 1899 | struct gfs2_glock_iter *gi = seq->private; |
bc015cb8 | 1900 | |
bc015cb8 | 1901 | gi->gl = NULL; |
88ffbf3e | 1902 | rhashtable_walk_stop(&gi->hti); |
7c52b166 RP |
1903 | } |
1904 | ||
6802e340 | 1905 | static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr) |
7c52b166 | 1906 | { |
ac3beb6a SW |
1907 | dump_glock(seq, iter_ptr); |
1908 | return 0; | |
7c52b166 RP |
1909 | } |
1910 | ||
a245769f SW |
1911 | static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos) |
1912 | { | |
81648d04 | 1913 | preempt_disable(); |
a245769f SW |
1914 | if (*pos >= GFS2_NR_SBSTATS) |
1915 | return NULL; | |
81648d04 | 1916 | return pos; |
a245769f SW |
1917 | } |
1918 | ||
1919 | static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr, | |
1920 | loff_t *pos) | |
1921 | { | |
a245769f | 1922 | (*pos)++; |
81648d04 | 1923 | if (*pos >= GFS2_NR_SBSTATS) |
a245769f | 1924 | return NULL; |
81648d04 | 1925 | return pos; |
a245769f SW |
1926 | } |
1927 | ||
1928 | static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr) | |
1929 | { | |
1930 | preempt_enable(); | |
1931 | } | |
1932 | ||
4ef29002 | 1933 | static const struct seq_operations gfs2_glock_seq_ops = { |
7c52b166 RP |
1934 | .start = gfs2_glock_seq_start, |
1935 | .next = gfs2_glock_seq_next, | |
1936 | .stop = gfs2_glock_seq_stop, | |
1937 | .show = gfs2_glock_seq_show, | |
1938 | }; | |
1939 | ||
a245769f SW |
1940 | static const struct seq_operations gfs2_glstats_seq_ops = { |
1941 | .start = gfs2_glock_seq_start, | |
1942 | .next = gfs2_glock_seq_next, | |
1943 | .stop = gfs2_glock_seq_stop, | |
1944 | .show = gfs2_glstats_seq_show, | |
1945 | }; | |
1946 | ||
1947 | static const struct seq_operations gfs2_sbstats_seq_ops = { | |
1948 | .start = gfs2_sbstats_seq_start, | |
1949 | .next = gfs2_sbstats_seq_next, | |
1950 | .stop = gfs2_sbstats_seq_stop, | |
1951 | .show = gfs2_sbstats_seq_show, | |
1952 | }; | |
1953 | ||
0fe2f1e9 SW |
1954 | #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL) |
1955 | ||
92ecd73a AG |
1956 | static int __gfs2_glocks_open(struct inode *inode, struct file *file, |
1957 | const struct seq_operations *ops) | |
7c52b166 | 1958 | { |
92ecd73a | 1959 | int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter)); |
6802e340 SW |
1960 | if (ret == 0) { |
1961 | struct seq_file *seq = file->private_data; | |
1962 | struct gfs2_glock_iter *gi = seq->private; | |
88ffbf3e | 1963 | |
6802e340 | 1964 | gi->sdp = inode->i_private; |
88ffbf3e | 1965 | gi->last_pos = 0; |
0fe2f1e9 | 1966 | seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN); |
df5d2f55 | 1967 | if (seq->buf) |
0fe2f1e9 | 1968 | seq->size = GFS2_SEQ_GOODSIZE; |
88ffbf3e | 1969 | gi->gl = NULL; |
cc37a627 | 1970 | rhashtable_walk_enter(&gl_hash_table, &gi->hti); |
6802e340 SW |
1971 | } |
1972 | return ret; | |
7c52b166 RP |
1973 | } |
1974 | ||
92ecd73a AG |
1975 | static int gfs2_glocks_open(struct inode *inode, struct file *file) |
1976 | { | |
1977 | return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops); | |
1978 | } | |
1979 | ||
88ffbf3e BP |
1980 | static int gfs2_glocks_release(struct inode *inode, struct file *file) |
1981 | { | |
1982 | struct seq_file *seq = file->private_data; | |
1983 | struct gfs2_glock_iter *gi = seq->private; | |
1984 | ||
1985 | gi->gl = NULL; | |
1986 | rhashtable_walk_exit(&gi->hti); | |
1987 | return seq_release_private(inode, file); | |
1988 | } | |
1989 | ||
a245769f SW |
1990 | static int gfs2_glstats_open(struct inode *inode, struct file *file) |
1991 | { | |
92ecd73a | 1992 | return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops); |
a245769f SW |
1993 | } |
1994 | ||
1995 | static int gfs2_sbstats_open(struct inode *inode, struct file *file) | |
1996 | { | |
81648d04 | 1997 | int ret = seq_open(file, &gfs2_sbstats_seq_ops); |
a245769f SW |
1998 | if (ret == 0) { |
1999 | struct seq_file *seq = file->private_data; | |
81648d04 | 2000 | seq->private = inode->i_private; /* sdp */ |
a245769f SW |
2001 | } |
2002 | return ret; | |
2003 | } | |
2004 | ||
2005 | static const struct file_operations gfs2_glocks_fops = { | |
2006 | .owner = THIS_MODULE, | |
2007 | .open = gfs2_glocks_open, | |
2008 | .read = seq_read, | |
2009 | .llseek = seq_lseek, | |
88ffbf3e | 2010 | .release = gfs2_glocks_release, |
a245769f SW |
2011 | }; |
2012 | ||
2013 | static const struct file_operations gfs2_glstats_fops = { | |
7c52b166 | 2014 | .owner = THIS_MODULE, |
a245769f SW |
2015 | .open = gfs2_glstats_open, |
2016 | .read = seq_read, | |
2017 | .llseek = seq_lseek, | |
88ffbf3e | 2018 | .release = gfs2_glocks_release, |
a245769f SW |
2019 | }; |
2020 | ||
2021 | static const struct file_operations gfs2_sbstats_fops = { | |
2022 | .owner = THIS_MODULE, | |
2023 | .open = gfs2_sbstats_open, | |
7c52b166 RP |
2024 | .read = seq_read, |
2025 | .llseek = seq_lseek, | |
81648d04 | 2026 | .release = seq_release, |
7c52b166 RP |
2027 | }; |
2028 | ||
2029 | int gfs2_create_debugfs_file(struct gfs2_sbd *sdp) | |
2030 | { | |
7b4ddfa7 CS |
2031 | struct dentry *dent; |
2032 | ||
2033 | dent = debugfs_create_dir(sdp->sd_table_name, gfs2_root); | |
2034 | if (IS_ERR_OR_NULL(dent)) | |
2035 | goto fail; | |
2036 | sdp->debugfs_dir = dent; | |
2037 | ||
2038 | dent = debugfs_create_file("glocks", | |
2039 | S_IFREG | S_IRUGO, | |
2040 | sdp->debugfs_dir, sdp, | |
2041 | &gfs2_glocks_fops); | |
2042 | if (IS_ERR_OR_NULL(dent)) | |
a245769f | 2043 | goto fail; |
7b4ddfa7 | 2044 | sdp->debugfs_dentry_glocks = dent; |
a245769f | 2045 | |
7b4ddfa7 CS |
2046 | dent = debugfs_create_file("glstats", |
2047 | S_IFREG | S_IRUGO, | |
2048 | sdp->debugfs_dir, sdp, | |
2049 | &gfs2_glstats_fops); | |
2050 | if (IS_ERR_OR_NULL(dent)) | |
a245769f | 2051 | goto fail; |
7b4ddfa7 | 2052 | sdp->debugfs_dentry_glstats = dent; |
a245769f | 2053 | |
7b4ddfa7 CS |
2054 | dent = debugfs_create_file("sbstats", |
2055 | S_IFREG | S_IRUGO, | |
2056 | sdp->debugfs_dir, sdp, | |
2057 | &gfs2_sbstats_fops); | |
2058 | if (IS_ERR_OR_NULL(dent)) | |
a245769f | 2059 | goto fail; |
7b4ddfa7 | 2060 | sdp->debugfs_dentry_sbstats = dent; |
7c52b166 RP |
2061 | |
2062 | return 0; | |
a245769f SW |
2063 | fail: |
2064 | gfs2_delete_debugfs_file(sdp); | |
7b4ddfa7 | 2065 | return dent ? PTR_ERR(dent) : -ENOMEM; |
7c52b166 RP |
2066 | } |
2067 | ||
2068 | void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp) | |
2069 | { | |
a245769f | 2070 | if (sdp->debugfs_dir) { |
5f882096 RP |
2071 | if (sdp->debugfs_dentry_glocks) { |
2072 | debugfs_remove(sdp->debugfs_dentry_glocks); | |
2073 | sdp->debugfs_dentry_glocks = NULL; | |
2074 | } | |
a245769f SW |
2075 | if (sdp->debugfs_dentry_glstats) { |
2076 | debugfs_remove(sdp->debugfs_dentry_glstats); | |
2077 | sdp->debugfs_dentry_glstats = NULL; | |
2078 | } | |
2079 | if (sdp->debugfs_dentry_sbstats) { | |
2080 | debugfs_remove(sdp->debugfs_dentry_sbstats); | |
2081 | sdp->debugfs_dentry_sbstats = NULL; | |
2082 | } | |
5f882096 RP |
2083 | debugfs_remove(sdp->debugfs_dir); |
2084 | sdp->debugfs_dir = NULL; | |
2085 | } | |
7c52b166 RP |
2086 | } |
2087 | ||
2088 | int gfs2_register_debugfs(void) | |
2089 | { | |
2090 | gfs2_root = debugfs_create_dir("gfs2", NULL); | |
7b4ddfa7 CS |
2091 | if (IS_ERR(gfs2_root)) |
2092 | return PTR_ERR(gfs2_root); | |
7c52b166 RP |
2093 | return gfs2_root ? 0 : -ENOMEM; |
2094 | } | |
2095 | ||
2096 | void gfs2_unregister_debugfs(void) | |
2097 | { | |
2098 | debugfs_remove(gfs2_root); | |
5f882096 | 2099 | gfs2_root = NULL; |
7c52b166 | 2100 | } |