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