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