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