[GFS2] Update copyright, tidy up incore.h
[deliverable/linux.git] / fs / gfs2 / glock.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 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>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/delay.h>
16#include <linux/sort.h>
17#include <linux/jhash.h>
18#include <linux/kref.h>
d0dc80db 19#include <linux/kallsyms.h>
5c676f6d 20#include <linux/gfs2_ondisk.h>
b3b94faa
DT
21#include <asm/uaccess.h>
22
23#include "gfs2.h"
5c676f6d
SW
24#include "lm_interface.h"
25#include "incore.h"
b3b94faa
DT
26#include "glock.h"
27#include "glops.h"
28#include "inode.h"
29#include "lm.h"
30#include "lops.h"
31#include "meta_io.h"
32#include "quota.h"
33#include "super.h"
5c676f6d 34#include "util.h"
b3b94faa
DT
35
36/* Must be kept in sync with the beginning of struct gfs2_glock */
37struct glock_plug {
38 struct list_head gl_list;
39 unsigned long gl_flags;
40};
41
42struct greedy {
43 struct gfs2_holder gr_gh;
44 struct work_struct gr_work;
45};
46
47typedef void (*glock_examiner) (struct gfs2_glock * gl);
48
08bc2dbc 49static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
320dd101 50static int dump_glock(struct gfs2_glock *gl);
08bc2dbc 51
b3b94faa
DT
52/**
53 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
54 * @actual: the current state of the lock
55 * @requested: the lock state that was requested by the caller
56 * @flags: the modifier flags passed in by the caller
57 *
58 * Returns: 1 if the locks are compatible, 0 otherwise
59 */
60
61static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
62 int flags)
63{
64 if (actual == requested)
65 return 1;
66
67 if (flags & GL_EXACT)
68 return 0;
69
70 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
71 return 1;
72
73 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
74 return 1;
75
76 return 0;
77}
78
79/**
80 * gl_hash() - Turn glock number into hash bucket number
81 * @lock: The glock number
82 *
83 * Returns: The number of the corresponding hash bucket
84 */
85
d6a53727 86static unsigned int gl_hash(const struct lm_lockname *name)
b3b94faa
DT
87{
88 unsigned int h;
89
90 h = jhash(&name->ln_number, sizeof(uint64_t), 0);
91 h = jhash(&name->ln_type, sizeof(unsigned int), h);
92 h &= GFS2_GL_HASH_MASK;
93
94 return h;
95}
96
97/**
98 * glock_free() - Perform a few checks and then release struct gfs2_glock
99 * @gl: The glock to release
100 *
101 * Also calls lock module to release its internal structure for this glock.
102 *
103 */
104
105static void glock_free(struct gfs2_glock *gl)
106{
107 struct gfs2_sbd *sdp = gl->gl_sbd;
108 struct inode *aspace = gl->gl_aspace;
109
110 gfs2_lm_put_lock(sdp, gl->gl_lock);
111
112 if (aspace)
113 gfs2_aspace_put(aspace);
114
115 kmem_cache_free(gfs2_glock_cachep, gl);
b3b94faa
DT
116}
117
118/**
119 * gfs2_glock_hold() - increment reference count on glock
120 * @gl: The glock to hold
121 *
122 */
123
124void gfs2_glock_hold(struct gfs2_glock *gl)
125{
126 kref_get(&gl->gl_ref);
127}
128
129/* All work is done after the return from kref_put() so we
130 can release the write_lock before the free. */
131
132static void kill_glock(struct kref *kref)
133{
134 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
135 struct gfs2_sbd *sdp = gl->gl_sbd;
136
137 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
138 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
139 gfs2_assert(sdp, list_empty(&gl->gl_holders));
140 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
141 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
143}
144
145/**
146 * gfs2_glock_put() - Decrement reference count on glock
147 * @gl: The glock to put
148 *
149 */
150
151int gfs2_glock_put(struct gfs2_glock *gl)
152{
b3b94faa
DT
153 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
154 int rv = 0;
155
b3b94faa
DT
156 write_lock(&bucket->hb_lock);
157 if (kref_put(&gl->gl_ref, kill_glock)) {
158 list_del_init(&gl->gl_list);
159 write_unlock(&bucket->hb_lock);
190562bd 160 BUG_ON(spin_is_locked(&gl->gl_spin));
b3b94faa
DT
161 glock_free(gl);
162 rv = 1;
163 goto out;
164 }
165 write_unlock(&bucket->hb_lock);
a2242db0 166out:
b3b94faa
DT
167 return rv;
168}
169
170/**
171 * queue_empty - check to see if a glock's queue is empty
172 * @gl: the glock
173 * @head: the head of the queue to check
174 *
175 * This function protects the list in the event that a process already
176 * has a holder on the list and is adding a second holder for itself.
177 * The glmutex lock is what generally prevents processes from working
178 * on the same glock at once, but the special case of adding a second
179 * holder for yourself ("recursive" locking) doesn't involve locking
180 * glmutex, making the spin lock necessary.
181 *
182 * Returns: 1 if the queue is empty
183 */
184
185static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
186{
187 int empty;
188 spin_lock(&gl->gl_spin);
189 empty = list_empty(head);
190 spin_unlock(&gl->gl_spin);
191 return empty;
192}
193
194/**
195 * search_bucket() - Find struct gfs2_glock by lock number
196 * @bucket: the bucket to search
197 * @name: The lock name
198 *
199 * Returns: NULL, or the struct gfs2_glock with the requested number
200 */
201
202static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
899be4d3 203 const struct gfs2_sbd *sdp,
d6a53727 204 const struct lm_lockname *name)
b3b94faa
DT
205{
206 struct gfs2_glock *gl;
207
208 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
209 if (test_bit(GLF_PLUG, &gl->gl_flags))
210 continue;
211 if (!lm_name_equal(&gl->gl_name, name))
212 continue;
899be4d3
SW
213 if (gl->gl_sbd != sdp)
214 continue;
b3b94faa
DT
215
216 kref_get(&gl->gl_ref);
217
218 return gl;
219 }
220
221 return NULL;
222}
223
224/**
225 * gfs2_glock_find() - Find glock by lock number
226 * @sdp: The GFS2 superblock
227 * @name: The lock name
228 *
229 * Returns: NULL, or the struct gfs2_glock with the requested number
230 */
231
08bc2dbc 232static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
d6a53727 233 const struct lm_lockname *name)
b3b94faa
DT
234{
235 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)];
236 struct gfs2_glock *gl;
237
238 read_lock(&bucket->hb_lock);
899be4d3 239 gl = search_bucket(bucket, sdp, name);
b3b94faa
DT
240 read_unlock(&bucket->hb_lock);
241
242 return gl;
243}
244
245/**
246 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
247 * @sdp: The GFS2 superblock
248 * @number: the lock number
249 * @glops: The glock_operations to use
250 * @create: If 0, don't create the glock if it doesn't exist
251 * @glp: the glock is returned here
252 *
253 * This does not lock a glock, just finds/creates structures for one.
254 *
255 * Returns: errno
256 */
257
258int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number,
8fb4b536 259 const struct gfs2_glock_operations *glops, int create,
b3b94faa
DT
260 struct gfs2_glock **glp)
261{
262 struct lm_lockname name;
263 struct gfs2_glock *gl, *tmp;
264 struct gfs2_gl_hash_bucket *bucket;
265 int error;
266
267 name.ln_number = number;
268 name.ln_type = glops->go_type;
269 bucket = &sdp->sd_gl_hash[gl_hash(&name)];
270
271 read_lock(&bucket->hb_lock);
899be4d3 272 gl = search_bucket(bucket, sdp, &name);
b3b94faa
DT
273 read_unlock(&bucket->hb_lock);
274
275 if (gl || !create) {
276 *glp = gl;
277 return 0;
278 }
279
280 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
281 if (!gl)
282 return -ENOMEM;
283
ec45d9f5 284 gl->gl_flags = 0;
b3b94faa
DT
285 gl->gl_name = name;
286 kref_init(&gl->gl_ref);
b3b94faa 287 gl->gl_state = LM_ST_UNLOCKED;
320dd101
SW
288 gl->gl_owner = NULL;
289 gl->gl_ip = 0;
b3b94faa 290 gl->gl_ops = glops;
ec45d9f5
SW
291 gl->gl_req_gh = NULL;
292 gl->gl_req_bh = NULL;
293 gl->gl_vn = 0;
294 gl->gl_stamp = jiffies;
295 gl->gl_object = NULL;
b3b94faa 296 gl->gl_bucket = bucket;
b3b94faa 297 gl->gl_sbd = sdp;
ec45d9f5 298 gl->gl_aspace = NULL;
b3b94faa 299 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
b3b94faa
DT
300
301 /* If this glock protects actual on-disk data or metadata blocks,
302 create a VFS inode to manage the pages/buffers holding them. */
303 if (glops == &gfs2_inode_glops ||
f45b7ddd 304 glops == &gfs2_rgrp_glops) {
b3b94faa
DT
305 gl->gl_aspace = gfs2_aspace_get(sdp);
306 if (!gl->gl_aspace) {
307 error = -ENOMEM;
308 goto fail;
309 }
310 }
311
312 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
313 if (error)
314 goto fail_aspace;
315
b3b94faa 316 write_lock(&bucket->hb_lock);
899be4d3 317 tmp = search_bucket(bucket, sdp, &name);
b3b94faa
DT
318 if (tmp) {
319 write_unlock(&bucket->hb_lock);
320 glock_free(gl);
321 gl = tmp;
322 } else {
323 list_add_tail(&gl->gl_list, &bucket->hb_list);
324 write_unlock(&bucket->hb_lock);
325 }
326
327 *glp = gl;
328
329 return 0;
330
ec45d9f5 331fail_aspace:
b3b94faa
DT
332 if (gl->gl_aspace)
333 gfs2_aspace_put(gl->gl_aspace);
ec45d9f5 334fail:
b3b94faa 335 kmem_cache_free(gfs2_glock_cachep, gl);
b3b94faa
DT
336 return error;
337}
338
339/**
340 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
341 * @gl: the glock
342 * @state: the state we're requesting
343 * @flags: the modifier flags
344 * @gh: the holder structure
345 *
346 */
347
190562bd 348void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
b3b94faa
DT
349 struct gfs2_holder *gh)
350{
351 INIT_LIST_HEAD(&gh->gh_list);
352 gh->gh_gl = gl;
d0dc80db 353 gh->gh_ip = (unsigned long)__builtin_return_address(0);
190562bd 354 gh->gh_owner = current;
b3b94faa
DT
355 gh->gh_state = state;
356 gh->gh_flags = flags;
357 gh->gh_error = 0;
358 gh->gh_iflags = 0;
359 init_completion(&gh->gh_wait);
360
361 if (gh->gh_state == LM_ST_EXCLUSIVE)
362 gh->gh_flags |= GL_LOCAL_EXCL;
363
364 gfs2_glock_hold(gl);
365}
366
367/**
368 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
369 * @state: the state we're requesting
370 * @flags: the modifier flags
371 * @gh: the holder structure
372 *
373 * Don't mess with the glock.
374 *
375 */
376
190562bd 377void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
b3b94faa
DT
378{
379 gh->gh_state = state;
579b78a4 380 gh->gh_flags = flags;
b3b94faa
DT
381 if (gh->gh_state == LM_ST_EXCLUSIVE)
382 gh->gh_flags |= GL_LOCAL_EXCL;
383
384 gh->gh_iflags &= 1 << HIF_ALLOCED;
d0dc80db 385 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b3b94faa
DT
386}
387
388/**
389 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
390 * @gh: the holder structure
391 *
392 */
393
394void gfs2_holder_uninit(struct gfs2_holder *gh)
395{
396 gfs2_glock_put(gh->gh_gl);
397 gh->gh_gl = NULL;
d0dc80db 398 gh->gh_ip = 0;
b3b94faa
DT
399}
400
401/**
402 * gfs2_holder_get - get a struct gfs2_holder structure
403 * @gl: the glock
404 * @state: the state we're requesting
405 * @flags: the modifier flags
af18ddb8 406 * @gfp_flags:
b3b94faa
DT
407 *
408 * Figure out how big an impact this function has. Either:
409 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
410 * 2) Leave it like it is
411 *
412 * Returns: the holder structure, NULL on ENOMEM
413 */
414
08bc2dbc
AB
415static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
416 unsigned int state,
417 int flags, gfp_t gfp_flags)
b3b94faa
DT
418{
419 struct gfs2_holder *gh;
420
421 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
422 if (!gh)
423 return NULL;
424
425 gfs2_holder_init(gl, state, flags, gh);
426 set_bit(HIF_ALLOCED, &gh->gh_iflags);
d0dc80db 427 gh->gh_ip = (unsigned long)__builtin_return_address(0);
b3b94faa
DT
428 return gh;
429}
430
431/**
432 * gfs2_holder_put - get rid of a struct gfs2_holder structure
433 * @gh: the holder structure
434 *
435 */
436
08bc2dbc 437static void gfs2_holder_put(struct gfs2_holder *gh)
b3b94faa
DT
438{
439 gfs2_holder_uninit(gh);
440 kfree(gh);
441}
442
b3b94faa
DT
443/**
444 * rq_mutex - process a mutex request in the queue
445 * @gh: the glock holder
446 *
447 * Returns: 1 if the queue is blocked
448 */
449
450static int rq_mutex(struct gfs2_holder *gh)
451{
452 struct gfs2_glock *gl = gh->gh_gl;
453
454 list_del_init(&gh->gh_list);
455 /* gh->gh_error never examined. */
456 set_bit(GLF_LOCK, &gl->gl_flags);
457 complete(&gh->gh_wait);
458
459 return 1;
460}
461
462/**
463 * rq_promote - process a promote request in the queue
464 * @gh: the glock holder
465 *
466 * Acquire a new inter-node lock, or change a lock state to more restrictive.
467 *
468 * Returns: 1 if the queue is blocked
469 */
470
471static int rq_promote(struct gfs2_holder *gh)
472{
473 struct gfs2_glock *gl = gh->gh_gl;
474 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 475 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
476
477 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
478 if (list_empty(&gl->gl_holders)) {
479 gl->gl_req_gh = gh;
480 set_bit(GLF_LOCK, &gl->gl_flags);
481 spin_unlock(&gl->gl_spin);
482
483 if (atomic_read(&sdp->sd_reclaim_count) >
484 gfs2_tune_get(sdp, gt_reclaim_limit) &&
485 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
486 gfs2_reclaim_glock(sdp);
487 gfs2_reclaim_glock(sdp);
488 }
489
ec45d9f5 490 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
b3b94faa
DT
491 spin_lock(&gl->gl_spin);
492 }
493 return 1;
494 }
495
496 if (list_empty(&gl->gl_holders)) {
497 set_bit(HIF_FIRST, &gh->gh_iflags);
498 set_bit(GLF_LOCK, &gl->gl_flags);
b3b94faa
DT
499 } else {
500 struct gfs2_holder *next_gh;
501 if (gh->gh_flags & GL_LOCAL_EXCL)
502 return 1;
503 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
504 gh_list);
505 if (next_gh->gh_flags & GL_LOCAL_EXCL)
506 return 1;
b3b94faa
DT
507 }
508
509 list_move_tail(&gh->gh_list, &gl->gl_holders);
510 gh->gh_error = 0;
511 set_bit(HIF_HOLDER, &gh->gh_iflags);
512
b3b94faa
DT
513 complete(&gh->gh_wait);
514
515 return 0;
516}
517
518/**
519 * rq_demote - process a demote request in the queue
520 * @gh: the glock holder
521 *
522 * Returns: 1 if the queue is blocked
523 */
524
525static int rq_demote(struct gfs2_holder *gh)
526{
527 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 528 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
529
530 if (!list_empty(&gl->gl_holders))
531 return 1;
532
533 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
534 list_del_init(&gh->gh_list);
535 gh->gh_error = 0;
536 spin_unlock(&gl->gl_spin);
537 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
538 gfs2_holder_put(gh);
539 else
540 complete(&gh->gh_wait);
541 spin_lock(&gl->gl_spin);
542 } else {
543 gl->gl_req_gh = gh;
544 set_bit(GLF_LOCK, &gl->gl_flags);
545 spin_unlock(&gl->gl_spin);
546
547 if (gh->gh_state == LM_ST_UNLOCKED ||
548 gl->gl_state != LM_ST_EXCLUSIVE)
549 glops->go_drop_th(gl);
550 else
551 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
552
553 spin_lock(&gl->gl_spin);
554 }
555
556 return 0;
557}
558
559/**
560 * rq_greedy - process a queued request to drop greedy status
561 * @gh: the glock holder
562 *
563 * Returns: 1 if the queue is blocked
564 */
565
566static int rq_greedy(struct gfs2_holder *gh)
567{
568 struct gfs2_glock *gl = gh->gh_gl;
569
570 list_del_init(&gh->gh_list);
571 /* gh->gh_error never examined. */
572 clear_bit(GLF_GREEDY, &gl->gl_flags);
573 spin_unlock(&gl->gl_spin);
574
575 gfs2_holder_uninit(gh);
576 kfree(container_of(gh, struct greedy, gr_gh));
577
578 spin_lock(&gl->gl_spin);
579
580 return 0;
581}
582
583/**
584 * run_queue - process holder structures on a glock
585 * @gl: the glock
586 *
587 */
b3b94faa
DT
588static void run_queue(struct gfs2_glock *gl)
589{
590 struct gfs2_holder *gh;
591 int blocked = 1;
592
593 for (;;) {
594 if (test_bit(GLF_LOCK, &gl->gl_flags))
595 break;
596
597 if (!list_empty(&gl->gl_waiters1)) {
598 gh = list_entry(gl->gl_waiters1.next,
599 struct gfs2_holder, gh_list);
600
601 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
602 blocked = rq_mutex(gh);
603 else
604 gfs2_assert_warn(gl->gl_sbd, 0);
605
606 } else if (!list_empty(&gl->gl_waiters2) &&
607 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
608 gh = list_entry(gl->gl_waiters2.next,
609 struct gfs2_holder, gh_list);
610
611 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
612 blocked = rq_demote(gh);
613 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
614 blocked = rq_greedy(gh);
615 else
616 gfs2_assert_warn(gl->gl_sbd, 0);
617
618 } else if (!list_empty(&gl->gl_waiters3)) {
619 gh = list_entry(gl->gl_waiters3.next,
620 struct gfs2_holder, gh_list);
621
622 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
623 blocked = rq_promote(gh);
624 else
625 gfs2_assert_warn(gl->gl_sbd, 0);
626
627 } else
628 break;
629
630 if (blocked)
631 break;
632 }
633}
634
635/**
636 * gfs2_glmutex_lock - acquire a local lock on a glock
637 * @gl: the glock
638 *
639 * Gives caller exclusive access to manipulate a glock structure.
640 */
641
feaa7bba 642static void gfs2_glmutex_lock(struct gfs2_glock *gl)
b3b94faa
DT
643{
644 struct gfs2_holder gh;
645
646 gfs2_holder_init(gl, 0, 0, &gh);
647 set_bit(HIF_MUTEX, &gh.gh_iflags);
648
649 spin_lock(&gl->gl_spin);
650 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
651 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
320dd101
SW
652 else {
653 gl->gl_owner = current;
654 gl->gl_ip = (unsigned long)__builtin_return_address(0);
b3b94faa 655 complete(&gh.gh_wait);
320dd101 656 }
b3b94faa
DT
657 spin_unlock(&gl->gl_spin);
658
659 wait_for_completion(&gh.gh_wait);
660 gfs2_holder_uninit(&gh);
661}
662
663/**
664 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
665 * @gl: the glock
666 *
667 * Returns: 1 if the glock is acquired
668 */
669
08bc2dbc 670static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
b3b94faa
DT
671{
672 int acquired = 1;
673
674 spin_lock(&gl->gl_spin);
675 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
676 acquired = 0;
320dd101
SW
677 else {
678 gl->gl_owner = current;
679 gl->gl_ip = (unsigned long)__builtin_return_address(0);
680 }
b3b94faa
DT
681 spin_unlock(&gl->gl_spin);
682
683 return acquired;
684}
685
686/**
687 * gfs2_glmutex_unlock - release a local lock on a glock
688 * @gl: the glock
689 *
690 */
691
feaa7bba 692static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
b3b94faa
DT
693{
694 spin_lock(&gl->gl_spin);
695 clear_bit(GLF_LOCK, &gl->gl_flags);
320dd101
SW
696 gl->gl_owner = NULL;
697 gl->gl_ip = 0;
b3b94faa 698 run_queue(gl);
190562bd 699 BUG_ON(!spin_is_locked(&gl->gl_spin));
b3b94faa
DT
700 spin_unlock(&gl->gl_spin);
701}
702
703/**
704 * handle_callback - add a demote request to a lock's queue
705 * @gl: the glock
706 * @state: the state the caller wants us to change to
707 *
af18ddb8 708 * Note: This may fail sliently if we are out of memory.
b3b94faa
DT
709 */
710
711static void handle_callback(struct gfs2_glock *gl, unsigned int state)
712{
713 struct gfs2_holder *gh, *new_gh = NULL;
714
feaa7bba 715restart:
b3b94faa
DT
716 spin_lock(&gl->gl_spin);
717
718 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
719 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
720 gl->gl_req_gh != gh) {
721 if (gh->gh_state != state)
722 gh->gh_state = LM_ST_UNLOCKED;
723 goto out;
724 }
725 }
726
727 if (new_gh) {
728 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
729 new_gh = NULL;
730 } else {
731 spin_unlock(&gl->gl_spin);
732
af18ddb8
SW
733 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
734 if (!new_gh)
735 return;
b3b94faa
DT
736 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
737 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
738
739 goto restart;
740 }
741
feaa7bba 742out:
b3b94faa
DT
743 spin_unlock(&gl->gl_spin);
744
745 if (new_gh)
746 gfs2_holder_put(new_gh);
747}
748
feaa7bba
SW
749void gfs2_glock_inode_squish(struct inode *inode)
750{
751 struct gfs2_holder gh;
752 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
753 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
754 set_bit(HIF_DEMOTE, &gh.gh_iflags);
755 spin_lock(&gl->gl_spin);
756 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
757 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
758 run_queue(gl);
759 spin_unlock(&gl->gl_spin);
5dd9feaf 760 wait_for_completion(&gh.gh_wait);
feaa7bba
SW
761 gfs2_holder_uninit(&gh);
762}
763
b3b94faa
DT
764/**
765 * state_change - record that the glock is now in a different state
766 * @gl: the glock
767 * @new_state the new state
768 *
769 */
770
771static void state_change(struct gfs2_glock *gl, unsigned int new_state)
772{
b3b94faa
DT
773 int held1, held2;
774
775 held1 = (gl->gl_state != LM_ST_UNLOCKED);
776 held2 = (new_state != LM_ST_UNLOCKED);
777
778 if (held1 != held2) {
6a6b3d01 779 if (held2)
b3b94faa 780 gfs2_glock_hold(gl);
6a6b3d01 781 else
b3b94faa 782 gfs2_glock_put(gl);
b3b94faa
DT
783 }
784
785 gl->gl_state = new_state;
786}
787
788/**
789 * xmote_bh - Called after the lock module is done acquiring a lock
790 * @gl: The glock in question
791 * @ret: the int returned from the lock module
792 *
793 */
794
795static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
796{
797 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 798 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
799 struct gfs2_holder *gh = gl->gl_req_gh;
800 int prev_state = gl->gl_state;
801 int op_done = 1;
802
803 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
804 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
805 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
806
807 state_change(gl, ret & LM_OUT_ST_MASK);
808
809 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
810 if (glops->go_inval)
811 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
812 } else if (gl->gl_state == LM_ST_DEFERRED) {
813 /* We might not want to do this here.
814 Look at moving to the inode glops. */
815 if (glops->go_inval)
816 glops->go_inval(gl, DIO_DATA);
817 }
818
819 /* Deal with each possible exit condition */
820
821 if (!gh)
822 gl->gl_stamp = jiffies;
823
824 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
825 spin_lock(&gl->gl_spin);
826 list_del_init(&gh->gh_list);
827 gh->gh_error = -EIO;
b3b94faa
DT
828 spin_unlock(&gl->gl_spin);
829
830 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
831 spin_lock(&gl->gl_spin);
832 list_del_init(&gh->gh_list);
833 if (gl->gl_state == gh->gh_state ||
834 gl->gl_state == LM_ST_UNLOCKED)
835 gh->gh_error = 0;
836 else {
837 if (gfs2_assert_warn(sdp, gh->gh_flags &
838 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
839 fs_warn(sdp, "ret = 0x%.8X\n", ret);
840 gh->gh_error = GLR_TRYFAILED;
841 }
842 spin_unlock(&gl->gl_spin);
843
844 if (ret & LM_OUT_CANCELED)
845 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
846
847 } else if (ret & LM_OUT_CANCELED) {
848 spin_lock(&gl->gl_spin);
849 list_del_init(&gh->gh_list);
850 gh->gh_error = GLR_CANCELED;
b3b94faa
DT
851 spin_unlock(&gl->gl_spin);
852
853 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
854 spin_lock(&gl->gl_spin);
855 list_move_tail(&gh->gh_list, &gl->gl_holders);
856 gh->gh_error = 0;
857 set_bit(HIF_HOLDER, &gh->gh_iflags);
858 spin_unlock(&gl->gl_spin);
859
860 set_bit(HIF_FIRST, &gh->gh_iflags);
861
862 op_done = 0;
863
864 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
865 spin_lock(&gl->gl_spin);
866 list_del_init(&gh->gh_list);
867 gh->gh_error = GLR_TRYFAILED;
b3b94faa
DT
868 spin_unlock(&gl->gl_spin);
869
870 } else {
871 if (gfs2_assert_withdraw(sdp, 0) == -1)
872 fs_err(sdp, "ret = 0x%.8X\n", ret);
873 }
874
875 if (glops->go_xmote_bh)
876 glops->go_xmote_bh(gl);
877
878 if (op_done) {
879 spin_lock(&gl->gl_spin);
880 gl->gl_req_gh = NULL;
881 gl->gl_req_bh = NULL;
882 clear_bit(GLF_LOCK, &gl->gl_flags);
883 run_queue(gl);
884 spin_unlock(&gl->gl_spin);
885 }
886
887 gfs2_glock_put(gl);
888
889 if (gh) {
890 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
891 gfs2_holder_put(gh);
892 else
893 complete(&gh->gh_wait);
894 }
895}
896
897/**
898 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
899 * @gl: The glock in question
900 * @state: the requested state
901 * @flags: modifier flags to the lock call
902 *
903 */
904
905void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
906{
907 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 908 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
909 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
910 LM_FLAG_NOEXP | LM_FLAG_ANY |
911 LM_FLAG_PRIORITY);
912 unsigned int lck_ret;
913
914 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
915 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
916 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
917 gfs2_assert_warn(sdp, state != gl->gl_state);
918
919 if (gl->gl_state == LM_ST_EXCLUSIVE) {
920 if (glops->go_sync)
921 glops->go_sync(gl,
922 DIO_METADATA | DIO_DATA | DIO_RELEASE);
923 }
924
925 gfs2_glock_hold(gl);
926 gl->gl_req_bh = xmote_bh;
927
ec45d9f5 928 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
b3b94faa
DT
929
930 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
931 return;
932
933 if (lck_ret & LM_OUT_ASYNC)
934 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
935 else
936 xmote_bh(gl, lck_ret);
937}
938
939/**
940 * drop_bh - Called after a lock module unlock completes
941 * @gl: the glock
942 * @ret: the return status
943 *
944 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
945 * Doesn't drop the reference on the glock the top half took out
946 *
947 */
948
949static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
950{
951 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 952 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
953 struct gfs2_holder *gh = gl->gl_req_gh;
954
955 clear_bit(GLF_PREFETCH, &gl->gl_flags);
956
957 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
958 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
959 gfs2_assert_warn(sdp, !ret);
960
961 state_change(gl, LM_ST_UNLOCKED);
962
963 if (glops->go_inval)
964 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
965
966 if (gh) {
967 spin_lock(&gl->gl_spin);
968 list_del_init(&gh->gh_list);
969 gh->gh_error = 0;
970 spin_unlock(&gl->gl_spin);
971 }
972
973 if (glops->go_drop_bh)
974 glops->go_drop_bh(gl);
975
976 spin_lock(&gl->gl_spin);
977 gl->gl_req_gh = NULL;
978 gl->gl_req_bh = NULL;
979 clear_bit(GLF_LOCK, &gl->gl_flags);
980 run_queue(gl);
981 spin_unlock(&gl->gl_spin);
982
983 gfs2_glock_put(gl);
984
985 if (gh) {
986 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
987 gfs2_holder_put(gh);
988 else
989 complete(&gh->gh_wait);
990 }
991}
992
993/**
994 * gfs2_glock_drop_th - call into the lock module to unlock a lock
995 * @gl: the glock
996 *
997 */
998
999void gfs2_glock_drop_th(struct gfs2_glock *gl)
1000{
1001 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 1002 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1003 unsigned int ret;
1004
1005 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1006 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1007 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1008
1009 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1010 if (glops->go_sync)
ec45d9f5 1011 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
b3b94faa
DT
1012 }
1013
1014 gfs2_glock_hold(gl);
1015 gl->gl_req_bh = drop_bh;
1016
b3b94faa
DT
1017 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1018
1019 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1020 return;
1021
1022 if (!ret)
1023 drop_bh(gl, ret);
1024 else
1025 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1026}
1027
1028/**
1029 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1030 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1031 *
1032 * Don't cancel GL_NOCANCEL requests.
1033 */
1034
1035static void do_cancels(struct gfs2_holder *gh)
1036{
1037 struct gfs2_glock *gl = gh->gh_gl;
1038
1039 spin_lock(&gl->gl_spin);
1040
1041 while (gl->gl_req_gh != gh &&
1042 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1043 !list_empty(&gh->gh_list)) {
1044 if (gl->gl_req_bh &&
1045 !(gl->gl_req_gh &&
1046 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1047 spin_unlock(&gl->gl_spin);
1048 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1049 msleep(100);
1050 spin_lock(&gl->gl_spin);
1051 } else {
1052 spin_unlock(&gl->gl_spin);
1053 msleep(100);
1054 spin_lock(&gl->gl_spin);
1055 }
1056 }
1057
1058 spin_unlock(&gl->gl_spin);
1059}
1060
1061/**
1062 * glock_wait_internal - wait on a glock acquisition
1063 * @gh: the glock holder
1064 *
1065 * Returns: 0 on success
1066 */
1067
1068static int glock_wait_internal(struct gfs2_holder *gh)
1069{
1070 struct gfs2_glock *gl = gh->gh_gl;
1071 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 1072 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1073
1074 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1075 return -EIO;
1076
1077 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1078 spin_lock(&gl->gl_spin);
1079 if (gl->gl_req_gh != gh &&
1080 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1081 !list_empty(&gh->gh_list)) {
1082 list_del_init(&gh->gh_list);
1083 gh->gh_error = GLR_TRYFAILED;
b3b94faa
DT
1084 run_queue(gl);
1085 spin_unlock(&gl->gl_spin);
1086 return gh->gh_error;
1087 }
1088 spin_unlock(&gl->gl_spin);
1089 }
1090
1091 if (gh->gh_flags & LM_FLAG_PRIORITY)
1092 do_cancels(gh);
1093
1094 wait_for_completion(&gh->gh_wait);
1095
1096 if (gh->gh_error)
1097 return gh->gh_error;
1098
1099 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1100 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1101 gh->gh_state,
1102 gh->gh_flags));
1103
1104 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1105 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1106
1107 if (glops->go_lock) {
1108 gh->gh_error = glops->go_lock(gh);
1109 if (gh->gh_error) {
1110 spin_lock(&gl->gl_spin);
1111 list_del_init(&gh->gh_list);
b3b94faa
DT
1112 spin_unlock(&gl->gl_spin);
1113 }
1114 }
1115
1116 spin_lock(&gl->gl_spin);
1117 gl->gl_req_gh = NULL;
1118 gl->gl_req_bh = NULL;
1119 clear_bit(GLF_LOCK, &gl->gl_flags);
b3b94faa
DT
1120 run_queue(gl);
1121 spin_unlock(&gl->gl_spin);
1122 }
1123
1124 return gh->gh_error;
1125}
1126
1127static inline struct gfs2_holder *
1128find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1129{
1130 struct gfs2_holder *gh;
1131
1132 list_for_each_entry(gh, head, gh_list) {
1133 if (gh->gh_owner == owner)
1134 return gh;
1135 }
1136
1137 return NULL;
1138}
1139
b3b94faa
DT
1140/**
1141 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1142 * @gh: the holder structure to add
1143 *
1144 */
1145
1146static void add_to_queue(struct gfs2_holder *gh)
1147{
1148 struct gfs2_glock *gl = gh->gh_gl;
1149 struct gfs2_holder *existing;
1150
190562bd
SW
1151 BUG_ON(!gh->gh_owner);
1152
b3b94faa
DT
1153 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1154 if (existing) {
5965b1f4 1155 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
86384605
AD
1156 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1157 printk(KERN_INFO "lock type : %d lock state : %d\n",
1158 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
5965b1f4 1159 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
86384605
AD
1160 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1161 printk(KERN_INFO "lock type : %d lock state : %d\n",
1162 gl->gl_name.ln_type, gl->gl_state);
5965b1f4 1163 BUG();
b3b94faa
DT
1164 }
1165
1166 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1167 if (existing) {
5965b1f4
SW
1168 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1169 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1170 BUG();
b3b94faa
DT
1171 }
1172
b3b94faa
DT
1173 if (gh->gh_flags & LM_FLAG_PRIORITY)
1174 list_add(&gh->gh_list, &gl->gl_waiters3);
1175 else
1176 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1177}
1178
1179/**
1180 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1181 * @gh: the holder structure
1182 *
1183 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1184 *
1185 * Returns: 0, GLR_TRYFAILED, or errno on failure
1186 */
1187
1188int gfs2_glock_nq(struct gfs2_holder *gh)
1189{
1190 struct gfs2_glock *gl = gh->gh_gl;
1191 struct gfs2_sbd *sdp = gl->gl_sbd;
1192 int error = 0;
1193
320dd101 1194restart:
b3b94faa
DT
1195 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1196 set_bit(HIF_ABORTED, &gh->gh_iflags);
1197 return -EIO;
1198 }
1199
1200 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1201
1202 spin_lock(&gl->gl_spin);
1203 add_to_queue(gh);
1204 run_queue(gl);
1205 spin_unlock(&gl->gl_spin);
1206
1207 if (!(gh->gh_flags & GL_ASYNC)) {
1208 error = glock_wait_internal(gh);
1209 if (error == GLR_CANCELED) {
190562bd 1210 msleep(100);
b3b94faa
DT
1211 goto restart;
1212 }
1213 }
1214
1215 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1216
320dd101
SW
1217 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1218 dump_glock(gl);
1219
b3b94faa
DT
1220 return error;
1221}
1222
1223/**
1224 * gfs2_glock_poll - poll to see if an async request has been completed
1225 * @gh: the holder
1226 *
1227 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1228 */
1229
1230int gfs2_glock_poll(struct gfs2_holder *gh)
1231{
1232 struct gfs2_glock *gl = gh->gh_gl;
1233 int ready = 0;
1234
1235 spin_lock(&gl->gl_spin);
1236
1237 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1238 ready = 1;
1239 else if (list_empty(&gh->gh_list)) {
1240 if (gh->gh_error == GLR_CANCELED) {
1241 spin_unlock(&gl->gl_spin);
190562bd 1242 msleep(100);
b3b94faa
DT
1243 if (gfs2_glock_nq(gh))
1244 return 1;
1245 return 0;
1246 } else
1247 ready = 1;
1248 }
1249
1250 spin_unlock(&gl->gl_spin);
1251
1252 return ready;
1253}
1254
1255/**
1256 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1257 * @gh: the holder structure
1258 *
1259 * Returns: 0, GLR_TRYFAILED, or errno on failure
1260 */
1261
1262int gfs2_glock_wait(struct gfs2_holder *gh)
1263{
1264 int error;
1265
1266 error = glock_wait_internal(gh);
1267 if (error == GLR_CANCELED) {
190562bd 1268 msleep(100);
b3b94faa
DT
1269 gh->gh_flags &= ~GL_ASYNC;
1270 error = gfs2_glock_nq(gh);
1271 }
1272
1273 return error;
1274}
1275
1276/**
1277 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1278 * @gh: the glock holder
1279 *
1280 */
1281
1282void gfs2_glock_dq(struct gfs2_holder *gh)
1283{
1284 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1285 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa 1286
b3b94faa
DT
1287 if (gh->gh_flags & GL_NOCACHE)
1288 handle_callback(gl, LM_ST_UNLOCKED);
1289
1290 gfs2_glmutex_lock(gl);
1291
1292 spin_lock(&gl->gl_spin);
1293 list_del_init(&gh->gh_list);
1294
1295 if (list_empty(&gl->gl_holders)) {
1296 spin_unlock(&gl->gl_spin);
1297
1298 if (glops->go_unlock)
1299 glops->go_unlock(gh);
1300
b3b94faa
DT
1301 gl->gl_stamp = jiffies;
1302
1303 spin_lock(&gl->gl_spin);
1304 }
1305
1306 clear_bit(GLF_LOCK, &gl->gl_flags);
1307 run_queue(gl);
1308 spin_unlock(&gl->gl_spin);
1309}
1310
1311/**
1312 * gfs2_glock_prefetch - Try to prefetch a glock
1313 * @gl: the glock
1314 * @state: the state to prefetch in
1315 * @flags: flags passed to go_xmote_th()
1316 *
1317 */
1318
08bc2dbc
AB
1319static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1320 int flags)
b3b94faa 1321{
8fb4b536 1322 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1323
1324 spin_lock(&gl->gl_spin);
1325
1326 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1327 !list_empty(&gl->gl_holders) ||
1328 !list_empty(&gl->gl_waiters1) ||
1329 !list_empty(&gl->gl_waiters2) ||
1330 !list_empty(&gl->gl_waiters3) ||
1331 relaxed_state_ok(gl->gl_state, state, flags)) {
1332 spin_unlock(&gl->gl_spin);
1333 return;
1334 }
1335
1336 set_bit(GLF_PREFETCH, &gl->gl_flags);
1337 set_bit(GLF_LOCK, &gl->gl_flags);
1338 spin_unlock(&gl->gl_spin);
1339
1340 glops->go_xmote_th(gl, state, flags);
b3b94faa
DT
1341}
1342
b3b94faa
DT
1343static void greedy_work(void *data)
1344{
e7f5c01c 1345 struct greedy *gr = data;
b3b94faa
DT
1346 struct gfs2_holder *gh = &gr->gr_gh;
1347 struct gfs2_glock *gl = gh->gh_gl;
8fb4b536 1348 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1349
1350 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1351
1352 if (glops->go_greedy)
1353 glops->go_greedy(gl);
1354
1355 spin_lock(&gl->gl_spin);
1356
1357 if (list_empty(&gl->gl_waiters2)) {
1358 clear_bit(GLF_GREEDY, &gl->gl_flags);
1359 spin_unlock(&gl->gl_spin);
1360 gfs2_holder_uninit(gh);
1361 kfree(gr);
1362 } else {
1363 gfs2_glock_hold(gl);
1364 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1365 run_queue(gl);
1366 spin_unlock(&gl->gl_spin);
1367 gfs2_glock_put(gl);
1368 }
1369}
1370
1371/**
1372 * gfs2_glock_be_greedy -
1373 * @gl:
1374 * @time:
1375 *
1376 * Returns: 0 if go_greedy will be called, 1 otherwise
1377 */
1378
1379int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1380{
1381 struct greedy *gr;
1382 struct gfs2_holder *gh;
1383
feaa7bba 1384 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
b3b94faa
DT
1385 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1386 return 1;
1387
1388 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1389 if (!gr) {
1390 clear_bit(GLF_GREEDY, &gl->gl_flags);
1391 return 1;
1392 }
1393 gh = &gr->gr_gh;
1394
579b78a4 1395 gfs2_holder_init(gl, 0, 0, gh);
b3b94faa
DT
1396 set_bit(HIF_GREEDY, &gh->gh_iflags);
1397 INIT_WORK(&gr->gr_work, greedy_work, gr);
1398
1399 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1400 schedule_delayed_work(&gr->gr_work, time);
1401
1402 return 0;
1403}
1404
b3b94faa
DT
1405/**
1406 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1407 * @gh: the holder structure
1408 *
1409 */
1410
1411void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1412{
1413 gfs2_glock_dq(gh);
1414 gfs2_holder_uninit(gh);
1415}
1416
1417/**
1418 * gfs2_glock_nq_num - acquire a glock based on lock number
1419 * @sdp: the filesystem
1420 * @number: the lock number
1421 * @glops: the glock operations for the type of glock
1422 * @state: the state to acquire the glock in
1423 * @flags: modifier flags for the aquisition
1424 * @gh: the struct gfs2_holder
1425 *
1426 * Returns: errno
1427 */
1428
1429int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
8fb4b536
SW
1430 const struct gfs2_glock_operations *glops,
1431 unsigned int state, int flags, struct gfs2_holder *gh)
b3b94faa
DT
1432{
1433 struct gfs2_glock *gl;
1434 int error;
1435
1436 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1437 if (!error) {
1438 error = gfs2_glock_nq_init(gl, state, flags, gh);
1439 gfs2_glock_put(gl);
1440 }
1441
1442 return error;
1443}
1444
1445/**
1446 * glock_compare - Compare two struct gfs2_glock structures for sorting
1447 * @arg_a: the first structure
1448 * @arg_b: the second structure
1449 *
1450 */
1451
1452static int glock_compare(const void *arg_a, const void *arg_b)
1453{
1454 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1455 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1456 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1457 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1458 int ret = 0;
1459
1460 if (a->ln_number > b->ln_number)
1461 ret = 1;
1462 else if (a->ln_number < b->ln_number)
1463 ret = -1;
1464 else {
1465 if (gh_a->gh_state == LM_ST_SHARED &&
1466 gh_b->gh_state == LM_ST_EXCLUSIVE)
1467 ret = 1;
1468 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1469 (gh_b->gh_flags & GL_LOCAL_EXCL))
1470 ret = 1;
1471 }
1472
1473 return ret;
1474}
1475
1476/**
1477 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1478 * @num_gh: the number of structures
1479 * @ghs: an array of struct gfs2_holder structures
1480 *
1481 * Returns: 0 on success (all glocks acquired),
1482 * errno on failure (no glocks acquired)
1483 */
1484
1485static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1486 struct gfs2_holder **p)
1487{
1488 unsigned int x;
1489 int error = 0;
1490
1491 for (x = 0; x < num_gh; x++)
1492 p[x] = &ghs[x];
1493
1494 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1495
1496 for (x = 0; x < num_gh; x++) {
1497 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1498
1499 error = gfs2_glock_nq(p[x]);
1500 if (error) {
1501 while (x--)
1502 gfs2_glock_dq(p[x]);
1503 break;
1504 }
1505 }
1506
1507 return error;
1508}
1509
1510/**
1511 * gfs2_glock_nq_m - acquire multiple glocks
1512 * @num_gh: the number of structures
1513 * @ghs: an array of struct gfs2_holder structures
1514 *
1515 * Figure out how big an impact this function has. Either:
1516 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1517 * 2) Forget async stuff and just call nq_m_sync()
1518 * 3) Leave it like it is
1519 *
1520 * Returns: 0 on success (all glocks acquired),
1521 * errno on failure (no glocks acquired)
1522 */
1523
1524int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1525{
1526 int *e;
1527 unsigned int x;
1528 int borked = 0, serious = 0;
1529 int error = 0;
1530
1531 if (!num_gh)
1532 return 0;
1533
1534 if (num_gh == 1) {
1535 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1536 return gfs2_glock_nq(ghs);
1537 }
1538
1539 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1540 if (!e)
1541 return -ENOMEM;
1542
1543 for (x = 0; x < num_gh; x++) {
1544 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1545 error = gfs2_glock_nq(&ghs[x]);
1546 if (error) {
1547 borked = 1;
1548 serious = error;
1549 num_gh = x;
1550 break;
1551 }
1552 }
1553
1554 for (x = 0; x < num_gh; x++) {
1555 error = e[x] = glock_wait_internal(&ghs[x]);
1556 if (error) {
1557 borked = 1;
1558 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1559 serious = error;
1560 }
1561 }
1562
1563 if (!borked) {
1564 kfree(e);
1565 return 0;
1566 }
1567
1568 for (x = 0; x < num_gh; x++)
1569 if (!e[x])
1570 gfs2_glock_dq(&ghs[x]);
1571
1572 if (serious)
1573 error = serious;
1574 else {
1575 for (x = 0; x < num_gh; x++)
1576 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1577 &ghs[x]);
1578 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1579 }
1580
1581 kfree(e);
1582
1583 return error;
1584}
1585
1586/**
1587 * gfs2_glock_dq_m - release multiple glocks
1588 * @num_gh: the number of structures
1589 * @ghs: an array of struct gfs2_holder structures
1590 *
1591 */
1592
1593void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1594{
1595 unsigned int x;
1596
1597 for (x = 0; x < num_gh; x++)
1598 gfs2_glock_dq(&ghs[x]);
1599}
1600
1601/**
1602 * gfs2_glock_dq_uninit_m - release multiple glocks
1603 * @num_gh: the number of structures
1604 * @ghs: an array of struct gfs2_holder structures
1605 *
1606 */
1607
1608void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1609{
1610 unsigned int x;
1611
1612 for (x = 0; x < num_gh; x++)
1613 gfs2_glock_dq_uninit(&ghs[x]);
1614}
1615
1616/**
1617 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1618 * @sdp: the filesystem
1619 * @number: the lock number
1620 * @glops: the glock operations for the type of glock
1621 * @state: the state to acquire the glock in
1622 * @flags: modifier flags for the aquisition
1623 *
1624 * Returns: errno
1625 */
1626
1627void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
8fb4b536 1628 const struct gfs2_glock_operations *glops,
b3b94faa
DT
1629 unsigned int state, int flags)
1630{
1631 struct gfs2_glock *gl;
1632 int error;
1633
1634 if (atomic_read(&sdp->sd_reclaim_count) <
1635 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1636 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1637 if (!error) {
1638 gfs2_glock_prefetch(gl, state, flags);
1639 gfs2_glock_put(gl);
1640 }
1641 }
1642}
1643
1644/**
1645 * gfs2_lvb_hold - attach a LVB from a glock
1646 * @gl: The glock in question
1647 *
1648 */
1649
1650int gfs2_lvb_hold(struct gfs2_glock *gl)
1651{
1652 int error;
1653
1654 gfs2_glmutex_lock(gl);
1655
1656 if (!atomic_read(&gl->gl_lvb_count)) {
1657 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1658 if (error) {
1659 gfs2_glmutex_unlock(gl);
1660 return error;
1661 }
1662 gfs2_glock_hold(gl);
1663 }
1664 atomic_inc(&gl->gl_lvb_count);
1665
1666 gfs2_glmutex_unlock(gl);
1667
1668 return 0;
1669}
1670
1671/**
1672 * gfs2_lvb_unhold - detach a LVB from a glock
1673 * @gl: The glock in question
1674 *
1675 */
1676
1677void gfs2_lvb_unhold(struct gfs2_glock *gl)
1678{
1679 gfs2_glock_hold(gl);
1680 gfs2_glmutex_lock(gl);
1681
1682 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1683 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1684 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1685 gl->gl_lvb = NULL;
1686 gfs2_glock_put(gl);
1687 }
1688
1689 gfs2_glmutex_unlock(gl);
1690 gfs2_glock_put(gl);
1691}
1692
08bc2dbc 1693#if 0
b3b94faa
DT
1694void gfs2_lvb_sync(struct gfs2_glock *gl)
1695{
1696 gfs2_glmutex_lock(gl);
1697
1698 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1699 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1700 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1701
1702 gfs2_glmutex_unlock(gl);
1703}
08bc2dbc 1704#endif /* 0 */
b3b94faa
DT
1705
1706static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1707 unsigned int state)
1708{
1709 struct gfs2_glock *gl;
1710
1711 gl = gfs2_glock_find(sdp, name);
1712 if (!gl)
1713 return;
1714
1715 if (gl->gl_ops->go_callback)
1716 gl->gl_ops->go_callback(gl, state);
1717 handle_callback(gl, state);
1718
1719 spin_lock(&gl->gl_spin);
1720 run_queue(gl);
1721 spin_unlock(&gl->gl_spin);
1722
1723 gfs2_glock_put(gl);
1724}
1725
1726/**
1727 * gfs2_glock_cb - Callback used by locking module
1728 * @fsdata: Pointer to the superblock
1729 * @type: Type of callback
1730 * @data: Type dependent data pointer
1731 *
1732 * Called by the locking module when it wants to tell us something.
1733 * Either we need to drop a lock, one of our ASYNC requests completed, or
1734 * a journal from another client needs to be recovered.
1735 */
1736
1737void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1738{
1739 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1740
b3b94faa
DT
1741 switch (type) {
1742 case LM_CB_NEED_E:
e7f5c01c 1743 blocking_cb(sdp, data, LM_ST_UNLOCKED);
b3b94faa
DT
1744 return;
1745
1746 case LM_CB_NEED_D:
e7f5c01c 1747 blocking_cb(sdp, data, LM_ST_DEFERRED);
b3b94faa
DT
1748 return;
1749
1750 case LM_CB_NEED_S:
e7f5c01c 1751 blocking_cb(sdp, data, LM_ST_SHARED);
b3b94faa
DT
1752 return;
1753
1754 case LM_CB_ASYNC: {
e7f5c01c 1755 struct lm_async_cb *async = data;
b3b94faa
DT
1756 struct gfs2_glock *gl;
1757
1758 gl = gfs2_glock_find(sdp, &async->lc_name);
1759 if (gfs2_assert_warn(sdp, gl))
1760 return;
1761 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1762 gl->gl_req_bh(gl, async->lc_ret);
1763 gfs2_glock_put(gl);
b3b94faa
DT
1764 return;
1765 }
1766
1767 case LM_CB_NEED_RECOVERY:
1768 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1769 if (sdp->sd_recoverd_process)
1770 wake_up_process(sdp->sd_recoverd_process);
1771 return;
1772
1773 case LM_CB_DROPLOCKS:
1774 gfs2_gl_hash_clear(sdp, NO_WAIT);
1775 gfs2_quota_scan(sdp);
1776 return;
1777
1778 default:
1779 gfs2_assert_warn(sdp, 0);
1780 return;
1781 }
1782}
1783
b3b94faa
DT
1784/**
1785 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1786 * iopen glock from memory
1787 * @io_gl: the iopen glock
1788 * @state: the state into which the glock should be put
1789 *
1790 */
1791
1792void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1793{
b3b94faa
DT
1794
1795 if (state != LM_ST_UNLOCKED)
1796 return;
feaa7bba 1797 /* FIXME: remove this? */
b3b94faa
DT
1798}
1799
1800/**
1801 * demote_ok - Check to see if it's ok to unlock a glock
1802 * @gl: the glock
1803 *
1804 * Returns: 1 if it's ok
1805 */
1806
1807static int demote_ok(struct gfs2_glock *gl)
1808{
1809 struct gfs2_sbd *sdp = gl->gl_sbd;
8fb4b536 1810 const struct gfs2_glock_operations *glops = gl->gl_ops;
b3b94faa
DT
1811 int demote = 1;
1812
1813 if (test_bit(GLF_STICKY, &gl->gl_flags))
1814 demote = 0;
1815 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1816 demote = time_after_eq(jiffies,
1817 gl->gl_stamp +
1818 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1819 else if (glops->go_demote_ok)
1820 demote = glops->go_demote_ok(gl);
1821
1822 return demote;
1823}
1824
1825/**
1826 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1827 * @gl: the glock
1828 *
1829 */
1830
1831void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1832{
1833 struct gfs2_sbd *sdp = gl->gl_sbd;
1834
1835 spin_lock(&sdp->sd_reclaim_lock);
1836 if (list_empty(&gl->gl_reclaim)) {
1837 gfs2_glock_hold(gl);
1838 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1839 atomic_inc(&sdp->sd_reclaim_count);
1840 }
1841 spin_unlock(&sdp->sd_reclaim_lock);
1842
1843 wake_up(&sdp->sd_reclaim_wq);
1844}
1845
1846/**
1847 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1848 * @sdp: the filesystem
1849 *
1850 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1851 * different glock and we notice that there are a lot of glocks in the
1852 * reclaim list.
1853 *
1854 */
1855
1856void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1857{
1858 struct gfs2_glock *gl;
1859
1860 spin_lock(&sdp->sd_reclaim_lock);
1861 if (list_empty(&sdp->sd_reclaim_list)) {
1862 spin_unlock(&sdp->sd_reclaim_lock);
1863 return;
1864 }
1865 gl = list_entry(sdp->sd_reclaim_list.next,
1866 struct gfs2_glock, gl_reclaim);
1867 list_del_init(&gl->gl_reclaim);
1868 spin_unlock(&sdp->sd_reclaim_lock);
1869
1870 atomic_dec(&sdp->sd_reclaim_count);
1871 atomic_inc(&sdp->sd_reclaimed);
1872
1873 if (gfs2_glmutex_trylock(gl)) {
b3b94faa
DT
1874 if (queue_empty(gl, &gl->gl_holders) &&
1875 gl->gl_state != LM_ST_UNLOCKED &&
1876 demote_ok(gl))
1877 handle_callback(gl, LM_ST_UNLOCKED);
1878 gfs2_glmutex_unlock(gl);
1879 }
1880
1881 gfs2_glock_put(gl);
1882}
1883
1884/**
1885 * examine_bucket - Call a function for glock in a hash bucket
1886 * @examiner: the function
1887 * @sdp: the filesystem
1888 * @bucket: the bucket
1889 *
1890 * Returns: 1 if the bucket has entries
1891 */
1892
1893static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1894 struct gfs2_gl_hash_bucket *bucket)
1895{
1896 struct glock_plug plug;
1897 struct list_head *tmp;
1898 struct gfs2_glock *gl;
1899 int entries;
1900
1901 /* Add "plug" to end of bucket list, work back up list from there */
1902 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1903 set_bit(GLF_PLUG, &plug.gl_flags);
1904
1905 write_lock(&bucket->hb_lock);
1906 list_add(&plug.gl_list, &bucket->hb_list);
1907 write_unlock(&bucket->hb_lock);
1908
1909 for (;;) {
1910 write_lock(&bucket->hb_lock);
1911
1912 for (;;) {
1913 tmp = plug.gl_list.next;
1914
1915 if (tmp == &bucket->hb_list) {
1916 list_del(&plug.gl_list);
1917 entries = !list_empty(&bucket->hb_list);
1918 write_unlock(&bucket->hb_lock);
1919 return entries;
1920 }
1921 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1922
1923 /* Move plug up list */
1924 list_move(&plug.gl_list, &gl->gl_list);
1925
1926 if (test_bit(GLF_PLUG, &gl->gl_flags))
1927 continue;
1928
1929 /* examiner() must glock_put() */
1930 gfs2_glock_hold(gl);
1931
1932 break;
1933 }
1934
1935 write_unlock(&bucket->hb_lock);
1936
1937 examiner(gl);
1938 }
1939}
1940
1941/**
1942 * scan_glock - look at a glock and see if we can reclaim it
1943 * @gl: the glock to look at
1944 *
1945 */
1946
1947static void scan_glock(struct gfs2_glock *gl)
1948{
a2242db0
SW
1949 if (gl->gl_ops == &gfs2_inode_glops)
1950 goto out;
1951
b3b94faa 1952 if (gfs2_glmutex_trylock(gl)) {
b3b94faa
DT
1953 if (queue_empty(gl, &gl->gl_holders) &&
1954 gl->gl_state != LM_ST_UNLOCKED &&
1955 demote_ok(gl))
1956 goto out_schedule;
b3b94faa
DT
1957 gfs2_glmutex_unlock(gl);
1958 }
a2242db0 1959out:
b3b94faa 1960 gfs2_glock_put(gl);
b3b94faa
DT
1961 return;
1962
627add2d 1963out_schedule:
b3b94faa
DT
1964 gfs2_glmutex_unlock(gl);
1965 gfs2_glock_schedule_for_reclaim(gl);
1966 gfs2_glock_put(gl);
1967}
1968
1969/**
1970 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1971 * @sdp: the filesystem
1972 *
1973 */
1974
1975void gfs2_scand_internal(struct gfs2_sbd *sdp)
1976{
1977 unsigned int x;
1978
1979 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1980 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1981 cond_resched();
1982 }
1983}
1984
1985/**
1986 * clear_glock - look at a glock and see if we can free it from glock cache
1987 * @gl: the glock to look at
1988 *
1989 */
1990
1991static void clear_glock(struct gfs2_glock *gl)
1992{
1993 struct gfs2_sbd *sdp = gl->gl_sbd;
1994 int released;
1995
1996 spin_lock(&sdp->sd_reclaim_lock);
1997 if (!list_empty(&gl->gl_reclaim)) {
1998 list_del_init(&gl->gl_reclaim);
1999 atomic_dec(&sdp->sd_reclaim_count);
190562bd 2000 spin_unlock(&sdp->sd_reclaim_lock);
b3b94faa
DT
2001 released = gfs2_glock_put(gl);
2002 gfs2_assert(sdp, !released);
190562bd
SW
2003 } else {
2004 spin_unlock(&sdp->sd_reclaim_lock);
b3b94faa 2005 }
b3b94faa
DT
2006
2007 if (gfs2_glmutex_trylock(gl)) {
b3b94faa
DT
2008 if (queue_empty(gl, &gl->gl_holders) &&
2009 gl->gl_state != LM_ST_UNLOCKED)
2010 handle_callback(gl, LM_ST_UNLOCKED);
2011
2012 gfs2_glmutex_unlock(gl);
2013 }
2014
2015 gfs2_glock_put(gl);
2016}
2017
2018/**
2019 * gfs2_gl_hash_clear - Empty out the glock hash table
2020 * @sdp: the filesystem
2021 * @wait: wait until it's all gone
2022 *
2023 * Called when unmounting the filesystem, or when inter-node lock manager
2024 * requests DROPLOCKS because it is running out of capacity.
2025 */
2026
2027void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2028{
2029 unsigned long t;
2030 unsigned int x;
2031 int cont;
2032
2033 t = jiffies;
2034
2035 for (;;) {
2036 cont = 0;
2037
2038 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2039 if (examine_bucket(clear_glock, sdp,
2040 &sdp->sd_gl_hash[x]))
2041 cont = 1;
2042
2043 if (!wait || !cont)
2044 break;
2045
2046 if (time_after_eq(jiffies,
2047 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2048 fs_warn(sdp, "Unmount seems to be stalled. "
2049 "Dumping lock state...\n");
2050 gfs2_dump_lockstate(sdp);
2051 t = jiffies;
2052 }
2053
b3b94faa 2054 invalidate_inodes(sdp->sd_vfs);
fd88de56 2055 msleep(10);
b3b94faa
DT
2056 }
2057}
2058
2059/*
2060 * Diagnostic routines to help debug distributed deadlock
2061 */
2062
2063/**
2064 * dump_holder - print information about a glock holder
2065 * @str: a string naming the type of holder
2066 * @gh: the glock holder
2067 *
2068 * Returns: 0 on success, -ENOBUFS when we run out of space
2069 */
2070
2071static int dump_holder(char *str, struct gfs2_holder *gh)
2072{
2073 unsigned int x;
2074 int error = -ENOBUFS;
2075
d92a8d48
SW
2076 printk(KERN_INFO " %s\n", str);
2077 printk(KERN_INFO " owner = %ld\n",
b3b94faa 2078 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
d92a8d48
SW
2079 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2080 printk(KERN_INFO " gh_flags =");
b3b94faa
DT
2081 for (x = 0; x < 32; x++)
2082 if (gh->gh_flags & (1 << x))
2083 printk(" %u", x);
2084 printk(" \n");
d92a8d48
SW
2085 printk(KERN_INFO " error = %d\n", gh->gh_error);
2086 printk(KERN_INFO " gh_iflags =");
b3b94faa
DT
2087 for (x = 0; x < 32; x++)
2088 if (test_bit(x, &gh->gh_iflags))
2089 printk(" %u", x);
2090 printk(" \n");
d0dc80db 2091 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
b3b94faa
DT
2092
2093 error = 0;
2094
2095 return error;
2096}
2097
2098/**
2099 * dump_inode - print information about an inode
2100 * @ip: the inode
2101 *
2102 * Returns: 0 on success, -ENOBUFS when we run out of space
2103 */
2104
2105static int dump_inode(struct gfs2_inode *ip)
2106{
2107 unsigned int x;
2108 int error = -ENOBUFS;
2109
d92a8d48
SW
2110 printk(KERN_INFO " Inode:\n");
2111 printk(KERN_INFO " num = %llu %llu\n",
382066da
SW
2112 (unsigned long long)ip->i_num.no_formal_ino,
2113 (unsigned long long)ip->i_num.no_addr);
d92a8d48 2114 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
d92a8d48 2115 printk(KERN_INFO " i_flags =");
b3b94faa
DT
2116 for (x = 0; x < 32; x++)
2117 if (test_bit(x, &ip->i_flags))
2118 printk(" %u", x);
2119 printk(" \n");
b3b94faa
DT
2120
2121 error = 0;
2122
2123 return error;
2124}
2125
2126/**
2127 * dump_glock - print information about a glock
2128 * @gl: the glock
2129 * @count: where we are in the buffer
2130 *
2131 * Returns: 0 on success, -ENOBUFS when we run out of space
2132 */
2133
2134static int dump_glock(struct gfs2_glock *gl)
2135{
2136 struct gfs2_holder *gh;
2137 unsigned int x;
2138 int error = -ENOBUFS;
2139
2140 spin_lock(&gl->gl_spin);
2141
88721877
RC
2142 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2143 gl,
2144 gl->gl_name.ln_type,
382066da 2145 (unsigned long long)gl->gl_name.ln_number);
d92a8d48 2146 printk(KERN_INFO " gl_flags =");
b3b94faa
DT
2147 for (x = 0; x < 32; x++)
2148 if (test_bit(x, &gl->gl_flags))
2149 printk(" %u", x);
2150 printk(" \n");
d92a8d48
SW
2151 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2152 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
320dd101
SW
2153 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2154 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
d92a8d48
SW
2155 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2156 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2157 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2158 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2159 printk(KERN_INFO " le = %s\n",
b3b94faa 2160 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
d92a8d48 2161 printk(KERN_INFO " reclaim = %s\n",
b3b94faa
DT
2162 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2163 if (gl->gl_aspace)
88721877
RC
2164 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2165 gl->gl_aspace,
2166 gl->gl_aspace->i_mapping->nrpages);
b3b94faa 2167 else
d92a8d48
SW
2168 printk(KERN_INFO " aspace = no\n");
2169 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
b3b94faa
DT
2170 if (gl->gl_req_gh) {
2171 error = dump_holder("Request", gl->gl_req_gh);
2172 if (error)
2173 goto out;
2174 }
2175 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2176 error = dump_holder("Holder", gh);
2177 if (error)
2178 goto out;
2179 }
2180 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2181 error = dump_holder("Waiter1", gh);
2182 if (error)
2183 goto out;
2184 }
2185 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2186 error = dump_holder("Waiter2", gh);
2187 if (error)
2188 goto out;
2189 }
2190 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2191 error = dump_holder("Waiter3", gh);
2192 if (error)
2193 goto out;
2194 }
5c676f6d 2195 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
b3b94faa
DT
2196 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2197 list_empty(&gl->gl_holders)) {
5c676f6d 2198 error = dump_inode(gl->gl_object);
b3b94faa
DT
2199 if (error)
2200 goto out;
2201 } else {
2202 error = -ENOBUFS;
d92a8d48 2203 printk(KERN_INFO " Inode: busy\n");
b3b94faa
DT
2204 }
2205 }
2206
2207 error = 0;
2208
2209 out:
2210 spin_unlock(&gl->gl_spin);
2211
2212 return error;
2213}
2214
2215/**
2216 * gfs2_dump_lockstate - print out the current lockstate
2217 * @sdp: the filesystem
2218 * @ub: the buffer to copy the information into
2219 *
2220 * If @ub is NULL, dump the lockstate to the console.
2221 *
2222 */
2223
08bc2dbc 2224static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
b3b94faa
DT
2225{
2226 struct gfs2_gl_hash_bucket *bucket;
2227 struct gfs2_glock *gl;
2228 unsigned int x;
2229 int error = 0;
2230
2231 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2232 bucket = &sdp->sd_gl_hash[x];
2233
2234 read_lock(&bucket->hb_lock);
2235
2236 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2237 if (test_bit(GLF_PLUG, &gl->gl_flags))
2238 continue;
2239
2240 error = dump_glock(gl);
2241 if (error)
2242 break;
2243 }
2244
2245 read_unlock(&bucket->hb_lock);
2246
2247 if (error)
2248 break;
2249 }
2250
2251
2252 return error;
2253}
2254
This page took 0.211889 seconds and 5 git commands to generate.