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