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;
857
858 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
859 return;
860
861 /* While a demote is in progress, the GLF_LOCK flag must be set. */
862 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
863
864 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
865 gl->gl_demote_state != gl->gl_state) {
866 if (find_first_holder(gl))
867 goto out_unlock;
868 if (nonblock)
869 goto out_sched;
870 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
871 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
872 gl->gl_target = gl->gl_demote_state;
873 do_xmote(gl, NULL, gl->gl_target);
874 return;
875 } else {
876 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
877 gfs2_demote_wake(gl);
878 if (do_promote(gl))
879 goto out_unlock;
880 gh = find_first_waiter(gl);
881 if (!gh)
882 goto out_unlock;
883 gl->gl_target = gh->gh_state;
884 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
885 do_error(gl, 0); /* Fail queued try locks */
886 do_xmote(gl, gh, gl->gl_target);
887 return;
888 }
889
890 out_sched:
891 clear_bit(GLF_LOCK, &gl->gl_flags);
892 smp_mb__after_atomic();
893 gl->gl_lockref.count++;
894 gfs2_glock_queue_work(gl, 0);
895 return;
896
897 out_unlock:
898 clear_bit(GLF_LOCK, &gl->gl_flags);
899 smp_mb__after_atomic();
900 return;
901 }
902
903 /**
904 * glock_set_object - set the gl_object field of a glock
905 * @gl: the glock
906 * @object: the object
907 */
glock_set_object(struct gfs2_glock * gl,void * object)908 void glock_set_object(struct gfs2_glock *gl, void *object)
909 {
910 void *prev_object;
911
912 spin_lock(&gl->gl_lockref.lock);
913 prev_object = gl->gl_object;
914 gl->gl_object = object;
915 spin_unlock(&gl->gl_lockref.lock);
916 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == NULL)) {
917 pr_warn("glock=%u/%llx\n",
918 gl->gl_name.ln_type,
919 (unsigned long long)gl->gl_name.ln_number);
920 gfs2_dump_glock(NULL, gl, true);
921 }
922 }
923
924 /**
925 * glock_clear_object - clear the gl_object field of a glock
926 * @gl: the glock
927 * @object: object the glock currently points at
928 */
glock_clear_object(struct gfs2_glock * gl,void * object)929 void glock_clear_object(struct gfs2_glock *gl, void *object)
930 {
931 void *prev_object;
932
933 spin_lock(&gl->gl_lockref.lock);
934 prev_object = gl->gl_object;
935 gl->gl_object = NULL;
936 spin_unlock(&gl->gl_lockref.lock);
937 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == object)) {
938 pr_warn("glock=%u/%llx\n",
939 gl->gl_name.ln_type,
940 (unsigned long long)gl->gl_name.ln_number);
941 gfs2_dump_glock(NULL, gl, true);
942 }
943 }
944
gfs2_inode_remember_delete(struct gfs2_glock * gl,u64 generation)945 void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation)
946 {
947 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
948
949 if (ri->ri_magic == 0)
950 ri->ri_magic = cpu_to_be32(GFS2_MAGIC);
951 if (ri->ri_magic == cpu_to_be32(GFS2_MAGIC))
952 ri->ri_generation_deleted = cpu_to_be64(generation);
953 }
954
gfs2_inode_already_deleted(struct gfs2_glock * gl,u64 generation)955 bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation)
956 {
957 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
958
959 if (ri->ri_magic != cpu_to_be32(GFS2_MAGIC))
960 return false;
961 return generation <= be64_to_cpu(ri->ri_generation_deleted);
962 }
963
gfs2_glock_poke(struct gfs2_glock * gl)964 static void gfs2_glock_poke(struct gfs2_glock *gl)
965 {
966 int flags = LM_FLAG_TRY_1CB | LM_FLAG_ANY | GL_SKIP;
967 struct gfs2_holder gh;
968 int error;
969
970 __gfs2_holder_init(gl, LM_ST_SHARED, flags, &gh, _RET_IP_);
971 error = gfs2_glock_nq(&gh);
972 if (!error)
973 gfs2_glock_dq(&gh);
974 gfs2_holder_uninit(&gh);
975 }
976
gfs2_try_evict(struct gfs2_glock * gl)977 static bool gfs2_try_evict(struct gfs2_glock *gl)
978 {
979 struct gfs2_inode *ip;
980 bool evicted = false;
981
982 /*
983 * If there is contention on the iopen glock and we have an inode, try
984 * to grab and release the inode so that it can be evicted. This will
985 * allow the remote node to go ahead and delete the inode without us
986 * having to do it, which will avoid rgrp glock thrashing.
987 *
988 * The remote node is likely still holding the corresponding inode
989 * glock, so it will run before we get to verify that the delete has
990 * happened below.
991 */
992 spin_lock(&gl->gl_lockref.lock);
993 ip = gl->gl_object;
994 if (ip && !igrab(&ip->i_inode))
995 ip = NULL;
996 spin_unlock(&gl->gl_lockref.lock);
997 if (ip) {
998 gl->gl_no_formal_ino = ip->i_no_formal_ino;
999 set_bit(GIF_DEFERRED_DELETE, &ip->i_flags);
1000 d_prune_aliases(&ip->i_inode);
1001 iput(&ip->i_inode);
1002
1003 /* If the inode was evicted, gl->gl_object will now be NULL. */
1004 spin_lock(&gl->gl_lockref.lock);
1005 ip = gl->gl_object;
1006 if (ip) {
1007 clear_bit(GIF_DEFERRED_DELETE, &ip->i_flags);
1008 if (!igrab(&ip->i_inode))
1009 ip = NULL;
1010 }
1011 spin_unlock(&gl->gl_lockref.lock);
1012 if (ip) {
1013 gfs2_glock_poke(ip->i_gl);
1014 iput(&ip->i_inode);
1015 }
1016 evicted = !ip;
1017 }
1018 return evicted;
1019 }
1020
gfs2_queue_try_to_evict(struct gfs2_glock * gl)1021 bool gfs2_queue_try_to_evict(struct gfs2_glock *gl)
1022 {
1023 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1024
1025 if (test_and_set_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
1026 return false;
1027 return queue_delayed_work(sdp->sd_delete_wq,
1028 &gl->gl_delete, 0);
1029 }
1030
gfs2_queue_verify_delete(struct gfs2_glock * gl,bool later)1031 bool gfs2_queue_verify_delete(struct gfs2_glock *gl, bool later)
1032 {
1033 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1034 unsigned long delay;
1035
1036 if (test_and_set_bit(GLF_VERIFY_DELETE, &gl->gl_flags))
1037 return false;
1038 delay = later ? 5 * HZ : 0;
1039 return queue_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, delay);
1040 }
1041
delete_work_func(struct work_struct * work)1042 static void delete_work_func(struct work_struct *work)
1043 {
1044 struct delayed_work *dwork = to_delayed_work(work);
1045 struct gfs2_glock *gl = container_of(dwork, struct gfs2_glock, gl_delete);
1046 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1047 struct inode *inode;
1048 u64 no_addr = gl->gl_name.ln_number;
1049
1050 if (test_and_clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags)) {
1051 /*
1052 * If we can evict the inode, give the remote node trying to
1053 * delete the inode some time before verifying that the delete
1054 * has happened. Otherwise, if we cause contention on the inode glock
1055 * immediately, the remote node will think that we still have
1056 * the inode in use, and so it will give up waiting.
1057 *
1058 * If we can't evict the inode, signal to the remote node that
1059 * the inode is still in use. We'll later try to delete the
1060 * inode locally in gfs2_evict_inode.
1061 *
1062 * FIXME: We only need to verify that the remote node has
1063 * deleted the inode because nodes before this remote delete
1064 * rework won't cooperate. At a later time, when we no longer
1065 * care about compatibility with such nodes, we can skip this
1066 * step entirely.
1067 */
1068 if (gfs2_try_evict(gl)) {
1069 if (test_bit(SDF_KILL, &sdp->sd_flags))
1070 goto out;
1071 if (gfs2_queue_verify_delete(gl, true))
1072 return;
1073 }
1074 goto out;
1075 }
1076
1077 if (test_and_clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags)) {
1078 inode = gfs2_lookup_by_inum(sdp, no_addr, gl->gl_no_formal_ino,
1079 GFS2_BLKST_UNLINKED);
1080 if (IS_ERR(inode)) {
1081 if (PTR_ERR(inode) == -EAGAIN &&
1082 !test_bit(SDF_KILL, &sdp->sd_flags) &&
1083 gfs2_queue_verify_delete(gl, true))
1084 return;
1085 } else {
1086 d_prune_aliases(inode);
1087 iput(inode);
1088 }
1089 }
1090
1091 out:
1092 gfs2_glock_put(gl);
1093 }
1094
glock_work_func(struct work_struct * work)1095 static void glock_work_func(struct work_struct *work)
1096 {
1097 unsigned long delay = 0;
1098 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
1099 unsigned int drop_refs = 1;
1100
1101 spin_lock(&gl->gl_lockref.lock);
1102 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
1103 clear_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1104 finish_xmote(gl, gl->gl_reply);
1105 drop_refs++;
1106 }
1107 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1108 gl->gl_state != LM_ST_UNLOCKED &&
1109 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
1110 unsigned long holdtime, now = jiffies;
1111
1112 holdtime = gl->gl_tchange + gl->gl_hold_time;
1113 if (time_before(now, holdtime))
1114 delay = holdtime - now;
1115
1116 if (!delay) {
1117 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
1118 gfs2_set_demote(gl);
1119 }
1120 }
1121 run_queue(gl, 0);
1122 if (delay) {
1123 /* Keep one glock reference for the work we requeue. */
1124 drop_refs--;
1125 if (gl->gl_name.ln_type != LM_TYPE_INODE)
1126 delay = 0;
1127 gfs2_glock_queue_work(gl, delay);
1128 }
1129
1130 /*
1131 * Drop the remaining glock references manually here. (Mind that
1132 * gfs2_glock_queue_work depends on the lockref spinlock begin held
1133 * here as well.)
1134 */
1135 gl->gl_lockref.count -= drop_refs;
1136 if (!gl->gl_lockref.count) {
1137 __gfs2_glock_put(gl);
1138 return;
1139 }
1140 spin_unlock(&gl->gl_lockref.lock);
1141 }
1142
find_insert_glock(struct lm_lockname * name,struct gfs2_glock * new)1143 static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
1144 struct gfs2_glock *new)
1145 {
1146 struct wait_glock_queue wait;
1147 wait_queue_head_t *wq = glock_waitqueue(name);
1148 struct gfs2_glock *gl;
1149
1150 wait.name = name;
1151 init_wait(&wait.wait);
1152 wait.wait.func = glock_wake_function;
1153
1154 again:
1155 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1156 rcu_read_lock();
1157 if (new) {
1158 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
1159 &new->gl_node, ht_parms);
1160 if (IS_ERR(gl))
1161 goto out;
1162 } else {
1163 gl = rhashtable_lookup_fast(&gl_hash_table,
1164 name, ht_parms);
1165 }
1166 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
1167 rcu_read_unlock();
1168 schedule();
1169 goto again;
1170 }
1171 out:
1172 rcu_read_unlock();
1173 finish_wait(wq, &wait.wait);
1174 return gl;
1175 }
1176
1177 /**
1178 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
1179 * @sdp: The GFS2 superblock
1180 * @number: the lock number
1181 * @glops: The glock_operations to use
1182 * @create: If 0, don't create the glock if it doesn't exist
1183 * @glp: the glock is returned here
1184 *
1185 * This does not lock a glock, just finds/creates structures for one.
1186 *
1187 * Returns: errno
1188 */
1189
gfs2_glock_get(struct gfs2_sbd * sdp,u64 number,const struct gfs2_glock_operations * glops,int create,struct gfs2_glock ** glp)1190 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
1191 const struct gfs2_glock_operations *glops, int create,
1192 struct gfs2_glock **glp)
1193 {
1194 struct super_block *s = sdp->sd_vfs;
1195 struct lm_lockname name = { .ln_number = number,
1196 .ln_type = glops->go_type,
1197 .ln_sbd = sdp };
1198 struct gfs2_glock *gl, *tmp;
1199 struct address_space *mapping;
1200 int ret = 0;
1201
1202 gl = find_insert_glock(&name, NULL);
1203 if (gl) {
1204 *glp = gl;
1205 return 0;
1206 }
1207 if (!create)
1208 return -ENOENT;
1209
1210 if (glops->go_flags & GLOF_ASPACE) {
1211 struct gfs2_glock_aspace *gla =
1212 kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_NOFS);
1213 if (!gla)
1214 return -ENOMEM;
1215 gl = &gla->glock;
1216 } else {
1217 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_NOFS);
1218 if (!gl)
1219 return -ENOMEM;
1220 }
1221 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
1222 gl->gl_ops = glops;
1223
1224 if (glops->go_flags & GLOF_LVB) {
1225 gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
1226 if (!gl->gl_lksb.sb_lvbptr) {
1227 gfs2_glock_dealloc(&gl->gl_rcu);
1228 return -ENOMEM;
1229 }
1230 }
1231
1232 atomic_inc(&sdp->sd_glock_disposal);
1233 gl->gl_node.next = NULL;
1234 gl->gl_flags = glops->go_instantiate ? BIT(GLF_INSTANTIATE_NEEDED) : 0;
1235 gl->gl_name = name;
1236 lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
1237 gl->gl_lockref.count = 1;
1238 gl->gl_state = LM_ST_UNLOCKED;
1239 gl->gl_target = LM_ST_UNLOCKED;
1240 gl->gl_demote_state = LM_ST_EXCLUSIVE;
1241 gl->gl_dstamp = 0;
1242 preempt_disable();
1243 /* We use the global stats to estimate the initial per-glock stats */
1244 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
1245 preempt_enable();
1246 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
1247 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
1248 gl->gl_tchange = jiffies;
1249 gl->gl_object = NULL;
1250 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
1251 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
1252 if (gl->gl_name.ln_type == LM_TYPE_IOPEN)
1253 INIT_DELAYED_WORK(&gl->gl_delete, delete_work_func);
1254
1255 mapping = gfs2_glock2aspace(gl);
1256 if (mapping) {
1257 mapping->a_ops = &gfs2_meta_aops;
1258 mapping->host = s->s_bdev->bd_inode;
1259 mapping->flags = 0;
1260 mapping_set_gfp_mask(mapping, GFP_NOFS);
1261 mapping->private_data = NULL;
1262 mapping->writeback_index = 0;
1263 }
1264
1265 tmp = find_insert_glock(&name, gl);
1266 if (!tmp) {
1267 *glp = gl;
1268 goto out;
1269 }
1270 if (IS_ERR(tmp)) {
1271 ret = PTR_ERR(tmp);
1272 goto out_free;
1273 }
1274 *glp = tmp;
1275
1276 out_free:
1277 gfs2_glock_dealloc(&gl->gl_rcu);
1278 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
1279 wake_up(&sdp->sd_kill_wait);
1280
1281 out:
1282 return ret;
1283 }
1284
1285 /**
1286 * __gfs2_holder_init - initialize a struct gfs2_holder in the default way
1287 * @gl: the glock
1288 * @state: the state we're requesting
1289 * @flags: the modifier flags
1290 * @gh: the holder structure
1291 *
1292 */
1293
__gfs2_holder_init(struct gfs2_glock * gl,unsigned int state,u16 flags,struct gfs2_holder * gh,unsigned long ip)1294 void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
1295 struct gfs2_holder *gh, unsigned long ip)
1296 {
1297 INIT_LIST_HEAD(&gh->gh_list);
1298 gh->gh_gl = gfs2_glock_hold(gl);
1299 gh->gh_ip = ip;
1300 gh->gh_owner_pid = get_pid(task_pid(current));
1301 gh->gh_state = state;
1302 gh->gh_flags = flags;
1303 gh->gh_iflags = 0;
1304 }
1305
1306 /**
1307 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
1308 * @state: the state we're requesting
1309 * @flags: the modifier flags
1310 * @gh: the holder structure
1311 *
1312 * Don't mess with the glock.
1313 *
1314 */
1315
gfs2_holder_reinit(unsigned int state,u16 flags,struct gfs2_holder * gh)1316 void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
1317 {
1318 gh->gh_state = state;
1319 gh->gh_flags = flags;
1320 gh->gh_iflags = 0;
1321 gh->gh_ip = _RET_IP_;
1322 put_pid(gh->gh_owner_pid);
1323 gh->gh_owner_pid = get_pid(task_pid(current));
1324 }
1325
1326 /**
1327 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
1328 * @gh: the holder structure
1329 *
1330 */
1331
gfs2_holder_uninit(struct gfs2_holder * gh)1332 void gfs2_holder_uninit(struct gfs2_holder *gh)
1333 {
1334 put_pid(gh->gh_owner_pid);
1335 gfs2_glock_put(gh->gh_gl);
1336 gfs2_holder_mark_uninitialized(gh);
1337 gh->gh_ip = 0;
1338 }
1339
gfs2_glock_update_hold_time(struct gfs2_glock * gl,unsigned long start_time)1340 static void gfs2_glock_update_hold_time(struct gfs2_glock *gl,
1341 unsigned long start_time)
1342 {
1343 /* Have we waited longer that a second? */
1344 if (time_after(jiffies, start_time + HZ)) {
1345 /* Lengthen the minimum hold time. */
1346 gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR,
1347 GL_GLOCK_MAX_HOLD);
1348 }
1349 }
1350
1351 /**
1352 * gfs2_glock_holder_ready - holder is ready and its error code can be collected
1353 * @gh: the glock holder
1354 *
1355 * Called when a glock holder no longer needs to be waited for because it is
1356 * now either held (HIF_HOLDER set; gh_error == 0), or acquiring the lock has
1357 * failed (gh_error != 0).
1358 */
1359
gfs2_glock_holder_ready(struct gfs2_holder * gh)1360 int gfs2_glock_holder_ready(struct gfs2_holder *gh)
1361 {
1362 if (gh->gh_error || (gh->gh_flags & GL_SKIP))
1363 return gh->gh_error;
1364 gh->gh_error = gfs2_instantiate(gh);
1365 if (gh->gh_error)
1366 gfs2_glock_dq(gh);
1367 return gh->gh_error;
1368 }
1369
1370 /**
1371 * gfs2_glock_wait - wait on a glock acquisition
1372 * @gh: the glock holder
1373 *
1374 * Returns: 0 on success
1375 */
1376
gfs2_glock_wait(struct gfs2_holder * gh)1377 int gfs2_glock_wait(struct gfs2_holder *gh)
1378 {
1379 unsigned long start_time = jiffies;
1380
1381 might_sleep();
1382 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1383 gfs2_glock_update_hold_time(gh->gh_gl, start_time);
1384 return gfs2_glock_holder_ready(gh);
1385 }
1386
glocks_pending(unsigned int num_gh,struct gfs2_holder * ghs)1387 static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs)
1388 {
1389 int i;
1390
1391 for (i = 0; i < num_gh; i++)
1392 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags))
1393 return 1;
1394 return 0;
1395 }
1396
1397 /**
1398 * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions
1399 * @num_gh: the number of holders in the array
1400 * @ghs: the glock holder array
1401 *
1402 * Returns: 0 on success, meaning all glocks have been granted and are held.
1403 * -ESTALE if the request timed out, meaning all glocks were released,
1404 * and the caller should retry the operation.
1405 */
1406
gfs2_glock_async_wait(unsigned int num_gh,struct gfs2_holder * ghs)1407 int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs)
1408 {
1409 struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd;
1410 int i, ret = 0, timeout = 0;
1411 unsigned long start_time = jiffies;
1412
1413 might_sleep();
1414 /*
1415 * Total up the (minimum hold time * 2) of all glocks and use that to
1416 * determine the max amount of time we should wait.
1417 */
1418 for (i = 0; i < num_gh; i++)
1419 timeout += ghs[i].gh_gl->gl_hold_time << 1;
1420
1421 if (!wait_event_timeout(sdp->sd_async_glock_wait,
1422 !glocks_pending(num_gh, ghs), timeout)) {
1423 ret = -ESTALE; /* request timed out. */
1424 goto out;
1425 }
1426
1427 for (i = 0; i < num_gh; i++) {
1428 struct gfs2_holder *gh = &ghs[i];
1429 int ret2;
1430
1431 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1432 gfs2_glock_update_hold_time(gh->gh_gl,
1433 start_time);
1434 }
1435 ret2 = gfs2_glock_holder_ready(gh);
1436 if (!ret)
1437 ret = ret2;
1438 }
1439
1440 out:
1441 if (ret) {
1442 for (i = 0; i < num_gh; i++) {
1443 struct gfs2_holder *gh = &ghs[i];
1444
1445 gfs2_glock_dq(gh);
1446 }
1447 }
1448 return ret;
1449 }
1450
1451 /**
1452 * handle_callback - process a demote request
1453 * @gl: the glock
1454 * @state: the state the caller wants us to change to
1455 * @delay: zero to demote immediately; otherwise pending demote
1456 * @remote: true if this came from a different cluster node
1457 *
1458 * There are only two requests that we are going to see in actual
1459 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
1460 */
1461
handle_callback(struct gfs2_glock * gl,unsigned int state,unsigned long delay,bool remote)1462 static void handle_callback(struct gfs2_glock *gl, unsigned int state,
1463 unsigned long delay, bool remote)
1464 {
1465 if (delay)
1466 set_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
1467 else
1468 gfs2_set_demote(gl);
1469 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
1470 gl->gl_demote_state = state;
1471 gl->gl_demote_time = jiffies;
1472 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
1473 gl->gl_demote_state != state) {
1474 gl->gl_demote_state = LM_ST_UNLOCKED;
1475 }
1476 if (gl->gl_ops->go_callback)
1477 gl->gl_ops->go_callback(gl, remote);
1478 trace_gfs2_demote_rq(gl, remote);
1479 }
1480
gfs2_print_dbg(struct seq_file * seq,const char * fmt,...)1481 void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
1482 {
1483 struct va_format vaf;
1484 va_list args;
1485
1486 va_start(args, fmt);
1487
1488 if (seq) {
1489 seq_vprintf(seq, fmt, args);
1490 } else {
1491 vaf.fmt = fmt;
1492 vaf.va = &args;
1493
1494 pr_err("%pV", &vaf);
1495 }
1496
1497 va_end(args);
1498 }
1499
pid_is_meaningful(const struct gfs2_holder * gh)1500 static inline bool pid_is_meaningful(const struct gfs2_holder *gh)
1501 {
1502 if (!(gh->gh_flags & GL_NOPID))
1503 return true;
1504 if (gh->gh_state == LM_ST_UNLOCKED)
1505 return true;
1506 return false;
1507 }
1508
1509 /**
1510 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1511 * @gh: the holder structure to add
1512 *
1513 * Eventually we should move the recursive locking trap to a
1514 * debugging option or something like that. This is the fast
1515 * path and needs to have the minimum number of distractions.
1516 *
1517 */
1518
add_to_queue(struct gfs2_holder * gh)1519 static inline void add_to_queue(struct gfs2_holder *gh)
1520 __releases(&gl->gl_lockref.lock)
1521 __acquires(&gl->gl_lockref.lock)
1522 {
1523 struct gfs2_glock *gl = gh->gh_gl;
1524 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1525 struct list_head *insert_pt = NULL;
1526 struct gfs2_holder *gh2;
1527 int try_futile = 0;
1528
1529 GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL);
1530 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1531 GLOCK_BUG_ON(gl, true);
1532
1533 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1534 if (test_bit(GLF_LOCK, &gl->gl_flags)) {
1535 struct gfs2_holder *current_gh;
1536
1537 current_gh = find_first_holder(gl);
1538 try_futile = !may_grant(gl, current_gh, gh);
1539 }
1540 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1541 goto fail;
1542 }
1543
1544 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1545 if (likely(gh2->gh_owner_pid != gh->gh_owner_pid))
1546 continue;
1547 if (gh->gh_gl->gl_ops->go_type == LM_TYPE_FLOCK)
1548 continue;
1549 if (!pid_is_meaningful(gh2))
1550 continue;
1551 goto trap_recursive;
1552 }
1553 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1554 if (try_futile &&
1555 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
1556 fail:
1557 gh->gh_error = GLR_TRYFAILED;
1558 gfs2_holder_wake(gh);
1559 return;
1560 }
1561 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1562 continue;
1563 }
1564 trace_gfs2_glock_queue(gh, 1);
1565 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1566 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
1567 if (likely(insert_pt == NULL)) {
1568 list_add_tail(&gh->gh_list, &gl->gl_holders);
1569 return;
1570 }
1571 list_add_tail(&gh->gh_list, insert_pt);
1572 gh = list_first_entry(&gl->gl_holders, struct gfs2_holder, gh_list);
1573 spin_unlock(&gl->gl_lockref.lock);
1574 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
1575 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
1576 spin_lock(&gl->gl_lockref.lock);
1577 return;
1578
1579 trap_recursive:
1580 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1581 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1582 fs_err(sdp, "lock type: %d req lock state : %d\n",
1583 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1584 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1585 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1586 fs_err(sdp, "lock type: %d req lock state : %d\n",
1587 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1588 gfs2_dump_glock(NULL, gl, true);
1589 BUG();
1590 }
1591
1592 /**
1593 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1594 * @gh: the holder structure
1595 *
1596 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1597 *
1598 * Returns: 0, GLR_TRYFAILED, or errno on failure
1599 */
1600
gfs2_glock_nq(struct gfs2_holder * gh)1601 int gfs2_glock_nq(struct gfs2_holder *gh)
1602 {
1603 struct gfs2_glock *gl = gh->gh_gl;
1604 int error = 0;
1605
1606 if (glock_blocked_by_withdraw(gl) && !(gh->gh_flags & LM_FLAG_NOEXP))
1607 return -EIO;
1608
1609 if (test_bit(GLF_LRU, &gl->gl_flags))
1610 gfs2_glock_remove_from_lru(gl);
1611
1612 gh->gh_error = 0;
1613 spin_lock(&gl->gl_lockref.lock);
1614 add_to_queue(gh);
1615 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1616 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
1617 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1618 gl->gl_lockref.count++;
1619 gfs2_glock_queue_work(gl, 0);
1620 }
1621 run_queue(gl, 1);
1622 spin_unlock(&gl->gl_lockref.lock);
1623
1624 if (!(gh->gh_flags & GL_ASYNC))
1625 error = gfs2_glock_wait(gh);
1626
1627 return error;
1628 }
1629
1630 /**
1631 * gfs2_glock_poll - poll to see if an async request has been completed
1632 * @gh: the holder
1633 *
1634 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1635 */
1636
gfs2_glock_poll(struct gfs2_holder * gh)1637 int gfs2_glock_poll(struct gfs2_holder *gh)
1638 {
1639 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
1640 }
1641
needs_demote(struct gfs2_glock * gl)1642 static inline bool needs_demote(struct gfs2_glock *gl)
1643 {
1644 return (test_bit(GLF_DEMOTE, &gl->gl_flags) ||
1645 test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags));
1646 }
1647
__gfs2_glock_dq(struct gfs2_holder * gh)1648 static void __gfs2_glock_dq(struct gfs2_holder *gh)
1649 {
1650 struct gfs2_glock *gl = gh->gh_gl;
1651 unsigned delay = 0;
1652 int fast_path = 0;
1653
1654 /*
1655 * This holder should not be cached, so mark it for demote.
1656 * Note: this should be done before the check for needs_demote
1657 * below.
1658 */
1659 if (gh->gh_flags & GL_NOCACHE)
1660 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
1661
1662 list_del_init(&gh->gh_list);
1663 clear_bit(HIF_HOLDER, &gh->gh_iflags);
1664 trace_gfs2_glock_queue(gh, 0);
1665
1666 /*
1667 * If there hasn't been a demote request we are done.
1668 * (Let the remaining holders, if any, keep holding it.)
1669 */
1670 if (!needs_demote(gl)) {
1671 if (list_empty(&gl->gl_holders))
1672 fast_path = 1;
1673 }
1674
1675 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
1676 gfs2_glock_add_to_lru(gl);
1677
1678 if (unlikely(!fast_path)) {
1679 gl->gl_lockref.count++;
1680 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1681 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1682 gl->gl_name.ln_type == LM_TYPE_INODE)
1683 delay = gl->gl_hold_time;
1684 gfs2_glock_queue_work(gl, delay);
1685 }
1686 }
1687
1688 /**
1689 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1690 * @gh: the glock holder
1691 *
1692 */
gfs2_glock_dq(struct gfs2_holder * gh)1693 void gfs2_glock_dq(struct gfs2_holder *gh)
1694 {
1695 struct gfs2_glock *gl = gh->gh_gl;
1696 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1697
1698 spin_lock(&gl->gl_lockref.lock);
1699 if (!gfs2_holder_queued(gh)) {
1700 /*
1701 * May have already been dequeued because the locking request
1702 * was GL_ASYNC and it has failed in the meantime.
1703 */
1704 goto out;
1705 }
1706
1707 if (list_is_first(&gh->gh_list, &gl->gl_holders) &&
1708 !test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1709 spin_unlock(&gl->gl_lockref.lock);
1710 gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl);
1711 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1712 spin_lock(&gl->gl_lockref.lock);
1713 }
1714
1715 /*
1716 * If we're in the process of file system withdraw, we cannot just
1717 * dequeue any glocks until our journal is recovered, lest we introduce
1718 * file system corruption. We need two exceptions to this rule: We need
1719 * to allow unlocking of nondisk glocks and the glock for our own
1720 * journal that needs recovery.
1721 */
1722 if (test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags) &&
1723 glock_blocked_by_withdraw(gl) &&
1724 gh->gh_gl != sdp->sd_jinode_gl) {
1725 sdp->sd_glock_dqs_held++;
1726 spin_unlock(&gl->gl_lockref.lock);
1727 might_sleep();
1728 wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY,
1729 TASK_UNINTERRUPTIBLE);
1730 spin_lock(&gl->gl_lockref.lock);
1731 }
1732
1733 __gfs2_glock_dq(gh);
1734 out:
1735 spin_unlock(&gl->gl_lockref.lock);
1736 }
1737
gfs2_glock_dq_wait(struct gfs2_holder * gh)1738 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1739 {
1740 struct gfs2_glock *gl = gh->gh_gl;
1741 gfs2_glock_dq(gh);
1742 might_sleep();
1743 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
1744 }
1745
1746 /**
1747 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1748 * @gh: the holder structure
1749 *
1750 */
1751
gfs2_glock_dq_uninit(struct gfs2_holder * gh)1752 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1753 {
1754 gfs2_glock_dq(gh);
1755 gfs2_holder_uninit(gh);
1756 }
1757
1758 /**
1759 * gfs2_glock_nq_num - acquire a glock based on lock number
1760 * @sdp: the filesystem
1761 * @number: the lock number
1762 * @glops: the glock operations for the type of glock
1763 * @state: the state to acquire the glock in
1764 * @flags: modifier flags for the acquisition
1765 * @gh: the struct gfs2_holder
1766 *
1767 * Returns: errno
1768 */
1769
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)1770 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1771 const struct gfs2_glock_operations *glops,
1772 unsigned int state, u16 flags, struct gfs2_holder *gh)
1773 {
1774 struct gfs2_glock *gl;
1775 int error;
1776
1777 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1778 if (!error) {
1779 error = gfs2_glock_nq_init(gl, state, flags, gh);
1780 gfs2_glock_put(gl);
1781 }
1782
1783 return error;
1784 }
1785
1786 /**
1787 * glock_compare - Compare two struct gfs2_glock structures for sorting
1788 * @arg_a: the first structure
1789 * @arg_b: the second structure
1790 *
1791 */
1792
glock_compare(const void * arg_a,const void * arg_b)1793 static int glock_compare(const void *arg_a, const void *arg_b)
1794 {
1795 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1796 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1797 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1798 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1799
1800 if (a->ln_number > b->ln_number)
1801 return 1;
1802 if (a->ln_number < b->ln_number)
1803 return -1;
1804 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1805 return 0;
1806 }
1807
1808 /**
1809 * nq_m_sync - synchronously acquire more than one glock in deadlock free order
1810 * @num_gh: the number of structures
1811 * @ghs: an array of struct gfs2_holder structures
1812 * @p: placeholder for the holder structure to pass back
1813 *
1814 * Returns: 0 on success (all glocks acquired),
1815 * errno on failure (no glocks acquired)
1816 */
1817
nq_m_sync(unsigned int num_gh,struct gfs2_holder * ghs,struct gfs2_holder ** p)1818 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1819 struct gfs2_holder **p)
1820 {
1821 unsigned int x;
1822 int error = 0;
1823
1824 for (x = 0; x < num_gh; x++)
1825 p[x] = &ghs[x];
1826
1827 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1828
1829 for (x = 0; x < num_gh; x++) {
1830 error = gfs2_glock_nq(p[x]);
1831 if (error) {
1832 while (x--)
1833 gfs2_glock_dq(p[x]);
1834 break;
1835 }
1836 }
1837
1838 return error;
1839 }
1840
1841 /**
1842 * gfs2_glock_nq_m - acquire multiple glocks
1843 * @num_gh: the number of structures
1844 * @ghs: an array of struct gfs2_holder structures
1845 *
1846 * Returns: 0 on success (all glocks acquired),
1847 * errno on failure (no glocks acquired)
1848 */
1849
gfs2_glock_nq_m(unsigned int num_gh,struct gfs2_holder * ghs)1850 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1851 {
1852 struct gfs2_holder *tmp[4];
1853 struct gfs2_holder **pph = tmp;
1854 int error = 0;
1855
1856 switch(num_gh) {
1857 case 0:
1858 return 0;
1859 case 1:
1860 return gfs2_glock_nq(ghs);
1861 default:
1862 if (num_gh <= 4)
1863 break;
1864 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1865 GFP_NOFS);
1866 if (!pph)
1867 return -ENOMEM;
1868 }
1869
1870 error = nq_m_sync(num_gh, ghs, pph);
1871
1872 if (pph != tmp)
1873 kfree(pph);
1874
1875 return error;
1876 }
1877
1878 /**
1879 * gfs2_glock_dq_m - release multiple glocks
1880 * @num_gh: the number of structures
1881 * @ghs: an array of struct gfs2_holder structures
1882 *
1883 */
1884
gfs2_glock_dq_m(unsigned int num_gh,struct gfs2_holder * ghs)1885 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1886 {
1887 while (num_gh--)
1888 gfs2_glock_dq(&ghs[num_gh]);
1889 }
1890
gfs2_glock_cb(struct gfs2_glock * gl,unsigned int state)1891 void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
1892 {
1893 unsigned long delay = 0;
1894 unsigned long holdtime;
1895 unsigned long now = jiffies;
1896
1897 gfs2_glock_hold(gl);
1898 spin_lock(&gl->gl_lockref.lock);
1899 holdtime = gl->gl_tchange + gl->gl_hold_time;
1900 if (!list_empty(&gl->gl_holders) &&
1901 gl->gl_name.ln_type == LM_TYPE_INODE) {
1902 if (time_before(now, holdtime))
1903 delay = holdtime - now;
1904 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1905 delay = gl->gl_hold_time;
1906 }
1907 handle_callback(gl, state, delay, true);
1908 gfs2_glock_queue_work(gl, delay);
1909 spin_unlock(&gl->gl_lockref.lock);
1910 }
1911
1912 /**
1913 * gfs2_should_freeze - Figure out if glock should be frozen
1914 * @gl: The glock in question
1915 *
1916 * Glocks are not frozen if (a) the result of the dlm operation is
1917 * an error, (b) the locking operation was an unlock operation or
1918 * (c) if there is a "noexp" flagged request anywhere in the queue
1919 *
1920 * Returns: 1 if freezing should occur, 0 otherwise
1921 */
1922
gfs2_should_freeze(const struct gfs2_glock * gl)1923 static int gfs2_should_freeze(const struct gfs2_glock *gl)
1924 {
1925 const struct gfs2_holder *gh;
1926
1927 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1928 return 0;
1929 if (gl->gl_target == LM_ST_UNLOCKED)
1930 return 0;
1931
1932 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1933 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1934 continue;
1935 if (LM_FLAG_NOEXP & gh->gh_flags)
1936 return 0;
1937 }
1938
1939 return 1;
1940 }
1941
1942 /**
1943 * gfs2_glock_complete - Callback used by locking
1944 * @gl: Pointer to the glock
1945 * @ret: The return value from the dlm
1946 *
1947 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
1948 * to use a bitfield shared with other glock state fields.
1949 */
1950
gfs2_glock_complete(struct gfs2_glock * gl,int ret)1951 void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
1952 {
1953 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
1954
1955 spin_lock(&gl->gl_lockref.lock);
1956 gl->gl_reply = ret;
1957
1958 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
1959 if (gfs2_should_freeze(gl)) {
1960 set_bit(GLF_FROZEN, &gl->gl_flags);
1961 spin_unlock(&gl->gl_lockref.lock);
1962 return;
1963 }
1964 }
1965
1966 gl->gl_lockref.count++;
1967 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1968 gfs2_glock_queue_work(gl, 0);
1969 spin_unlock(&gl->gl_lockref.lock);
1970 }
1971
glock_cmp(void * priv,const struct list_head * a,const struct list_head * b)1972 static int glock_cmp(void *priv, const struct list_head *a,
1973 const struct list_head *b)
1974 {
1975 struct gfs2_glock *gla, *glb;
1976
1977 gla = list_entry(a, struct gfs2_glock, gl_lru);
1978 glb = list_entry(b, struct gfs2_glock, gl_lru);
1979
1980 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1981 return 1;
1982 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1983 return -1;
1984
1985 return 0;
1986 }
1987
1988 /**
1989 * gfs2_dispose_glock_lru - Demote a list of glocks
1990 * @list: The list to dispose of
1991 *
1992 * Disposing of glocks may involve disk accesses, so that here we sort
1993 * the glocks by number (i.e. disk location of the inodes) so that if
1994 * there are any such accesses, they'll be sent in order (mostly).
1995 *
1996 * Must be called under the lru_lock, but may drop and retake this
1997 * lock. While the lru_lock is dropped, entries may vanish from the
1998 * list, but no new entries will appear on the list (since it is
1999 * private)
2000 */
2001
gfs2_dispose_glock_lru(struct list_head * list)2002 static void gfs2_dispose_glock_lru(struct list_head *list)
2003 __releases(&lru_lock)
2004 __acquires(&lru_lock)
2005 {
2006 struct gfs2_glock *gl;
2007
2008 list_sort(NULL, list, glock_cmp);
2009
2010 while(!list_empty(list)) {
2011 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
2012 list_del_init(&gl->gl_lru);
2013 clear_bit(GLF_LRU, &gl->gl_flags);
2014 if (!spin_trylock(&gl->gl_lockref.lock)) {
2015 add_back_to_lru:
2016 list_add(&gl->gl_lru, &lru_list);
2017 set_bit(GLF_LRU, &gl->gl_flags);
2018 atomic_inc(&lru_count);
2019 continue;
2020 }
2021 if (test_bit(GLF_LOCK, &gl->gl_flags)) {
2022 spin_unlock(&gl->gl_lockref.lock);
2023 goto add_back_to_lru;
2024 }
2025 gl->gl_lockref.count++;
2026 if (demote_ok(gl))
2027 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
2028 gfs2_glock_queue_work(gl, 0);
2029 spin_unlock(&gl->gl_lockref.lock);
2030 cond_resched_lock(&lru_lock);
2031 }
2032 }
2033
2034 /**
2035 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
2036 * @nr: The number of entries to scan
2037 *
2038 * This function selects the entries on the LRU which are able to
2039 * be demoted, and then kicks off the process by calling
2040 * gfs2_dispose_glock_lru() above.
2041 */
2042
gfs2_scan_glock_lru(int nr)2043 static long gfs2_scan_glock_lru(int nr)
2044 {
2045 struct gfs2_glock *gl, *next;
2046 LIST_HEAD(dispose);
2047 long freed = 0;
2048
2049 spin_lock(&lru_lock);
2050 list_for_each_entry_safe(gl, next, &lru_list, gl_lru) {
2051 if (nr-- <= 0)
2052 break;
2053 /* Test for being demotable */
2054 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
2055 if (!spin_trylock(&gl->gl_lockref.lock))
2056 continue;
2057 if (gl->gl_lockref.count <= 1 &&
2058 (gl->gl_state == LM_ST_UNLOCKED ||
2059 demote_ok(gl))) {
2060 list_move(&gl->gl_lru, &dispose);
2061 atomic_dec(&lru_count);
2062 freed++;
2063 }
2064 spin_unlock(&gl->gl_lockref.lock);
2065 }
2066 }
2067 if (!list_empty(&dispose))
2068 gfs2_dispose_glock_lru(&dispose);
2069 spin_unlock(&lru_lock);
2070
2071 return freed;
2072 }
2073
gfs2_glock_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)2074 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
2075 struct shrink_control *sc)
2076 {
2077 if (!(sc->gfp_mask & __GFP_FS))
2078 return SHRINK_STOP;
2079 return gfs2_scan_glock_lru(sc->nr_to_scan);
2080 }
2081
gfs2_glock_shrink_count(struct shrinker * shrink,struct shrink_control * sc)2082 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
2083 struct shrink_control *sc)
2084 {
2085 return vfs_pressure_ratio(atomic_read(&lru_count));
2086 }
2087
2088 static struct shrinker glock_shrinker = {
2089 .seeks = DEFAULT_SEEKS,
2090 .count_objects = gfs2_glock_shrink_count,
2091 .scan_objects = gfs2_glock_shrink_scan,
2092 };
2093
2094 /**
2095 * glock_hash_walk - Call a function for glock in a hash bucket
2096 * @examiner: the function
2097 * @sdp: the filesystem
2098 *
2099 * Note that the function can be called multiple times on the same
2100 * object. So the user must ensure that the function can cope with
2101 * that.
2102 */
2103
glock_hash_walk(glock_examiner examiner,const struct gfs2_sbd * sdp)2104 static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
2105 {
2106 struct gfs2_glock *gl;
2107 struct rhashtable_iter iter;
2108
2109 rhashtable_walk_enter(&gl_hash_table, &iter);
2110
2111 do {
2112 rhashtable_walk_start(&iter);
2113
2114 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) {
2115 if (gl->gl_name.ln_sbd == sdp)
2116 examiner(gl);
2117 }
2118
2119 rhashtable_walk_stop(&iter);
2120 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
2121
2122 rhashtable_walk_exit(&iter);
2123 }
2124
gfs2_cancel_delete_work(struct gfs2_glock * gl)2125 void gfs2_cancel_delete_work(struct gfs2_glock *gl)
2126 {
2127 clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags);
2128 clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
2129 if (cancel_delayed_work(&gl->gl_delete))
2130 gfs2_glock_put(gl);
2131 }
2132
flush_delete_work(struct gfs2_glock * gl)2133 static void flush_delete_work(struct gfs2_glock *gl)
2134 {
2135 if (gl->gl_name.ln_type == LM_TYPE_IOPEN) {
2136 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2137
2138 if (cancel_delayed_work(&gl->gl_delete)) {
2139 queue_delayed_work(sdp->sd_delete_wq,
2140 &gl->gl_delete, 0);
2141 }
2142 }
2143 }
2144
gfs2_flush_delete_work(struct gfs2_sbd * sdp)2145 void gfs2_flush_delete_work(struct gfs2_sbd *sdp)
2146 {
2147 glock_hash_walk(flush_delete_work, sdp);
2148 flush_workqueue(sdp->sd_delete_wq);
2149 }
2150
2151 /**
2152 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
2153 * @gl: The glock to thaw
2154 *
2155 */
2156
thaw_glock(struct gfs2_glock * gl)2157 static void thaw_glock(struct gfs2_glock *gl)
2158 {
2159 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
2160 return;
2161 if (!lockref_get_not_dead(&gl->gl_lockref))
2162 return;
2163
2164 spin_lock(&gl->gl_lockref.lock);
2165 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
2166 gfs2_glock_queue_work(gl, 0);
2167 spin_unlock(&gl->gl_lockref.lock);
2168 }
2169
2170 /**
2171 * clear_glock - look at a glock and see if we can free it from glock cache
2172 * @gl: the glock to look at
2173 *
2174 */
2175
clear_glock(struct gfs2_glock * gl)2176 static void clear_glock(struct gfs2_glock *gl)
2177 {
2178 gfs2_glock_remove_from_lru(gl);
2179
2180 spin_lock(&gl->gl_lockref.lock);
2181 if (!__lockref_is_dead(&gl->gl_lockref)) {
2182 gl->gl_lockref.count++;
2183 if (gl->gl_state != LM_ST_UNLOCKED)
2184 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
2185 gfs2_glock_queue_work(gl, 0);
2186 }
2187 spin_unlock(&gl->gl_lockref.lock);
2188 }
2189
2190 /**
2191 * gfs2_glock_thaw - Thaw any frozen glocks
2192 * @sdp: The super block
2193 *
2194 */
2195
gfs2_glock_thaw(struct gfs2_sbd * sdp)2196 void gfs2_glock_thaw(struct gfs2_sbd *sdp)
2197 {
2198 glock_hash_walk(thaw_glock, sdp);
2199 }
2200
dump_glock(struct seq_file * seq,struct gfs2_glock * gl,bool fsid)2201 static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2202 {
2203 spin_lock(&gl->gl_lockref.lock);
2204 gfs2_dump_glock(seq, gl, fsid);
2205 spin_unlock(&gl->gl_lockref.lock);
2206 }
2207
dump_glock_func(struct gfs2_glock * gl)2208 static void dump_glock_func(struct gfs2_glock *gl)
2209 {
2210 dump_glock(NULL, gl, true);
2211 }
2212
withdraw_dq(struct gfs2_glock * gl)2213 static void withdraw_dq(struct gfs2_glock *gl)
2214 {
2215 spin_lock(&gl->gl_lockref.lock);
2216 if (!__lockref_is_dead(&gl->gl_lockref) &&
2217 glock_blocked_by_withdraw(gl))
2218 do_error(gl, LM_OUT_ERROR); /* remove pending waiters */
2219 spin_unlock(&gl->gl_lockref.lock);
2220 }
2221
gfs2_gl_dq_holders(struct gfs2_sbd * sdp)2222 void gfs2_gl_dq_holders(struct gfs2_sbd *sdp)
2223 {
2224 glock_hash_walk(withdraw_dq, sdp);
2225 }
2226
2227 /**
2228 * gfs2_gl_hash_clear - Empty out the glock hash table
2229 * @sdp: the filesystem
2230 *
2231 * Called when unmounting the filesystem.
2232 */
2233
gfs2_gl_hash_clear(struct gfs2_sbd * sdp)2234 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
2235 {
2236 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
2237 flush_workqueue(glock_workqueue);
2238 glock_hash_walk(clear_glock, sdp);
2239 flush_workqueue(glock_workqueue);
2240 wait_event_timeout(sdp->sd_kill_wait,
2241 atomic_read(&sdp->sd_glock_disposal) == 0,
2242 HZ * 600);
2243 gfs2_lm_unmount(sdp);
2244 gfs2_free_dead_glocks(sdp);
2245 glock_hash_walk(dump_glock_func, sdp);
2246 }
2247
state2str(unsigned state)2248 static const char *state2str(unsigned state)
2249 {
2250 switch(state) {
2251 case LM_ST_UNLOCKED:
2252 return "UN";
2253 case LM_ST_SHARED:
2254 return "SH";
2255 case LM_ST_DEFERRED:
2256 return "DF";
2257 case LM_ST_EXCLUSIVE:
2258 return "EX";
2259 }
2260 return "??";
2261 }
2262
hflags2str(char * buf,u16 flags,unsigned long iflags)2263 static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
2264 {
2265 char *p = buf;
2266 if (flags & LM_FLAG_TRY)
2267 *p++ = 't';
2268 if (flags & LM_FLAG_TRY_1CB)
2269 *p++ = 'T';
2270 if (flags & LM_FLAG_NOEXP)
2271 *p++ = 'e';
2272 if (flags & LM_FLAG_ANY)
2273 *p++ = 'A';
2274 if (flags & LM_FLAG_NODE_SCOPE)
2275 *p++ = 'n';
2276 if (flags & GL_ASYNC)
2277 *p++ = 'a';
2278 if (flags & GL_EXACT)
2279 *p++ = 'E';
2280 if (flags & GL_NOCACHE)
2281 *p++ = 'c';
2282 if (test_bit(HIF_HOLDER, &iflags))
2283 *p++ = 'H';
2284 if (test_bit(HIF_WAIT, &iflags))
2285 *p++ = 'W';
2286 if (flags & GL_SKIP)
2287 *p++ = 's';
2288 *p = 0;
2289 return buf;
2290 }
2291
2292 /**
2293 * dump_holder - print information about a glock holder
2294 * @seq: the seq_file struct
2295 * @gh: the glock holder
2296 * @fs_id_buf: pointer to file system id (if requested)
2297 *
2298 */
2299
dump_holder(struct seq_file * seq,const struct gfs2_holder * gh,const char * fs_id_buf)2300 static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh,
2301 const char *fs_id_buf)
2302 {
2303 const char *comm = "(none)";
2304 pid_t owner_pid = 0;
2305 char flags_buf[32];
2306
2307 rcu_read_lock();
2308 if (pid_is_meaningful(gh)) {
2309 struct task_struct *gh_owner;
2310
2311 comm = "(ended)";
2312 owner_pid = pid_nr(gh->gh_owner_pid);
2313 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
2314 if (gh_owner)
2315 comm = gh_owner->comm;
2316 }
2317 gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
2318 fs_id_buf, state2str(gh->gh_state),
2319 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
2320 gh->gh_error, (long)owner_pid, comm, (void *)gh->gh_ip);
2321 rcu_read_unlock();
2322 }
2323
gflags2str(char * buf,const struct gfs2_glock * gl)2324 static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
2325 {
2326 const unsigned long *gflags = &gl->gl_flags;
2327 char *p = buf;
2328
2329 if (test_bit(GLF_LOCK, gflags))
2330 *p++ = 'l';
2331 if (test_bit(GLF_DEMOTE, gflags))
2332 *p++ = 'D';
2333 if (test_bit(GLF_PENDING_DEMOTE, gflags))
2334 *p++ = 'd';
2335 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
2336 *p++ = 'p';
2337 if (test_bit(GLF_DIRTY, gflags))
2338 *p++ = 'y';
2339 if (test_bit(GLF_LFLUSH, gflags))
2340 *p++ = 'f';
2341 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
2342 *p++ = 'i';
2343 if (test_bit(GLF_REPLY_PENDING, gflags))
2344 *p++ = 'r';
2345 if (test_bit(GLF_INITIAL, gflags))
2346 *p++ = 'I';
2347 if (test_bit(GLF_FROZEN, gflags))
2348 *p++ = 'F';
2349 if (!list_empty(&gl->gl_holders))
2350 *p++ = 'q';
2351 if (test_bit(GLF_LRU, gflags))
2352 *p++ = 'L';
2353 if (gl->gl_object)
2354 *p++ = 'o';
2355 if (test_bit(GLF_BLOCKING, gflags))
2356 *p++ = 'b';
2357 if (test_bit(GLF_FREEING, gflags))
2358 *p++ = 'x';
2359 if (test_bit(GLF_INSTANTIATE_NEEDED, gflags))
2360 *p++ = 'n';
2361 if (test_bit(GLF_INSTANTIATE_IN_PROG, gflags))
2362 *p++ = 'N';
2363 if (test_bit(GLF_TRY_TO_EVICT, gflags))
2364 *p++ = 'e';
2365 if (test_bit(GLF_VERIFY_DELETE, gflags))
2366 *p++ = 'E';
2367 *p = 0;
2368 return buf;
2369 }
2370
2371 /**
2372 * gfs2_dump_glock - print information about a glock
2373 * @seq: The seq_file struct
2374 * @gl: the glock
2375 * @fsid: If true, also dump the file system id
2376 *
2377 * The file format is as follows:
2378 * One line per object, capital letters are used to indicate objects
2379 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
2380 * other objects are indented by a single space and follow the glock to
2381 * which they are related. Fields are indicated by lower case letters
2382 * followed by a colon and the field value, except for strings which are in
2383 * [] so that its possible to see if they are composed of spaces for
2384 * example. The field's are n = number (id of the object), f = flags,
2385 * t = type, s = state, r = refcount, e = error, p = pid.
2386 *
2387 */
2388
gfs2_dump_glock(struct seq_file * seq,struct gfs2_glock * gl,bool fsid)2389 void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2390 {
2391 const struct gfs2_glock_operations *glops = gl->gl_ops;
2392 unsigned long long dtime;
2393 const struct gfs2_holder *gh;
2394 char gflags_buf[32];
2395 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2396 char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
2397 unsigned long nrpages = 0;
2398
2399 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
2400 struct address_space *mapping = gfs2_glock2aspace(gl);
2401
2402 nrpages = mapping->nrpages;
2403 }
2404 memset(fs_id_buf, 0, sizeof(fs_id_buf));
2405 if (fsid && sdp) /* safety precaution */
2406 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
2407 dtime = jiffies - gl->gl_demote_time;
2408 dtime *= 1000000/HZ; /* demote time in uSec */
2409 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
2410 dtime = 0;
2411 gfs2_print_dbg(seq, "%sG: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d "
2412 "v:%d r:%d m:%ld p:%lu\n",
2413 fs_id_buf, state2str(gl->gl_state),
2414 gl->gl_name.ln_type,
2415 (unsigned long long)gl->gl_name.ln_number,
2416 gflags2str(gflags_buf, gl),
2417 state2str(gl->gl_target),
2418 state2str(gl->gl_demote_state), dtime,
2419 atomic_read(&gl->gl_ail_count),
2420 atomic_read(&gl->gl_revokes),
2421 (int)gl->gl_lockref.count, gl->gl_hold_time, nrpages);
2422
2423 list_for_each_entry(gh, &gl->gl_holders, gh_list)
2424 dump_holder(seq, gh, fs_id_buf);
2425
2426 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
2427 glops->go_dump(seq, gl, fs_id_buf);
2428 }
2429
gfs2_glstats_seq_show(struct seq_file * seq,void * iter_ptr)2430 static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
2431 {
2432 struct gfs2_glock *gl = iter_ptr;
2433
2434 seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
2435 gl->gl_name.ln_type,
2436 (unsigned long long)gl->gl_name.ln_number,
2437 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
2438 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
2439 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
2440 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
2441 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
2442 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
2443 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
2444 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
2445 return 0;
2446 }
2447
2448 static const char *gfs2_gltype[] = {
2449 "type",
2450 "reserved",
2451 "nondisk",
2452 "inode",
2453 "rgrp",
2454 "meta",
2455 "iopen",
2456 "flock",
2457 "plock",
2458 "quota",
2459 "journal",
2460 };
2461
2462 static const char *gfs2_stype[] = {
2463 [GFS2_LKS_SRTT] = "srtt",
2464 [GFS2_LKS_SRTTVAR] = "srttvar",
2465 [GFS2_LKS_SRTTB] = "srttb",
2466 [GFS2_LKS_SRTTVARB] = "srttvarb",
2467 [GFS2_LKS_SIRT] = "sirt",
2468 [GFS2_LKS_SIRTVAR] = "sirtvar",
2469 [GFS2_LKS_DCOUNT] = "dlm",
2470 [GFS2_LKS_QCOUNT] = "queue",
2471 };
2472
2473 #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
2474
gfs2_sbstats_seq_show(struct seq_file * seq,void * iter_ptr)2475 static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
2476 {
2477 struct gfs2_sbd *sdp = seq->private;
2478 loff_t pos = *(loff_t *)iter_ptr;
2479 unsigned index = pos >> 3;
2480 unsigned subindex = pos & 0x07;
2481 int i;
2482
2483 if (index == 0 && subindex != 0)
2484 return 0;
2485
2486 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
2487 (index == 0) ? "cpu": gfs2_stype[subindex]);
2488
2489 for_each_possible_cpu(i) {
2490 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
2491
2492 if (index == 0)
2493 seq_printf(seq, " %15u", i);
2494 else
2495 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
2496 lkstats[index - 1].stats[subindex]);
2497 }
2498 seq_putc(seq, '\n');
2499 return 0;
2500 }
2501
gfs2_glock_init(void)2502 int __init gfs2_glock_init(void)
2503 {
2504 int i, ret;
2505
2506 ret = rhashtable_init(&gl_hash_table, &ht_parms);
2507 if (ret < 0)
2508 return ret;
2509
2510 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
2511 WQ_HIGHPRI | WQ_FREEZABLE, 0);
2512 if (!glock_workqueue) {
2513 rhashtable_destroy(&gl_hash_table);
2514 return -ENOMEM;
2515 }
2516
2517 ret = register_shrinker(&glock_shrinker, "gfs2-glock");
2518 if (ret) {
2519 destroy_workqueue(glock_workqueue);
2520 rhashtable_destroy(&gl_hash_table);
2521 return ret;
2522 }
2523
2524 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
2525 init_waitqueue_head(glock_wait_table + i);
2526
2527 return 0;
2528 }
2529
gfs2_glock_exit(void)2530 void gfs2_glock_exit(void)
2531 {
2532 unregister_shrinker(&glock_shrinker);
2533 rhashtable_destroy(&gl_hash_table);
2534 destroy_workqueue(glock_workqueue);
2535 }
2536
gfs2_glock_iter_next(struct gfs2_glock_iter * gi,loff_t n)2537 static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
2538 {
2539 struct gfs2_glock *gl = gi->gl;
2540
2541 if (gl) {
2542 if (n == 0)
2543 return;
2544 gfs2_glock_put_async(gl);
2545 }
2546 for (;;) {
2547 gl = rhashtable_walk_next(&gi->hti);
2548 if (IS_ERR_OR_NULL(gl)) {
2549 if (gl == ERR_PTR(-EAGAIN)) {
2550 n = 1;
2551 continue;
2552 }
2553 gl = NULL;
2554 break;
2555 }
2556 if (gl->gl_name.ln_sbd != gi->sdp)
2557 continue;
2558 if (n <= 1) {
2559 if (!lockref_get_not_dead(&gl->gl_lockref))
2560 continue;
2561 break;
2562 } else {
2563 if (__lockref_is_dead(&gl->gl_lockref))
2564 continue;
2565 n--;
2566 }
2567 }
2568 gi->gl = gl;
2569 }
2570
gfs2_glock_seq_start(struct seq_file * seq,loff_t * pos)2571 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
2572 __acquires(RCU)
2573 {
2574 struct gfs2_glock_iter *gi = seq->private;
2575 loff_t n;
2576
2577 /*
2578 * We can either stay where we are, skip to the next hash table
2579 * entry, or start from the beginning.
2580 */
2581 if (*pos < gi->last_pos) {
2582 rhashtable_walk_exit(&gi->hti);
2583 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2584 n = *pos + 1;
2585 } else {
2586 n = *pos - gi->last_pos;
2587 }
2588
2589 rhashtable_walk_start(&gi->hti);
2590
2591 gfs2_glock_iter_next(gi, n);
2592 gi->last_pos = *pos;
2593 return gi->gl;
2594 }
2595
gfs2_glock_seq_next(struct seq_file * seq,void * iter_ptr,loff_t * pos)2596 static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
2597 loff_t *pos)
2598 {
2599 struct gfs2_glock_iter *gi = seq->private;
2600
2601 (*pos)++;
2602 gi->last_pos = *pos;
2603 gfs2_glock_iter_next(gi, 1);
2604 return gi->gl;
2605 }
2606
gfs2_glock_seq_stop(struct seq_file * seq,void * iter_ptr)2607 static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
2608 __releases(RCU)
2609 {
2610 struct gfs2_glock_iter *gi = seq->private;
2611
2612 rhashtable_walk_stop(&gi->hti);
2613 }
2614
gfs2_glock_seq_show(struct seq_file * seq,void * iter_ptr)2615 static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
2616 {
2617 dump_glock(seq, iter_ptr, false);
2618 return 0;
2619 }
2620
gfs2_sbstats_seq_start(struct seq_file * seq,loff_t * pos)2621 static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2622 {
2623 preempt_disable();
2624 if (*pos >= GFS2_NR_SBSTATS)
2625 return NULL;
2626 return pos;
2627 }
2628
gfs2_sbstats_seq_next(struct seq_file * seq,void * iter_ptr,loff_t * pos)2629 static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2630 loff_t *pos)
2631 {
2632 (*pos)++;
2633 if (*pos >= GFS2_NR_SBSTATS)
2634 return NULL;
2635 return pos;
2636 }
2637
gfs2_sbstats_seq_stop(struct seq_file * seq,void * iter_ptr)2638 static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2639 {
2640 preempt_enable();
2641 }
2642
2643 static const struct seq_operations gfs2_glock_seq_ops = {
2644 .start = gfs2_glock_seq_start,
2645 .next = gfs2_glock_seq_next,
2646 .stop = gfs2_glock_seq_stop,
2647 .show = gfs2_glock_seq_show,
2648 };
2649
2650 static const struct seq_operations gfs2_glstats_seq_ops = {
2651 .start = gfs2_glock_seq_start,
2652 .next = gfs2_glock_seq_next,
2653 .stop = gfs2_glock_seq_stop,
2654 .show = gfs2_glstats_seq_show,
2655 };
2656
2657 static const struct seq_operations gfs2_sbstats_sops = {
2658 .start = gfs2_sbstats_seq_start,
2659 .next = gfs2_sbstats_seq_next,
2660 .stop = gfs2_sbstats_seq_stop,
2661 .show = gfs2_sbstats_seq_show,
2662 };
2663
2664 #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2665
__gfs2_glocks_open(struct inode * inode,struct file * file,const struct seq_operations * ops)2666 static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2667 const struct seq_operations *ops)
2668 {
2669 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
2670 if (ret == 0) {
2671 struct seq_file *seq = file->private_data;
2672 struct gfs2_glock_iter *gi = seq->private;
2673
2674 gi->sdp = inode->i_private;
2675 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
2676 if (seq->buf)
2677 seq->size = GFS2_SEQ_GOODSIZE;
2678 /*
2679 * Initially, we are "before" the first hash table entry; the
2680 * first call to rhashtable_walk_next gets us the first entry.
2681 */
2682 gi->last_pos = -1;
2683 gi->gl = NULL;
2684 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2685 }
2686 return ret;
2687 }
2688
gfs2_glocks_open(struct inode * inode,struct file * file)2689 static int gfs2_glocks_open(struct inode *inode, struct file *file)
2690 {
2691 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2692 }
2693
gfs2_glocks_release(struct inode * inode,struct file * file)2694 static int gfs2_glocks_release(struct inode *inode, struct file *file)
2695 {
2696 struct seq_file *seq = file->private_data;
2697 struct gfs2_glock_iter *gi = seq->private;
2698
2699 if (gi->gl)
2700 gfs2_glock_put(gi->gl);
2701 rhashtable_walk_exit(&gi->hti);
2702 return seq_release_private(inode, file);
2703 }
2704
gfs2_glstats_open(struct inode * inode,struct file * file)2705 static int gfs2_glstats_open(struct inode *inode, struct file *file)
2706 {
2707 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
2708 }
2709
2710 static const struct file_operations gfs2_glocks_fops = {
2711 .owner = THIS_MODULE,
2712 .open = gfs2_glocks_open,
2713 .read = seq_read,
2714 .llseek = seq_lseek,
2715 .release = gfs2_glocks_release,
2716 };
2717
2718 static const struct file_operations gfs2_glstats_fops = {
2719 .owner = THIS_MODULE,
2720 .open = gfs2_glstats_open,
2721 .read = seq_read,
2722 .llseek = seq_lseek,
2723 .release = gfs2_glocks_release,
2724 };
2725
2726 struct gfs2_glockfd_iter {
2727 struct super_block *sb;
2728 unsigned int tgid;
2729 struct task_struct *task;
2730 unsigned int fd;
2731 struct file *file;
2732 };
2733
gfs2_glockfd_next_task(struct gfs2_glockfd_iter * i)2734 static struct task_struct *gfs2_glockfd_next_task(struct gfs2_glockfd_iter *i)
2735 {
2736 struct pid_namespace *ns = task_active_pid_ns(current);
2737 struct pid *pid;
2738
2739 if (i->task)
2740 put_task_struct(i->task);
2741
2742 rcu_read_lock();
2743 retry:
2744 i->task = NULL;
2745 pid = find_ge_pid(i->tgid, ns);
2746 if (pid) {
2747 i->tgid = pid_nr_ns(pid, ns);
2748 i->task = pid_task(pid, PIDTYPE_TGID);
2749 if (!i->task) {
2750 i->tgid++;
2751 goto retry;
2752 }
2753 get_task_struct(i->task);
2754 }
2755 rcu_read_unlock();
2756 return i->task;
2757 }
2758
gfs2_glockfd_next_file(struct gfs2_glockfd_iter * i)2759 static struct file *gfs2_glockfd_next_file(struct gfs2_glockfd_iter *i)
2760 {
2761 if (i->file) {
2762 fput(i->file);
2763 i->file = NULL;
2764 }
2765
2766 rcu_read_lock();
2767 for(;; i->fd++) {
2768 struct inode *inode;
2769
2770 i->file = task_lookup_next_fd_rcu(i->task, &i->fd);
2771 if (!i->file) {
2772 i->fd = 0;
2773 break;
2774 }
2775 inode = file_inode(i->file);
2776 if (inode->i_sb != i->sb)
2777 continue;
2778 if (get_file_rcu(i->file))
2779 break;
2780 }
2781 rcu_read_unlock();
2782 return i->file;
2783 }
2784
gfs2_glockfd_seq_start(struct seq_file * seq,loff_t * pos)2785 static void *gfs2_glockfd_seq_start(struct seq_file *seq, loff_t *pos)
2786 {
2787 struct gfs2_glockfd_iter *i = seq->private;
2788
2789 if (*pos)
2790 return NULL;
2791 while (gfs2_glockfd_next_task(i)) {
2792 if (gfs2_glockfd_next_file(i))
2793 return i;
2794 i->tgid++;
2795 }
2796 return NULL;
2797 }
2798
gfs2_glockfd_seq_next(struct seq_file * seq,void * iter_ptr,loff_t * pos)2799 static void *gfs2_glockfd_seq_next(struct seq_file *seq, void *iter_ptr,
2800 loff_t *pos)
2801 {
2802 struct gfs2_glockfd_iter *i = seq->private;
2803
2804 (*pos)++;
2805 i->fd++;
2806 do {
2807 if (gfs2_glockfd_next_file(i))
2808 return i;
2809 i->tgid++;
2810 } while (gfs2_glockfd_next_task(i));
2811 return NULL;
2812 }
2813
gfs2_glockfd_seq_stop(struct seq_file * seq,void * iter_ptr)2814 static void gfs2_glockfd_seq_stop(struct seq_file *seq, void *iter_ptr)
2815 {
2816 struct gfs2_glockfd_iter *i = seq->private;
2817
2818 if (i->file)
2819 fput(i->file);
2820 if (i->task)
2821 put_task_struct(i->task);
2822 }
2823
gfs2_glockfd_seq_show_flock(struct seq_file * seq,struct gfs2_glockfd_iter * i)2824 static void gfs2_glockfd_seq_show_flock(struct seq_file *seq,
2825 struct gfs2_glockfd_iter *i)
2826 {
2827 struct gfs2_file *fp = i->file->private_data;
2828 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
2829 struct lm_lockname gl_name = { .ln_type = LM_TYPE_RESERVED };
2830
2831 if (!READ_ONCE(fl_gh->gh_gl))
2832 return;
2833
2834 spin_lock(&i->file->f_lock);
2835 if (gfs2_holder_initialized(fl_gh))
2836 gl_name = fl_gh->gh_gl->gl_name;
2837 spin_unlock(&i->file->f_lock);
2838
2839 if (gl_name.ln_type != LM_TYPE_RESERVED) {
2840 seq_printf(seq, "%d %u %u/%llx\n",
2841 i->tgid, i->fd, gl_name.ln_type,
2842 (unsigned long long)gl_name.ln_number);
2843 }
2844 }
2845
gfs2_glockfd_seq_show(struct seq_file * seq,void * iter_ptr)2846 static int gfs2_glockfd_seq_show(struct seq_file *seq, void *iter_ptr)
2847 {
2848 struct gfs2_glockfd_iter *i = seq->private;
2849 struct inode *inode = file_inode(i->file);
2850 struct gfs2_glock *gl;
2851
2852 inode_lock_shared(inode);
2853 gl = GFS2_I(inode)->i_iopen_gh.gh_gl;
2854 if (gl) {
2855 seq_printf(seq, "%d %u %u/%llx\n",
2856 i->tgid, i->fd, gl->gl_name.ln_type,
2857 (unsigned long long)gl->gl_name.ln_number);
2858 }
2859 gfs2_glockfd_seq_show_flock(seq, i);
2860 inode_unlock_shared(inode);
2861 return 0;
2862 }
2863
2864 static const struct seq_operations gfs2_glockfd_seq_ops = {
2865 .start = gfs2_glockfd_seq_start,
2866 .next = gfs2_glockfd_seq_next,
2867 .stop = gfs2_glockfd_seq_stop,
2868 .show = gfs2_glockfd_seq_show,
2869 };
2870
gfs2_glockfd_open(struct inode * inode,struct file * file)2871 static int gfs2_glockfd_open(struct inode *inode, struct file *file)
2872 {
2873 struct gfs2_glockfd_iter *i;
2874 struct gfs2_sbd *sdp = inode->i_private;
2875
2876 i = __seq_open_private(file, &gfs2_glockfd_seq_ops,
2877 sizeof(struct gfs2_glockfd_iter));
2878 if (!i)
2879 return -ENOMEM;
2880 i->sb = sdp->sd_vfs;
2881 return 0;
2882 }
2883
2884 static const struct file_operations gfs2_glockfd_fops = {
2885 .owner = THIS_MODULE,
2886 .open = gfs2_glockfd_open,
2887 .read = seq_read,
2888 .llseek = seq_lseek,
2889 .release = seq_release_private,
2890 };
2891
2892 DEFINE_SEQ_ATTRIBUTE(gfs2_sbstats);
2893
gfs2_create_debugfs_file(struct gfs2_sbd * sdp)2894 void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2895 {
2896 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2897
2898 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2899 &gfs2_glocks_fops);
2900
2901 debugfs_create_file("glockfd", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2902 &gfs2_glockfd_fops);
2903
2904 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2905 &gfs2_glstats_fops);
2906
2907 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2908 &gfs2_sbstats_fops);
2909 }
2910
gfs2_delete_debugfs_file(struct gfs2_sbd * sdp)2911 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2912 {
2913 debugfs_remove_recursive(sdp->debugfs_dir);
2914 sdp->debugfs_dir = NULL;
2915 }
2916
gfs2_register_debugfs(void)2917 void gfs2_register_debugfs(void)
2918 {
2919 gfs2_root = debugfs_create_dir("gfs2", NULL);
2920 }
2921
gfs2_unregister_debugfs(void)2922 void gfs2_unregister_debugfs(void)
2923 {
2924 debugfs_remove(gfs2_root);
2925 gfs2_root = NULL;
2926 }
2927