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