2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kallsyms.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/list.h>
21 #include <linux/lm_interface.h>
22 #include <linux/wait.h>
23 #include <linux/module.h>
24 #include <linux/rwsem.h>
25 #include <asm/uaccess.h>
26 #include <linux/seq_file.h>
27 #include <linux/debugfs.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30 #include <linux/workqueue.h>
31 #include <linux/jiffies.h>
44 struct gfs2_gl_hash_bucket {
45 struct hlist_head hb_list;
48 struct 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 */
55 typedef void (*glock_examiner) (struct gfs2_glock * gl);
57 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
58 static 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)
60 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
62 static DECLARE_RWSEM(gfs2_umount_flush_sem);
63 static struct dentry *gfs2_root;
64 static struct task_struct *scand_process;
65 static unsigned int scand_secs = 5;
66 static struct workqueue_struct *glock_workqueue;
68 #define GFS2_GL_HASH_SHIFT 15
69 #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
70 #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
72 static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
73 static struct dentry *gfs2_root;
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.
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.
85 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
86 defined(CONFIG_PROVE_LOCKING)
89 # define GL_HASH_LOCK_SZ 256
92 # define GL_HASH_LOCK_SZ 4096
94 # define GL_HASH_LOCK_SZ 2048
96 # define GL_HASH_LOCK_SZ 1024
98 # define GL_HASH_LOCK_SZ 512
100 # define GL_HASH_LOCK_SZ 256
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
110 static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
112 static inline rwlock_t *gl_lock_addr(unsigned int x)
114 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
116 #else /* not SMP, so no spinlocks required */
117 static inline rwlock_t *gl_lock_addr(unsigned int x)
124 * gl_hash() - Turn glock number into hash bucket number
125 * @lock: The glock number
127 * Returns: The number of the corresponding hash bucket
130 static unsigned int gl_hash(const struct gfs2_sbd *sdp,
131 const struct lm_lockname *name)
135 h = jhash(&name->ln_number, sizeof(u64), 0);
136 h = jhash(&name->ln_type, sizeof(unsigned int), h);
137 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
138 h &= GFS2_GL_HASH_MASK;
144 * glock_free() - Perform a few checks and then release struct gfs2_glock
145 * @gl: The glock to release
147 * Also calls lock module to release its internal structure for this glock.
151 static void glock_free(struct gfs2_glock *gl)
153 struct gfs2_sbd *sdp = gl->gl_sbd;
154 struct inode *aspace = gl->gl_aspace;
156 if (sdp->sd_lockstruct.ls_ops->lm_put_lock)
157 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl->gl_lock);
160 gfs2_aspace_put(aspace);
162 kmem_cache_free(gfs2_glock_cachep, gl);
166 * gfs2_glock_hold() - increment reference count on glock
167 * @gl: The glock to hold
171 static void gfs2_glock_hold(struct gfs2_glock *gl)
173 atomic_inc(&gl->gl_ref);
177 * gfs2_glock_put() - Decrement reference count on glock
178 * @gl: The glock to put
182 int gfs2_glock_put(struct gfs2_glock *gl)
186 write_lock(gl_lock_addr(gl->gl_hash));
187 if (atomic_dec_and_test(&gl->gl_ref)) {
188 hlist_del(&gl->gl_list);
189 write_unlock(gl_lock_addr(gl->gl_hash));
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));
197 write_unlock(gl_lock_addr(gl->gl_hash));
203 * search_bucket() - Find struct gfs2_glock by lock number
204 * @bucket: the bucket to search
205 * @name: The lock name
207 * Returns: NULL, or the struct gfs2_glock with the requested number
210 static struct gfs2_glock *search_bucket(unsigned int hash,
211 const struct gfs2_sbd *sdp,
212 const struct lm_lockname *name)
214 struct gfs2_glock *gl;
215 struct hlist_node *h;
217 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
218 if (!lm_name_equal(&gl->gl_name, name))
220 if (gl->gl_sbd != sdp)
223 atomic_inc(&gl->gl_ref);
232 * gfs2_glock_find() - Find glock by lock number
233 * @sdp: The GFS2 superblock
234 * @name: The lock name
236 * Returns: NULL, or the struct gfs2_glock with the requested number
239 static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
240 const struct lm_lockname *name)
242 unsigned int hash = gl_hash(sdp, name);
243 struct gfs2_glock *gl;
245 read_lock(gl_lock_addr(hash));
246 gl = search_bucket(hash, sdp, name);
247 read_unlock(gl_lock_addr(hash));
253 * may_grant - check if its ok to grant a new lock
255 * @gh: The lock request which we wish to grant
257 * Returns: true if its ok to grant the lock
260 static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
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)
266 if (gl->gl_state == gh->gh_state)
268 if (gh->gh_flags & GL_EXACT)
270 if (gl->gl_state == LM_ST_EXCLUSIVE) {
271 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
273 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
276 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
281 static void gfs2_holder_wake(struct gfs2_holder *gh)
283 clear_bit(HIF_WAIT, &gh->gh_iflags);
284 smp_mb__after_clear_bit();
285 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
289 * do_promote - promote as many requests as possible on the current queue
292 * Returns: true if there is a blocked holder at the head of the list
295 static int do_promote(struct gfs2_glock *gl)
297 const struct gfs2_glock_operations *glops = gl->gl_ops;
298 struct gfs2_holder *gh, *tmp;
302 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
303 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
305 if (may_grant(gl, gh)) {
306 if (gh->gh_list.prev == &gl->gl_holders &&
308 spin_unlock(&gl->gl_spin);
309 /* FIXME: eliminate this eventually */
310 ret = glops->go_lock(gh);
311 spin_lock(&gl->gl_spin);
314 list_del_init(&gh->gh_list);
315 gfs2_holder_wake(gh);
318 set_bit(HIF_HOLDER, &gh->gh_iflags);
319 gfs2_holder_wake(gh);
322 set_bit(HIF_HOLDER, &gh->gh_iflags);
323 gfs2_holder_wake(gh);
326 if (gh->gh_list.prev == &gl->gl_holders)
334 * do_error - Something unexpected has happened during a lock request
338 static inline void do_error(struct gfs2_glock *gl, const int ret)
340 struct gfs2_holder *gh, *tmp;
342 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
343 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
345 if (ret & LM_OUT_ERROR)
347 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
348 gh->gh_error = GLR_TRYFAILED;
351 list_del_init(&gh->gh_list);
352 gfs2_holder_wake(gh);
357 * find_first_waiter - find the first gh that's waiting for the glock
361 static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
363 struct gfs2_holder *gh;
365 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
366 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
373 * state_change - record that the glock is now in a different state
375 * @new_state the new state
379 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
383 held1 = (gl->gl_state != LM_ST_UNLOCKED);
384 held2 = (new_state != LM_ST_UNLOCKED);
386 if (held1 != held2) {
393 gl->gl_state = new_state;
394 gl->gl_tchange = jiffies;
397 static void gfs2_demote_wake(struct gfs2_glock *gl)
399 gl->gl_demote_state = LM_ST_EXCLUSIVE;
400 clear_bit(GLF_DEMOTE, &gl->gl_flags);
401 smp_mb__after_clear_bit();
402 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
406 * finish_xmote - The DLM has replied to one of our lock requests
408 * @ret: The status from the DLM
412 static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
414 const struct gfs2_glock_operations *glops = gl->gl_ops;
415 struct gfs2_holder *gh;
416 unsigned state = ret & LM_OUT_ST_MASK;
418 spin_lock(&gl->gl_spin);
419 state_change(gl, state);
420 gh = find_first_waiter(gl);
422 /* Demote to UN request arrived during demote to SH or DF */
423 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
424 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
425 gl->gl_target = LM_ST_UNLOCKED;
427 /* Check for state != intended state */
428 if (unlikely(state != gl->gl_target)) {
429 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
430 /* move to back of queue and try next entry */
431 if (ret & LM_OUT_CANCELED) {
432 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
433 list_move_tail(&gh->gh_list, &gl->gl_holders);
434 gh = find_first_waiter(gl);
435 gl->gl_target = gh->gh_state;
438 /* Some error or failed "try lock" - report it */
439 if ((ret & LM_OUT_ERROR) ||
440 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
441 gl->gl_target = gl->gl_state;
447 /* Unlocked due to conversion deadlock, try again */
450 do_xmote(gl, gh, gl->gl_target);
452 /* Conversion fails, unlock and try again */
455 do_xmote(gl, gh, LM_ST_UNLOCKED);
457 default: /* Everything else */
458 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
461 spin_unlock(&gl->gl_spin);
466 /* Fast path - we got what we asked for */
467 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
468 gfs2_demote_wake(gl);
469 if (state != LM_ST_UNLOCKED) {
470 if (glops->go_xmote_bh) {
472 spin_unlock(&gl->gl_spin);
473 rv = glops->go_xmote_bh(gl, gh);
476 spin_lock(&gl->gl_spin);
485 clear_bit(GLF_LOCK, &gl->gl_flags);
486 spin_unlock(&gl->gl_spin);
490 static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
491 unsigned int cur_state, unsigned int req_state,
494 int ret = LM_OUT_ERROR;
496 if (!sdp->sd_lockstruct.ls_ops->lm_lock)
497 return req_state == LM_ST_UNLOCKED ? 0 : req_state;
499 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
500 ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock, cur_state,
506 * do_xmote - Calls the DLM to change the state of a lock
507 * @gl: The lock state
508 * @gh: The holder (only for promotes)
509 * @target: The target lock state
513 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
515 const struct gfs2_glock_operations *glops = gl->gl_ops;
516 struct gfs2_sbd *sdp = gl->gl_sbd;
517 unsigned int lck_flags = gh ? gh->gh_flags : 0;
520 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
522 BUG_ON(gl->gl_state == target);
523 BUG_ON(gl->gl_state == gl->gl_target);
524 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
526 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
527 do_error(gl, 0); /* Fail queued try locks */
529 spin_unlock(&gl->gl_spin);
530 if (glops->go_xmote_th)
531 glops->go_xmote_th(gl);
532 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
533 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
534 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
537 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
538 gl->gl_state == LM_ST_DEFERRED) &&
539 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
540 lck_flags |= LM_FLAG_TRY_1CB;
541 ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, target, lck_flags);
543 if (!(ret & LM_OUT_ASYNC)) {
544 finish_xmote(gl, ret);
546 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
549 GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC);
551 spin_lock(&gl->gl_spin);
555 * find_first_holder - find the first "holder" gh
559 static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
561 struct gfs2_holder *gh;
563 if (!list_empty(&gl->gl_holders)) {
564 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
565 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
572 * run_queue - do all outstanding tasks related to a glock
573 * @gl: The glock in question
574 * @nonblock: True if we must not block in run_queue
578 static void run_queue(struct gfs2_glock *gl, const int nonblock)
580 struct gfs2_holder *gh = NULL;
582 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
585 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
587 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
588 gl->gl_demote_state != gl->gl_state) {
589 if (find_first_holder(gl))
593 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
594 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
595 gl->gl_target = gl->gl_demote_state;
597 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
598 gfs2_demote_wake(gl);
599 if (do_promote(gl) == 0)
601 gh = find_first_waiter(gl);
602 gl->gl_target = gh->gh_state;
603 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
604 do_error(gl, 0); /* Fail queued try locks */
606 do_xmote(gl, gh, gl->gl_target);
611 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
614 clear_bit(GLF_LOCK, &gl->gl_flags);
617 static void glock_work_func(struct work_struct *work)
619 unsigned long delay = 0;
620 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
622 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags))
623 finish_xmote(gl, gl->gl_reply);
624 spin_lock(&gl->gl_spin);
625 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
626 gl->gl_state != LM_ST_UNLOCKED &&
627 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
628 unsigned long holdtime, now = jiffies;
629 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
630 if (time_before(now, holdtime))
631 delay = holdtime - now;
632 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
635 spin_unlock(&gl->gl_spin);
637 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
641 static int gfs2_lm_get_lock(struct gfs2_sbd *sdp, struct lm_lockname *name,
645 if (!sdp->sd_lockstruct.ls_ops->lm_get_lock)
647 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
648 error = sdp->sd_lockstruct.ls_ops->lm_get_lock(
649 sdp->sd_lockstruct.ls_lockspace, name, lockp);
654 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
655 * @sdp: The GFS2 superblock
656 * @number: the lock number
657 * @glops: The glock_operations to use
658 * @create: If 0, don't create the glock if it doesn't exist
659 * @glp: the glock is returned here
661 * This does not lock a glock, just finds/creates structures for one.
666 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
667 const struct gfs2_glock_operations *glops, int create,
668 struct gfs2_glock **glp)
670 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
671 struct gfs2_glock *gl, *tmp;
672 unsigned int hash = gl_hash(sdp, &name);
675 read_lock(gl_lock_addr(hash));
676 gl = search_bucket(hash, sdp, &name);
677 read_unlock(gl_lock_addr(hash));
684 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
690 atomic_set(&gl->gl_ref, 1);
691 gl->gl_state = LM_ST_UNLOCKED;
692 gl->gl_target = LM_ST_UNLOCKED;
693 gl->gl_demote_state = LM_ST_EXCLUSIVE;
696 gl->gl_stamp = jiffies;
697 gl->gl_tchange = jiffies;
698 gl->gl_object = NULL;
700 gl->gl_aspace = NULL;
701 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
703 /* If this glock protects actual on-disk data or metadata blocks,
704 create a VFS inode to manage the pages/buffers holding them. */
705 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
706 gl->gl_aspace = gfs2_aspace_get(sdp);
707 if (!gl->gl_aspace) {
713 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
717 write_lock(gl_lock_addr(hash));
718 tmp = search_bucket(hash, sdp, &name);
720 write_unlock(gl_lock_addr(hash));
724 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
725 write_unlock(gl_lock_addr(hash));
734 gfs2_aspace_put(gl->gl_aspace);
736 kmem_cache_free(gfs2_glock_cachep, gl);
741 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
743 * @state: the state we're requesting
744 * @flags: the modifier flags
745 * @gh: the holder structure
749 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
750 struct gfs2_holder *gh)
752 INIT_LIST_HEAD(&gh->gh_list);
754 gh->gh_ip = (unsigned long)__builtin_return_address(0);
755 gh->gh_owner_pid = get_pid(task_pid(current));
756 gh->gh_state = state;
757 gh->gh_flags = flags;
764 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
765 * @state: the state we're requesting
766 * @flags: the modifier flags
767 * @gh: the holder structure
769 * Don't mess with the glock.
773 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
775 gh->gh_state = state;
776 gh->gh_flags = flags;
778 gh->gh_ip = (unsigned long)__builtin_return_address(0);
782 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
783 * @gh: the holder structure
787 void gfs2_holder_uninit(struct gfs2_holder *gh)
789 put_pid(gh->gh_owner_pid);
790 gfs2_glock_put(gh->gh_gl);
795 static int just_schedule(void *word)
801 static void wait_on_holder(struct gfs2_holder *gh)
804 wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
807 static void wait_on_demote(struct gfs2_glock *gl)
810 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
814 * handle_callback - process a demote request
816 * @state: the state the caller wants us to change to
818 * There are only two requests that we are going to see in actual
819 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
822 static void handle_callback(struct gfs2_glock *gl, unsigned int state,
823 int remote, unsigned long delay)
825 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
827 set_bit(bit, &gl->gl_flags);
828 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
829 gl->gl_demote_state = state;
830 gl->gl_demote_time = jiffies;
831 if (remote && gl->gl_ops->go_type == LM_TYPE_IOPEN &&
833 gfs2_glock_schedule_for_reclaim(gl);
834 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
835 gl->gl_demote_state != state) {
836 gl->gl_demote_state = LM_ST_UNLOCKED;
841 * gfs2_glock_wait - wait on a glock acquisition
842 * @gh: the glock holder
844 * Returns: 0 on success
847 int gfs2_glock_wait(struct gfs2_holder *gh)
853 void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
859 struct gfs2_glock_iter *gi = seq->private;
860 vsprintf(gi->string, fmt, args);
861 seq_printf(seq, gi->string);
863 printk(KERN_ERR " ");
870 * add_to_queue - Add a holder to the wait queue (but look for recursion)
871 * @gh: the holder structure to add
873 * Eventually we should move the recursive locking trap to a
874 * debugging option or something like that. This is the fast
875 * path and needs to have the minimum number of distractions.
879 static inline void add_to_queue(struct gfs2_holder *gh)
881 struct gfs2_glock *gl = gh->gh_gl;
882 struct gfs2_sbd *sdp = gl->gl_sbd;
883 struct list_head *insert_pt = NULL;
884 struct gfs2_holder *gh2;
887 BUG_ON(gh->gh_owner_pid == NULL);
888 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
891 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
892 if (test_bit(GLF_LOCK, &gl->gl_flags))
894 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
898 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
899 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
900 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
903 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
904 !may_grant(gl, gh)) {
906 gh->gh_error = GLR_TRYFAILED;
907 gfs2_holder_wake(gh);
910 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
912 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
913 insert_pt = &gh2->gh_list;
915 if (likely(insert_pt == NULL)) {
916 list_add_tail(&gh->gh_list, &gl->gl_holders);
917 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
921 list_add_tail(&gh->gh_list, insert_pt);
923 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
924 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
925 spin_unlock(&gl->gl_spin);
926 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
927 sdp->sd_lockstruct.ls_ops->lm_cancel(gl->gl_lock);
928 spin_lock(&gl->gl_spin);
933 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
934 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
935 printk(KERN_ERR "lock type: %d req lock state : %d\n",
936 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
937 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
938 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
939 printk(KERN_ERR "lock type: %d req lock state : %d\n",
940 gh->gh_gl->gl_name.ln_type, gh->gh_state);
941 __dump_glock(NULL, gl);
946 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
947 * @gh: the holder structure
949 * if (gh->gh_flags & GL_ASYNC), this never returns an error
951 * Returns: 0, GLR_TRYFAILED, or errno on failure
954 int gfs2_glock_nq(struct gfs2_holder *gh)
956 struct gfs2_glock *gl = gh->gh_gl;
957 struct gfs2_sbd *sdp = gl->gl_sbd;
960 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
963 spin_lock(&gl->gl_spin);
966 spin_unlock(&gl->gl_spin);
968 if (!(gh->gh_flags & GL_ASYNC))
969 error = gfs2_glock_wait(gh);
975 * gfs2_glock_poll - poll to see if an async request has been completed
978 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
981 int gfs2_glock_poll(struct gfs2_holder *gh)
983 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
987 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
988 * @gh: the glock holder
992 void gfs2_glock_dq(struct gfs2_holder *gh)
994 struct gfs2_glock *gl = gh->gh_gl;
995 const struct gfs2_glock_operations *glops = gl->gl_ops;
999 spin_lock(&gl->gl_spin);
1000 if (gh->gh_flags & GL_NOCACHE)
1001 handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
1003 list_del_init(&gh->gh_list);
1004 if (find_first_holder(gl) == NULL) {
1005 if (glops->go_unlock) {
1006 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
1007 spin_unlock(&gl->gl_spin);
1008 glops->go_unlock(gh);
1009 spin_lock(&gl->gl_spin);
1010 clear_bit(GLF_LOCK, &gl->gl_flags);
1012 gl->gl_stamp = jiffies;
1013 if (list_empty(&gl->gl_holders) &&
1014 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1015 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1018 spin_unlock(&gl->gl_spin);
1019 if (likely(fast_path))
1022 gfs2_glock_hold(gl);
1023 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1024 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1025 delay = gl->gl_ops->go_min_hold_time;
1026 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1030 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1032 struct gfs2_glock *gl = gh->gh_gl;
1038 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1039 * @gh: the holder structure
1043 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1046 gfs2_holder_uninit(gh);
1050 * gfs2_glock_nq_num - acquire a glock based on lock number
1051 * @sdp: the filesystem
1052 * @number: the lock number
1053 * @glops: the glock operations for the type of glock
1054 * @state: the state to acquire the glock in
1055 * @flags: modifier flags for the aquisition
1056 * @gh: the struct gfs2_holder
1061 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1062 const struct gfs2_glock_operations *glops,
1063 unsigned int state, int flags, struct gfs2_holder *gh)
1065 struct gfs2_glock *gl;
1068 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1070 error = gfs2_glock_nq_init(gl, state, flags, gh);
1078 * glock_compare - Compare two struct gfs2_glock structures for sorting
1079 * @arg_a: the first structure
1080 * @arg_b: the second structure
1084 static int glock_compare(const void *arg_a, const void *arg_b)
1086 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1087 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1088 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1089 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1091 if (a->ln_number > b->ln_number)
1093 if (a->ln_number < b->ln_number)
1095 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1100 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1101 * @num_gh: the number of structures
1102 * @ghs: an array of struct gfs2_holder structures
1104 * Returns: 0 on success (all glocks acquired),
1105 * errno on failure (no glocks acquired)
1108 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1109 struct gfs2_holder **p)
1114 for (x = 0; x < num_gh; x++)
1117 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1119 for (x = 0; x < num_gh; x++) {
1120 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1122 error = gfs2_glock_nq(p[x]);
1125 gfs2_glock_dq(p[x]);
1134 * gfs2_glock_nq_m - acquire multiple glocks
1135 * @num_gh: the number of structures
1136 * @ghs: an array of struct gfs2_holder structures
1139 * Returns: 0 on success (all glocks acquired),
1140 * errno on failure (no glocks acquired)
1143 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1145 struct gfs2_holder *tmp[4];
1146 struct gfs2_holder **pph = tmp;
1153 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1154 return gfs2_glock_nq(ghs);
1158 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1163 error = nq_m_sync(num_gh, ghs, pph);
1172 * gfs2_glock_dq_m - release multiple glocks
1173 * @num_gh: the number of structures
1174 * @ghs: an array of struct gfs2_holder structures
1178 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1182 for (x = 0; x < num_gh; x++)
1183 gfs2_glock_dq(&ghs[x]);
1187 * gfs2_glock_dq_uninit_m - release multiple glocks
1188 * @num_gh: the number of structures
1189 * @ghs: an array of struct gfs2_holder structures
1193 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1197 for (x = 0; x < num_gh; x++)
1198 gfs2_glock_dq_uninit(&ghs[x]);
1201 static int gfs2_lm_hold_lvb(struct gfs2_sbd *sdp, void *lock, char **lvbp)
1204 if (!sdp->sd_lockstruct.ls_ops->lm_hold_lvb)
1206 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
1207 error = sdp->sd_lockstruct.ls_ops->lm_hold_lvb(lock, lvbp);
1212 * gfs2_lvb_hold - attach a LVB from a glock
1213 * @gl: The glock in question
1217 int gfs2_lvb_hold(struct gfs2_glock *gl)
1221 if (!atomic_read(&gl->gl_lvb_count)) {
1222 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1225 gfs2_glock_hold(gl);
1227 atomic_inc(&gl->gl_lvb_count);
1233 * gfs2_lvb_unhold - detach a LVB from a glock
1234 * @gl: The glock in question
1238 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1240 struct gfs2_sbd *sdp = gl->gl_sbd;
1242 gfs2_glock_hold(gl);
1243 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1244 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1245 if (sdp->sd_lockstruct.ls_ops->lm_unhold_lvb)
1246 sdp->sd_lockstruct.ls_ops->lm_unhold_lvb(gl->gl_lock, gl->gl_lvb);
1253 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1256 struct gfs2_glock *gl;
1257 unsigned long delay = 0;
1258 unsigned long holdtime;
1259 unsigned long now = jiffies;
1261 gl = gfs2_glock_find(sdp, name);
1265 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
1266 if (time_before(now, holdtime))
1267 delay = holdtime - now;
1268 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1269 delay = gl->gl_ops->go_min_hold_time;
1271 spin_lock(&gl->gl_spin);
1272 handle_callback(gl, state, 1, delay);
1273 spin_unlock(&gl->gl_spin);
1274 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1279 * gfs2_glock_cb - Callback used by locking module
1280 * @sdp: Pointer to the superblock
1281 * @type: Type of callback
1282 * @data: Type dependent data pointer
1284 * Called by the locking module when it wants to tell us something.
1285 * Either we need to drop a lock, one of our ASYNC requests completed, or
1286 * a journal from another client needs to be recovered.
1289 void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
1291 struct gfs2_sbd *sdp = cb_data;
1295 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1299 blocking_cb(sdp, data, LM_ST_DEFERRED);
1303 blocking_cb(sdp, data, LM_ST_SHARED);
1307 struct lm_async_cb *async = data;
1308 struct gfs2_glock *gl;
1310 down_read(&gfs2_umount_flush_sem);
1311 gl = gfs2_glock_find(sdp, &async->lc_name);
1312 if (gfs2_assert_warn(sdp, gl))
1314 gl->gl_reply = async->lc_ret;
1315 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1316 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1318 up_read(&gfs2_umount_flush_sem);
1322 case LM_CB_NEED_RECOVERY:
1323 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1324 if (sdp->sd_recoverd_process)
1325 wake_up_process(sdp->sd_recoverd_process);
1329 gfs2_assert_warn(sdp, 0);
1335 * demote_ok - Check to see if it's ok to unlock a glock
1338 * Returns: 1 if it's ok
1341 static int demote_ok(struct gfs2_glock *gl)
1343 const struct gfs2_glock_operations *glops = gl->gl_ops;
1346 if (test_bit(GLF_STICKY, &gl->gl_flags))
1348 else if (glops->go_demote_ok)
1349 demote = glops->go_demote_ok(gl);
1355 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1360 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1362 struct gfs2_sbd *sdp = gl->gl_sbd;
1364 spin_lock(&sdp->sd_reclaim_lock);
1365 if (list_empty(&gl->gl_reclaim)) {
1366 gfs2_glock_hold(gl);
1367 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1368 atomic_inc(&sdp->sd_reclaim_count);
1369 spin_unlock(&sdp->sd_reclaim_lock);
1370 wake_up(&sdp->sd_reclaim_wq);
1372 spin_unlock(&sdp->sd_reclaim_lock);
1376 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1377 * @sdp: the filesystem
1379 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1380 * different glock and we notice that there are a lot of glocks in the
1385 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1387 struct gfs2_glock *gl;
1388 int done_callback = 0;
1390 spin_lock(&sdp->sd_reclaim_lock);
1391 if (list_empty(&sdp->sd_reclaim_list)) {
1392 spin_unlock(&sdp->sd_reclaim_lock);
1395 gl = list_entry(sdp->sd_reclaim_list.next,
1396 struct gfs2_glock, gl_reclaim);
1397 list_del_init(&gl->gl_reclaim);
1398 spin_unlock(&sdp->sd_reclaim_lock);
1400 atomic_dec(&sdp->sd_reclaim_count);
1401 atomic_inc(&sdp->sd_reclaimed);
1403 spin_lock(&gl->gl_spin);
1404 if (find_first_holder(gl) == NULL &&
1405 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl)) {
1406 handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
1409 spin_unlock(&gl->gl_spin);
1410 if (!done_callback ||
1411 queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1416 * examine_bucket - Call a function for glock in a hash bucket
1417 * @examiner: the function
1418 * @sdp: the filesystem
1419 * @bucket: the bucket
1421 * Returns: 1 if the bucket has entries
1424 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1427 struct gfs2_glock *gl, *prev = NULL;
1428 int has_entries = 0;
1429 struct hlist_head *head = &gl_hash_table[hash].hb_list;
1431 read_lock(gl_lock_addr(hash));
1432 /* Can't use hlist_for_each_entry - don't want prefetch here */
1433 if (hlist_empty(head))
1435 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1437 if (!sdp || gl->gl_sbd == sdp) {
1438 gfs2_glock_hold(gl);
1439 read_unlock(gl_lock_addr(hash));
1441 gfs2_glock_put(prev);
1445 read_lock(gl_lock_addr(hash));
1447 if (gl->gl_list.next == NULL)
1449 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
1452 read_unlock(gl_lock_addr(hash));
1454 gfs2_glock_put(prev);
1460 * scan_glock - look at a glock and see if we can reclaim it
1461 * @gl: the glock to look at
1465 static void scan_glock(struct gfs2_glock *gl)
1467 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
1469 if (test_bit(GLF_LOCK, &gl->gl_flags))
1472 spin_lock(&gl->gl_spin);
1473 if (find_first_holder(gl) == NULL &&
1474 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1475 gfs2_glock_schedule_for_reclaim(gl);
1476 spin_unlock(&gl->gl_spin);
1480 * clear_glock - look at a glock and see if we can free it from glock cache
1481 * @gl: the glock to look at
1485 static void clear_glock(struct gfs2_glock *gl)
1487 struct gfs2_sbd *sdp = gl->gl_sbd;
1490 spin_lock(&sdp->sd_reclaim_lock);
1491 if (!list_empty(&gl->gl_reclaim)) {
1492 list_del_init(&gl->gl_reclaim);
1493 atomic_dec(&sdp->sd_reclaim_count);
1494 spin_unlock(&sdp->sd_reclaim_lock);
1495 released = gfs2_glock_put(gl);
1496 gfs2_assert(sdp, !released);
1498 spin_unlock(&sdp->sd_reclaim_lock);
1501 spin_lock(&gl->gl_spin);
1502 if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED)
1503 handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
1504 spin_unlock(&gl->gl_spin);
1505 gfs2_glock_hold(gl);
1506 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1511 * gfs2_gl_hash_clear - Empty out the glock hash table
1512 * @sdp: the filesystem
1513 * @wait: wait until it's all gone
1515 * Called when unmounting the filesystem.
1518 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
1528 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1529 if (examine_bucket(clear_glock, sdp, x))
1536 if (time_after_eq(jiffies,
1537 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1538 fs_warn(sdp, "Unmount seems to be stalled. "
1539 "Dumping lock state...\n");
1540 gfs2_dump_lockstate(sdp);
1544 down_write(&gfs2_umount_flush_sem);
1545 invalidate_inodes(sdp->sd_vfs);
1546 up_write(&gfs2_umount_flush_sem);
1551 static const char *state2str(unsigned state)
1554 case LM_ST_UNLOCKED:
1558 case LM_ST_DEFERRED:
1560 case LM_ST_EXCLUSIVE:
1566 static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1569 if (flags & LM_FLAG_TRY)
1571 if (flags & LM_FLAG_TRY_1CB)
1573 if (flags & LM_FLAG_NOEXP)
1575 if (flags & LM_FLAG_ANY)
1577 if (flags & LM_FLAG_PRIORITY)
1579 if (flags & GL_ASYNC)
1581 if (flags & GL_EXACT)
1583 if (flags & GL_NOCACHE)
1585 if (test_bit(HIF_HOLDER, &iflags))
1587 if (test_bit(HIF_WAIT, &iflags))
1589 if (test_bit(HIF_FIRST, &iflags))
1596 * dump_holder - print information about a glock holder
1597 * @seq: the seq_file struct
1598 * @gh: the glock holder
1600 * Returns: 0 on success, -ENOBUFS when we run out of space
1603 static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
1605 struct task_struct *gh_owner = NULL;
1606 char buffer[KSYM_SYMBOL_LEN];
1609 sprint_symbol(buffer, gh->gh_ip);
1610 if (gh->gh_owner_pid)
1611 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
1612 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n",
1613 state2str(gh->gh_state),
1614 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1616 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1617 gh_owner ? gh_owner->comm : "(ended)", buffer);
1621 static const char *gflags2str(char *buf, const unsigned long *gflags)
1624 if (test_bit(GLF_LOCK, gflags))
1626 if (test_bit(GLF_STICKY, gflags))
1628 if (test_bit(GLF_DEMOTE, gflags))
1630 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1632 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1634 if (test_bit(GLF_DIRTY, gflags))
1636 if (test_bit(GLF_LFLUSH, gflags))
1638 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1640 if (test_bit(GLF_REPLY_PENDING, gflags))
1647 * __dump_glock - print information about a glock
1648 * @seq: The seq_file struct
1651 * The file format is as follows:
1652 * One line per object, capital letters are used to indicate objects
1653 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1654 * other objects are indented by a single space and follow the glock to
1655 * which they are related. Fields are indicated by lower case letters
1656 * followed by a colon and the field value, except for strings which are in
1657 * [] so that its possible to see if they are composed of spaces for
1658 * example. The field's are n = number (id of the object), f = flags,
1659 * t = type, s = state, r = refcount, e = error, p = pid.
1661 * Returns: 0 on success, -ENOBUFS when we run out of space
1664 static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
1666 const struct gfs2_glock_operations *glops = gl->gl_ops;
1667 unsigned long long dtime;
1668 const struct gfs2_holder *gh;
1669 char gflags_buf[32];
1672 dtime = jiffies - gl->gl_demote_time;
1673 dtime *= 1000000/HZ; /* demote time in uSec */
1674 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1676 gfs2_print_dbg(seq, "G: s:%s n:%u/%llu f:%s t:%s d:%s/%llu l:%d a:%d r:%d\n",
1677 state2str(gl->gl_state),
1678 gl->gl_name.ln_type,
1679 (unsigned long long)gl->gl_name.ln_number,
1680 gflags2str(gflags_buf, &gl->gl_flags),
1681 state2str(gl->gl_target),
1682 state2str(gl->gl_demote_state), dtime,
1683 atomic_read(&gl->gl_lvb_count),
1684 atomic_read(&gl->gl_ail_count),
1685 atomic_read(&gl->gl_ref));
1687 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1688 error = dump_holder(seq, gh);
1692 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1693 error = glops->go_dump(seq, gl);
1698 static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1701 spin_lock(&gl->gl_spin);
1702 ret = __dump_glock(seq, gl);
1703 spin_unlock(&gl->gl_spin);
1708 * gfs2_dump_lockstate - print out the current lockstate
1709 * @sdp: the filesystem
1710 * @ub: the buffer to copy the information into
1712 * If @ub is NULL, dump the lockstate to the console.
1716 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
1718 struct gfs2_glock *gl;
1719 struct hlist_node *h;
1723 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1725 read_lock(gl_lock_addr(x));
1727 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
1728 if (gl->gl_sbd != sdp)
1731 error = dump_glock(NULL, gl);
1736 read_unlock(gl_lock_addr(x));
1747 * gfs2_scand - Look for cached glocks and inodes to toss from memory
1748 * @sdp: Pointer to GFS2 superblock
1750 * One of these daemons runs, finding candidates to add to sd_reclaim_list.
1754 static int gfs2_scand(void *data)
1759 while (!kthread_should_stop()) {
1760 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1761 examine_bucket(scan_glock, NULL, x);
1762 if (freezing(current))
1767 schedule_timeout_interruptible(delay * HZ);
1775 int __init gfs2_glock_init(void)
1778 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
1779 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
1781 #ifdef GL_HASH_LOCK_SZ
1782 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
1783 rwlock_init(&gl_hash_locks[i]);
1787 scand_process = kthread_run(gfs2_scand, NULL, "gfs2_scand");
1788 if (IS_ERR(scand_process))
1789 return PTR_ERR(scand_process);
1791 glock_workqueue = create_workqueue("glock_workqueue");
1792 if (IS_ERR(glock_workqueue)) {
1793 kthread_stop(scand_process);
1794 return PTR_ERR(glock_workqueue);
1800 void gfs2_glock_exit(void)
1802 destroy_workqueue(glock_workqueue);
1803 kthread_stop(scand_process);
1806 module_param(scand_secs, uint, S_IRUGO|S_IWUSR);
1807 MODULE_PARM_DESC(scand_secs, "The number of seconds between scand runs");
1809 static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
1811 struct gfs2_glock *gl;
1814 read_lock(gl_lock_addr(gi->hash));
1817 gi->gl = hlist_entry(gl->gl_list.next,
1818 struct gfs2_glock, gl_list);
1820 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1821 struct gfs2_glock, gl_list);
1824 gfs2_glock_hold(gi->gl);
1825 read_unlock(gl_lock_addr(gi->hash));
1828 while (gi->gl == NULL) {
1830 if (gi->hash >= GFS2_GL_HASH_SIZE)
1832 read_lock(gl_lock_addr(gi->hash));
1833 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1834 struct gfs2_glock, gl_list);
1836 gfs2_glock_hold(gi->gl);
1837 read_unlock(gl_lock_addr(gi->hash));
1840 if (gi->sdp != gi->gl->gl_sbd)
1846 static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi)
1849 gfs2_glock_put(gi->gl);
1853 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
1855 struct gfs2_glock_iter *gi = seq->private;
1861 if (gfs2_glock_iter_next(gi)) {
1862 gfs2_glock_iter_free(gi);
1870 static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
1873 struct gfs2_glock_iter *gi = seq->private;
1877 if (gfs2_glock_iter_next(gi)) {
1878 gfs2_glock_iter_free(gi);
1885 static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
1887 struct gfs2_glock_iter *gi = seq->private;
1888 gfs2_glock_iter_free(gi);
1891 static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
1893 return dump_glock(seq, iter_ptr);
1896 static const struct seq_operations gfs2_glock_seq_ops = {
1897 .start = gfs2_glock_seq_start,
1898 .next = gfs2_glock_seq_next,
1899 .stop = gfs2_glock_seq_stop,
1900 .show = gfs2_glock_seq_show,
1903 static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1905 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1906 sizeof(struct gfs2_glock_iter));
1908 struct seq_file *seq = file->private_data;
1909 struct gfs2_glock_iter *gi = seq->private;
1910 gi->sdp = inode->i_private;
1915 static const struct file_operations gfs2_debug_fops = {
1916 .owner = THIS_MODULE,
1917 .open = gfs2_debugfs_open,
1919 .llseek = seq_lseek,
1920 .release = seq_release_private,
1923 int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1925 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1926 if (!sdp->debugfs_dir)
1928 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1930 sdp->debugfs_dir, sdp,
1932 if (!sdp->debugfs_dentry_glocks)
1938 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1940 if (sdp && sdp->debugfs_dir) {
1941 if (sdp->debugfs_dentry_glocks) {
1942 debugfs_remove(sdp->debugfs_dentry_glocks);
1943 sdp->debugfs_dentry_glocks = NULL;
1945 debugfs_remove(sdp->debugfs_dir);
1946 sdp->debugfs_dir = NULL;
1950 int gfs2_register_debugfs(void)
1952 gfs2_root = debugfs_create_dir("gfs2", NULL);
1953 return gfs2_root ? 0 : -ENOMEM;
1956 void gfs2_unregister_debugfs(void)
1958 debugfs_remove(gfs2_root);