fanotify: merge duplicate events on parent and child
[linux/fpc-iii.git] / fs / gfs2 / glock.c
blob19ebc6cd0f2baffa622c27faba1dfeffce01bd1e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 */
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>
37 #include "gfs2.h"
38 #include "incore.h"
39 #include "glock.h"
40 #include "glops.h"
41 #include "inode.h"
42 #include "lops.h"
43 #include "meta_io.h"
44 #include "quota.h"
45 #include "super.h"
46 #include "util.h"
47 #include "bmap.h"
48 #define CREATE_TRACE_POINTS
49 #include "trace_gfs2.h"
51 struct gfs2_glock_iter {
52 struct gfs2_sbd *sdp; /* incore superblock */
53 struct rhashtable_iter hti; /* rhashtable iterator */
54 struct gfs2_glock *gl; /* current glock struct */
55 loff_t last_pos; /* last position */
58 typedef void (*glock_examiner) (struct gfs2_glock * gl);
60 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
62 static struct dentry *gfs2_root;
63 static struct workqueue_struct *glock_workqueue;
64 struct workqueue_struct *gfs2_delete_workqueue;
65 static LIST_HEAD(lru_list);
66 static atomic_t lru_count = ATOMIC_INIT(0);
67 static DEFINE_SPINLOCK(lru_lock);
69 #define GFS2_GL_HASH_SHIFT 15
70 #define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT)
72 static const struct rhashtable_params ht_parms = {
73 .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
74 .key_len = offsetofend(struct lm_lockname, ln_type),
75 .key_offset = offsetof(struct gfs2_glock, gl_name),
76 .head_offset = offsetof(struct gfs2_glock, gl_node),
79 static struct rhashtable gl_hash_table;
81 #define GLOCK_WAIT_TABLE_BITS 12
82 #define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS)
83 static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned;
85 struct wait_glock_queue {
86 struct lm_lockname *name;
87 wait_queue_entry_t wait;
90 static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode,
91 int sync, void *key)
93 struct wait_glock_queue *wait_glock =
94 container_of(wait, struct wait_glock_queue, wait);
95 struct lm_lockname *wait_name = wait_glock->name;
96 struct lm_lockname *wake_name = key;
98 if (wake_name->ln_sbd != wait_name->ln_sbd ||
99 wake_name->ln_number != wait_name->ln_number ||
100 wake_name->ln_type != wait_name->ln_type)
101 return 0;
102 return autoremove_wake_function(wait, mode, sync, key);
105 static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name)
107 u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0);
109 return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS);
113 * wake_up_glock - Wake up waiters on a glock
114 * @gl: the glock
116 static void wake_up_glock(struct gfs2_glock *gl)
118 wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
120 if (waitqueue_active(wq))
121 __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
124 static void gfs2_glock_dealloc(struct rcu_head *rcu)
126 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
128 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
129 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
130 } else {
131 kfree(gl->gl_lksb.sb_lvbptr);
132 kmem_cache_free(gfs2_glock_cachep, gl);
136 void gfs2_glock_free(struct gfs2_glock *gl)
138 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
140 BUG_ON(atomic_read(&gl->gl_revokes));
141 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
142 smp_mb();
143 wake_up_glock(gl);
144 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
145 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
146 wake_up(&sdp->sd_glock_wait);
150 * gfs2_glock_hold() - increment reference count on glock
151 * @gl: The glock to hold
155 void gfs2_glock_hold(struct gfs2_glock *gl)
157 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
158 lockref_get(&gl->gl_lockref);
162 * demote_ok - Check to see if it's ok to unlock a glock
163 * @gl: the glock
165 * Returns: 1 if it's ok
168 static int demote_ok(const struct gfs2_glock *gl)
170 const struct gfs2_glock_operations *glops = gl->gl_ops;
172 if (gl->gl_state == LM_ST_UNLOCKED)
173 return 0;
174 if (!list_empty(&gl->gl_holders))
175 return 0;
176 if (glops->go_demote_ok)
177 return glops->go_demote_ok(gl);
178 return 1;
182 void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
184 if (!(gl->gl_ops->go_flags & GLOF_LRU))
185 return;
187 spin_lock(&lru_lock);
189 list_del(&gl->gl_lru);
190 list_add_tail(&gl->gl_lru, &lru_list);
192 if (!test_bit(GLF_LRU, &gl->gl_flags)) {
193 set_bit(GLF_LRU, &gl->gl_flags);
194 atomic_inc(&lru_count);
197 spin_unlock(&lru_lock);
200 static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
202 if (!(gl->gl_ops->go_flags & GLOF_LRU))
203 return;
205 spin_lock(&lru_lock);
206 if (test_bit(GLF_LRU, &gl->gl_flags)) {
207 list_del_init(&gl->gl_lru);
208 atomic_dec(&lru_count);
209 clear_bit(GLF_LRU, &gl->gl_flags);
211 spin_unlock(&lru_lock);
215 * Enqueue the glock on the work queue. Passes one glock reference on to the
216 * work queue.
218 static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
219 if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) {
221 * We are holding the lockref spinlock, and the work was still
222 * queued above. The queued work (glock_work_func) takes that
223 * spinlock before dropping its glock reference(s), so it
224 * cannot have dropped them in the meantime.
226 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
227 gl->gl_lockref.count--;
231 static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
232 spin_lock(&gl->gl_lockref.lock);
233 __gfs2_glock_queue_work(gl, delay);
234 spin_unlock(&gl->gl_lockref.lock);
237 static void __gfs2_glock_put(struct gfs2_glock *gl)
239 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
240 struct address_space *mapping = gfs2_glock2aspace(gl);
242 lockref_mark_dead(&gl->gl_lockref);
244 gfs2_glock_remove_from_lru(gl);
245 spin_unlock(&gl->gl_lockref.lock);
246 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
247 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
248 trace_gfs2_glock_put(gl);
249 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
253 * Cause the glock to be put in work queue context.
255 void gfs2_glock_queue_put(struct gfs2_glock *gl)
257 gfs2_glock_queue_work(gl, 0);
261 * gfs2_glock_put() - Decrement reference count on glock
262 * @gl: The glock to put
266 void gfs2_glock_put(struct gfs2_glock *gl)
268 if (lockref_put_or_lock(&gl->gl_lockref))
269 return;
271 __gfs2_glock_put(gl);
275 * may_grant - check if its ok to grant a new lock
276 * @gl: The glock
277 * @gh: The lock request which we wish to grant
279 * Returns: true if its ok to grant the lock
282 static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
284 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
285 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
286 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
287 return 0;
288 if (gl->gl_state == gh->gh_state)
289 return 1;
290 if (gh->gh_flags & GL_EXACT)
291 return 0;
292 if (gl->gl_state == LM_ST_EXCLUSIVE) {
293 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
294 return 1;
295 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
296 return 1;
298 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
299 return 1;
300 return 0;
303 static void gfs2_holder_wake(struct gfs2_holder *gh)
305 clear_bit(HIF_WAIT, &gh->gh_iflags);
306 smp_mb__after_atomic();
307 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
308 if (gh->gh_flags & GL_ASYNC) {
309 struct gfs2_sbd *sdp = gh->gh_gl->gl_name.ln_sbd;
311 wake_up(&sdp->sd_async_glock_wait);
316 * do_error - Something unexpected has happened during a lock request
320 static void do_error(struct gfs2_glock *gl, const int ret)
322 struct gfs2_holder *gh, *tmp;
324 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
325 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
326 continue;
327 if (ret & LM_OUT_ERROR)
328 gh->gh_error = -EIO;
329 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
330 gh->gh_error = GLR_TRYFAILED;
331 else
332 continue;
333 list_del_init(&gh->gh_list);
334 trace_gfs2_glock_queue(gh, 0);
335 gfs2_holder_wake(gh);
340 * do_promote - promote as many requests as possible on the current queue
341 * @gl: The glock
343 * Returns: 1 if there is a blocked holder at the head of the list, or 2
344 * if a type specific operation is underway.
347 static int do_promote(struct gfs2_glock *gl)
348 __releases(&gl->gl_lockref.lock)
349 __acquires(&gl->gl_lockref.lock)
351 const struct gfs2_glock_operations *glops = gl->gl_ops;
352 struct gfs2_holder *gh, *tmp;
353 int ret;
355 restart:
356 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
357 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
358 continue;
359 if (may_grant(gl, gh)) {
360 if (gh->gh_list.prev == &gl->gl_holders &&
361 glops->go_lock) {
362 spin_unlock(&gl->gl_lockref.lock);
363 /* FIXME: eliminate this eventually */
364 ret = glops->go_lock(gh);
365 spin_lock(&gl->gl_lockref.lock);
366 if (ret) {
367 if (ret == 1)
368 return 2;
369 gh->gh_error = ret;
370 list_del_init(&gh->gh_list);
371 trace_gfs2_glock_queue(gh, 0);
372 gfs2_holder_wake(gh);
373 goto restart;
375 set_bit(HIF_HOLDER, &gh->gh_iflags);
376 trace_gfs2_promote(gh, 1);
377 gfs2_holder_wake(gh);
378 goto restart;
380 set_bit(HIF_HOLDER, &gh->gh_iflags);
381 trace_gfs2_promote(gh, 0);
382 gfs2_holder_wake(gh);
383 continue;
385 if (gh->gh_list.prev == &gl->gl_holders)
386 return 1;
387 do_error(gl, 0);
388 break;
390 return 0;
394 * find_first_waiter - find the first gh that's waiting for the glock
395 * @gl: the glock
398 static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
400 struct gfs2_holder *gh;
402 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
403 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
404 return gh;
406 return NULL;
410 * state_change - record that the glock is now in a different state
411 * @gl: the glock
412 * @new_state the new state
416 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
418 int held1, held2;
420 held1 = (gl->gl_state != LM_ST_UNLOCKED);
421 held2 = (new_state != LM_ST_UNLOCKED);
423 if (held1 != held2) {
424 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
425 if (held2)
426 gl->gl_lockref.count++;
427 else
428 gl->gl_lockref.count--;
430 if (held1 && held2 && list_empty(&gl->gl_holders))
431 clear_bit(GLF_QUEUED, &gl->gl_flags);
433 if (new_state != gl->gl_target)
434 /* shorten our minimum hold time */
435 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
436 GL_GLOCK_MIN_HOLD);
437 gl->gl_state = new_state;
438 gl->gl_tchange = jiffies;
441 static void gfs2_demote_wake(struct gfs2_glock *gl)
443 gl->gl_demote_state = LM_ST_EXCLUSIVE;
444 clear_bit(GLF_DEMOTE, &gl->gl_flags);
445 smp_mb__after_atomic();
446 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
450 * finish_xmote - The DLM has replied to one of our lock requests
451 * @gl: The glock
452 * @ret: The status from the DLM
456 static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
458 const struct gfs2_glock_operations *glops = gl->gl_ops;
459 struct gfs2_holder *gh;
460 unsigned state = ret & LM_OUT_ST_MASK;
461 int rv;
463 spin_lock(&gl->gl_lockref.lock);
464 trace_gfs2_glock_state_change(gl, state);
465 state_change(gl, state);
466 gh = find_first_waiter(gl);
468 /* Demote to UN request arrived during demote to SH or DF */
469 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
470 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
471 gl->gl_target = LM_ST_UNLOCKED;
473 /* Check for state != intended state */
474 if (unlikely(state != gl->gl_target)) {
475 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
476 /* move to back of queue and try next entry */
477 if (ret & LM_OUT_CANCELED) {
478 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
479 list_move_tail(&gh->gh_list, &gl->gl_holders);
480 gh = find_first_waiter(gl);
481 gl->gl_target = gh->gh_state;
482 goto retry;
484 /* Some error or failed "try lock" - report it */
485 if ((ret & LM_OUT_ERROR) ||
486 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
487 gl->gl_target = gl->gl_state;
488 do_error(gl, ret);
489 goto out;
492 switch(state) {
493 /* Unlocked due to conversion deadlock, try again */
494 case LM_ST_UNLOCKED:
495 retry:
496 do_xmote(gl, gh, gl->gl_target);
497 break;
498 /* Conversion fails, unlock and try again */
499 case LM_ST_SHARED:
500 case LM_ST_DEFERRED:
501 do_xmote(gl, gh, LM_ST_UNLOCKED);
502 break;
503 default: /* Everything else */
504 fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
505 gl->gl_target, state);
506 GLOCK_BUG_ON(gl, 1);
508 spin_unlock(&gl->gl_lockref.lock);
509 return;
512 /* Fast path - we got what we asked for */
513 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
514 gfs2_demote_wake(gl);
515 if (state != LM_ST_UNLOCKED) {
516 if (glops->go_xmote_bh) {
517 spin_unlock(&gl->gl_lockref.lock);
518 rv = glops->go_xmote_bh(gl, gh);
519 spin_lock(&gl->gl_lockref.lock);
520 if (rv) {
521 do_error(gl, rv);
522 goto out;
525 rv = do_promote(gl);
526 if (rv == 2)
527 goto out_locked;
529 out:
530 clear_bit(GLF_LOCK, &gl->gl_flags);
531 out_locked:
532 spin_unlock(&gl->gl_lockref.lock);
536 * do_xmote - Calls the DLM to change the state of a lock
537 * @gl: The lock state
538 * @gh: The holder (only for promotes)
539 * @target: The target lock state
543 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
544 __releases(&gl->gl_lockref.lock)
545 __acquires(&gl->gl_lockref.lock)
547 const struct gfs2_glock_operations *glops = gl->gl_ops;
548 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
549 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
550 int ret;
552 if (unlikely(gfs2_withdrawn(sdp)) &&
553 target != LM_ST_UNLOCKED)
554 return;
555 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
556 LM_FLAG_PRIORITY);
557 GLOCK_BUG_ON(gl, gl->gl_state == target);
558 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
559 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
560 glops->go_inval) {
562 * If another process is already doing the invalidate, let that
563 * finish first. The glock state machine will get back to this
564 * holder again later.
566 if (test_and_set_bit(GLF_INVALIDATE_IN_PROGRESS,
567 &gl->gl_flags))
568 return;
569 do_error(gl, 0); /* Fail queued try locks */
571 gl->gl_req = target;
572 set_bit(GLF_BLOCKING, &gl->gl_flags);
573 if ((gl->gl_req == LM_ST_UNLOCKED) ||
574 (gl->gl_state == LM_ST_EXCLUSIVE) ||
575 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
576 clear_bit(GLF_BLOCKING, &gl->gl_flags);
577 spin_unlock(&gl->gl_lockref.lock);
578 if (glops->go_sync)
579 glops->go_sync(gl);
580 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
581 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
582 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
584 gfs2_glock_hold(gl);
585 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
586 /* lock_dlm */
587 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
588 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
589 target == LM_ST_UNLOCKED &&
590 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
591 finish_xmote(gl, target);
592 gfs2_glock_queue_work(gl, 0);
594 else if (ret) {
595 fs_err(sdp, "lm_lock ret %d\n", ret);
596 GLOCK_BUG_ON(gl, !gfs2_withdrawn(sdp));
598 } else { /* lock_nolock */
599 finish_xmote(gl, target);
600 gfs2_glock_queue_work(gl, 0);
603 spin_lock(&gl->gl_lockref.lock);
607 * find_first_holder - find the first "holder" gh
608 * @gl: the glock
611 static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
613 struct gfs2_holder *gh;
615 if (!list_empty(&gl->gl_holders)) {
616 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
617 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
618 return gh;
620 return NULL;
624 * run_queue - do all outstanding tasks related to a glock
625 * @gl: The glock in question
626 * @nonblock: True if we must not block in run_queue
630 static void run_queue(struct gfs2_glock *gl, const int nonblock)
631 __releases(&gl->gl_lockref.lock)
632 __acquires(&gl->gl_lockref.lock)
634 struct gfs2_holder *gh = NULL;
635 int ret;
637 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
638 return;
640 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
642 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
643 gl->gl_demote_state != gl->gl_state) {
644 if (find_first_holder(gl))
645 goto out_unlock;
646 if (nonblock)
647 goto out_sched;
648 smp_mb();
649 if (atomic_read(&gl->gl_revokes) != 0)
650 goto out_sched;
651 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
652 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
653 gl->gl_target = gl->gl_demote_state;
654 } else {
655 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
656 gfs2_demote_wake(gl);
657 ret = do_promote(gl);
658 if (ret == 0)
659 goto out_unlock;
660 if (ret == 2)
661 goto out;
662 gh = find_first_waiter(gl);
663 gl->gl_target = gh->gh_state;
664 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
665 do_error(gl, 0); /* Fail queued try locks */
667 do_xmote(gl, gh, gl->gl_target);
668 out:
669 return;
671 out_sched:
672 clear_bit(GLF_LOCK, &gl->gl_flags);
673 smp_mb__after_atomic();
674 gl->gl_lockref.count++;
675 __gfs2_glock_queue_work(gl, 0);
676 return;
678 out_unlock:
679 clear_bit(GLF_LOCK, &gl->gl_flags);
680 smp_mb__after_atomic();
681 return;
684 static void delete_work_func(struct work_struct *work)
686 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
687 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
688 struct inode *inode;
689 u64 no_addr = gl->gl_name.ln_number;
691 /* If someone's using this glock to create a new dinode, the block must
692 have been freed by another node, then re-used, in which case our
693 iopen callback is too late after the fact. Ignore it. */
694 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
695 goto out;
697 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
698 if (!IS_ERR_OR_NULL(inode)) {
699 d_prune_aliases(inode);
700 iput(inode);
702 out:
703 gfs2_glock_put(gl);
706 static void glock_work_func(struct work_struct *work)
708 unsigned long delay = 0;
709 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
710 unsigned int drop_refs = 1;
712 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
713 finish_xmote(gl, gl->gl_reply);
714 drop_refs++;
716 spin_lock(&gl->gl_lockref.lock);
717 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
718 gl->gl_state != LM_ST_UNLOCKED &&
719 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
720 unsigned long holdtime, now = jiffies;
722 holdtime = gl->gl_tchange + gl->gl_hold_time;
723 if (time_before(now, holdtime))
724 delay = holdtime - now;
726 if (!delay) {
727 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
728 set_bit(GLF_DEMOTE, &gl->gl_flags);
731 run_queue(gl, 0);
732 if (delay) {
733 /* Keep one glock reference for the work we requeue. */
734 drop_refs--;
735 if (gl->gl_name.ln_type != LM_TYPE_INODE)
736 delay = 0;
737 __gfs2_glock_queue_work(gl, delay);
741 * Drop the remaining glock references manually here. (Mind that
742 * __gfs2_glock_queue_work depends on the lockref spinlock begin held
743 * here as well.)
745 gl->gl_lockref.count -= drop_refs;
746 if (!gl->gl_lockref.count) {
747 __gfs2_glock_put(gl);
748 return;
750 spin_unlock(&gl->gl_lockref.lock);
753 static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
754 struct gfs2_glock *new)
756 struct wait_glock_queue wait;
757 wait_queue_head_t *wq = glock_waitqueue(name);
758 struct gfs2_glock *gl;
760 wait.name = name;
761 init_wait(&wait.wait);
762 wait.wait.func = glock_wake_function;
764 again:
765 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
766 rcu_read_lock();
767 if (new) {
768 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
769 &new->gl_node, ht_parms);
770 if (IS_ERR(gl))
771 goto out;
772 } else {
773 gl = rhashtable_lookup_fast(&gl_hash_table,
774 name, ht_parms);
776 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
777 rcu_read_unlock();
778 schedule();
779 goto again;
781 out:
782 rcu_read_unlock();
783 finish_wait(wq, &wait.wait);
784 return gl;
788 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
789 * @sdp: The GFS2 superblock
790 * @number: the lock number
791 * @glops: The glock_operations to use
792 * @create: If 0, don't create the glock if it doesn't exist
793 * @glp: the glock is returned here
795 * This does not lock a glock, just finds/creates structures for one.
797 * Returns: errno
800 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
801 const struct gfs2_glock_operations *glops, int create,
802 struct gfs2_glock **glp)
804 struct super_block *s = sdp->sd_vfs;
805 struct lm_lockname name = { .ln_number = number,
806 .ln_type = glops->go_type,
807 .ln_sbd = sdp };
808 struct gfs2_glock *gl, *tmp;
809 struct address_space *mapping;
810 struct kmem_cache *cachep;
811 int ret = 0;
813 gl = find_insert_glock(&name, NULL);
814 if (gl) {
815 *glp = gl;
816 return 0;
818 if (!create)
819 return -ENOENT;
821 if (glops->go_flags & GLOF_ASPACE)
822 cachep = gfs2_glock_aspace_cachep;
823 else
824 cachep = gfs2_glock_cachep;
825 gl = kmem_cache_alloc(cachep, GFP_NOFS);
826 if (!gl)
827 return -ENOMEM;
829 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
831 if (glops->go_flags & GLOF_LVB) {
832 gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
833 if (!gl->gl_lksb.sb_lvbptr) {
834 kmem_cache_free(cachep, gl);
835 return -ENOMEM;
839 atomic_inc(&sdp->sd_glock_disposal);
840 gl->gl_node.next = NULL;
841 gl->gl_flags = 0;
842 gl->gl_name = name;
843 gl->gl_lockref.count = 1;
844 gl->gl_state = LM_ST_UNLOCKED;
845 gl->gl_target = LM_ST_UNLOCKED;
846 gl->gl_demote_state = LM_ST_EXCLUSIVE;
847 gl->gl_ops = glops;
848 gl->gl_dstamp = 0;
849 preempt_disable();
850 /* We use the global stats to estimate the initial per-glock stats */
851 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
852 preempt_enable();
853 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
854 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
855 gl->gl_tchange = jiffies;
856 gl->gl_object = NULL;
857 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
858 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
859 INIT_WORK(&gl->gl_delete, delete_work_func);
861 mapping = gfs2_glock2aspace(gl);
862 if (mapping) {
863 mapping->a_ops = &gfs2_meta_aops;
864 mapping->host = s->s_bdev->bd_inode;
865 mapping->flags = 0;
866 mapping_set_gfp_mask(mapping, GFP_NOFS);
867 mapping->private_data = NULL;
868 mapping->writeback_index = 0;
871 tmp = find_insert_glock(&name, gl);
872 if (!tmp) {
873 *glp = gl;
874 goto out;
876 if (IS_ERR(tmp)) {
877 ret = PTR_ERR(tmp);
878 goto out_free;
880 *glp = tmp;
882 out_free:
883 kfree(gl->gl_lksb.sb_lvbptr);
884 kmem_cache_free(cachep, gl);
885 atomic_dec(&sdp->sd_glock_disposal);
887 out:
888 return ret;
892 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
893 * @gl: the glock
894 * @state: the state we're requesting
895 * @flags: the modifier flags
896 * @gh: the holder structure
900 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
901 struct gfs2_holder *gh)
903 INIT_LIST_HEAD(&gh->gh_list);
904 gh->gh_gl = gl;
905 gh->gh_ip = _RET_IP_;
906 gh->gh_owner_pid = get_pid(task_pid(current));
907 gh->gh_state = state;
908 gh->gh_flags = flags;
909 gh->gh_error = 0;
910 gh->gh_iflags = 0;
911 gfs2_glock_hold(gl);
915 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
916 * @state: the state we're requesting
917 * @flags: the modifier flags
918 * @gh: the holder structure
920 * Don't mess with the glock.
924 void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
926 gh->gh_state = state;
927 gh->gh_flags = flags;
928 gh->gh_iflags = 0;
929 gh->gh_ip = _RET_IP_;
930 put_pid(gh->gh_owner_pid);
931 gh->gh_owner_pid = get_pid(task_pid(current));
935 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
936 * @gh: the holder structure
940 void gfs2_holder_uninit(struct gfs2_holder *gh)
942 put_pid(gh->gh_owner_pid);
943 gfs2_glock_put(gh->gh_gl);
944 gfs2_holder_mark_uninitialized(gh);
945 gh->gh_ip = 0;
948 static void gfs2_glock_update_hold_time(struct gfs2_glock *gl,
949 unsigned long start_time)
951 /* Have we waited longer that a second? */
952 if (time_after(jiffies, start_time + HZ)) {
953 /* Lengthen the minimum hold time. */
954 gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR,
955 GL_GLOCK_MAX_HOLD);
960 * gfs2_glock_wait - wait on a glock acquisition
961 * @gh: the glock holder
963 * Returns: 0 on success
966 int gfs2_glock_wait(struct gfs2_holder *gh)
968 unsigned long start_time = jiffies;
970 might_sleep();
971 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
972 gfs2_glock_update_hold_time(gh->gh_gl, start_time);
973 return gh->gh_error;
976 static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs)
978 int i;
980 for (i = 0; i < num_gh; i++)
981 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags))
982 return 1;
983 return 0;
987 * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions
988 * @num_gh: the number of holders in the array
989 * @ghs: the glock holder array
991 * Returns: 0 on success, meaning all glocks have been granted and are held.
992 * -ESTALE if the request timed out, meaning all glocks were released,
993 * and the caller should retry the operation.
996 int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs)
998 struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd;
999 int i, ret = 0, timeout = 0;
1000 unsigned long start_time = jiffies;
1001 bool keep_waiting;
1003 might_sleep();
1005 * Total up the (minimum hold time * 2) of all glocks and use that to
1006 * determine the max amount of time we should wait.
1008 for (i = 0; i < num_gh; i++)
1009 timeout += ghs[i].gh_gl->gl_hold_time << 1;
1011 wait_for_dlm:
1012 if (!wait_event_timeout(sdp->sd_async_glock_wait,
1013 !glocks_pending(num_gh, ghs), timeout))
1014 ret = -ESTALE; /* request timed out. */
1017 * If dlm granted all our requests, we need to adjust the glock
1018 * minimum hold time values according to how long we waited.
1020 * If our request timed out, we need to repeatedly release any held
1021 * glocks we acquired thus far to allow dlm to acquire the remaining
1022 * glocks without deadlocking. We cannot currently cancel outstanding
1023 * glock acquisitions.
1025 * The HIF_WAIT bit tells us which requests still need a response from
1026 * dlm.
1028 * If dlm sent us any errors, we return the first error we find.
1030 keep_waiting = false;
1031 for (i = 0; i < num_gh; i++) {
1032 /* Skip holders we have already dequeued below. */
1033 if (!gfs2_holder_queued(&ghs[i]))
1034 continue;
1035 /* Skip holders with a pending DLM response. */
1036 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags)) {
1037 keep_waiting = true;
1038 continue;
1041 if (test_bit(HIF_HOLDER, &ghs[i].gh_iflags)) {
1042 if (ret == -ESTALE)
1043 gfs2_glock_dq(&ghs[i]);
1044 else
1045 gfs2_glock_update_hold_time(ghs[i].gh_gl,
1046 start_time);
1048 if (!ret)
1049 ret = ghs[i].gh_error;
1052 if (keep_waiting)
1053 goto wait_for_dlm;
1056 * At this point, we've either acquired all locks or released them all.
1058 return ret;
1062 * handle_callback - process a demote request
1063 * @gl: the glock
1064 * @state: the state the caller wants us to change to
1066 * There are only two requests that we are going to see in actual
1067 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
1070 static void handle_callback(struct gfs2_glock *gl, unsigned int state,
1071 unsigned long delay, bool remote)
1073 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
1075 set_bit(bit, &gl->gl_flags);
1076 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
1077 gl->gl_demote_state = state;
1078 gl->gl_demote_time = jiffies;
1079 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
1080 gl->gl_demote_state != state) {
1081 gl->gl_demote_state = LM_ST_UNLOCKED;
1083 if (gl->gl_ops->go_callback)
1084 gl->gl_ops->go_callback(gl, remote);
1085 trace_gfs2_demote_rq(gl, remote);
1088 void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
1090 struct va_format vaf;
1091 va_list args;
1093 va_start(args, fmt);
1095 if (seq) {
1096 seq_vprintf(seq, fmt, args);
1097 } else {
1098 vaf.fmt = fmt;
1099 vaf.va = &args;
1101 pr_err("%pV", &vaf);
1104 va_end(args);
1108 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1109 * @gh: the holder structure to add
1111 * Eventually we should move the recursive locking trap to a
1112 * debugging option or something like that. This is the fast
1113 * path and needs to have the minimum number of distractions.
1117 static inline void add_to_queue(struct gfs2_holder *gh)
1118 __releases(&gl->gl_lockref.lock)
1119 __acquires(&gl->gl_lockref.lock)
1121 struct gfs2_glock *gl = gh->gh_gl;
1122 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1123 struct list_head *insert_pt = NULL;
1124 struct gfs2_holder *gh2;
1125 int try_futile = 0;
1127 GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL);
1128 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1129 GLOCK_BUG_ON(gl, true);
1131 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1132 if (test_bit(GLF_LOCK, &gl->gl_flags))
1133 try_futile = !may_grant(gl, gh);
1134 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1135 goto fail;
1138 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1139 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
1140 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
1141 goto trap_recursive;
1142 if (try_futile &&
1143 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
1144 fail:
1145 gh->gh_error = GLR_TRYFAILED;
1146 gfs2_holder_wake(gh);
1147 return;
1149 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1150 continue;
1151 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
1152 insert_pt = &gh2->gh_list;
1154 set_bit(GLF_QUEUED, &gl->gl_flags);
1155 trace_gfs2_glock_queue(gh, 1);
1156 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1157 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
1158 if (likely(insert_pt == NULL)) {
1159 list_add_tail(&gh->gh_list, &gl->gl_holders);
1160 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
1161 goto do_cancel;
1162 return;
1164 list_add_tail(&gh->gh_list, insert_pt);
1165 do_cancel:
1166 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
1167 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
1168 spin_unlock(&gl->gl_lockref.lock);
1169 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
1170 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
1171 spin_lock(&gl->gl_lockref.lock);
1173 return;
1175 trap_recursive:
1176 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1177 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1178 fs_err(sdp, "lock type: %d req lock state : %d\n",
1179 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1180 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1181 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1182 fs_err(sdp, "lock type: %d req lock state : %d\n",
1183 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1184 gfs2_dump_glock(NULL, gl, true);
1185 BUG();
1189 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1190 * @gh: the holder structure
1192 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1194 * Returns: 0, GLR_TRYFAILED, or errno on failure
1197 int gfs2_glock_nq(struct gfs2_holder *gh)
1199 struct gfs2_glock *gl = gh->gh_gl;
1200 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1201 int error = 0;
1203 if (unlikely(gfs2_withdrawn(sdp)))
1204 return -EIO;
1206 if (test_bit(GLF_LRU, &gl->gl_flags))
1207 gfs2_glock_remove_from_lru(gl);
1209 spin_lock(&gl->gl_lockref.lock);
1210 add_to_queue(gh);
1211 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1212 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
1213 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1214 gl->gl_lockref.count++;
1215 __gfs2_glock_queue_work(gl, 0);
1217 run_queue(gl, 1);
1218 spin_unlock(&gl->gl_lockref.lock);
1220 if (!(gh->gh_flags & GL_ASYNC))
1221 error = gfs2_glock_wait(gh);
1223 return error;
1227 * gfs2_glock_poll - poll to see if an async request has been completed
1228 * @gh: the holder
1230 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1233 int gfs2_glock_poll(struct gfs2_holder *gh)
1235 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
1239 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1240 * @gh: the glock holder
1244 void gfs2_glock_dq(struct gfs2_holder *gh)
1246 struct gfs2_glock *gl = gh->gh_gl;
1247 const struct gfs2_glock_operations *glops = gl->gl_ops;
1248 unsigned delay = 0;
1249 int fast_path = 0;
1251 spin_lock(&gl->gl_lockref.lock);
1252 if (gh->gh_flags & GL_NOCACHE)
1253 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
1255 list_del_init(&gh->gh_list);
1256 clear_bit(HIF_HOLDER, &gh->gh_iflags);
1257 if (find_first_holder(gl) == NULL) {
1258 if (glops->go_unlock) {
1259 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
1260 spin_unlock(&gl->gl_lockref.lock);
1261 glops->go_unlock(gh);
1262 spin_lock(&gl->gl_lockref.lock);
1263 clear_bit(GLF_LOCK, &gl->gl_flags);
1265 if (list_empty(&gl->gl_holders) &&
1266 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1267 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1268 fast_path = 1;
1270 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
1271 gfs2_glock_add_to_lru(gl);
1273 trace_gfs2_glock_queue(gh, 0);
1274 if (unlikely(!fast_path)) {
1275 gl->gl_lockref.count++;
1276 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1277 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1278 gl->gl_name.ln_type == LM_TYPE_INODE)
1279 delay = gl->gl_hold_time;
1280 __gfs2_glock_queue_work(gl, delay);
1282 spin_unlock(&gl->gl_lockref.lock);
1285 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1287 struct gfs2_glock *gl = gh->gh_gl;
1288 gfs2_glock_dq(gh);
1289 might_sleep();
1290 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
1294 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1295 * @gh: the holder structure
1299 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1301 gfs2_glock_dq(gh);
1302 gfs2_holder_uninit(gh);
1306 * gfs2_glock_nq_num - acquire a glock based on lock number
1307 * @sdp: the filesystem
1308 * @number: the lock number
1309 * @glops: the glock operations for the type of glock
1310 * @state: the state to acquire the glock in
1311 * @flags: modifier flags for the acquisition
1312 * @gh: the struct gfs2_holder
1314 * Returns: errno
1317 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1318 const struct gfs2_glock_operations *glops,
1319 unsigned int state, u16 flags, struct gfs2_holder *gh)
1321 struct gfs2_glock *gl;
1322 int error;
1324 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1325 if (!error) {
1326 error = gfs2_glock_nq_init(gl, state, flags, gh);
1327 gfs2_glock_put(gl);
1330 return error;
1334 * glock_compare - Compare two struct gfs2_glock structures for sorting
1335 * @arg_a: the first structure
1336 * @arg_b: the second structure
1340 static int glock_compare(const void *arg_a, const void *arg_b)
1342 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1343 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1344 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1345 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1347 if (a->ln_number > b->ln_number)
1348 return 1;
1349 if (a->ln_number < b->ln_number)
1350 return -1;
1351 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1352 return 0;
1356 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1357 * @num_gh: the number of structures
1358 * @ghs: an array of struct gfs2_holder structures
1360 * Returns: 0 on success (all glocks acquired),
1361 * errno on failure (no glocks acquired)
1364 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1365 struct gfs2_holder **p)
1367 unsigned int x;
1368 int error = 0;
1370 for (x = 0; x < num_gh; x++)
1371 p[x] = &ghs[x];
1373 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1375 for (x = 0; x < num_gh; x++) {
1376 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1378 error = gfs2_glock_nq(p[x]);
1379 if (error) {
1380 while (x--)
1381 gfs2_glock_dq(p[x]);
1382 break;
1386 return error;
1390 * gfs2_glock_nq_m - acquire multiple glocks
1391 * @num_gh: the number of structures
1392 * @ghs: an array of struct gfs2_holder structures
1395 * Returns: 0 on success (all glocks acquired),
1396 * errno on failure (no glocks acquired)
1399 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1401 struct gfs2_holder *tmp[4];
1402 struct gfs2_holder **pph = tmp;
1403 int error = 0;
1405 switch(num_gh) {
1406 case 0:
1407 return 0;
1408 case 1:
1409 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1410 return gfs2_glock_nq(ghs);
1411 default:
1412 if (num_gh <= 4)
1413 break;
1414 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1415 GFP_NOFS);
1416 if (!pph)
1417 return -ENOMEM;
1420 error = nq_m_sync(num_gh, ghs, pph);
1422 if (pph != tmp)
1423 kfree(pph);
1425 return error;
1429 * gfs2_glock_dq_m - release multiple glocks
1430 * @num_gh: the number of structures
1431 * @ghs: an array of struct gfs2_holder structures
1435 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1437 while (num_gh--)
1438 gfs2_glock_dq(&ghs[num_gh]);
1441 void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
1443 unsigned long delay = 0;
1444 unsigned long holdtime;
1445 unsigned long now = jiffies;
1447 gfs2_glock_hold(gl);
1448 holdtime = gl->gl_tchange + gl->gl_hold_time;
1449 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1450 gl->gl_name.ln_type == LM_TYPE_INODE) {
1451 if (time_before(now, holdtime))
1452 delay = holdtime - now;
1453 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1454 delay = gl->gl_hold_time;
1457 spin_lock(&gl->gl_lockref.lock);
1458 handle_callback(gl, state, delay, true);
1459 __gfs2_glock_queue_work(gl, delay);
1460 spin_unlock(&gl->gl_lockref.lock);
1464 * gfs2_should_freeze - Figure out if glock should be frozen
1465 * @gl: The glock in question
1467 * Glocks are not frozen if (a) the result of the dlm operation is
1468 * an error, (b) the locking operation was an unlock operation or
1469 * (c) if there is a "noexp" flagged request anywhere in the queue
1471 * Returns: 1 if freezing should occur, 0 otherwise
1474 static int gfs2_should_freeze(const struct gfs2_glock *gl)
1476 const struct gfs2_holder *gh;
1478 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1479 return 0;
1480 if (gl->gl_target == LM_ST_UNLOCKED)
1481 return 0;
1483 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1484 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1485 continue;
1486 if (LM_FLAG_NOEXP & gh->gh_flags)
1487 return 0;
1490 return 1;
1494 * gfs2_glock_complete - Callback used by locking
1495 * @gl: Pointer to the glock
1496 * @ret: The return value from the dlm
1498 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
1499 * to use a bitfield shared with other glock state fields.
1502 void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
1504 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
1506 spin_lock(&gl->gl_lockref.lock);
1507 gl->gl_reply = ret;
1509 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
1510 if (gfs2_should_freeze(gl)) {
1511 set_bit(GLF_FROZEN, &gl->gl_flags);
1512 spin_unlock(&gl->gl_lockref.lock);
1513 return;
1517 gl->gl_lockref.count++;
1518 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1519 __gfs2_glock_queue_work(gl, 0);
1520 spin_unlock(&gl->gl_lockref.lock);
1523 static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1525 struct gfs2_glock *gla, *glb;
1527 gla = list_entry(a, struct gfs2_glock, gl_lru);
1528 glb = list_entry(b, struct gfs2_glock, gl_lru);
1530 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1531 return 1;
1532 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1533 return -1;
1535 return 0;
1539 * gfs2_dispose_glock_lru - Demote a list of glocks
1540 * @list: The list to dispose of
1542 * Disposing of glocks may involve disk accesses, so that here we sort
1543 * the glocks by number (i.e. disk location of the inodes) so that if
1544 * there are any such accesses, they'll be sent in order (mostly).
1546 * Must be called under the lru_lock, but may drop and retake this
1547 * lock. While the lru_lock is dropped, entries may vanish from the
1548 * list, but no new entries will appear on the list (since it is
1549 * private)
1552 static void gfs2_dispose_glock_lru(struct list_head *list)
1553 __releases(&lru_lock)
1554 __acquires(&lru_lock)
1556 struct gfs2_glock *gl;
1558 list_sort(NULL, list, glock_cmp);
1560 while(!list_empty(list)) {
1561 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1562 list_del_init(&gl->gl_lru);
1563 if (!spin_trylock(&gl->gl_lockref.lock)) {
1564 add_back_to_lru:
1565 list_add(&gl->gl_lru, &lru_list);
1566 set_bit(GLF_LRU, &gl->gl_flags);
1567 atomic_inc(&lru_count);
1568 continue;
1570 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1571 spin_unlock(&gl->gl_lockref.lock);
1572 goto add_back_to_lru;
1574 gl->gl_lockref.count++;
1575 if (demote_ok(gl))
1576 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
1577 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
1578 __gfs2_glock_queue_work(gl, 0);
1579 spin_unlock(&gl->gl_lockref.lock);
1580 cond_resched_lock(&lru_lock);
1585 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1586 * @nr: The number of entries to scan
1588 * This function selects the entries on the LRU which are able to
1589 * be demoted, and then kicks off the process by calling
1590 * gfs2_dispose_glock_lru() above.
1593 static long gfs2_scan_glock_lru(int nr)
1595 struct gfs2_glock *gl;
1596 LIST_HEAD(skipped);
1597 LIST_HEAD(dispose);
1598 long freed = 0;
1600 spin_lock(&lru_lock);
1601 while ((nr-- >= 0) && !list_empty(&lru_list)) {
1602 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1604 /* Test for being demotable */
1605 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
1606 list_move(&gl->gl_lru, &dispose);
1607 atomic_dec(&lru_count);
1608 clear_bit(GLF_LRU, &gl->gl_flags);
1609 freed++;
1610 continue;
1613 list_move(&gl->gl_lru, &skipped);
1615 list_splice(&skipped, &lru_list);
1616 if (!list_empty(&dispose))
1617 gfs2_dispose_glock_lru(&dispose);
1618 spin_unlock(&lru_lock);
1620 return freed;
1623 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1624 struct shrink_control *sc)
1626 if (!(sc->gfp_mask & __GFP_FS))
1627 return SHRINK_STOP;
1628 return gfs2_scan_glock_lru(sc->nr_to_scan);
1631 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1632 struct shrink_control *sc)
1634 return vfs_pressure_ratio(atomic_read(&lru_count));
1637 static struct shrinker glock_shrinker = {
1638 .seeks = DEFAULT_SEEKS,
1639 .count_objects = gfs2_glock_shrink_count,
1640 .scan_objects = gfs2_glock_shrink_scan,
1644 * examine_bucket - Call a function for glock in a hash bucket
1645 * @examiner: the function
1646 * @sdp: the filesystem
1647 * @bucket: the bucket
1649 * Note that the function can be called multiple times on the same
1650 * object. So the user must ensure that the function can cope with
1651 * that.
1654 static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1656 struct gfs2_glock *gl;
1657 struct rhashtable_iter iter;
1659 rhashtable_walk_enter(&gl_hash_table, &iter);
1661 do {
1662 rhashtable_walk_start(&iter);
1664 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl))
1665 if (gl->gl_name.ln_sbd == sdp &&
1666 lockref_get_not_dead(&gl->gl_lockref))
1667 examiner(gl);
1669 rhashtable_walk_stop(&iter);
1670 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1672 rhashtable_walk_exit(&iter);
1676 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1677 * @gl: The glock to thaw
1681 static void thaw_glock(struct gfs2_glock *gl)
1683 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) {
1684 gfs2_glock_put(gl);
1685 return;
1687 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1688 gfs2_glock_queue_work(gl, 0);
1692 * clear_glock - look at a glock and see if we can free it from glock cache
1693 * @gl: the glock to look at
1697 static void clear_glock(struct gfs2_glock *gl)
1699 gfs2_glock_remove_from_lru(gl);
1701 spin_lock(&gl->gl_lockref.lock);
1702 if (gl->gl_state != LM_ST_UNLOCKED)
1703 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
1704 __gfs2_glock_queue_work(gl, 0);
1705 spin_unlock(&gl->gl_lockref.lock);
1709 * gfs2_glock_thaw - Thaw any frozen glocks
1710 * @sdp: The super block
1714 void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1716 glock_hash_walk(thaw_glock, sdp);
1719 static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
1721 spin_lock(&gl->gl_lockref.lock);
1722 gfs2_dump_glock(seq, gl, fsid);
1723 spin_unlock(&gl->gl_lockref.lock);
1726 static void dump_glock_func(struct gfs2_glock *gl)
1728 dump_glock(NULL, gl, true);
1732 * gfs2_gl_hash_clear - Empty out the glock hash table
1733 * @sdp: the filesystem
1734 * @wait: wait until it's all gone
1736 * Called when unmounting the filesystem.
1739 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
1741 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
1742 flush_workqueue(glock_workqueue);
1743 glock_hash_walk(clear_glock, sdp);
1744 flush_workqueue(glock_workqueue);
1745 wait_event_timeout(sdp->sd_glock_wait,
1746 atomic_read(&sdp->sd_glock_disposal) == 0,
1747 HZ * 600);
1748 glock_hash_walk(dump_glock_func, sdp);
1751 void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1753 struct gfs2_glock *gl = ip->i_gl;
1754 int ret;
1756 ret = gfs2_truncatei_resume(ip);
1757 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
1759 spin_lock(&gl->gl_lockref.lock);
1760 clear_bit(GLF_LOCK, &gl->gl_flags);
1761 run_queue(gl, 1);
1762 spin_unlock(&gl->gl_lockref.lock);
1765 static const char *state2str(unsigned state)
1767 switch(state) {
1768 case LM_ST_UNLOCKED:
1769 return "UN";
1770 case LM_ST_SHARED:
1771 return "SH";
1772 case LM_ST_DEFERRED:
1773 return "DF";
1774 case LM_ST_EXCLUSIVE:
1775 return "EX";
1777 return "??";
1780 static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
1782 char *p = buf;
1783 if (flags & LM_FLAG_TRY)
1784 *p++ = 't';
1785 if (flags & LM_FLAG_TRY_1CB)
1786 *p++ = 'T';
1787 if (flags & LM_FLAG_NOEXP)
1788 *p++ = 'e';
1789 if (flags & LM_FLAG_ANY)
1790 *p++ = 'A';
1791 if (flags & LM_FLAG_PRIORITY)
1792 *p++ = 'p';
1793 if (flags & GL_ASYNC)
1794 *p++ = 'a';
1795 if (flags & GL_EXACT)
1796 *p++ = 'E';
1797 if (flags & GL_NOCACHE)
1798 *p++ = 'c';
1799 if (test_bit(HIF_HOLDER, &iflags))
1800 *p++ = 'H';
1801 if (test_bit(HIF_WAIT, &iflags))
1802 *p++ = 'W';
1803 if (test_bit(HIF_FIRST, &iflags))
1804 *p++ = 'F';
1805 *p = 0;
1806 return buf;
1810 * dump_holder - print information about a glock holder
1811 * @seq: the seq_file struct
1812 * @gh: the glock holder
1813 * @fs_id_buf: pointer to file system id (if requested)
1817 static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh,
1818 const char *fs_id_buf)
1820 struct task_struct *gh_owner = NULL;
1821 char flags_buf[32];
1823 rcu_read_lock();
1824 if (gh->gh_owner_pid)
1825 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
1826 gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1827 fs_id_buf, state2str(gh->gh_state),
1828 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1829 gh->gh_error,
1830 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1831 gh_owner ? gh_owner->comm : "(ended)",
1832 (void *)gh->gh_ip);
1833 rcu_read_unlock();
1836 static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
1838 const unsigned long *gflags = &gl->gl_flags;
1839 char *p = buf;
1841 if (test_bit(GLF_LOCK, gflags))
1842 *p++ = 'l';
1843 if (test_bit(GLF_DEMOTE, gflags))
1844 *p++ = 'D';
1845 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1846 *p++ = 'd';
1847 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1848 *p++ = 'p';
1849 if (test_bit(GLF_DIRTY, gflags))
1850 *p++ = 'y';
1851 if (test_bit(GLF_LFLUSH, gflags))
1852 *p++ = 'f';
1853 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1854 *p++ = 'i';
1855 if (test_bit(GLF_REPLY_PENDING, gflags))
1856 *p++ = 'r';
1857 if (test_bit(GLF_INITIAL, gflags))
1858 *p++ = 'I';
1859 if (test_bit(GLF_FROZEN, gflags))
1860 *p++ = 'F';
1861 if (test_bit(GLF_QUEUED, gflags))
1862 *p++ = 'q';
1863 if (test_bit(GLF_LRU, gflags))
1864 *p++ = 'L';
1865 if (gl->gl_object)
1866 *p++ = 'o';
1867 if (test_bit(GLF_BLOCKING, gflags))
1868 *p++ = 'b';
1869 *p = 0;
1870 return buf;
1874 * gfs2_dump_glock - print information about a glock
1875 * @seq: The seq_file struct
1876 * @gl: the glock
1877 * @fsid: If true, also dump the file system id
1879 * The file format is as follows:
1880 * One line per object, capital letters are used to indicate objects
1881 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1882 * other objects are indented by a single space and follow the glock to
1883 * which they are related. Fields are indicated by lower case letters
1884 * followed by a colon and the field value, except for strings which are in
1885 * [] so that its possible to see if they are composed of spaces for
1886 * example. The field's are n = number (id of the object), f = flags,
1887 * t = type, s = state, r = refcount, e = error, p = pid.
1891 void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
1893 const struct gfs2_glock_operations *glops = gl->gl_ops;
1894 unsigned long long dtime;
1895 const struct gfs2_holder *gh;
1896 char gflags_buf[32];
1897 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1898 char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
1900 memset(fs_id_buf, 0, sizeof(fs_id_buf));
1901 if (fsid && sdp) /* safety precaution */
1902 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
1903 dtime = jiffies - gl->gl_demote_time;
1904 dtime *= 1000000/HZ; /* demote time in uSec */
1905 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1906 dtime = 0;
1907 gfs2_print_dbg(seq, "%sG: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d "
1908 "v:%d r:%d m:%ld\n", fs_id_buf, state2str(gl->gl_state),
1909 gl->gl_name.ln_type,
1910 (unsigned long long)gl->gl_name.ln_number,
1911 gflags2str(gflags_buf, gl),
1912 state2str(gl->gl_target),
1913 state2str(gl->gl_demote_state), dtime,
1914 atomic_read(&gl->gl_ail_count),
1915 atomic_read(&gl->gl_revokes),
1916 (int)gl->gl_lockref.count, gl->gl_hold_time);
1918 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1919 dump_holder(seq, gh, fs_id_buf);
1921 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1922 glops->go_dump(seq, gl, fs_id_buf);
1925 static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1927 struct gfs2_glock *gl = iter_ptr;
1929 seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
1930 gl->gl_name.ln_type,
1931 (unsigned long long)gl->gl_name.ln_number,
1932 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1933 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1934 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1935 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1936 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1937 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1938 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1939 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
1940 return 0;
1943 static const char *gfs2_gltype[] = {
1944 "type",
1945 "reserved",
1946 "nondisk",
1947 "inode",
1948 "rgrp",
1949 "meta",
1950 "iopen",
1951 "flock",
1952 "plock",
1953 "quota",
1954 "journal",
1957 static const char *gfs2_stype[] = {
1958 [GFS2_LKS_SRTT] = "srtt",
1959 [GFS2_LKS_SRTTVAR] = "srttvar",
1960 [GFS2_LKS_SRTTB] = "srttb",
1961 [GFS2_LKS_SRTTVARB] = "srttvarb",
1962 [GFS2_LKS_SIRT] = "sirt",
1963 [GFS2_LKS_SIRTVAR] = "sirtvar",
1964 [GFS2_LKS_DCOUNT] = "dlm",
1965 [GFS2_LKS_QCOUNT] = "queue",
1968 #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1970 static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1972 struct gfs2_sbd *sdp = seq->private;
1973 loff_t pos = *(loff_t *)iter_ptr;
1974 unsigned index = pos >> 3;
1975 unsigned subindex = pos & 0x07;
1976 int i;
1978 if (index == 0 && subindex != 0)
1979 return 0;
1981 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1982 (index == 0) ? "cpu": gfs2_stype[subindex]);
1984 for_each_possible_cpu(i) {
1985 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
1987 if (index == 0)
1988 seq_printf(seq, " %15u", i);
1989 else
1990 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1991 lkstats[index - 1].stats[subindex]);
1993 seq_putc(seq, '\n');
1994 return 0;
1997 int __init gfs2_glock_init(void)
1999 int i, ret;
2001 ret = rhashtable_init(&gl_hash_table, &ht_parms);
2002 if (ret < 0)
2003 return ret;
2005 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
2006 WQ_HIGHPRI | WQ_FREEZABLE, 0);
2007 if (!glock_workqueue) {
2008 rhashtable_destroy(&gl_hash_table);
2009 return -ENOMEM;
2011 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
2012 WQ_MEM_RECLAIM | WQ_FREEZABLE,
2014 if (!gfs2_delete_workqueue) {
2015 destroy_workqueue(glock_workqueue);
2016 rhashtable_destroy(&gl_hash_table);
2017 return -ENOMEM;
2020 ret = register_shrinker(&glock_shrinker);
2021 if (ret) {
2022 destroy_workqueue(gfs2_delete_workqueue);
2023 destroy_workqueue(glock_workqueue);
2024 rhashtable_destroy(&gl_hash_table);
2025 return ret;
2028 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
2029 init_waitqueue_head(glock_wait_table + i);
2031 return 0;
2034 void gfs2_glock_exit(void)
2036 unregister_shrinker(&glock_shrinker);
2037 rhashtable_destroy(&gl_hash_table);
2038 destroy_workqueue(glock_workqueue);
2039 destroy_workqueue(gfs2_delete_workqueue);
2042 static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
2044 struct gfs2_glock *gl = gi->gl;
2046 if (gl) {
2047 if (n == 0)
2048 return;
2049 if (!lockref_put_not_zero(&gl->gl_lockref))
2050 gfs2_glock_queue_put(gl);
2052 for (;;) {
2053 gl = rhashtable_walk_next(&gi->hti);
2054 if (IS_ERR_OR_NULL(gl)) {
2055 if (gl == ERR_PTR(-EAGAIN)) {
2056 n = 1;
2057 continue;
2059 gl = NULL;
2060 break;
2062 if (gl->gl_name.ln_sbd != gi->sdp)
2063 continue;
2064 if (n <= 1) {
2065 if (!lockref_get_not_dead(&gl->gl_lockref))
2066 continue;
2067 break;
2068 } else {
2069 if (__lockref_is_dead(&gl->gl_lockref))
2070 continue;
2071 n--;
2074 gi->gl = gl;
2077 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
2078 __acquires(RCU)
2080 struct gfs2_glock_iter *gi = seq->private;
2081 loff_t n;
2084 * We can either stay where we are, skip to the next hash table
2085 * entry, or start from the beginning.
2087 if (*pos < gi->last_pos) {
2088 rhashtable_walk_exit(&gi->hti);
2089 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2090 n = *pos + 1;
2091 } else {
2092 n = *pos - gi->last_pos;
2095 rhashtable_walk_start(&gi->hti);
2097 gfs2_glock_iter_next(gi, n);
2098 gi->last_pos = *pos;
2099 return gi->gl;
2102 static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
2103 loff_t *pos)
2105 struct gfs2_glock_iter *gi = seq->private;
2107 (*pos)++;
2108 gi->last_pos = *pos;
2109 gfs2_glock_iter_next(gi, 1);
2110 return gi->gl;
2113 static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
2114 __releases(RCU)
2116 struct gfs2_glock_iter *gi = seq->private;
2118 rhashtable_walk_stop(&gi->hti);
2121 static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
2123 dump_glock(seq, iter_ptr, false);
2124 return 0;
2127 static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2129 preempt_disable();
2130 if (*pos >= GFS2_NR_SBSTATS)
2131 return NULL;
2132 return pos;
2135 static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2136 loff_t *pos)
2138 (*pos)++;
2139 if (*pos >= GFS2_NR_SBSTATS)
2140 return NULL;
2141 return pos;
2144 static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2146 preempt_enable();
2149 static const struct seq_operations gfs2_glock_seq_ops = {
2150 .start = gfs2_glock_seq_start,
2151 .next = gfs2_glock_seq_next,
2152 .stop = gfs2_glock_seq_stop,
2153 .show = gfs2_glock_seq_show,
2156 static const struct seq_operations gfs2_glstats_seq_ops = {
2157 .start = gfs2_glock_seq_start,
2158 .next = gfs2_glock_seq_next,
2159 .stop = gfs2_glock_seq_stop,
2160 .show = gfs2_glstats_seq_show,
2163 static const struct seq_operations gfs2_sbstats_seq_ops = {
2164 .start = gfs2_sbstats_seq_start,
2165 .next = gfs2_sbstats_seq_next,
2166 .stop = gfs2_sbstats_seq_stop,
2167 .show = gfs2_sbstats_seq_show,
2170 #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2172 static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2173 const struct seq_operations *ops)
2175 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
2176 if (ret == 0) {
2177 struct seq_file *seq = file->private_data;
2178 struct gfs2_glock_iter *gi = seq->private;
2180 gi->sdp = inode->i_private;
2181 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
2182 if (seq->buf)
2183 seq->size = GFS2_SEQ_GOODSIZE;
2185 * Initially, we are "before" the first hash table entry; the
2186 * first call to rhashtable_walk_next gets us the first entry.
2188 gi->last_pos = -1;
2189 gi->gl = NULL;
2190 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2192 return ret;
2195 static int gfs2_glocks_open(struct inode *inode, struct file *file)
2197 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2200 static int gfs2_glocks_release(struct inode *inode, struct file *file)
2202 struct seq_file *seq = file->private_data;
2203 struct gfs2_glock_iter *gi = seq->private;
2205 if (gi->gl)
2206 gfs2_glock_put(gi->gl);
2207 rhashtable_walk_exit(&gi->hti);
2208 return seq_release_private(inode, file);
2211 static int gfs2_glstats_open(struct inode *inode, struct file *file)
2213 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
2216 static int gfs2_sbstats_open(struct inode *inode, struct file *file)
2218 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
2219 if (ret == 0) {
2220 struct seq_file *seq = file->private_data;
2221 seq->private = inode->i_private; /* sdp */
2223 return ret;
2226 static const struct file_operations gfs2_glocks_fops = {
2227 .owner = THIS_MODULE,
2228 .open = gfs2_glocks_open,
2229 .read = seq_read,
2230 .llseek = seq_lseek,
2231 .release = gfs2_glocks_release,
2234 static const struct file_operations gfs2_glstats_fops = {
2235 .owner = THIS_MODULE,
2236 .open = gfs2_glstats_open,
2237 .read = seq_read,
2238 .llseek = seq_lseek,
2239 .release = gfs2_glocks_release,
2242 static const struct file_operations gfs2_sbstats_fops = {
2243 .owner = THIS_MODULE,
2244 .open = gfs2_sbstats_open,
2245 .read = seq_read,
2246 .llseek = seq_lseek,
2247 .release = seq_release,
2250 void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2252 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2254 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2255 &gfs2_glocks_fops);
2257 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2258 &gfs2_glstats_fops);
2260 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2261 &gfs2_sbstats_fops);
2264 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2266 debugfs_remove_recursive(sdp->debugfs_dir);
2267 sdp->debugfs_dir = NULL;
2270 void gfs2_register_debugfs(void)
2272 gfs2_root = debugfs_create_dir("gfs2", NULL);
2275 void gfs2_unregister_debugfs(void)
2277 debugfs_remove(gfs2_root);
2278 gfs2_root = NULL;