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