Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 */
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
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/file.h>
38#include <linux/random.h>
39
40#include "gfs2.h"
41#include "incore.h"
42#include "glock.h"
43#include "glops.h"
44#include "inode.h"
45#include "lops.h"
46#include "meta_io.h"
47#include "quota.h"
48#include "super.h"
49#include "util.h"
50#include "bmap.h"
51#define CREATE_TRACE_POINTS
52#include "trace_gfs2.h"
53
54struct gfs2_glock_iter {
55 struct gfs2_sbd *sdp; /* incore superblock */
56 struct rhashtable_iter hti; /* rhashtable iterator */
57 struct gfs2_glock *gl; /* current glock struct */
58 loff_t last_pos; /* last position */
59};
60
61typedef void (*glock_examiner) (struct gfs2_glock * gl);
62
63static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
64static void request_demote(struct gfs2_glock *gl, unsigned int state,
65 unsigned long delay, bool remote);
66
67static struct dentry *gfs2_root;
68static LIST_HEAD(lru_list);
69static atomic_t lru_count = ATOMIC_INIT(0);
70static DEFINE_SPINLOCK(lru_lock);
71
72#define GFS2_GL_HASH_SHIFT 15
73#define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT)
74
75static const struct rhashtable_params ht_parms = {
76 .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
77 .key_len = offsetofend(struct lm_lockname, ln_type),
78 .key_offset = offsetof(struct gfs2_glock, gl_name),
79 .head_offset = offsetof(struct gfs2_glock, gl_node),
80};
81
82static struct rhashtable gl_hash_table;
83
84#define GLOCK_WAIT_TABLE_BITS 12
85#define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS)
86static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned;
87
88struct wait_glock_queue {
89 struct lm_lockname *name;
90 wait_queue_entry_t wait;
91};
92
93static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode,
94 int sync, void *key)
95{
96 struct wait_glock_queue *wait_glock =
97 container_of(wait, struct wait_glock_queue, wait);
98 struct lm_lockname *wait_name = wait_glock->name;
99 struct lm_lockname *wake_name = key;
100
101 if (wake_name->ln_sbd != wait_name->ln_sbd ||
102 wake_name->ln_number != wait_name->ln_number ||
103 wake_name->ln_type != wait_name->ln_type)
104 return 0;
105 return autoremove_wake_function(wait, mode, sync, key);
106}
107
108static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name)
109{
110 u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0);
111
112 return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS);
113}
114
115/**
116 * wake_up_glock - Wake up waiters on a glock
117 * @gl: the glock
118 */
119static void wake_up_glock(struct gfs2_glock *gl)
120{
121 wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
122
123 if (waitqueue_active(wq))
124 __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
125}
126
127static void gfs2_glock_dealloc(struct rcu_head *rcu)
128{
129 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
130
131 kfree(gl->gl_lksb.sb_lvbptr);
132 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
133 struct gfs2_glock_aspace *gla =
134 container_of(gl, struct gfs2_glock_aspace, glock);
135 kmem_cache_free(gfs2_glock_aspace_cachep, gla);
136 } else
137 kmem_cache_free(gfs2_glock_cachep, gl);
138}
139
140static void __gfs2_glock_free(struct gfs2_glock *gl)
141{
142 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
143 smp_mb();
144 wake_up_glock(gl);
145 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
146}
147
148void gfs2_glock_free(struct gfs2_glock *gl) {
149 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
150
151 __gfs2_glock_free(gl);
152 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
153 wake_up(&sdp->sd_kill_wait);
154}
155
156void gfs2_glock_free_later(struct gfs2_glock *gl) {
157 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
158
159 spin_lock(&lru_lock);
160 list_add(&gl->gl_lru, &sdp->sd_dead_glocks);
161 spin_unlock(&lru_lock);
162 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
163 wake_up(&sdp->sd_kill_wait);
164}
165
166static void gfs2_free_dead_glocks(struct gfs2_sbd *sdp)
167{
168 struct list_head *list = &sdp->sd_dead_glocks;
169
170 while(!list_empty(list)) {
171 struct gfs2_glock *gl;
172
173 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
174 list_del_init(&gl->gl_lru);
175 __gfs2_glock_free(gl);
176 }
177}
178
179/**
180 * gfs2_glock_hold() - increment reference count on glock
181 * @gl: The glock to hold
182 *
183 */
184
185struct gfs2_glock *gfs2_glock_hold(struct gfs2_glock *gl)
186{
187 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
188 lockref_get(&gl->gl_lockref);
189 return gl;
190}
191
192static void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
193{
194 spin_lock(&lru_lock);
195 list_move_tail(&gl->gl_lru, &lru_list);
196
197 if (!test_bit(GLF_LRU, &gl->gl_flags)) {
198 set_bit(GLF_LRU, &gl->gl_flags);
199 atomic_inc(&lru_count);
200 }
201
202 spin_unlock(&lru_lock);
203}
204
205static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
206{
207 spin_lock(&lru_lock);
208 if (test_bit(GLF_LRU, &gl->gl_flags)) {
209 list_del_init(&gl->gl_lru);
210 atomic_dec(&lru_count);
211 clear_bit(GLF_LRU, &gl->gl_flags);
212 }
213 spin_unlock(&lru_lock);
214}
215
216/*
217 * Enqueue the glock on the work queue. Passes one glock reference on to the
218 * work queue.
219 */
220static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
221 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
222
223 if (!queue_delayed_work(sdp->sd_glock_wq, &gl->gl_work, delay)) {
224 /*
225 * We are holding the lockref spinlock, and the work was still
226 * queued above. The queued work (glock_work_func) takes that
227 * spinlock before dropping its glock reference(s), so it
228 * cannot have dropped them in the meantime.
229 */
230 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
231 gl->gl_lockref.count--;
232 }
233}
234
235static void __gfs2_glock_put(struct gfs2_glock *gl)
236{
237 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
238 struct address_space *mapping = gfs2_glock2aspace(gl);
239
240 lockref_mark_dead(&gl->gl_lockref);
241 spin_unlock(&gl->gl_lockref.lock);
242 gfs2_glock_remove_from_lru(gl);
243 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
244 if (mapping) {
245 truncate_inode_pages_final(mapping);
246 if (!gfs2_withdrawn(sdp))
247 GLOCK_BUG_ON(gl, !mapping_empty(mapping));
248 }
249 trace_gfs2_glock_put(gl);
250 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
251}
252
253static bool __gfs2_glock_put_or_lock(struct gfs2_glock *gl)
254{
255 if (lockref_put_or_lock(&gl->gl_lockref))
256 return true;
257 GLOCK_BUG_ON(gl, gl->gl_lockref.count != 1);
258 if (gl->gl_state != LM_ST_UNLOCKED) {
259 gl->gl_lockref.count--;
260 gfs2_glock_add_to_lru(gl);
261 spin_unlock(&gl->gl_lockref.lock);
262 return true;
263 }
264 return false;
265}
266
267/**
268 * gfs2_glock_put() - Decrement reference count on glock
269 * @gl: The glock to put
270 *
271 */
272
273void gfs2_glock_put(struct gfs2_glock *gl)
274{
275 if (__gfs2_glock_put_or_lock(gl))
276 return;
277
278 __gfs2_glock_put(gl);
279}
280
281/*
282 * gfs2_glock_put_async - Decrement reference count without sleeping
283 * @gl: The glock to put
284 *
285 * Decrement the reference count on glock immediately unless it is the last
286 * reference. Defer putting the last reference to work queue context.
287 */
288void gfs2_glock_put_async(struct gfs2_glock *gl)
289{
290 if (__gfs2_glock_put_or_lock(gl))
291 return;
292
293 gfs2_glock_queue_work(gl, 0);
294 spin_unlock(&gl->gl_lockref.lock);
295}
296
297/**
298 * may_grant - check if it's ok to grant a new lock
299 * @gl: The glock
300 * @current_gh: One of the current holders of @gl
301 * @gh: The lock request which we wish to grant
302 *
303 * With our current compatibility rules, if a glock has one or more active
304 * holders (HIF_HOLDER flag set), any of those holders can be passed in as
305 * @current_gh; they are all the same as far as compatibility with the new @gh
306 * goes.
307 *
308 * Returns true if it's ok to grant the lock.
309 */
310
311static inline bool may_grant(struct gfs2_glock *gl,
312 struct gfs2_holder *current_gh,
313 struct gfs2_holder *gh)
314{
315 if (current_gh) {
316 GLOCK_BUG_ON(gl, !test_bit(HIF_HOLDER, ¤t_gh->gh_iflags));
317
318 switch(current_gh->gh_state) {
319 case LM_ST_EXCLUSIVE:
320 /*
321 * Here we make a special exception to grant holders
322 * who agree to share the EX lock with other holders
323 * who also have the bit set. If the original holder
324 * has the LM_FLAG_NODE_SCOPE bit set, we grant more
325 * holders with the bit set.
326 */
327 return gh->gh_state == LM_ST_EXCLUSIVE &&
328 (current_gh->gh_flags & LM_FLAG_NODE_SCOPE) &&
329 (gh->gh_flags & LM_FLAG_NODE_SCOPE);
330
331 case LM_ST_SHARED:
332 case LM_ST_DEFERRED:
333 return gh->gh_state == current_gh->gh_state;
334
335 default:
336 return false;
337 }
338 }
339
340 if (gl->gl_state == gh->gh_state)
341 return true;
342 if (gh->gh_flags & GL_EXACT)
343 return false;
344 if (gl->gl_state == LM_ST_EXCLUSIVE) {
345 return gh->gh_state == LM_ST_SHARED ||
346 gh->gh_state == LM_ST_DEFERRED;
347 }
348 if (gh->gh_flags & LM_FLAG_ANY)
349 return gl->gl_state != LM_ST_UNLOCKED;
350 return false;
351}
352
353static void gfs2_holder_wake(struct gfs2_holder *gh)
354{
355 clear_bit(HIF_WAIT, &gh->gh_iflags);
356 smp_mb__after_atomic();
357 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
358 if (gh->gh_flags & GL_ASYNC) {
359 struct gfs2_sbd *sdp = gh->gh_gl->gl_name.ln_sbd;
360
361 wake_up(&sdp->sd_async_glock_wait);
362 }
363}
364
365/**
366 * do_error - Something unexpected has happened during a lock request
367 * @gl: The glock
368 * @ret: The status from the DLM
369 */
370
371static void do_error(struct gfs2_glock *gl, const int ret)
372{
373 struct gfs2_holder *gh, *tmp;
374
375 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
376 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
377 continue;
378 if (ret & LM_OUT_ERROR)
379 gh->gh_error = -EIO;
380 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
381 gh->gh_error = GLR_TRYFAILED;
382 else
383 continue;
384 list_del_init(&gh->gh_list);
385 trace_gfs2_glock_queue(gh, 0);
386 gfs2_holder_wake(gh);
387 }
388}
389
390/**
391 * find_first_holder - find the first "holder" gh
392 * @gl: the glock
393 */
394
395static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
396{
397 struct gfs2_holder *gh;
398
399 if (!list_empty(&gl->gl_holders)) {
400 gh = list_first_entry(&gl->gl_holders, struct gfs2_holder,
401 gh_list);
402 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
403 return gh;
404 }
405 return NULL;
406}
407
408/*
409 * gfs2_instantiate - Call the glops instantiate function
410 * @gh: The glock holder
411 *
412 * Returns: 0 if instantiate was successful, or error.
413 */
414int gfs2_instantiate(struct gfs2_holder *gh)
415{
416 struct gfs2_glock *gl = gh->gh_gl;
417 const struct gfs2_glock_operations *glops = gl->gl_ops;
418 int ret;
419
420again:
421 if (!test_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags))
422 goto done;
423
424 /*
425 * Since we unlock the lockref lock, we set a flag to indicate
426 * instantiate is in progress.
427 */
428 if (test_and_set_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags)) {
429 wait_on_bit(&gl->gl_flags, GLF_INSTANTIATE_IN_PROG,
430 TASK_UNINTERRUPTIBLE);
431 /*
432 * Here we just waited for a different instantiate to finish.
433 * But that may not have been successful, as when a process
434 * locks an inode glock _before_ it has an actual inode to
435 * instantiate into. So we check again. This process might
436 * have an inode to instantiate, so might be successful.
437 */
438 goto again;
439 }
440
441 ret = glops->go_instantiate(gl);
442 if (!ret)
443 clear_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags);
444 clear_and_wake_up_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags);
445 if (ret)
446 return ret;
447
448done:
449 if (glops->go_held)
450 return glops->go_held(gh);
451 return 0;
452}
453
454/**
455 * do_promote - promote as many requests as possible on the current queue
456 * @gl: The glock
457 */
458
459static void do_promote(struct gfs2_glock *gl)
460{
461 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
462 struct gfs2_holder *gh, *current_gh;
463
464 if (gfs2_withdrawn(sdp)) {
465 do_error(gl, LM_OUT_ERROR);
466 return;
467 }
468
469 current_gh = find_first_holder(gl);
470 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
471 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
472 continue;
473 if (!may_grant(gl, current_gh, gh)) {
474 /*
475 * If we get here, it means we may not grant this
476 * holder for some reason.
477 */
478 if (current_gh)
479 do_error(gl, 0); /* Fail queued try locks */
480 break;
481 }
482 set_bit(HIF_HOLDER, &gh->gh_iflags);
483 trace_gfs2_promote(gh);
484 gfs2_holder_wake(gh);
485 if (!current_gh)
486 current_gh = gh;
487 }
488}
489
490/**
491 * find_first_waiter - find the first gh that's waiting for the glock
492 * @gl: the glock
493 */
494
495static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
496{
497 struct gfs2_holder *gh;
498
499 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
500 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
501 return gh;
502 }
503 return NULL;
504}
505
506/**
507 * find_last_waiter - find the last gh that's waiting for the glock
508 * @gl: the glock
509 *
510 * This also is a fast way of finding out if there are any waiters.
511 */
512
513static inline struct gfs2_holder *find_last_waiter(const struct gfs2_glock *gl)
514{
515 struct gfs2_holder *gh;
516
517 if (list_empty(&gl->gl_holders))
518 return NULL;
519 gh = list_last_entry(&gl->gl_holders, struct gfs2_holder, gh_list);
520 return test_bit(HIF_HOLDER, &gh->gh_iflags) ? NULL : gh;
521}
522
523/**
524 * state_change - record that the glock is now in a different state
525 * @gl: the glock
526 * @new_state: the new state
527 */
528
529static void state_change(struct gfs2_glock *gl, unsigned int new_state)
530{
531 if (new_state != gl->gl_target)
532 /* shorten our minimum hold time */
533 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
534 GL_GLOCK_MIN_HOLD);
535 gl->gl_state = new_state;
536 gl->gl_tchange = jiffies;
537}
538
539static void gfs2_set_demote(int nr, struct gfs2_glock *gl)
540{
541 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
542
543 set_bit(nr, &gl->gl_flags);
544 smp_mb();
545 wake_up(&sdp->sd_async_glock_wait);
546}
547
548static void gfs2_demote_wake(struct gfs2_glock *gl)
549{
550 gl->gl_demote_state = LM_ST_EXCLUSIVE;
551 clear_bit(GLF_DEMOTE, &gl->gl_flags);
552 smp_mb__after_atomic();
553 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
554}
555
556/**
557 * finish_xmote - The DLM has replied to one of our lock requests
558 * @gl: The glock
559 * @ret: The status from the DLM
560 *
561 */
562
563static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
564{
565 const struct gfs2_glock_operations *glops = gl->gl_ops;
566
567 if (!(ret & ~LM_OUT_ST_MASK)) {
568 unsigned state = ret & LM_OUT_ST_MASK;
569
570 trace_gfs2_glock_state_change(gl, state);
571 state_change(gl, state);
572 }
573
574 /* Demote to UN request arrived during demote to SH or DF */
575 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
576 gl->gl_state != LM_ST_UNLOCKED &&
577 gl->gl_demote_state == LM_ST_UNLOCKED)
578 gl->gl_target = LM_ST_UNLOCKED;
579
580 /* Check for state != intended state */
581 if (unlikely(gl->gl_state != gl->gl_target)) {
582 struct gfs2_holder *gh = find_first_waiter(gl);
583
584 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
585 if (ret & LM_OUT_CANCELED) {
586 list_del_init(&gh->gh_list);
587 trace_gfs2_glock_queue(gh, 0);
588 gfs2_holder_wake(gh);
589 gl->gl_target = gl->gl_state;
590 goto out;
591 }
592 /* Some error or failed "try lock" - report it */
593 if ((ret & LM_OUT_ERROR) ||
594 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
595 gl->gl_target = gl->gl_state;
596 do_error(gl, ret);
597 goto out;
598 }
599 }
600 switch(gl->gl_state) {
601 /* Unlocked due to conversion deadlock, try again */
602 case LM_ST_UNLOCKED:
603 do_xmote(gl, gh, gl->gl_target);
604 break;
605 /* Conversion fails, unlock and try again */
606 case LM_ST_SHARED:
607 case LM_ST_DEFERRED:
608 do_xmote(gl, gh, LM_ST_UNLOCKED);
609 break;
610 default: /* Everything else */
611 fs_err(gl->gl_name.ln_sbd,
612 "glock %u:%llu requested=%u ret=%u\n",
613 gl->gl_name.ln_type, gl->gl_name.ln_number,
614 gl->gl_req, ret);
615 GLOCK_BUG_ON(gl, 1);
616 }
617 return;
618 }
619
620 /* Fast path - we got what we asked for */
621 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
622 clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
623 gfs2_demote_wake(gl);
624 }
625 if (gl->gl_state != LM_ST_UNLOCKED) {
626 if (glops->go_xmote_bh) {
627 int rv;
628
629 spin_unlock(&gl->gl_lockref.lock);
630 rv = glops->go_xmote_bh(gl);
631 spin_lock(&gl->gl_lockref.lock);
632 if (rv) {
633 do_error(gl, rv);
634 goto out;
635 }
636 }
637 do_promote(gl);
638 }
639out:
640 if (!test_bit(GLF_CANCELING, &gl->gl_flags))
641 clear_bit(GLF_LOCK, &gl->gl_flags);
642}
643
644/**
645 * do_xmote - Calls the DLM to change the state of a lock
646 * @gl: The lock state
647 * @gh: The holder (only for promotes)
648 * @target: The target lock state
649 *
650 */
651
652static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh,
653 unsigned int target)
654__releases(&gl->gl_lockref.lock)
655__acquires(&gl->gl_lockref.lock)
656{
657 const struct gfs2_glock_operations *glops = gl->gl_ops;
658 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
659 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
660 int ret;
661
662 /*
663 * When a filesystem is withdrawing, the remaining cluster nodes will
664 * take care of recovering the withdrawing node's journal. We only
665 * need to make sure that once we trigger remote recovery, we won't
666 * write to the shared block device anymore. This means that here,
667 *
668 * - no new writes to the filesystem must be triggered (->go_sync()).
669 *
670 * - any cached data should be discarded by calling ->go_inval(), dirty
671 * or not and journaled or unjournaled.
672 *
673 * - no more dlm locking operations should be issued (->lm_lock()).
674 */
675
676 GLOCK_BUG_ON(gl, gl->gl_state == target);
677 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
678
679 if (!glops->go_inval || !glops->go_sync)
680 goto skip_inval;
681
682 spin_unlock(&gl->gl_lockref.lock);
683 if (!gfs2_withdrawn(sdp)) {
684 ret = glops->go_sync(gl);
685 if (ret) {
686 if (cmpxchg(&sdp->sd_log_error, 0, ret)) {
687 fs_err(sdp, "Error %d syncing glock\n", ret);
688 gfs2_dump_glock(NULL, gl, true);
689 gfs2_withdraw(sdp);
690 }
691 }
692 }
693
694 if (target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED)
695 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
696 spin_lock(&gl->gl_lockref.lock);
697
698skip_inval:
699 if (gfs2_withdrawn(sdp)) {
700 if (target != LM_ST_UNLOCKED)
701 target = LM_OUT_ERROR;
702 goto out;
703 }
704
705 if (ls->ls_ops->lm_lock) {
706 set_bit(GLF_PENDING_REPLY, &gl->gl_flags);
707 spin_unlock(&gl->gl_lockref.lock);
708 ret = ls->ls_ops->lm_lock(gl, target, gh ? gh->gh_flags : 0);
709 spin_lock(&gl->gl_lockref.lock);
710
711 if (!ret) {
712 /* The operation will be completed asynchronously. */
713 gl->gl_lockref.count++;
714 return;
715 }
716 clear_bit(GLF_PENDING_REPLY, &gl->gl_flags);
717
718 if (ret == -ENODEV) {
719 /*
720 * The lockspace has been released and the lock has
721 * been unlocked implicitly.
722 */
723 if (target != LM_ST_UNLOCKED) {
724 target = LM_OUT_ERROR;
725 goto out;
726 }
727 } else {
728 fs_err(sdp, "lm_lock ret %d\n", ret);
729 GLOCK_BUG_ON(gl, !gfs2_withdrawn(sdp));
730 return;
731 }
732 }
733
734out:
735 /* Complete the operation now. */
736 finish_xmote(gl, target);
737 gl->gl_lockref.count++;
738 gfs2_glock_queue_work(gl, 0);
739}
740
741/**
742 * run_queue - do all outstanding tasks related to a glock
743 * @gl: The glock in question
744 * @nonblock: True if we must not block in run_queue
745 *
746 */
747
748static void run_queue(struct gfs2_glock *gl, const int nonblock)
749__releases(&gl->gl_lockref.lock)
750__acquires(&gl->gl_lockref.lock)
751{
752 struct gfs2_holder *gh;
753
754 if (test_bit(GLF_LOCK, &gl->gl_flags))
755 return;
756 set_bit(GLF_LOCK, &gl->gl_flags);
757
758 /*
759 * The GLF_DEMOTE_IN_PROGRESS flag is only set intermittently during
760 * locking operations. We have just started a locking operation by
761 * setting the GLF_LOCK flag, so the GLF_DEMOTE_IN_PROGRESS flag must
762 * be cleared.
763 */
764 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
765
766 if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
767 if (gl->gl_demote_state == gl->gl_state) {
768 gfs2_demote_wake(gl);
769 goto promote;
770 }
771
772 if (find_first_holder(gl))
773 goto out_unlock;
774 if (nonblock)
775 goto out_sched;
776 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
777 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
778 gl->gl_target = gl->gl_demote_state;
779 do_xmote(gl, NULL, gl->gl_target);
780 return;
781 }
782
783promote:
784 do_promote(gl);
785 if (find_first_holder(gl))
786 goto out_unlock;
787 gh = find_first_waiter(gl);
788 if (!gh)
789 goto out_unlock;
790 if (nonblock)
791 goto out_sched;
792 gl->gl_target = gh->gh_state;
793 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
794 do_error(gl, 0); /* Fail queued try locks */
795 do_xmote(gl, gh, gl->gl_target);
796 return;
797
798out_sched:
799 clear_bit(GLF_LOCK, &gl->gl_flags);
800 gl->gl_lockref.count++;
801 gfs2_glock_queue_work(gl, 0);
802 return;
803
804out_unlock:
805 clear_bit(GLF_LOCK, &gl->gl_flags);
806}
807
808/**
809 * glock_set_object - set the gl_object field of a glock
810 * @gl: the glock
811 * @object: the object
812 */
813void glock_set_object(struct gfs2_glock *gl, void *object)
814{
815 void *prev_object;
816
817 spin_lock(&gl->gl_lockref.lock);
818 prev_object = gl->gl_object;
819 gl->gl_object = object;
820 spin_unlock(&gl->gl_lockref.lock);
821 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == NULL))
822 gfs2_dump_glock(NULL, gl, true);
823}
824
825/**
826 * glock_clear_object - clear the gl_object field of a glock
827 * @gl: the glock
828 * @object: object the glock currently points at
829 */
830void glock_clear_object(struct gfs2_glock *gl, void *object)
831{
832 void *prev_object;
833
834 spin_lock(&gl->gl_lockref.lock);
835 prev_object = gl->gl_object;
836 gl->gl_object = NULL;
837 spin_unlock(&gl->gl_lockref.lock);
838 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == object))
839 gfs2_dump_glock(NULL, gl, true);
840}
841
842void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation)
843{
844 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
845
846 if (ri->ri_magic == 0)
847 ri->ri_magic = cpu_to_be32(GFS2_MAGIC);
848 if (ri->ri_magic == cpu_to_be32(GFS2_MAGIC))
849 ri->ri_generation_deleted = cpu_to_be64(generation);
850}
851
852bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation)
853{
854 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
855
856 if (ri->ri_magic != cpu_to_be32(GFS2_MAGIC))
857 return false;
858 return generation <= be64_to_cpu(ri->ri_generation_deleted);
859}
860
861static void gfs2_glock_poke(struct gfs2_glock *gl)
862{
863 int flags = LM_FLAG_TRY_1CB | LM_FLAG_ANY | GL_SKIP;
864 struct gfs2_holder gh;
865 int error;
866
867 __gfs2_holder_init(gl, LM_ST_SHARED, flags, &gh, _RET_IP_);
868 error = gfs2_glock_nq(&gh);
869 if (!error)
870 gfs2_glock_dq(&gh);
871 gfs2_holder_uninit(&gh);
872}
873
874static struct gfs2_inode *gfs2_grab_existing_inode(struct gfs2_glock *gl)
875{
876 struct gfs2_inode *ip;
877
878 spin_lock(&gl->gl_lockref.lock);
879 ip = gl->gl_object;
880 if (ip && !igrab(&ip->i_inode))
881 ip = NULL;
882 spin_unlock(&gl->gl_lockref.lock);
883 if (ip) {
884 wait_on_new_inode(&ip->i_inode);
885 if (is_bad_inode(&ip->i_inode)) {
886 iput(&ip->i_inode);
887 ip = NULL;
888 }
889 }
890 return ip;
891}
892
893static void gfs2_try_to_evict(struct gfs2_glock *gl)
894{
895 struct gfs2_inode *ip;
896
897 /*
898 * If there is contention on the iopen glock and we have an inode, try
899 * to grab and release the inode so that it can be evicted. The
900 * GLF_DEFER_DELETE flag indicates to gfs2_evict_inode() that the inode
901 * should not be deleted locally. This will allow the remote node to
902 * go ahead and delete the inode without us having to do it, which will
903 * avoid rgrp glock thrashing.
904 *
905 * The remote node is likely still holding the corresponding inode
906 * glock, so it will run before we get to verify that the delete has
907 * happened below. (Verification is triggered by the call to
908 * gfs2_queue_verify_delete() in gfs2_evict_inode().)
909 */
910 ip = gfs2_grab_existing_inode(gl);
911 if (ip) {
912 set_bit(GLF_DEFER_DELETE, &gl->gl_flags);
913 d_prune_aliases(&ip->i_inode);
914 iput(&ip->i_inode);
915 clear_bit(GLF_DEFER_DELETE, &gl->gl_flags);
916
917 /* If the inode was evicted, gl->gl_object will now be NULL. */
918 ip = gfs2_grab_existing_inode(gl);
919 if (ip) {
920 gfs2_glock_poke(ip->i_gl);
921 iput(&ip->i_inode);
922 }
923 }
924}
925
926bool gfs2_queue_try_to_evict(struct gfs2_glock *gl)
927{
928 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
929
930 if (test_and_set_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
931 return false;
932 return !mod_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, 0);
933}
934
935bool gfs2_queue_verify_delete(struct gfs2_glock *gl, bool later)
936{
937 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
938 unsigned long delay;
939
940 if (test_and_set_bit(GLF_VERIFY_DELETE, &gl->gl_flags))
941 return false;
942 delay = later ? HZ + get_random_long() % (HZ * 9) : 0;
943 return queue_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, delay);
944}
945
946static void delete_work_func(struct work_struct *work)
947{
948 struct delayed_work *dwork = to_delayed_work(work);
949 struct gfs2_glock *gl = container_of(dwork, struct gfs2_glock, gl_delete);
950 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
951 bool verify_delete = test_and_clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
952
953 /*
954 * Check for the GLF_VERIFY_DELETE above: this ensures that we won't
955 * immediately process GLF_VERIFY_DELETE work that the below call to
956 * gfs2_try_to_evict() queues.
957 */
958
959 if (test_and_clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
960 gfs2_try_to_evict(gl);
961
962 if (verify_delete) {
963 u64 no_addr = gl->gl_name.ln_number;
964 struct inode *inode;
965
966 inode = gfs2_lookup_by_inum(sdp, no_addr, gl->gl_no_formal_ino,
967 GFS2_BLKST_UNLINKED);
968 if (IS_ERR(inode)) {
969 if (PTR_ERR(inode) == -EAGAIN &&
970 !test_bit(SDF_KILL, &sdp->sd_flags) &&
971 gfs2_queue_verify_delete(gl, true))
972 return;
973 } else {
974 d_prune_aliases(inode);
975 iput(inode);
976 }
977 }
978
979 gfs2_glock_put(gl);
980}
981
982static void glock_work_func(struct work_struct *work)
983{
984 unsigned long delay = 0;
985 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
986 unsigned int drop_refs = 1;
987
988 spin_lock(&gl->gl_lockref.lock);
989 if (test_bit(GLF_HAVE_REPLY, &gl->gl_flags)) {
990 clear_bit(GLF_HAVE_REPLY, &gl->gl_flags);
991 finish_xmote(gl, gl->gl_reply);
992 drop_refs++;
993 }
994 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
995 gl->gl_state != LM_ST_UNLOCKED &&
996 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
997 if (gl->gl_name.ln_type == LM_TYPE_INODE) {
998 unsigned long holdtime, now = jiffies;
999
1000 holdtime = gl->gl_tchange + gl->gl_hold_time;
1001 if (time_before(now, holdtime))
1002 delay = holdtime - now;
1003 }
1004
1005 if (!delay) {
1006 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
1007 gfs2_set_demote(GLF_DEMOTE, gl);
1008 }
1009 }
1010 run_queue(gl, 0);
1011 if (delay) {
1012 /* Keep one glock reference for the work we requeue. */
1013 drop_refs--;
1014 gfs2_glock_queue_work(gl, delay);
1015 }
1016
1017 /* Drop the remaining glock references manually. */
1018 GLOCK_BUG_ON(gl, gl->gl_lockref.count < drop_refs);
1019 gl->gl_lockref.count -= drop_refs;
1020 if (!gl->gl_lockref.count) {
1021 if (gl->gl_state == LM_ST_UNLOCKED) {
1022 __gfs2_glock_put(gl);
1023 return;
1024 }
1025 gfs2_glock_add_to_lru(gl);
1026 }
1027 spin_unlock(&gl->gl_lockref.lock);
1028}
1029
1030static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
1031 struct gfs2_glock *new)
1032{
1033 struct wait_glock_queue wait;
1034 wait_queue_head_t *wq = glock_waitqueue(name);
1035 struct gfs2_glock *gl;
1036
1037 wait.name = name;
1038 init_wait(&wait.wait);
1039 wait.wait.func = glock_wake_function;
1040
1041again:
1042 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1043 rcu_read_lock();
1044 if (new) {
1045 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
1046 &new->gl_node, ht_parms);
1047 if (IS_ERR(gl))
1048 goto out;
1049 } else {
1050 gl = rhashtable_lookup_fast(&gl_hash_table,
1051 name, ht_parms);
1052 }
1053 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
1054 rcu_read_unlock();
1055 schedule();
1056 goto again;
1057 }
1058out:
1059 rcu_read_unlock();
1060 finish_wait(wq, &wait.wait);
1061 if (gl)
1062 gfs2_glock_remove_from_lru(gl);
1063 return gl;
1064}
1065
1066/**
1067 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
1068 * @sdp: The GFS2 superblock
1069 * @number: the lock number
1070 * @glops: The glock_operations to use
1071 * @create: If 0, don't create the glock if it doesn't exist
1072 * @glp: the glock is returned here
1073 *
1074 * This does not lock a glock, just finds/creates structures for one.
1075 *
1076 * Returns: errno
1077 */
1078
1079int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
1080 const struct gfs2_glock_operations *glops, int create,
1081 struct gfs2_glock **glp)
1082{
1083 struct lm_lockname name = { .ln_number = number,
1084 .ln_type = glops->go_type,
1085 .ln_sbd = sdp };
1086 struct gfs2_glock *gl, *tmp;
1087 struct address_space *mapping;
1088
1089 gl = find_insert_glock(&name, NULL);
1090 if (gl)
1091 goto found;
1092 if (!create)
1093 return -ENOENT;
1094
1095 if (glops->go_flags & GLOF_ASPACE) {
1096 struct gfs2_glock_aspace *gla =
1097 kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_NOFS);
1098 if (!gla)
1099 return -ENOMEM;
1100 gl = &gla->glock;
1101 } else {
1102 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_NOFS);
1103 if (!gl)
1104 return -ENOMEM;
1105 }
1106 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
1107 gl->gl_ops = glops;
1108
1109 if (glops->go_flags & GLOF_LVB) {
1110 gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
1111 if (!gl->gl_lksb.sb_lvbptr) {
1112 gfs2_glock_dealloc(&gl->gl_rcu);
1113 return -ENOMEM;
1114 }
1115 }
1116
1117 atomic_inc(&sdp->sd_glock_disposal);
1118 gl->gl_node.next = NULL;
1119 gl->gl_flags = BIT(GLF_INITIAL);
1120 if (glops->go_instantiate)
1121 gl->gl_flags |= BIT(GLF_INSTANTIATE_NEEDED);
1122 gl->gl_name = name;
1123 lockref_init(&gl->gl_lockref);
1124 lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
1125 gl->gl_state = LM_ST_UNLOCKED;
1126 gl->gl_target = LM_ST_UNLOCKED;
1127 gl->gl_demote_state = LM_ST_EXCLUSIVE;
1128 gl->gl_dstamp = 0;
1129 preempt_disable();
1130 /* We use the global stats to estimate the initial per-glock stats */
1131 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
1132 preempt_enable();
1133 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
1134 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
1135 gl->gl_tchange = jiffies;
1136 gl->gl_object = NULL;
1137 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
1138 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
1139 if (gl->gl_name.ln_type == LM_TYPE_IOPEN)
1140 INIT_DELAYED_WORK(&gl->gl_delete, delete_work_func);
1141
1142 mapping = gfs2_glock2aspace(gl);
1143 if (mapping) {
1144 gfp_t gfp_mask;
1145
1146 mapping->a_ops = &gfs2_meta_aops;
1147 mapping->host = sdp->sd_inode;
1148 mapping->flags = 0;
1149 gfp_mask = mapping_gfp_mask(sdp->sd_inode->i_mapping);
1150 mapping_set_gfp_mask(mapping, gfp_mask);
1151 mapping->i_private_data = NULL;
1152 mapping->writeback_index = 0;
1153 }
1154
1155 tmp = find_insert_glock(&name, gl);
1156 if (tmp) {
1157 gfs2_glock_dealloc(&gl->gl_rcu);
1158 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
1159 wake_up(&sdp->sd_kill_wait);
1160
1161 if (IS_ERR(tmp))
1162 return PTR_ERR(tmp);
1163 gl = tmp;
1164 }
1165
1166found:
1167 *glp = gl;
1168 return 0;
1169}
1170
1171/**
1172 * __gfs2_holder_init - initialize a struct gfs2_holder in the default way
1173 * @gl: the glock
1174 * @state: the state we're requesting
1175 * @flags: the modifier flags
1176 * @gh: the holder structure
1177 * @ip: caller's return address for debugging
1178 */
1179
1180void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
1181 struct gfs2_holder *gh, unsigned long ip)
1182{
1183 INIT_LIST_HEAD(&gh->gh_list);
1184 gh->gh_gl = gfs2_glock_hold(gl);
1185 gh->gh_ip = ip;
1186 gh->gh_owner_pid = get_pid(task_pid(current));
1187 gh->gh_state = state;
1188 gh->gh_flags = flags;
1189 gh->gh_iflags = 0;
1190}
1191
1192/**
1193 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
1194 * @state: the state we're requesting
1195 * @flags: the modifier flags
1196 * @gh: the holder structure
1197 *
1198 * Don't mess with the glock.
1199 *
1200 */
1201
1202void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
1203{
1204 gh->gh_state = state;
1205 gh->gh_flags = flags;
1206 gh->gh_iflags = 0;
1207 gh->gh_ip = _RET_IP_;
1208 put_pid(gh->gh_owner_pid);
1209 gh->gh_owner_pid = get_pid(task_pid(current));
1210}
1211
1212/**
1213 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
1214 * @gh: the holder structure
1215 *
1216 */
1217
1218void gfs2_holder_uninit(struct gfs2_holder *gh)
1219{
1220 put_pid(gh->gh_owner_pid);
1221 gfs2_glock_put(gh->gh_gl);
1222 gfs2_holder_mark_uninitialized(gh);
1223 gh->gh_ip = 0;
1224}
1225
1226static void gfs2_glock_update_hold_time(struct gfs2_glock *gl,
1227 unsigned long start_time)
1228{
1229 /* Have we waited longer that a second? */
1230 if (time_after(jiffies, start_time + HZ)) {
1231 /* Lengthen the minimum hold time. */
1232 gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR,
1233 GL_GLOCK_MAX_HOLD);
1234 }
1235}
1236
1237/**
1238 * gfs2_glock_holder_ready - holder is ready and its error code can be collected
1239 * @gh: the glock holder
1240 *
1241 * Called when a glock holder no longer needs to be waited for because it is
1242 * now either held (HIF_HOLDER set; gh_error == 0), or acquiring the lock has
1243 * failed (gh_error != 0).
1244 */
1245
1246int gfs2_glock_holder_ready(struct gfs2_holder *gh)
1247{
1248 if (gh->gh_error || (gh->gh_flags & GL_SKIP))
1249 return gh->gh_error;
1250 gh->gh_error = gfs2_instantiate(gh);
1251 if (gh->gh_error)
1252 gfs2_glock_dq(gh);
1253 return gh->gh_error;
1254}
1255
1256/**
1257 * gfs2_glock_wait - wait on a glock acquisition
1258 * @gh: the glock holder
1259 *
1260 * Returns: 0 on success
1261 */
1262
1263int gfs2_glock_wait(struct gfs2_holder *gh)
1264{
1265 unsigned long start_time = jiffies;
1266
1267 might_sleep();
1268 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1269 gfs2_glock_update_hold_time(gh->gh_gl, start_time);
1270 return gfs2_glock_holder_ready(gh);
1271}
1272
1273static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs)
1274{
1275 int i;
1276
1277 for (i = 0; i < num_gh; i++)
1278 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags))
1279 return 1;
1280 return 0;
1281}
1282
1283/**
1284 * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions
1285 * @num_gh: the number of holders in the array
1286 * @ghs: the glock holder array
1287 *
1288 * Returns: 0 on success, meaning all glocks have been granted and are held.
1289 * -ESTALE if the request timed out, meaning all glocks were released,
1290 * and the caller should retry the operation.
1291 */
1292
1293int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs)
1294{
1295 struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd;
1296 int i, ret = 0, timeout = 0;
1297 unsigned long start_time = jiffies;
1298
1299 might_sleep();
1300 /*
1301 * Total up the (minimum hold time * 2) of all glocks and use that to
1302 * determine the max amount of time we should wait.
1303 */
1304 for (i = 0; i < num_gh; i++)
1305 timeout += ghs[i].gh_gl->gl_hold_time << 1;
1306
1307 if (!wait_event_timeout(sdp->sd_async_glock_wait,
1308 !glocks_pending(num_gh, ghs), timeout)) {
1309 ret = -ESTALE; /* request timed out. */
1310 goto out;
1311 }
1312
1313 for (i = 0; i < num_gh; i++) {
1314 struct gfs2_holder *gh = &ghs[i];
1315 int ret2;
1316
1317 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1318 gfs2_glock_update_hold_time(gh->gh_gl,
1319 start_time);
1320 }
1321 ret2 = gfs2_glock_holder_ready(gh);
1322 if (!ret)
1323 ret = ret2;
1324 }
1325
1326out:
1327 if (ret) {
1328 for (i = 0; i < num_gh; i++) {
1329 struct gfs2_holder *gh = &ghs[i];
1330
1331 gfs2_glock_dq(gh);
1332 }
1333 }
1334 return ret;
1335}
1336
1337/**
1338 * request_demote - process a demote request
1339 * @gl: the glock
1340 * @state: the state the caller wants us to change to
1341 * @delay: zero to demote immediately; otherwise pending demote
1342 * @remote: true if this came from a different cluster node
1343 *
1344 * There are only two requests that we are going to see in actual
1345 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
1346 */
1347
1348static void request_demote(struct gfs2_glock *gl, unsigned int state,
1349 unsigned long delay, bool remote)
1350{
1351 gfs2_set_demote(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, gl);
1352 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
1353 gl->gl_demote_state = state;
1354 gl->gl_demote_time = jiffies;
1355 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
1356 gl->gl_demote_state != state) {
1357 gl->gl_demote_state = LM_ST_UNLOCKED;
1358 }
1359 if (gl->gl_ops->go_callback)
1360 gl->gl_ops->go_callback(gl, remote);
1361 trace_gfs2_demote_rq(gl, remote);
1362}
1363
1364void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
1365{
1366 struct va_format vaf;
1367 va_list args;
1368
1369 va_start(args, fmt);
1370
1371 if (seq) {
1372 seq_vprintf(seq, fmt, args);
1373 } else {
1374 vaf.fmt = fmt;
1375 vaf.va = &args;
1376
1377 pr_err("%pV", &vaf);
1378 }
1379
1380 va_end(args);
1381}
1382
1383static bool gfs2_should_queue_trylock(struct gfs2_glock *gl,
1384 struct gfs2_holder *gh)
1385{
1386 struct gfs2_holder *current_gh, *gh2;
1387
1388 current_gh = find_first_holder(gl);
1389 if (current_gh && !may_grant(gl, current_gh, gh))
1390 return false;
1391
1392 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1393 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1394 continue;
1395 if (!(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
1396 return false;
1397 }
1398 return true;
1399}
1400
1401static inline bool pid_is_meaningful(const struct gfs2_holder *gh)
1402{
1403 if (!(gh->gh_flags & GL_NOPID))
1404 return true;
1405 return !test_bit(HIF_HOLDER, &gh->gh_iflags);
1406}
1407
1408/**
1409 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1410 * @gh: the holder structure to add
1411 *
1412 * Eventually we should move the recursive locking trap to a
1413 * debugging option or something like that. This is the fast
1414 * path and needs to have the minimum number of distractions.
1415 *
1416 */
1417
1418static inline void add_to_queue(struct gfs2_holder *gh)
1419{
1420 struct gfs2_glock *gl = gh->gh_gl;
1421 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1422 struct gfs2_holder *gh2;
1423
1424 GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL);
1425 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1426 GLOCK_BUG_ON(gl, true);
1427
1428 if ((gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
1429 !gfs2_should_queue_trylock(gl, gh)) {
1430 gh->gh_error = GLR_TRYFAILED;
1431 gfs2_holder_wake(gh);
1432 return;
1433 }
1434
1435 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1436 if (likely(gh2->gh_owner_pid != gh->gh_owner_pid))
1437 continue;
1438 if (gh->gh_gl->gl_ops->go_type == LM_TYPE_FLOCK)
1439 continue;
1440 if (!pid_is_meaningful(gh2))
1441 continue;
1442 goto trap_recursive;
1443 }
1444 trace_gfs2_glock_queue(gh, 1);
1445 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1446 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
1447 list_add_tail(&gh->gh_list, &gl->gl_holders);
1448 return;
1449
1450trap_recursive:
1451 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1452 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1453 fs_err(sdp, "lock type: %d req lock state : %d\n",
1454 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1455 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1456 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1457 fs_err(sdp, "lock type: %d req lock state : %d\n",
1458 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1459 gfs2_dump_glock(NULL, gl, true);
1460 BUG();
1461}
1462
1463/**
1464 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1465 * @gh: the holder structure
1466 *
1467 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1468 *
1469 * Returns: 0, GLR_TRYFAILED, or errno on failure
1470 */
1471
1472int gfs2_glock_nq(struct gfs2_holder *gh)
1473{
1474 struct gfs2_glock *gl = gh->gh_gl;
1475 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1476 int error;
1477
1478 if (gfs2_withdrawn(sdp))
1479 return -EIO;
1480
1481 if (gh->gh_flags & GL_NOBLOCK) {
1482 struct gfs2_holder *current_gh;
1483
1484 error = -ECHILD;
1485 spin_lock(&gl->gl_lockref.lock);
1486 if (find_last_waiter(gl))
1487 goto unlock;
1488 current_gh = find_first_holder(gl);
1489 if (!may_grant(gl, current_gh, gh))
1490 goto unlock;
1491 set_bit(HIF_HOLDER, &gh->gh_iflags);
1492 list_add_tail(&gh->gh_list, &gl->gl_holders);
1493 trace_gfs2_promote(gh);
1494 error = 0;
1495unlock:
1496 spin_unlock(&gl->gl_lockref.lock);
1497 return error;
1498 }
1499
1500 gh->gh_error = 0;
1501 spin_lock(&gl->gl_lockref.lock);
1502 add_to_queue(gh);
1503 if (unlikely((LM_FLAG_RECOVER & gh->gh_flags) &&
1504 test_and_clear_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags))) {
1505 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1506 gl->gl_lockref.count++;
1507 gfs2_glock_queue_work(gl, 0);
1508 }
1509 run_queue(gl, 1);
1510 spin_unlock(&gl->gl_lockref.lock);
1511
1512 error = 0;
1513 if (!(gh->gh_flags & GL_ASYNC))
1514 error = gfs2_glock_wait(gh);
1515
1516 return error;
1517}
1518
1519/**
1520 * gfs2_glock_poll - poll to see if an async request has been completed
1521 * @gh: the holder
1522 *
1523 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1524 */
1525
1526int gfs2_glock_poll(struct gfs2_holder *gh)
1527{
1528 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
1529}
1530
1531static void __gfs2_glock_dq(struct gfs2_holder *gh)
1532{
1533 struct gfs2_glock *gl = gh->gh_gl;
1534 unsigned delay = 0;
1535 int fast_path = 0;
1536
1537 /*
1538 * This holder should not be cached, so mark it for demote.
1539 * Note: this should be done before the glock_needs_demote
1540 * check below.
1541 */
1542 if (gh->gh_flags & GL_NOCACHE)
1543 request_demote(gl, LM_ST_UNLOCKED, 0, false);
1544
1545 list_del_init(&gh->gh_list);
1546 clear_bit(HIF_HOLDER, &gh->gh_iflags);
1547 trace_gfs2_glock_queue(gh, 0);
1548
1549 /*
1550 * If there hasn't been a demote request we are done.
1551 * (Let the remaining holders, if any, keep holding it.)
1552 */
1553 if (!glock_needs_demote(gl)) {
1554 if (list_empty(&gl->gl_holders))
1555 fast_path = 1;
1556 }
1557
1558 if (unlikely(!fast_path)) {
1559 gl->gl_lockref.count++;
1560 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1561 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1562 gl->gl_name.ln_type == LM_TYPE_INODE)
1563 delay = gl->gl_hold_time;
1564 gfs2_glock_queue_work(gl, delay);
1565 }
1566}
1567
1568/**
1569 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1570 * @gh: the glock holder
1571 *
1572 */
1573void gfs2_glock_dq(struct gfs2_holder *gh)
1574{
1575 struct gfs2_glock *gl = gh->gh_gl;
1576
1577 spin_lock(&gl->gl_lockref.lock);
1578 if (!gfs2_holder_queued(gh)) {
1579 /*
1580 * May have already been dequeued because the locking request
1581 * was GL_ASYNC and it has failed in the meantime.
1582 */
1583 goto out;
1584 }
1585
1586 if (list_is_first(&gh->gh_list, &gl->gl_holders) &&
1587 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1588 test_bit(GLF_LOCK, &gl->gl_flags) &&
1589 !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
1590 !test_bit(GLF_CANCELING, &gl->gl_flags)) {
1591 set_bit(GLF_CANCELING, &gl->gl_flags);
1592 spin_unlock(&gl->gl_lockref.lock);
1593 gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl);
1594 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1595 spin_lock(&gl->gl_lockref.lock);
1596 clear_bit(GLF_CANCELING, &gl->gl_flags);
1597 clear_bit(GLF_LOCK, &gl->gl_flags);
1598 if (!gfs2_holder_queued(gh))
1599 goto out;
1600 }
1601
1602 __gfs2_glock_dq(gh);
1603out:
1604 spin_unlock(&gl->gl_lockref.lock);
1605}
1606
1607void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1608{
1609 struct gfs2_glock *gl = gh->gh_gl;
1610 gfs2_glock_dq(gh);
1611 might_sleep();
1612 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
1613}
1614
1615/**
1616 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1617 * @gh: the holder structure
1618 *
1619 */
1620
1621void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1622{
1623 gfs2_glock_dq(gh);
1624 gfs2_holder_uninit(gh);
1625}
1626
1627/**
1628 * gfs2_glock_nq_num - acquire a glock based on lock number
1629 * @sdp: the filesystem
1630 * @number: the lock number
1631 * @glops: the glock operations for the type of glock
1632 * @state: the state to acquire the glock in
1633 * @flags: modifier flags for the acquisition
1634 * @gh: the struct gfs2_holder
1635 *
1636 * Returns: errno
1637 */
1638
1639int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1640 const struct gfs2_glock_operations *glops,
1641 unsigned int state, u16 flags, struct gfs2_holder *gh)
1642{
1643 struct gfs2_glock *gl;
1644 int error;
1645
1646 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1647 if (!error) {
1648 error = gfs2_glock_nq_init(gl, state, flags, gh);
1649 gfs2_glock_put(gl);
1650 }
1651
1652 return error;
1653}
1654
1655/**
1656 * glock_compare - Compare two struct gfs2_glock structures for sorting
1657 * @arg_a: the first structure
1658 * @arg_b: the second structure
1659 *
1660 */
1661
1662static int glock_compare(const void *arg_a, const void *arg_b)
1663{
1664 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1665 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1666 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1667 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1668
1669 if (a->ln_number > b->ln_number)
1670 return 1;
1671 if (a->ln_number < b->ln_number)
1672 return -1;
1673 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1674 return 0;
1675}
1676
1677/**
1678 * nq_m_sync - synchronously acquire more than one glock in deadlock free order
1679 * @num_gh: the number of structures
1680 * @ghs: an array of struct gfs2_holder structures
1681 * @p: placeholder for the holder structure to pass back
1682 *
1683 * Returns: 0 on success (all glocks acquired),
1684 * errno on failure (no glocks acquired)
1685 */
1686
1687static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1688 struct gfs2_holder **p)
1689{
1690 unsigned int x;
1691 int error = 0;
1692
1693 for (x = 0; x < num_gh; x++)
1694 p[x] = &ghs[x];
1695
1696 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1697
1698 for (x = 0; x < num_gh; x++) {
1699 error = gfs2_glock_nq(p[x]);
1700 if (error) {
1701 while (x--)
1702 gfs2_glock_dq(p[x]);
1703 break;
1704 }
1705 }
1706
1707 return error;
1708}
1709
1710/**
1711 * gfs2_glock_nq_m - acquire multiple glocks
1712 * @num_gh: the number of structures
1713 * @ghs: an array of struct gfs2_holder structures
1714 *
1715 * Returns: 0 on success (all glocks acquired),
1716 * errno on failure (no glocks acquired)
1717 */
1718
1719int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1720{
1721 struct gfs2_holder *tmp[4];
1722 struct gfs2_holder **pph = tmp;
1723 int error = 0;
1724
1725 switch(num_gh) {
1726 case 0:
1727 return 0;
1728 case 1:
1729 return gfs2_glock_nq(ghs);
1730 default:
1731 if (num_gh <= 4)
1732 break;
1733 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1734 GFP_NOFS);
1735 if (!pph)
1736 return -ENOMEM;
1737 }
1738
1739 error = nq_m_sync(num_gh, ghs, pph);
1740
1741 if (pph != tmp)
1742 kfree(pph);
1743
1744 return error;
1745}
1746
1747/**
1748 * gfs2_glock_dq_m - release multiple glocks
1749 * @num_gh: the number of structures
1750 * @ghs: an array of struct gfs2_holder structures
1751 *
1752 */
1753
1754void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1755{
1756 while (num_gh--)
1757 gfs2_glock_dq(&ghs[num_gh]);
1758}
1759
1760void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
1761{
1762 unsigned long delay = 0;
1763
1764 gfs2_glock_hold(gl);
1765 spin_lock(&gl->gl_lockref.lock);
1766 if (!list_empty(&gl->gl_holders) &&
1767 gl->gl_name.ln_type == LM_TYPE_INODE) {
1768 unsigned long now = jiffies;
1769 unsigned long holdtime;
1770
1771 holdtime = gl->gl_tchange + gl->gl_hold_time;
1772
1773 if (time_before(now, holdtime))
1774 delay = holdtime - now;
1775 if (test_bit(GLF_HAVE_REPLY, &gl->gl_flags))
1776 delay = gl->gl_hold_time;
1777 }
1778 request_demote(gl, state, delay, true);
1779 gfs2_glock_queue_work(gl, delay);
1780 spin_unlock(&gl->gl_lockref.lock);
1781}
1782
1783/**
1784 * gfs2_should_freeze - Figure out if glock should be frozen
1785 * @gl: The glock in question
1786 *
1787 * Glocks are not frozen if (a) the result of the dlm operation is
1788 * an error, (b) the locking operation was an unlock operation or
1789 * (c) if there is a "recover" flagged request anywhere in the queue
1790 *
1791 * Returns: 1 if freezing should occur, 0 otherwise
1792 */
1793
1794static int gfs2_should_freeze(const struct gfs2_glock *gl)
1795{
1796 const struct gfs2_holder *gh;
1797
1798 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1799 return 0;
1800 if (gl->gl_target == LM_ST_UNLOCKED)
1801 return 0;
1802
1803 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1804 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1805 continue;
1806 if (LM_FLAG_RECOVER & gh->gh_flags)
1807 return 0;
1808 }
1809
1810 return 1;
1811}
1812
1813/**
1814 * gfs2_glock_complete - Callback used by locking
1815 * @gl: Pointer to the glock
1816 * @ret: The return value from the dlm
1817 *
1818 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
1819 * to use a bitfield shared with other glock state fields.
1820 */
1821
1822void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
1823{
1824 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
1825
1826 spin_lock(&gl->gl_lockref.lock);
1827 clear_bit(GLF_PENDING_REPLY, &gl->gl_flags);
1828 gl->gl_reply = ret;
1829
1830 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
1831 if (gfs2_should_freeze(gl)) {
1832 set_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags);
1833 spin_unlock(&gl->gl_lockref.lock);
1834 return;
1835 }
1836 }
1837
1838 gl->gl_lockref.count++;
1839 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1840 gfs2_glock_queue_work(gl, 0);
1841 spin_unlock(&gl->gl_lockref.lock);
1842}
1843
1844static int glock_cmp(void *priv, const struct list_head *a,
1845 const struct list_head *b)
1846{
1847 struct gfs2_glock *gla, *glb;
1848
1849 gla = list_entry(a, struct gfs2_glock, gl_lru);
1850 glb = list_entry(b, struct gfs2_glock, gl_lru);
1851
1852 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1853 return 1;
1854 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1855 return -1;
1856
1857 return 0;
1858}
1859
1860static bool can_free_glock(struct gfs2_glock *gl)
1861{
1862 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1863
1864 return !test_bit(GLF_LOCK, &gl->gl_flags) &&
1865 !gl->gl_lockref.count &&
1866 (!test_bit(GLF_LFLUSH, &gl->gl_flags) ||
1867 test_bit(SDF_KILL, &sdp->sd_flags));
1868}
1869
1870/**
1871 * gfs2_dispose_glock_lru - Demote a list of glocks
1872 * @list: The list to dispose of
1873 *
1874 * Disposing of glocks may involve disk accesses, so that here we sort
1875 * the glocks by number (i.e. disk location of the inodes) so that if
1876 * there are any such accesses, they'll be sent in order (mostly).
1877 *
1878 * Must be called under the lru_lock, but may drop and retake this
1879 * lock. While the lru_lock is dropped, entries may vanish from the
1880 * list, but no new entries will appear on the list (since it is
1881 * private)
1882 */
1883
1884static unsigned long gfs2_dispose_glock_lru(struct list_head *list)
1885__releases(&lru_lock)
1886__acquires(&lru_lock)
1887{
1888 struct gfs2_glock *gl;
1889 unsigned long freed = 0;
1890
1891 list_sort(NULL, list, glock_cmp);
1892
1893 while(!list_empty(list)) {
1894 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
1895 if (!spin_trylock(&gl->gl_lockref.lock)) {
1896add_back_to_lru:
1897 list_move(&gl->gl_lru, &lru_list);
1898 continue;
1899 }
1900 if (!can_free_glock(gl)) {
1901 spin_unlock(&gl->gl_lockref.lock);
1902 goto add_back_to_lru;
1903 }
1904 list_del_init(&gl->gl_lru);
1905 atomic_dec(&lru_count);
1906 clear_bit(GLF_LRU, &gl->gl_flags);
1907 freed++;
1908 gl->gl_lockref.count++;
1909 if (gl->gl_state != LM_ST_UNLOCKED)
1910 request_demote(gl, LM_ST_UNLOCKED, 0, false);
1911 gfs2_glock_queue_work(gl, 0);
1912 spin_unlock(&gl->gl_lockref.lock);
1913 cond_resched_lock(&lru_lock);
1914 }
1915 return freed;
1916}
1917
1918/**
1919 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1920 * @nr: The number of entries to scan
1921 *
1922 * This function selects the entries on the LRU which are able to
1923 * be demoted, and then kicks off the process by calling
1924 * gfs2_dispose_glock_lru() above.
1925 */
1926
1927static unsigned long gfs2_scan_glock_lru(unsigned long nr)
1928{
1929 struct gfs2_glock *gl, *next;
1930 LIST_HEAD(dispose);
1931 unsigned long freed = 0;
1932
1933 spin_lock(&lru_lock);
1934 list_for_each_entry_safe(gl, next, &lru_list, gl_lru) {
1935 if (!nr--)
1936 break;
1937 if (can_free_glock(gl))
1938 list_move(&gl->gl_lru, &dispose);
1939 }
1940 if (!list_empty(&dispose))
1941 freed = gfs2_dispose_glock_lru(&dispose);
1942 spin_unlock(&lru_lock);
1943
1944 return freed;
1945}
1946
1947static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1948 struct shrink_control *sc)
1949{
1950 if (!(sc->gfp_mask & __GFP_FS))
1951 return SHRINK_STOP;
1952 return gfs2_scan_glock_lru(sc->nr_to_scan);
1953}
1954
1955static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1956 struct shrink_control *sc)
1957{
1958 return vfs_pressure_ratio(atomic_read(&lru_count));
1959}
1960
1961static struct shrinker *glock_shrinker;
1962
1963/**
1964 * glock_hash_walk - Call a function for glock in a hash bucket
1965 * @examiner: the function
1966 * @sdp: the filesystem
1967 *
1968 * Note that the function can be called multiple times on the same
1969 * object. So the user must ensure that the function can cope with
1970 * that.
1971 */
1972
1973static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1974{
1975 struct gfs2_glock *gl;
1976 struct rhashtable_iter iter;
1977
1978 rhashtable_walk_enter(&gl_hash_table, &iter);
1979
1980 do {
1981 rhashtable_walk_start(&iter);
1982
1983 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) {
1984 if (gl->gl_name.ln_sbd == sdp)
1985 examiner(gl);
1986 }
1987
1988 rhashtable_walk_stop(&iter);
1989 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1990
1991 rhashtable_walk_exit(&iter);
1992}
1993
1994void gfs2_cancel_delete_work(struct gfs2_glock *gl)
1995{
1996 clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags);
1997 clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
1998 if (cancel_delayed_work(&gl->gl_delete))
1999 gfs2_glock_put(gl);
2000}
2001
2002static void flush_delete_work(struct gfs2_glock *gl)
2003{
2004 if (gl->gl_name.ln_type == LM_TYPE_IOPEN) {
2005 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2006
2007 if (cancel_delayed_work(&gl->gl_delete)) {
2008 queue_delayed_work(sdp->sd_delete_wq,
2009 &gl->gl_delete, 0);
2010 }
2011 }
2012}
2013
2014void gfs2_flush_delete_work(struct gfs2_sbd *sdp)
2015{
2016 glock_hash_walk(flush_delete_work, sdp);
2017 flush_workqueue(sdp->sd_delete_wq);
2018}
2019
2020/**
2021 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
2022 * @gl: The glock to thaw
2023 *
2024 */
2025
2026static void thaw_glock(struct gfs2_glock *gl)
2027{
2028 if (!test_and_clear_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags))
2029 return;
2030 if (!lockref_get_not_dead(&gl->gl_lockref))
2031 return;
2032
2033 gfs2_glock_remove_from_lru(gl);
2034 spin_lock(&gl->gl_lockref.lock);
2035 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
2036 gfs2_glock_queue_work(gl, 0);
2037 spin_unlock(&gl->gl_lockref.lock);
2038}
2039
2040/**
2041 * clear_glock - look at a glock and see if we can free it from glock cache
2042 * @gl: the glock to look at
2043 *
2044 */
2045
2046static void clear_glock(struct gfs2_glock *gl)
2047{
2048 gfs2_glock_remove_from_lru(gl);
2049
2050 spin_lock(&gl->gl_lockref.lock);
2051 if (!__lockref_is_dead(&gl->gl_lockref)) {
2052 gl->gl_lockref.count++;
2053 if (gl->gl_state != LM_ST_UNLOCKED)
2054 request_demote(gl, LM_ST_UNLOCKED, 0, false);
2055 gfs2_glock_queue_work(gl, 0);
2056 }
2057 spin_unlock(&gl->gl_lockref.lock);
2058}
2059
2060/**
2061 * gfs2_glock_thaw - Thaw any frozen glocks
2062 * @sdp: The super block
2063 *
2064 */
2065
2066void gfs2_glock_thaw(struct gfs2_sbd *sdp)
2067{
2068 glock_hash_walk(thaw_glock, sdp);
2069}
2070
2071static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2072{
2073 spin_lock(&gl->gl_lockref.lock);
2074 gfs2_dump_glock(seq, gl, fsid);
2075 spin_unlock(&gl->gl_lockref.lock);
2076}
2077
2078static void dump_glock_func(struct gfs2_glock *gl)
2079{
2080 dump_glock(NULL, gl, true);
2081}
2082
2083static void withdraw_glock(struct gfs2_glock *gl)
2084{
2085 spin_lock(&gl->gl_lockref.lock);
2086 if (!__lockref_is_dead(&gl->gl_lockref)) {
2087 /*
2088 * We don't want to write back any more dirty data. Unlock the
2089 * remaining inode and resource group glocks; this will cause
2090 * their ->go_inval() hooks to toss out all the remaining
2091 * cached data, dirty or not.
2092 */
2093 if (gl->gl_ops->go_inval && gl->gl_state != LM_ST_UNLOCKED)
2094 request_demote(gl, LM_ST_UNLOCKED, 0, false);
2095 do_error(gl, LM_OUT_ERROR); /* remove pending waiters */
2096 }
2097 spin_unlock(&gl->gl_lockref.lock);
2098}
2099
2100void gfs2_withdraw_glocks(struct gfs2_sbd *sdp)
2101{
2102 glock_hash_walk(withdraw_glock, sdp);
2103}
2104
2105/**
2106 * gfs2_gl_hash_clear - Empty out the glock hash table
2107 * @sdp: the filesystem
2108 *
2109 * Called when unmounting the filesystem.
2110 */
2111
2112void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
2113{
2114 unsigned long start = jiffies;
2115 bool timed_out = false;
2116
2117 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
2118 flush_workqueue(sdp->sd_glock_wq);
2119 glock_hash_walk(clear_glock, sdp);
2120 flush_workqueue(sdp->sd_glock_wq);
2121
2122 while (!timed_out) {
2123 wait_event_timeout(sdp->sd_kill_wait,
2124 !atomic_read(&sdp->sd_glock_disposal),
2125 HZ * 60);
2126 if (!atomic_read(&sdp->sd_glock_disposal))
2127 break;
2128 timed_out = time_after(jiffies, start + (HZ * 600));
2129 fs_warn(sdp, "%u glocks left after %u seconds%s\n",
2130 atomic_read(&sdp->sd_glock_disposal),
2131 jiffies_to_msecs(jiffies - start) / 1000,
2132 timed_out ? ":" : "; still waiting");
2133 }
2134 gfs2_lm_unmount(sdp);
2135 gfs2_free_dead_glocks(sdp);
2136 glock_hash_walk(dump_glock_func, sdp);
2137 destroy_workqueue(sdp->sd_glock_wq);
2138 sdp->sd_glock_wq = NULL;
2139}
2140
2141static const char *state2str(unsigned state)
2142{
2143 switch(state) {
2144 case LM_ST_UNLOCKED:
2145 return "UN";
2146 case LM_ST_SHARED:
2147 return "SH";
2148 case LM_ST_DEFERRED:
2149 return "DF";
2150 case LM_ST_EXCLUSIVE:
2151 return "EX";
2152 }
2153 return "??";
2154}
2155
2156static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
2157{
2158 char *p = buf;
2159 if (flags & LM_FLAG_TRY)
2160 *p++ = 't';
2161 if (flags & LM_FLAG_TRY_1CB)
2162 *p++ = 'T';
2163 if (flags & LM_FLAG_RECOVER)
2164 *p++ = 'e';
2165 if (flags & LM_FLAG_ANY)
2166 *p++ = 'A';
2167 if (flags & LM_FLAG_NODE_SCOPE)
2168 *p++ = 'n';
2169 if (flags & GL_ASYNC)
2170 *p++ = 'a';
2171 if (flags & GL_EXACT)
2172 *p++ = 'E';
2173 if (flags & GL_NOCACHE)
2174 *p++ = 'c';
2175 if (test_bit(HIF_HOLDER, &iflags))
2176 *p++ = 'H';
2177 if (test_bit(HIF_WAIT, &iflags))
2178 *p++ = 'W';
2179 if (flags & GL_SKIP)
2180 *p++ = 's';
2181 *p = 0;
2182 return buf;
2183}
2184
2185/**
2186 * dump_holder - print information about a glock holder
2187 * @seq: the seq_file struct
2188 * @gh: the glock holder
2189 * @fs_id_buf: pointer to file system id (if requested)
2190 *
2191 */
2192
2193static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh,
2194 const char *fs_id_buf)
2195{
2196 const char *comm = "(none)";
2197 pid_t owner_pid = 0;
2198 char flags_buf[32];
2199
2200 rcu_read_lock();
2201 if (pid_is_meaningful(gh)) {
2202 struct task_struct *gh_owner;
2203
2204 comm = "(ended)";
2205 owner_pid = pid_nr(gh->gh_owner_pid);
2206 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
2207 if (gh_owner)
2208 comm = gh_owner->comm;
2209 }
2210 gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
2211 fs_id_buf, state2str(gh->gh_state),
2212 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
2213 gh->gh_error, (long)owner_pid, comm, (void *)gh->gh_ip);
2214 rcu_read_unlock();
2215}
2216
2217static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
2218{
2219 const unsigned long *gflags = &gl->gl_flags;
2220 char *p = buf;
2221
2222 if (test_bit(GLF_LOCK, gflags))
2223 *p++ = 'l';
2224 if (test_bit(GLF_DEMOTE, gflags))
2225 *p++ = 'D';
2226 if (test_bit(GLF_PENDING_DEMOTE, gflags))
2227 *p++ = 'd';
2228 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
2229 *p++ = 'p';
2230 if (test_bit(GLF_DIRTY, gflags))
2231 *p++ = 'y';
2232 if (test_bit(GLF_LFLUSH, gflags))
2233 *p++ = 'f';
2234 if (test_bit(GLF_PENDING_REPLY, gflags))
2235 *p++ = 'R';
2236 if (test_bit(GLF_HAVE_REPLY, gflags))
2237 *p++ = 'r';
2238 if (test_bit(GLF_INITIAL, gflags))
2239 *p++ = 'a';
2240 if (test_bit(GLF_HAVE_FROZEN_REPLY, gflags))
2241 *p++ = 'F';
2242 if (!list_empty(&gl->gl_holders))
2243 *p++ = 'q';
2244 if (test_bit(GLF_LRU, gflags))
2245 *p++ = 'L';
2246 if (gl->gl_object)
2247 *p++ = 'o';
2248 if (test_bit(GLF_BLOCKING, gflags))
2249 *p++ = 'b';
2250 if (test_bit(GLF_INSTANTIATE_NEEDED, gflags))
2251 *p++ = 'n';
2252 if (test_bit(GLF_INSTANTIATE_IN_PROG, gflags))
2253 *p++ = 'N';
2254 if (test_bit(GLF_TRY_TO_EVICT, gflags))
2255 *p++ = 'e';
2256 if (test_bit(GLF_VERIFY_DELETE, gflags))
2257 *p++ = 'E';
2258 if (test_bit(GLF_DEFER_DELETE, gflags))
2259 *p++ = 's';
2260 if (test_bit(GLF_CANCELING, gflags))
2261 *p++ = 'C';
2262 *p = 0;
2263 return buf;
2264}
2265
2266/**
2267 * gfs2_dump_glock - print information about a glock
2268 * @seq: The seq_file struct
2269 * @gl: the glock
2270 * @fsid: If true, also dump the file system id
2271 *
2272 * The file format is as follows:
2273 * One line per object, capital letters are used to indicate objects
2274 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
2275 * other objects are indented by a single space and follow the glock to
2276 * which they are related. Fields are indicated by lower case letters
2277 * followed by a colon and the field value, except for strings which are in
2278 * [] so that its possible to see if they are composed of spaces for
2279 * example. The field's are n = number (id of the object), f = flags,
2280 * t = type, s = state, r = refcount, e = error, p = pid.
2281 *
2282 */
2283
2284void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2285{
2286 const struct gfs2_glock_operations *glops = gl->gl_ops;
2287 unsigned long long dtime;
2288 const struct gfs2_holder *gh;
2289 char gflags_buf[32];
2290 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2291 char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
2292 unsigned long nrpages = 0;
2293
2294 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
2295 struct address_space *mapping = gfs2_glock2aspace(gl);
2296
2297 nrpages = mapping->nrpages;
2298 }
2299 memset(fs_id_buf, 0, sizeof(fs_id_buf));
2300 if (fsid && sdp) /* safety precaution */
2301 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
2302 dtime = jiffies - gl->gl_demote_time;
2303 dtime *= 1000000/HZ; /* demote time in uSec */
2304 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
2305 dtime = 0;
2306 gfs2_print_dbg(seq, "%sG: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d "
2307 "v:%d r:%d m:%ld p:%lu\n",
2308 fs_id_buf, state2str(gl->gl_state),
2309 gl->gl_name.ln_type,
2310 (unsigned long long)gl->gl_name.ln_number,
2311 gflags2str(gflags_buf, gl),
2312 state2str(gl->gl_target),
2313 state2str(gl->gl_demote_state), dtime,
2314 atomic_read(&gl->gl_ail_count),
2315 atomic_read(&gl->gl_revokes),
2316 (int)gl->gl_lockref.count, gl->gl_hold_time, nrpages);
2317
2318 list_for_each_entry(gh, &gl->gl_holders, gh_list)
2319 dump_holder(seq, gh, fs_id_buf);
2320
2321 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
2322 glops->go_dump(seq, gl, fs_id_buf);
2323}
2324
2325static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
2326{
2327 struct gfs2_glock *gl = iter_ptr;
2328
2329 seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
2330 gl->gl_name.ln_type,
2331 (unsigned long long)gl->gl_name.ln_number,
2332 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
2333 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
2334 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
2335 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
2336 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
2337 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
2338 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
2339 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
2340 return 0;
2341}
2342
2343static const char *gfs2_gltype[] = {
2344 "type",
2345 "reserved",
2346 "nondisk",
2347 "inode",
2348 "rgrp",
2349 "meta",
2350 "iopen",
2351 "flock",
2352 "plock",
2353 "quota",
2354 "journal",
2355};
2356
2357static const char *gfs2_stype[] = {
2358 [GFS2_LKS_SRTT] = "srtt",
2359 [GFS2_LKS_SRTTVAR] = "srttvar",
2360 [GFS2_LKS_SRTTB] = "srttb",
2361 [GFS2_LKS_SRTTVARB] = "srttvarb",
2362 [GFS2_LKS_SIRT] = "sirt",
2363 [GFS2_LKS_SIRTVAR] = "sirtvar",
2364 [GFS2_LKS_DCOUNT] = "dlm",
2365 [GFS2_LKS_QCOUNT] = "queue",
2366};
2367
2368#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
2369
2370static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
2371{
2372 struct gfs2_sbd *sdp = seq->private;
2373 loff_t pos = *(loff_t *)iter_ptr;
2374 unsigned index = pos >> 3;
2375 unsigned subindex = pos & 0x07;
2376 int i;
2377
2378 if (index == 0 && subindex != 0)
2379 return 0;
2380
2381 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
2382 (index == 0) ? "cpu": gfs2_stype[subindex]);
2383
2384 for_each_possible_cpu(i) {
2385 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
2386
2387 if (index == 0)
2388 seq_printf(seq, " %15u", i);
2389 else
2390 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
2391 lkstats[index - 1].stats[subindex]);
2392 }
2393 seq_putc(seq, '\n');
2394 return 0;
2395}
2396
2397int __init gfs2_glock_init(void)
2398{
2399 int i, ret;
2400
2401 ret = rhashtable_init(&gl_hash_table, &ht_parms);
2402 if (ret < 0)
2403 return ret;
2404
2405 glock_shrinker = shrinker_alloc(0, "gfs2-glock");
2406 if (!glock_shrinker) {
2407 rhashtable_destroy(&gl_hash_table);
2408 return -ENOMEM;
2409 }
2410
2411 glock_shrinker->count_objects = gfs2_glock_shrink_count;
2412 glock_shrinker->scan_objects = gfs2_glock_shrink_scan;
2413
2414 shrinker_register(glock_shrinker);
2415
2416 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
2417 init_waitqueue_head(glock_wait_table + i);
2418
2419 return 0;
2420}
2421
2422void gfs2_glock_exit(void)
2423{
2424 shrinker_free(glock_shrinker);
2425 rhashtable_destroy(&gl_hash_table);
2426}
2427
2428static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
2429{
2430 struct gfs2_glock *gl = gi->gl;
2431
2432 if (gl) {
2433 if (n == 0)
2434 return;
2435 gfs2_glock_put_async(gl);
2436 }
2437 for (;;) {
2438 gl = rhashtable_walk_next(&gi->hti);
2439 if (IS_ERR_OR_NULL(gl)) {
2440 if (gl == ERR_PTR(-EAGAIN)) {
2441 n = 1;
2442 continue;
2443 }
2444 gl = NULL;
2445 break;
2446 }
2447 if (gl->gl_name.ln_sbd != gi->sdp)
2448 continue;
2449 if (n <= 1) {
2450 if (!lockref_get_not_dead(&gl->gl_lockref))
2451 continue;
2452 break;
2453 } else {
2454 if (__lockref_is_dead(&gl->gl_lockref))
2455 continue;
2456 n--;
2457 }
2458 }
2459 gi->gl = gl;
2460}
2461
2462static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
2463 __acquires(RCU)
2464{
2465 struct gfs2_glock_iter *gi = seq->private;
2466 loff_t n;
2467
2468 /*
2469 * We can either stay where we are, skip to the next hash table
2470 * entry, or start from the beginning.
2471 */
2472 if (*pos < gi->last_pos) {
2473 rhashtable_walk_exit(&gi->hti);
2474 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2475 n = *pos + 1;
2476 } else {
2477 n = *pos - gi->last_pos;
2478 }
2479
2480 rhashtable_walk_start(&gi->hti);
2481
2482 gfs2_glock_iter_next(gi, n);
2483 gi->last_pos = *pos;
2484 return gi->gl;
2485}
2486
2487static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
2488 loff_t *pos)
2489{
2490 struct gfs2_glock_iter *gi = seq->private;
2491
2492 (*pos)++;
2493 gi->last_pos = *pos;
2494 gfs2_glock_iter_next(gi, 1);
2495 return gi->gl;
2496}
2497
2498static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
2499 __releases(RCU)
2500{
2501 struct gfs2_glock_iter *gi = seq->private;
2502
2503 rhashtable_walk_stop(&gi->hti);
2504}
2505
2506static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
2507{
2508 dump_glock(seq, iter_ptr, false);
2509 return 0;
2510}
2511
2512static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2513{
2514 preempt_disable();
2515 if (*pos >= GFS2_NR_SBSTATS)
2516 return NULL;
2517 return pos;
2518}
2519
2520static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2521 loff_t *pos)
2522{
2523 (*pos)++;
2524 if (*pos >= GFS2_NR_SBSTATS)
2525 return NULL;
2526 return pos;
2527}
2528
2529static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2530{
2531 preempt_enable();
2532}
2533
2534static const struct seq_operations gfs2_glock_seq_ops = {
2535 .start = gfs2_glock_seq_start,
2536 .next = gfs2_glock_seq_next,
2537 .stop = gfs2_glock_seq_stop,
2538 .show = gfs2_glock_seq_show,
2539};
2540
2541static const struct seq_operations gfs2_glstats_seq_ops = {
2542 .start = gfs2_glock_seq_start,
2543 .next = gfs2_glock_seq_next,
2544 .stop = gfs2_glock_seq_stop,
2545 .show = gfs2_glstats_seq_show,
2546};
2547
2548static const struct seq_operations gfs2_sbstats_sops = {
2549 .start = gfs2_sbstats_seq_start,
2550 .next = gfs2_sbstats_seq_next,
2551 .stop = gfs2_sbstats_seq_stop,
2552 .show = gfs2_sbstats_seq_show,
2553};
2554
2555#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2556
2557static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2558 const struct seq_operations *ops)
2559{
2560 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
2561 if (ret == 0) {
2562 struct seq_file *seq = file->private_data;
2563 struct gfs2_glock_iter *gi = seq->private;
2564
2565 gi->sdp = inode->i_private;
2566 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
2567 if (seq->buf)
2568 seq->size = GFS2_SEQ_GOODSIZE;
2569 /*
2570 * Initially, we are "before" the first hash table entry; the
2571 * first call to rhashtable_walk_next gets us the first entry.
2572 */
2573 gi->last_pos = -1;
2574 gi->gl = NULL;
2575 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2576 }
2577 return ret;
2578}
2579
2580static int gfs2_glocks_open(struct inode *inode, struct file *file)
2581{
2582 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2583}
2584
2585static int gfs2_glocks_release(struct inode *inode, struct file *file)
2586{
2587 struct seq_file *seq = file->private_data;
2588 struct gfs2_glock_iter *gi = seq->private;
2589
2590 if (gi->gl)
2591 gfs2_glock_put(gi->gl);
2592 rhashtable_walk_exit(&gi->hti);
2593 return seq_release_private(inode, file);
2594}
2595
2596static int gfs2_glstats_open(struct inode *inode, struct file *file)
2597{
2598 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
2599}
2600
2601static const struct file_operations gfs2_glocks_fops = {
2602 .owner = THIS_MODULE,
2603 .open = gfs2_glocks_open,
2604 .read = seq_read,
2605 .llseek = seq_lseek,
2606 .release = gfs2_glocks_release,
2607};
2608
2609static const struct file_operations gfs2_glstats_fops = {
2610 .owner = THIS_MODULE,
2611 .open = gfs2_glstats_open,
2612 .read = seq_read,
2613 .llseek = seq_lseek,
2614 .release = gfs2_glocks_release,
2615};
2616
2617struct gfs2_glockfd_iter {
2618 struct super_block *sb;
2619 unsigned int tgid;
2620 struct task_struct *task;
2621 unsigned int fd;
2622 struct file *file;
2623};
2624
2625static struct task_struct *gfs2_glockfd_next_task(struct gfs2_glockfd_iter *i)
2626{
2627 struct pid_namespace *ns = task_active_pid_ns(current);
2628 struct pid *pid;
2629
2630 if (i->task)
2631 put_task_struct(i->task);
2632
2633 rcu_read_lock();
2634retry:
2635 i->task = NULL;
2636 pid = find_ge_pid(i->tgid, ns);
2637 if (pid) {
2638 i->tgid = pid_nr_ns(pid, ns);
2639 i->task = pid_task(pid, PIDTYPE_TGID);
2640 if (!i->task) {
2641 i->tgid++;
2642 goto retry;
2643 }
2644 get_task_struct(i->task);
2645 }
2646 rcu_read_unlock();
2647 return i->task;
2648}
2649
2650static struct file *gfs2_glockfd_next_file(struct gfs2_glockfd_iter *i)
2651{
2652 if (i->file) {
2653 fput(i->file);
2654 i->file = NULL;
2655 }
2656
2657 for(;; i->fd++) {
2658 i->file = fget_task_next(i->task, &i->fd);
2659 if (!i->file) {
2660 i->fd = 0;
2661 break;
2662 }
2663
2664 if (file_inode(i->file)->i_sb == i->sb)
2665 break;
2666
2667 fput(i->file);
2668 }
2669 return i->file;
2670}
2671
2672static void *gfs2_glockfd_seq_start(struct seq_file *seq, loff_t *pos)
2673{
2674 struct gfs2_glockfd_iter *i = seq->private;
2675
2676 if (*pos)
2677 return NULL;
2678 while (gfs2_glockfd_next_task(i)) {
2679 if (gfs2_glockfd_next_file(i))
2680 return i;
2681 i->tgid++;
2682 }
2683 return NULL;
2684}
2685
2686static void *gfs2_glockfd_seq_next(struct seq_file *seq, void *iter_ptr,
2687 loff_t *pos)
2688{
2689 struct gfs2_glockfd_iter *i = seq->private;
2690
2691 (*pos)++;
2692 i->fd++;
2693 do {
2694 if (gfs2_glockfd_next_file(i))
2695 return i;
2696 i->tgid++;
2697 } while (gfs2_glockfd_next_task(i));
2698 return NULL;
2699}
2700
2701static void gfs2_glockfd_seq_stop(struct seq_file *seq, void *iter_ptr)
2702{
2703 struct gfs2_glockfd_iter *i = seq->private;
2704
2705 if (i->file)
2706 fput(i->file);
2707 if (i->task)
2708 put_task_struct(i->task);
2709}
2710
2711static void gfs2_glockfd_seq_show_flock(struct seq_file *seq,
2712 struct gfs2_glockfd_iter *i)
2713{
2714 struct gfs2_file *fp = i->file->private_data;
2715 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
2716 struct lm_lockname gl_name = { .ln_type = LM_TYPE_RESERVED };
2717
2718 if (!READ_ONCE(fl_gh->gh_gl))
2719 return;
2720
2721 spin_lock(&i->file->f_lock);
2722 if (gfs2_holder_initialized(fl_gh))
2723 gl_name = fl_gh->gh_gl->gl_name;
2724 spin_unlock(&i->file->f_lock);
2725
2726 if (gl_name.ln_type != LM_TYPE_RESERVED) {
2727 seq_printf(seq, "%d %u %u/%llx\n",
2728 i->tgid, i->fd, gl_name.ln_type,
2729 (unsigned long long)gl_name.ln_number);
2730 }
2731}
2732
2733static int gfs2_glockfd_seq_show(struct seq_file *seq, void *iter_ptr)
2734{
2735 struct gfs2_glockfd_iter *i = seq->private;
2736 struct inode *inode = file_inode(i->file);
2737 struct gfs2_glock *gl;
2738
2739 inode_lock_shared(inode);
2740 gl = GFS2_I(inode)->i_iopen_gh.gh_gl;
2741 if (gl) {
2742 seq_printf(seq, "%d %u %u/%llx\n",
2743 i->tgid, i->fd, gl->gl_name.ln_type,
2744 (unsigned long long)gl->gl_name.ln_number);
2745 }
2746 gfs2_glockfd_seq_show_flock(seq, i);
2747 inode_unlock_shared(inode);
2748 return 0;
2749}
2750
2751static const struct seq_operations gfs2_glockfd_seq_ops = {
2752 .start = gfs2_glockfd_seq_start,
2753 .next = gfs2_glockfd_seq_next,
2754 .stop = gfs2_glockfd_seq_stop,
2755 .show = gfs2_glockfd_seq_show,
2756};
2757
2758static int gfs2_glockfd_open(struct inode *inode, struct file *file)
2759{
2760 struct gfs2_glockfd_iter *i;
2761 struct gfs2_sbd *sdp = inode->i_private;
2762
2763 i = __seq_open_private(file, &gfs2_glockfd_seq_ops,
2764 sizeof(struct gfs2_glockfd_iter));
2765 if (!i)
2766 return -ENOMEM;
2767 i->sb = sdp->sd_vfs;
2768 return 0;
2769}
2770
2771static const struct file_operations gfs2_glockfd_fops = {
2772 .owner = THIS_MODULE,
2773 .open = gfs2_glockfd_open,
2774 .read = seq_read,
2775 .llseek = seq_lseek,
2776 .release = seq_release_private,
2777};
2778
2779DEFINE_SEQ_ATTRIBUTE(gfs2_sbstats);
2780
2781void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2782{
2783 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2784
2785 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2786 &gfs2_glocks_fops);
2787
2788 debugfs_create_file("glockfd", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2789 &gfs2_glockfd_fops);
2790
2791 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2792 &gfs2_glstats_fops);
2793
2794 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2795 &gfs2_sbstats_fops);
2796}
2797
2798void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2799{
2800 debugfs_remove_recursive(sdp->debugfs_dir);
2801 sdp->debugfs_dir = NULL;
2802}
2803
2804void gfs2_register_debugfs(void)
2805{
2806 gfs2_root = debugfs_create_dir("gfs2", NULL);
2807}
2808
2809void gfs2_unregister_debugfs(void)
2810{
2811 debugfs_remove(gfs2_root);
2812 gfs2_root = NULL;
2813}