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