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