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