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