]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/gfs2/glock.c
[GFS2] go_drop_bh is never used, so remove it
[mirror_ubuntu-artful-kernel.git] / fs / gfs2 / glock.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 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>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/delay.h>
16#include <linux/sort.h>
17#include <linux/jhash.h>
d0dc80db 18#include <linux/kallsyms.h>
5c676f6d 19#include <linux/gfs2_ondisk.h>
24264434 20#include <linux/list.h>
7d308590 21#include <linux/lm_interface.h>
fee852e3 22#include <linux/wait.h>
61be084e 23#include <linux/rwsem.h>
b3b94faa
DT
24#include <asm/uaccess.h>
25
26#include "gfs2.h"
5c676f6d 27#include "incore.h"
b3b94faa
DT
28#include "glock.h"
29#include "glops.h"
30#include "inode.h"
31#include "lm.h"
32#include "lops.h"
33#include "meta_io.h"
34#include "quota.h"
35#include "super.h"
5c676f6d 36#include "util.h"
b3b94faa 37
37b2fa6a 38struct gfs2_gl_hash_bucket {
b6397893 39 struct hlist_head hb_list;
37b2fa6a
SW
40};
41
b3b94faa
DT
42typedef void (*glock_examiner) (struct gfs2_glock * gl);
43
08bc2dbc 44static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
320dd101 45static int dump_glock(struct gfs2_glock *gl);
24264434 46static int dump_inode(struct gfs2_inode *ip);
b5d32bea
SW
47static void gfs2_glock_xmote_th(struct gfs2_holder *gh);
48static void gfs2_glock_drop_th(struct gfs2_glock *gl);
61be084e 49static DECLARE_RWSEM(gfs2_umount_flush_sem);
08bc2dbc 50
b6397893 51#define GFS2_GL_HASH_SHIFT 15
087efdd3
SW
52#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
53#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
54
85d1da67 55static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
087efdd3
SW
56
57/*
58 * Despite what you might think, the numbers below are not arbitrary :-)
59 * They are taken from the ipv4 routing hash code, which is well tested
60 * and thus should be nearly optimal. Later on we might tweek the numbers
61 * but for now this should be fine.
62 *
63 * The reason for putting the locks in a separate array from the list heads
64 * is that we can have fewer locks than list heads and save memory. We use
65 * the same hash function for both, but with a different hash mask.
66 */
67#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
68 defined(CONFIG_PROVE_LOCKING)
69
70#ifdef CONFIG_LOCKDEP
71# define GL_HASH_LOCK_SZ 256
72#else
73# if NR_CPUS >= 32
74# define GL_HASH_LOCK_SZ 4096
75# elif NR_CPUS >= 16
76# define GL_HASH_LOCK_SZ 2048
77# elif NR_CPUS >= 8
78# define GL_HASH_LOCK_SZ 1024
79# elif NR_CPUS >= 4
80# define GL_HASH_LOCK_SZ 512
81# else
82# define GL_HASH_LOCK_SZ 256
83# endif
84#endif
85
86/* We never want more locks than chains */
87#if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
88# undef GL_HASH_LOCK_SZ
89# define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
90#endif
91
92static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
93
94static inline rwlock_t *gl_lock_addr(unsigned int x)
95{
94610610 96 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
087efdd3
SW
97}
98#else /* not SMP, so no spinlocks required */
0ac23069 99static inline rwlock_t *gl_lock_addr(unsigned int x)
087efdd3
SW
100{
101 return NULL;
102}
103#endif
85d1da67 104
b3b94faa
DT
105/**
106 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
107 * @actual: the current state of the lock
108 * @requested: the lock state that was requested by the caller
109 * @flags: the modifier flags passed in by the caller
110 *
111 * Returns: 1 if the locks are compatible, 0 otherwise
112 */
113
114static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
115 int flags)
116{
117 if (actual == requested)
118 return 1;
119
120 if (flags & GL_EXACT)
121 return 0;
122
123 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
124 return 1;
125
126 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
127 return 1;
128
129 return 0;
130}
131
132/**
133 * gl_hash() - Turn glock number into hash bucket number
134 * @lock: The glock number
135 *
136 * Returns: The number of the corresponding hash bucket
137 */
138
b8547856
SW
139static unsigned int gl_hash(const struct gfs2_sbd *sdp,
140 const struct lm_lockname *name)
b3b94faa
DT
141{
142 unsigned int h;
143
cd915493 144 h = jhash(&name->ln_number, sizeof(u64), 0);
b3b94faa 145 h = jhash(&name->ln_type, sizeof(unsigned int), h);
b8547856 146 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
b3b94faa
DT
147 h &= GFS2_GL_HASH_MASK;
148
149 return h;
150}
151
152/**
153 * glock_free() - Perform a few checks and then release struct gfs2_glock
154 * @gl: The glock to release
155 *
156 * Also calls lock module to release its internal structure for this glock.
157 *
158 */
159
160static void glock_free(struct gfs2_glock *gl)
161{
162 struct gfs2_sbd *sdp = gl->gl_sbd;
163 struct inode *aspace = gl->gl_aspace;
164
165 gfs2_lm_put_lock(sdp, gl->gl_lock);
166
167 if (aspace)
168 gfs2_aspace_put(aspace);
169
170 kmem_cache_free(gfs2_glock_cachep, gl);
b3b94faa
DT
171}
172
173/**
174 * gfs2_glock_hold() - increment reference count on glock
175 * @gl: The glock to hold
176 *
177 */
178
179void gfs2_glock_hold(struct gfs2_glock *gl)
180{
16feb9fe 181 atomic_inc(&gl->gl_ref);
b3b94faa
DT
182}
183
184/**
185 * gfs2_glock_put() - Decrement reference count on glock
186 * @gl: The glock to put
187 *
188 */
189
190int gfs2_glock_put(struct gfs2_glock *gl)
191{
b3b94faa 192 int rv = 0;
16feb9fe 193 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa 194
087efdd3 195 write_lock(gl_lock_addr(gl->gl_hash));
16feb9fe 196 if (atomic_dec_and_test(&gl->gl_ref)) {
b6397893 197 hlist_del(&gl->gl_list);
087efdd3 198 write_unlock(gl_lock_addr(gl->gl_hash));
190562bd 199 BUG_ON(spin_is_locked(&gl->gl_spin));
16feb9fe
SW
200 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
201 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
202 gfs2_assert(sdp, list_empty(&gl->gl_holders));
203 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
204 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
205 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
b3b94faa
DT
206 glock_free(gl);
207 rv = 1;
208 goto out;
209 }
087efdd3 210 write_unlock(gl_lock_addr(gl->gl_hash));
a2242db0 211out:
b3b94faa
DT
212 return rv;
213}
214
b3b94faa
DT
215/**
216 * search_bucket() - Find struct gfs2_glock by lock number
217 * @bucket: the bucket to search
218 * @name: The lock name
219 *
220 * Returns: NULL, or the struct gfs2_glock with the requested number
221 */
222
37b2fa6a 223static struct gfs2_glock *search_bucket(unsigned int hash,
899be4d3 224 const struct gfs2_sbd *sdp,
d6a53727 225 const struct lm_lockname *name)
b3b94faa
DT
226{
227 struct gfs2_glock *gl;
b6397893 228 struct hlist_node *h;
b3b94faa 229
b6397893 230 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
b3b94faa
DT
231 if (!lm_name_equal(&gl->gl_name, name))
232 continue;
899be4d3
SW
233 if (gl->gl_sbd != sdp)
234 continue;
b3b94faa 235
16feb9fe 236 atomic_inc(&gl->gl_ref);
b3b94faa
DT
237
238 return gl;
239 }
240
241 return NULL;
242}
243
244/**
245 * gfs2_glock_find() - Find glock by lock number
246 * @sdp: The GFS2 superblock
247 * @name: The lock name
248 *
249 * Returns: NULL, or the struct gfs2_glock with the requested number
250 */
251
85d1da67 252static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
d6a53727 253 const struct lm_lockname *name)
b3b94faa 254{
37b2fa6a 255 unsigned int hash = gl_hash(sdp, name);
b3b94faa
DT
256 struct gfs2_glock *gl;
257
087efdd3 258 read_lock(gl_lock_addr(hash));
37b2fa6a 259 gl = search_bucket(hash, sdp, name);
087efdd3 260 read_unlock(gl_lock_addr(hash));
b3b94faa
DT
261
262 return gl;
263}
264
265/**
266 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
267 * @sdp: The GFS2 superblock
268 * @number: the lock number
269 * @glops: The glock_operations to use
270 * @create: If 0, don't create the glock if it doesn't exist
271 * @glp: the glock is returned here
272 *
273 * This does not lock a glock, just finds/creates structures for one.
274 *
275 * Returns: errno
276 */
277
cd915493 278int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
8fb4b536 279 const struct gfs2_glock_operations *glops, int create,
b3b94faa
DT
280 struct gfs2_glock **glp)
281{
37b2fa6a 282 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
b3b94faa 283 struct gfs2_glock *gl, *tmp;
37b2fa6a 284 unsigned int hash = gl_hash(sdp, &name);
b3b94faa
DT
285 int error;
286
087efdd3 287 read_lock(gl_lock_addr(hash));
37b2fa6a 288 gl = search_bucket(hash, sdp, &name);
087efdd3 289 read_unlock(gl_lock_addr(hash));
b3b94faa
DT
290
291 if (gl || !create) {
292 *glp = gl;
293 return 0;
294 }
295
296 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
297 if (!gl)
298 return -ENOMEM;
299
ec45d9f5 300 gl->gl_flags = 0;
b3b94faa 301 gl->gl_name = name;
16feb9fe 302 atomic_set(&gl->gl_ref, 1);
b3b94faa 303 gl->gl_state = LM_ST_UNLOCKED;
37b2fa6a 304 gl->gl_hash = hash;
320dd101
SW
305 gl->gl_owner = NULL;
306 gl->gl_ip = 0;
b3b94faa 307 gl->gl_ops = glops;
ec45d9f5
SW
308 gl->gl_req_gh = NULL;
309 gl->gl_req_bh = NULL;
310 gl->gl_vn = 0;
311 gl->gl_stamp = jiffies;
312 gl->gl_object = NULL;
b3b94faa 313 gl->gl_sbd = sdp;
ec45d9f5 314 gl->gl_aspace = NULL;
b3b94faa 315 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
b3b94faa
DT
316
317 /* If this glock protects actual on-disk data or metadata blocks,
318 create a VFS inode to manage the pages/buffers holding them. */
50299965 319 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
b3b94faa
DT
320 gl->gl_aspace = gfs2_aspace_get(sdp);
321 if (!gl->gl_aspace) {
322 error = -ENOMEM;
323 goto fail;
324 }
325 }
326
327 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
328 if (error)
329 goto fail_aspace;
330
087efdd3 331 write_lock(gl_lock_addr(hash));
37b2fa6a 332 tmp = search_bucket(hash, sdp, &name);
b3b94faa 333 if (tmp) {
087efdd3 334 write_unlock(gl_lock_addr(hash));
b3b94faa
DT
335 glock_free(gl);
336 gl = tmp;
337 } else {
b6397893 338 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
087efdd3 339 write_unlock(gl_lock_addr(hash));
b3b94faa
DT
340 }
341
342 *glp = gl;
343
344 return 0;
345
ec45d9f5 346fail_aspace:
b3b94faa
DT
347 if (gl->gl_aspace)
348 gfs2_aspace_put(gl->gl_aspace);
ec45d9f5 349fail:
907b9bce 350 kmem_cache_free(gfs2_glock_cachep, gl);
b3b94faa
DT
351 return error;
352}
353
354/**
355 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
356 * @gl: the glock
357 * @state: the state we're requesting
358 * @flags: the modifier flags
359 * @gh: the holder structure
360 *
361 */
362
190562bd 363void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
b3b94faa
DT
364 struct gfs2_holder *gh)
365{
366 INIT_LIST_HEAD(&gh->gh_list);
367 gh->gh_gl = gl;
d0dc80db 368 gh->gh_ip = (unsigned long)__builtin_return_address(0);
190562bd 369 gh->gh_owner = current;
b3b94faa
DT
370 gh->gh_state = state;
371 gh->gh_flags = flags;
372 gh->gh_error = 0;
373 gh->gh_iflags = 0;
b3b94faa
DT
374 gfs2_glock_hold(gl);
375}
376
377/**
378 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
379 * @state: the state we're requesting
380 * @flags: the modifier flags
381 * @gh: the holder structure
382 *
383 * Don't mess with the glock.
384 *
385 */
386
190562bd 387void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
b3b94faa
DT
388{
389 gh->gh_state = state;
579b78a4 390 gh->gh_flags = flags;
b3b94faa 391 gh->gh_iflags &= 1 << HIF_ALLOCED;
d0dc80db 392 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b3b94faa
DT
393}
394
395/**
396 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
397 * @gh: the holder structure
398 *
399 */
400
401void gfs2_holder_uninit(struct gfs2_holder *gh)
402{
403 gfs2_glock_put(gh->gh_gl);
404 gh->gh_gl = NULL;
d0dc80db 405 gh->gh_ip = 0;
b3b94faa
DT
406}
407
408/**
409 * gfs2_holder_get - get a struct gfs2_holder structure
410 * @gl: the glock
411 * @state: the state we're requesting
412 * @flags: the modifier flags
af18ddb8 413 * @gfp_flags:
b3b94faa
DT
414 *
415 * Figure out how big an impact this function has. Either:
416 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
417 * 2) Leave it like it is
418 *
419 * Returns: the holder structure, NULL on ENOMEM
420 */
421
08bc2dbc
AB
422static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
423 unsigned int state,
424 int flags, gfp_t gfp_flags)
b3b94faa
DT
425{
426 struct gfs2_holder *gh;
427
428 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
429 if (!gh)
430 return NULL;
431
432 gfs2_holder_init(gl, state, flags, gh);
433 set_bit(HIF_ALLOCED, &gh->gh_iflags);
d0dc80db 434 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b3b94faa
DT
435 return gh;
436}
437
438/**
439 * gfs2_holder_put - get rid of a struct gfs2_holder structure
440 * @gh: the holder structure
441 *
442 */
443
08bc2dbc 444static void gfs2_holder_put(struct gfs2_holder *gh)
b3b94faa
DT
445{
446 gfs2_holder_uninit(gh);
447 kfree(gh);
448}
449
fee852e3
SW
450static void gfs2_holder_dispose_or_wake(struct gfs2_holder *gh)
451{
452 if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) {
453 gfs2_holder_put(gh);
454 return;
455 }
456 clear_bit(HIF_WAIT, &gh->gh_iflags);
457 smp_mb();
458 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
459}
460
461static int holder_wait(void *word)
462{
463 schedule();
464 return 0;
465}
466
467static void wait_on_holder(struct gfs2_holder *gh)
468{
469 might_sleep();
470 wait_on_bit(&gh->gh_iflags, HIF_WAIT, holder_wait, TASK_UNINTERRUPTIBLE);
471}
472
b3b94faa
DT
473/**
474 * rq_mutex - process a mutex request in the queue
475 * @gh: the glock holder
476 *
477 * Returns: 1 if the queue is blocked
478 */
479
480static int rq_mutex(struct gfs2_holder *gh)
481{
482 struct gfs2_glock *gl = gh->gh_gl;
483
484 list_del_init(&gh->gh_list);
485 /* gh->gh_error never examined. */
486 set_bit(GLF_LOCK, &gl->gl_flags);
d043e190 487 clear_bit(HIF_WAIT, &gh->gh_iflags);
fee852e3
SW
488 smp_mb();
489 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
b3b94faa
DT
490
491 return 1;
492}
493
494/**
495 * rq_promote - process a promote request in the queue
496 * @gh: the glock holder
497 *
498 * Acquire a new inter-node lock, or change a lock state to more restrictive.
499 *
500 * Returns: 1 if the queue is blocked
501 */
502
503static int rq_promote(struct gfs2_holder *gh)
504{
505 struct gfs2_glock *gl = gh->gh_gl;
506 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa
DT
507
508 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
509 if (list_empty(&gl->gl_holders)) {
510 gl->gl_req_gh = gh;
511 set_bit(GLF_LOCK, &gl->gl_flags);
512 spin_unlock(&gl->gl_spin);
513
514 if (atomic_read(&sdp->sd_reclaim_count) >
515 gfs2_tune_get(sdp, gt_reclaim_limit) &&
516 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
517 gfs2_reclaim_glock(sdp);
518 gfs2_reclaim_glock(sdp);
519 }
520
b5d32bea 521 gfs2_glock_xmote_th(gh);
b3b94faa
DT
522 spin_lock(&gl->gl_spin);
523 }
524 return 1;
525 }
526
527 if (list_empty(&gl->gl_holders)) {
528 set_bit(HIF_FIRST, &gh->gh_iflags);
529 set_bit(GLF_LOCK, &gl->gl_flags);
b3b94faa
DT
530 } else {
531 struct gfs2_holder *next_gh;
1c0f4872 532 if (gh->gh_state == LM_ST_EXCLUSIVE)
b3b94faa
DT
533 return 1;
534 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
535 gh_list);
1c0f4872 536 if (next_gh->gh_state == LM_ST_EXCLUSIVE)
b3b94faa 537 return 1;
b3b94faa
DT
538 }
539
540 list_move_tail(&gh->gh_list, &gl->gl_holders);
541 gh->gh_error = 0;
542 set_bit(HIF_HOLDER, &gh->gh_iflags);
543
fee852e3 544 gfs2_holder_dispose_or_wake(gh);
b3b94faa
DT
545
546 return 0;
547}
548
549/**
550 * rq_demote - process a demote request in the queue
551 * @gh: the glock holder
552 *
553 * Returns: 1 if the queue is blocked
554 */
555
556static int rq_demote(struct gfs2_holder *gh)
557{
558 struct gfs2_glock *gl = gh->gh_gl;
b3b94faa
DT
559
560 if (!list_empty(&gl->gl_holders))
561 return 1;
562
563 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
564 list_del_init(&gh->gh_list);
565 gh->gh_error = 0;
566 spin_unlock(&gl->gl_spin);
fee852e3 567 gfs2_holder_dispose_or_wake(gh);
b3b94faa
DT
568 spin_lock(&gl->gl_spin);
569 } else {
570 gl->gl_req_gh = gh;
571 set_bit(GLF_LOCK, &gl->gl_flags);
572 spin_unlock(&gl->gl_spin);
573
574 if (gh->gh_state == LM_ST_UNLOCKED ||
575 gl->gl_state != LM_ST_EXCLUSIVE)
b5d32bea 576 gfs2_glock_drop_th(gl);
b3b94faa 577 else
b5d32bea 578 gfs2_glock_xmote_th(gh);
b3b94faa
DT
579
580 spin_lock(&gl->gl_spin);
581 }
582
583 return 0;
584}
585
b3b94faa
DT
586/**
587 * run_queue - process holder structures on a glock
588 * @gl: the glock
589 *
590 */
b3b94faa
DT
591static void run_queue(struct gfs2_glock *gl)
592{
593 struct gfs2_holder *gh;
594 int blocked = 1;
595
596 for (;;) {
597 if (test_bit(GLF_LOCK, &gl->gl_flags))
598 break;
599
600 if (!list_empty(&gl->gl_waiters1)) {
601 gh = list_entry(gl->gl_waiters1.next,
602 struct gfs2_holder, gh_list);
603
604 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
605 blocked = rq_mutex(gh);
606 else
607 gfs2_assert_warn(gl->gl_sbd, 0);
608
609 } else if (!list_empty(&gl->gl_waiters2) &&
610 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
611 gh = list_entry(gl->gl_waiters2.next,
612 struct gfs2_holder, gh_list);
613
614 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
615 blocked = rq_demote(gh);
b3b94faa
DT
616 else
617 gfs2_assert_warn(gl->gl_sbd, 0);
618
619 } else if (!list_empty(&gl->gl_waiters3)) {
620 gh = list_entry(gl->gl_waiters3.next,
621 struct gfs2_holder, gh_list);
622
623 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
624 blocked = rq_promote(gh);
625 else
626 gfs2_assert_warn(gl->gl_sbd, 0);
627
628 } else
629 break;
630
631 if (blocked)
632 break;
633 }
634}
635
636/**
637 * gfs2_glmutex_lock - acquire a local lock on a glock
638 * @gl: the glock
639 *
640 * Gives caller exclusive access to manipulate a glock structure.
641 */
642
feaa7bba 643static void gfs2_glmutex_lock(struct gfs2_glock *gl)
b3b94faa
DT
644{
645 struct gfs2_holder gh;
646
647 gfs2_holder_init(gl, 0, 0, &gh);
648 set_bit(HIF_MUTEX, &gh.gh_iflags);
fee852e3
SW
649 if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
650 BUG();
b3b94faa
DT
651
652 spin_lock(&gl->gl_spin);
85d1da67 653 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
b3b94faa 654 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
85d1da67 655 } else {
320dd101
SW
656 gl->gl_owner = current;
657 gl->gl_ip = (unsigned long)__builtin_return_address(0);
fee852e3
SW
658 clear_bit(HIF_WAIT, &gh.gh_iflags);
659 smp_mb();
660 wake_up_bit(&gh.gh_iflags, HIF_WAIT);
320dd101 661 }
b3b94faa
DT
662 spin_unlock(&gl->gl_spin);
663
fee852e3 664 wait_on_holder(&gh);
b3b94faa
DT
665 gfs2_holder_uninit(&gh);
666}
667
668/**
669 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
670 * @gl: the glock
671 *
672 * Returns: 1 if the glock is acquired
673 */
674
08bc2dbc 675static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
b3b94faa
DT
676{
677 int acquired = 1;
678
679 spin_lock(&gl->gl_spin);
85d1da67 680 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
b3b94faa 681 acquired = 0;
85d1da67 682 } else {
320dd101
SW
683 gl->gl_owner = current;
684 gl->gl_ip = (unsigned long)__builtin_return_address(0);
685 }
b3b94faa
DT
686 spin_unlock(&gl->gl_spin);
687
688 return acquired;
689}
690
691/**
692 * gfs2_glmutex_unlock - release a local lock on a glock
693 * @gl: the glock
694 *
695 */
696
feaa7bba 697static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
b3b94faa
DT
698{
699 spin_lock(&gl->gl_spin);
700 clear_bit(GLF_LOCK, &gl->gl_flags);
320dd101
SW
701 gl->gl_owner = NULL;
702 gl->gl_ip = 0;
b3b94faa 703 run_queue(gl);
190562bd 704 BUG_ON(!spin_is_locked(&gl->gl_spin));
b3b94faa
DT
705 spin_unlock(&gl->gl_spin);
706}
707
708/**
709 * handle_callback - add a demote request to a lock's queue
710 * @gl: the glock
711 * @state: the state the caller wants us to change to
712 *
af18ddb8 713 * Note: This may fail sliently if we are out of memory.
b3b94faa
DT
714 */
715
716static void handle_callback(struct gfs2_glock *gl, unsigned int state)
717{
718 struct gfs2_holder *gh, *new_gh = NULL;
719
feaa7bba 720restart:
b3b94faa
DT
721 spin_lock(&gl->gl_spin);
722
723 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
724 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
725 gl->gl_req_gh != gh) {
726 if (gh->gh_state != state)
727 gh->gh_state = LM_ST_UNLOCKED;
728 goto out;
729 }
730 }
731
732 if (new_gh) {
733 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
734 new_gh = NULL;
735 } else {
736 spin_unlock(&gl->gl_spin);
737
ab923031 738 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_NOFS);
af18ddb8
SW
739 if (!new_gh)
740 return;
b3b94faa
DT
741 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
742 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
fee852e3 743 set_bit(HIF_WAIT, &new_gh->gh_iflags);
b3b94faa
DT
744
745 goto restart;
746 }
747
feaa7bba 748out:
b3b94faa
DT
749 spin_unlock(&gl->gl_spin);
750
751 if (new_gh)
752 gfs2_holder_put(new_gh);
753}
754
755/**
756 * state_change - record that the glock is now in a different state
757 * @gl: the glock
758 * @new_state the new state
759 *
760 */
761
762static void state_change(struct gfs2_glock *gl, unsigned int new_state)
763{
b3b94faa
DT
764 int held1, held2;
765
766 held1 = (gl->gl_state != LM_ST_UNLOCKED);
767 held2 = (new_state != LM_ST_UNLOCKED);
768
769 if (held1 != held2) {
6a6b3d01 770 if (held2)
b3b94faa 771 gfs2_glock_hold(gl);
6a6b3d01 772 else
b3b94faa 773 gfs2_glock_put(gl);
b3b94faa
DT
774 }
775
776 gl->gl_state = new_state;
777}
778
779/**
780 * xmote_bh - Called after the lock module is done acquiring a lock
781 * @gl: The glock in question
782 * @ret: the int returned from the lock module
783 *
784 */
785
786static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
787{
788 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 789 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
790 struct gfs2_holder *gh = gl->gl_req_gh;
791 int prev_state = gl->gl_state;
792 int op_done = 1;
793
794 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
12132933 795 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
b3b94faa
DT
796 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
797
798 state_change(gl, ret & LM_OUT_ST_MASK);
799
800 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
801 if (glops->go_inval)
1a14d3a6 802 glops->go_inval(gl, DIO_METADATA);
b3b94faa
DT
803 } else if (gl->gl_state == LM_ST_DEFERRED) {
804 /* We might not want to do this here.
805 Look at moving to the inode glops. */
806 if (glops->go_inval)
1a14d3a6 807 glops->go_inval(gl, 0);
b3b94faa
DT
808 }
809
810 /* Deal with each possible exit condition */
811
812 if (!gh)
813 gl->gl_stamp = jiffies;
b3b94faa
DT
814 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
815 spin_lock(&gl->gl_spin);
816 list_del_init(&gh->gh_list);
817 gh->gh_error = -EIO;
b3b94faa 818 spin_unlock(&gl->gl_spin);
b3b94faa
DT
819 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
820 spin_lock(&gl->gl_spin);
821 list_del_init(&gh->gh_list);
822 if (gl->gl_state == gh->gh_state ||
85d1da67 823 gl->gl_state == LM_ST_UNLOCKED) {
b3b94faa 824 gh->gh_error = 0;
85d1da67 825 } else {
b3b94faa
DT
826 if (gfs2_assert_warn(sdp, gh->gh_flags &
827 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
828 fs_warn(sdp, "ret = 0x%.8X\n", ret);
829 gh->gh_error = GLR_TRYFAILED;
830 }
831 spin_unlock(&gl->gl_spin);
832
833 if (ret & LM_OUT_CANCELED)
50299965 834 handle_callback(gl, LM_ST_UNLOCKED);
b3b94faa
DT
835
836 } else if (ret & LM_OUT_CANCELED) {
837 spin_lock(&gl->gl_spin);
838 list_del_init(&gh->gh_list);
839 gh->gh_error = GLR_CANCELED;
b3b94faa
DT
840 spin_unlock(&gl->gl_spin);
841
842 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
843 spin_lock(&gl->gl_spin);
844 list_move_tail(&gh->gh_list, &gl->gl_holders);
845 gh->gh_error = 0;
846 set_bit(HIF_HOLDER, &gh->gh_iflags);
847 spin_unlock(&gl->gl_spin);
848
849 set_bit(HIF_FIRST, &gh->gh_iflags);
850
851 op_done = 0;
852
853 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
854 spin_lock(&gl->gl_spin);
855 list_del_init(&gh->gh_list);
856 gh->gh_error = GLR_TRYFAILED;
b3b94faa
DT
857 spin_unlock(&gl->gl_spin);
858
859 } else {
860 if (gfs2_assert_withdraw(sdp, 0) == -1)
861 fs_err(sdp, "ret = 0x%.8X\n", ret);
862 }
863
864 if (glops->go_xmote_bh)
865 glops->go_xmote_bh(gl);
866
867 if (op_done) {
868 spin_lock(&gl->gl_spin);
869 gl->gl_req_gh = NULL;
870 gl->gl_req_bh = NULL;
871 clear_bit(GLF_LOCK, &gl->gl_flags);
872 run_queue(gl);
873 spin_unlock(&gl->gl_spin);
874 }
875
876 gfs2_glock_put(gl);
877
fee852e3
SW
878 if (gh)
879 gfs2_holder_dispose_or_wake(gh);
b3b94faa
DT
880}
881
882/**
883 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
884 * @gl: The glock in question
885 * @state: the requested state
886 * @flags: modifier flags to the lock call
887 *
888 */
889
b5d32bea 890void gfs2_glock_xmote_th(struct gfs2_holder *gh)
b3b94faa 891{
b5d32bea 892 struct gfs2_glock *gl = gh->gh_gl;
b3b94faa 893 struct gfs2_sbd *sdp = gl->gl_sbd;
b5d32bea
SW
894 int flags = gh->gh_flags;
895 unsigned state = gh->gh_state;
8fb4b536 896 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
897 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
898 LM_FLAG_NOEXP | LM_FLAG_ANY |
899 LM_FLAG_PRIORITY);
900 unsigned int lck_ret;
901
b5d32bea
SW
902 if (glops->go_xmote_th)
903 glops->go_xmote_th(gl);
904
b3b94faa 905 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
12132933 906 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
b3b94faa
DT
907 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
908 gfs2_assert_warn(sdp, state != gl->gl_state);
909
b3b94faa
DT
910 gfs2_glock_hold(gl);
911 gl->gl_req_bh = xmote_bh;
912
ec45d9f5 913 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
b3b94faa
DT
914
915 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
916 return;
917
918 if (lck_ret & LM_OUT_ASYNC)
919 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
920 else
921 xmote_bh(gl, lck_ret);
922}
923
924/**
925 * drop_bh - Called after a lock module unlock completes
926 * @gl: the glock
927 * @ret: the return status
928 *
929 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
930 * Doesn't drop the reference on the glock the top half took out
931 *
932 */
933
934static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
935{
936 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 937 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
938 struct gfs2_holder *gh = gl->gl_req_gh;
939
b3b94faa 940 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
12132933 941 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
b3b94faa
DT
942 gfs2_assert_warn(sdp, !ret);
943
944 state_change(gl, LM_ST_UNLOCKED);
945
946 if (glops->go_inval)
1a14d3a6 947 glops->go_inval(gl, DIO_METADATA);
b3b94faa
DT
948
949 if (gh) {
950 spin_lock(&gl->gl_spin);
951 list_del_init(&gh->gh_list);
952 gh->gh_error = 0;
953 spin_unlock(&gl->gl_spin);
954 }
955
b3b94faa
DT
956 spin_lock(&gl->gl_spin);
957 gl->gl_req_gh = NULL;
958 gl->gl_req_bh = NULL;
959 clear_bit(GLF_LOCK, &gl->gl_flags);
960 run_queue(gl);
961 spin_unlock(&gl->gl_spin);
962
963 gfs2_glock_put(gl);
964
fee852e3
SW
965 if (gh)
966 gfs2_holder_dispose_or_wake(gh);
b3b94faa
DT
967}
968
969/**
970 * gfs2_glock_drop_th - call into the lock module to unlock a lock
971 * @gl: the glock
972 *
973 */
974
b5d32bea 975static void gfs2_glock_drop_th(struct gfs2_glock *gl)
b3b94faa
DT
976{
977 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 978 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
979 unsigned int ret;
980
b5d32bea
SW
981 if (glops->go_drop_th)
982 glops->go_drop_th(gl);
983
b3b94faa 984 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
12132933 985 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
b3b94faa
DT
986 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
987
b3b94faa
DT
988 gfs2_glock_hold(gl);
989 gl->gl_req_bh = drop_bh;
990
b3b94faa
DT
991 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
992
993 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
994 return;
995
996 if (!ret)
997 drop_bh(gl, ret);
998 else
999 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1000}
1001
1002/**
1003 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1004 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1005 *
1006 * Don't cancel GL_NOCANCEL requests.
1007 */
1008
1009static void do_cancels(struct gfs2_holder *gh)
1010{
1011 struct gfs2_glock *gl = gh->gh_gl;
1012
1013 spin_lock(&gl->gl_spin);
1014
1015 while (gl->gl_req_gh != gh &&
1016 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1017 !list_empty(&gh->gh_list)) {
50299965
SW
1018 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1019 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
b3b94faa
DT
1020 spin_unlock(&gl->gl_spin);
1021 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1022 msleep(100);
1023 spin_lock(&gl->gl_spin);
1024 } else {
1025 spin_unlock(&gl->gl_spin);
1026 msleep(100);
1027 spin_lock(&gl->gl_spin);
1028 }
1029 }
1030
1031 spin_unlock(&gl->gl_spin);
1032}
1033
1034/**
1035 * glock_wait_internal - wait on a glock acquisition
1036 * @gh: the glock holder
1037 *
1038 * Returns: 0 on success
1039 */
1040
1041static int glock_wait_internal(struct gfs2_holder *gh)
1042{
1043 struct gfs2_glock *gl = gh->gh_gl;
1044 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 1045 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1046
1047 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1048 return -EIO;
1049
1050 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1051 spin_lock(&gl->gl_spin);
1052 if (gl->gl_req_gh != gh &&
1053 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1054 !list_empty(&gh->gh_list)) {
1055 list_del_init(&gh->gh_list);
1056 gh->gh_error = GLR_TRYFAILED;
b3b94faa
DT
1057 run_queue(gl);
1058 spin_unlock(&gl->gl_spin);
1059 return gh->gh_error;
1060 }
1061 spin_unlock(&gl->gl_spin);
1062 }
1063
1064 if (gh->gh_flags & LM_FLAG_PRIORITY)
1065 do_cancels(gh);
1066
fee852e3 1067 wait_on_holder(gh);
b3b94faa
DT
1068 if (gh->gh_error)
1069 return gh->gh_error;
1070
1071 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
85d1da67 1072 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
b3b94faa
DT
1073 gh->gh_flags));
1074
1075 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1076 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1077
1078 if (glops->go_lock) {
1079 gh->gh_error = glops->go_lock(gh);
1080 if (gh->gh_error) {
1081 spin_lock(&gl->gl_spin);
1082 list_del_init(&gh->gh_list);
b3b94faa
DT
1083 spin_unlock(&gl->gl_spin);
1084 }
1085 }
1086
1087 spin_lock(&gl->gl_spin);
1088 gl->gl_req_gh = NULL;
1089 gl->gl_req_bh = NULL;
1090 clear_bit(GLF_LOCK, &gl->gl_flags);
b3b94faa
DT
1091 run_queue(gl);
1092 spin_unlock(&gl->gl_spin);
1093 }
1094
1095 return gh->gh_error;
1096}
1097
1098static inline struct gfs2_holder *
1099find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1100{
1101 struct gfs2_holder *gh;
1102
1103 list_for_each_entry(gh, head, gh_list) {
1104 if (gh->gh_owner == owner)
1105 return gh;
1106 }
1107
1108 return NULL;
1109}
1110
b3b94faa
DT
1111/**
1112 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1113 * @gh: the holder structure to add
1114 *
1115 */
1116
1117static void add_to_queue(struct gfs2_holder *gh)
1118{
1119 struct gfs2_glock *gl = gh->gh_gl;
1120 struct gfs2_holder *existing;
1121
190562bd 1122 BUG_ON(!gh->gh_owner);
fee852e3
SW
1123 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1124 BUG();
190562bd 1125
b3b94faa
DT
1126 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1127 if (existing) {
5965b1f4 1128 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
86384605 1129 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
907b9bce 1130 printk(KERN_INFO "lock type : %d lock state : %d\n",
86384605 1131 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
5965b1f4 1132 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
86384605 1133 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
907b9bce 1134 printk(KERN_INFO "lock type : %d lock state : %d\n",
86384605 1135 gl->gl_name.ln_type, gl->gl_state);
5965b1f4 1136 BUG();
b3b94faa
DT
1137 }
1138
1139 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1140 if (existing) {
5965b1f4
SW
1141 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1142 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1143 BUG();
b3b94faa
DT
1144 }
1145
b3b94faa
DT
1146 if (gh->gh_flags & LM_FLAG_PRIORITY)
1147 list_add(&gh->gh_list, &gl->gl_waiters3);
1148 else
907b9bce 1149 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
b3b94faa
DT
1150}
1151
1152/**
1153 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1154 * @gh: the holder structure
1155 *
1156 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1157 *
1158 * Returns: 0, GLR_TRYFAILED, or errno on failure
1159 */
1160
1161int gfs2_glock_nq(struct gfs2_holder *gh)
1162{
1163 struct gfs2_glock *gl = gh->gh_gl;
1164 struct gfs2_sbd *sdp = gl->gl_sbd;
1165 int error = 0;
1166
320dd101 1167restart:
b3b94faa
DT
1168 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1169 set_bit(HIF_ABORTED, &gh->gh_iflags);
1170 return -EIO;
1171 }
1172
1173 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1174
1175 spin_lock(&gl->gl_spin);
1176 add_to_queue(gh);
1177 run_queue(gl);
1178 spin_unlock(&gl->gl_spin);
1179
1180 if (!(gh->gh_flags & GL_ASYNC)) {
1181 error = glock_wait_internal(gh);
1182 if (error == GLR_CANCELED) {
190562bd 1183 msleep(100);
b3b94faa
DT
1184 goto restart;
1185 }
1186 }
1187
b3b94faa
DT
1188 return error;
1189}
1190
1191/**
1192 * gfs2_glock_poll - poll to see if an async request has been completed
1193 * @gh: the holder
1194 *
1195 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1196 */
1197
1198int gfs2_glock_poll(struct gfs2_holder *gh)
1199{
1200 struct gfs2_glock *gl = gh->gh_gl;
1201 int ready = 0;
1202
1203 spin_lock(&gl->gl_spin);
1204
1205 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1206 ready = 1;
1207 else if (list_empty(&gh->gh_list)) {
1208 if (gh->gh_error == GLR_CANCELED) {
1209 spin_unlock(&gl->gl_spin);
190562bd 1210 msleep(100);
b3b94faa
DT
1211 if (gfs2_glock_nq(gh))
1212 return 1;
1213 return 0;
1214 } else
1215 ready = 1;
1216 }
1217
1218 spin_unlock(&gl->gl_spin);
1219
1220 return ready;
1221}
1222
1223/**
1224 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1225 * @gh: the holder structure
1226 *
1227 * Returns: 0, GLR_TRYFAILED, or errno on failure
1228 */
1229
1230int gfs2_glock_wait(struct gfs2_holder *gh)
1231{
1232 int error;
1233
1234 error = glock_wait_internal(gh);
1235 if (error == GLR_CANCELED) {
190562bd 1236 msleep(100);
b3b94faa
DT
1237 gh->gh_flags &= ~GL_ASYNC;
1238 error = gfs2_glock_nq(gh);
1239 }
1240
1241 return error;
1242}
1243
1244/**
1245 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1246 * @gh: the glock holder
1247 *
1248 */
1249
1250void gfs2_glock_dq(struct gfs2_holder *gh)
1251{
1252 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1253 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa 1254
b3b94faa
DT
1255 if (gh->gh_flags & GL_NOCACHE)
1256 handle_callback(gl, LM_ST_UNLOCKED);
1257
1258 gfs2_glmutex_lock(gl);
1259
1260 spin_lock(&gl->gl_spin);
1261 list_del_init(&gh->gh_list);
1262
1263 if (list_empty(&gl->gl_holders)) {
1264 spin_unlock(&gl->gl_spin);
1265
1266 if (glops->go_unlock)
1267 glops->go_unlock(gh);
1268
b3b94faa
DT
1269 gl->gl_stamp = jiffies;
1270
1271 spin_lock(&gl->gl_spin);
1272 }
1273
1274 clear_bit(GLF_LOCK, &gl->gl_flags);
1275 run_queue(gl);
1276 spin_unlock(&gl->gl_spin);
1277}
1278
b3b94faa
DT
1279/**
1280 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1281 * @gh: the holder structure
1282 *
1283 */
1284
1285void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1286{
1287 gfs2_glock_dq(gh);
1288 gfs2_holder_uninit(gh);
1289}
1290
1291/**
1292 * gfs2_glock_nq_num - acquire a glock based on lock number
1293 * @sdp: the filesystem
1294 * @number: the lock number
1295 * @glops: the glock operations for the type of glock
1296 * @state: the state to acquire the glock in
1297 * @flags: modifier flags for the aquisition
1298 * @gh: the struct gfs2_holder
1299 *
1300 * Returns: errno
1301 */
1302
cd915493 1303int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
8fb4b536
SW
1304 const struct gfs2_glock_operations *glops,
1305 unsigned int state, int flags, struct gfs2_holder *gh)
b3b94faa
DT
1306{
1307 struct gfs2_glock *gl;
1308 int error;
1309
1310 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1311 if (!error) {
1312 error = gfs2_glock_nq_init(gl, state, flags, gh);
1313 gfs2_glock_put(gl);
1314 }
1315
1316 return error;
1317}
1318
1319/**
1320 * glock_compare - Compare two struct gfs2_glock structures for sorting
1321 * @arg_a: the first structure
1322 * @arg_b: the second structure
1323 *
1324 */
1325
1326static int glock_compare(const void *arg_a, const void *arg_b)
1327{
a5e08a9e
SW
1328 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1329 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1330 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1331 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
b3b94faa
DT
1332
1333 if (a->ln_number > b->ln_number)
a5e08a9e
SW
1334 return 1;
1335 if (a->ln_number < b->ln_number)
1336 return -1;
1c0f4872 1337 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
a5e08a9e 1338 return 0;
b3b94faa
DT
1339}
1340
1341/**
1342 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1343 * @num_gh: the number of structures
1344 * @ghs: an array of struct gfs2_holder structures
1345 *
1346 * Returns: 0 on success (all glocks acquired),
1347 * errno on failure (no glocks acquired)
1348 */
1349
1350static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1351 struct gfs2_holder **p)
1352{
1353 unsigned int x;
1354 int error = 0;
1355
1356 for (x = 0; x < num_gh; x++)
1357 p[x] = &ghs[x];
1358
1359 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1360
1361 for (x = 0; x < num_gh; x++) {
1362 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1363
1364 error = gfs2_glock_nq(p[x]);
1365 if (error) {
1366 while (x--)
1367 gfs2_glock_dq(p[x]);
1368 break;
1369 }
1370 }
1371
1372 return error;
1373}
1374
1375/**
1376 * gfs2_glock_nq_m - acquire multiple glocks
1377 * @num_gh: the number of structures
1378 * @ghs: an array of struct gfs2_holder structures
1379 *
1380 * Figure out how big an impact this function has. Either:
1381 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1382 * 2) Forget async stuff and just call nq_m_sync()
1383 * 3) Leave it like it is
1384 *
1385 * Returns: 0 on success (all glocks acquired),
1386 * errno on failure (no glocks acquired)
1387 */
1388
1389int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1390{
1391 int *e;
1392 unsigned int x;
1393 int borked = 0, serious = 0;
1394 int error = 0;
1395
1396 if (!num_gh)
1397 return 0;
1398
1399 if (num_gh == 1) {
1400 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1401 return gfs2_glock_nq(ghs);
1402 }
1403
1404 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1405 if (!e)
1406 return -ENOMEM;
1407
1408 for (x = 0; x < num_gh; x++) {
1409 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1410 error = gfs2_glock_nq(&ghs[x]);
1411 if (error) {
1412 borked = 1;
1413 serious = error;
1414 num_gh = x;
1415 break;
1416 }
1417 }
1418
1419 for (x = 0; x < num_gh; x++) {
1420 error = e[x] = glock_wait_internal(&ghs[x]);
1421 if (error) {
1422 borked = 1;
1423 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1424 serious = error;
1425 }
1426 }
1427
1428 if (!borked) {
1429 kfree(e);
1430 return 0;
1431 }
1432
1433 for (x = 0; x < num_gh; x++)
1434 if (!e[x])
1435 gfs2_glock_dq(&ghs[x]);
1436
1437 if (serious)
1438 error = serious;
1439 else {
1440 for (x = 0; x < num_gh; x++)
1441 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1442 &ghs[x]);
1443 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1444 }
1445
1446 kfree(e);
1447
1448 return error;
1449}
1450
1451/**
1452 * gfs2_glock_dq_m - release multiple glocks
1453 * @num_gh: the number of structures
1454 * @ghs: an array of struct gfs2_holder structures
1455 *
1456 */
1457
1458void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1459{
1460 unsigned int x;
1461
1462 for (x = 0; x < num_gh; x++)
1463 gfs2_glock_dq(&ghs[x]);
1464}
1465
1466/**
1467 * gfs2_glock_dq_uninit_m - release multiple glocks
1468 * @num_gh: the number of structures
1469 * @ghs: an array of struct gfs2_holder structures
1470 *
1471 */
1472
1473void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1474{
1475 unsigned int x;
1476
1477 for (x = 0; x < num_gh; x++)
1478 gfs2_glock_dq_uninit(&ghs[x]);
1479}
1480
b3b94faa
DT
1481/**
1482 * gfs2_lvb_hold - attach a LVB from a glock
1483 * @gl: The glock in question
1484 *
1485 */
1486
1487int gfs2_lvb_hold(struct gfs2_glock *gl)
1488{
1489 int error;
1490
1491 gfs2_glmutex_lock(gl);
1492
1493 if (!atomic_read(&gl->gl_lvb_count)) {
1494 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1495 if (error) {
1496 gfs2_glmutex_unlock(gl);
1497 return error;
1498 }
1499 gfs2_glock_hold(gl);
1500 }
1501 atomic_inc(&gl->gl_lvb_count);
1502
1503 gfs2_glmutex_unlock(gl);
1504
1505 return 0;
1506}
1507
1508/**
1509 * gfs2_lvb_unhold - detach a LVB from a glock
1510 * @gl: The glock in question
1511 *
1512 */
1513
1514void gfs2_lvb_unhold(struct gfs2_glock *gl)
1515{
1516 gfs2_glock_hold(gl);
1517 gfs2_glmutex_lock(gl);
1518
1519 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1520 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1521 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1522 gl->gl_lvb = NULL;
1523 gfs2_glock_put(gl);
1524 }
1525
1526 gfs2_glmutex_unlock(gl);
1527 gfs2_glock_put(gl);
1528}
1529
b3b94faa
DT
1530static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1531 unsigned int state)
1532{
1533 struct gfs2_glock *gl;
1534
1535 gl = gfs2_glock_find(sdp, name);
1536 if (!gl)
1537 return;
1538
b3b94faa
DT
1539 handle_callback(gl, state);
1540
1541 spin_lock(&gl->gl_spin);
1542 run_queue(gl);
1543 spin_unlock(&gl->gl_spin);
1544
1545 gfs2_glock_put(gl);
1546}
1547
1548/**
1549 * gfs2_glock_cb - Callback used by locking module
1c089c32 1550 * @sdp: Pointer to the superblock
b3b94faa
DT
1551 * @type: Type of callback
1552 * @data: Type dependent data pointer
1553 *
1554 * Called by the locking module when it wants to tell us something.
1555 * Either we need to drop a lock, one of our ASYNC requests completed, or
1556 * a journal from another client needs to be recovered.
1557 */
1558
9b47c11d 1559void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
b3b94faa 1560{
9b47c11d 1561 struct gfs2_sbd *sdp = cb_data;
b3b94faa 1562
b3b94faa
DT
1563 switch (type) {
1564 case LM_CB_NEED_E:
e7f5c01c 1565 blocking_cb(sdp, data, LM_ST_UNLOCKED);
b3b94faa
DT
1566 return;
1567
1568 case LM_CB_NEED_D:
e7f5c01c 1569 blocking_cb(sdp, data, LM_ST_DEFERRED);
b3b94faa
DT
1570 return;
1571
1572 case LM_CB_NEED_S:
e7f5c01c 1573 blocking_cb(sdp, data, LM_ST_SHARED);
b3b94faa
DT
1574 return;
1575
1576 case LM_CB_ASYNC: {
e7f5c01c 1577 struct lm_async_cb *async = data;
b3b94faa
DT
1578 struct gfs2_glock *gl;
1579
61be084e 1580 down_read(&gfs2_umount_flush_sem);
b3b94faa
DT
1581 gl = gfs2_glock_find(sdp, &async->lc_name);
1582 if (gfs2_assert_warn(sdp, gl))
1583 return;
1584 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1585 gl->gl_req_bh(gl, async->lc_ret);
1586 gfs2_glock_put(gl);
61be084e 1587 up_read(&gfs2_umount_flush_sem);
b3b94faa
DT
1588 return;
1589 }
1590
1591 case LM_CB_NEED_RECOVERY:
1592 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1593 if (sdp->sd_recoverd_process)
1594 wake_up_process(sdp->sd_recoverd_process);
1595 return;
1596
1597 case LM_CB_DROPLOCKS:
1598 gfs2_gl_hash_clear(sdp, NO_WAIT);
1599 gfs2_quota_scan(sdp);
1600 return;
1601
1602 default:
1603 gfs2_assert_warn(sdp, 0);
1604 return;
1605 }
1606}
1607
b3b94faa
DT
1608/**
1609 * demote_ok - Check to see if it's ok to unlock a glock
1610 * @gl: the glock
1611 *
1612 * Returns: 1 if it's ok
1613 */
1614
1615static int demote_ok(struct gfs2_glock *gl)
1616{
8fb4b536 1617 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1618 int demote = 1;
1619
1620 if (test_bit(GLF_STICKY, &gl->gl_flags))
1621 demote = 0;
b3b94faa
DT
1622 else if (glops->go_demote_ok)
1623 demote = glops->go_demote_ok(gl);
1624
1625 return demote;
1626}
1627
1628/**
1629 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1630 * @gl: the glock
1631 *
1632 */
1633
1634void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1635{
1636 struct gfs2_sbd *sdp = gl->gl_sbd;
1637
1638 spin_lock(&sdp->sd_reclaim_lock);
1639 if (list_empty(&gl->gl_reclaim)) {
1640 gfs2_glock_hold(gl);
1641 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1642 atomic_inc(&sdp->sd_reclaim_count);
1643 }
1644 spin_unlock(&sdp->sd_reclaim_lock);
1645
1646 wake_up(&sdp->sd_reclaim_wq);
1647}
1648
1649/**
1650 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1651 * @sdp: the filesystem
1652 *
1653 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1654 * different glock and we notice that there are a lot of glocks in the
1655 * reclaim list.
1656 *
1657 */
1658
1659void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1660{
1661 struct gfs2_glock *gl;
1662
1663 spin_lock(&sdp->sd_reclaim_lock);
1664 if (list_empty(&sdp->sd_reclaim_list)) {
1665 spin_unlock(&sdp->sd_reclaim_lock);
1666 return;
1667 }
1668 gl = list_entry(sdp->sd_reclaim_list.next,
1669 struct gfs2_glock, gl_reclaim);
1670 list_del_init(&gl->gl_reclaim);
1671 spin_unlock(&sdp->sd_reclaim_lock);
1672
1673 atomic_dec(&sdp->sd_reclaim_count);
1674 atomic_inc(&sdp->sd_reclaimed);
1675
1676 if (gfs2_glmutex_trylock(gl)) {
12132933 1677 if (list_empty(&gl->gl_holders) &&
50299965 1678 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
b3b94faa
DT
1679 handle_callback(gl, LM_ST_UNLOCKED);
1680 gfs2_glmutex_unlock(gl);
1681 }
1682
1683 gfs2_glock_put(gl);
1684}
1685
1686/**
1687 * examine_bucket - Call a function for glock in a hash bucket
1688 * @examiner: the function
1689 * @sdp: the filesystem
1690 * @bucket: the bucket
1691 *
1692 * Returns: 1 if the bucket has entries
1693 */
1694
1695static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
37b2fa6a 1696 unsigned int hash)
b3b94faa 1697{
24264434
SW
1698 struct gfs2_glock *gl, *prev = NULL;
1699 int has_entries = 0;
b6397893 1700 struct hlist_head *head = &gl_hash_table[hash].hb_list;
b3b94faa 1701
24264434 1702 read_lock(gl_lock_addr(hash));
b6397893
SW
1703 /* Can't use hlist_for_each_entry - don't want prefetch here */
1704 if (hlist_empty(head))
24264434 1705 goto out;
b6397893
SW
1706 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1707 while(1) {
24264434 1708 if (gl->gl_sbd == sdp) {
b3b94faa 1709 gfs2_glock_hold(gl);
24264434
SW
1710 read_unlock(gl_lock_addr(hash));
1711 if (prev)
1712 gfs2_glock_put(prev);
1713 prev = gl;
1714 examiner(gl);
a8336344 1715 has_entries = 1;
24264434 1716 read_lock(gl_lock_addr(hash));
b3b94faa 1717 }
b6397893
SW
1718 if (gl->gl_list.next == NULL)
1719 break;
24264434 1720 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
b3b94faa 1721 }
24264434
SW
1722out:
1723 read_unlock(gl_lock_addr(hash));
1724 if (prev)
1725 gfs2_glock_put(prev);
1726 return has_entries;
b3b94faa
DT
1727}
1728
1729/**
1730 * scan_glock - look at a glock and see if we can reclaim it
1731 * @gl: the glock to look at
1732 *
1733 */
1734
1735static void scan_glock(struct gfs2_glock *gl)
1736{
b004157a 1737 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
24264434 1738 return;
a2242db0 1739
b3b94faa 1740 if (gfs2_glmutex_trylock(gl)) {
12132933 1741 if (list_empty(&gl->gl_holders) &&
24264434 1742 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
b3b94faa 1743 goto out_schedule;
b3b94faa
DT
1744 gfs2_glmutex_unlock(gl);
1745 }
b3b94faa
DT
1746 return;
1747
627add2d 1748out_schedule:
b3b94faa
DT
1749 gfs2_glmutex_unlock(gl);
1750 gfs2_glock_schedule_for_reclaim(gl);
b3b94faa
DT
1751}
1752
1753/**
1754 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1755 * @sdp: the filesystem
1756 *
1757 */
1758
1759void gfs2_scand_internal(struct gfs2_sbd *sdp)
1760{
1761 unsigned int x;
1762
94610610 1763 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
37b2fa6a 1764 examine_bucket(scan_glock, sdp, x);
b3b94faa
DT
1765}
1766
1767/**
1768 * clear_glock - look at a glock and see if we can free it from glock cache
1769 * @gl: the glock to look at
1770 *
1771 */
1772
1773static void clear_glock(struct gfs2_glock *gl)
1774{
1775 struct gfs2_sbd *sdp = gl->gl_sbd;
1776 int released;
1777
1778 spin_lock(&sdp->sd_reclaim_lock);
1779 if (!list_empty(&gl->gl_reclaim)) {
1780 list_del_init(&gl->gl_reclaim);
1781 atomic_dec(&sdp->sd_reclaim_count);
190562bd 1782 spin_unlock(&sdp->sd_reclaim_lock);
b3b94faa
DT
1783 released = gfs2_glock_put(gl);
1784 gfs2_assert(sdp, !released);
190562bd
SW
1785 } else {
1786 spin_unlock(&sdp->sd_reclaim_lock);
b3b94faa 1787 }
b3b94faa
DT
1788
1789 if (gfs2_glmutex_trylock(gl)) {
90101c31 1790 if (list_empty(&gl->gl_holders) &&
b3b94faa
DT
1791 gl->gl_state != LM_ST_UNLOCKED)
1792 handle_callback(gl, LM_ST_UNLOCKED);
b3b94faa
DT
1793 gfs2_glmutex_unlock(gl);
1794 }
b3b94faa
DT
1795}
1796
1797/**
1798 * gfs2_gl_hash_clear - Empty out the glock hash table
1799 * @sdp: the filesystem
1800 * @wait: wait until it's all gone
1801 *
1802 * Called when unmounting the filesystem, or when inter-node lock manager
1803 * requests DROPLOCKS because it is running out of capacity.
1804 */
1805
1806void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
1807{
1808 unsigned long t;
1809 unsigned int x;
1810 int cont;
1811
1812 t = jiffies;
1813
1814 for (;;) {
1815 cont = 0;
24264434 1816 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
907b9bce 1817 if (examine_bucket(clear_glock, sdp, x))
b3b94faa 1818 cont = 1;
24264434 1819 }
b3b94faa
DT
1820
1821 if (!wait || !cont)
1822 break;
1823
1824 if (time_after_eq(jiffies,
1825 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1826 fs_warn(sdp, "Unmount seems to be stalled. "
1827 "Dumping lock state...\n");
1828 gfs2_dump_lockstate(sdp);
1829 t = jiffies;
1830 }
1831
61be084e 1832 down_write(&gfs2_umount_flush_sem);
b3b94faa 1833 invalidate_inodes(sdp->sd_vfs);
61be084e 1834 up_write(&gfs2_umount_flush_sem);
fd88de56 1835 msleep(10);
b3b94faa
DT
1836 }
1837}
1838
1839/*
1840 * Diagnostic routines to help debug distributed deadlock
1841 */
1842
1843/**
1844 * dump_holder - print information about a glock holder
1845 * @str: a string naming the type of holder
1846 * @gh: the glock holder
1847 *
1848 * Returns: 0 on success, -ENOBUFS when we run out of space
1849 */
1850
1851static int dump_holder(char *str, struct gfs2_holder *gh)
1852{
1853 unsigned int x;
1854 int error = -ENOBUFS;
1855
d92a8d48
SW
1856 printk(KERN_INFO " %s\n", str);
1857 printk(KERN_INFO " owner = %ld\n",
b3b94faa 1858 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
d92a8d48
SW
1859 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
1860 printk(KERN_INFO " gh_flags =");
b3b94faa
DT
1861 for (x = 0; x < 32; x++)
1862 if (gh->gh_flags & (1 << x))
1863 printk(" %u", x);
1864 printk(" \n");
d92a8d48
SW
1865 printk(KERN_INFO " error = %d\n", gh->gh_error);
1866 printk(KERN_INFO " gh_iflags =");
b3b94faa
DT
1867 for (x = 0; x < 32; x++)
1868 if (test_bit(x, &gh->gh_iflags))
1869 printk(" %u", x);
1870 printk(" \n");
d0dc80db 1871 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
b3b94faa
DT
1872
1873 error = 0;
1874
1875 return error;
1876}
1877
1878/**
1879 * dump_inode - print information about an inode
1880 * @ip: the inode
1881 *
1882 * Returns: 0 on success, -ENOBUFS when we run out of space
1883 */
1884
1885static int dump_inode(struct gfs2_inode *ip)
1886{
1887 unsigned int x;
1888 int error = -ENOBUFS;
1889
d92a8d48
SW
1890 printk(KERN_INFO " Inode:\n");
1891 printk(KERN_INFO " num = %llu %llu\n",
382066da
SW
1892 (unsigned long long)ip->i_num.no_formal_ino,
1893 (unsigned long long)ip->i_num.no_addr);
b60623c2 1894 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_inode.i_mode));
d92a8d48 1895 printk(KERN_INFO " i_flags =");
b3b94faa
DT
1896 for (x = 0; x < 32; x++)
1897 if (test_bit(x, &ip->i_flags))
1898 printk(" %u", x);
1899 printk(" \n");
b3b94faa
DT
1900
1901 error = 0;
1902
1903 return error;
1904}
1905
1906/**
1907 * dump_glock - print information about a glock
1908 * @gl: the glock
1909 * @count: where we are in the buffer
1910 *
1911 * Returns: 0 on success, -ENOBUFS when we run out of space
1912 */
1913
1914static int dump_glock(struct gfs2_glock *gl)
1915{
1916 struct gfs2_holder *gh;
1917 unsigned int x;
1918 int error = -ENOBUFS;
1919
1920 spin_lock(&gl->gl_spin);
1921
85d1da67 1922 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
382066da 1923 (unsigned long long)gl->gl_name.ln_number);
d92a8d48 1924 printk(KERN_INFO " gl_flags =");
85d1da67 1925 for (x = 0; x < 32; x++) {
b3b94faa
DT
1926 if (test_bit(x, &gl->gl_flags))
1927 printk(" %u", x);
85d1da67 1928 }
b3b94faa 1929 printk(" \n");
16feb9fe 1930 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref));
d92a8d48 1931 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
320dd101
SW
1932 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
1933 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
d92a8d48
SW
1934 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
1935 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
1936 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
1937 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
1938 printk(KERN_INFO " le = %s\n",
b3b94faa 1939 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
d92a8d48 1940 printk(KERN_INFO " reclaim = %s\n",
b3b94faa
DT
1941 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
1942 if (gl->gl_aspace)
85d1da67 1943 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
88721877 1944 gl->gl_aspace->i_mapping->nrpages);
b3b94faa 1945 else
d92a8d48
SW
1946 printk(KERN_INFO " aspace = no\n");
1947 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
b3b94faa
DT
1948 if (gl->gl_req_gh) {
1949 error = dump_holder("Request", gl->gl_req_gh);
1950 if (error)
1951 goto out;
1952 }
1953 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1954 error = dump_holder("Holder", gh);
1955 if (error)
1956 goto out;
1957 }
1958 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
1959 error = dump_holder("Waiter1", gh);
1960 if (error)
1961 goto out;
1962 }
1963 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
1964 error = dump_holder("Waiter2", gh);
1965 if (error)
1966 goto out;
1967 }
1968 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
1969 error = dump_holder("Waiter3", gh);
1970 if (error)
1971 goto out;
1972 }
5c676f6d 1973 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
b3b94faa
DT
1974 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
1975 list_empty(&gl->gl_holders)) {
5c676f6d 1976 error = dump_inode(gl->gl_object);
b3b94faa
DT
1977 if (error)
1978 goto out;
1979 } else {
1980 error = -ENOBUFS;
d92a8d48 1981 printk(KERN_INFO " Inode: busy\n");
b3b94faa
DT
1982 }
1983 }
1984
1985 error = 0;
1986
a91ea69f 1987out:
b3b94faa 1988 spin_unlock(&gl->gl_spin);
b3b94faa
DT
1989 return error;
1990}
1991
1992/**
1993 * gfs2_dump_lockstate - print out the current lockstate
1994 * @sdp: the filesystem
1995 * @ub: the buffer to copy the information into
1996 *
1997 * If @ub is NULL, dump the lockstate to the console.
1998 *
1999 */
2000
08bc2dbc 2001static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
b3b94faa 2002{
b3b94faa 2003 struct gfs2_glock *gl;
b6397893 2004 struct hlist_node *h;
b3b94faa
DT
2005 unsigned int x;
2006 int error = 0;
2007
2008 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
b3b94faa 2009
087efdd3 2010 read_lock(gl_lock_addr(x));
b3b94faa 2011
b6397893 2012 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
85d1da67
SW
2013 if (gl->gl_sbd != sdp)
2014 continue;
b3b94faa
DT
2015
2016 error = dump_glock(gl);
2017 if (error)
2018 break;
2019 }
2020
087efdd3 2021 read_unlock(gl_lock_addr(x));
b3b94faa
DT
2022
2023 if (error)
2024 break;
2025 }
2026
2027
2028 return error;
2029}
2030
85d1da67
SW
2031int __init gfs2_glock_init(void)
2032{
2033 unsigned i;
2034 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
b6397893 2035 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
85d1da67 2036 }
087efdd3
SW
2037#ifdef GL_HASH_LOCK_SZ
2038 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2039 rwlock_init(&gl_hash_locks[i]);
2040 }
2041#endif
85d1da67
SW
2042 return 0;
2043}
2044