add hlist_bl_lock/unlock helpers
[deliverable/linux.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{
1879fd6a 96 hlist_bl_lock(&gl_hash_table[hash]);
bc015cb8 97}
b3b94faa 98
bc015cb8
SW
99static inline void spin_unlock_bucket(unsigned int hash)
100{
1879fd6a 101 hlist_bl_unlock(&gl_hash_table[hash]);
bc015cb8 102}
b3b94faa 103
fc0e38da 104static void gfs2_glock_dealloc(struct rcu_head *rcu)
b3b94faa 105{
bc015cb8 106 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
b3b94faa 107
bc015cb8
SW
108 if (gl->gl_ops->go_flags & GLOF_ASPACE)
109 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
110 else
111 kmem_cache_free(gfs2_glock_cachep, gl);
fc0e38da
SW
112}
113
114void gfs2_glock_free(struct gfs2_glock *gl)
b3b94faa
DT
115{
116 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa 117
fc0e38da 118 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
bc015cb8
SW
119 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
120 wake_up(&sdp->sd_glock_wait);
b3b94faa
DT
121}
122
123/**
124 * gfs2_glock_hold() - increment reference count on glock
125 * @gl: The glock to hold
126 *
127 */
128
b94a170e 129void gfs2_glock_hold(struct gfs2_glock *gl)
b3b94faa 130{
d8348de0 131 GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
16feb9fe 132 atomic_inc(&gl->gl_ref);
b3b94faa
DT
133}
134
8ff22a6f
BM
135/**
136 * demote_ok - Check to see if it's ok to unlock a glock
137 * @gl: the glock
138 *
139 * Returns: 1 if it's ok
140 */
141
142static int demote_ok(const struct gfs2_glock *gl)
143{
144 const struct gfs2_glock_operations *glops = gl->gl_ops;
145
bc015cb8
SW
146 /* assert_spin_locked(&gl->gl_spin); */
147
8ff22a6f
BM
148 if (gl->gl_state == LM_ST_UNLOCKED)
149 return 0;
bc015cb8
SW
150 if (test_bit(GLF_LFLUSH, &gl->gl_flags))
151 return 0;
152 if ((gl->gl_name.ln_type != LM_TYPE_INODE) &&
153 !list_empty(&gl->gl_holders))
8ff22a6f
BM
154 return 0;
155 if (glops->go_demote_ok)
156 return glops->go_demote_ok(gl);
157 return 1;
158}
159
bc015cb8 160
97cc1025 161/**
bc015cb8 162 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
97cc1025
SW
163 * @gl: the glock
164 *
bc015cb8
SW
165 * If the glock is demotable, then we add it (or move it) to the end
166 * of the glock LRU list.
97cc1025
SW
167 */
168
bc015cb8 169static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
97cc1025 170{
bc015cb8
SW
171 if (demote_ok(gl)) {
172 spin_lock(&lru_lock);
173
174 if (!list_empty(&gl->gl_lru))
175 list_del_init(&gl->gl_lru);
176 else
177 atomic_inc(&lru_count);
178
97cc1025 179 list_add_tail(&gl->gl_lru, &lru_list);
bc015cb8 180 spin_unlock(&lru_lock);
97cc1025 181 }
bc015cb8
SW
182}
183
184void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
185{
186 spin_lock(&gl->gl_spin);
187 __gfs2_glock_schedule_for_reclaim(gl);
188 spin_unlock(&gl->gl_spin);
97cc1025
SW
189}
190
8ff22a6f
BM
191/**
192 * gfs2_glock_put_nolock() - Decrement reference count on glock
193 * @gl: The glock to put
194 *
195 * This function should only be used if the caller has its own reference
196 * to the glock, in addition to the one it is dropping.
197 */
198
b94a170e 199void gfs2_glock_put_nolock(struct gfs2_glock *gl)
8ff22a6f
BM
200{
201 if (atomic_dec_and_test(&gl->gl_ref))
202 GLOCK_BUG_ON(gl, 1);
8ff22a6f
BM
203}
204
b3b94faa
DT
205/**
206 * gfs2_glock_put() - Decrement reference count on glock
207 * @gl: The glock to put
208 *
209 */
210
bc015cb8 211void gfs2_glock_put(struct gfs2_glock *gl)
b3b94faa 212{
bc015cb8
SW
213 struct gfs2_sbd *sdp = gl->gl_sbd;
214 struct address_space *mapping = gfs2_glock2aspace(gl);
b3b94faa 215
bc015cb8
SW
216 if (atomic_dec_and_test(&gl->gl_ref)) {
217 spin_lock_bucket(gl->gl_hash);
218 hlist_bl_del_rcu(&gl->gl_list);
219 spin_unlock_bucket(gl->gl_hash);
220 spin_lock(&lru_lock);
97cc1025
SW
221 if (!list_empty(&gl->gl_lru)) {
222 list_del_init(&gl->gl_lru);
223 atomic_dec(&lru_count);
224 }
225 spin_unlock(&lru_lock);
6802e340 226 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
bc015cb8
SW
227 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
228 trace_gfs2_glock_put(gl);
229 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
b3b94faa 230 }
b3b94faa
DT
231}
232
b3b94faa
DT
233/**
234 * search_bucket() - Find struct gfs2_glock by lock number
235 * @bucket: the bucket to search
236 * @name: The lock name
237 *
238 * Returns: NULL, or the struct gfs2_glock with the requested number
239 */
240
37b2fa6a 241static struct gfs2_glock *search_bucket(unsigned int hash,
899be4d3 242 const struct gfs2_sbd *sdp,
d6a53727 243 const struct lm_lockname *name)
b3b94faa
DT
244{
245 struct gfs2_glock *gl;
bc015cb8 246 struct hlist_bl_node *h;
b3b94faa 247
bc015cb8 248 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
b3b94faa
DT
249 if (!lm_name_equal(&gl->gl_name, name))
250 continue;
899be4d3
SW
251 if (gl->gl_sbd != sdp)
252 continue;
bc015cb8
SW
253 if (atomic_inc_not_zero(&gl->gl_ref))
254 return gl;
b3b94faa
DT
255 }
256
257 return NULL;
258}
259
6802e340
SW
260/**
261 * may_grant - check if its ok to grant a new lock
262 * @gl: The glock
263 * @gh: The lock request which we wish to grant
264 *
265 * Returns: true if its ok to grant the lock
266 */
267
268static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
269{
270 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
271 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
272 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
273 return 0;
274 if (gl->gl_state == gh->gh_state)
275 return 1;
276 if (gh->gh_flags & GL_EXACT)
277 return 0;
209806ab
SW
278 if (gl->gl_state == LM_ST_EXCLUSIVE) {
279 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
280 return 1;
281 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
282 return 1;
283 }
6802e340
SW
284 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
285 return 1;
286 return 0;
287}
288
289static void gfs2_holder_wake(struct gfs2_holder *gh)
290{
291 clear_bit(HIF_WAIT, &gh->gh_iflags);
292 smp_mb__after_clear_bit();
293 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
294}
295
d5341a92
SW
296/**
297 * do_error - Something unexpected has happened during a lock request
298 *
299 */
300
301static inline void do_error(struct gfs2_glock *gl, const int ret)
302{
303 struct gfs2_holder *gh, *tmp;
304
305 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
306 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
307 continue;
308 if (ret & LM_OUT_ERROR)
309 gh->gh_error = -EIO;
310 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
311 gh->gh_error = GLR_TRYFAILED;
312 else
313 continue;
314 list_del_init(&gh->gh_list);
315 trace_gfs2_glock_queue(gh, 0);
316 gfs2_holder_wake(gh);
317 }
318}
319
6802e340
SW
320/**
321 * do_promote - promote as many requests as possible on the current queue
322 * @gl: The glock
323 *
813e0c46
SW
324 * Returns: 1 if there is a blocked holder at the head of the list, or 2
325 * if a type specific operation is underway.
6802e340
SW
326 */
327
328static int do_promote(struct gfs2_glock *gl)
55ba474d
HH
329__releases(&gl->gl_spin)
330__acquires(&gl->gl_spin)
6802e340
SW
331{
332 const struct gfs2_glock_operations *glops = gl->gl_ops;
333 struct gfs2_holder *gh, *tmp;
334 int ret;
335
336restart:
337 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
338 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
339 continue;
340 if (may_grant(gl, gh)) {
341 if (gh->gh_list.prev == &gl->gl_holders &&
342 glops->go_lock) {
343 spin_unlock(&gl->gl_spin);
344 /* FIXME: eliminate this eventually */
345 ret = glops->go_lock(gh);
346 spin_lock(&gl->gl_spin);
347 if (ret) {
813e0c46
SW
348 if (ret == 1)
349 return 2;
6802e340
SW
350 gh->gh_error = ret;
351 list_del_init(&gh->gh_list);
63997775 352 trace_gfs2_glock_queue(gh, 0);
6802e340
SW
353 gfs2_holder_wake(gh);
354 goto restart;
355 }
356 set_bit(HIF_HOLDER, &gh->gh_iflags);
63997775 357 trace_gfs2_promote(gh, 1);
6802e340
SW
358 gfs2_holder_wake(gh);
359 goto restart;
360 }
361 set_bit(HIF_HOLDER, &gh->gh_iflags);
63997775 362 trace_gfs2_promote(gh, 0);
6802e340
SW
363 gfs2_holder_wake(gh);
364 continue;
365 }
366 if (gh->gh_list.prev == &gl->gl_holders)
367 return 1;
d5341a92 368 do_error(gl, 0);
6802e340
SW
369 break;
370 }
371 return 0;
372}
373
6802e340
SW
374/**
375 * find_first_waiter - find the first gh that's waiting for the glock
376 * @gl: the glock
377 */
378
379static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
380{
381 struct gfs2_holder *gh;
382
383 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
384 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
385 return gh;
386 }
387 return NULL;
388}
389
390/**
391 * state_change - record that the glock is now in a different state
392 * @gl: the glock
393 * @new_state the new state
394 *
395 */
396
397static void state_change(struct gfs2_glock *gl, unsigned int new_state)
398{
399 int held1, held2;
400
401 held1 = (gl->gl_state != LM_ST_UNLOCKED);
402 held2 = (new_state != LM_ST_UNLOCKED);
403
404 if (held1 != held2) {
405 if (held2)
406 gfs2_glock_hold(gl);
407 else
8ff22a6f 408 gfs2_glock_put_nolock(gl);
6802e340 409 }
7b5e3d5f
SW
410 if (held1 && held2 && list_empty(&gl->gl_holders))
411 clear_bit(GLF_QUEUED, &gl->gl_flags);
6802e340
SW
412
413 gl->gl_state = new_state;
414 gl->gl_tchange = jiffies;
415}
416
417static void gfs2_demote_wake(struct gfs2_glock *gl)
418{
419 gl->gl_demote_state = LM_ST_EXCLUSIVE;
420 clear_bit(GLF_DEMOTE, &gl->gl_flags);
421 smp_mb__after_clear_bit();
422 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
423}
424
425/**
426 * finish_xmote - The DLM has replied to one of our lock requests
427 * @gl: The glock
428 * @ret: The status from the DLM
429 *
430 */
431
432static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
433{
434 const struct gfs2_glock_operations *glops = gl->gl_ops;
435 struct gfs2_holder *gh;
436 unsigned state = ret & LM_OUT_ST_MASK;
813e0c46 437 int rv;
6802e340
SW
438
439 spin_lock(&gl->gl_spin);
63997775 440 trace_gfs2_glock_state_change(gl, state);
6802e340
SW
441 state_change(gl, state);
442 gh = find_first_waiter(gl);
443
444 /* Demote to UN request arrived during demote to SH or DF */
445 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
446 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
447 gl->gl_target = LM_ST_UNLOCKED;
448
449 /* Check for state != intended state */
450 if (unlikely(state != gl->gl_target)) {
451 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
452 /* move to back of queue and try next entry */
453 if (ret & LM_OUT_CANCELED) {
454 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
455 list_move_tail(&gh->gh_list, &gl->gl_holders);
456 gh = find_first_waiter(gl);
457 gl->gl_target = gh->gh_state;
458 goto retry;
459 }
460 /* Some error or failed "try lock" - report it */
461 if ((ret & LM_OUT_ERROR) ||
462 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
463 gl->gl_target = gl->gl_state;
464 do_error(gl, ret);
465 goto out;
466 }
467 }
468 switch(state) {
469 /* Unlocked due to conversion deadlock, try again */
470 case LM_ST_UNLOCKED:
471retry:
472 do_xmote(gl, gh, gl->gl_target);
473 break;
474 /* Conversion fails, unlock and try again */
475 case LM_ST_SHARED:
476 case LM_ST_DEFERRED:
477 do_xmote(gl, gh, LM_ST_UNLOCKED);
478 break;
479 default: /* Everything else */
480 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
481 GLOCK_BUG_ON(gl, 1);
482 }
483 spin_unlock(&gl->gl_spin);
6802e340
SW
484 return;
485 }
486
487 /* Fast path - we got what we asked for */
488 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
489 gfs2_demote_wake(gl);
490 if (state != LM_ST_UNLOCKED) {
491 if (glops->go_xmote_bh) {
6802e340
SW
492 spin_unlock(&gl->gl_spin);
493 rv = glops->go_xmote_bh(gl, gh);
6802e340
SW
494 spin_lock(&gl->gl_spin);
495 if (rv) {
496 do_error(gl, rv);
497 goto out;
498 }
499 }
813e0c46
SW
500 rv = do_promote(gl);
501 if (rv == 2)
502 goto out_locked;
6802e340
SW
503 }
504out:
505 clear_bit(GLF_LOCK, &gl->gl_flags);
813e0c46 506out_locked:
6802e340 507 spin_unlock(&gl->gl_spin);
6802e340
SW
508}
509
6802e340
SW
510/**
511 * do_xmote - Calls the DLM to change the state of a lock
512 * @gl: The lock state
513 * @gh: The holder (only for promotes)
514 * @target: The target lock state
515 *
516 */
517
518static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
55ba474d
HH
519__releases(&gl->gl_spin)
520__acquires(&gl->gl_spin)
6802e340
SW
521{
522 const struct gfs2_glock_operations *glops = gl->gl_ops;
523 struct gfs2_sbd *sdp = gl->gl_sbd;
524 unsigned int lck_flags = gh ? gh->gh_flags : 0;
525 int ret;
526
527 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
528 LM_FLAG_PRIORITY);
921169ca
SW
529 GLOCK_BUG_ON(gl, gl->gl_state == target);
530 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
6802e340
SW
531 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
532 glops->go_inval) {
533 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
534 do_error(gl, 0); /* Fail queued try locks */
535 }
47a25380 536 gl->gl_req = target;
6802e340
SW
537 spin_unlock(&gl->gl_spin);
538 if (glops->go_xmote_th)
539 glops->go_xmote_th(gl);
540 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
541 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
542 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
543
544 gfs2_glock_hold(gl);
545 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
546 gl->gl_state == LM_ST_DEFERRED) &&
547 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
548 lck_flags |= LM_FLAG_TRY_1CB;
6802e340 549
921169ca
SW
550 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
551 /* lock_dlm */
552 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
553 GLOCK_BUG_ON(gl, ret);
554 } else { /* lock_nolock */
555 finish_xmote(gl, target);
6802e340
SW
556 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
557 gfs2_glock_put(gl);
6802e340 558 }
921169ca 559
6802e340
SW
560 spin_lock(&gl->gl_spin);
561}
562
563/**
564 * find_first_holder - find the first "holder" gh
565 * @gl: the glock
566 */
567
568static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
569{
570 struct gfs2_holder *gh;
571
572 if (!list_empty(&gl->gl_holders)) {
573 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
574 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
575 return gh;
576 }
577 return NULL;
578}
579
580/**
581 * run_queue - do all outstanding tasks related to a glock
582 * @gl: The glock in question
583 * @nonblock: True if we must not block in run_queue
584 *
585 */
586
587static void run_queue(struct gfs2_glock *gl, const int nonblock)
55ba474d
HH
588__releases(&gl->gl_spin)
589__acquires(&gl->gl_spin)
6802e340
SW
590{
591 struct gfs2_holder *gh = NULL;
813e0c46 592 int ret;
6802e340
SW
593
594 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
595 return;
596
597 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
598
599 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
600 gl->gl_demote_state != gl->gl_state) {
601 if (find_first_holder(gl))
d8348de0 602 goto out_unlock;
6802e340
SW
603 if (nonblock)
604 goto out_sched;
605 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
265d529c 606 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
6802e340
SW
607 gl->gl_target = gl->gl_demote_state;
608 } else {
609 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
610 gfs2_demote_wake(gl);
813e0c46
SW
611 ret = do_promote(gl);
612 if (ret == 0)
d8348de0 613 goto out_unlock;
813e0c46 614 if (ret == 2)
a228df63 615 goto out;
6802e340
SW
616 gh = find_first_waiter(gl);
617 gl->gl_target = gh->gh_state;
618 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
619 do_error(gl, 0); /* Fail queued try locks */
620 }
621 do_xmote(gl, gh, gl->gl_target);
a228df63 622out:
6802e340
SW
623 return;
624
625out_sched:
7e71c55e
SW
626 clear_bit(GLF_LOCK, &gl->gl_flags);
627 smp_mb__after_clear_bit();
6802e340
SW
628 gfs2_glock_hold(gl);
629 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
8ff22a6f 630 gfs2_glock_put_nolock(gl);
7e71c55e
SW
631 return;
632
d8348de0 633out_unlock:
6802e340 634 clear_bit(GLF_LOCK, &gl->gl_flags);
7e71c55e
SW
635 smp_mb__after_clear_bit();
636 return;
6802e340
SW
637}
638
b94a170e
BM
639static void delete_work_func(struct work_struct *work)
640{
641 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
642 struct gfs2_sbd *sdp = gl->gl_sbd;
044b9414 643 struct gfs2_inode *ip;
b94a170e 644 struct inode *inode;
044b9414
SW
645 u64 no_addr = gl->gl_name.ln_number;
646
647 ip = gl->gl_object;
648 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
b94a170e 649
b94a170e 650 if (ip)
b94a170e 651 inode = gfs2_ilookup(sdp->sd_vfs, no_addr);
044b9414
SW
652 else
653 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
654 if (inode && !IS_ERR(inode)) {
655 d_prune_aliases(inode);
656 iput(inode);
b94a170e
BM
657 }
658 gfs2_glock_put(gl);
659}
660
c4f68a13
BM
661static void glock_work_func(struct work_struct *work)
662{
6802e340 663 unsigned long delay = 0;
c4f68a13 664 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
26bb7505 665 int drop_ref = 0;
c4f68a13 666
26bb7505 667 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
6802e340 668 finish_xmote(gl, gl->gl_reply);
26bb7505
SW
669 drop_ref = 1;
670 }
c4f68a13 671 spin_lock(&gl->gl_spin);
265d529c
SW
672 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
673 gl->gl_state != LM_ST_UNLOCKED &&
674 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
6802e340
SW
675 unsigned long holdtime, now = jiffies;
676 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
677 if (time_before(now, holdtime))
678 delay = holdtime - now;
679 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
680 }
681 run_queue(gl, 0);
c4f68a13 682 spin_unlock(&gl->gl_spin);
6802e340
SW
683 if (!delay ||
684 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
685 gfs2_glock_put(gl);
26bb7505
SW
686 if (drop_ref)
687 gfs2_glock_put(gl);
c4f68a13
BM
688}
689
b3b94faa
DT
690/**
691 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
692 * @sdp: The GFS2 superblock
693 * @number: the lock number
694 * @glops: The glock_operations to use
695 * @create: If 0, don't create the glock if it doesn't exist
696 * @glp: the glock is returned here
697 *
698 * This does not lock a glock, just finds/creates structures for one.
699 *
700 * Returns: errno
701 */
702
cd915493 703int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
8fb4b536 704 const struct gfs2_glock_operations *glops, int create,
b3b94faa
DT
705 struct gfs2_glock **glp)
706{
009d8518 707 struct super_block *s = sdp->sd_vfs;
37b2fa6a 708 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
b3b94faa 709 struct gfs2_glock *gl, *tmp;
37b2fa6a 710 unsigned int hash = gl_hash(sdp, &name);
009d8518 711 struct address_space *mapping;
bc015cb8 712 struct kmem_cache *cachep;
b3b94faa 713
bc015cb8 714 rcu_read_lock();
37b2fa6a 715 gl = search_bucket(hash, sdp, &name);
bc015cb8 716 rcu_read_unlock();
b3b94faa 717
64d576ba
SW
718 *glp = gl;
719 if (gl)
b3b94faa 720 return 0;
64d576ba
SW
721 if (!create)
722 return -ENOENT;
b3b94faa 723
009d8518 724 if (glops->go_flags & GLOF_ASPACE)
bc015cb8 725 cachep = gfs2_glock_aspace_cachep;
009d8518 726 else
bc015cb8
SW
727 cachep = gfs2_glock_cachep;
728 gl = kmem_cache_alloc(cachep, GFP_KERNEL);
b3b94faa
DT
729 if (!gl)
730 return -ENOMEM;
731
8f05228e 732 atomic_inc(&sdp->sd_glock_disposal);
ec45d9f5 733 gl->gl_flags = 0;
b3b94faa 734 gl->gl_name = name;
16feb9fe 735 atomic_set(&gl->gl_ref, 1);
b3b94faa 736 gl->gl_state = LM_ST_UNLOCKED;
6802e340 737 gl->gl_target = LM_ST_UNLOCKED;
c4f68a13 738 gl->gl_demote_state = LM_ST_EXCLUSIVE;
37b2fa6a 739 gl->gl_hash = hash;
b3b94faa 740 gl->gl_ops = glops;
f057f6cd
SW
741 snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
742 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
743 gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
c4f68a13 744 gl->gl_tchange = jiffies;
ec45d9f5 745 gl->gl_object = NULL;
b3b94faa 746 gl->gl_sbd = sdp;
c4f68a13 747 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
b94a170e 748 INIT_WORK(&gl->gl_delete, delete_work_func);
b3b94faa 749
009d8518
SW
750 mapping = gfs2_glock2aspace(gl);
751 if (mapping) {
752 mapping->a_ops = &gfs2_meta_aops;
753 mapping->host = s->s_bdev->bd_inode;
754 mapping->flags = 0;
755 mapping_set_gfp_mask(mapping, GFP_NOFS);
756 mapping->assoc_mapping = NULL;
757 mapping->backing_dev_info = s->s_bdi;
758 mapping->writeback_index = 0;
b3b94faa
DT
759 }
760
bc015cb8 761 spin_lock_bucket(hash);
37b2fa6a 762 tmp = search_bucket(hash, sdp, &name);
b3b94faa 763 if (tmp) {
bc015cb8
SW
764 spin_unlock_bucket(hash);
765 kmem_cache_free(cachep, gl);
fc0e38da 766 atomic_dec(&sdp->sd_glock_disposal);
b3b94faa
DT
767 gl = tmp;
768 } else {
bc015cb8
SW
769 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
770 spin_unlock_bucket(hash);
b3b94faa
DT
771 }
772
773 *glp = gl;
774
775 return 0;
b3b94faa
DT
776}
777
778/**
779 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
780 * @gl: the glock
781 * @state: the state we're requesting
782 * @flags: the modifier flags
783 * @gh: the holder structure
784 *
785 */
786
190562bd 787void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
b3b94faa
DT
788 struct gfs2_holder *gh)
789{
790 INIT_LIST_HEAD(&gh->gh_list);
791 gh->gh_gl = gl;
d0dc80db 792 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b1e058da 793 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
794 gh->gh_state = state;
795 gh->gh_flags = flags;
796 gh->gh_error = 0;
797 gh->gh_iflags = 0;
b3b94faa
DT
798 gfs2_glock_hold(gl);
799}
800
801/**
802 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
803 * @state: the state we're requesting
804 * @flags: the modifier flags
805 * @gh: the holder structure
806 *
807 * Don't mess with the glock.
808 *
809 */
810
190562bd 811void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
b3b94faa
DT
812{
813 gh->gh_state = state;
579b78a4 814 gh->gh_flags = flags;
3b8249f6 815 gh->gh_iflags = 0;
d0dc80db 816 gh->gh_ip = (unsigned long)__builtin_return_address(0);
1a0eae88
BP
817 if (gh->gh_owner_pid)
818 put_pid(gh->gh_owner_pid);
819 gh->gh_owner_pid = get_pid(task_pid(current));
b3b94faa
DT
820}
821
822/**
823 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
824 * @gh: the holder structure
825 *
826 */
827
828void gfs2_holder_uninit(struct gfs2_holder *gh)
829{
b1e058da 830 put_pid(gh->gh_owner_pid);
b3b94faa
DT
831 gfs2_glock_put(gh->gh_gl);
832 gh->gh_gl = NULL;
d0dc80db 833 gh->gh_ip = 0;
b3b94faa
DT
834}
835
fe64d517
SW
836/**
837 * gfs2_glock_holder_wait
838 * @word: unused
839 *
840 * This function and gfs2_glock_demote_wait both show up in the WCHAN
841 * field. Thus I've separated these otherwise identical functions in
842 * order to be more informative to the user.
843 */
844
845static int gfs2_glock_holder_wait(void *word)
fee852e3
SW
846{
847 schedule();
848 return 0;
849}
850
fe64d517
SW
851static int gfs2_glock_demote_wait(void *word)
852{
853 schedule();
854 return 0;
855}
856
6802e340 857static void wait_on_holder(struct gfs2_holder *gh)
da755fdb 858{
6802e340 859 might_sleep();
fe64d517 860 wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
da755fdb
SW
861}
862
6802e340 863static void wait_on_demote(struct gfs2_glock *gl)
b3b94faa 864{
6802e340 865 might_sleep();
fe64d517 866 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
b3b94faa
DT
867}
868
869/**
6802e340
SW
870 * handle_callback - process a demote request
871 * @gl: the glock
872 * @state: the state the caller wants us to change to
b3b94faa 873 *
6802e340
SW
874 * There are only two requests that we are going to see in actual
875 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
b3b94faa
DT
876 */
877
6802e340 878static void handle_callback(struct gfs2_glock *gl, unsigned int state,
97cc1025 879 unsigned long delay)
b3b94faa 880{
6802e340 881 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
b3b94faa 882
6802e340
SW
883 set_bit(bit, &gl->gl_flags);
884 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
885 gl->gl_demote_state = state;
886 gl->gl_demote_time = jiffies;
6802e340
SW
887 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
888 gl->gl_demote_state != state) {
889 gl->gl_demote_state = LM_ST_UNLOCKED;
b3b94faa 890 }
b94a170e
BM
891 if (gl->gl_ops->go_callback)
892 gl->gl_ops->go_callback(gl);
63997775 893 trace_gfs2_demote_rq(gl);
b3b94faa
DT
894}
895
896/**
6802e340 897 * gfs2_glock_wait - wait on a glock acquisition
b3b94faa
DT
898 * @gh: the glock holder
899 *
900 * Returns: 0 on success
901 */
902
6802e340 903int gfs2_glock_wait(struct gfs2_holder *gh)
b3b94faa 904{
fee852e3 905 wait_on_holder(gh);
b3b94faa
DT
906 return gh->gh_error;
907}
908
6802e340 909void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
7c52b166 910{
5e69069c 911 struct va_format vaf;
7c52b166
RP
912 va_list args;
913
914 va_start(args, fmt);
5e69069c 915
6802e340
SW
916 if (seq) {
917 struct gfs2_glock_iter *gi = seq->private;
7c52b166 918 vsprintf(gi->string, fmt, args);
6802e340
SW
919 seq_printf(seq, gi->string);
920 } else {
5e69069c
JP
921 vaf.fmt = fmt;
922 vaf.va = &args;
923
924 printk(KERN_ERR " %pV", &vaf);
6802e340 925 }
5e69069c 926
7c52b166
RP
927 va_end(args);
928}
929
b3b94faa
DT
930/**
931 * add_to_queue - Add a holder to the wait queue (but look for recursion)
932 * @gh: the holder structure to add
933 *
6802e340
SW
934 * Eventually we should move the recursive locking trap to a
935 * debugging option or something like that. This is the fast
936 * path and needs to have the minimum number of distractions.
937 *
b3b94faa
DT
938 */
939
6802e340 940static inline void add_to_queue(struct gfs2_holder *gh)
55ba474d
HH
941__releases(&gl->gl_spin)
942__acquires(&gl->gl_spin)
b3b94faa
DT
943{
944 struct gfs2_glock *gl = gh->gh_gl;
6802e340
SW
945 struct gfs2_sbd *sdp = gl->gl_sbd;
946 struct list_head *insert_pt = NULL;
947 struct gfs2_holder *gh2;
948 int try_lock = 0;
b3b94faa 949
b1e058da 950 BUG_ON(gh->gh_owner_pid == NULL);
fee852e3
SW
951 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
952 BUG();
190562bd 953
6802e340
SW
954 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
955 if (test_bit(GLF_LOCK, &gl->gl_flags))
956 try_lock = 1;
957 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
958 goto fail;
959 }
960
961 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
962 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
963 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
964 goto trap_recursive;
965 if (try_lock &&
966 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
967 !may_grant(gl, gh)) {
968fail:
969 gh->gh_error = GLR_TRYFAILED;
970 gfs2_holder_wake(gh);
971 return;
b4c20166 972 }
6802e340
SW
973 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
974 continue;
975 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
976 insert_pt = &gh2->gh_list;
977 }
7b5e3d5f 978 set_bit(GLF_QUEUED, &gl->gl_flags);
edae38a6 979 trace_gfs2_glock_queue(gh, 1);
6802e340
SW
980 if (likely(insert_pt == NULL)) {
981 list_add_tail(&gh->gh_list, &gl->gl_holders);
982 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
983 goto do_cancel;
984 return;
985 }
986 list_add_tail(&gh->gh_list, insert_pt);
987do_cancel:
988 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
989 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
990 spin_unlock(&gl->gl_spin);
048bca22 991 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
f057f6cd 992 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
6802e340 993 spin_lock(&gl->gl_spin);
b3b94faa 994 }
6802e340 995 return;
b3b94faa 996
6802e340
SW
997trap_recursive:
998 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
999 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1000 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1001 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1002 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
1003 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
1004 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1005 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1006 __dump_glock(NULL, gl);
1007 BUG();
b3b94faa
DT
1008}
1009
1010/**
1011 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1012 * @gh: the holder structure
1013 *
1014 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1015 *
1016 * Returns: 0, GLR_TRYFAILED, or errno on failure
1017 */
1018
1019int gfs2_glock_nq(struct gfs2_holder *gh)
1020{
1021 struct gfs2_glock *gl = gh->gh_gl;
1022 struct gfs2_sbd *sdp = gl->gl_sbd;
1023 int error = 0;
1024
6802e340 1025 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
b3b94faa 1026 return -EIO;
b3b94faa 1027
b3b94faa
DT
1028 spin_lock(&gl->gl_spin);
1029 add_to_queue(gh);
0809f6ec
SW
1030 if ((LM_FLAG_NOEXP & gh->gh_flags) &&
1031 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1032 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
6802e340 1033 run_queue(gl, 1);
b3b94faa
DT
1034 spin_unlock(&gl->gl_spin);
1035
6802e340
SW
1036 if (!(gh->gh_flags & GL_ASYNC))
1037 error = gfs2_glock_wait(gh);
b3b94faa 1038
b3b94faa
DT
1039 return error;
1040}
1041
1042/**
1043 * gfs2_glock_poll - poll to see if an async request has been completed
1044 * @gh: the holder
1045 *
1046 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1047 */
1048
1049int gfs2_glock_poll(struct gfs2_holder *gh)
1050{
6802e340 1051 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
b3b94faa
DT
1052}
1053
1054/**
1055 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1056 * @gh: the glock holder
1057 *
1058 */
1059
1060void gfs2_glock_dq(struct gfs2_holder *gh)
1061{
1062 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1063 const struct gfs2_glock_operations *glops = gl->gl_ops;
c4f68a13 1064 unsigned delay = 0;
6802e340 1065 int fast_path = 0;
b3b94faa 1066
6802e340 1067 spin_lock(&gl->gl_spin);
b3b94faa 1068 if (gh->gh_flags & GL_NOCACHE)
97cc1025 1069 handle_callback(gl, LM_ST_UNLOCKED, 0);
b3b94faa 1070
b3b94faa 1071 list_del_init(&gh->gh_list);
6802e340 1072 if (find_first_holder(gl) == NULL) {
3042a2cc 1073 if (glops->go_unlock) {
6802e340 1074 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
3042a2cc 1075 spin_unlock(&gl->gl_spin);
b3b94faa 1076 glops->go_unlock(gh);
3042a2cc 1077 spin_lock(&gl->gl_spin);
6802e340 1078 clear_bit(GLF_LOCK, &gl->gl_flags);
3042a2cc 1079 }
6802e340
SW
1080 if (list_empty(&gl->gl_holders) &&
1081 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1082 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1083 fast_path = 1;
b3b94faa 1084 }
bc015cb8 1085 __gfs2_glock_schedule_for_reclaim(gl);
63997775 1086 trace_gfs2_glock_queue(gh, 0);
b3b94faa 1087 spin_unlock(&gl->gl_spin);
6802e340
SW
1088 if (likely(fast_path))
1089 return;
c4f68a13
BM
1090
1091 gfs2_glock_hold(gl);
1092 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1093 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1094 delay = gl->gl_ops->go_min_hold_time;
1095 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1096 gfs2_glock_put(gl);
b3b94faa
DT
1097}
1098
d93cfa98
AD
1099void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1100{
1101 struct gfs2_glock *gl = gh->gh_gl;
1102 gfs2_glock_dq(gh);
1103 wait_on_demote(gl);
1104}
1105
b3b94faa
DT
1106/**
1107 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1108 * @gh: the holder structure
1109 *
1110 */
1111
1112void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1113{
1114 gfs2_glock_dq(gh);
1115 gfs2_holder_uninit(gh);
1116}
1117
1118/**
1119 * gfs2_glock_nq_num - acquire a glock based on lock number
1120 * @sdp: the filesystem
1121 * @number: the lock number
1122 * @glops: the glock operations for the type of glock
1123 * @state: the state to acquire the glock in
25985edc 1124 * @flags: modifier flags for the acquisition
b3b94faa
DT
1125 * @gh: the struct gfs2_holder
1126 *
1127 * Returns: errno
1128 */
1129
cd915493 1130int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
8fb4b536
SW
1131 const struct gfs2_glock_operations *glops,
1132 unsigned int state, int flags, struct gfs2_holder *gh)
b3b94faa
DT
1133{
1134 struct gfs2_glock *gl;
1135 int error;
1136
1137 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1138 if (!error) {
1139 error = gfs2_glock_nq_init(gl, state, flags, gh);
1140 gfs2_glock_put(gl);
1141 }
1142
1143 return error;
1144}
1145
1146/**
1147 * glock_compare - Compare two struct gfs2_glock structures for sorting
1148 * @arg_a: the first structure
1149 * @arg_b: the second structure
1150 *
1151 */
1152
1153static int glock_compare(const void *arg_a, const void *arg_b)
1154{
a5e08a9e
SW
1155 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1156 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1157 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1158 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
b3b94faa
DT
1159
1160 if (a->ln_number > b->ln_number)
a5e08a9e
SW
1161 return 1;
1162 if (a->ln_number < b->ln_number)
1163 return -1;
1c0f4872 1164 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
a5e08a9e 1165 return 0;
b3b94faa
DT
1166}
1167
1168/**
1169 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1170 * @num_gh: the number of structures
1171 * @ghs: an array of struct gfs2_holder structures
1172 *
1173 * Returns: 0 on success (all glocks acquired),
1174 * errno on failure (no glocks acquired)
1175 */
1176
1177static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1178 struct gfs2_holder **p)
1179{
1180 unsigned int x;
1181 int error = 0;
1182
1183 for (x = 0; x < num_gh; x++)
1184 p[x] = &ghs[x];
1185
1186 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1187
1188 for (x = 0; x < num_gh; x++) {
1189 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1190
1191 error = gfs2_glock_nq(p[x]);
1192 if (error) {
1193 while (x--)
1194 gfs2_glock_dq(p[x]);
1195 break;
1196 }
1197 }
1198
1199 return error;
1200}
1201
1202/**
1203 * gfs2_glock_nq_m - acquire multiple glocks
1204 * @num_gh: the number of structures
1205 * @ghs: an array of struct gfs2_holder structures
1206 *
b3b94faa
DT
1207 *
1208 * Returns: 0 on success (all glocks acquired),
1209 * errno on failure (no glocks acquired)
1210 */
1211
1212int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1213{
eaf5bd3c
SW
1214 struct gfs2_holder *tmp[4];
1215 struct gfs2_holder **pph = tmp;
b3b94faa
DT
1216 int error = 0;
1217
eaf5bd3c
SW
1218 switch(num_gh) {
1219 case 0:
b3b94faa 1220 return 0;
eaf5bd3c 1221 case 1:
b3b94faa
DT
1222 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1223 return gfs2_glock_nq(ghs);
eaf5bd3c
SW
1224 default:
1225 if (num_gh <= 4)
b3b94faa 1226 break;
eaf5bd3c
SW
1227 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1228 if (!pph)
1229 return -ENOMEM;
b3b94faa
DT
1230 }
1231
eaf5bd3c 1232 error = nq_m_sync(num_gh, ghs, pph);
b3b94faa 1233
eaf5bd3c
SW
1234 if (pph != tmp)
1235 kfree(pph);
b3b94faa
DT
1236
1237 return error;
1238}
1239
1240/**
1241 * gfs2_glock_dq_m - release multiple glocks
1242 * @num_gh: the number of structures
1243 * @ghs: an array of struct gfs2_holder structures
1244 *
1245 */
1246
1247void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1248{
fa1bbdea
BP
1249 while (num_gh--)
1250 gfs2_glock_dq(&ghs[num_gh]);
b3b94faa
DT
1251}
1252
1253/**
1254 * gfs2_glock_dq_uninit_m - release multiple glocks
1255 * @num_gh: the number of structures
1256 * @ghs: an array of struct gfs2_holder structures
1257 *
1258 */
1259
1260void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1261{
fa1bbdea
BP
1262 while (num_gh--)
1263 gfs2_glock_dq_uninit(&ghs[num_gh]);
b3b94faa
DT
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
6802e340 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 |
58a69cb4 1693 WQ_HIGHPRI | WQ_FREEZABLE, 0);
97cc1025 1694 if (IS_ERR(glock_workqueue))
c4f68a13 1695 return PTR_ERR(glock_workqueue);
d2115778 1696 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
58a69cb4 1697 WQ_MEM_RECLAIM | WQ_FREEZABLE,
d2115778 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{
7e32d026 1724 return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
bc015cb8
SW
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}
This page took 1.096291 seconds and 5 git commands to generate.