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