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