Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[linux-2.6-block.git] / fs / gfs2 / glock.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
cf45b752 3 * Copyright (C) 2004-2008 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>
fee852e3 22#include <linux/wait.h>
95d97b7d 23#include <linux/module.h>
61be084e 24#include <linux/rwsem.h>
b3b94faa 25#include <asm/uaccess.h>
7c52b166
RP
26#include <linux/seq_file.h>
27#include <linux/debugfs.h>
8fbbfd21
SW
28#include <linux/kthread.h>
29#include <linux/freezer.h>
c4f68a13
BM
30#include <linux/workqueue.h>
31#include <linux/jiffies.h>
b3b94faa
DT
32
33#include "gfs2.h"
5c676f6d 34#include "incore.h"
b3b94faa
DT
35#include "glock.h"
36#include "glops.h"
37#include "inode.h"
b3b94faa
DT
38#include "lops.h"
39#include "meta_io.h"
40#include "quota.h"
41#include "super.h"
5c676f6d 42#include "util.h"
813e0c46 43#include "bmap.h"
b3b94faa 44
37b2fa6a 45struct gfs2_gl_hash_bucket {
b6397893 46 struct hlist_head hb_list;
37b2fa6a
SW
47};
48
6802e340
SW
49struct gfs2_glock_iter {
50 int hash; /* hash bucket index */
51 struct gfs2_sbd *sdp; /* incore superblock */
52 struct gfs2_glock *gl; /* current glock struct */
53 char string[512]; /* scratch space */
7c52b166
RP
54};
55
b3b94faa
DT
56typedef void (*glock_examiner) (struct gfs2_glock * gl);
57
08bc2dbc 58static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
6802e340
SW
59static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
60#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
61static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
c4f68a13 62
61be084e 63static DECLARE_RWSEM(gfs2_umount_flush_sem);
7c52b166 64static struct dentry *gfs2_root;
c4f68a13 65static struct workqueue_struct *glock_workqueue;
97cc1025
SW
66static LIST_HEAD(lru_list);
67static atomic_t lru_count = ATOMIC_INIT(0);
eb8374e7 68static DEFINE_SPINLOCK(lru_lock);
08bc2dbc 69
b6397893 70#define GFS2_GL_HASH_SHIFT 15
087efdd3
SW
71#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
72#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
73
85d1da67 74static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
04b933f2 75static struct dentry *gfs2_root;
087efdd3
SW
76
77/*
78 * Despite what you might think, the numbers below are not arbitrary :-)
79 * They are taken from the ipv4 routing hash code, which is well tested
80 * and thus should be nearly optimal. Later on we might tweek the numbers
81 * but for now this should be fine.
82 *
83 * The reason for putting the locks in a separate array from the list heads
84 * is that we can have fewer locks than list heads and save memory. We use
85 * the same hash function for both, but with a different hash mask.
86 */
87#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
88 defined(CONFIG_PROVE_LOCKING)
89
90#ifdef CONFIG_LOCKDEP
91# define GL_HASH_LOCK_SZ 256
92#else
93# if NR_CPUS >= 32
94# define GL_HASH_LOCK_SZ 4096
95# elif NR_CPUS >= 16
96# define GL_HASH_LOCK_SZ 2048
97# elif NR_CPUS >= 8
98# define GL_HASH_LOCK_SZ 1024
99# elif NR_CPUS >= 4
100# define GL_HASH_LOCK_SZ 512
101# else
102# define GL_HASH_LOCK_SZ 256
103# endif
104#endif
105
106/* We never want more locks than chains */
107#if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
108# undef GL_HASH_LOCK_SZ
109# define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
110#endif
111
112static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
113
114static inline rwlock_t *gl_lock_addr(unsigned int x)
115{
94610610 116 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
087efdd3
SW
117}
118#else /* not SMP, so no spinlocks required */
0ac23069 119static inline rwlock_t *gl_lock_addr(unsigned int x)
087efdd3
SW
120{
121 return NULL;
122}
123#endif
85d1da67 124
b3b94faa
DT
125/**
126 * gl_hash() - Turn glock number into hash bucket number
127 * @lock: The glock number
128 *
129 * Returns: The number of the corresponding hash bucket
130 */
131
b8547856
SW
132static unsigned int gl_hash(const struct gfs2_sbd *sdp,
133 const struct lm_lockname *name)
b3b94faa
DT
134{
135 unsigned int h;
136
cd915493 137 h = jhash(&name->ln_number, sizeof(u64), 0);
b3b94faa 138 h = jhash(&name->ln_type, sizeof(unsigned int), h);
b8547856 139 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
b3b94faa
DT
140 h &= GFS2_GL_HASH_MASK;
141
142 return h;
143}
144
145/**
146 * glock_free() - Perform a few checks and then release struct gfs2_glock
147 * @gl: The glock to release
148 *
149 * Also calls lock module to release its internal structure for this glock.
150 *
151 */
152
153static void glock_free(struct gfs2_glock *gl)
154{
155 struct gfs2_sbd *sdp = gl->gl_sbd;
156 struct inode *aspace = gl->gl_aspace;
157
048bca22 158 if (sdp->sd_lockstruct.ls_ops->lm_put_lock)
da755fdb 159 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl->gl_lock);
b3b94faa
DT
160
161 if (aspace)
162 gfs2_aspace_put(aspace);
163
164 kmem_cache_free(gfs2_glock_cachep, gl);
b3b94faa
DT
165}
166
167/**
168 * gfs2_glock_hold() - increment reference count on glock
169 * @gl: The glock to hold
170 *
171 */
172
048786f1 173static void gfs2_glock_hold(struct gfs2_glock *gl)
b3b94faa 174{
16feb9fe 175 atomic_inc(&gl->gl_ref);
b3b94faa
DT
176}
177
97cc1025
SW
178/**
179 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
180 * @gl: the glock
181 *
182 */
183
184static void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
185{
186 spin_lock(&lru_lock);
187 if (list_empty(&gl->gl_lru) && gl->gl_state != LM_ST_UNLOCKED) {
188 list_add_tail(&gl->gl_lru, &lru_list);
189 atomic_inc(&lru_count);
190 }
191 spin_unlock(&lru_lock);
192}
193
b3b94faa
DT
194/**
195 * gfs2_glock_put() - Decrement reference count on glock
196 * @gl: The glock to put
197 *
198 */
199
200int gfs2_glock_put(struct gfs2_glock *gl)
201{
b3b94faa
DT
202 int rv = 0;
203
087efdd3 204 write_lock(gl_lock_addr(gl->gl_hash));
16feb9fe 205 if (atomic_dec_and_test(&gl->gl_ref)) {
b6397893 206 hlist_del(&gl->gl_list);
087efdd3 207 write_unlock(gl_lock_addr(gl->gl_hash));
97cc1025
SW
208 spin_lock(&lru_lock);
209 if (!list_empty(&gl->gl_lru)) {
210 list_del_init(&gl->gl_lru);
211 atomic_dec(&lru_count);
212 }
213 spin_unlock(&lru_lock);
6802e340 214 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_UNLOCKED);
97cc1025 215 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_lru));
6802e340 216 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
b3b94faa
DT
217 glock_free(gl);
218 rv = 1;
219 goto out;
220 }
087efdd3 221 write_unlock(gl_lock_addr(gl->gl_hash));
97cc1025
SW
222 /* 1 for being hashed, 1 for having state != LM_ST_UNLOCKED */
223 if (atomic_read(&gl->gl_ref) == 2)
224 gfs2_glock_schedule_for_reclaim(gl);
a2242db0 225out:
b3b94faa
DT
226 return rv;
227}
228
b3b94faa
DT
229/**
230 * search_bucket() - Find struct gfs2_glock by lock number
231 * @bucket: the bucket to search
232 * @name: The lock name
233 *
234 * Returns: NULL, or the struct gfs2_glock with the requested number
235 */
236
37b2fa6a 237static struct gfs2_glock *search_bucket(unsigned int hash,
899be4d3 238 const struct gfs2_sbd *sdp,
d6a53727 239 const struct lm_lockname *name)
b3b94faa
DT
240{
241 struct gfs2_glock *gl;
b6397893 242 struct hlist_node *h;
b3b94faa 243
b6397893 244 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
b3b94faa
DT
245 if (!lm_name_equal(&gl->gl_name, name))
246 continue;
899be4d3
SW
247 if (gl->gl_sbd != sdp)
248 continue;
b3b94faa 249
16feb9fe 250 atomic_inc(&gl->gl_ref);
b3b94faa
DT
251
252 return gl;
253 }
254
255 return NULL;
256}
257
258/**
259 * gfs2_glock_find() - Find glock by lock number
260 * @sdp: The GFS2 superblock
261 * @name: The lock name
262 *
263 * Returns: NULL, or the struct gfs2_glock with the requested number
264 */
265
85d1da67 266static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
d6a53727 267 const struct lm_lockname *name)
b3b94faa 268{
37b2fa6a 269 unsigned int hash = gl_hash(sdp, name);
b3b94faa
DT
270 struct gfs2_glock *gl;
271
087efdd3 272 read_lock(gl_lock_addr(hash));
37b2fa6a 273 gl = search_bucket(hash, sdp, name);
087efdd3 274 read_unlock(gl_lock_addr(hash));
b3b94faa
DT
275
276 return gl;
277}
278
6802e340
SW
279/**
280 * may_grant - check if its ok to grant a new lock
281 * @gl: The glock
282 * @gh: The lock request which we wish to grant
283 *
284 * Returns: true if its ok to grant the lock
285 */
286
287static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
288{
289 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
290 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
291 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
292 return 0;
293 if (gl->gl_state == gh->gh_state)
294 return 1;
295 if (gh->gh_flags & GL_EXACT)
296 return 0;
209806ab
SW
297 if (gl->gl_state == LM_ST_EXCLUSIVE) {
298 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
299 return 1;
300 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
301 return 1;
302 }
6802e340
SW
303 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
304 return 1;
305 return 0;
306}
307
308static void gfs2_holder_wake(struct gfs2_holder *gh)
309{
310 clear_bit(HIF_WAIT, &gh->gh_iflags);
311 smp_mb__after_clear_bit();
312 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
313}
314
315/**
316 * do_promote - promote as many requests as possible on the current queue
317 * @gl: The glock
318 *
813e0c46
SW
319 * Returns: 1 if there is a blocked holder at the head of the list, or 2
320 * if a type specific operation is underway.
6802e340
SW
321 */
322
323static int do_promote(struct gfs2_glock *gl)
55ba474d
HH
324__releases(&gl->gl_spin)
325__acquires(&gl->gl_spin)
6802e340
SW
326{
327 const struct gfs2_glock_operations *glops = gl->gl_ops;
328 struct gfs2_holder *gh, *tmp;
329 int ret;
330
331restart:
332 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
333 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
334 continue;
335 if (may_grant(gl, gh)) {
336 if (gh->gh_list.prev == &gl->gl_holders &&
337 glops->go_lock) {
338 spin_unlock(&gl->gl_spin);
339 /* FIXME: eliminate this eventually */
340 ret = glops->go_lock(gh);
341 spin_lock(&gl->gl_spin);
342 if (ret) {
813e0c46
SW
343 if (ret == 1)
344 return 2;
6802e340
SW
345 gh->gh_error = ret;
346 list_del_init(&gh->gh_list);
347 gfs2_holder_wake(gh);
348 goto restart;
349 }
350 set_bit(HIF_HOLDER, &gh->gh_iflags);
351 gfs2_holder_wake(gh);
352 goto restart;
353 }
354 set_bit(HIF_HOLDER, &gh->gh_iflags);
355 gfs2_holder_wake(gh);
356 continue;
357 }
358 if (gh->gh_list.prev == &gl->gl_holders)
359 return 1;
360 break;
361 }
362 return 0;
363}
364
365/**
366 * do_error - Something unexpected has happened during a lock request
367 *
368 */
369
370static inline void do_error(struct gfs2_glock *gl, const int ret)
371{
372 struct gfs2_holder *gh, *tmp;
373
374 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
375 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
376 continue;
377 if (ret & LM_OUT_ERROR)
378 gh->gh_error = -EIO;
379 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
380 gh->gh_error = GLR_TRYFAILED;
381 else
382 continue;
383 list_del_init(&gh->gh_list);
384 gfs2_holder_wake(gh);
385 }
386}
387
388/**
389 * find_first_waiter - find the first gh that's waiting for the glock
390 * @gl: the glock
391 */
392
393static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
394{
395 struct gfs2_holder *gh;
396
397 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
398 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
399 return gh;
400 }
401 return NULL;
402}
403
404/**
405 * state_change - record that the glock is now in a different state
406 * @gl: the glock
407 * @new_state the new state
408 *
409 */
410
411static void state_change(struct gfs2_glock *gl, unsigned int new_state)
412{
413 int held1, held2;
414
415 held1 = (gl->gl_state != LM_ST_UNLOCKED);
416 held2 = (new_state != LM_ST_UNLOCKED);
417
418 if (held1 != held2) {
419 if (held2)
420 gfs2_glock_hold(gl);
421 else
422 gfs2_glock_put(gl);
423 }
424
425 gl->gl_state = new_state;
426 gl->gl_tchange = jiffies;
427}
428
429static void gfs2_demote_wake(struct gfs2_glock *gl)
430{
431 gl->gl_demote_state = LM_ST_EXCLUSIVE;
432 clear_bit(GLF_DEMOTE, &gl->gl_flags);
433 smp_mb__after_clear_bit();
434 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
435}
436
437/**
438 * finish_xmote - The DLM has replied to one of our lock requests
439 * @gl: The glock
440 * @ret: The status from the DLM
441 *
442 */
443
444static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
445{
446 const struct gfs2_glock_operations *glops = gl->gl_ops;
447 struct gfs2_holder *gh;
448 unsigned state = ret & LM_OUT_ST_MASK;
813e0c46 449 int rv;
6802e340
SW
450
451 spin_lock(&gl->gl_spin);
452 state_change(gl, state);
453 gh = find_first_waiter(gl);
454
455 /* Demote to UN request arrived during demote to SH or DF */
456 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
457 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
458 gl->gl_target = LM_ST_UNLOCKED;
459
460 /* Check for state != intended state */
461 if (unlikely(state != gl->gl_target)) {
462 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
463 /* move to back of queue and try next entry */
464 if (ret & LM_OUT_CANCELED) {
465 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
466 list_move_tail(&gh->gh_list, &gl->gl_holders);
467 gh = find_first_waiter(gl);
468 gl->gl_target = gh->gh_state;
469 goto retry;
470 }
471 /* Some error or failed "try lock" - report it */
472 if ((ret & LM_OUT_ERROR) ||
473 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
474 gl->gl_target = gl->gl_state;
475 do_error(gl, ret);
476 goto out;
477 }
478 }
479 switch(state) {
480 /* Unlocked due to conversion deadlock, try again */
481 case LM_ST_UNLOCKED:
482retry:
483 do_xmote(gl, gh, gl->gl_target);
484 break;
485 /* Conversion fails, unlock and try again */
486 case LM_ST_SHARED:
487 case LM_ST_DEFERRED:
488 do_xmote(gl, gh, LM_ST_UNLOCKED);
489 break;
490 default: /* Everything else */
491 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
492 GLOCK_BUG_ON(gl, 1);
493 }
494 spin_unlock(&gl->gl_spin);
495 gfs2_glock_put(gl);
496 return;
497 }
498
499 /* Fast path - we got what we asked for */
500 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
501 gfs2_demote_wake(gl);
502 if (state != LM_ST_UNLOCKED) {
503 if (glops->go_xmote_bh) {
6802e340
SW
504 spin_unlock(&gl->gl_spin);
505 rv = glops->go_xmote_bh(gl, gh);
506 if (rv == -EAGAIN)
507 return;
508 spin_lock(&gl->gl_spin);
509 if (rv) {
510 do_error(gl, rv);
511 goto out;
512 }
513 }
813e0c46
SW
514 rv = do_promote(gl);
515 if (rv == 2)
516 goto out_locked;
6802e340
SW
517 }
518out:
519 clear_bit(GLF_LOCK, &gl->gl_flags);
813e0c46 520out_locked:
6802e340
SW
521 spin_unlock(&gl->gl_spin);
522 gfs2_glock_put(gl);
523}
524
525static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
526 unsigned int cur_state, unsigned int req_state,
527 unsigned int flags)
528{
529 int ret = LM_OUT_ERROR;
048bca22
SW
530
531 if (!sdp->sd_lockstruct.ls_ops->lm_lock)
532 return req_state == LM_ST_UNLOCKED ? 0 : req_state;
533
6802e340
SW
534 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
535 ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock, cur_state,
536 req_state, flags);
537 return ret;
538}
539
540/**
541 * do_xmote - Calls the DLM to change the state of a lock
542 * @gl: The lock state
543 * @gh: The holder (only for promotes)
544 * @target: The target lock state
545 *
546 */
547
548static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
55ba474d
HH
549__releases(&gl->gl_spin)
550__acquires(&gl->gl_spin)
6802e340
SW
551{
552 const struct gfs2_glock_operations *glops = gl->gl_ops;
553 struct gfs2_sbd *sdp = gl->gl_sbd;
554 unsigned int lck_flags = gh ? gh->gh_flags : 0;
555 int ret;
556
557 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
558 LM_FLAG_PRIORITY);
559 BUG_ON(gl->gl_state == target);
560 BUG_ON(gl->gl_state == gl->gl_target);
561 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
562 glops->go_inval) {
563 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
564 do_error(gl, 0); /* Fail queued try locks */
565 }
566 spin_unlock(&gl->gl_spin);
567 if (glops->go_xmote_th)
568 glops->go_xmote_th(gl);
569 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
570 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
571 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
572
573 gfs2_glock_hold(gl);
574 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
575 gl->gl_state == LM_ST_DEFERRED) &&
576 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
577 lck_flags |= LM_FLAG_TRY_1CB;
578 ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, target, lck_flags);
579
580 if (!(ret & LM_OUT_ASYNC)) {
581 finish_xmote(gl, ret);
582 gfs2_glock_hold(gl);
583 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
584 gfs2_glock_put(gl);
585 } else {
586 GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC);
587 }
588 spin_lock(&gl->gl_spin);
589}
590
591/**
592 * find_first_holder - find the first "holder" gh
593 * @gl: the glock
594 */
595
596static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
597{
598 struct gfs2_holder *gh;
599
600 if (!list_empty(&gl->gl_holders)) {
601 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
602 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
603 return gh;
604 }
605 return NULL;
606}
607
608/**
609 * run_queue - do all outstanding tasks related to a glock
610 * @gl: The glock in question
611 * @nonblock: True if we must not block in run_queue
612 *
613 */
614
615static void run_queue(struct gfs2_glock *gl, const int nonblock)
55ba474d
HH
616__releases(&gl->gl_spin)
617__acquires(&gl->gl_spin)
6802e340
SW
618{
619 struct gfs2_holder *gh = NULL;
813e0c46 620 int ret;
6802e340
SW
621
622 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
623 return;
624
625 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
626
627 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
628 gl->gl_demote_state != gl->gl_state) {
629 if (find_first_holder(gl))
630 goto out;
631 if (nonblock)
632 goto out_sched;
633 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
265d529c 634 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
6802e340
SW
635 gl->gl_target = gl->gl_demote_state;
636 } else {
637 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
638 gfs2_demote_wake(gl);
813e0c46
SW
639 ret = do_promote(gl);
640 if (ret == 0)
6802e340 641 goto out;
813e0c46
SW
642 if (ret == 2)
643 return;
6802e340
SW
644 gh = find_first_waiter(gl);
645 gl->gl_target = gh->gh_state;
646 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
647 do_error(gl, 0); /* Fail queued try locks */
648 }
649 do_xmote(gl, gh, gl->gl_target);
650 return;
651
652out_sched:
653 gfs2_glock_hold(gl);
654 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
655 gfs2_glock_put(gl);
656out:
657 clear_bit(GLF_LOCK, &gl->gl_flags);
658}
659
c4f68a13
BM
660static void glock_work_func(struct work_struct *work)
661{
6802e340 662 unsigned long delay = 0;
c4f68a13
BM
663 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
664
6802e340
SW
665 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags))
666 finish_xmote(gl, gl->gl_reply);
c4f68a13 667 spin_lock(&gl->gl_spin);
265d529c
SW
668 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
669 gl->gl_state != LM_ST_UNLOCKED &&
670 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
6802e340
SW
671 unsigned long holdtime, now = jiffies;
672 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
673 if (time_before(now, holdtime))
674 delay = holdtime - now;
675 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
676 }
677 run_queue(gl, 0);
c4f68a13 678 spin_unlock(&gl->gl_spin);
6802e340
SW
679 if (!delay ||
680 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
681 gfs2_glock_put(gl);
c4f68a13
BM
682}
683
da755fdb
SW
684static int gfs2_lm_get_lock(struct gfs2_sbd *sdp, struct lm_lockname *name,
685 void **lockp)
686{
687 int error = -EIO;
048bca22
SW
688 if (!sdp->sd_lockstruct.ls_ops->lm_get_lock)
689 return 0;
da755fdb
SW
690 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
691 error = sdp->sd_lockstruct.ls_ops->lm_get_lock(
692 sdp->sd_lockstruct.ls_lockspace, name, lockp);
693 return error;
694}
695
b3b94faa
DT
696/**
697 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
698 * @sdp: The GFS2 superblock
699 * @number: the lock number
700 * @glops: The glock_operations to use
701 * @create: If 0, don't create the glock if it doesn't exist
702 * @glp: the glock is returned here
703 *
704 * This does not lock a glock, just finds/creates structures for one.
705 *
706 * Returns: errno
707 */
708
cd915493 709int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
8fb4b536 710 const struct gfs2_glock_operations *glops, int create,
b3b94faa
DT
711 struct gfs2_glock **glp)
712{
37b2fa6a 713 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
b3b94faa 714 struct gfs2_glock *gl, *tmp;
37b2fa6a 715 unsigned int hash = gl_hash(sdp, &name);
b3b94faa
DT
716 int error;
717
087efdd3 718 read_lock(gl_lock_addr(hash));
37b2fa6a 719 gl = search_bucket(hash, sdp, &name);
087efdd3 720 read_unlock(gl_lock_addr(hash));
b3b94faa
DT
721
722 if (gl || !create) {
723 *glp = gl;
724 return 0;
725 }
726
727 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
728 if (!gl)
729 return -ENOMEM;
730
ec45d9f5 731 gl->gl_flags = 0;
b3b94faa 732 gl->gl_name = name;
16feb9fe 733 atomic_set(&gl->gl_ref, 1);
b3b94faa 734 gl->gl_state = LM_ST_UNLOCKED;
6802e340 735 gl->gl_target = LM_ST_UNLOCKED;
c4f68a13 736 gl->gl_demote_state = LM_ST_EXCLUSIVE;
37b2fa6a 737 gl->gl_hash = hash;
b3b94faa 738 gl->gl_ops = glops;
ec45d9f5 739 gl->gl_stamp = jiffies;
c4f68a13 740 gl->gl_tchange = jiffies;
ec45d9f5 741 gl->gl_object = NULL;
b3b94faa 742 gl->gl_sbd = sdp;
ec45d9f5 743 gl->gl_aspace = NULL;
c4f68a13 744 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
b3b94faa
DT
745
746 /* If this glock protects actual on-disk data or metadata blocks,
747 create a VFS inode to manage the pages/buffers holding them. */
50299965 748 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
b3b94faa
DT
749 gl->gl_aspace = gfs2_aspace_get(sdp);
750 if (!gl->gl_aspace) {
751 error = -ENOMEM;
752 goto fail;
753 }
754 }
755
756 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
757 if (error)
758 goto fail_aspace;
759
087efdd3 760 write_lock(gl_lock_addr(hash));
37b2fa6a 761 tmp = search_bucket(hash, sdp, &name);
b3b94faa 762 if (tmp) {
087efdd3 763 write_unlock(gl_lock_addr(hash));
b3b94faa
DT
764 glock_free(gl);
765 gl = tmp;
766 } else {
b6397893 767 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
087efdd3 768 write_unlock(gl_lock_addr(hash));
b3b94faa
DT
769 }
770
771 *glp = gl;
772
773 return 0;
774
ec45d9f5 775fail_aspace:
b3b94faa
DT
776 if (gl->gl_aspace)
777 gfs2_aspace_put(gl->gl_aspace);
ec45d9f5 778fail:
907b9bce 779 kmem_cache_free(gfs2_glock_cachep, gl);
b3b94faa
DT
780 return error;
781}
782
783/**
784 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
785 * @gl: the glock
786 * @state: the state we're requesting
787 * @flags: the modifier flags
788 * @gh: the holder structure
789 *
790 */
791
190562bd 792void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
b3b94faa
DT
793 struct gfs2_holder *gh)
794{
795 INIT_LIST_HEAD(&gh->gh_list);
796 gh->gh_gl = gl;
d0dc80db 797 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b1e058da 798 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
799 gh->gh_state = state;
800 gh->gh_flags = flags;
801 gh->gh_error = 0;
802 gh->gh_iflags = 0;
b3b94faa
DT
803 gfs2_glock_hold(gl);
804}
805
806/**
807 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
808 * @state: the state we're requesting
809 * @flags: the modifier flags
810 * @gh: the holder structure
811 *
812 * Don't mess with the glock.
813 *
814 */
815
190562bd 816void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
b3b94faa
DT
817{
818 gh->gh_state = state;
579b78a4 819 gh->gh_flags = flags;
3b8249f6 820 gh->gh_iflags = 0;
d0dc80db 821 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b3b94faa
DT
822}
823
824/**
825 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
826 * @gh: the holder structure
827 *
828 */
829
830void gfs2_holder_uninit(struct gfs2_holder *gh)
831{
b1e058da 832 put_pid(gh->gh_owner_pid);
b3b94faa
DT
833 gfs2_glock_put(gh->gh_gl);
834 gh->gh_gl = NULL;
d0dc80db 835 gh->gh_ip = 0;
b3b94faa
DT
836}
837
d93cfa98 838static int just_schedule(void *word)
fee852e3
SW
839{
840 schedule();
841 return 0;
842}
843
6802e340 844static void wait_on_holder(struct gfs2_holder *gh)
da755fdb 845{
6802e340
SW
846 might_sleep();
847 wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
da755fdb
SW
848}
849
6802e340 850static void wait_on_demote(struct gfs2_glock *gl)
b3b94faa 851{
6802e340
SW
852 might_sleep();
853 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
b3b94faa
DT
854}
855
856/**
6802e340
SW
857 * handle_callback - process a demote request
858 * @gl: the glock
859 * @state: the state the caller wants us to change to
b3b94faa 860 *
6802e340
SW
861 * There are only two requests that we are going to see in actual
862 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
b3b94faa
DT
863 */
864
6802e340 865static void handle_callback(struct gfs2_glock *gl, unsigned int state,
97cc1025 866 unsigned long delay)
b3b94faa 867{
6802e340 868 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
b3b94faa 869
6802e340
SW
870 set_bit(bit, &gl->gl_flags);
871 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
872 gl->gl_demote_state = state;
873 gl->gl_demote_time = jiffies;
6802e340
SW
874 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
875 gl->gl_demote_state != state) {
876 gl->gl_demote_state = LM_ST_UNLOCKED;
b3b94faa 877 }
b3b94faa
DT
878}
879
880/**
6802e340 881 * gfs2_glock_wait - wait on a glock acquisition
b3b94faa
DT
882 * @gh: the glock holder
883 *
884 * Returns: 0 on success
885 */
886
6802e340 887int gfs2_glock_wait(struct gfs2_holder *gh)
b3b94faa 888{
fee852e3 889 wait_on_holder(gh);
b3b94faa
DT
890 return gh->gh_error;
891}
892
6802e340 893void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
7c52b166
RP
894{
895 va_list args;
896
897 va_start(args, fmt);
6802e340
SW
898 if (seq) {
899 struct gfs2_glock_iter *gi = seq->private;
7c52b166 900 vsprintf(gi->string, fmt, args);
6802e340
SW
901 seq_printf(seq, gi->string);
902 } else {
903 printk(KERN_ERR " ");
7c52b166 904 vprintk(fmt, args);
6802e340 905 }
7c52b166
RP
906 va_end(args);
907}
908
b3b94faa
DT
909/**
910 * add_to_queue - Add a holder to the wait queue (but look for recursion)
911 * @gh: the holder structure to add
912 *
6802e340
SW
913 * Eventually we should move the recursive locking trap to a
914 * debugging option or something like that. This is the fast
915 * path and needs to have the minimum number of distractions.
916 *
b3b94faa
DT
917 */
918
6802e340 919static inline void add_to_queue(struct gfs2_holder *gh)
55ba474d
HH
920__releases(&gl->gl_spin)
921__acquires(&gl->gl_spin)
b3b94faa
DT
922{
923 struct gfs2_glock *gl = gh->gh_gl;
6802e340
SW
924 struct gfs2_sbd *sdp = gl->gl_sbd;
925 struct list_head *insert_pt = NULL;
926 struct gfs2_holder *gh2;
927 int try_lock = 0;
b3b94faa 928
b1e058da 929 BUG_ON(gh->gh_owner_pid == NULL);
fee852e3
SW
930 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
931 BUG();
190562bd 932
6802e340
SW
933 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
934 if (test_bit(GLF_LOCK, &gl->gl_flags))
935 try_lock = 1;
936 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
937 goto fail;
938 }
939
940 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
941 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
942 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
943 goto trap_recursive;
944 if (try_lock &&
945 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
946 !may_grant(gl, gh)) {
947fail:
948 gh->gh_error = GLR_TRYFAILED;
949 gfs2_holder_wake(gh);
950 return;
b4c20166 951 }
6802e340
SW
952 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
953 continue;
954 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
955 insert_pt = &gh2->gh_list;
956 }
957 if (likely(insert_pt == NULL)) {
958 list_add_tail(&gh->gh_list, &gl->gl_holders);
959 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
960 goto do_cancel;
961 return;
962 }
963 list_add_tail(&gh->gh_list, insert_pt);
964do_cancel:
965 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
966 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
967 spin_unlock(&gl->gl_spin);
048bca22
SW
968 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
969 sdp->sd_lockstruct.ls_ops->lm_cancel(gl->gl_lock);
6802e340 970 spin_lock(&gl->gl_spin);
b3b94faa 971 }
6802e340 972 return;
b3b94faa 973
6802e340
SW
974trap_recursive:
975 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
976 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
977 printk(KERN_ERR "lock type: %d req lock state : %d\n",
978 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
979 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
980 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
981 printk(KERN_ERR "lock type: %d req lock state : %d\n",
982 gh->gh_gl->gl_name.ln_type, gh->gh_state);
983 __dump_glock(NULL, gl);
984 BUG();
b3b94faa
DT
985}
986
987/**
988 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
989 * @gh: the holder structure
990 *
991 * if (gh->gh_flags & GL_ASYNC), this never returns an error
992 *
993 * Returns: 0, GLR_TRYFAILED, or errno on failure
994 */
995
996int gfs2_glock_nq(struct gfs2_holder *gh)
997{
998 struct gfs2_glock *gl = gh->gh_gl;
999 struct gfs2_sbd *sdp = gl->gl_sbd;
1000 int error = 0;
1001
6802e340 1002 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
b3b94faa 1003 return -EIO;
b3b94faa 1004
b3b94faa
DT
1005 spin_lock(&gl->gl_spin);
1006 add_to_queue(gh);
6802e340 1007 run_queue(gl, 1);
b3b94faa
DT
1008 spin_unlock(&gl->gl_spin);
1009
6802e340
SW
1010 if (!(gh->gh_flags & GL_ASYNC))
1011 error = gfs2_glock_wait(gh);
b3b94faa 1012
b3b94faa
DT
1013 return error;
1014}
1015
1016/**
1017 * gfs2_glock_poll - poll to see if an async request has been completed
1018 * @gh: the holder
1019 *
1020 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1021 */
1022
1023int gfs2_glock_poll(struct gfs2_holder *gh)
1024{
6802e340 1025 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
b3b94faa
DT
1026}
1027
1028/**
1029 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1030 * @gh: the glock holder
1031 *
1032 */
1033
1034void gfs2_glock_dq(struct gfs2_holder *gh)
1035{
1036 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1037 const struct gfs2_glock_operations *glops = gl->gl_ops;
c4f68a13 1038 unsigned delay = 0;
6802e340 1039 int fast_path = 0;
b3b94faa 1040
6802e340 1041 spin_lock(&gl->gl_spin);
b3b94faa 1042 if (gh->gh_flags & GL_NOCACHE)
97cc1025 1043 handle_callback(gl, LM_ST_UNLOCKED, 0);
b3b94faa 1044
b3b94faa 1045 list_del_init(&gh->gh_list);
6802e340 1046 if (find_first_holder(gl) == NULL) {
3042a2cc 1047 if (glops->go_unlock) {
6802e340 1048 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
3042a2cc 1049 spin_unlock(&gl->gl_spin);
b3b94faa 1050 glops->go_unlock(gh);
3042a2cc 1051 spin_lock(&gl->gl_spin);
6802e340 1052 clear_bit(GLF_LOCK, &gl->gl_flags);
3042a2cc 1053 }
3b8249f6 1054 gl->gl_stamp = jiffies;
6802e340
SW
1055 if (list_empty(&gl->gl_holders) &&
1056 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1057 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1058 fast_path = 1;
b3b94faa 1059 }
b3b94faa 1060 spin_unlock(&gl->gl_spin);
6802e340
SW
1061 if (likely(fast_path))
1062 return;
c4f68a13
BM
1063
1064 gfs2_glock_hold(gl);
1065 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1066 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1067 delay = gl->gl_ops->go_min_hold_time;
1068 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1069 gfs2_glock_put(gl);
b3b94faa
DT
1070}
1071
d93cfa98
AD
1072void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1073{
1074 struct gfs2_glock *gl = gh->gh_gl;
1075 gfs2_glock_dq(gh);
1076 wait_on_demote(gl);
1077}
1078
b3b94faa
DT
1079/**
1080 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1081 * @gh: the holder structure
1082 *
1083 */
1084
1085void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1086{
1087 gfs2_glock_dq(gh);
1088 gfs2_holder_uninit(gh);
1089}
1090
1091/**
1092 * gfs2_glock_nq_num - acquire a glock based on lock number
1093 * @sdp: the filesystem
1094 * @number: the lock number
1095 * @glops: the glock operations for the type of glock
1096 * @state: the state to acquire the glock in
1097 * @flags: modifier flags for the aquisition
1098 * @gh: the struct gfs2_holder
1099 *
1100 * Returns: errno
1101 */
1102
cd915493 1103int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
8fb4b536
SW
1104 const struct gfs2_glock_operations *glops,
1105 unsigned int state, int flags, struct gfs2_holder *gh)
b3b94faa
DT
1106{
1107 struct gfs2_glock *gl;
1108 int error;
1109
1110 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1111 if (!error) {
1112 error = gfs2_glock_nq_init(gl, state, flags, gh);
1113 gfs2_glock_put(gl);
1114 }
1115
1116 return error;
1117}
1118
1119/**
1120 * glock_compare - Compare two struct gfs2_glock structures for sorting
1121 * @arg_a: the first structure
1122 * @arg_b: the second structure
1123 *
1124 */
1125
1126static int glock_compare(const void *arg_a, const void *arg_b)
1127{
a5e08a9e
SW
1128 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1129 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1130 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1131 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
b3b94faa
DT
1132
1133 if (a->ln_number > b->ln_number)
a5e08a9e
SW
1134 return 1;
1135 if (a->ln_number < b->ln_number)
1136 return -1;
1c0f4872 1137 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
a5e08a9e 1138 return 0;
b3b94faa
DT
1139}
1140
1141/**
1142 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1143 * @num_gh: the number of structures
1144 * @ghs: an array of struct gfs2_holder structures
1145 *
1146 * Returns: 0 on success (all glocks acquired),
1147 * errno on failure (no glocks acquired)
1148 */
1149
1150static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1151 struct gfs2_holder **p)
1152{
1153 unsigned int x;
1154 int error = 0;
1155
1156 for (x = 0; x < num_gh; x++)
1157 p[x] = &ghs[x];
1158
1159 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1160
1161 for (x = 0; x < num_gh; x++) {
1162 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1163
1164 error = gfs2_glock_nq(p[x]);
1165 if (error) {
1166 while (x--)
1167 gfs2_glock_dq(p[x]);
1168 break;
1169 }
1170 }
1171
1172 return error;
1173}
1174
1175/**
1176 * gfs2_glock_nq_m - acquire multiple glocks
1177 * @num_gh: the number of structures
1178 * @ghs: an array of struct gfs2_holder structures
1179 *
b3b94faa
DT
1180 *
1181 * Returns: 0 on success (all glocks acquired),
1182 * errno on failure (no glocks acquired)
1183 */
1184
1185int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1186{
eaf5bd3c
SW
1187 struct gfs2_holder *tmp[4];
1188 struct gfs2_holder **pph = tmp;
b3b94faa
DT
1189 int error = 0;
1190
eaf5bd3c
SW
1191 switch(num_gh) {
1192 case 0:
b3b94faa 1193 return 0;
eaf5bd3c 1194 case 1:
b3b94faa
DT
1195 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1196 return gfs2_glock_nq(ghs);
eaf5bd3c
SW
1197 default:
1198 if (num_gh <= 4)
b3b94faa 1199 break;
eaf5bd3c
SW
1200 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1201 if (!pph)
1202 return -ENOMEM;
b3b94faa
DT
1203 }
1204
eaf5bd3c 1205 error = nq_m_sync(num_gh, ghs, pph);
b3b94faa 1206
eaf5bd3c
SW
1207 if (pph != tmp)
1208 kfree(pph);
b3b94faa
DT
1209
1210 return error;
1211}
1212
1213/**
1214 * gfs2_glock_dq_m - release multiple glocks
1215 * @num_gh: the number of structures
1216 * @ghs: an array of struct gfs2_holder structures
1217 *
1218 */
1219
1220void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1221{
1222 unsigned int x;
1223
1224 for (x = 0; x < num_gh; x++)
1225 gfs2_glock_dq(&ghs[x]);
1226}
1227
1228/**
1229 * gfs2_glock_dq_uninit_m - release multiple glocks
1230 * @num_gh: the number of structures
1231 * @ghs: an array of struct gfs2_holder structures
1232 *
1233 */
1234
1235void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1236{
1237 unsigned int x;
1238
1239 for (x = 0; x < num_gh; x++)
1240 gfs2_glock_dq_uninit(&ghs[x]);
1241}
1242
da755fdb
SW
1243static int gfs2_lm_hold_lvb(struct gfs2_sbd *sdp, void *lock, char **lvbp)
1244{
1245 int error = -EIO;
048bca22
SW
1246 if (!sdp->sd_lockstruct.ls_ops->lm_hold_lvb)
1247 return 0;
da755fdb
SW
1248 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
1249 error = sdp->sd_lockstruct.ls_ops->lm_hold_lvb(lock, lvbp);
1250 return error;
1251}
1252
b3b94faa
DT
1253/**
1254 * gfs2_lvb_hold - attach a LVB from a glock
1255 * @gl: The glock in question
1256 *
1257 */
1258
1259int gfs2_lvb_hold(struct gfs2_glock *gl)
1260{
1261 int error;
1262
b3b94faa
DT
1263 if (!atomic_read(&gl->gl_lvb_count)) {
1264 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
6802e340 1265 if (error)
b3b94faa 1266 return error;
b3b94faa
DT
1267 gfs2_glock_hold(gl);
1268 }
1269 atomic_inc(&gl->gl_lvb_count);
1270
b3b94faa
DT
1271 return 0;
1272}
1273
1274/**
1275 * gfs2_lvb_unhold - detach a LVB from a glock
1276 * @gl: The glock in question
1277 *
1278 */
1279
1280void gfs2_lvb_unhold(struct gfs2_glock *gl)
1281{
da755fdb
SW
1282 struct gfs2_sbd *sdp = gl->gl_sbd;
1283
b3b94faa 1284 gfs2_glock_hold(gl);
b3b94faa
DT
1285 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1286 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
048bca22 1287 if (sdp->sd_lockstruct.ls_ops->lm_unhold_lvb)
da755fdb 1288 sdp->sd_lockstruct.ls_ops->lm_unhold_lvb(gl->gl_lock, gl->gl_lvb);
b3b94faa
DT
1289 gl->gl_lvb = NULL;
1290 gfs2_glock_put(gl);
1291 }
b3b94faa
DT
1292 gfs2_glock_put(gl);
1293}
1294
b3b94faa
DT
1295static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1296 unsigned int state)
1297{
1298 struct gfs2_glock *gl;
c4f68a13
BM
1299 unsigned long delay = 0;
1300 unsigned long holdtime;
1301 unsigned long now = jiffies;
b3b94faa
DT
1302
1303 gl = gfs2_glock_find(sdp, name);
1304 if (!gl)
1305 return;
1306
c4f68a13
BM
1307 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
1308 if (time_before(now, holdtime))
1309 delay = holdtime - now;
dff52574
SW
1310 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1311 delay = gl->gl_ops->go_min_hold_time;
b3b94faa 1312
6802e340 1313 spin_lock(&gl->gl_spin);
97cc1025 1314 handle_callback(gl, state, delay);
6802e340 1315 spin_unlock(&gl->gl_spin);
c4f68a13
BM
1316 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1317 gfs2_glock_put(gl);
b3b94faa
DT
1318}
1319
2bfb6449
SW
1320static void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
1321{
1322 struct gfs2_jdesc *jd;
1323
1324 spin_lock(&sdp->sd_jindex_spin);
1325 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
1326 if (jd->jd_jid != jid)
1327 continue;
1328 jd->jd_dirty = 1;
1329 break;
1330 }
1331 spin_unlock(&sdp->sd_jindex_spin);
1332}
1333
b3b94faa
DT
1334/**
1335 * gfs2_glock_cb - Callback used by locking module
1c089c32 1336 * @sdp: Pointer to the superblock
b3b94faa
DT
1337 * @type: Type of callback
1338 * @data: Type dependent data pointer
1339 *
1340 * Called by the locking module when it wants to tell us something.
1341 * Either we need to drop a lock, one of our ASYNC requests completed, or
1342 * a journal from another client needs to be recovered.
1343 */
1344
9b47c11d 1345void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
b3b94faa 1346{
9b47c11d 1347 struct gfs2_sbd *sdp = cb_data;
b3b94faa 1348
b3b94faa
DT
1349 switch (type) {
1350 case LM_CB_NEED_E:
e7f5c01c 1351 blocking_cb(sdp, data, LM_ST_UNLOCKED);
b3b94faa
DT
1352 return;
1353
1354 case LM_CB_NEED_D:
e7f5c01c 1355 blocking_cb(sdp, data, LM_ST_DEFERRED);
b3b94faa
DT
1356 return;
1357
1358 case LM_CB_NEED_S:
e7f5c01c 1359 blocking_cb(sdp, data, LM_ST_SHARED);
b3b94faa
DT
1360 return;
1361
1362 case LM_CB_ASYNC: {
e7f5c01c 1363 struct lm_async_cb *async = data;
b3b94faa
DT
1364 struct gfs2_glock *gl;
1365
61be084e 1366 down_read(&gfs2_umount_flush_sem);
b3b94faa
DT
1367 gl = gfs2_glock_find(sdp, &async->lc_name);
1368 if (gfs2_assert_warn(sdp, gl))
1369 return;
6802e340
SW
1370 gl->gl_reply = async->lc_ret;
1371 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
c4f68a13
BM
1372 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1373 gfs2_glock_put(gl);
61be084e 1374 up_read(&gfs2_umount_flush_sem);
b3b94faa
DT
1375 return;
1376 }
1377
1378 case LM_CB_NEED_RECOVERY:
1379 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1380 if (sdp->sd_recoverd_process)
1381 wake_up_process(sdp->sd_recoverd_process);
1382 return;
1383
b3b94faa
DT
1384 default:
1385 gfs2_assert_warn(sdp, 0);
1386 return;
1387 }
1388}
1389
b3b94faa
DT
1390/**
1391 * demote_ok - Check to see if it's ok to unlock a glock
1392 * @gl: the glock
1393 *
1394 * Returns: 1 if it's ok
1395 */
1396
97cc1025 1397static int demote_ok(const struct gfs2_glock *gl)
b3b94faa 1398{
8fb4b536 1399 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa 1400
97cc1025
SW
1401 if (gl->gl_state == LM_ST_UNLOCKED)
1402 return 0;
1403 if (!list_empty(&gl->gl_holders))
1404 return 0;
1405 if (glops->go_demote_ok)
1406 return glops->go_demote_ok(gl);
1407 return 1;
b3b94faa
DT
1408}
1409
b3b94faa 1410
97cc1025 1411static int gfs2_shrink_glock_memory(int nr, gfp_t gfp_mask)
b3b94faa
DT
1412{
1413 struct gfs2_glock *gl;
97cc1025
SW
1414 int may_demote;
1415 int nr_skipped = 0;
1416 int got_ref = 0;
1417 LIST_HEAD(skipped);
b3b94faa 1418
97cc1025
SW
1419 if (nr == 0)
1420 goto out;
b3b94faa 1421
97cc1025
SW
1422 if (!(gfp_mask & __GFP_FS))
1423 return -1;
b3b94faa 1424
97cc1025
SW
1425 spin_lock(&lru_lock);
1426 while(nr && !list_empty(&lru_list)) {
1427 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1428 list_del_init(&gl->gl_lru);
1429 atomic_dec(&lru_count);
1430
1431 /* Test for being demotable */
1432 if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1433 gfs2_glock_hold(gl);
1434 got_ref = 1;
1435 spin_unlock(&lru_lock);
1436 spin_lock(&gl->gl_spin);
1437 may_demote = demote_ok(gl);
1438 spin_unlock(&gl->gl_spin);
1439 clear_bit(GLF_LOCK, &gl->gl_flags);
1440 if (may_demote) {
1441 handle_callback(gl, LM_ST_UNLOCKED, 0);
1442 nr--;
1443 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1444 gfs2_glock_put(gl);
1445 }
1446 spin_lock(&lru_lock);
1447 if (may_demote)
1448 continue;
1449 }
1450 if (list_empty(&gl->gl_lru) &&
1451 (atomic_read(&gl->gl_ref) <= (2 + got_ref))) {
1452 nr_skipped++;
1453 list_add(&gl->gl_lru, &skipped);
1454 }
1455 if (got_ref) {
1456 spin_unlock(&lru_lock);
1457 gfs2_glock_put(gl);
1458 spin_lock(&lru_lock);
1459 got_ref = 0;
1460 }
b3b94faa 1461 }
97cc1025
SW
1462 list_splice(&skipped, &lru_list);
1463 atomic_add(nr_skipped, &lru_count);
1464 spin_unlock(&lru_lock);
1465out:
1466 return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
b3b94faa
DT
1467}
1468
97cc1025
SW
1469static struct shrinker glock_shrinker = {
1470 .shrink = gfs2_shrink_glock_memory,
1471 .seeks = DEFAULT_SEEKS,
1472};
1473
b3b94faa
DT
1474/**
1475 * examine_bucket - Call a function for glock in a hash bucket
1476 * @examiner: the function
1477 * @sdp: the filesystem
1478 * @bucket: the bucket
1479 *
1480 * Returns: 1 if the bucket has entries
1481 */
1482
1483static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
37b2fa6a 1484 unsigned int hash)
b3b94faa 1485{
24264434
SW
1486 struct gfs2_glock *gl, *prev = NULL;
1487 int has_entries = 0;
b6397893 1488 struct hlist_head *head = &gl_hash_table[hash].hb_list;
b3b94faa 1489
24264434 1490 read_lock(gl_lock_addr(hash));
b6397893
SW
1491 /* Can't use hlist_for_each_entry - don't want prefetch here */
1492 if (hlist_empty(head))
24264434 1493 goto out;
b6397893
SW
1494 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1495 while(1) {
8fbbfd21 1496 if (!sdp || gl->gl_sbd == sdp) {
b3b94faa 1497 gfs2_glock_hold(gl);
24264434
SW
1498 read_unlock(gl_lock_addr(hash));
1499 if (prev)
1500 gfs2_glock_put(prev);
1501 prev = gl;
1502 examiner(gl);
a8336344 1503 has_entries = 1;
24264434 1504 read_lock(gl_lock_addr(hash));
b3b94faa 1505 }
b6397893
SW
1506 if (gl->gl_list.next == NULL)
1507 break;
24264434 1508 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
b3b94faa 1509 }
24264434
SW
1510out:
1511 read_unlock(gl_lock_addr(hash));
1512 if (prev)
1513 gfs2_glock_put(prev);
8fbbfd21 1514 cond_resched();
24264434 1515 return has_entries;
b3b94faa
DT
1516}
1517
b3b94faa
DT
1518/**
1519 * clear_glock - look at a glock and see if we can free it from glock cache
1520 * @gl: the glock to look at
1521 *
1522 */
1523
1524static void clear_glock(struct gfs2_glock *gl)
1525{
97cc1025
SW
1526 spin_lock(&lru_lock);
1527 if (!list_empty(&gl->gl_lru)) {
1528 list_del_init(&gl->gl_lru);
1529 atomic_dec(&lru_count);
b3b94faa 1530 }
97cc1025 1531 spin_unlock(&lru_lock);
b3b94faa 1532
6802e340
SW
1533 spin_lock(&gl->gl_spin);
1534 if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED)
97cc1025 1535 handle_callback(gl, LM_ST_UNLOCKED, 0);
6802e340
SW
1536 spin_unlock(&gl->gl_spin);
1537 gfs2_glock_hold(gl);
1538 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1539 gfs2_glock_put(gl);
b3b94faa
DT
1540}
1541
1542/**
1543 * gfs2_gl_hash_clear - Empty out the glock hash table
1544 * @sdp: the filesystem
1545 * @wait: wait until it's all gone
1546 *
1bdad606 1547 * Called when unmounting the filesystem.
b3b94faa
DT
1548 */
1549
fefc03bf 1550void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
b3b94faa
DT
1551{
1552 unsigned long t;
1553 unsigned int x;
1554 int cont;
1555
1556 t = jiffies;
1557
1558 for (;;) {
1559 cont = 0;
24264434 1560 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
907b9bce 1561 if (examine_bucket(clear_glock, sdp, x))
b3b94faa 1562 cont = 1;
24264434 1563 }
b3b94faa 1564
1bdad606 1565 if (!cont)
b3b94faa
DT
1566 break;
1567
1568 if (time_after_eq(jiffies,
1569 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1570 fs_warn(sdp, "Unmount seems to be stalled. "
1571 "Dumping lock state...\n");
1572 gfs2_dump_lockstate(sdp);
1573 t = jiffies;
1574 }
1575
61be084e 1576 down_write(&gfs2_umount_flush_sem);
b3b94faa 1577 invalidate_inodes(sdp->sd_vfs);
61be084e 1578 up_write(&gfs2_umount_flush_sem);
fd88de56 1579 msleep(10);
b3b94faa
DT
1580 }
1581}
1582
813e0c46
SW
1583void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1584{
1585 struct gfs2_glock *gl = ip->i_gl;
1586 int ret;
1587
1588 ret = gfs2_truncatei_resume(ip);
1589 gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
1590
1591 spin_lock(&gl->gl_spin);
1592 clear_bit(GLF_LOCK, &gl->gl_flags);
1593 run_queue(gl, 1);
1594 spin_unlock(&gl->gl_spin);
1595}
1596
6802e340 1597static const char *state2str(unsigned state)
04b933f2 1598{
6802e340
SW
1599 switch(state) {
1600 case LM_ST_UNLOCKED:
1601 return "UN";
1602 case LM_ST_SHARED:
1603 return "SH";
1604 case LM_ST_DEFERRED:
1605 return "DF";
1606 case LM_ST_EXCLUSIVE:
1607 return "EX";
1608 }
1609 return "??";
1610}
1611
1612static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1613{
1614 char *p = buf;
1615 if (flags & LM_FLAG_TRY)
1616 *p++ = 't';
1617 if (flags & LM_FLAG_TRY_1CB)
1618 *p++ = 'T';
1619 if (flags & LM_FLAG_NOEXP)
1620 *p++ = 'e';
1621 if (flags & LM_FLAG_ANY)
1622 *p++ = 'a';
1623 if (flags & LM_FLAG_PRIORITY)
1624 *p++ = 'p';
1625 if (flags & GL_ASYNC)
1626 *p++ = 'a';
1627 if (flags & GL_EXACT)
1628 *p++ = 'E';
6802e340
SW
1629 if (flags & GL_NOCACHE)
1630 *p++ = 'c';
1631 if (test_bit(HIF_HOLDER, &iflags))
1632 *p++ = 'H';
1633 if (test_bit(HIF_WAIT, &iflags))
1634 *p++ = 'W';
1635 if (test_bit(HIF_FIRST, &iflags))
1636 *p++ = 'F';
1637 *p = 0;
1638 return buf;
04b933f2
RP
1639}
1640
b3b94faa
DT
1641/**
1642 * dump_holder - print information about a glock holder
6802e340 1643 * @seq: the seq_file struct
b3b94faa
DT
1644 * @gh: the glock holder
1645 *
1646 * Returns: 0 on success, -ENOBUFS when we run out of space
1647 */
1648
6802e340 1649static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
b3b94faa 1650{
6802e340
SW
1651 struct task_struct *gh_owner = NULL;
1652 char buffer[KSYM_SYMBOL_LEN];
1653 char flags_buf[32];
b3b94faa 1654
6802e340
SW
1655 sprint_symbol(buffer, gh->gh_ip);
1656 if (gh->gh_owner_pid)
b1e058da 1657 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
6802e340
SW
1658 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n",
1659 state2str(gh->gh_state),
1660 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1661 gh->gh_error,
1662 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1663 gh_owner ? gh_owner->comm : "(ended)", buffer);
7c52b166 1664 return 0;
b3b94faa
DT
1665}
1666
6802e340
SW
1667static const char *gflags2str(char *buf, const unsigned long *gflags)
1668{
1669 char *p = buf;
1670 if (test_bit(GLF_LOCK, gflags))
1671 *p++ = 'l';
6802e340
SW
1672 if (test_bit(GLF_DEMOTE, gflags))
1673 *p++ = 'D';
1674 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1675 *p++ = 'd';
1676 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1677 *p++ = 'p';
1678 if (test_bit(GLF_DIRTY, gflags))
1679 *p++ = 'y';
1680 if (test_bit(GLF_LFLUSH, gflags))
1681 *p++ = 'f';
1682 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1683 *p++ = 'i';
1684 if (test_bit(GLF_REPLY_PENDING, gflags))
1685 *p++ = 'r';
1686 *p = 0;
1687 return buf;
b3b94faa
DT
1688}
1689
1690/**
6802e340
SW
1691 * __dump_glock - print information about a glock
1692 * @seq: The seq_file struct
b3b94faa 1693 * @gl: the glock
6802e340
SW
1694 *
1695 * The file format is as follows:
1696 * One line per object, capital letters are used to indicate objects
1697 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1698 * other objects are indented by a single space and follow the glock to
1699 * which they are related. Fields are indicated by lower case letters
1700 * followed by a colon and the field value, except for strings which are in
1701 * [] so that its possible to see if they are composed of spaces for
1702 * example. The field's are n = number (id of the object), f = flags,
1703 * t = type, s = state, r = refcount, e = error, p = pid.
b3b94faa
DT
1704 *
1705 * Returns: 0 on success, -ENOBUFS when we run out of space
1706 */
1707
6802e340 1708static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
b3b94faa 1709{
6802e340
SW
1710 const struct gfs2_glock_operations *glops = gl->gl_ops;
1711 unsigned long long dtime;
1712 const struct gfs2_holder *gh;
1713 char gflags_buf[32];
1714 int error = 0;
b3b94faa 1715
6802e340
SW
1716 dtime = jiffies - gl->gl_demote_time;
1717 dtime *= 1000000/HZ; /* demote time in uSec */
1718 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1719 dtime = 0;
1720 gfs2_print_dbg(seq, "G: s:%s n:%u/%llu f:%s t:%s d:%s/%llu l:%d a:%d r:%d\n",
1721 state2str(gl->gl_state),
1722 gl->gl_name.ln_type,
1723 (unsigned long long)gl->gl_name.ln_number,
1724 gflags2str(gflags_buf, &gl->gl_flags),
1725 state2str(gl->gl_target),
1726 state2str(gl->gl_demote_state), dtime,
1727 atomic_read(&gl->gl_lvb_count),
1728 atomic_read(&gl->gl_ail_count),
1729 atomic_read(&gl->gl_ref));
b3b94faa 1730
b3b94faa 1731 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
6802e340 1732 error = dump_holder(seq, gh);
b3b94faa
DT
1733 if (error)
1734 goto out;
1735 }
6802e340
SW
1736 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1737 error = glops->go_dump(seq, gl);
a91ea69f 1738out:
b3b94faa
DT
1739 return error;
1740}
1741
6802e340
SW
1742static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1743{
1744 int ret;
1745 spin_lock(&gl->gl_spin);
1746 ret = __dump_glock(seq, gl);
1747 spin_unlock(&gl->gl_spin);
1748 return ret;
1749}
1750
b3b94faa
DT
1751/**
1752 * gfs2_dump_lockstate - print out the current lockstate
1753 * @sdp: the filesystem
1754 * @ub: the buffer to copy the information into
1755 *
1756 * If @ub is NULL, dump the lockstate to the console.
1757 *
1758 */
1759
08bc2dbc 1760static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
b3b94faa 1761{
b3b94faa 1762 struct gfs2_glock *gl;
b6397893 1763 struct hlist_node *h;
b3b94faa
DT
1764 unsigned int x;
1765 int error = 0;
1766
1767 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
b3b94faa 1768
087efdd3 1769 read_lock(gl_lock_addr(x));
b3b94faa 1770
b6397893 1771 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
85d1da67
SW
1772 if (gl->gl_sbd != sdp)
1773 continue;
b3b94faa 1774
7c52b166 1775 error = dump_glock(NULL, gl);
b3b94faa
DT
1776 if (error)
1777 break;
1778 }
1779
087efdd3 1780 read_unlock(gl_lock_addr(x));
b3b94faa
DT
1781
1782 if (error)
1783 break;
1784 }
1785
1786
1787 return error;
1788}
1789
8fbbfd21 1790
85d1da67
SW
1791int __init gfs2_glock_init(void)
1792{
1793 unsigned i;
1794 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
b6397893 1795 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
85d1da67 1796 }
087efdd3
SW
1797#ifdef GL_HASH_LOCK_SZ
1798 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
1799 rwlock_init(&gl_hash_locks[i]);
1800 }
1801#endif
8fbbfd21 1802
c4f68a13 1803 glock_workqueue = create_workqueue("glock_workqueue");
97cc1025 1804 if (IS_ERR(glock_workqueue))
c4f68a13 1805 return PTR_ERR(glock_workqueue);
97cc1025
SW
1806
1807 register_shrinker(&glock_shrinker);
c4f68a13 1808
85d1da67
SW
1809 return 0;
1810}
1811
8fbbfd21
SW
1812void gfs2_glock_exit(void)
1813{
97cc1025 1814 unregister_shrinker(&glock_shrinker);
c4f68a13 1815 destroy_workqueue(glock_workqueue);
8fbbfd21
SW
1816}
1817
6802e340 1818static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
7c52b166 1819{
7b08fc62
SW
1820 struct gfs2_glock *gl;
1821
a947e033 1822restart:
7a0079d9 1823 read_lock(gl_lock_addr(gi->hash));
7b08fc62
SW
1824 gl = gi->gl;
1825 if (gl) {
a947e033
AD
1826 gi->gl = hlist_entry(gl->gl_list.next,
1827 struct gfs2_glock, gl_list);
c1e817d0
SW
1828 } else {
1829 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1830 struct gfs2_glock, gl_list);
7c52b166 1831 }
c1e817d0
SW
1832 if (gi->gl)
1833 gfs2_glock_hold(gi->gl);
7a0079d9 1834 read_unlock(gl_lock_addr(gi->hash));
7b08fc62
SW
1835 if (gl)
1836 gfs2_glock_put(gl);
6802e340 1837 while (gi->gl == NULL) {
c1e817d0 1838 gi->hash++;
7b08fc62
SW
1839 if (gi->hash >= GFS2_GL_HASH_SIZE)
1840 return 1;
1841 read_lock(gl_lock_addr(gi->hash));
1842 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1843 struct gfs2_glock, gl_list);
1844 if (gi->gl)
1845 gfs2_glock_hold(gi->gl);
1846 read_unlock(gl_lock_addr(gi->hash));
1847 }
a947e033
AD
1848
1849 if (gi->sdp != gi->gl->gl_sbd)
1850 goto restart;
1851
7c52b166
RP
1852 return 0;
1853}
1854
6802e340 1855static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi)
7c52b166 1856{
7b08fc62
SW
1857 if (gi->gl)
1858 gfs2_glock_put(gi->gl);
a947e033 1859 gi->gl = NULL;
7c52b166
RP
1860}
1861
6802e340 1862static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
7c52b166 1863{
6802e340 1864 struct gfs2_glock_iter *gi = seq->private;
7c52b166
RP
1865 loff_t n = *pos;
1866
6802e340 1867 gi->hash = 0;
7c52b166 1868
6802e340 1869 do {
7c52b166
RP
1870 if (gfs2_glock_iter_next(gi)) {
1871 gfs2_glock_iter_free(gi);
1872 return NULL;
1873 }
6802e340 1874 } while (n--);
7c52b166 1875
6802e340 1876 return gi->gl;
7c52b166
RP
1877}
1878
6802e340 1879static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
7c52b166
RP
1880 loff_t *pos)
1881{
6802e340 1882 struct gfs2_glock_iter *gi = seq->private;
7c52b166
RP
1883
1884 (*pos)++;
1885
1886 if (gfs2_glock_iter_next(gi)) {
1887 gfs2_glock_iter_free(gi);
1888 return NULL;
1889 }
1890
6802e340 1891 return gi->gl;
7c52b166
RP
1892}
1893
6802e340 1894static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
7c52b166 1895{
6802e340
SW
1896 struct gfs2_glock_iter *gi = seq->private;
1897 gfs2_glock_iter_free(gi);
7c52b166
RP
1898}
1899
6802e340 1900static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
7c52b166 1901{
6802e340 1902 return dump_glock(seq, iter_ptr);
7c52b166
RP
1903}
1904
4ef29002 1905static const struct seq_operations gfs2_glock_seq_ops = {
7c52b166
RP
1906 .start = gfs2_glock_seq_start,
1907 .next = gfs2_glock_seq_next,
1908 .stop = gfs2_glock_seq_stop,
1909 .show = gfs2_glock_seq_show,
1910};
1911
1912static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1913{
6802e340
SW
1914 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1915 sizeof(struct gfs2_glock_iter));
1916 if (ret == 0) {
1917 struct seq_file *seq = file->private_data;
1918 struct gfs2_glock_iter *gi = seq->private;
1919 gi->sdp = inode->i_private;
1920 }
1921 return ret;
7c52b166
RP
1922}
1923
1924static const struct file_operations gfs2_debug_fops = {
1925 .owner = THIS_MODULE,
1926 .open = gfs2_debugfs_open,
1927 .read = seq_read,
1928 .llseek = seq_lseek,
6802e340 1929 .release = seq_release_private,
7c52b166
RP
1930};
1931
1932int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1933{
5f882096
RP
1934 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1935 if (!sdp->debugfs_dir)
1936 return -ENOMEM;
1937 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1938 S_IFREG | S_IRUGO,
1939 sdp->debugfs_dir, sdp,
1940 &gfs2_debug_fops);
1941 if (!sdp->debugfs_dentry_glocks)
7c52b166
RP
1942 return -ENOMEM;
1943
1944 return 0;
1945}
1946
1947void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1948{
5f882096
RP
1949 if (sdp && sdp->debugfs_dir) {
1950 if (sdp->debugfs_dentry_glocks) {
1951 debugfs_remove(sdp->debugfs_dentry_glocks);
1952 sdp->debugfs_dentry_glocks = NULL;
1953 }
1954 debugfs_remove(sdp->debugfs_dir);
1955 sdp->debugfs_dir = NULL;
1956 }
7c52b166
RP
1957}
1958
1959int gfs2_register_debugfs(void)
1960{
1961 gfs2_root = debugfs_create_dir("gfs2", NULL);
1962 return gfs2_root ? 0 : -ENOMEM;
1963}
1964
1965void gfs2_unregister_debugfs(void)
1966{
1967 debugfs_remove(gfs2_root);
5f882096 1968 gfs2_root = NULL;
7c52b166 1969}