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