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