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