1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/buffer_head.h>
13 #include <linux/delay.h>
14 #include <linux/sort.h>
15 #include <linux/hash.h>
16 #include <linux/jhash.h>
17 #include <linux/kallsyms.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/list.h>
20 #include <linux/wait.h>
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/seq_file.h>
24 #include <linux/debugfs.h>
25 #include <linux/kthread.h>
26 #include <linux/freezer.h>
27 #include <linux/workqueue.h>
28 #include <linux/jiffies.h>
29 #include <linux/rcupdate.h>
30 #include <linux/rculist_bl.h>
31 #include <linux/bit_spinlock.h>
32 #include <linux/percpu.h>
33 #include <linux/list_sort.h>
34 #include <linux/lockref.h>
35 #include <linux/rhashtable.h>
36 #include <linux/pid_namespace.h>
37 #include <linux/fdtable.h>
38 #include <linux/file.h>
39 #include <linux/random.h>
52 #define CREATE_TRACE_POINTS
53 #include "trace_gfs2.h"
55 struct gfs2_glock_iter {
56 struct gfs2_sbd *sdp; /* incore superblock */
57 struct rhashtable_iter hti; /* rhashtable iterator */
58 struct gfs2_glock *gl; /* current glock struct */
59 loff_t last_pos; /* last position */
62 typedef void (*glock_examiner) (struct gfs2_glock * gl);
64 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
65 static void request_demote(struct gfs2_glock *gl, unsigned int state,
66 unsigned long delay, bool remote);
68 static struct dentry *gfs2_root;
69 static LIST_HEAD(lru_list);
70 static atomic_t lru_count = ATOMIC_INIT(0);
71 static DEFINE_SPINLOCK(lru_lock);
73 #define GFS2_GL_HASH_SHIFT 15
74 #define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT)
76 static const struct rhashtable_params ht_parms = {
77 .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
78 .key_len = offsetofend(struct lm_lockname, ln_type),
79 .key_offset = offsetof(struct gfs2_glock, gl_name),
80 .head_offset = offsetof(struct gfs2_glock, gl_node),
83 static struct rhashtable gl_hash_table;
85 #define GLOCK_WAIT_TABLE_BITS 12
86 #define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS)
87 static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned;
89 struct wait_glock_queue {
90 struct lm_lockname *name;
91 wait_queue_entry_t wait;
94 static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode,
97 struct wait_glock_queue *wait_glock =
98 container_of(wait, struct wait_glock_queue, wait);
99 struct lm_lockname *wait_name = wait_glock->name;
100 struct lm_lockname *wake_name = key;
102 if (wake_name->ln_sbd != wait_name->ln_sbd ||
103 wake_name->ln_number != wait_name->ln_number ||
104 wake_name->ln_type != wait_name->ln_type)
106 return autoremove_wake_function(wait, mode, sync, key);
109 static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name)
111 u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0);
113 return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS);
117 * wake_up_glock - Wake up waiters on a glock
120 static void wake_up_glock(struct gfs2_glock *gl)
122 wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
124 if (waitqueue_active(wq))
125 __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
128 static void gfs2_glock_dealloc(struct rcu_head *rcu)
130 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
132 kfree(gl->gl_lksb.sb_lvbptr);
133 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
134 struct gfs2_glock_aspace *gla =
135 container_of(gl, struct gfs2_glock_aspace, glock);
136 kmem_cache_free(gfs2_glock_aspace_cachep, gla);
138 kmem_cache_free(gfs2_glock_cachep, gl);
142 * glock_blocked_by_withdraw - determine if we can still use a glock
145 * We need to allow some glocks to be enqueued, dequeued, promoted, and demoted
146 * when we're withdrawn. For example, to maintain metadata integrity, we should
147 * disallow the use of inode and rgrp glocks when withdrawn. Other glocks like
148 * the iopen or freeze glock may be safely used because none of their
149 * metadata goes through the journal. So in general, we should disallow all
150 * glocks that are journaled, and allow all the others. One exception is:
151 * we need to allow our active journal to be promoted and demoted so others
152 * may recover it and we can reacquire it when they're done.
154 static bool glock_blocked_by_withdraw(struct gfs2_glock *gl)
156 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
158 if (!gfs2_withdrawing_or_withdrawn(sdp))
160 if (gl->gl_ops->go_flags & GLOF_NONDISK)
162 if (!sdp->sd_jdesc ||
163 gl->gl_name.ln_number == sdp->sd_jdesc->jd_no_addr)
168 static void __gfs2_glock_free(struct gfs2_glock *gl)
170 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
173 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
176 void gfs2_glock_free(struct gfs2_glock *gl) {
177 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
179 __gfs2_glock_free(gl);
180 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
181 wake_up(&sdp->sd_kill_wait);
184 void gfs2_glock_free_later(struct gfs2_glock *gl) {
185 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
187 spin_lock(&lru_lock);
188 list_add(&gl->gl_lru, &sdp->sd_dead_glocks);
189 spin_unlock(&lru_lock);
190 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
191 wake_up(&sdp->sd_kill_wait);
194 static void gfs2_free_dead_glocks(struct gfs2_sbd *sdp)
196 struct list_head *list = &sdp->sd_dead_glocks;
198 while(!list_empty(list)) {
199 struct gfs2_glock *gl;
201 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
202 list_del_init(&gl->gl_lru);
203 __gfs2_glock_free(gl);
208 * gfs2_glock_hold() - increment reference count on glock
209 * @gl: The glock to hold
213 struct gfs2_glock *gfs2_glock_hold(struct gfs2_glock *gl)
215 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
216 lockref_get(&gl->gl_lockref);
220 static void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
222 spin_lock(&lru_lock);
223 list_move_tail(&gl->gl_lru, &lru_list);
225 if (!test_bit(GLF_LRU, &gl->gl_flags)) {
226 set_bit(GLF_LRU, &gl->gl_flags);
227 atomic_inc(&lru_count);
230 spin_unlock(&lru_lock);
233 static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
235 spin_lock(&lru_lock);
236 if (test_bit(GLF_LRU, &gl->gl_flags)) {
237 list_del_init(&gl->gl_lru);
238 atomic_dec(&lru_count);
239 clear_bit(GLF_LRU, &gl->gl_flags);
241 spin_unlock(&lru_lock);
245 * Enqueue the glock on the work queue. Passes one glock reference on to the
248 static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
249 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
251 if (!queue_delayed_work(sdp->sd_glock_wq, &gl->gl_work, delay)) {
253 * We are holding the lockref spinlock, and the work was still
254 * queued above. The queued work (glock_work_func) takes that
255 * spinlock before dropping its glock reference(s), so it
256 * cannot have dropped them in the meantime.
258 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
259 gl->gl_lockref.count--;
263 static void __gfs2_glock_put(struct gfs2_glock *gl)
265 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
266 struct address_space *mapping = gfs2_glock2aspace(gl);
268 lockref_mark_dead(&gl->gl_lockref);
269 spin_unlock(&gl->gl_lockref.lock);
270 gfs2_glock_remove_from_lru(gl);
271 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
273 truncate_inode_pages_final(mapping);
274 if (!gfs2_withdrawing_or_withdrawn(sdp))
275 GLOCK_BUG_ON(gl, !mapping_empty(mapping));
277 trace_gfs2_glock_put(gl);
278 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
281 static bool __gfs2_glock_put_or_lock(struct gfs2_glock *gl)
283 if (lockref_put_or_lock(&gl->gl_lockref))
285 GLOCK_BUG_ON(gl, gl->gl_lockref.count != 1);
286 if (gl->gl_state != LM_ST_UNLOCKED) {
287 gl->gl_lockref.count--;
288 gfs2_glock_add_to_lru(gl);
289 spin_unlock(&gl->gl_lockref.lock);
296 * gfs2_glock_put() - Decrement reference count on glock
297 * @gl: The glock to put
301 void gfs2_glock_put(struct gfs2_glock *gl)
303 if (__gfs2_glock_put_or_lock(gl))
306 __gfs2_glock_put(gl);
310 * gfs2_glock_put_async - Decrement reference count without sleeping
311 * @gl: The glock to put
313 * Decrement the reference count on glock immediately unless it is the last
314 * reference. Defer putting the last reference to work queue context.
316 void gfs2_glock_put_async(struct gfs2_glock *gl)
318 if (__gfs2_glock_put_or_lock(gl))
321 gfs2_glock_queue_work(gl, 0);
322 spin_unlock(&gl->gl_lockref.lock);
326 * may_grant - check if it's ok to grant a new lock
328 * @current_gh: One of the current holders of @gl
329 * @gh: The lock request which we wish to grant
331 * With our current compatibility rules, if a glock has one or more active
332 * holders (HIF_HOLDER flag set), any of those holders can be passed in as
333 * @current_gh; they are all the same as far as compatibility with the new @gh
336 * Returns true if it's ok to grant the lock.
339 static inline bool may_grant(struct gfs2_glock *gl,
340 struct gfs2_holder *current_gh,
341 struct gfs2_holder *gh)
344 GLOCK_BUG_ON(gl, !test_bit(HIF_HOLDER, ¤t_gh->gh_iflags));
346 switch(current_gh->gh_state) {
347 case LM_ST_EXCLUSIVE:
349 * Here we make a special exception to grant holders
350 * who agree to share the EX lock with other holders
351 * who also have the bit set. If the original holder
352 * has the LM_FLAG_NODE_SCOPE bit set, we grant more
353 * holders with the bit set.
355 return gh->gh_state == LM_ST_EXCLUSIVE &&
356 (current_gh->gh_flags & LM_FLAG_NODE_SCOPE) &&
357 (gh->gh_flags & LM_FLAG_NODE_SCOPE);
361 return gh->gh_state == current_gh->gh_state;
368 if (gl->gl_state == gh->gh_state)
370 if (gh->gh_flags & GL_EXACT)
372 if (gl->gl_state == LM_ST_EXCLUSIVE) {
373 return gh->gh_state == LM_ST_SHARED ||
374 gh->gh_state == LM_ST_DEFERRED;
376 if (gh->gh_flags & LM_FLAG_ANY)
377 return gl->gl_state != LM_ST_UNLOCKED;
381 static void gfs2_holder_wake(struct gfs2_holder *gh)
383 clear_bit(HIF_WAIT, &gh->gh_iflags);
384 smp_mb__after_atomic();
385 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
386 if (gh->gh_flags & GL_ASYNC) {
387 struct gfs2_sbd *sdp = gh->gh_gl->gl_name.ln_sbd;
389 wake_up(&sdp->sd_async_glock_wait);
394 * do_error - Something unexpected has happened during a lock request
396 * @ret: The status from the DLM
399 static void do_error(struct gfs2_glock *gl, const int ret)
401 struct gfs2_holder *gh, *tmp;
403 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
404 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
406 if (ret & LM_OUT_ERROR)
408 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
409 gh->gh_error = GLR_TRYFAILED;
412 list_del_init(&gh->gh_list);
413 trace_gfs2_glock_queue(gh, 0);
414 gfs2_holder_wake(gh);
419 * find_first_holder - find the first "holder" gh
423 static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
425 struct gfs2_holder *gh;
427 if (!list_empty(&gl->gl_holders)) {
428 gh = list_first_entry(&gl->gl_holders, struct gfs2_holder,
430 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
437 * gfs2_instantiate - Call the glops instantiate function
438 * @gh: The glock holder
440 * Returns: 0 if instantiate was successful, or error.
442 int gfs2_instantiate(struct gfs2_holder *gh)
444 struct gfs2_glock *gl = gh->gh_gl;
445 const struct gfs2_glock_operations *glops = gl->gl_ops;
449 if (!test_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags))
453 * Since we unlock the lockref lock, we set a flag to indicate
454 * instantiate is in progress.
456 if (test_and_set_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags)) {
457 wait_on_bit(&gl->gl_flags, GLF_INSTANTIATE_IN_PROG,
458 TASK_UNINTERRUPTIBLE);
460 * Here we just waited for a different instantiate to finish.
461 * But that may not have been successful, as when a process
462 * locks an inode glock _before_ it has an actual inode to
463 * instantiate into. So we check again. This process might
464 * have an inode to instantiate, so might be successful.
469 ret = glops->go_instantiate(gl);
471 clear_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags);
472 clear_and_wake_up_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags);
478 return glops->go_held(gh);
483 * do_promote - promote as many requests as possible on the current queue
486 * Returns true on success (i.e., progress was made or there are no waiters).
489 static bool do_promote(struct gfs2_glock *gl)
491 struct gfs2_holder *gh, *current_gh;
493 current_gh = find_first_holder(gl);
494 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
495 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
497 if (!may_grant(gl, current_gh, gh)) {
499 * If we get here, it means we may not grant this
500 * holder for some reason. If this holder is at the
501 * head of the list, it means we have a blocked holder
502 * at the head, so return false.
504 if (list_is_first(&gh->gh_list, &gl->gl_holders))
509 set_bit(HIF_HOLDER, &gh->gh_iflags);
510 trace_gfs2_promote(gh);
511 gfs2_holder_wake(gh);
519 * find_first_waiter - find the first gh that's waiting for the glock
523 static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
525 struct gfs2_holder *gh;
527 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
528 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
535 * find_last_waiter - find the last gh that's waiting for the glock
538 * This also is a fast way of finding out if there are any waiters.
541 static inline struct gfs2_holder *find_last_waiter(const struct gfs2_glock *gl)
543 struct gfs2_holder *gh;
545 if (list_empty(&gl->gl_holders))
547 gh = list_last_entry(&gl->gl_holders, struct gfs2_holder, gh_list);
548 return test_bit(HIF_HOLDER, &gh->gh_iflags) ? NULL : gh;
552 * state_change - record that the glock is now in a different state
554 * @new_state: the new state
557 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
559 if (new_state != gl->gl_target)
560 /* shorten our minimum hold time */
561 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
563 gl->gl_state = new_state;
564 gl->gl_tchange = jiffies;
567 static void gfs2_set_demote(int nr, struct gfs2_glock *gl)
569 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
571 set_bit(nr, &gl->gl_flags);
573 wake_up(&sdp->sd_async_glock_wait);
576 static void gfs2_demote_wake(struct gfs2_glock *gl)
578 gl->gl_demote_state = LM_ST_EXCLUSIVE;
579 clear_bit(GLF_DEMOTE, &gl->gl_flags);
580 smp_mb__after_atomic();
581 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
585 * finish_xmote - The DLM has replied to one of our lock requests
587 * @ret: The status from the DLM
591 static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
593 const struct gfs2_glock_operations *glops = gl->gl_ops;
594 struct gfs2_holder *gh;
595 unsigned state = ret & LM_OUT_ST_MASK;
597 trace_gfs2_glock_state_change(gl, state);
598 state_change(gl, state);
599 gh = find_first_waiter(gl);
601 /* Demote to UN request arrived during demote to SH or DF */
602 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
603 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
604 gl->gl_target = LM_ST_UNLOCKED;
606 /* Check for state != intended state */
607 if (unlikely(state != gl->gl_target)) {
608 if (gh && (ret & LM_OUT_CANCELED))
609 gfs2_holder_wake(gh);
610 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
611 /* move to back of queue and try next entry */
612 if (ret & LM_OUT_CANCELED) {
613 list_move_tail(&gh->gh_list, &gl->gl_holders);
614 gh = find_first_waiter(gl);
615 gl->gl_target = gh->gh_state;
620 /* Some error or failed "try lock" - report it */
621 if ((ret & LM_OUT_ERROR) ||
622 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
623 gl->gl_target = gl->gl_state;
629 /* Unlocked due to conversion deadlock, try again */
632 do_xmote(gl, gh, gl->gl_target);
634 /* Conversion fails, unlock and try again */
637 do_xmote(gl, gh, LM_ST_UNLOCKED);
639 default: /* Everything else */
640 fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
641 gl->gl_target, state);
647 /* Fast path - we got what we asked for */
648 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
649 gfs2_demote_wake(gl);
650 if (state != LM_ST_UNLOCKED) {
651 if (glops->go_xmote_bh) {
654 spin_unlock(&gl->gl_lockref.lock);
655 rv = glops->go_xmote_bh(gl);
656 spin_lock(&gl->gl_lockref.lock);
665 clear_bit(GLF_LOCK, &gl->gl_flags);
668 static bool is_system_glock(struct gfs2_glock *gl)
670 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
671 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
673 if (gl == m_ip->i_gl)
679 * do_xmote - Calls the DLM to change the state of a lock
680 * @gl: The lock state
681 * @gh: The holder (only for promotes)
682 * @target: The target lock state
686 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh,
688 __releases(&gl->gl_lockref.lock)
689 __acquires(&gl->gl_lockref.lock)
691 const struct gfs2_glock_operations *glops = gl->gl_ops;
692 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
693 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
694 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
697 if (target != LM_ST_UNLOCKED && glock_blocked_by_withdraw(gl) &&
698 gh && !(gh->gh_flags & LM_FLAG_NOEXP))
701 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP);
702 GLOCK_BUG_ON(gl, gl->gl_state == target);
703 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
704 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
707 * If another process is already doing the invalidate, let that
708 * finish first. The glock state machine will get back to this
709 * holder again later.
711 if (test_and_set_bit(GLF_INVALIDATE_IN_PROGRESS,
714 do_error(gl, 0); /* Fail queued try locks */
717 set_bit(GLF_BLOCKING, &gl->gl_flags);
718 if ((gl->gl_req == LM_ST_UNLOCKED) ||
719 (gl->gl_state == LM_ST_EXCLUSIVE) ||
720 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
721 clear_bit(GLF_BLOCKING, &gl->gl_flags);
722 if (!glops->go_inval && !glops->go_sync)
725 spin_unlock(&gl->gl_lockref.lock);
726 if (glops->go_sync) {
727 ret = glops->go_sync(gl);
728 /* If we had a problem syncing (due to io errors or whatever,
729 * we should not invalidate the metadata or tell dlm to
730 * release the glock to other nodes.
733 if (cmpxchg(&sdp->sd_log_error, 0, ret)) {
734 fs_err(sdp, "Error %d syncing glock \n", ret);
735 gfs2_dump_glock(NULL, gl, true);
737 spin_lock(&gl->gl_lockref.lock);
741 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) {
743 * The call to go_sync should have cleared out the ail list.
744 * If there are still items, we have a problem. We ought to
745 * withdraw, but we can't because the withdraw code also uses
746 * glocks. Warn about the error, dump the glock, then fall
747 * through and wait for logd to do the withdraw for us.
749 if ((atomic_read(&gl->gl_ail_count) != 0) &&
750 (!cmpxchg(&sdp->sd_log_error, 0, -EIO))) {
751 gfs2_glock_assert_warn(gl,
752 !atomic_read(&gl->gl_ail_count));
753 gfs2_dump_glock(NULL, gl, true);
755 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
756 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
758 spin_lock(&gl->gl_lockref.lock);
761 gl->gl_lockref.count++;
763 * Check for an error encountered since we called go_sync and go_inval.
764 * If so, we can't withdraw from the glock code because the withdraw
765 * code itself uses glocks (see function signal_our_withdraw) to
766 * change the mount to read-only. Most importantly, we must not call
767 * dlm to unlock the glock until the journal is in a known good state
768 * (after journal replay) otherwise other nodes may use the object
769 * (rgrp or dinode) and then later, journal replay will corrupt the
770 * file system. The best we can do here is wait for the logd daemon
771 * to see sd_log_error and withdraw, and in the meantime, requeue the
774 * We make a special exception for some system glocks, such as the
775 * system statfs inode glock, which needs to be granted before the
776 * gfs2_quotad daemon can exit, and that exit needs to finish before
777 * we can unmount the withdrawn file system.
779 * However, if we're just unlocking the lock (say, for unmount, when
780 * gfs2_gl_hash_clear calls clear_glock) and recovery is complete
781 * then it's okay to tell dlm to unlock it.
783 if (unlikely(sdp->sd_log_error) && !gfs2_withdrawing_or_withdrawn(sdp))
784 gfs2_withdraw_delayed(sdp);
785 if (glock_blocked_by_withdraw(gl) &&
786 (target != LM_ST_UNLOCKED ||
787 test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags))) {
788 if (!is_system_glock(gl)) {
789 request_demote(gl, LM_ST_UNLOCKED, 0, false);
791 * Ordinarily, we would call dlm and its callback would call
792 * finish_xmote, which would call state_change() to the new state.
793 * Since we withdrew, we won't call dlm, so call state_change
794 * manually, but to the UNLOCKED state we desire.
796 state_change(gl, LM_ST_UNLOCKED);
798 * We skip telling dlm to do the locking, so we won't get a
799 * reply that would otherwise clear GLF_LOCK. So we clear it here.
801 clear_bit(GLF_LOCK, &gl->gl_flags);
802 clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
803 gfs2_glock_queue_work(gl, GL_GLOCK_DFT_HOLD);
806 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
810 if (ls->ls_ops->lm_lock) {
811 spin_unlock(&gl->gl_lockref.lock);
812 ret = ls->ls_ops->lm_lock(gl, target, lck_flags);
813 spin_lock(&gl->gl_lockref.lock);
815 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
816 target == LM_ST_UNLOCKED &&
817 test_bit(DFL_UNMOUNT, &ls->ls_recover_flags)) {
819 * The lockspace has been released and the lock has
820 * been unlocked implicitly.
823 fs_err(sdp, "lm_lock ret %d\n", ret);
824 target = gl->gl_state | LM_OUT_ERROR;
826 /* The operation will be completed asynchronously. */
831 /* Complete the operation now. */
832 finish_xmote(gl, target);
833 gfs2_glock_queue_work(gl, 0);
837 * run_queue - do all outstanding tasks related to a glock
838 * @gl: The glock in question
839 * @nonblock: True if we must not block in run_queue
843 static void run_queue(struct gfs2_glock *gl, const int nonblock)
844 __releases(&gl->gl_lockref.lock)
845 __acquires(&gl->gl_lockref.lock)
847 struct gfs2_holder *gh = NULL;
849 if (test_bit(GLF_LOCK, &gl->gl_flags))
851 set_bit(GLF_LOCK, &gl->gl_flags);
853 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
855 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
856 gl->gl_demote_state != gl->gl_state) {
857 if (find_first_holder(gl))
861 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
862 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
863 gl->gl_target = gl->gl_demote_state;
865 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
866 gfs2_demote_wake(gl);
869 gh = find_first_waiter(gl);
870 gl->gl_target = gh->gh_state;
871 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
872 do_error(gl, 0); /* Fail queued try locks */
874 do_xmote(gl, gh, gl->gl_target);
878 clear_bit(GLF_LOCK, &gl->gl_flags);
879 smp_mb__after_atomic();
880 gl->gl_lockref.count++;
881 gfs2_glock_queue_work(gl, 0);
885 clear_bit(GLF_LOCK, &gl->gl_flags);
886 smp_mb__after_atomic();
890 * glock_set_object - set the gl_object field of a glock
892 * @object: the object
894 void glock_set_object(struct gfs2_glock *gl, void *object)
898 spin_lock(&gl->gl_lockref.lock);
899 prev_object = gl->gl_object;
900 gl->gl_object = object;
901 spin_unlock(&gl->gl_lockref.lock);
902 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == NULL)) {
903 pr_warn("glock=%u/%llx\n",
905 (unsigned long long)gl->gl_name.ln_number);
906 gfs2_dump_glock(NULL, gl, true);
911 * glock_clear_object - clear the gl_object field of a glock
913 * @object: object the glock currently points at
915 void glock_clear_object(struct gfs2_glock *gl, void *object)
919 spin_lock(&gl->gl_lockref.lock);
920 prev_object = gl->gl_object;
921 gl->gl_object = NULL;
922 spin_unlock(&gl->gl_lockref.lock);
923 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == object)) {
924 pr_warn("glock=%u/%llx\n",
926 (unsigned long long)gl->gl_name.ln_number);
927 gfs2_dump_glock(NULL, gl, true);
931 void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation)
933 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
935 if (ri->ri_magic == 0)
936 ri->ri_magic = cpu_to_be32(GFS2_MAGIC);
937 if (ri->ri_magic == cpu_to_be32(GFS2_MAGIC))
938 ri->ri_generation_deleted = cpu_to_be64(generation);
941 bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation)
943 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
945 if (ri->ri_magic != cpu_to_be32(GFS2_MAGIC))
947 return generation <= be64_to_cpu(ri->ri_generation_deleted);
950 static void gfs2_glock_poke(struct gfs2_glock *gl)
952 int flags = LM_FLAG_TRY_1CB | LM_FLAG_ANY | GL_SKIP;
953 struct gfs2_holder gh;
956 __gfs2_holder_init(gl, LM_ST_SHARED, flags, &gh, _RET_IP_);
957 error = gfs2_glock_nq(&gh);
960 gfs2_holder_uninit(&gh);
963 static void gfs2_try_evict(struct gfs2_glock *gl)
965 struct gfs2_inode *ip;
968 * If there is contention on the iopen glock and we have an inode, try
969 * to grab and release the inode so that it can be evicted. The
970 * GIF_DEFER_DELETE flag indicates to gfs2_evict_inode() that the inode
971 * should not be deleted locally. This will allow the remote node to
972 * go ahead and delete the inode without us having to do it, which will
973 * avoid rgrp glock thrashing.
975 * The remote node is likely still holding the corresponding inode
976 * glock, so it will run before we get to verify that the delete has
977 * happened below. (Verification is triggered by the call to
978 * gfs2_queue_verify_delete() in gfs2_evict_inode().)
980 spin_lock(&gl->gl_lockref.lock);
982 if (ip && !igrab(&ip->i_inode))
984 spin_unlock(&gl->gl_lockref.lock);
986 wait_on_inode(&ip->i_inode);
987 if (is_bad_inode(&ip->i_inode)) {
993 set_bit(GIF_DEFER_DELETE, &ip->i_flags);
994 d_prune_aliases(&ip->i_inode);
997 /* If the inode was evicted, gl->gl_object will now be NULL. */
998 spin_lock(&gl->gl_lockref.lock);
1001 clear_bit(GIF_DEFER_DELETE, &ip->i_flags);
1002 if (!igrab(&ip->i_inode))
1005 spin_unlock(&gl->gl_lockref.lock);
1007 gfs2_glock_poke(ip->i_gl);
1013 bool gfs2_queue_try_to_evict(struct gfs2_glock *gl)
1015 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1017 if (test_and_set_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
1019 return !mod_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, 0);
1022 bool gfs2_queue_verify_delete(struct gfs2_glock *gl, bool later)
1024 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1025 unsigned long delay;
1027 if (test_and_set_bit(GLF_VERIFY_DELETE, &gl->gl_flags))
1029 delay = later ? HZ + get_random_long() % (HZ * 9) : 0;
1030 return queue_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, delay);
1033 static void delete_work_func(struct work_struct *work)
1035 struct delayed_work *dwork = to_delayed_work(work);
1036 struct gfs2_glock *gl = container_of(dwork, struct gfs2_glock, gl_delete);
1037 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1038 bool verify_delete = test_and_clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
1040 if (test_and_clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
1043 if (verify_delete) {
1044 u64 no_addr = gl->gl_name.ln_number;
1045 struct inode *inode;
1047 inode = gfs2_lookup_by_inum(sdp, no_addr, gl->gl_no_formal_ino,
1048 GFS2_BLKST_UNLINKED);
1049 if (IS_ERR(inode)) {
1050 if (PTR_ERR(inode) == -EAGAIN &&
1051 !test_bit(SDF_KILL, &sdp->sd_flags) &&
1052 gfs2_queue_verify_delete(gl, true))
1055 d_prune_aliases(inode);
1063 static void glock_work_func(struct work_struct *work)
1065 unsigned long delay = 0;
1066 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
1067 unsigned int drop_refs = 1;
1069 spin_lock(&gl->gl_lockref.lock);
1070 if (test_bit(GLF_HAVE_REPLY, &gl->gl_flags)) {
1071 clear_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1072 finish_xmote(gl, gl->gl_reply);
1075 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1076 gl->gl_state != LM_ST_UNLOCKED &&
1077 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
1078 if (gl->gl_name.ln_type == LM_TYPE_INODE) {
1079 unsigned long holdtime, now = jiffies;
1081 holdtime = gl->gl_tchange + gl->gl_hold_time;
1082 if (time_before(now, holdtime))
1083 delay = holdtime - now;
1087 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
1088 gfs2_set_demote(GLF_DEMOTE, gl);
1093 /* Keep one glock reference for the work we requeue. */
1095 gfs2_glock_queue_work(gl, delay);
1098 /* Drop the remaining glock references manually. */
1099 GLOCK_BUG_ON(gl, gl->gl_lockref.count < drop_refs);
1100 gl->gl_lockref.count -= drop_refs;
1101 if (!gl->gl_lockref.count) {
1102 if (gl->gl_state == LM_ST_UNLOCKED) {
1103 __gfs2_glock_put(gl);
1106 gfs2_glock_add_to_lru(gl);
1108 spin_unlock(&gl->gl_lockref.lock);
1111 static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
1112 struct gfs2_glock *new)
1114 struct wait_glock_queue wait;
1115 wait_queue_head_t *wq = glock_waitqueue(name);
1116 struct gfs2_glock *gl;
1119 init_wait(&wait.wait);
1120 wait.wait.func = glock_wake_function;
1123 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1126 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
1127 &new->gl_node, ht_parms);
1131 gl = rhashtable_lookup_fast(&gl_hash_table,
1134 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
1141 finish_wait(wq, &wait.wait);
1143 gfs2_glock_remove_from_lru(gl);
1148 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
1149 * @sdp: The GFS2 superblock
1150 * @number: the lock number
1151 * @glops: The glock_operations to use
1152 * @create: If 0, don't create the glock if it doesn't exist
1153 * @glp: the glock is returned here
1155 * This does not lock a glock, just finds/creates structures for one.
1160 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
1161 const struct gfs2_glock_operations *glops, int create,
1162 struct gfs2_glock **glp)
1164 struct super_block *s = sdp->sd_vfs;
1165 struct lm_lockname name = { .ln_number = number,
1166 .ln_type = glops->go_type,
1168 struct gfs2_glock *gl, *tmp;
1169 struct address_space *mapping;
1171 gl = find_insert_glock(&name, NULL);
1177 if (glops->go_flags & GLOF_ASPACE) {
1178 struct gfs2_glock_aspace *gla =
1179 kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_NOFS);
1184 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_NOFS);
1188 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
1191 if (glops->go_flags & GLOF_LVB) {
1192 gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
1193 if (!gl->gl_lksb.sb_lvbptr) {
1194 gfs2_glock_dealloc(&gl->gl_rcu);
1199 atomic_inc(&sdp->sd_glock_disposal);
1200 gl->gl_node.next = NULL;
1201 gl->gl_flags = BIT(GLF_INITIAL);
1202 if (glops->go_instantiate)
1203 gl->gl_flags |= BIT(GLF_INSTANTIATE_NEEDED);
1205 lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
1206 gl->gl_lockref.count = 1;
1207 gl->gl_state = LM_ST_UNLOCKED;
1208 gl->gl_target = LM_ST_UNLOCKED;
1209 gl->gl_demote_state = LM_ST_EXCLUSIVE;
1212 /* We use the global stats to estimate the initial per-glock stats */
1213 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
1215 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
1216 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
1217 gl->gl_tchange = jiffies;
1218 gl->gl_object = NULL;
1219 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
1220 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
1221 if (gl->gl_name.ln_type == LM_TYPE_IOPEN)
1222 INIT_DELAYED_WORK(&gl->gl_delete, delete_work_func);
1224 mapping = gfs2_glock2aspace(gl);
1226 mapping->a_ops = &gfs2_meta_aops;
1227 mapping->host = s->s_bdev->bd_mapping->host;
1229 mapping_set_gfp_mask(mapping, GFP_NOFS);
1230 mapping->i_private_data = NULL;
1231 mapping->writeback_index = 0;
1234 tmp = find_insert_glock(&name, gl);
1236 gfs2_glock_dealloc(&gl->gl_rcu);
1237 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
1238 wake_up(&sdp->sd_kill_wait);
1241 return PTR_ERR(tmp);
1251 * __gfs2_holder_init - initialize a struct gfs2_holder in the default way
1253 * @state: the state we're requesting
1254 * @flags: the modifier flags
1255 * @gh: the holder structure
1259 void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
1260 struct gfs2_holder *gh, unsigned long ip)
1262 INIT_LIST_HEAD(&gh->gh_list);
1263 gh->gh_gl = gfs2_glock_hold(gl);
1265 gh->gh_owner_pid = get_pid(task_pid(current));
1266 gh->gh_state = state;
1267 gh->gh_flags = flags;
1272 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
1273 * @state: the state we're requesting
1274 * @flags: the modifier flags
1275 * @gh: the holder structure
1277 * Don't mess with the glock.
1281 void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
1283 gh->gh_state = state;
1284 gh->gh_flags = flags;
1286 gh->gh_ip = _RET_IP_;
1287 put_pid(gh->gh_owner_pid);
1288 gh->gh_owner_pid = get_pid(task_pid(current));
1292 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
1293 * @gh: the holder structure
1297 void gfs2_holder_uninit(struct gfs2_holder *gh)
1299 put_pid(gh->gh_owner_pid);
1300 gfs2_glock_put(gh->gh_gl);
1301 gfs2_holder_mark_uninitialized(gh);
1305 static void gfs2_glock_update_hold_time(struct gfs2_glock *gl,
1306 unsigned long start_time)
1308 /* Have we waited longer that a second? */
1309 if (time_after(jiffies, start_time + HZ)) {
1310 /* Lengthen the minimum hold time. */
1311 gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR,
1317 * gfs2_glock_holder_ready - holder is ready and its error code can be collected
1318 * @gh: the glock holder
1320 * Called when a glock holder no longer needs to be waited for because it is
1321 * now either held (HIF_HOLDER set; gh_error == 0), or acquiring the lock has
1322 * failed (gh_error != 0).
1325 int gfs2_glock_holder_ready(struct gfs2_holder *gh)
1327 if (gh->gh_error || (gh->gh_flags & GL_SKIP))
1328 return gh->gh_error;
1329 gh->gh_error = gfs2_instantiate(gh);
1332 return gh->gh_error;
1336 * gfs2_glock_wait - wait on a glock acquisition
1337 * @gh: the glock holder
1339 * Returns: 0 on success
1342 int gfs2_glock_wait(struct gfs2_holder *gh)
1344 unsigned long start_time = jiffies;
1347 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1348 gfs2_glock_update_hold_time(gh->gh_gl, start_time);
1349 return gfs2_glock_holder_ready(gh);
1352 static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs)
1356 for (i = 0; i < num_gh; i++)
1357 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags))
1363 * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions
1364 * @num_gh: the number of holders in the array
1365 * @ghs: the glock holder array
1367 * Returns: 0 on success, meaning all glocks have been granted and are held.
1368 * -ESTALE if the request timed out, meaning all glocks were released,
1369 * and the caller should retry the operation.
1372 int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs)
1374 struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd;
1375 int i, ret = 0, timeout = 0;
1376 unsigned long start_time = jiffies;
1380 * Total up the (minimum hold time * 2) of all glocks and use that to
1381 * determine the max amount of time we should wait.
1383 for (i = 0; i < num_gh; i++)
1384 timeout += ghs[i].gh_gl->gl_hold_time << 1;
1386 if (!wait_event_timeout(sdp->sd_async_glock_wait,
1387 !glocks_pending(num_gh, ghs), timeout)) {
1388 ret = -ESTALE; /* request timed out. */
1392 for (i = 0; i < num_gh; i++) {
1393 struct gfs2_holder *gh = &ghs[i];
1396 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1397 gfs2_glock_update_hold_time(gh->gh_gl,
1400 ret2 = gfs2_glock_holder_ready(gh);
1407 for (i = 0; i < num_gh; i++) {
1408 struct gfs2_holder *gh = &ghs[i];
1417 * request_demote - process a demote request
1419 * @state: the state the caller wants us to change to
1420 * @delay: zero to demote immediately; otherwise pending demote
1421 * @remote: true if this came from a different cluster node
1423 * There are only two requests that we are going to see in actual
1424 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
1427 static void request_demote(struct gfs2_glock *gl, unsigned int state,
1428 unsigned long delay, bool remote)
1430 gfs2_set_demote(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, gl);
1431 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
1432 gl->gl_demote_state = state;
1433 gl->gl_demote_time = jiffies;
1434 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
1435 gl->gl_demote_state != state) {
1436 gl->gl_demote_state = LM_ST_UNLOCKED;
1438 if (gl->gl_ops->go_callback)
1439 gl->gl_ops->go_callback(gl, remote);
1440 trace_gfs2_demote_rq(gl, remote);
1443 void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
1445 struct va_format vaf;
1448 va_start(args, fmt);
1451 seq_vprintf(seq, fmt, args);
1456 pr_err("%pV", &vaf);
1462 static inline bool pid_is_meaningful(const struct gfs2_holder *gh)
1464 if (!(gh->gh_flags & GL_NOPID))
1466 if (gh->gh_state == LM_ST_UNLOCKED)
1472 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1473 * @gh: the holder structure to add
1475 * Eventually we should move the recursive locking trap to a
1476 * debugging option or something like that. This is the fast
1477 * path and needs to have the minimum number of distractions.
1481 static inline void add_to_queue(struct gfs2_holder *gh)
1482 __releases(&gl->gl_lockref.lock)
1483 __acquires(&gl->gl_lockref.lock)
1485 struct gfs2_glock *gl = gh->gh_gl;
1486 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1487 struct list_head *insert_pt = NULL;
1488 struct gfs2_holder *gh2;
1491 GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL);
1492 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1493 GLOCK_BUG_ON(gl, true);
1495 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1496 if (test_bit(GLF_LOCK, &gl->gl_flags)) {
1497 struct gfs2_holder *current_gh;
1499 current_gh = find_first_holder(gl);
1500 try_futile = !may_grant(gl, current_gh, gh);
1502 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1506 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1507 if (likely(gh2->gh_owner_pid != gh->gh_owner_pid))
1509 if (gh->gh_gl->gl_ops->go_type == LM_TYPE_FLOCK)
1511 if (!pid_is_meaningful(gh2))
1513 goto trap_recursive;
1515 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1517 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
1519 gh->gh_error = GLR_TRYFAILED;
1520 gfs2_holder_wake(gh);
1523 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1526 trace_gfs2_glock_queue(gh, 1);
1527 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1528 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
1529 if (likely(insert_pt == NULL)) {
1530 list_add_tail(&gh->gh_list, &gl->gl_holders);
1533 list_add_tail(&gh->gh_list, insert_pt);
1534 spin_unlock(&gl->gl_lockref.lock);
1535 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
1536 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
1537 spin_lock(&gl->gl_lockref.lock);
1541 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1542 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1543 fs_err(sdp, "lock type: %d req lock state : %d\n",
1544 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1545 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1546 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1547 fs_err(sdp, "lock type: %d req lock state : %d\n",
1548 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1549 gfs2_dump_glock(NULL, gl, true);
1554 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1555 * @gh: the holder structure
1557 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1559 * Returns: 0, GLR_TRYFAILED, or errno on failure
1562 int gfs2_glock_nq(struct gfs2_holder *gh)
1564 struct gfs2_glock *gl = gh->gh_gl;
1567 if (glock_blocked_by_withdraw(gl) && !(gh->gh_flags & LM_FLAG_NOEXP))
1570 if (gh->gh_flags & GL_NOBLOCK) {
1571 struct gfs2_holder *current_gh;
1574 spin_lock(&gl->gl_lockref.lock);
1575 if (find_last_waiter(gl))
1577 current_gh = find_first_holder(gl);
1578 if (!may_grant(gl, current_gh, gh))
1580 set_bit(HIF_HOLDER, &gh->gh_iflags);
1581 list_add_tail(&gh->gh_list, &gl->gl_holders);
1582 trace_gfs2_promote(gh);
1585 spin_unlock(&gl->gl_lockref.lock);
1590 spin_lock(&gl->gl_lockref.lock);
1592 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1593 test_and_clear_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags))) {
1594 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1595 gl->gl_lockref.count++;
1596 gfs2_glock_queue_work(gl, 0);
1599 spin_unlock(&gl->gl_lockref.lock);
1602 if (!(gh->gh_flags & GL_ASYNC))
1603 error = gfs2_glock_wait(gh);
1609 * gfs2_glock_poll - poll to see if an async request has been completed
1612 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1615 int gfs2_glock_poll(struct gfs2_holder *gh)
1617 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
1620 static void __gfs2_glock_dq(struct gfs2_holder *gh)
1622 struct gfs2_glock *gl = gh->gh_gl;
1627 * This holder should not be cached, so mark it for demote.
1628 * Note: this should be done before the glock_needs_demote
1631 if (gh->gh_flags & GL_NOCACHE)
1632 request_demote(gl, LM_ST_UNLOCKED, 0, false);
1634 list_del_init(&gh->gh_list);
1635 clear_bit(HIF_HOLDER, &gh->gh_iflags);
1636 trace_gfs2_glock_queue(gh, 0);
1639 * If there hasn't been a demote request we are done.
1640 * (Let the remaining holders, if any, keep holding it.)
1642 if (!glock_needs_demote(gl)) {
1643 if (list_empty(&gl->gl_holders))
1647 if (unlikely(!fast_path)) {
1648 gl->gl_lockref.count++;
1649 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1650 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1651 gl->gl_name.ln_type == LM_TYPE_INODE)
1652 delay = gl->gl_hold_time;
1653 gfs2_glock_queue_work(gl, delay);
1658 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1659 * @gh: the glock holder
1662 void gfs2_glock_dq(struct gfs2_holder *gh)
1664 struct gfs2_glock *gl = gh->gh_gl;
1665 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1667 spin_lock(&gl->gl_lockref.lock);
1668 if (!gfs2_holder_queued(gh)) {
1670 * May have already been dequeued because the locking request
1671 * was GL_ASYNC and it has failed in the meantime.
1676 if (list_is_first(&gh->gh_list, &gl->gl_holders) &&
1677 !test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1678 spin_unlock(&gl->gl_lockref.lock);
1679 gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl);
1680 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1681 spin_lock(&gl->gl_lockref.lock);
1685 * If we're in the process of file system withdraw, we cannot just
1686 * dequeue any glocks until our journal is recovered, lest we introduce
1687 * file system corruption. We need two exceptions to this rule: We need
1688 * to allow unlocking of nondisk glocks and the glock for our own
1689 * journal that needs recovery.
1691 if (test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags) &&
1692 glock_blocked_by_withdraw(gl) &&
1693 gh->gh_gl != sdp->sd_jinode_gl) {
1694 sdp->sd_glock_dqs_held++;
1695 spin_unlock(&gl->gl_lockref.lock);
1697 wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY,
1698 TASK_UNINTERRUPTIBLE);
1699 spin_lock(&gl->gl_lockref.lock);
1702 __gfs2_glock_dq(gh);
1704 spin_unlock(&gl->gl_lockref.lock);
1707 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1709 struct gfs2_glock *gl = gh->gh_gl;
1712 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
1716 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1717 * @gh: the holder structure
1721 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1724 gfs2_holder_uninit(gh);
1728 * gfs2_glock_nq_num - acquire a glock based on lock number
1729 * @sdp: the filesystem
1730 * @number: the lock number
1731 * @glops: the glock operations for the type of glock
1732 * @state: the state to acquire the glock in
1733 * @flags: modifier flags for the acquisition
1734 * @gh: the struct gfs2_holder
1739 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1740 const struct gfs2_glock_operations *glops,
1741 unsigned int state, u16 flags, struct gfs2_holder *gh)
1743 struct gfs2_glock *gl;
1746 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1748 error = gfs2_glock_nq_init(gl, state, flags, gh);
1756 * glock_compare - Compare two struct gfs2_glock structures for sorting
1757 * @arg_a: the first structure
1758 * @arg_b: the second structure
1762 static int glock_compare(const void *arg_a, const void *arg_b)
1764 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1765 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1766 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1767 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1769 if (a->ln_number > b->ln_number)
1771 if (a->ln_number < b->ln_number)
1773 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1778 * nq_m_sync - synchronously acquire more than one glock in deadlock free order
1779 * @num_gh: the number of structures
1780 * @ghs: an array of struct gfs2_holder structures
1781 * @p: placeholder for the holder structure to pass back
1783 * Returns: 0 on success (all glocks acquired),
1784 * errno on failure (no glocks acquired)
1787 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1788 struct gfs2_holder **p)
1793 for (x = 0; x < num_gh; x++)
1796 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1798 for (x = 0; x < num_gh; x++) {
1799 error = gfs2_glock_nq(p[x]);
1802 gfs2_glock_dq(p[x]);
1811 * gfs2_glock_nq_m - acquire multiple glocks
1812 * @num_gh: the number of structures
1813 * @ghs: an array of struct gfs2_holder structures
1815 * Returns: 0 on success (all glocks acquired),
1816 * errno on failure (no glocks acquired)
1819 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1821 struct gfs2_holder *tmp[4];
1822 struct gfs2_holder **pph = tmp;
1829 return gfs2_glock_nq(ghs);
1833 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1839 error = nq_m_sync(num_gh, ghs, pph);
1848 * gfs2_glock_dq_m - release multiple glocks
1849 * @num_gh: the number of structures
1850 * @ghs: an array of struct gfs2_holder structures
1854 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1857 gfs2_glock_dq(&ghs[num_gh]);
1860 void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
1862 unsigned long delay = 0;
1864 gfs2_glock_hold(gl);
1865 spin_lock(&gl->gl_lockref.lock);
1866 if (!list_empty(&gl->gl_holders) &&
1867 gl->gl_name.ln_type == LM_TYPE_INODE) {
1868 unsigned long now = jiffies;
1869 unsigned long holdtime;
1871 holdtime = gl->gl_tchange + gl->gl_hold_time;
1873 if (time_before(now, holdtime))
1874 delay = holdtime - now;
1875 if (test_bit(GLF_HAVE_REPLY, &gl->gl_flags))
1876 delay = gl->gl_hold_time;
1878 request_demote(gl, state, delay, true);
1879 gfs2_glock_queue_work(gl, delay);
1880 spin_unlock(&gl->gl_lockref.lock);
1884 * gfs2_should_freeze - Figure out if glock should be frozen
1885 * @gl: The glock in question
1887 * Glocks are not frozen if (a) the result of the dlm operation is
1888 * an error, (b) the locking operation was an unlock operation or
1889 * (c) if there is a "noexp" flagged request anywhere in the queue
1891 * Returns: 1 if freezing should occur, 0 otherwise
1894 static int gfs2_should_freeze(const struct gfs2_glock *gl)
1896 const struct gfs2_holder *gh;
1898 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1900 if (gl->gl_target == LM_ST_UNLOCKED)
1903 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1904 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1906 if (LM_FLAG_NOEXP & gh->gh_flags)
1914 * gfs2_glock_complete - Callback used by locking
1915 * @gl: Pointer to the glock
1916 * @ret: The return value from the dlm
1918 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
1919 * to use a bitfield shared with other glock state fields.
1922 void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
1924 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
1926 spin_lock(&gl->gl_lockref.lock);
1929 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
1930 if (gfs2_should_freeze(gl)) {
1931 set_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags);
1932 spin_unlock(&gl->gl_lockref.lock);
1937 gl->gl_lockref.count++;
1938 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1939 gfs2_glock_queue_work(gl, 0);
1940 spin_unlock(&gl->gl_lockref.lock);
1943 static int glock_cmp(void *priv, const struct list_head *a,
1944 const struct list_head *b)
1946 struct gfs2_glock *gla, *glb;
1948 gla = list_entry(a, struct gfs2_glock, gl_lru);
1949 glb = list_entry(b, struct gfs2_glock, gl_lru);
1951 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1953 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1959 static bool can_free_glock(struct gfs2_glock *gl)
1961 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1963 return !test_bit(GLF_LOCK, &gl->gl_flags) &&
1964 !gl->gl_lockref.count &&
1965 (!test_bit(GLF_LFLUSH, &gl->gl_flags) ||
1966 test_bit(SDF_KILL, &sdp->sd_flags));
1970 * gfs2_dispose_glock_lru - Demote a list of glocks
1971 * @list: The list to dispose of
1973 * Disposing of glocks may involve disk accesses, so that here we sort
1974 * the glocks by number (i.e. disk location of the inodes) so that if
1975 * there are any such accesses, they'll be sent in order (mostly).
1977 * Must be called under the lru_lock, but may drop and retake this
1978 * lock. While the lru_lock is dropped, entries may vanish from the
1979 * list, but no new entries will appear on the list (since it is
1983 static unsigned long gfs2_dispose_glock_lru(struct list_head *list)
1984 __releases(&lru_lock)
1985 __acquires(&lru_lock)
1987 struct gfs2_glock *gl;
1988 unsigned long freed = 0;
1990 list_sort(NULL, list, glock_cmp);
1992 while(!list_empty(list)) {
1993 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
1994 if (!spin_trylock(&gl->gl_lockref.lock)) {
1996 list_move(&gl->gl_lru, &lru_list);
1999 if (!can_free_glock(gl)) {
2000 spin_unlock(&gl->gl_lockref.lock);
2001 goto add_back_to_lru;
2003 list_del_init(&gl->gl_lru);
2004 atomic_dec(&lru_count);
2005 clear_bit(GLF_LRU, &gl->gl_flags);
2007 gl->gl_lockref.count++;
2008 if (gl->gl_state != LM_ST_UNLOCKED)
2009 request_demote(gl, LM_ST_UNLOCKED, 0, false);
2010 gfs2_glock_queue_work(gl, 0);
2011 spin_unlock(&gl->gl_lockref.lock);
2012 cond_resched_lock(&lru_lock);
2018 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
2019 * @nr: The number of entries to scan
2021 * This function selects the entries on the LRU which are able to
2022 * be demoted, and then kicks off the process by calling
2023 * gfs2_dispose_glock_lru() above.
2026 static unsigned long gfs2_scan_glock_lru(unsigned long nr)
2028 struct gfs2_glock *gl, *next;
2030 unsigned long freed = 0;
2032 spin_lock(&lru_lock);
2033 list_for_each_entry_safe(gl, next, &lru_list, gl_lru) {
2036 if (can_free_glock(gl))
2037 list_move(&gl->gl_lru, &dispose);
2039 if (!list_empty(&dispose))
2040 freed = gfs2_dispose_glock_lru(&dispose);
2041 spin_unlock(&lru_lock);
2046 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
2047 struct shrink_control *sc)
2049 if (!(sc->gfp_mask & __GFP_FS))
2051 return gfs2_scan_glock_lru(sc->nr_to_scan);
2054 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
2055 struct shrink_control *sc)
2057 return vfs_pressure_ratio(atomic_read(&lru_count));
2060 static struct shrinker *glock_shrinker;
2063 * glock_hash_walk - Call a function for glock in a hash bucket
2064 * @examiner: the function
2065 * @sdp: the filesystem
2067 * Note that the function can be called multiple times on the same
2068 * object. So the user must ensure that the function can cope with
2072 static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
2074 struct gfs2_glock *gl;
2075 struct rhashtable_iter iter;
2077 rhashtable_walk_enter(&gl_hash_table, &iter);
2080 rhashtable_walk_start(&iter);
2082 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) {
2083 if (gl->gl_name.ln_sbd == sdp)
2087 rhashtable_walk_stop(&iter);
2088 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
2090 rhashtable_walk_exit(&iter);
2093 void gfs2_cancel_delete_work(struct gfs2_glock *gl)
2095 clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags);
2096 clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
2097 if (cancel_delayed_work(&gl->gl_delete))
2101 static void flush_delete_work(struct gfs2_glock *gl)
2103 if (gl->gl_name.ln_type == LM_TYPE_IOPEN) {
2104 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2106 if (cancel_delayed_work(&gl->gl_delete)) {
2107 queue_delayed_work(sdp->sd_delete_wq,
2113 void gfs2_flush_delete_work(struct gfs2_sbd *sdp)
2115 glock_hash_walk(flush_delete_work, sdp);
2116 flush_workqueue(sdp->sd_delete_wq);
2120 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
2121 * @gl: The glock to thaw
2125 static void thaw_glock(struct gfs2_glock *gl)
2127 if (!test_and_clear_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags))
2129 if (!lockref_get_not_dead(&gl->gl_lockref))
2132 gfs2_glock_remove_from_lru(gl);
2133 spin_lock(&gl->gl_lockref.lock);
2134 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
2135 gfs2_glock_queue_work(gl, 0);
2136 spin_unlock(&gl->gl_lockref.lock);
2140 * clear_glock - look at a glock and see if we can free it from glock cache
2141 * @gl: the glock to look at
2145 static void clear_glock(struct gfs2_glock *gl)
2147 gfs2_glock_remove_from_lru(gl);
2149 spin_lock(&gl->gl_lockref.lock);
2150 if (!__lockref_is_dead(&gl->gl_lockref)) {
2151 gl->gl_lockref.count++;
2152 if (gl->gl_state != LM_ST_UNLOCKED)
2153 request_demote(gl, LM_ST_UNLOCKED, 0, false);
2154 gfs2_glock_queue_work(gl, 0);
2156 spin_unlock(&gl->gl_lockref.lock);
2160 * gfs2_glock_thaw - Thaw any frozen glocks
2161 * @sdp: The super block
2165 void gfs2_glock_thaw(struct gfs2_sbd *sdp)
2167 glock_hash_walk(thaw_glock, sdp);
2170 static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2172 spin_lock(&gl->gl_lockref.lock);
2173 gfs2_dump_glock(seq, gl, fsid);
2174 spin_unlock(&gl->gl_lockref.lock);
2177 static void dump_glock_func(struct gfs2_glock *gl)
2179 dump_glock(NULL, gl, true);
2182 static void withdraw_dq(struct gfs2_glock *gl)
2184 spin_lock(&gl->gl_lockref.lock);
2185 if (!__lockref_is_dead(&gl->gl_lockref) &&
2186 glock_blocked_by_withdraw(gl))
2187 do_error(gl, LM_OUT_ERROR); /* remove pending waiters */
2188 spin_unlock(&gl->gl_lockref.lock);
2191 void gfs2_gl_dq_holders(struct gfs2_sbd *sdp)
2193 glock_hash_walk(withdraw_dq, sdp);
2197 * gfs2_gl_hash_clear - Empty out the glock hash table
2198 * @sdp: the filesystem
2200 * Called when unmounting the filesystem.
2203 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
2205 unsigned long start = jiffies;
2206 bool timed_out = false;
2208 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
2209 flush_workqueue(sdp->sd_glock_wq);
2210 glock_hash_walk(clear_glock, sdp);
2211 flush_workqueue(sdp->sd_glock_wq);
2213 while (!timed_out) {
2214 wait_event_timeout(sdp->sd_kill_wait,
2215 !atomic_read(&sdp->sd_glock_disposal),
2217 if (!atomic_read(&sdp->sd_glock_disposal))
2219 timed_out = time_after(jiffies, start + (HZ * 600));
2220 fs_warn(sdp, "%u glocks left after %u seconds%s\n",
2221 atomic_read(&sdp->sd_glock_disposal),
2222 jiffies_to_msecs(jiffies - start) / 1000,
2223 timed_out ? ":" : "; still waiting");
2225 gfs2_lm_unmount(sdp);
2226 gfs2_free_dead_glocks(sdp);
2227 glock_hash_walk(dump_glock_func, sdp);
2228 destroy_workqueue(sdp->sd_glock_wq);
2229 sdp->sd_glock_wq = NULL;
2232 static const char *state2str(unsigned state)
2235 case LM_ST_UNLOCKED:
2239 case LM_ST_DEFERRED:
2241 case LM_ST_EXCLUSIVE:
2247 static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
2250 if (flags & LM_FLAG_TRY)
2252 if (flags & LM_FLAG_TRY_1CB)
2254 if (flags & LM_FLAG_NOEXP)
2256 if (flags & LM_FLAG_ANY)
2258 if (flags & LM_FLAG_NODE_SCOPE)
2260 if (flags & GL_ASYNC)
2262 if (flags & GL_EXACT)
2264 if (flags & GL_NOCACHE)
2266 if (test_bit(HIF_HOLDER, &iflags))
2268 if (test_bit(HIF_WAIT, &iflags))
2270 if (flags & GL_SKIP)
2277 * dump_holder - print information about a glock holder
2278 * @seq: the seq_file struct
2279 * @gh: the glock holder
2280 * @fs_id_buf: pointer to file system id (if requested)
2284 static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh,
2285 const char *fs_id_buf)
2287 const char *comm = "(none)";
2288 pid_t owner_pid = 0;
2292 if (pid_is_meaningful(gh)) {
2293 struct task_struct *gh_owner;
2296 owner_pid = pid_nr(gh->gh_owner_pid);
2297 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
2299 comm = gh_owner->comm;
2301 gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
2302 fs_id_buf, state2str(gh->gh_state),
2303 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
2304 gh->gh_error, (long)owner_pid, comm, (void *)gh->gh_ip);
2308 static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
2310 const unsigned long *gflags = &gl->gl_flags;
2313 if (test_bit(GLF_LOCK, gflags))
2315 if (test_bit(GLF_DEMOTE, gflags))
2317 if (test_bit(GLF_PENDING_DEMOTE, gflags))
2319 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
2321 if (test_bit(GLF_DIRTY, gflags))
2323 if (test_bit(GLF_LFLUSH, gflags))
2325 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
2327 if (test_bit(GLF_HAVE_REPLY, gflags))
2329 if (test_bit(GLF_INITIAL, gflags))
2331 if (test_bit(GLF_HAVE_FROZEN_REPLY, gflags))
2333 if (!list_empty(&gl->gl_holders))
2335 if (test_bit(GLF_LRU, gflags))
2339 if (test_bit(GLF_BLOCKING, gflags))
2341 if (test_bit(GLF_UNLOCKED, gflags))
2343 if (test_bit(GLF_INSTANTIATE_NEEDED, gflags))
2345 if (test_bit(GLF_INSTANTIATE_IN_PROG, gflags))
2347 if (test_bit(GLF_TRY_TO_EVICT, gflags))
2349 if (test_bit(GLF_VERIFY_DELETE, gflags))
2356 * gfs2_dump_glock - print information about a glock
2357 * @seq: The seq_file struct
2359 * @fsid: If true, also dump the file system id
2361 * The file format is as follows:
2362 * One line per object, capital letters are used to indicate objects
2363 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
2364 * other objects are indented by a single space and follow the glock to
2365 * which they are related. Fields are indicated by lower case letters
2366 * followed by a colon and the field value, except for strings which are in
2367 * [] so that its possible to see if they are composed of spaces for
2368 * example. The field's are n = number (id of the object), f = flags,
2369 * t = type, s = state, r = refcount, e = error, p = pid.
2373 void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2375 const struct gfs2_glock_operations *glops = gl->gl_ops;
2376 unsigned long long dtime;
2377 const struct gfs2_holder *gh;
2378 char gflags_buf[32];
2379 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2380 char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
2381 unsigned long nrpages = 0;
2383 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
2384 struct address_space *mapping = gfs2_glock2aspace(gl);
2386 nrpages = mapping->nrpages;
2388 memset(fs_id_buf, 0, sizeof(fs_id_buf));
2389 if (fsid && sdp) /* safety precaution */
2390 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
2391 dtime = jiffies - gl->gl_demote_time;
2392 dtime *= 1000000/HZ; /* demote time in uSec */
2393 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
2395 gfs2_print_dbg(seq, "%sG: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d "
2396 "v:%d r:%d m:%ld p:%lu\n",
2397 fs_id_buf, state2str(gl->gl_state),
2398 gl->gl_name.ln_type,
2399 (unsigned long long)gl->gl_name.ln_number,
2400 gflags2str(gflags_buf, gl),
2401 state2str(gl->gl_target),
2402 state2str(gl->gl_demote_state), dtime,
2403 atomic_read(&gl->gl_ail_count),
2404 atomic_read(&gl->gl_revokes),
2405 (int)gl->gl_lockref.count, gl->gl_hold_time, nrpages);
2407 list_for_each_entry(gh, &gl->gl_holders, gh_list)
2408 dump_holder(seq, gh, fs_id_buf);
2410 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
2411 glops->go_dump(seq, gl, fs_id_buf);
2414 static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
2416 struct gfs2_glock *gl = iter_ptr;
2418 seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
2419 gl->gl_name.ln_type,
2420 (unsigned long long)gl->gl_name.ln_number,
2421 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
2422 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
2423 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
2424 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
2425 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
2426 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
2427 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
2428 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
2432 static const char *gfs2_gltype[] = {
2446 static const char *gfs2_stype[] = {
2447 [GFS2_LKS_SRTT] = "srtt",
2448 [GFS2_LKS_SRTTVAR] = "srttvar",
2449 [GFS2_LKS_SRTTB] = "srttb",
2450 [GFS2_LKS_SRTTVARB] = "srttvarb",
2451 [GFS2_LKS_SIRT] = "sirt",
2452 [GFS2_LKS_SIRTVAR] = "sirtvar",
2453 [GFS2_LKS_DCOUNT] = "dlm",
2454 [GFS2_LKS_QCOUNT] = "queue",
2457 #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
2459 static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
2461 struct gfs2_sbd *sdp = seq->private;
2462 loff_t pos = *(loff_t *)iter_ptr;
2463 unsigned index = pos >> 3;
2464 unsigned subindex = pos & 0x07;
2467 if (index == 0 && subindex != 0)
2470 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
2471 (index == 0) ? "cpu": gfs2_stype[subindex]);
2473 for_each_possible_cpu(i) {
2474 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
2477 seq_printf(seq, " %15u", i);
2479 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
2480 lkstats[index - 1].stats[subindex]);
2482 seq_putc(seq, '\n');
2486 int __init gfs2_glock_init(void)
2490 ret = rhashtable_init(&gl_hash_table, &ht_parms);
2494 glock_shrinker = shrinker_alloc(0, "gfs2-glock");
2495 if (!glock_shrinker) {
2496 rhashtable_destroy(&gl_hash_table);
2500 glock_shrinker->count_objects = gfs2_glock_shrink_count;
2501 glock_shrinker->scan_objects = gfs2_glock_shrink_scan;
2503 shrinker_register(glock_shrinker);
2505 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
2506 init_waitqueue_head(glock_wait_table + i);
2511 void gfs2_glock_exit(void)
2513 shrinker_free(glock_shrinker);
2514 rhashtable_destroy(&gl_hash_table);
2517 static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
2519 struct gfs2_glock *gl = gi->gl;
2524 gfs2_glock_put_async(gl);
2527 gl = rhashtable_walk_next(&gi->hti);
2528 if (IS_ERR_OR_NULL(gl)) {
2529 if (gl == ERR_PTR(-EAGAIN)) {
2536 if (gl->gl_name.ln_sbd != gi->sdp)
2539 if (!lockref_get_not_dead(&gl->gl_lockref))
2543 if (__lockref_is_dead(&gl->gl_lockref))
2551 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
2554 struct gfs2_glock_iter *gi = seq->private;
2558 * We can either stay where we are, skip to the next hash table
2559 * entry, or start from the beginning.
2561 if (*pos < gi->last_pos) {
2562 rhashtable_walk_exit(&gi->hti);
2563 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2566 n = *pos - gi->last_pos;
2569 rhashtable_walk_start(&gi->hti);
2571 gfs2_glock_iter_next(gi, n);
2572 gi->last_pos = *pos;
2576 static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
2579 struct gfs2_glock_iter *gi = seq->private;
2582 gi->last_pos = *pos;
2583 gfs2_glock_iter_next(gi, 1);
2587 static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
2590 struct gfs2_glock_iter *gi = seq->private;
2592 rhashtable_walk_stop(&gi->hti);
2595 static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
2597 dump_glock(seq, iter_ptr, false);
2601 static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2604 if (*pos >= GFS2_NR_SBSTATS)
2609 static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2613 if (*pos >= GFS2_NR_SBSTATS)
2618 static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2623 static const struct seq_operations gfs2_glock_seq_ops = {
2624 .start = gfs2_glock_seq_start,
2625 .next = gfs2_glock_seq_next,
2626 .stop = gfs2_glock_seq_stop,
2627 .show = gfs2_glock_seq_show,
2630 static const struct seq_operations gfs2_glstats_seq_ops = {
2631 .start = gfs2_glock_seq_start,
2632 .next = gfs2_glock_seq_next,
2633 .stop = gfs2_glock_seq_stop,
2634 .show = gfs2_glstats_seq_show,
2637 static const struct seq_operations gfs2_sbstats_sops = {
2638 .start = gfs2_sbstats_seq_start,
2639 .next = gfs2_sbstats_seq_next,
2640 .stop = gfs2_sbstats_seq_stop,
2641 .show = gfs2_sbstats_seq_show,
2644 #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2646 static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2647 const struct seq_operations *ops)
2649 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
2651 struct seq_file *seq = file->private_data;
2652 struct gfs2_glock_iter *gi = seq->private;
2654 gi->sdp = inode->i_private;
2655 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
2657 seq->size = GFS2_SEQ_GOODSIZE;
2659 * Initially, we are "before" the first hash table entry; the
2660 * first call to rhashtable_walk_next gets us the first entry.
2664 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2669 static int gfs2_glocks_open(struct inode *inode, struct file *file)
2671 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2674 static int gfs2_glocks_release(struct inode *inode, struct file *file)
2676 struct seq_file *seq = file->private_data;
2677 struct gfs2_glock_iter *gi = seq->private;
2680 gfs2_glock_put(gi->gl);
2681 rhashtable_walk_exit(&gi->hti);
2682 return seq_release_private(inode, file);
2685 static int gfs2_glstats_open(struct inode *inode, struct file *file)
2687 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
2690 static const struct file_operations gfs2_glocks_fops = {
2691 .owner = THIS_MODULE,
2692 .open = gfs2_glocks_open,
2694 .llseek = seq_lseek,
2695 .release = gfs2_glocks_release,
2698 static const struct file_operations gfs2_glstats_fops = {
2699 .owner = THIS_MODULE,
2700 .open = gfs2_glstats_open,
2702 .llseek = seq_lseek,
2703 .release = gfs2_glocks_release,
2706 struct gfs2_glockfd_iter {
2707 struct super_block *sb;
2709 struct task_struct *task;
2714 static struct task_struct *gfs2_glockfd_next_task(struct gfs2_glockfd_iter *i)
2716 struct pid_namespace *ns = task_active_pid_ns(current);
2720 put_task_struct(i->task);
2725 pid = find_ge_pid(i->tgid, ns);
2727 i->tgid = pid_nr_ns(pid, ns);
2728 i->task = pid_task(pid, PIDTYPE_TGID);
2733 get_task_struct(i->task);
2739 static struct file *gfs2_glockfd_next_file(struct gfs2_glockfd_iter *i)
2748 struct inode *inode;
2750 i->file = task_lookup_next_fdget_rcu(i->task, &i->fd);
2756 inode = file_inode(i->file);
2757 if (inode->i_sb == i->sb)
2768 static void *gfs2_glockfd_seq_start(struct seq_file *seq, loff_t *pos)
2770 struct gfs2_glockfd_iter *i = seq->private;
2774 while (gfs2_glockfd_next_task(i)) {
2775 if (gfs2_glockfd_next_file(i))
2782 static void *gfs2_glockfd_seq_next(struct seq_file *seq, void *iter_ptr,
2785 struct gfs2_glockfd_iter *i = seq->private;
2790 if (gfs2_glockfd_next_file(i))
2793 } while (gfs2_glockfd_next_task(i));
2797 static void gfs2_glockfd_seq_stop(struct seq_file *seq, void *iter_ptr)
2799 struct gfs2_glockfd_iter *i = seq->private;
2804 put_task_struct(i->task);
2807 static void gfs2_glockfd_seq_show_flock(struct seq_file *seq,
2808 struct gfs2_glockfd_iter *i)
2810 struct gfs2_file *fp = i->file->private_data;
2811 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
2812 struct lm_lockname gl_name = { .ln_type = LM_TYPE_RESERVED };
2814 if (!READ_ONCE(fl_gh->gh_gl))
2817 spin_lock(&i->file->f_lock);
2818 if (gfs2_holder_initialized(fl_gh))
2819 gl_name = fl_gh->gh_gl->gl_name;
2820 spin_unlock(&i->file->f_lock);
2822 if (gl_name.ln_type != LM_TYPE_RESERVED) {
2823 seq_printf(seq, "%d %u %u/%llx\n",
2824 i->tgid, i->fd, gl_name.ln_type,
2825 (unsigned long long)gl_name.ln_number);
2829 static int gfs2_glockfd_seq_show(struct seq_file *seq, void *iter_ptr)
2831 struct gfs2_glockfd_iter *i = seq->private;
2832 struct inode *inode = file_inode(i->file);
2833 struct gfs2_glock *gl;
2835 inode_lock_shared(inode);
2836 gl = GFS2_I(inode)->i_iopen_gh.gh_gl;
2838 seq_printf(seq, "%d %u %u/%llx\n",
2839 i->tgid, i->fd, gl->gl_name.ln_type,
2840 (unsigned long long)gl->gl_name.ln_number);
2842 gfs2_glockfd_seq_show_flock(seq, i);
2843 inode_unlock_shared(inode);
2847 static const struct seq_operations gfs2_glockfd_seq_ops = {
2848 .start = gfs2_glockfd_seq_start,
2849 .next = gfs2_glockfd_seq_next,
2850 .stop = gfs2_glockfd_seq_stop,
2851 .show = gfs2_glockfd_seq_show,
2854 static int gfs2_glockfd_open(struct inode *inode, struct file *file)
2856 struct gfs2_glockfd_iter *i;
2857 struct gfs2_sbd *sdp = inode->i_private;
2859 i = __seq_open_private(file, &gfs2_glockfd_seq_ops,
2860 sizeof(struct gfs2_glockfd_iter));
2863 i->sb = sdp->sd_vfs;
2867 static const struct file_operations gfs2_glockfd_fops = {
2868 .owner = THIS_MODULE,
2869 .open = gfs2_glockfd_open,
2871 .llseek = seq_lseek,
2872 .release = seq_release_private,
2875 DEFINE_SEQ_ATTRIBUTE(gfs2_sbstats);
2877 void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2879 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2881 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2884 debugfs_create_file("glockfd", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2885 &gfs2_glockfd_fops);
2887 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2888 &gfs2_glstats_fops);
2890 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2891 &gfs2_sbstats_fops);
2894 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2896 debugfs_remove_recursive(sdp->debugfs_dir);
2897 sdp->debugfs_dir = NULL;
2900 void gfs2_register_debugfs(void)
2902 gfs2_root = debugfs_create_dir("gfs2", NULL);
2905 void gfs2_unregister_debugfs(void)
2907 debugfs_remove(gfs2_root);