]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/gfs2/glock.c
GFS2: Improve tracing support (adds two flags)
[mirror_ubuntu-bionic-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
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
b3b94faa
DT
13#include <linux/buffer_head.h>
14#include <linux/delay.h>
15#include <linux/sort.h>
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>
b3b94faa 22#include <asm/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>
b3b94faa
DT
32
33#include "gfs2.h"
5c676f6d 34#include "incore.h"
b3b94faa
DT
35#include "glock.h"
36#include "glops.h"
37#include "inode.h"
b3b94faa
DT
38#include "lops.h"
39#include "meta_io.h"
40#include "quota.h"
41#include "super.h"
5c676f6d 42#include "util.h"
813e0c46 43#include "bmap.h"
63997775
SW
44#define CREATE_TRACE_POINTS
45#include "trace_gfs2.h"
b3b94faa 46
6802e340
SW
47struct gfs2_glock_iter {
48 int hash; /* hash bucket index */
49 struct gfs2_sbd *sdp; /* incore superblock */
50 struct gfs2_glock *gl; /* current glock struct */
51 char string[512]; /* scratch space */
7c52b166
RP
52};
53
b3b94faa
DT
54typedef void (*glock_examiner) (struct gfs2_glock * gl);
55
6802e340
SW
56static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
57#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
58static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
c4f68a13 59
7c52b166 60static struct dentry *gfs2_root;
c4f68a13 61static struct workqueue_struct *glock_workqueue;
b94a170e 62struct workqueue_struct *gfs2_delete_workqueue;
97cc1025
SW
63static LIST_HEAD(lru_list);
64static atomic_t lru_count = ATOMIC_INIT(0);
eb8374e7 65static DEFINE_SPINLOCK(lru_lock);
08bc2dbc 66
b6397893 67#define GFS2_GL_HASH_SHIFT 15
087efdd3
SW
68#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
69#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
70
bc015cb8 71static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
04b933f2 72static struct dentry *gfs2_root;
087efdd3 73
b3b94faa
DT
74/**
75 * gl_hash() - Turn glock number into hash bucket number
76 * @lock: The glock number
77 *
78 * Returns: The number of the corresponding hash bucket
79 */
80
b8547856
SW
81static unsigned int gl_hash(const struct gfs2_sbd *sdp,
82 const struct lm_lockname *name)
b3b94faa
DT
83{
84 unsigned int h;
85
cd915493 86 h = jhash(&name->ln_number, sizeof(u64), 0);
b3b94faa 87 h = jhash(&name->ln_type, sizeof(unsigned int), h);
b8547856 88 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
b3b94faa
DT
89 h &= GFS2_GL_HASH_MASK;
90
91 return h;
92}
93
bc015cb8
SW
94static inline void spin_lock_bucket(unsigned int hash)
95{
96 struct hlist_bl_head *bl = &gl_hash_table[hash];
97 bit_spin_lock(0, (unsigned long *)bl);
98}
b3b94faa 99
bc015cb8
SW
100static inline void spin_unlock_bucket(unsigned int hash)
101{
102 struct hlist_bl_head *bl = &gl_hash_table[hash];
103 __bit_spin_unlock(0, (unsigned long *)bl);
104}
b3b94faa 105
fc0e38da 106static void gfs2_glock_dealloc(struct rcu_head *rcu)
b3b94faa 107{
bc015cb8 108 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
b3b94faa 109
bc015cb8
SW
110 if (gl->gl_ops->go_flags & GLOF_ASPACE)
111 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
112 else
113 kmem_cache_free(gfs2_glock_cachep, gl);
fc0e38da
SW
114}
115
116void gfs2_glock_free(struct gfs2_glock *gl)
b3b94faa
DT
117{
118 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa 119
fc0e38da 120 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
bc015cb8
SW
121 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
122 wake_up(&sdp->sd_glock_wait);
b3b94faa
DT
123}
124
125/**
126 * gfs2_glock_hold() - increment reference count on glock
127 * @gl: The glock to hold
128 *
129 */
130
b94a170e 131void gfs2_glock_hold(struct gfs2_glock *gl)
b3b94faa 132{
d8348de0 133 GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
16feb9fe 134 atomic_inc(&gl->gl_ref);
b3b94faa
DT
135}
136
8ff22a6f
BM
137/**
138 * demote_ok - Check to see if it's ok to unlock a glock
139 * @gl: the glock
140 *
141 * Returns: 1 if it's ok
142 */
143
144static int demote_ok(const struct gfs2_glock *gl)
145{
146 const struct gfs2_glock_operations *glops = gl->gl_ops;
147
bc015cb8
SW
148 /* assert_spin_locked(&gl->gl_spin); */
149
8ff22a6f
BM
150 if (gl->gl_state == LM_ST_UNLOCKED)
151 return 0;
bc015cb8
SW
152 if (test_bit(GLF_LFLUSH, &gl->gl_flags))
153 return 0;
154 if ((gl->gl_name.ln_type != LM_TYPE_INODE) &&
155 !list_empty(&gl->gl_holders))
8ff22a6f
BM
156 return 0;
157 if (glops->go_demote_ok)
158 return glops->go_demote_ok(gl);
159 return 1;
160}
161
bc015cb8 162
29687a2a
SW
163void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
164{
165 spin_lock(&lru_lock);
166
167 if (!list_empty(&gl->gl_lru))
168 list_del_init(&gl->gl_lru);
169 else
170 atomic_inc(&lru_count);
171
172 list_add_tail(&gl->gl_lru, &lru_list);
627c10b7 173 set_bit(GLF_LRU, &gl->gl_flags);
29687a2a
SW
174 spin_unlock(&lru_lock);
175}
176
97cc1025 177/**
bc015cb8 178 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
97cc1025
SW
179 * @gl: the glock
180 *
bc015cb8
SW
181 * If the glock is demotable, then we add it (or move it) to the end
182 * of the glock LRU list.
97cc1025
SW
183 */
184
bc015cb8 185static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
97cc1025 186{
29687a2a
SW
187 if (demote_ok(gl))
188 gfs2_glock_add_to_lru(gl);
97cc1025
SW
189}
190
8ff22a6f
BM
191/**
192 * gfs2_glock_put_nolock() - Decrement reference count on glock
193 * @gl: The glock to put
194 *
195 * This function should only be used if the caller has its own reference
196 * to the glock, in addition to the one it is dropping.
197 */
198
b94a170e 199void gfs2_glock_put_nolock(struct gfs2_glock *gl)
8ff22a6f
BM
200{
201 if (atomic_dec_and_test(&gl->gl_ref))
202 GLOCK_BUG_ON(gl, 1);
8ff22a6f
BM
203}
204
b3b94faa
DT
205/**
206 * gfs2_glock_put() - Decrement reference count on glock
207 * @gl: The glock to put
208 *
209 */
210
bc015cb8 211void gfs2_glock_put(struct gfs2_glock *gl)
b3b94faa 212{
bc015cb8
SW
213 struct gfs2_sbd *sdp = gl->gl_sbd;
214 struct address_space *mapping = gfs2_glock2aspace(gl);
b3b94faa 215
bc015cb8
SW
216 if (atomic_dec_and_test(&gl->gl_ref)) {
217 spin_lock_bucket(gl->gl_hash);
218 hlist_bl_del_rcu(&gl->gl_list);
219 spin_unlock_bucket(gl->gl_hash);
220 spin_lock(&lru_lock);
97cc1025
SW
221 if (!list_empty(&gl->gl_lru)) {
222 list_del_init(&gl->gl_lru);
223 atomic_dec(&lru_count);
224 }
225 spin_unlock(&lru_lock);
6802e340 226 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
bc015cb8
SW
227 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
228 trace_gfs2_glock_put(gl);
229 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
b3b94faa 230 }
b3b94faa
DT
231}
232
b3b94faa
DT
233/**
234 * search_bucket() - Find struct gfs2_glock by lock number
235 * @bucket: the bucket to search
236 * @name: The lock name
237 *
238 * Returns: NULL, or the struct gfs2_glock with the requested number
239 */
240
37b2fa6a 241static struct gfs2_glock *search_bucket(unsigned int hash,
899be4d3 242 const struct gfs2_sbd *sdp,
d6a53727 243 const struct lm_lockname *name)
b3b94faa
DT
244{
245 struct gfs2_glock *gl;
bc015cb8 246 struct hlist_bl_node *h;
b3b94faa 247
bc015cb8 248 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
b3b94faa
DT
249 if (!lm_name_equal(&gl->gl_name, name))
250 continue;
899be4d3
SW
251 if (gl->gl_sbd != sdp)
252 continue;
bc015cb8
SW
253 if (atomic_inc_not_zero(&gl->gl_ref))
254 return gl;
b3b94faa
DT
255 }
256
257 return NULL;
258}
259
6802e340
SW
260/**
261 * may_grant - check if its ok to grant a new lock
262 * @gl: The glock
263 * @gh: The lock request which we wish to grant
264 *
265 * Returns: true if its ok to grant the lock
266 */
267
268static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
269{
270 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
271 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
272 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
273 return 0;
274 if (gl->gl_state == gh->gh_state)
275 return 1;
276 if (gh->gh_flags & GL_EXACT)
277 return 0;
209806ab
SW
278 if (gl->gl_state == LM_ST_EXCLUSIVE) {
279 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
280 return 1;
281 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
282 return 1;
283 }
6802e340
SW
284 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
285 return 1;
286 return 0;
287}
288
289static void gfs2_holder_wake(struct gfs2_holder *gh)
290{
291 clear_bit(HIF_WAIT, &gh->gh_iflags);
292 smp_mb__after_clear_bit();
293 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
294}
295
d5341a92
SW
296/**
297 * do_error - Something unexpected has happened during a lock request
298 *
299 */
300
301static inline void do_error(struct gfs2_glock *gl, const int ret)
302{
303 struct gfs2_holder *gh, *tmp;
304
305 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
306 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
307 continue;
308 if (ret & LM_OUT_ERROR)
309 gh->gh_error = -EIO;
310 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
311 gh->gh_error = GLR_TRYFAILED;
312 else
313 continue;
314 list_del_init(&gh->gh_list);
315 trace_gfs2_glock_queue(gh, 0);
316 gfs2_holder_wake(gh);
317 }
318}
319
6802e340
SW
320/**
321 * do_promote - promote as many requests as possible on the current queue
322 * @gl: The glock
323 *
813e0c46
SW
324 * Returns: 1 if there is a blocked holder at the head of the list, or 2
325 * if a type specific operation is underway.
6802e340
SW
326 */
327
328static int do_promote(struct gfs2_glock *gl)
55ba474d
HH
329__releases(&gl->gl_spin)
330__acquires(&gl->gl_spin)
6802e340
SW
331{
332 const struct gfs2_glock_operations *glops = gl->gl_ops;
333 struct gfs2_holder *gh, *tmp;
334 int ret;
335
336restart:
337 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
338 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
339 continue;
340 if (may_grant(gl, gh)) {
341 if (gh->gh_list.prev == &gl->gl_holders &&
342 glops->go_lock) {
343 spin_unlock(&gl->gl_spin);
344 /* FIXME: eliminate this eventually */
345 ret = glops->go_lock(gh);
346 spin_lock(&gl->gl_spin);
347 if (ret) {
813e0c46
SW
348 if (ret == 1)
349 return 2;
6802e340
SW
350 gh->gh_error = ret;
351 list_del_init(&gh->gh_list);
63997775 352 trace_gfs2_glock_queue(gh, 0);
6802e340
SW
353 gfs2_holder_wake(gh);
354 goto restart;
355 }
356 set_bit(HIF_HOLDER, &gh->gh_iflags);
63997775 357 trace_gfs2_promote(gh, 1);
6802e340
SW
358 gfs2_holder_wake(gh);
359 goto restart;
360 }
361 set_bit(HIF_HOLDER, &gh->gh_iflags);
63997775 362 trace_gfs2_promote(gh, 0);
6802e340
SW
363 gfs2_holder_wake(gh);
364 continue;
365 }
366 if (gh->gh_list.prev == &gl->gl_holders)
367 return 1;
d5341a92 368 do_error(gl, 0);
6802e340
SW
369 break;
370 }
371 return 0;
372}
373
6802e340
SW
374/**
375 * find_first_waiter - find the first gh that's waiting for the glock
376 * @gl: the glock
377 */
378
379static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
380{
381 struct gfs2_holder *gh;
382
383 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
384 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
385 return gh;
386 }
387 return NULL;
388}
389
390/**
391 * state_change - record that the glock is now in a different state
392 * @gl: the glock
393 * @new_state the new state
394 *
395 */
396
397static void state_change(struct gfs2_glock *gl, unsigned int new_state)
398{
399 int held1, held2;
400
401 held1 = (gl->gl_state != LM_ST_UNLOCKED);
402 held2 = (new_state != LM_ST_UNLOCKED);
403
404 if (held1 != held2) {
405 if (held2)
406 gfs2_glock_hold(gl);
407 else
8ff22a6f 408 gfs2_glock_put_nolock(gl);
6802e340 409 }
7b5e3d5f
SW
410 if (held1 && held2 && list_empty(&gl->gl_holders))
411 clear_bit(GLF_QUEUED, &gl->gl_flags);
6802e340
SW
412
413 gl->gl_state = new_state;
414 gl->gl_tchange = jiffies;
415}
416
417static void gfs2_demote_wake(struct gfs2_glock *gl)
418{
419 gl->gl_demote_state = LM_ST_EXCLUSIVE;
420 clear_bit(GLF_DEMOTE, &gl->gl_flags);
421 smp_mb__after_clear_bit();
422 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
423}
424
425/**
426 * finish_xmote - The DLM has replied to one of our lock requests
427 * @gl: The glock
428 * @ret: The status from the DLM
429 *
430 */
431
432static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
433{
434 const struct gfs2_glock_operations *glops = gl->gl_ops;
435 struct gfs2_holder *gh;
436 unsigned state = ret & LM_OUT_ST_MASK;
813e0c46 437 int rv;
6802e340
SW
438
439 spin_lock(&gl->gl_spin);
63997775 440 trace_gfs2_glock_state_change(gl, state);
6802e340
SW
441 state_change(gl, state);
442 gh = find_first_waiter(gl);
443
444 /* Demote to UN request arrived during demote to SH or DF */
445 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
446 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
447 gl->gl_target = LM_ST_UNLOCKED;
448
449 /* Check for state != intended state */
450 if (unlikely(state != gl->gl_target)) {
451 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
452 /* move to back of queue and try next entry */
453 if (ret & LM_OUT_CANCELED) {
454 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
455 list_move_tail(&gh->gh_list, &gl->gl_holders);
456 gh = find_first_waiter(gl);
457 gl->gl_target = gh->gh_state;
458 goto retry;
459 }
460 /* Some error or failed "try lock" - report it */
461 if ((ret & LM_OUT_ERROR) ||
462 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
463 gl->gl_target = gl->gl_state;
464 do_error(gl, ret);
465 goto out;
466 }
467 }
468 switch(state) {
469 /* Unlocked due to conversion deadlock, try again */
470 case LM_ST_UNLOCKED:
471retry:
472 do_xmote(gl, gh, gl->gl_target);
473 break;
474 /* Conversion fails, unlock and try again */
475 case LM_ST_SHARED:
476 case LM_ST_DEFERRED:
477 do_xmote(gl, gh, LM_ST_UNLOCKED);
478 break;
479 default: /* Everything else */
480 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
481 GLOCK_BUG_ON(gl, 1);
482 }
483 spin_unlock(&gl->gl_spin);
6802e340
SW
484 return;
485 }
486
487 /* Fast path - we got what we asked for */
488 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
489 gfs2_demote_wake(gl);
490 if (state != LM_ST_UNLOCKED) {
491 if (glops->go_xmote_bh) {
6802e340
SW
492 spin_unlock(&gl->gl_spin);
493 rv = glops->go_xmote_bh(gl, gh);
6802e340
SW
494 spin_lock(&gl->gl_spin);
495 if (rv) {
496 do_error(gl, rv);
497 goto out;
498 }
499 }
813e0c46
SW
500 rv = do_promote(gl);
501 if (rv == 2)
502 goto out_locked;
6802e340
SW
503 }
504out:
505 clear_bit(GLF_LOCK, &gl->gl_flags);
813e0c46 506out_locked:
6802e340 507 spin_unlock(&gl->gl_spin);
6802e340
SW
508}
509
6802e340
SW
510/**
511 * do_xmote - Calls the DLM to change the state of a lock
512 * @gl: The lock state
513 * @gh: The holder (only for promotes)
514 * @target: The target lock state
515 *
516 */
517
518static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
55ba474d
HH
519__releases(&gl->gl_spin)
520__acquires(&gl->gl_spin)
6802e340
SW
521{
522 const struct gfs2_glock_operations *glops = gl->gl_ops;
523 struct gfs2_sbd *sdp = gl->gl_sbd;
524 unsigned int lck_flags = gh ? gh->gh_flags : 0;
525 int ret;
526
527 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
528 LM_FLAG_PRIORITY);
921169ca
SW
529 GLOCK_BUG_ON(gl, gl->gl_state == target);
530 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
6802e340
SW
531 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
532 glops->go_inval) {
533 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
534 do_error(gl, 0); /* Fail queued try locks */
535 }
47a25380 536 gl->gl_req = target;
6802e340
SW
537 spin_unlock(&gl->gl_spin);
538 if (glops->go_xmote_th)
539 glops->go_xmote_th(gl);
540 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
541 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
542 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
543
544 gfs2_glock_hold(gl);
545 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
546 gl->gl_state == LM_ST_DEFERRED) &&
547 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
548 lck_flags |= LM_FLAG_TRY_1CB;
6802e340 549
921169ca
SW
550 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
551 /* lock_dlm */
552 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
553 GLOCK_BUG_ON(gl, ret);
554 } else { /* lock_nolock */
555 finish_xmote(gl, target);
6802e340
SW
556 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
557 gfs2_glock_put(gl);
6802e340 558 }
921169ca 559
6802e340
SW
560 spin_lock(&gl->gl_spin);
561}
562
563/**
564 * find_first_holder - find the first "holder" gh
565 * @gl: the glock
566 */
567
568static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
569{
570 struct gfs2_holder *gh;
571
572 if (!list_empty(&gl->gl_holders)) {
573 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
574 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
575 return gh;
576 }
577 return NULL;
578}
579
580/**
581 * run_queue - do all outstanding tasks related to a glock
582 * @gl: The glock in question
583 * @nonblock: True if we must not block in run_queue
584 *
585 */
586
587static void run_queue(struct gfs2_glock *gl, const int nonblock)
55ba474d
HH
588__releases(&gl->gl_spin)
589__acquires(&gl->gl_spin)
6802e340
SW
590{
591 struct gfs2_holder *gh = NULL;
813e0c46 592 int ret;
6802e340
SW
593
594 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
595 return;
596
597 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
598
599 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
600 gl->gl_demote_state != gl->gl_state) {
601 if (find_first_holder(gl))
d8348de0 602 goto out_unlock;
6802e340
SW
603 if (nonblock)
604 goto out_sched;
605 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
265d529c 606 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
6802e340
SW
607 gl->gl_target = gl->gl_demote_state;
608 } else {
609 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
610 gfs2_demote_wake(gl);
813e0c46
SW
611 ret = do_promote(gl);
612 if (ret == 0)
d8348de0 613 goto out_unlock;
813e0c46 614 if (ret == 2)
a228df63 615 goto out;
6802e340
SW
616 gh = find_first_waiter(gl);
617 gl->gl_target = gh->gh_state;
618 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
619 do_error(gl, 0); /* Fail queued try locks */
620 }
621 do_xmote(gl, gh, gl->gl_target);
a228df63 622out:
6802e340
SW
623 return;
624
625out_sched:
7e71c55e
SW
626 clear_bit(GLF_LOCK, &gl->gl_flags);
627 smp_mb__after_clear_bit();
6802e340
SW
628 gfs2_glock_hold(gl);
629 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
8ff22a6f 630 gfs2_glock_put_nolock(gl);
7e71c55e
SW
631 return;
632
d8348de0 633out_unlock:
6802e340 634 clear_bit(GLF_LOCK, &gl->gl_flags);
7e71c55e
SW
635 smp_mb__after_clear_bit();
636 return;
6802e340
SW
637}
638
b94a170e
BM
639static void delete_work_func(struct work_struct *work)
640{
641 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
642 struct gfs2_sbd *sdp = gl->gl_sbd;
044b9414 643 struct gfs2_inode *ip;
b94a170e 644 struct inode *inode;
044b9414
SW
645 u64 no_addr = gl->gl_name.ln_number;
646
647 ip = gl->gl_object;
648 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
b94a170e 649
b94a170e 650 if (ip)
b94a170e 651 inode = gfs2_ilookup(sdp->sd_vfs, no_addr);
044b9414
SW
652 else
653 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
654 if (inode && !IS_ERR(inode)) {
655 d_prune_aliases(inode);
656 iput(inode);
b94a170e
BM
657 }
658 gfs2_glock_put(gl);
659}
660
c4f68a13
BM
661static void glock_work_func(struct work_struct *work)
662{
6802e340 663 unsigned long delay = 0;
c4f68a13 664 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
26bb7505 665 int drop_ref = 0;
c4f68a13 666
26bb7505 667 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
6802e340 668 finish_xmote(gl, gl->gl_reply);
26bb7505
SW
669 drop_ref = 1;
670 }
c4f68a13 671 spin_lock(&gl->gl_spin);
265d529c
SW
672 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
673 gl->gl_state != LM_ST_UNLOCKED &&
674 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
6802e340
SW
675 unsigned long holdtime, now = jiffies;
676 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
677 if (time_before(now, holdtime))
678 delay = holdtime - now;
679 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
680 }
681 run_queue(gl, 0);
c4f68a13 682 spin_unlock(&gl->gl_spin);
6802e340
SW
683 if (!delay ||
684 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
685 gfs2_glock_put(gl);
26bb7505
SW
686 if (drop_ref)
687 gfs2_glock_put(gl);
c4f68a13
BM
688}
689
b3b94faa
DT
690/**
691 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
692 * @sdp: The GFS2 superblock
693 * @number: the lock number
694 * @glops: The glock_operations to use
695 * @create: If 0, don't create the glock if it doesn't exist
696 * @glp: the glock is returned here
697 *
698 * This does not lock a glock, just finds/creates structures for one.
699 *
700 * Returns: errno
701 */
702
cd915493 703int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
8fb4b536 704 const struct gfs2_glock_operations *glops, int create,
b3b94faa
DT
705 struct gfs2_glock **glp)
706{
009d8518 707 struct super_block *s = sdp->sd_vfs;
37b2fa6a 708 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
b3b94faa 709 struct gfs2_glock *gl, *tmp;
37b2fa6a 710 unsigned int hash = gl_hash(sdp, &name);
009d8518 711 struct address_space *mapping;
bc015cb8 712 struct kmem_cache *cachep;
b3b94faa 713
bc015cb8 714 rcu_read_lock();
37b2fa6a 715 gl = search_bucket(hash, sdp, &name);
bc015cb8 716 rcu_read_unlock();
b3b94faa 717
64d576ba
SW
718 *glp = gl;
719 if (gl)
b3b94faa 720 return 0;
64d576ba
SW
721 if (!create)
722 return -ENOENT;
b3b94faa 723
009d8518 724 if (glops->go_flags & GLOF_ASPACE)
bc015cb8 725 cachep = gfs2_glock_aspace_cachep;
009d8518 726 else
bc015cb8
SW
727 cachep = gfs2_glock_cachep;
728 gl = kmem_cache_alloc(cachep, GFP_KERNEL);
b3b94faa
DT
729 if (!gl)
730 return -ENOMEM;
731
8f05228e 732 atomic_inc(&sdp->sd_glock_disposal);
ec45d9f5 733 gl->gl_flags = 0;
b3b94faa 734 gl->gl_name = name;
16feb9fe 735 atomic_set(&gl->gl_ref, 1);
b3b94faa 736 gl->gl_state = LM_ST_UNLOCKED;
6802e340 737 gl->gl_target = LM_ST_UNLOCKED;
c4f68a13 738 gl->gl_demote_state = LM_ST_EXCLUSIVE;
37b2fa6a 739 gl->gl_hash = hash;
b3b94faa 740 gl->gl_ops = glops;
f057f6cd
SW
741 snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
742 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
743 gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
c4f68a13 744 gl->gl_tchange = jiffies;
ec45d9f5 745 gl->gl_object = NULL;
b3b94faa 746 gl->gl_sbd = sdp;
c4f68a13 747 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
b94a170e 748 INIT_WORK(&gl->gl_delete, delete_work_func);
b3b94faa 749
009d8518
SW
750 mapping = gfs2_glock2aspace(gl);
751 if (mapping) {
752 mapping->a_ops = &gfs2_meta_aops;
753 mapping->host = s->s_bdev->bd_inode;
754 mapping->flags = 0;
755 mapping_set_gfp_mask(mapping, GFP_NOFS);
756 mapping->assoc_mapping = NULL;
757 mapping->backing_dev_info = s->s_bdi;
758 mapping->writeback_index = 0;
b3b94faa
DT
759 }
760
bc015cb8 761 spin_lock_bucket(hash);
37b2fa6a 762 tmp = search_bucket(hash, sdp, &name);
b3b94faa 763 if (tmp) {
bc015cb8
SW
764 spin_unlock_bucket(hash);
765 kmem_cache_free(cachep, gl);
fc0e38da 766 atomic_dec(&sdp->sd_glock_disposal);
b3b94faa
DT
767 gl = tmp;
768 } else {
bc015cb8
SW
769 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
770 spin_unlock_bucket(hash);
b3b94faa
DT
771 }
772
773 *glp = gl;
774
775 return 0;
b3b94faa
DT
776}
777
778/**
779 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
780 * @gl: the glock
781 * @state: the state we're requesting
782 * @flags: the modifier flags
783 * @gh: the holder structure
784 *
785 */
786
190562bd 787void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
b3b94faa
DT
788 struct gfs2_holder *gh)
789{
790 INIT_LIST_HEAD(&gh->gh_list);
791 gh->gh_gl = gl;
d0dc80db 792 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b1e058da 793 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
794 gh->gh_state = state;
795 gh->gh_flags = flags;
796 gh->gh_error = 0;
797 gh->gh_iflags = 0;
b3b94faa
DT
798 gfs2_glock_hold(gl);
799}
800
801/**
802 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
803 * @state: the state we're requesting
804 * @flags: the modifier flags
805 * @gh: the holder structure
806 *
807 * Don't mess with the glock.
808 *
809 */
810
190562bd 811void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
b3b94faa
DT
812{
813 gh->gh_state = state;
579b78a4 814 gh->gh_flags = flags;
3b8249f6 815 gh->gh_iflags = 0;
d0dc80db 816 gh->gh_ip = (unsigned long)__builtin_return_address(0);
1a0eae88
BP
817 if (gh->gh_owner_pid)
818 put_pid(gh->gh_owner_pid);
819 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
820}
821
822/**
823 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
824 * @gh: the holder structure
825 *
826 */
827
828void gfs2_holder_uninit(struct gfs2_holder *gh)
829{
b1e058da 830 put_pid(gh->gh_owner_pid);
b3b94faa
DT
831 gfs2_glock_put(gh->gh_gl);
832 gh->gh_gl = NULL;
d0dc80db 833 gh->gh_ip = 0;
b3b94faa
DT
834}
835
fe64d517
SW
836/**
837 * gfs2_glock_holder_wait
838 * @word: unused
839 *
840 * This function and gfs2_glock_demote_wait both show up in the WCHAN
841 * field. Thus I've separated these otherwise identical functions in
842 * order to be more informative to the user.
843 */
844
845static int gfs2_glock_holder_wait(void *word)
fee852e3
SW
846{
847 schedule();
848 return 0;
849}
850
fe64d517
SW
851static int gfs2_glock_demote_wait(void *word)
852{
853 schedule();
854 return 0;
855}
856
6802e340 857static void wait_on_holder(struct gfs2_holder *gh)
da755fdb 858{
6802e340 859 might_sleep();
fe64d517 860 wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
da755fdb
SW
861}
862
6802e340 863static void wait_on_demote(struct gfs2_glock *gl)
b3b94faa 864{
6802e340 865 might_sleep();
fe64d517 866 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
b3b94faa
DT
867}
868
869/**
6802e340
SW
870 * handle_callback - process a demote request
871 * @gl: the glock
872 * @state: the state the caller wants us to change to
b3b94faa 873 *
6802e340
SW
874 * There are only two requests that we are going to see in actual
875 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
b3b94faa
DT
876 */
877
6802e340 878static void handle_callback(struct gfs2_glock *gl, unsigned int state,
97cc1025 879 unsigned long delay)
b3b94faa 880{
6802e340 881 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
b3b94faa 882
6802e340
SW
883 set_bit(bit, &gl->gl_flags);
884 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
885 gl->gl_demote_state = state;
886 gl->gl_demote_time = jiffies;
6802e340
SW
887 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
888 gl->gl_demote_state != state) {
889 gl->gl_demote_state = LM_ST_UNLOCKED;
b3b94faa 890 }
b94a170e
BM
891 if (gl->gl_ops->go_callback)
892 gl->gl_ops->go_callback(gl);
63997775 893 trace_gfs2_demote_rq(gl);
b3b94faa
DT
894}
895
896/**
6802e340 897 * gfs2_glock_wait - wait on a glock acquisition
b3b94faa
DT
898 * @gh: the glock holder
899 *
900 * Returns: 0 on success
901 */
902
6802e340 903int gfs2_glock_wait(struct gfs2_holder *gh)
b3b94faa 904{
fee852e3 905 wait_on_holder(gh);
b3b94faa
DT
906 return gh->gh_error;
907}
908
6802e340 909void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
7c52b166 910{
5e69069c 911 struct va_format vaf;
7c52b166
RP
912 va_list args;
913
914 va_start(args, fmt);
5e69069c 915
6802e340
SW
916 if (seq) {
917 struct gfs2_glock_iter *gi = seq->private;
7c52b166 918 vsprintf(gi->string, fmt, args);
6802e340
SW
919 seq_printf(seq, gi->string);
920 } else {
5e69069c
JP
921 vaf.fmt = fmt;
922 vaf.va = &args;
923
924 printk(KERN_ERR " %pV", &vaf);
6802e340 925 }
5e69069c 926
7c52b166
RP
927 va_end(args);
928}
929
b3b94faa
DT
930/**
931 * add_to_queue - Add a holder to the wait queue (but look for recursion)
932 * @gh: the holder structure to add
933 *
6802e340
SW
934 * Eventually we should move the recursive locking trap to a
935 * debugging option or something like that. This is the fast
936 * path and needs to have the minimum number of distractions.
937 *
b3b94faa
DT
938 */
939
6802e340 940static inline void add_to_queue(struct gfs2_holder *gh)
55ba474d
HH
941__releases(&gl->gl_spin)
942__acquires(&gl->gl_spin)
b3b94faa
DT
943{
944 struct gfs2_glock *gl = gh->gh_gl;
6802e340
SW
945 struct gfs2_sbd *sdp = gl->gl_sbd;
946 struct list_head *insert_pt = NULL;
947 struct gfs2_holder *gh2;
948 int try_lock = 0;
b3b94faa 949
b1e058da 950 BUG_ON(gh->gh_owner_pid == NULL);
fee852e3
SW
951 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
952 BUG();
190562bd 953
6802e340
SW
954 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
955 if (test_bit(GLF_LOCK, &gl->gl_flags))
956 try_lock = 1;
957 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
958 goto fail;
959 }
960
961 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
962 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
963 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
964 goto trap_recursive;
965 if (try_lock &&
966 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
967 !may_grant(gl, gh)) {
968fail:
969 gh->gh_error = GLR_TRYFAILED;
970 gfs2_holder_wake(gh);
971 return;
b4c20166 972 }
6802e340
SW
973 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
974 continue;
975 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
976 insert_pt = &gh2->gh_list;
977 }
7b5e3d5f 978 set_bit(GLF_QUEUED, &gl->gl_flags);
edae38a6 979 trace_gfs2_glock_queue(gh, 1);
6802e340
SW
980 if (likely(insert_pt == NULL)) {
981 list_add_tail(&gh->gh_list, &gl->gl_holders);
982 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
983 goto do_cancel;
984 return;
985 }
986 list_add_tail(&gh->gh_list, insert_pt);
987do_cancel:
988 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
989 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
990 spin_unlock(&gl->gl_spin);
048bca22 991 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
f057f6cd 992 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
6802e340 993 spin_lock(&gl->gl_spin);
b3b94faa 994 }
6802e340 995 return;
b3b94faa 996
6802e340
SW
997trap_recursive:
998 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
999 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1000 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1001 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1002 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
1003 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
1004 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1005 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1006 __dump_glock(NULL, gl);
1007 BUG();
b3b94faa
DT
1008}
1009
1010/**
1011 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1012 * @gh: the holder structure
1013 *
1014 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1015 *
1016 * Returns: 0, GLR_TRYFAILED, or errno on failure
1017 */
1018
1019int gfs2_glock_nq(struct gfs2_holder *gh)
1020{
1021 struct gfs2_glock *gl = gh->gh_gl;
1022 struct gfs2_sbd *sdp = gl->gl_sbd;
1023 int error = 0;
1024
6802e340 1025 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
b3b94faa 1026 return -EIO;
b3b94faa 1027
b3b94faa
DT
1028 spin_lock(&gl->gl_spin);
1029 add_to_queue(gh);
0809f6ec
SW
1030 if ((LM_FLAG_NOEXP & gh->gh_flags) &&
1031 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1032 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
6802e340 1033 run_queue(gl, 1);
b3b94faa
DT
1034 spin_unlock(&gl->gl_spin);
1035
6802e340
SW
1036 if (!(gh->gh_flags & GL_ASYNC))
1037 error = gfs2_glock_wait(gh);
b3b94faa 1038
b3b94faa
DT
1039 return error;
1040}
1041
1042/**
1043 * gfs2_glock_poll - poll to see if an async request has been completed
1044 * @gh: the holder
1045 *
1046 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1047 */
1048
1049int gfs2_glock_poll(struct gfs2_holder *gh)
1050{
6802e340 1051 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
b3b94faa
DT
1052}
1053
1054/**
1055 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1056 * @gh: the glock holder
1057 *
1058 */
1059
1060void gfs2_glock_dq(struct gfs2_holder *gh)
1061{
1062 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1063 const struct gfs2_glock_operations *glops = gl->gl_ops;
c4f68a13 1064 unsigned delay = 0;
6802e340 1065 int fast_path = 0;
b3b94faa 1066
6802e340 1067 spin_lock(&gl->gl_spin);
b3b94faa 1068 if (gh->gh_flags & GL_NOCACHE)
97cc1025 1069 handle_callback(gl, LM_ST_UNLOCKED, 0);
b3b94faa 1070
b3b94faa 1071 list_del_init(&gh->gh_list);
6802e340 1072 if (find_first_holder(gl) == NULL) {
3042a2cc 1073 if (glops->go_unlock) {
6802e340 1074 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
3042a2cc 1075 spin_unlock(&gl->gl_spin);
b3b94faa 1076 glops->go_unlock(gh);
3042a2cc 1077 spin_lock(&gl->gl_spin);
6802e340 1078 clear_bit(GLF_LOCK, &gl->gl_flags);
3042a2cc 1079 }
6802e340
SW
1080 if (list_empty(&gl->gl_holders) &&
1081 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1082 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1083 fast_path = 1;
b3b94faa 1084 }
bc015cb8 1085 __gfs2_glock_schedule_for_reclaim(gl);
63997775 1086 trace_gfs2_glock_queue(gh, 0);
b3b94faa 1087 spin_unlock(&gl->gl_spin);
6802e340
SW
1088 if (likely(fast_path))
1089 return;
c4f68a13
BM
1090
1091 gfs2_glock_hold(gl);
1092 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1093 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1094 delay = gl->gl_ops->go_min_hold_time;
1095 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1096 gfs2_glock_put(gl);
b3b94faa
DT
1097}
1098
d93cfa98
AD
1099void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1100{
1101 struct gfs2_glock *gl = gh->gh_gl;
1102 gfs2_glock_dq(gh);
1103 wait_on_demote(gl);
1104}
1105
b3b94faa
DT
1106/**
1107 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1108 * @gh: the holder structure
1109 *
1110 */
1111
1112void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1113{
1114 gfs2_glock_dq(gh);
1115 gfs2_holder_uninit(gh);
1116}
1117
1118/**
1119 * gfs2_glock_nq_num - acquire a glock based on lock number
1120 * @sdp: the filesystem
1121 * @number: the lock number
1122 * @glops: the glock operations for the type of glock
1123 * @state: the state to acquire the glock in
25985edc 1124 * @flags: modifier flags for the acquisition
b3b94faa
DT
1125 * @gh: the struct gfs2_holder
1126 *
1127 * Returns: errno
1128 */
1129
cd915493 1130int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
8fb4b536
SW
1131 const struct gfs2_glock_operations *glops,
1132 unsigned int state, int flags, struct gfs2_holder *gh)
b3b94faa
DT
1133{
1134 struct gfs2_glock *gl;
1135 int error;
1136
1137 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1138 if (!error) {
1139 error = gfs2_glock_nq_init(gl, state, flags, gh);
1140 gfs2_glock_put(gl);
1141 }
1142
1143 return error;
1144}
1145
1146/**
1147 * glock_compare - Compare two struct gfs2_glock structures for sorting
1148 * @arg_a: the first structure
1149 * @arg_b: the second structure
1150 *
1151 */
1152
1153static int glock_compare(const void *arg_a, const void *arg_b)
1154{
a5e08a9e
SW
1155 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1156 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1157 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1158 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
b3b94faa
DT
1159
1160 if (a->ln_number > b->ln_number)
a5e08a9e
SW
1161 return 1;
1162 if (a->ln_number < b->ln_number)
1163 return -1;
1c0f4872 1164 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
a5e08a9e 1165 return 0;
b3b94faa
DT
1166}
1167
1168/**
1169 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1170 * @num_gh: the number of structures
1171 * @ghs: an array of struct gfs2_holder structures
1172 *
1173 * Returns: 0 on success (all glocks acquired),
1174 * errno on failure (no glocks acquired)
1175 */
1176
1177static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1178 struct gfs2_holder **p)
1179{
1180 unsigned int x;
1181 int error = 0;
1182
1183 for (x = 0; x < num_gh; x++)
1184 p[x] = &ghs[x];
1185
1186 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1187
1188 for (x = 0; x < num_gh; x++) {
1189 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1190
1191 error = gfs2_glock_nq(p[x]);
1192 if (error) {
1193 while (x--)
1194 gfs2_glock_dq(p[x]);
1195 break;
1196 }
1197 }
1198
1199 return error;
1200}
1201
1202/**
1203 * gfs2_glock_nq_m - acquire multiple glocks
1204 * @num_gh: the number of structures
1205 * @ghs: an array of struct gfs2_holder structures
1206 *
b3b94faa
DT
1207 *
1208 * Returns: 0 on success (all glocks acquired),
1209 * errno on failure (no glocks acquired)
1210 */
1211
1212int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1213{
eaf5bd3c
SW
1214 struct gfs2_holder *tmp[4];
1215 struct gfs2_holder **pph = tmp;
b3b94faa
DT
1216 int error = 0;
1217
eaf5bd3c
SW
1218 switch(num_gh) {
1219 case 0:
b3b94faa 1220 return 0;
eaf5bd3c 1221 case 1:
b3b94faa
DT
1222 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1223 return gfs2_glock_nq(ghs);
eaf5bd3c
SW
1224 default:
1225 if (num_gh <= 4)
b3b94faa 1226 break;
eaf5bd3c
SW
1227 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1228 if (!pph)
1229 return -ENOMEM;
b3b94faa
DT
1230 }
1231
eaf5bd3c 1232 error = nq_m_sync(num_gh, ghs, pph);
b3b94faa 1233
eaf5bd3c
SW
1234 if (pph != tmp)
1235 kfree(pph);
b3b94faa
DT
1236
1237 return error;
1238}
1239
1240/**
1241 * gfs2_glock_dq_m - release multiple glocks
1242 * @num_gh: the number of structures
1243 * @ghs: an array of struct gfs2_holder structures
1244 *
1245 */
1246
1247void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1248{
fa1bbdea
BP
1249 while (num_gh--)
1250 gfs2_glock_dq(&ghs[num_gh]);
b3b94faa
DT
1251}
1252
1253/**
1254 * gfs2_glock_dq_uninit_m - release multiple glocks
1255 * @num_gh: the number of structures
1256 * @ghs: an array of struct gfs2_holder structures
1257 *
1258 */
1259
1260void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1261{
fa1bbdea
BP
1262 while (num_gh--)
1263 gfs2_glock_dq_uninit(&ghs[num_gh]);
b3b94faa
DT
1264}
1265
f057f6cd 1266void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
da755fdb 1267{
c4f68a13
BM
1268 unsigned long delay = 0;
1269 unsigned long holdtime;
1270 unsigned long now = jiffies;
b3b94faa 1271
f057f6cd 1272 gfs2_glock_hold(gl);
c4f68a13 1273 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
7b5e3d5f
SW
1274 if (test_bit(GLF_QUEUED, &gl->gl_flags)) {
1275 if (time_before(now, holdtime))
1276 delay = holdtime - now;
1277 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1278 delay = gl->gl_ops->go_min_hold_time;
1279 }
b3b94faa 1280
6802e340 1281 spin_lock(&gl->gl_spin);
97cc1025 1282 handle_callback(gl, state, delay);
6802e340 1283 spin_unlock(&gl->gl_spin);
c4f68a13
BM
1284 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1285 gfs2_glock_put(gl);
b3b94faa
DT
1286}
1287
0809f6ec
SW
1288/**
1289 * gfs2_should_freeze - Figure out if glock should be frozen
1290 * @gl: The glock in question
1291 *
1292 * Glocks are not frozen if (a) the result of the dlm operation is
1293 * an error, (b) the locking operation was an unlock operation or
1294 * (c) if there is a "noexp" flagged request anywhere in the queue
1295 *
1296 * Returns: 1 if freezing should occur, 0 otherwise
1297 */
1298
1299static int gfs2_should_freeze(const struct gfs2_glock *gl)
1300{
1301 const struct gfs2_holder *gh;
1302
1303 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1304 return 0;
1305 if (gl->gl_target == LM_ST_UNLOCKED)
1306 return 0;
1307
1308 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1309 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1310 continue;
1311 if (LM_FLAG_NOEXP & gh->gh_flags)
1312 return 0;
1313 }
1314
1315 return 1;
1316}
1317
b3b94faa 1318/**
f057f6cd
SW
1319 * gfs2_glock_complete - Callback used by locking
1320 * @gl: Pointer to the glock
1321 * @ret: The return value from the dlm
b3b94faa 1322 *
47a25380
SW
1323 * The gl_reply field is under the gl_spin lock so that it is ok
1324 * to use a bitfield shared with other glock state fields.
b3b94faa
DT
1325 */
1326
f057f6cd 1327void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
b3b94faa 1328{
f057f6cd 1329 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
0809f6ec 1330
47a25380 1331 spin_lock(&gl->gl_spin);
f057f6cd 1332 gl->gl_reply = ret;
0809f6ec 1333
f057f6cd 1334 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
0809f6ec 1335 if (gfs2_should_freeze(gl)) {
f057f6cd 1336 set_bit(GLF_FROZEN, &gl->gl_flags);
0809f6ec 1337 spin_unlock(&gl->gl_spin);
b3b94faa 1338 return;
0809f6ec 1339 }
b3b94faa 1340 }
47a25380
SW
1341
1342 spin_unlock(&gl->gl_spin);
f057f6cd 1343 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
47a25380 1344 smp_wmb();
f057f6cd
SW
1345 gfs2_glock_hold(gl);
1346 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1347 gfs2_glock_put(gl);
b3b94faa
DT
1348}
1349
b3b94faa 1350
7f8275d0 1351static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
b3b94faa
DT
1352{
1353 struct gfs2_glock *gl;
97cc1025
SW
1354 int may_demote;
1355 int nr_skipped = 0;
97cc1025 1356 LIST_HEAD(skipped);
b3b94faa 1357
97cc1025
SW
1358 if (nr == 0)
1359 goto out;
b3b94faa 1360
97cc1025
SW
1361 if (!(gfp_mask & __GFP_FS))
1362 return -1;
b3b94faa 1363
97cc1025
SW
1364 spin_lock(&lru_lock);
1365 while(nr && !list_empty(&lru_list)) {
1366 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1367 list_del_init(&gl->gl_lru);
627c10b7 1368 clear_bit(GLF_LRU, &gl->gl_flags);
97cc1025
SW
1369 atomic_dec(&lru_count);
1370
1371 /* Test for being demotable */
1372 if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1373 gfs2_glock_hold(gl);
97cc1025
SW
1374 spin_unlock(&lru_lock);
1375 spin_lock(&gl->gl_spin);
1376 may_demote = demote_ok(gl);
97cc1025
SW
1377 if (may_demote) {
1378 handle_callback(gl, LM_ST_UNLOCKED, 0);
1379 nr--;
97cc1025 1380 }
7e71c55e
SW
1381 clear_bit(GLF_LOCK, &gl->gl_flags);
1382 smp_mb__after_clear_bit();
2163b1e6 1383 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
b94a170e
BM
1384 gfs2_glock_put_nolock(gl);
1385 spin_unlock(&gl->gl_spin);
97cc1025 1386 spin_lock(&lru_lock);
2163b1e6 1387 continue;
97cc1025 1388 }
2163b1e6
SW
1389 nr_skipped++;
1390 list_add(&gl->gl_lru, &skipped);
627c10b7 1391 set_bit(GLF_LRU, &gl->gl_flags);
b3b94faa 1392 }
97cc1025
SW
1393 list_splice(&skipped, &lru_list);
1394 atomic_add(nr_skipped, &lru_count);
1395 spin_unlock(&lru_lock);
1396out:
1397 return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
b3b94faa
DT
1398}
1399
97cc1025
SW
1400static struct shrinker glock_shrinker = {
1401 .shrink = gfs2_shrink_glock_memory,
1402 .seeks = DEFAULT_SEEKS,
1403};
1404
b3b94faa
DT
1405/**
1406 * examine_bucket - Call a function for glock in a hash bucket
1407 * @examiner: the function
1408 * @sdp: the filesystem
1409 * @bucket: the bucket
1410 *
b3b94faa
DT
1411 */
1412
bc015cb8 1413static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
37b2fa6a 1414 unsigned int hash)
b3b94faa 1415{
bc015cb8
SW
1416 struct gfs2_glock *gl;
1417 struct hlist_bl_head *head = &gl_hash_table[hash];
1418 struct hlist_bl_node *pos;
b3b94faa 1419
bc015cb8
SW
1420 rcu_read_lock();
1421 hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
1422 if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
24264434 1423 examiner(gl);
b3b94faa 1424 }
bc015cb8 1425 rcu_read_unlock();
8fbbfd21 1426 cond_resched();
bc015cb8
SW
1427}
1428
1429static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1430{
1431 unsigned x;
1432
1433 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1434 examine_bucket(examiner, sdp, x);
b3b94faa
DT
1435}
1436
f057f6cd
SW
1437
1438/**
1439 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1440 * @gl: The glock to thaw
1441 *
1442 * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
1443 * so this has to result in the ref count being dropped by one.
1444 */
1445
1446static void thaw_glock(struct gfs2_glock *gl)
1447{
1448 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1449 return;
f057f6cd
SW
1450 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1451 gfs2_glock_hold(gl);
1452 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1453 gfs2_glock_put(gl);
f057f6cd
SW
1454}
1455
b3b94faa
DT
1456/**
1457 * clear_glock - look at a glock and see if we can free it from glock cache
1458 * @gl: the glock to look at
1459 *
1460 */
1461
1462static void clear_glock(struct gfs2_glock *gl)
1463{
97cc1025
SW
1464 spin_lock(&lru_lock);
1465 if (!list_empty(&gl->gl_lru)) {
1466 list_del_init(&gl->gl_lru);
1467 atomic_dec(&lru_count);
b3b94faa 1468 }
97cc1025 1469 spin_unlock(&lru_lock);
b3b94faa 1470
6802e340 1471 spin_lock(&gl->gl_spin);
c741c455 1472 if (gl->gl_state != LM_ST_UNLOCKED)
97cc1025 1473 handle_callback(gl, LM_ST_UNLOCKED, 0);
6802e340
SW
1474 spin_unlock(&gl->gl_spin);
1475 gfs2_glock_hold(gl);
1476 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1477 gfs2_glock_put(gl);
b3b94faa
DT
1478}
1479
f057f6cd
SW
1480/**
1481 * gfs2_glock_thaw - Thaw any frozen glocks
1482 * @sdp: The super block
1483 *
1484 */
1485
1486void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1487{
bc015cb8
SW
1488 glock_hash_walk(thaw_glock, sdp);
1489}
f057f6cd 1490
bc015cb8
SW
1491static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1492{
1493 int ret;
1494 spin_lock(&gl->gl_spin);
1495 ret = __dump_glock(seq, gl);
1496 spin_unlock(&gl->gl_spin);
1497 return ret;
1498}
1499
1500static void dump_glock_func(struct gfs2_glock *gl)
1501{
1502 dump_glock(NULL, gl);
f057f6cd
SW
1503}
1504
b3b94faa
DT
1505/**
1506 * gfs2_gl_hash_clear - Empty out the glock hash table
1507 * @sdp: the filesystem
1508 * @wait: wait until it's all gone
1509 *
1bdad606 1510 * Called when unmounting the filesystem.
b3b94faa
DT
1511 */
1512
fefc03bf 1513void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
b3b94faa 1514{
bc015cb8 1515 glock_hash_walk(clear_glock, sdp);
8f05228e
SW
1516 flush_workqueue(glock_workqueue);
1517 wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
bc015cb8 1518 glock_hash_walk(dump_glock_func, sdp);
b3b94faa
DT
1519}
1520
813e0c46
SW
1521void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1522{
1523 struct gfs2_glock *gl = ip->i_gl;
1524 int ret;
1525
1526 ret = gfs2_truncatei_resume(ip);
1527 gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
1528
1529 spin_lock(&gl->gl_spin);
1530 clear_bit(GLF_LOCK, &gl->gl_flags);
1531 run_queue(gl, 1);
1532 spin_unlock(&gl->gl_spin);
1533}
1534
6802e340 1535static const char *state2str(unsigned state)
04b933f2 1536{
6802e340
SW
1537 switch(state) {
1538 case LM_ST_UNLOCKED:
1539 return "UN";
1540 case LM_ST_SHARED:
1541 return "SH";
1542 case LM_ST_DEFERRED:
1543 return "DF";
1544 case LM_ST_EXCLUSIVE:
1545 return "EX";
1546 }
1547 return "??";
1548}
1549
1550static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1551{
1552 char *p = buf;
1553 if (flags & LM_FLAG_TRY)
1554 *p++ = 't';
1555 if (flags & LM_FLAG_TRY_1CB)
1556 *p++ = 'T';
1557 if (flags & LM_FLAG_NOEXP)
1558 *p++ = 'e';
1559 if (flags & LM_FLAG_ANY)
f057f6cd 1560 *p++ = 'A';
6802e340
SW
1561 if (flags & LM_FLAG_PRIORITY)
1562 *p++ = 'p';
1563 if (flags & GL_ASYNC)
1564 *p++ = 'a';
1565 if (flags & GL_EXACT)
1566 *p++ = 'E';
6802e340
SW
1567 if (flags & GL_NOCACHE)
1568 *p++ = 'c';
1569 if (test_bit(HIF_HOLDER, &iflags))
1570 *p++ = 'H';
1571 if (test_bit(HIF_WAIT, &iflags))
1572 *p++ = 'W';
1573 if (test_bit(HIF_FIRST, &iflags))
1574 *p++ = 'F';
1575 *p = 0;
1576 return buf;
04b933f2
RP
1577}
1578
b3b94faa
DT
1579/**
1580 * dump_holder - print information about a glock holder
6802e340 1581 * @seq: the seq_file struct
b3b94faa
DT
1582 * @gh: the glock holder
1583 *
1584 * Returns: 0 on success, -ENOBUFS when we run out of space
1585 */
1586
6802e340 1587static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
b3b94faa 1588{
6802e340 1589 struct task_struct *gh_owner = NULL;
6802e340 1590 char flags_buf[32];
b3b94faa 1591
6802e340 1592 if (gh->gh_owner_pid)
b1e058da 1593 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
cc18152e
JP
1594 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1595 state2str(gh->gh_state),
1596 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1597 gh->gh_error,
1598 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1599 gh_owner ? gh_owner->comm : "(ended)",
1600 (void *)gh->gh_ip);
7c52b166 1601 return 0;
b3b94faa
DT
1602}
1603
627c10b7 1604static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
6802e340 1605{
627c10b7 1606 const unsigned long *gflags = &gl->gl_flags;
6802e340 1607 char *p = buf;
627c10b7 1608
6802e340
SW
1609 if (test_bit(GLF_LOCK, gflags))
1610 *p++ = 'l';
6802e340
SW
1611 if (test_bit(GLF_DEMOTE, gflags))
1612 *p++ = 'D';
1613 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1614 *p++ = 'd';
1615 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1616 *p++ = 'p';
1617 if (test_bit(GLF_DIRTY, gflags))
1618 *p++ = 'y';
1619 if (test_bit(GLF_LFLUSH, gflags))
1620 *p++ = 'f';
1621 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1622 *p++ = 'i';
1623 if (test_bit(GLF_REPLY_PENDING, gflags))
1624 *p++ = 'r';
f057f6cd 1625 if (test_bit(GLF_INITIAL, gflags))
d8348de0 1626 *p++ = 'I';
f057f6cd
SW
1627 if (test_bit(GLF_FROZEN, gflags))
1628 *p++ = 'F';
7b5e3d5f
SW
1629 if (test_bit(GLF_QUEUED, gflags))
1630 *p++ = 'q';
627c10b7
SW
1631 if (test_bit(GLF_LRU, gflags))
1632 *p++ = 'L';
1633 if (gl->gl_object)
1634 *p++ = 'o';
6802e340
SW
1635 *p = 0;
1636 return buf;
b3b94faa
DT
1637}
1638
1639/**
6802e340
SW
1640 * __dump_glock - print information about a glock
1641 * @seq: The seq_file struct
b3b94faa 1642 * @gl: the glock
6802e340
SW
1643 *
1644 * The file format is as follows:
1645 * One line per object, capital letters are used to indicate objects
1646 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1647 * other objects are indented by a single space and follow the glock to
1648 * which they are related. Fields are indicated by lower case letters
1649 * followed by a colon and the field value, except for strings which are in
1650 * [] so that its possible to see if they are composed of spaces for
1651 * example. The field's are n = number (id of the object), f = flags,
1652 * t = type, s = state, r = refcount, e = error, p = pid.
b3b94faa
DT
1653 *
1654 * Returns: 0 on success, -ENOBUFS when we run out of space
1655 */
1656
6802e340 1657static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
b3b94faa 1658{
6802e340
SW
1659 const struct gfs2_glock_operations *glops = gl->gl_ops;
1660 unsigned long long dtime;
1661 const struct gfs2_holder *gh;
1662 char gflags_buf[32];
1663 int error = 0;
b3b94faa 1664
6802e340
SW
1665 dtime = jiffies - gl->gl_demote_time;
1666 dtime *= 1000000/HZ; /* demote time in uSec */
1667 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1668 dtime = 0;
4818972e 1669 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d r:%d\n",
6802e340
SW
1670 state2str(gl->gl_state),
1671 gl->gl_name.ln_type,
1672 (unsigned long long)gl->gl_name.ln_number,
627c10b7 1673 gflags2str(gflags_buf, gl),
6802e340
SW
1674 state2str(gl->gl_target),
1675 state2str(gl->gl_demote_state), dtime,
6802e340
SW
1676 atomic_read(&gl->gl_ail_count),
1677 atomic_read(&gl->gl_ref));
b3b94faa 1678
b3b94faa 1679 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
6802e340 1680 error = dump_holder(seq, gh);
b3b94faa
DT
1681 if (error)
1682 goto out;
1683 }
6802e340
SW
1684 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1685 error = glops->go_dump(seq, gl);
a91ea69f 1686out:
b3b94faa
DT
1687 return error;
1688}
1689
6802e340 1690
b3b94faa 1691
8fbbfd21 1692
85d1da67
SW
1693int __init gfs2_glock_init(void)
1694{
1695 unsigned i;
1696 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
bc015cb8 1697 INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
087efdd3 1698 }
8fbbfd21 1699
d2115778 1700 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
58a69cb4 1701 WQ_HIGHPRI | WQ_FREEZABLE, 0);
97cc1025 1702 if (IS_ERR(glock_workqueue))
c4f68a13 1703 return PTR_ERR(glock_workqueue);
d2115778 1704 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
58a69cb4 1705 WQ_MEM_RECLAIM | WQ_FREEZABLE,
d2115778 1706 0);
b94a170e
BM
1707 if (IS_ERR(gfs2_delete_workqueue)) {
1708 destroy_workqueue(glock_workqueue);
1709 return PTR_ERR(gfs2_delete_workqueue);
1710 }
97cc1025
SW
1711
1712 register_shrinker(&glock_shrinker);
c4f68a13 1713
85d1da67
SW
1714 return 0;
1715}
1716
8fbbfd21
SW
1717void gfs2_glock_exit(void)
1718{
97cc1025 1719 unregister_shrinker(&glock_shrinker);
c4f68a13 1720 destroy_workqueue(glock_workqueue);
b94a170e 1721 destroy_workqueue(gfs2_delete_workqueue);
8fbbfd21
SW
1722}
1723
bc015cb8
SW
1724static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
1725{
1726 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
1727 struct gfs2_glock, gl_list);
1728}
1729
1730static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
1731{
7e32d026 1732 return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
bc015cb8
SW
1733 struct gfs2_glock, gl_list);
1734}
1735
6802e340 1736static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
7c52b166 1737{
7b08fc62
SW
1738 struct gfs2_glock *gl;
1739
bc015cb8
SW
1740 do {
1741 gl = gi->gl;
1742 if (gl) {
1743 gi->gl = glock_hash_next(gl);
1744 } else {
1745 gi->gl = glock_hash_chain(gi->hash);
1746 }
1747 while (gi->gl == NULL) {
1748 gi->hash++;
1749 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1750 rcu_read_unlock();
1751 return 1;
1752 }
1753 gi->gl = glock_hash_chain(gi->hash);
1754 }
1755 /* Skip entries for other sb and dead entries */
1756 } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
a947e033 1757
7c52b166
RP
1758 return 0;
1759}
1760
6802e340 1761static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
7c52b166 1762{
6802e340 1763 struct gfs2_glock_iter *gi = seq->private;
7c52b166
RP
1764 loff_t n = *pos;
1765
6802e340 1766 gi->hash = 0;
bc015cb8 1767 rcu_read_lock();
7c52b166 1768
6802e340 1769 do {
bc015cb8 1770 if (gfs2_glock_iter_next(gi))
7c52b166 1771 return NULL;
6802e340 1772 } while (n--);
7c52b166 1773
6802e340 1774 return gi->gl;
7c52b166
RP
1775}
1776
6802e340 1777static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
7c52b166
RP
1778 loff_t *pos)
1779{
6802e340 1780 struct gfs2_glock_iter *gi = seq->private;
7c52b166
RP
1781
1782 (*pos)++;
1783
bc015cb8 1784 if (gfs2_glock_iter_next(gi))
7c52b166 1785 return NULL;
7c52b166 1786
6802e340 1787 return gi->gl;
7c52b166
RP
1788}
1789
6802e340 1790static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
7c52b166 1791{
6802e340 1792 struct gfs2_glock_iter *gi = seq->private;
bc015cb8
SW
1793
1794 if (gi->gl)
1795 rcu_read_unlock();
1796 gi->gl = NULL;
7c52b166
RP
1797}
1798
6802e340 1799static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
7c52b166 1800{
6802e340 1801 return dump_glock(seq, iter_ptr);
7c52b166
RP
1802}
1803
4ef29002 1804static const struct seq_operations gfs2_glock_seq_ops = {
7c52b166
RP
1805 .start = gfs2_glock_seq_start,
1806 .next = gfs2_glock_seq_next,
1807 .stop = gfs2_glock_seq_stop,
1808 .show = gfs2_glock_seq_show,
1809};
1810
1811static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1812{
6802e340
SW
1813 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1814 sizeof(struct gfs2_glock_iter));
1815 if (ret == 0) {
1816 struct seq_file *seq = file->private_data;
1817 struct gfs2_glock_iter *gi = seq->private;
1818 gi->sdp = inode->i_private;
1819 }
1820 return ret;
7c52b166
RP
1821}
1822
1823static const struct file_operations gfs2_debug_fops = {
1824 .owner = THIS_MODULE,
1825 .open = gfs2_debugfs_open,
1826 .read = seq_read,
1827 .llseek = seq_lseek,
6802e340 1828 .release = seq_release_private,
7c52b166
RP
1829};
1830
1831int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1832{
5f882096
RP
1833 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1834 if (!sdp->debugfs_dir)
1835 return -ENOMEM;
1836 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1837 S_IFREG | S_IRUGO,
1838 sdp->debugfs_dir, sdp,
1839 &gfs2_debug_fops);
1840 if (!sdp->debugfs_dentry_glocks)
7c52b166
RP
1841 return -ENOMEM;
1842
1843 return 0;
1844}
1845
1846void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1847{
5f882096
RP
1848 if (sdp && sdp->debugfs_dir) {
1849 if (sdp->debugfs_dentry_glocks) {
1850 debugfs_remove(sdp->debugfs_dentry_glocks);
1851 sdp->debugfs_dentry_glocks = NULL;
1852 }
1853 debugfs_remove(sdp->debugfs_dir);
1854 sdp->debugfs_dir = NULL;
1855 }
7c52b166
RP
1856}
1857
1858int gfs2_register_debugfs(void)
1859{
1860 gfs2_root = debugfs_create_dir("gfs2", NULL);
1861 return gfs2_root ? 0 : -ENOMEM;
1862}
1863
1864void gfs2_unregister_debugfs(void)
1865{
1866 debugfs_remove(gfs2_root);
5f882096 1867 gfs2_root = NULL;
7c52b166 1868}