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