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