]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/gfs2/glock.c
GFS2: quota allows exceeding hard limit
[mirror_ubuntu-bionic-kernel.git] / fs / gfs2 / glock.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
cf45b752 3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
b3b94faa
DT
13#include <linux/buffer_head.h>
14#include <linux/delay.h>
15#include <linux/sort.h>
16#include <linux/jhash.h>
d0dc80db 17#include <linux/kallsyms.h>
5c676f6d 18#include <linux/gfs2_ondisk.h>
24264434 19#include <linux/list.h>
fee852e3 20#include <linux/wait.h>
95d97b7d 21#include <linux/module.h>
b3b94faa 22#include <asm/uaccess.h>
7c52b166
RP
23#include <linux/seq_file.h>
24#include <linux/debugfs.h>
8fbbfd21
SW
25#include <linux/kthread.h>
26#include <linux/freezer.h>
c4f68a13
BM
27#include <linux/workqueue.h>
28#include <linux/jiffies.h>
bc015cb8
SW
29#include <linux/rcupdate.h>
30#include <linux/rculist_bl.h>
31#include <linux/bit_spinlock.h>
b3b94faa
DT
32
33#include "gfs2.h"
5c676f6d 34#include "incore.h"
b3b94faa
DT
35#include "glock.h"
36#include "glops.h"
37#include "inode.h"
b3b94faa
DT
38#include "lops.h"
39#include "meta_io.h"
40#include "quota.h"
41#include "super.h"
5c676f6d 42#include "util.h"
813e0c46 43#include "bmap.h"
63997775
SW
44#define CREATE_TRACE_POINTS
45#include "trace_gfs2.h"
b3b94faa 46
6802e340
SW
47struct gfs2_glock_iter {
48 int hash; /* hash bucket index */
49 struct gfs2_sbd *sdp; /* incore superblock */
50 struct gfs2_glock *gl; /* current glock struct */
51 char string[512]; /* scratch space */
7c52b166
RP
52};
53
b3b94faa
DT
54typedef void (*glock_examiner) (struct gfs2_glock * gl);
55
6802e340
SW
56static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
57#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
58static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
c4f68a13 59
7c52b166 60static struct dentry *gfs2_root;
c4f68a13 61static struct workqueue_struct *glock_workqueue;
b94a170e 62struct workqueue_struct *gfs2_delete_workqueue;
97cc1025
SW
63static LIST_HEAD(lru_list);
64static atomic_t lru_count = ATOMIC_INIT(0);
eb8374e7 65static DEFINE_SPINLOCK(lru_lock);
08bc2dbc 66
b6397893 67#define GFS2_GL_HASH_SHIFT 15
087efdd3
SW
68#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
69#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
70
bc015cb8 71static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
04b933f2 72static struct dentry *gfs2_root;
087efdd3 73
b3b94faa
DT
74/**
75 * gl_hash() - Turn glock number into hash bucket number
76 * @lock: The glock number
77 *
78 * Returns: The number of the corresponding hash bucket
79 */
80
b8547856
SW
81static unsigned int gl_hash(const struct gfs2_sbd *sdp,
82 const struct lm_lockname *name)
b3b94faa
DT
83{
84 unsigned int h;
85
cd915493 86 h = jhash(&name->ln_number, sizeof(u64), 0);
b3b94faa 87 h = jhash(&name->ln_type, sizeof(unsigned int), h);
b8547856 88 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
b3b94faa
DT
89 h &= GFS2_GL_HASH_MASK;
90
91 return h;
92}
93
bc015cb8
SW
94static inline void spin_lock_bucket(unsigned int hash)
95{
96 struct hlist_bl_head *bl = &gl_hash_table[hash];
97 bit_spin_lock(0, (unsigned long *)bl);
98}
99
100static inline void spin_unlock_bucket(unsigned int hash)
101{
102 struct hlist_bl_head *bl = &gl_hash_table[hash];
103 __bit_spin_unlock(0, (unsigned long *)bl);
104}
b3b94faa 105
bc015cb8 106void gfs2_glock_free(struct rcu_head *rcu)
b3b94faa 107{
bc015cb8 108 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
b3b94faa 109 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa 110
bc015cb8
SW
111 if (gl->gl_ops->go_flags & GLOF_ASPACE)
112 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
113 else
114 kmem_cache_free(gfs2_glock_cachep, gl);
115
116 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
117 wake_up(&sdp->sd_glock_wait);
b3b94faa
DT
118}
119
120/**
121 * gfs2_glock_hold() - increment reference count on glock
122 * @gl: The glock to hold
123 *
124 */
125
b94a170e 126void gfs2_glock_hold(struct gfs2_glock *gl)
b3b94faa 127{
d8348de0 128 GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
16feb9fe 129 atomic_inc(&gl->gl_ref);
b3b94faa
DT
130}
131
8ff22a6f
BM
132/**
133 * demote_ok - Check to see if it's ok to unlock a glock
134 * @gl: the glock
135 *
136 * Returns: 1 if it's ok
137 */
138
139static int demote_ok(const struct gfs2_glock *gl)
140{
141 const struct gfs2_glock_operations *glops = gl->gl_ops;
142
bc015cb8
SW
143 /* assert_spin_locked(&gl->gl_spin); */
144
8ff22a6f
BM
145 if (gl->gl_state == LM_ST_UNLOCKED)
146 return 0;
bc015cb8
SW
147 if (test_bit(GLF_LFLUSH, &gl->gl_flags))
148 return 0;
149 if ((gl->gl_name.ln_type != LM_TYPE_INODE) &&
150 !list_empty(&gl->gl_holders))
8ff22a6f
BM
151 return 0;
152 if (glops->go_demote_ok)
153 return glops->go_demote_ok(gl);
154 return 1;
155}
156
bc015cb8 157
97cc1025 158/**
bc015cb8 159 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
97cc1025
SW
160 * @gl: the glock
161 *
bc015cb8
SW
162 * If the glock is demotable, then we add it (or move it) to the end
163 * of the glock LRU list.
97cc1025
SW
164 */
165
bc015cb8 166static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
97cc1025 167{
bc015cb8
SW
168 if (demote_ok(gl)) {
169 spin_lock(&lru_lock);
170
171 if (!list_empty(&gl->gl_lru))
172 list_del_init(&gl->gl_lru);
173 else
174 atomic_inc(&lru_count);
175
97cc1025 176 list_add_tail(&gl->gl_lru, &lru_list);
bc015cb8 177 spin_unlock(&lru_lock);
97cc1025 178 }
bc015cb8
SW
179}
180
181void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
182{
183 spin_lock(&gl->gl_spin);
184 __gfs2_glock_schedule_for_reclaim(gl);
185 spin_unlock(&gl->gl_spin);
97cc1025
SW
186}
187
8ff22a6f
BM
188/**
189 * gfs2_glock_put_nolock() - Decrement reference count on glock
190 * @gl: The glock to put
191 *
192 * This function should only be used if the caller has its own reference
193 * to the glock, in addition to the one it is dropping.
194 */
195
b94a170e 196void gfs2_glock_put_nolock(struct gfs2_glock *gl)
8ff22a6f
BM
197{
198 if (atomic_dec_and_test(&gl->gl_ref))
199 GLOCK_BUG_ON(gl, 1);
8ff22a6f
BM
200}
201
b3b94faa
DT
202/**
203 * gfs2_glock_put() - Decrement reference count on glock
204 * @gl: The glock to put
205 *
206 */
207
bc015cb8 208void gfs2_glock_put(struct gfs2_glock *gl)
b3b94faa 209{
bc015cb8
SW
210 struct gfs2_sbd *sdp = gl->gl_sbd;
211 struct address_space *mapping = gfs2_glock2aspace(gl);
b3b94faa 212
bc015cb8
SW
213 if (atomic_dec_and_test(&gl->gl_ref)) {
214 spin_lock_bucket(gl->gl_hash);
215 hlist_bl_del_rcu(&gl->gl_list);
216 spin_unlock_bucket(gl->gl_hash);
217 spin_lock(&lru_lock);
97cc1025
SW
218 if (!list_empty(&gl->gl_lru)) {
219 list_del_init(&gl->gl_lru);
220 atomic_dec(&lru_count);
221 }
222 spin_unlock(&lru_lock);
6802e340 223 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
bc015cb8
SW
224 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
225 trace_gfs2_glock_put(gl);
226 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
b3b94faa 227 }
b3b94faa
DT
228}
229
b3b94faa
DT
230/**
231 * search_bucket() - Find struct gfs2_glock by lock number
232 * @bucket: the bucket to search
233 * @name: The lock name
234 *
235 * Returns: NULL, or the struct gfs2_glock with the requested number
236 */
237
37b2fa6a 238static struct gfs2_glock *search_bucket(unsigned int hash,
899be4d3 239 const struct gfs2_sbd *sdp,
d6a53727 240 const struct lm_lockname *name)
b3b94faa
DT
241{
242 struct gfs2_glock *gl;
bc015cb8 243 struct hlist_bl_node *h;
b3b94faa 244
bc015cb8 245 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
b3b94faa
DT
246 if (!lm_name_equal(&gl->gl_name, name))
247 continue;
899be4d3
SW
248 if (gl->gl_sbd != sdp)
249 continue;
bc015cb8
SW
250 if (atomic_inc_not_zero(&gl->gl_ref))
251 return gl;
b3b94faa
DT
252 }
253
254 return NULL;
255}
256
6802e340
SW
257/**
258 * may_grant - check if its ok to grant a new lock
259 * @gl: The glock
260 * @gh: The lock request which we wish to grant
261 *
262 * Returns: true if its ok to grant the lock
263 */
264
265static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
266{
267 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
268 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
269 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
270 return 0;
271 if (gl->gl_state == gh->gh_state)
272 return 1;
273 if (gh->gh_flags & GL_EXACT)
274 return 0;
209806ab
SW
275 if (gl->gl_state == LM_ST_EXCLUSIVE) {
276 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
277 return 1;
278 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
279 return 1;
280 }
6802e340
SW
281 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
282 return 1;
283 return 0;
284}
285
286static void gfs2_holder_wake(struct gfs2_holder *gh)
287{
288 clear_bit(HIF_WAIT, &gh->gh_iflags);
289 smp_mb__after_clear_bit();
290 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
291}
292
d5341a92
SW
293/**
294 * do_error - Something unexpected has happened during a lock request
295 *
296 */
297
298static inline void do_error(struct gfs2_glock *gl, const int ret)
299{
300 struct gfs2_holder *gh, *tmp;
301
302 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
303 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
304 continue;
305 if (ret & LM_OUT_ERROR)
306 gh->gh_error = -EIO;
307 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
308 gh->gh_error = GLR_TRYFAILED;
309 else
310 continue;
311 list_del_init(&gh->gh_list);
312 trace_gfs2_glock_queue(gh, 0);
313 gfs2_holder_wake(gh);
314 }
315}
316
6802e340
SW
317/**
318 * do_promote - promote as many requests as possible on the current queue
319 * @gl: The glock
320 *
813e0c46
SW
321 * Returns: 1 if there is a blocked holder at the head of the list, or 2
322 * if a type specific operation is underway.
6802e340
SW
323 */
324
325static int do_promote(struct gfs2_glock *gl)
55ba474d
HH
326__releases(&gl->gl_spin)
327__acquires(&gl->gl_spin)
6802e340
SW
328{
329 const struct gfs2_glock_operations *glops = gl->gl_ops;
330 struct gfs2_holder *gh, *tmp;
331 int ret;
332
333restart:
334 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
335 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
336 continue;
337 if (may_grant(gl, gh)) {
338 if (gh->gh_list.prev == &gl->gl_holders &&
339 glops->go_lock) {
340 spin_unlock(&gl->gl_spin);
341 /* FIXME: eliminate this eventually */
342 ret = glops->go_lock(gh);
343 spin_lock(&gl->gl_spin);
344 if (ret) {
813e0c46
SW
345 if (ret == 1)
346 return 2;
6802e340
SW
347 gh->gh_error = ret;
348 list_del_init(&gh->gh_list);
63997775 349 trace_gfs2_glock_queue(gh, 0);
6802e340
SW
350 gfs2_holder_wake(gh);
351 goto restart;
352 }
353 set_bit(HIF_HOLDER, &gh->gh_iflags);
63997775 354 trace_gfs2_promote(gh, 1);
6802e340
SW
355 gfs2_holder_wake(gh);
356 goto restart;
357 }
358 set_bit(HIF_HOLDER, &gh->gh_iflags);
63997775 359 trace_gfs2_promote(gh, 0);
6802e340
SW
360 gfs2_holder_wake(gh);
361 continue;
362 }
363 if (gh->gh_list.prev == &gl->gl_holders)
364 return 1;
d5341a92 365 do_error(gl, 0);
6802e340
SW
366 break;
367 }
368 return 0;
369}
370
6802e340
SW
371/**
372 * find_first_waiter - find the first gh that's waiting for the glock
373 * @gl: the glock
374 */
375
376static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
377{
378 struct gfs2_holder *gh;
379
380 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
381 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
382 return gh;
383 }
384 return NULL;
385}
386
387/**
388 * state_change - record that the glock is now in a different state
389 * @gl: the glock
390 * @new_state the new state
391 *
392 */
393
394static void state_change(struct gfs2_glock *gl, unsigned int new_state)
395{
396 int held1, held2;
397
398 held1 = (gl->gl_state != LM_ST_UNLOCKED);
399 held2 = (new_state != LM_ST_UNLOCKED);
400
401 if (held1 != held2) {
402 if (held2)
403 gfs2_glock_hold(gl);
404 else
8ff22a6f 405 gfs2_glock_put_nolock(gl);
6802e340 406 }
7b5e3d5f
SW
407 if (held1 && held2 && list_empty(&gl->gl_holders))
408 clear_bit(GLF_QUEUED, &gl->gl_flags);
6802e340
SW
409
410 gl->gl_state = new_state;
411 gl->gl_tchange = jiffies;
412}
413
414static void gfs2_demote_wake(struct gfs2_glock *gl)
415{
416 gl->gl_demote_state = LM_ST_EXCLUSIVE;
417 clear_bit(GLF_DEMOTE, &gl->gl_flags);
418 smp_mb__after_clear_bit();
419 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
420}
421
422/**
423 * finish_xmote - The DLM has replied to one of our lock requests
424 * @gl: The glock
425 * @ret: The status from the DLM
426 *
427 */
428
429static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
430{
431 const struct gfs2_glock_operations *glops = gl->gl_ops;
432 struct gfs2_holder *gh;
433 unsigned state = ret & LM_OUT_ST_MASK;
813e0c46 434 int rv;
6802e340
SW
435
436 spin_lock(&gl->gl_spin);
63997775 437 trace_gfs2_glock_state_change(gl, state);
6802e340
SW
438 state_change(gl, state);
439 gh = find_first_waiter(gl);
440
441 /* Demote to UN request arrived during demote to SH or DF */
442 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
443 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
444 gl->gl_target = LM_ST_UNLOCKED;
445
446 /* Check for state != intended state */
447 if (unlikely(state != gl->gl_target)) {
448 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
449 /* move to back of queue and try next entry */
450 if (ret & LM_OUT_CANCELED) {
451 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
452 list_move_tail(&gh->gh_list, &gl->gl_holders);
453 gh = find_first_waiter(gl);
454 gl->gl_target = gh->gh_state;
455 goto retry;
456 }
457 /* Some error or failed "try lock" - report it */
458 if ((ret & LM_OUT_ERROR) ||
459 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
460 gl->gl_target = gl->gl_state;
461 do_error(gl, ret);
462 goto out;
463 }
464 }
465 switch(state) {
466 /* Unlocked due to conversion deadlock, try again */
467 case LM_ST_UNLOCKED:
468retry:
469 do_xmote(gl, gh, gl->gl_target);
470 break;
471 /* Conversion fails, unlock and try again */
472 case LM_ST_SHARED:
473 case LM_ST_DEFERRED:
474 do_xmote(gl, gh, LM_ST_UNLOCKED);
475 break;
476 default: /* Everything else */
477 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
478 GLOCK_BUG_ON(gl, 1);
479 }
480 spin_unlock(&gl->gl_spin);
6802e340
SW
481 return;
482 }
483
484 /* Fast path - we got what we asked for */
485 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
486 gfs2_demote_wake(gl);
487 if (state != LM_ST_UNLOCKED) {
488 if (glops->go_xmote_bh) {
6802e340
SW
489 spin_unlock(&gl->gl_spin);
490 rv = glops->go_xmote_bh(gl, gh);
6802e340
SW
491 spin_lock(&gl->gl_spin);
492 if (rv) {
493 do_error(gl, rv);
494 goto out;
495 }
496 }
813e0c46
SW
497 rv = do_promote(gl);
498 if (rv == 2)
499 goto out_locked;
6802e340
SW
500 }
501out:
502 clear_bit(GLF_LOCK, &gl->gl_flags);
813e0c46 503out_locked:
6802e340 504 spin_unlock(&gl->gl_spin);
6802e340
SW
505}
506
6802e340
SW
507/**
508 * do_xmote - Calls the DLM to change the state of a lock
509 * @gl: The lock state
510 * @gh: The holder (only for promotes)
511 * @target: The target lock state
512 *
513 */
514
515static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
55ba474d
HH
516__releases(&gl->gl_spin)
517__acquires(&gl->gl_spin)
6802e340
SW
518{
519 const struct gfs2_glock_operations *glops = gl->gl_ops;
520 struct gfs2_sbd *sdp = gl->gl_sbd;
521 unsigned int lck_flags = gh ? gh->gh_flags : 0;
522 int ret;
523
524 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
525 LM_FLAG_PRIORITY);
921169ca
SW
526 GLOCK_BUG_ON(gl, gl->gl_state == target);
527 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
6802e340
SW
528 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
529 glops->go_inval) {
530 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
531 do_error(gl, 0); /* Fail queued try locks */
532 }
47a25380 533 gl->gl_req = target;
6802e340
SW
534 spin_unlock(&gl->gl_spin);
535 if (glops->go_xmote_th)
536 glops->go_xmote_th(gl);
537 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
538 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
539 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
540
541 gfs2_glock_hold(gl);
542 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
543 gl->gl_state == LM_ST_DEFERRED) &&
544 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
545 lck_flags |= LM_FLAG_TRY_1CB;
6802e340 546
921169ca
SW
547 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
548 /* lock_dlm */
549 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
550 GLOCK_BUG_ON(gl, ret);
551 } else { /* lock_nolock */
552 finish_xmote(gl, target);
6802e340
SW
553 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
554 gfs2_glock_put(gl);
6802e340 555 }
921169ca 556
6802e340
SW
557 spin_lock(&gl->gl_spin);
558}
559
560/**
561 * find_first_holder - find the first "holder" gh
562 * @gl: the glock
563 */
564
565static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
566{
567 struct gfs2_holder *gh;
568
569 if (!list_empty(&gl->gl_holders)) {
570 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
571 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
572 return gh;
573 }
574 return NULL;
575}
576
577/**
578 * run_queue - do all outstanding tasks related to a glock
579 * @gl: The glock in question
580 * @nonblock: True if we must not block in run_queue
581 *
582 */
583
584static void run_queue(struct gfs2_glock *gl, const int nonblock)
55ba474d
HH
585__releases(&gl->gl_spin)
586__acquires(&gl->gl_spin)
6802e340
SW
587{
588 struct gfs2_holder *gh = NULL;
813e0c46 589 int ret;
6802e340
SW
590
591 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
592 return;
593
594 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
595
596 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
597 gl->gl_demote_state != gl->gl_state) {
598 if (find_first_holder(gl))
d8348de0 599 goto out_unlock;
6802e340
SW
600 if (nonblock)
601 goto out_sched;
602 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
265d529c 603 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
6802e340
SW
604 gl->gl_target = gl->gl_demote_state;
605 } else {
606 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
607 gfs2_demote_wake(gl);
813e0c46
SW
608 ret = do_promote(gl);
609 if (ret == 0)
d8348de0 610 goto out_unlock;
813e0c46 611 if (ret == 2)
a228df63 612 goto out;
6802e340
SW
613 gh = find_first_waiter(gl);
614 gl->gl_target = gh->gh_state;
615 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
616 do_error(gl, 0); /* Fail queued try locks */
617 }
618 do_xmote(gl, gh, gl->gl_target);
a228df63 619out:
6802e340
SW
620 return;
621
622out_sched:
7e71c55e
SW
623 clear_bit(GLF_LOCK, &gl->gl_flags);
624 smp_mb__after_clear_bit();
6802e340
SW
625 gfs2_glock_hold(gl);
626 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
8ff22a6f 627 gfs2_glock_put_nolock(gl);
7e71c55e
SW
628 return;
629
d8348de0 630out_unlock:
6802e340 631 clear_bit(GLF_LOCK, &gl->gl_flags);
7e71c55e
SW
632 smp_mb__after_clear_bit();
633 return;
6802e340
SW
634}
635
b94a170e
BM
636static void delete_work_func(struct work_struct *work)
637{
638 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
639 struct gfs2_sbd *sdp = gl->gl_sbd;
044b9414 640 struct gfs2_inode *ip;
b94a170e 641 struct inode *inode;
044b9414
SW
642 u64 no_addr = gl->gl_name.ln_number;
643
644 ip = gl->gl_object;
645 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
b94a170e 646
b94a170e 647 if (ip)
b94a170e 648 inode = gfs2_ilookup(sdp->sd_vfs, no_addr);
044b9414
SW
649 else
650 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
651 if (inode && !IS_ERR(inode)) {
652 d_prune_aliases(inode);
653 iput(inode);
b94a170e
BM
654 }
655 gfs2_glock_put(gl);
656}
657
c4f68a13
BM
658static void glock_work_func(struct work_struct *work)
659{
6802e340 660 unsigned long delay = 0;
c4f68a13 661 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
26bb7505 662 int drop_ref = 0;
c4f68a13 663
26bb7505 664 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
6802e340 665 finish_xmote(gl, gl->gl_reply);
26bb7505
SW
666 drop_ref = 1;
667 }
c4f68a13 668 spin_lock(&gl->gl_spin);
265d529c
SW
669 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
670 gl->gl_state != LM_ST_UNLOCKED &&
671 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
6802e340
SW
672 unsigned long holdtime, now = jiffies;
673 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
674 if (time_before(now, holdtime))
675 delay = holdtime - now;
676 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
677 }
678 run_queue(gl, 0);
c4f68a13 679 spin_unlock(&gl->gl_spin);
6802e340
SW
680 if (!delay ||
681 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
682 gfs2_glock_put(gl);
26bb7505
SW
683 if (drop_ref)
684 gfs2_glock_put(gl);
c4f68a13
BM
685}
686
b3b94faa
DT
687/**
688 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
689 * @sdp: The GFS2 superblock
690 * @number: the lock number
691 * @glops: The glock_operations to use
692 * @create: If 0, don't create the glock if it doesn't exist
693 * @glp: the glock is returned here
694 *
695 * This does not lock a glock, just finds/creates structures for one.
696 *
697 * Returns: errno
698 */
699
cd915493 700int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
8fb4b536 701 const struct gfs2_glock_operations *glops, int create,
b3b94faa
DT
702 struct gfs2_glock **glp)
703{
009d8518 704 struct super_block *s = sdp->sd_vfs;
37b2fa6a 705 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
b3b94faa 706 struct gfs2_glock *gl, *tmp;
37b2fa6a 707 unsigned int hash = gl_hash(sdp, &name);
009d8518 708 struct address_space *mapping;
bc015cb8 709 struct kmem_cache *cachep;
b3b94faa 710
bc015cb8 711 rcu_read_lock();
37b2fa6a 712 gl = search_bucket(hash, sdp, &name);
bc015cb8 713 rcu_read_unlock();
b3b94faa 714
64d576ba
SW
715 *glp = gl;
716 if (gl)
b3b94faa 717 return 0;
64d576ba
SW
718 if (!create)
719 return -ENOENT;
b3b94faa 720
009d8518 721 if (glops->go_flags & GLOF_ASPACE)
bc015cb8 722 cachep = gfs2_glock_aspace_cachep;
009d8518 723 else
bc015cb8
SW
724 cachep = gfs2_glock_cachep;
725 gl = kmem_cache_alloc(cachep, GFP_KERNEL);
b3b94faa
DT
726 if (!gl)
727 return -ENOMEM;
728
8f05228e 729 atomic_inc(&sdp->sd_glock_disposal);
ec45d9f5 730 gl->gl_flags = 0;
b3b94faa 731 gl->gl_name = name;
16feb9fe 732 atomic_set(&gl->gl_ref, 1);
b3b94faa 733 gl->gl_state = LM_ST_UNLOCKED;
6802e340 734 gl->gl_target = LM_ST_UNLOCKED;
c4f68a13 735 gl->gl_demote_state = LM_ST_EXCLUSIVE;
37b2fa6a 736 gl->gl_hash = hash;
b3b94faa 737 gl->gl_ops = glops;
f057f6cd
SW
738 snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
739 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
740 gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
c4f68a13 741 gl->gl_tchange = jiffies;
ec45d9f5 742 gl->gl_object = NULL;
b3b94faa 743 gl->gl_sbd = sdp;
c4f68a13 744 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
b94a170e 745 INIT_WORK(&gl->gl_delete, delete_work_func);
b3b94faa 746
009d8518
SW
747 mapping = gfs2_glock2aspace(gl);
748 if (mapping) {
749 mapping->a_ops = &gfs2_meta_aops;
750 mapping->host = s->s_bdev->bd_inode;
751 mapping->flags = 0;
752 mapping_set_gfp_mask(mapping, GFP_NOFS);
753 mapping->assoc_mapping = NULL;
754 mapping->backing_dev_info = s->s_bdi;
755 mapping->writeback_index = 0;
b3b94faa
DT
756 }
757
bc015cb8 758 spin_lock_bucket(hash);
37b2fa6a 759 tmp = search_bucket(hash, sdp, &name);
b3b94faa 760 if (tmp) {
bc015cb8
SW
761 spin_unlock_bucket(hash);
762 kmem_cache_free(cachep, gl);
b3b94faa
DT
763 gl = tmp;
764 } else {
bc015cb8
SW
765 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
766 spin_unlock_bucket(hash);
b3b94faa
DT
767 }
768
769 *glp = gl;
770
771 return 0;
b3b94faa
DT
772}
773
774/**
775 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
776 * @gl: the glock
777 * @state: the state we're requesting
778 * @flags: the modifier flags
779 * @gh: the holder structure
780 *
781 */
782
190562bd 783void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
b3b94faa
DT
784 struct gfs2_holder *gh)
785{
786 INIT_LIST_HEAD(&gh->gh_list);
787 gh->gh_gl = gl;
d0dc80db 788 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b1e058da 789 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
790 gh->gh_state = state;
791 gh->gh_flags = flags;
792 gh->gh_error = 0;
793 gh->gh_iflags = 0;
b3b94faa
DT
794 gfs2_glock_hold(gl);
795}
796
797/**
798 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
799 * @state: the state we're requesting
800 * @flags: the modifier flags
801 * @gh: the holder structure
802 *
803 * Don't mess with the glock.
804 *
805 */
806
190562bd 807void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
b3b94faa
DT
808{
809 gh->gh_state = state;
579b78a4 810 gh->gh_flags = flags;
3b8249f6 811 gh->gh_iflags = 0;
d0dc80db 812 gh->gh_ip = (unsigned long)__builtin_return_address(0);
1a0eae88
BP
813 if (gh->gh_owner_pid)
814 put_pid(gh->gh_owner_pid);
815 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
816}
817
818/**
819 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
820 * @gh: the holder structure
821 *
822 */
823
824void gfs2_holder_uninit(struct gfs2_holder *gh)
825{
b1e058da 826 put_pid(gh->gh_owner_pid);
b3b94faa
DT
827 gfs2_glock_put(gh->gh_gl);
828 gh->gh_gl = NULL;
d0dc80db 829 gh->gh_ip = 0;
b3b94faa
DT
830}
831
fe64d517
SW
832/**
833 * gfs2_glock_holder_wait
834 * @word: unused
835 *
836 * This function and gfs2_glock_demote_wait both show up in the WCHAN
837 * field. Thus I've separated these otherwise identical functions in
838 * order to be more informative to the user.
839 */
840
841static int gfs2_glock_holder_wait(void *word)
fee852e3
SW
842{
843 schedule();
844 return 0;
845}
846
fe64d517
SW
847static int gfs2_glock_demote_wait(void *word)
848{
849 schedule();
850 return 0;
851}
852
6802e340 853static void wait_on_holder(struct gfs2_holder *gh)
da755fdb 854{
6802e340 855 might_sleep();
fe64d517 856 wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
da755fdb
SW
857}
858
6802e340 859static void wait_on_demote(struct gfs2_glock *gl)
b3b94faa 860{
6802e340 861 might_sleep();
fe64d517 862 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
b3b94faa
DT
863}
864
865/**
6802e340
SW
866 * handle_callback - process a demote request
867 * @gl: the glock
868 * @state: the state the caller wants us to change to
b3b94faa 869 *
6802e340
SW
870 * There are only two requests that we are going to see in actual
871 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
b3b94faa
DT
872 */
873
6802e340 874static void handle_callback(struct gfs2_glock *gl, unsigned int state,
97cc1025 875 unsigned long delay)
b3b94faa 876{
6802e340 877 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
b3b94faa 878
6802e340
SW
879 set_bit(bit, &gl->gl_flags);
880 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
881 gl->gl_demote_state = state;
882 gl->gl_demote_time = jiffies;
6802e340
SW
883 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
884 gl->gl_demote_state != state) {
885 gl->gl_demote_state = LM_ST_UNLOCKED;
b3b94faa 886 }
b94a170e
BM
887 if (gl->gl_ops->go_callback)
888 gl->gl_ops->go_callback(gl);
63997775 889 trace_gfs2_demote_rq(gl);
b3b94faa
DT
890}
891
892/**
6802e340 893 * gfs2_glock_wait - wait on a glock acquisition
b3b94faa
DT
894 * @gh: the glock holder
895 *
896 * Returns: 0 on success
897 */
898
6802e340 899int gfs2_glock_wait(struct gfs2_holder *gh)
b3b94faa 900{
fee852e3 901 wait_on_holder(gh);
b3b94faa
DT
902 return gh->gh_error;
903}
904
6802e340 905void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
7c52b166 906{
5e69069c 907 struct va_format vaf;
7c52b166
RP
908 va_list args;
909
910 va_start(args, fmt);
5e69069c 911
6802e340
SW
912 if (seq) {
913 struct gfs2_glock_iter *gi = seq->private;
7c52b166 914 vsprintf(gi->string, fmt, args);
6802e340
SW
915 seq_printf(seq, gi->string);
916 } else {
5e69069c
JP
917 vaf.fmt = fmt;
918 vaf.va = &args;
919
920 printk(KERN_ERR " %pV", &vaf);
6802e340 921 }
5e69069c 922
7c52b166
RP
923 va_end(args);
924}
925
b3b94faa
DT
926/**
927 * add_to_queue - Add a holder to the wait queue (but look for recursion)
928 * @gh: the holder structure to add
929 *
6802e340
SW
930 * Eventually we should move the recursive locking trap to a
931 * debugging option or something like that. This is the fast
932 * path and needs to have the minimum number of distractions.
933 *
b3b94faa
DT
934 */
935
6802e340 936static inline void add_to_queue(struct gfs2_holder *gh)
55ba474d
HH
937__releases(&gl->gl_spin)
938__acquires(&gl->gl_spin)
b3b94faa
DT
939{
940 struct gfs2_glock *gl = gh->gh_gl;
6802e340
SW
941 struct gfs2_sbd *sdp = gl->gl_sbd;
942 struct list_head *insert_pt = NULL;
943 struct gfs2_holder *gh2;
944 int try_lock = 0;
b3b94faa 945
b1e058da 946 BUG_ON(gh->gh_owner_pid == NULL);
fee852e3
SW
947 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
948 BUG();
190562bd 949
6802e340
SW
950 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
951 if (test_bit(GLF_LOCK, &gl->gl_flags))
952 try_lock = 1;
953 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
954 goto fail;
955 }
956
957 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
958 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
959 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
960 goto trap_recursive;
961 if (try_lock &&
962 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
963 !may_grant(gl, gh)) {
964fail:
965 gh->gh_error = GLR_TRYFAILED;
966 gfs2_holder_wake(gh);
967 return;
b4c20166 968 }
6802e340
SW
969 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
970 continue;
971 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
972 insert_pt = &gh2->gh_list;
973 }
7b5e3d5f 974 set_bit(GLF_QUEUED, &gl->gl_flags);
edae38a6 975 trace_gfs2_glock_queue(gh, 1);
6802e340
SW
976 if (likely(insert_pt == NULL)) {
977 list_add_tail(&gh->gh_list, &gl->gl_holders);
978 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
979 goto do_cancel;
980 return;
981 }
982 list_add_tail(&gh->gh_list, insert_pt);
983do_cancel:
984 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
985 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
986 spin_unlock(&gl->gl_spin);
048bca22 987 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
f057f6cd 988 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
6802e340 989 spin_lock(&gl->gl_spin);
b3b94faa 990 }
6802e340 991 return;
b3b94faa 992
6802e340
SW
993trap_recursive:
994 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
995 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
996 printk(KERN_ERR "lock type: %d req lock state : %d\n",
997 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
998 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
999 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
1000 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1001 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1002 __dump_glock(NULL, gl);
1003 BUG();
b3b94faa
DT
1004}
1005
1006/**
1007 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1008 * @gh: the holder structure
1009 *
1010 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1011 *
1012 * Returns: 0, GLR_TRYFAILED, or errno on failure
1013 */
1014
1015int gfs2_glock_nq(struct gfs2_holder *gh)
1016{
1017 struct gfs2_glock *gl = gh->gh_gl;
1018 struct gfs2_sbd *sdp = gl->gl_sbd;
1019 int error = 0;
1020
6802e340 1021 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
b3b94faa 1022 return -EIO;
b3b94faa 1023
b3b94faa
DT
1024 spin_lock(&gl->gl_spin);
1025 add_to_queue(gh);
0809f6ec
SW
1026 if ((LM_FLAG_NOEXP & gh->gh_flags) &&
1027 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1028 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
6802e340 1029 run_queue(gl, 1);
b3b94faa
DT
1030 spin_unlock(&gl->gl_spin);
1031
6802e340
SW
1032 if (!(gh->gh_flags & GL_ASYNC))
1033 error = gfs2_glock_wait(gh);
b3b94faa 1034
b3b94faa
DT
1035 return error;
1036}
1037
1038/**
1039 * gfs2_glock_poll - poll to see if an async request has been completed
1040 * @gh: the holder
1041 *
1042 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1043 */
1044
1045int gfs2_glock_poll(struct gfs2_holder *gh)
1046{
6802e340 1047 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
b3b94faa
DT
1048}
1049
1050/**
1051 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1052 * @gh: the glock holder
1053 *
1054 */
1055
1056void gfs2_glock_dq(struct gfs2_holder *gh)
1057{
1058 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1059 const struct gfs2_glock_operations *glops = gl->gl_ops;
c4f68a13 1060 unsigned delay = 0;
6802e340 1061 int fast_path = 0;
b3b94faa 1062
6802e340 1063 spin_lock(&gl->gl_spin);
b3b94faa 1064 if (gh->gh_flags & GL_NOCACHE)
97cc1025 1065 handle_callback(gl, LM_ST_UNLOCKED, 0);
b3b94faa 1066
b3b94faa 1067 list_del_init(&gh->gh_list);
6802e340 1068 if (find_first_holder(gl) == NULL) {
3042a2cc 1069 if (glops->go_unlock) {
6802e340 1070 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
3042a2cc 1071 spin_unlock(&gl->gl_spin);
b3b94faa 1072 glops->go_unlock(gh);
3042a2cc 1073 spin_lock(&gl->gl_spin);
6802e340 1074 clear_bit(GLF_LOCK, &gl->gl_flags);
3042a2cc 1075 }
6802e340
SW
1076 if (list_empty(&gl->gl_holders) &&
1077 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1078 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1079 fast_path = 1;
b3b94faa 1080 }
bc015cb8 1081 __gfs2_glock_schedule_for_reclaim(gl);
63997775 1082 trace_gfs2_glock_queue(gh, 0);
b3b94faa 1083 spin_unlock(&gl->gl_spin);
6802e340
SW
1084 if (likely(fast_path))
1085 return;
c4f68a13
BM
1086
1087 gfs2_glock_hold(gl);
1088 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1089 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1090 delay = gl->gl_ops->go_min_hold_time;
1091 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1092 gfs2_glock_put(gl);
b3b94faa
DT
1093}
1094
d93cfa98
AD
1095void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1096{
1097 struct gfs2_glock *gl = gh->gh_gl;
1098 gfs2_glock_dq(gh);
1099 wait_on_demote(gl);
1100}
1101
b3b94faa
DT
1102/**
1103 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1104 * @gh: the holder structure
1105 *
1106 */
1107
1108void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1109{
1110 gfs2_glock_dq(gh);
1111 gfs2_holder_uninit(gh);
1112}
1113
1114/**
1115 * gfs2_glock_nq_num - acquire a glock based on lock number
1116 * @sdp: the filesystem
1117 * @number: the lock number
1118 * @glops: the glock operations for the type of glock
1119 * @state: the state to acquire the glock in
1120 * @flags: modifier flags for the aquisition
1121 * @gh: the struct gfs2_holder
1122 *
1123 * Returns: errno
1124 */
1125
cd915493 1126int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
8fb4b536
SW
1127 const struct gfs2_glock_operations *glops,
1128 unsigned int state, int flags, struct gfs2_holder *gh)
b3b94faa
DT
1129{
1130 struct gfs2_glock *gl;
1131 int error;
1132
1133 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1134 if (!error) {
1135 error = gfs2_glock_nq_init(gl, state, flags, gh);
1136 gfs2_glock_put(gl);
1137 }
1138
1139 return error;
1140}
1141
1142/**
1143 * glock_compare - Compare two struct gfs2_glock structures for sorting
1144 * @arg_a: the first structure
1145 * @arg_b: the second structure
1146 *
1147 */
1148
1149static int glock_compare(const void *arg_a, const void *arg_b)
1150{
a5e08a9e
SW
1151 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1152 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1153 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1154 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
b3b94faa
DT
1155
1156 if (a->ln_number > b->ln_number)
a5e08a9e
SW
1157 return 1;
1158 if (a->ln_number < b->ln_number)
1159 return -1;
1c0f4872 1160 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
a5e08a9e 1161 return 0;
b3b94faa
DT
1162}
1163
1164/**
1165 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1166 * @num_gh: the number of structures
1167 * @ghs: an array of struct gfs2_holder structures
1168 *
1169 * Returns: 0 on success (all glocks acquired),
1170 * errno on failure (no glocks acquired)
1171 */
1172
1173static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1174 struct gfs2_holder **p)
1175{
1176 unsigned int x;
1177 int error = 0;
1178
1179 for (x = 0; x < num_gh; x++)
1180 p[x] = &ghs[x];
1181
1182 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1183
1184 for (x = 0; x < num_gh; x++) {
1185 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1186
1187 error = gfs2_glock_nq(p[x]);
1188 if (error) {
1189 while (x--)
1190 gfs2_glock_dq(p[x]);
1191 break;
1192 }
1193 }
1194
1195 return error;
1196}
1197
1198/**
1199 * gfs2_glock_nq_m - acquire multiple glocks
1200 * @num_gh: the number of structures
1201 * @ghs: an array of struct gfs2_holder structures
1202 *
b3b94faa
DT
1203 *
1204 * Returns: 0 on success (all glocks acquired),
1205 * errno on failure (no glocks acquired)
1206 */
1207
1208int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1209{
eaf5bd3c
SW
1210 struct gfs2_holder *tmp[4];
1211 struct gfs2_holder **pph = tmp;
b3b94faa
DT
1212 int error = 0;
1213
eaf5bd3c
SW
1214 switch(num_gh) {
1215 case 0:
b3b94faa 1216 return 0;
eaf5bd3c 1217 case 1:
b3b94faa
DT
1218 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1219 return gfs2_glock_nq(ghs);
eaf5bd3c
SW
1220 default:
1221 if (num_gh <= 4)
b3b94faa 1222 break;
eaf5bd3c
SW
1223 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1224 if (!pph)
1225 return -ENOMEM;
b3b94faa
DT
1226 }
1227
eaf5bd3c 1228 error = nq_m_sync(num_gh, ghs, pph);
b3b94faa 1229
eaf5bd3c
SW
1230 if (pph != tmp)
1231 kfree(pph);
b3b94faa
DT
1232
1233 return error;
1234}
1235
1236/**
1237 * gfs2_glock_dq_m - release multiple glocks
1238 * @num_gh: the number of structures
1239 * @ghs: an array of struct gfs2_holder structures
1240 *
1241 */
1242
1243void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1244{
1245 unsigned int x;
1246
1247 for (x = 0; x < num_gh; x++)
1248 gfs2_glock_dq(&ghs[x]);
1249}
1250
1251/**
1252 * gfs2_glock_dq_uninit_m - release multiple glocks
1253 * @num_gh: the number of structures
1254 * @ghs: an array of struct gfs2_holder structures
1255 *
1256 */
1257
1258void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1259{
1260 unsigned int x;
1261
1262 for (x = 0; x < num_gh; x++)
1263 gfs2_glock_dq_uninit(&ghs[x]);
1264}
1265
f057f6cd 1266void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
da755fdb 1267{
c4f68a13
BM
1268 unsigned long delay = 0;
1269 unsigned long holdtime;
1270 unsigned long now = jiffies;
b3b94faa 1271
f057f6cd 1272 gfs2_glock_hold(gl);
c4f68a13 1273 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
7b5e3d5f
SW
1274 if (test_bit(GLF_QUEUED, &gl->gl_flags)) {
1275 if (time_before(now, holdtime))
1276 delay = holdtime - now;
1277 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1278 delay = gl->gl_ops->go_min_hold_time;
1279 }
b3b94faa 1280
6802e340 1281 spin_lock(&gl->gl_spin);
97cc1025 1282 handle_callback(gl, state, delay);
6802e340 1283 spin_unlock(&gl->gl_spin);
c4f68a13
BM
1284 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1285 gfs2_glock_put(gl);
b3b94faa
DT
1286}
1287
0809f6ec
SW
1288/**
1289 * gfs2_should_freeze - Figure out if glock should be frozen
1290 * @gl: The glock in question
1291 *
1292 * Glocks are not frozen if (a) the result of the dlm operation is
1293 * an error, (b) the locking operation was an unlock operation or
1294 * (c) if there is a "noexp" flagged request anywhere in the queue
1295 *
1296 * Returns: 1 if freezing should occur, 0 otherwise
1297 */
1298
1299static int gfs2_should_freeze(const struct gfs2_glock *gl)
1300{
1301 const struct gfs2_holder *gh;
1302
1303 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1304 return 0;
1305 if (gl->gl_target == LM_ST_UNLOCKED)
1306 return 0;
1307
1308 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1309 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1310 continue;
1311 if (LM_FLAG_NOEXP & gh->gh_flags)
1312 return 0;
1313 }
1314
1315 return 1;
1316}
1317
b3b94faa 1318/**
f057f6cd
SW
1319 * gfs2_glock_complete - Callback used by locking
1320 * @gl: Pointer to the glock
1321 * @ret: The return value from the dlm
b3b94faa 1322 *
47a25380
SW
1323 * The gl_reply field is under the gl_spin lock so that it is ok
1324 * to use a bitfield shared with other glock state fields.
b3b94faa
DT
1325 */
1326
f057f6cd 1327void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
b3b94faa 1328{
f057f6cd 1329 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
0809f6ec 1330
47a25380 1331 spin_lock(&gl->gl_spin);
f057f6cd 1332 gl->gl_reply = ret;
0809f6ec 1333
f057f6cd 1334 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
0809f6ec 1335 if (gfs2_should_freeze(gl)) {
f057f6cd 1336 set_bit(GLF_FROZEN, &gl->gl_flags);
0809f6ec 1337 spin_unlock(&gl->gl_spin);
b3b94faa 1338 return;
0809f6ec 1339 }
b3b94faa 1340 }
47a25380
SW
1341
1342 spin_unlock(&gl->gl_spin);
f057f6cd 1343 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
47a25380 1344 smp_wmb();
f057f6cd
SW
1345 gfs2_glock_hold(gl);
1346 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1347 gfs2_glock_put(gl);
b3b94faa
DT
1348}
1349
b3b94faa 1350
7f8275d0 1351static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
b3b94faa
DT
1352{
1353 struct gfs2_glock *gl;
97cc1025
SW
1354 int may_demote;
1355 int nr_skipped = 0;
97cc1025 1356 LIST_HEAD(skipped);
b3b94faa 1357
97cc1025
SW
1358 if (nr == 0)
1359 goto out;
b3b94faa 1360
97cc1025
SW
1361 if (!(gfp_mask & __GFP_FS))
1362 return -1;
b3b94faa 1363
97cc1025
SW
1364 spin_lock(&lru_lock);
1365 while(nr && !list_empty(&lru_list)) {
1366 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1367 list_del_init(&gl->gl_lru);
1368 atomic_dec(&lru_count);
1369
1370 /* Test for being demotable */
1371 if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1372 gfs2_glock_hold(gl);
97cc1025
SW
1373 spin_unlock(&lru_lock);
1374 spin_lock(&gl->gl_spin);
1375 may_demote = demote_ok(gl);
97cc1025
SW
1376 if (may_demote) {
1377 handle_callback(gl, LM_ST_UNLOCKED, 0);
1378 nr--;
97cc1025 1379 }
7e71c55e
SW
1380 clear_bit(GLF_LOCK, &gl->gl_flags);
1381 smp_mb__after_clear_bit();
2163b1e6 1382 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
b94a170e
BM
1383 gfs2_glock_put_nolock(gl);
1384 spin_unlock(&gl->gl_spin);
97cc1025 1385 spin_lock(&lru_lock);
2163b1e6 1386 continue;
97cc1025 1387 }
2163b1e6
SW
1388 nr_skipped++;
1389 list_add(&gl->gl_lru, &skipped);
b3b94faa 1390 }
97cc1025
SW
1391 list_splice(&skipped, &lru_list);
1392 atomic_add(nr_skipped, &lru_count);
1393 spin_unlock(&lru_lock);
1394out:
1395 return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
b3b94faa
DT
1396}
1397
97cc1025
SW
1398static struct shrinker glock_shrinker = {
1399 .shrink = gfs2_shrink_glock_memory,
1400 .seeks = DEFAULT_SEEKS,
1401};
1402
b3b94faa
DT
1403/**
1404 * examine_bucket - Call a function for glock in a hash bucket
1405 * @examiner: the function
1406 * @sdp: the filesystem
1407 * @bucket: the bucket
1408 *
b3b94faa
DT
1409 */
1410
bc015cb8 1411static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
37b2fa6a 1412 unsigned int hash)
b3b94faa 1413{
bc015cb8
SW
1414 struct gfs2_glock *gl;
1415 struct hlist_bl_head *head = &gl_hash_table[hash];
1416 struct hlist_bl_node *pos;
b3b94faa 1417
bc015cb8
SW
1418 rcu_read_lock();
1419 hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
1420 if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
24264434 1421 examiner(gl);
b3b94faa 1422 }
bc015cb8 1423 rcu_read_unlock();
8fbbfd21 1424 cond_resched();
bc015cb8
SW
1425}
1426
1427static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1428{
1429 unsigned x;
1430
1431 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1432 examine_bucket(examiner, sdp, x);
b3b94faa
DT
1433}
1434
f057f6cd
SW
1435
1436/**
1437 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1438 * @gl: The glock to thaw
1439 *
1440 * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
1441 * so this has to result in the ref count being dropped by one.
1442 */
1443
1444static void thaw_glock(struct gfs2_glock *gl)
1445{
1446 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1447 return;
f057f6cd
SW
1448 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1449 gfs2_glock_hold(gl);
1450 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1451 gfs2_glock_put(gl);
f057f6cd
SW
1452}
1453
b3b94faa
DT
1454/**
1455 * clear_glock - look at a glock and see if we can free it from glock cache
1456 * @gl: the glock to look at
1457 *
1458 */
1459
1460static void clear_glock(struct gfs2_glock *gl)
1461{
97cc1025
SW
1462 spin_lock(&lru_lock);
1463 if (!list_empty(&gl->gl_lru)) {
1464 list_del_init(&gl->gl_lru);
1465 atomic_dec(&lru_count);
b3b94faa 1466 }
97cc1025 1467 spin_unlock(&lru_lock);
b3b94faa 1468
6802e340 1469 spin_lock(&gl->gl_spin);
c741c455 1470 if (gl->gl_state != LM_ST_UNLOCKED)
97cc1025 1471 handle_callback(gl, LM_ST_UNLOCKED, 0);
6802e340
SW
1472 spin_unlock(&gl->gl_spin);
1473 gfs2_glock_hold(gl);
1474 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1475 gfs2_glock_put(gl);
b3b94faa
DT
1476}
1477
f057f6cd
SW
1478/**
1479 * gfs2_glock_thaw - Thaw any frozen glocks
1480 * @sdp: The super block
1481 *
1482 */
1483
1484void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1485{
bc015cb8
SW
1486 glock_hash_walk(thaw_glock, sdp);
1487}
f057f6cd 1488
bc015cb8
SW
1489static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1490{
1491 int ret;
1492 spin_lock(&gl->gl_spin);
1493 ret = __dump_glock(seq, gl);
1494 spin_unlock(&gl->gl_spin);
1495 return ret;
1496}
1497
1498static void dump_glock_func(struct gfs2_glock *gl)
1499{
1500 dump_glock(NULL, gl);
f057f6cd
SW
1501}
1502
b3b94faa
DT
1503/**
1504 * gfs2_gl_hash_clear - Empty out the glock hash table
1505 * @sdp: the filesystem
1506 * @wait: wait until it's all gone
1507 *
1bdad606 1508 * Called when unmounting the filesystem.
b3b94faa
DT
1509 */
1510
fefc03bf 1511void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
b3b94faa 1512{
bc015cb8 1513 glock_hash_walk(clear_glock, sdp);
8f05228e
SW
1514 flush_workqueue(glock_workqueue);
1515 wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
bc015cb8 1516 glock_hash_walk(dump_glock_func, sdp);
b3b94faa
DT
1517}
1518
813e0c46
SW
1519void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1520{
1521 struct gfs2_glock *gl = ip->i_gl;
1522 int ret;
1523
1524 ret = gfs2_truncatei_resume(ip);
1525 gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
1526
1527 spin_lock(&gl->gl_spin);
1528 clear_bit(GLF_LOCK, &gl->gl_flags);
1529 run_queue(gl, 1);
1530 spin_unlock(&gl->gl_spin);
1531}
1532
6802e340 1533static const char *state2str(unsigned state)
04b933f2 1534{
6802e340
SW
1535 switch(state) {
1536 case LM_ST_UNLOCKED:
1537 return "UN";
1538 case LM_ST_SHARED:
1539 return "SH";
1540 case LM_ST_DEFERRED:
1541 return "DF";
1542 case LM_ST_EXCLUSIVE:
1543 return "EX";
1544 }
1545 return "??";
1546}
1547
1548static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1549{
1550 char *p = buf;
1551 if (flags & LM_FLAG_TRY)
1552 *p++ = 't';
1553 if (flags & LM_FLAG_TRY_1CB)
1554 *p++ = 'T';
1555 if (flags & LM_FLAG_NOEXP)
1556 *p++ = 'e';
1557 if (flags & LM_FLAG_ANY)
f057f6cd 1558 *p++ = 'A';
6802e340
SW
1559 if (flags & LM_FLAG_PRIORITY)
1560 *p++ = 'p';
1561 if (flags & GL_ASYNC)
1562 *p++ = 'a';
1563 if (flags & GL_EXACT)
1564 *p++ = 'E';
6802e340
SW
1565 if (flags & GL_NOCACHE)
1566 *p++ = 'c';
1567 if (test_bit(HIF_HOLDER, &iflags))
1568 *p++ = 'H';
1569 if (test_bit(HIF_WAIT, &iflags))
1570 *p++ = 'W';
1571 if (test_bit(HIF_FIRST, &iflags))
1572 *p++ = 'F';
1573 *p = 0;
1574 return buf;
04b933f2
RP
1575}
1576
b3b94faa
DT
1577/**
1578 * dump_holder - print information about a glock holder
6802e340 1579 * @seq: the seq_file struct
b3b94faa
DT
1580 * @gh: the glock holder
1581 *
1582 * Returns: 0 on success, -ENOBUFS when we run out of space
1583 */
1584
6802e340 1585static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
b3b94faa 1586{
6802e340 1587 struct task_struct *gh_owner = NULL;
6802e340 1588 char flags_buf[32];
b3b94faa 1589
6802e340 1590 if (gh->gh_owner_pid)
b1e058da 1591 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
cc18152e
JP
1592 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1593 state2str(gh->gh_state),
1594 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1595 gh->gh_error,
1596 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1597 gh_owner ? gh_owner->comm : "(ended)",
1598 (void *)gh->gh_ip);
7c52b166 1599 return 0;
b3b94faa
DT
1600}
1601
6802e340
SW
1602static const char *gflags2str(char *buf, const unsigned long *gflags)
1603{
1604 char *p = buf;
1605 if (test_bit(GLF_LOCK, gflags))
1606 *p++ = 'l';
6802e340
SW
1607 if (test_bit(GLF_DEMOTE, gflags))
1608 *p++ = 'D';
1609 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1610 *p++ = 'd';
1611 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1612 *p++ = 'p';
1613 if (test_bit(GLF_DIRTY, gflags))
1614 *p++ = 'y';
1615 if (test_bit(GLF_LFLUSH, gflags))
1616 *p++ = 'f';
1617 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1618 *p++ = 'i';
1619 if (test_bit(GLF_REPLY_PENDING, gflags))
1620 *p++ = 'r';
f057f6cd 1621 if (test_bit(GLF_INITIAL, gflags))
d8348de0 1622 *p++ = 'I';
f057f6cd
SW
1623 if (test_bit(GLF_FROZEN, gflags))
1624 *p++ = 'F';
7b5e3d5f
SW
1625 if (test_bit(GLF_QUEUED, gflags))
1626 *p++ = 'q';
6802e340
SW
1627 *p = 0;
1628 return buf;
b3b94faa
DT
1629}
1630
1631/**
6802e340
SW
1632 * __dump_glock - print information about a glock
1633 * @seq: The seq_file struct
b3b94faa 1634 * @gl: the glock
6802e340
SW
1635 *
1636 * The file format is as follows:
1637 * One line per object, capital letters are used to indicate objects
1638 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1639 * other objects are indented by a single space and follow the glock to
1640 * which they are related. Fields are indicated by lower case letters
1641 * followed by a colon and the field value, except for strings which are in
1642 * [] so that its possible to see if they are composed of spaces for
1643 * example. The field's are n = number (id of the object), f = flags,
1644 * t = type, s = state, r = refcount, e = error, p = pid.
b3b94faa
DT
1645 *
1646 * Returns: 0 on success, -ENOBUFS when we run out of space
1647 */
1648
6802e340 1649static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
b3b94faa 1650{
6802e340
SW
1651 const struct gfs2_glock_operations *glops = gl->gl_ops;
1652 unsigned long long dtime;
1653 const struct gfs2_holder *gh;
1654 char gflags_buf[32];
1655 int error = 0;
b3b94faa 1656
6802e340
SW
1657 dtime = jiffies - gl->gl_demote_time;
1658 dtime *= 1000000/HZ; /* demote time in uSec */
1659 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1660 dtime = 0;
4818972e 1661 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d r:%d\n",
6802e340
SW
1662 state2str(gl->gl_state),
1663 gl->gl_name.ln_type,
1664 (unsigned long long)gl->gl_name.ln_number,
1665 gflags2str(gflags_buf, &gl->gl_flags),
1666 state2str(gl->gl_target),
1667 state2str(gl->gl_demote_state), dtime,
6802e340
SW
1668 atomic_read(&gl->gl_ail_count),
1669 atomic_read(&gl->gl_ref));
b3b94faa 1670
b3b94faa 1671 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
6802e340 1672 error = dump_holder(seq, gh);
b3b94faa
DT
1673 if (error)
1674 goto out;
1675 }
6802e340
SW
1676 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1677 error = glops->go_dump(seq, gl);
a91ea69f 1678out:
b3b94faa
DT
1679 return error;
1680}
1681
b3b94faa 1682
b3b94faa 1683
8fbbfd21 1684
85d1da67
SW
1685int __init gfs2_glock_init(void)
1686{
1687 unsigned i;
1688 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
bc015cb8 1689 INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
087efdd3 1690 }
8fbbfd21 1691
d2115778 1692 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
9fa0ea9f 1693 WQ_HIGHPRI | WQ_FREEZEABLE, 0);
97cc1025 1694 if (IS_ERR(glock_workqueue))
c4f68a13 1695 return PTR_ERR(glock_workqueue);
d2115778
SW
1696 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
1697 WQ_MEM_RECLAIM | WQ_FREEZEABLE,
1698 0);
b94a170e
BM
1699 if (IS_ERR(gfs2_delete_workqueue)) {
1700 destroy_workqueue(glock_workqueue);
1701 return PTR_ERR(gfs2_delete_workqueue);
1702 }
97cc1025
SW
1703
1704 register_shrinker(&glock_shrinker);
c4f68a13 1705
85d1da67
SW
1706 return 0;
1707}
1708
8fbbfd21
SW
1709void gfs2_glock_exit(void)
1710{
97cc1025 1711 unregister_shrinker(&glock_shrinker);
c4f68a13 1712 destroy_workqueue(glock_workqueue);
b94a170e 1713 destroy_workqueue(gfs2_delete_workqueue);
8fbbfd21
SW
1714}
1715
bc015cb8
SW
1716static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
1717{
1718 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
1719 struct gfs2_glock, gl_list);
1720}
1721
1722static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
1723{
1724 return hlist_bl_entry(rcu_dereference_raw(gl->gl_list.next),
1725 struct gfs2_glock, gl_list);
1726}
1727
6802e340 1728static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
7c52b166 1729{
7b08fc62
SW
1730 struct gfs2_glock *gl;
1731
bc015cb8
SW
1732 do {
1733 gl = gi->gl;
1734 if (gl) {
1735 gi->gl = glock_hash_next(gl);
1736 } else {
1737 gi->gl = glock_hash_chain(gi->hash);
1738 }
1739 while (gi->gl == NULL) {
1740 gi->hash++;
1741 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1742 rcu_read_unlock();
1743 return 1;
1744 }
1745 gi->gl = glock_hash_chain(gi->hash);
1746 }
1747 /* Skip entries for other sb and dead entries */
1748 } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
a947e033 1749
7c52b166
RP
1750 return 0;
1751}
1752
6802e340 1753static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
7c52b166 1754{
6802e340 1755 struct gfs2_glock_iter *gi = seq->private;
7c52b166
RP
1756 loff_t n = *pos;
1757
6802e340 1758 gi->hash = 0;
bc015cb8 1759 rcu_read_lock();
7c52b166 1760
6802e340 1761 do {
bc015cb8 1762 if (gfs2_glock_iter_next(gi))
7c52b166 1763 return NULL;
6802e340 1764 } while (n--);
7c52b166 1765
6802e340 1766 return gi->gl;
7c52b166
RP
1767}
1768
6802e340 1769static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
7c52b166
RP
1770 loff_t *pos)
1771{
6802e340 1772 struct gfs2_glock_iter *gi = seq->private;
7c52b166
RP
1773
1774 (*pos)++;
1775
bc015cb8 1776 if (gfs2_glock_iter_next(gi))
7c52b166 1777 return NULL;
7c52b166 1778
6802e340 1779 return gi->gl;
7c52b166
RP
1780}
1781
6802e340 1782static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
7c52b166 1783{
6802e340 1784 struct gfs2_glock_iter *gi = seq->private;
bc015cb8
SW
1785
1786 if (gi->gl)
1787 rcu_read_unlock();
1788 gi->gl = NULL;
7c52b166
RP
1789}
1790
6802e340 1791static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
7c52b166 1792{
6802e340 1793 return dump_glock(seq, iter_ptr);
7c52b166
RP
1794}
1795
4ef29002 1796static const struct seq_operations gfs2_glock_seq_ops = {
7c52b166
RP
1797 .start = gfs2_glock_seq_start,
1798 .next = gfs2_glock_seq_next,
1799 .stop = gfs2_glock_seq_stop,
1800 .show = gfs2_glock_seq_show,
1801};
1802
1803static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1804{
6802e340
SW
1805 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1806 sizeof(struct gfs2_glock_iter));
1807 if (ret == 0) {
1808 struct seq_file *seq = file->private_data;
1809 struct gfs2_glock_iter *gi = seq->private;
1810 gi->sdp = inode->i_private;
1811 }
1812 return ret;
7c52b166
RP
1813}
1814
1815static const struct file_operations gfs2_debug_fops = {
1816 .owner = THIS_MODULE,
1817 .open = gfs2_debugfs_open,
1818 .read = seq_read,
1819 .llseek = seq_lseek,
6802e340 1820 .release = seq_release_private,
7c52b166
RP
1821};
1822
1823int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1824{
5f882096
RP
1825 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1826 if (!sdp->debugfs_dir)
1827 return -ENOMEM;
1828 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1829 S_IFREG | S_IRUGO,
1830 sdp->debugfs_dir, sdp,
1831 &gfs2_debug_fops);
1832 if (!sdp->debugfs_dentry_glocks)
7c52b166
RP
1833 return -ENOMEM;
1834
1835 return 0;
1836}
1837
1838void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1839{
5f882096
RP
1840 if (sdp && sdp->debugfs_dir) {
1841 if (sdp->debugfs_dentry_glocks) {
1842 debugfs_remove(sdp->debugfs_dentry_glocks);
1843 sdp->debugfs_dentry_glocks = NULL;
1844 }
1845 debugfs_remove(sdp->debugfs_dir);
1846 sdp->debugfs_dir = NULL;
1847 }
7c52b166
RP
1848}
1849
1850int gfs2_register_debugfs(void)
1851{
1852 gfs2_root = debugfs_create_dir("gfs2", NULL);
1853 return gfs2_root ? 0 : -ENOMEM;
1854}
1855
1856void gfs2_unregister_debugfs(void)
1857{
1858 debugfs_remove(gfs2_root);
5f882096 1859 gfs2_root = NULL;
7c52b166 1860}