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