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