blob: 91ac9c9d4a9658e6b9ba5eba540a11cc0ffcba97 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050019#include <linux/kallsyms.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include <asm/uaccess.h>
22
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "lm_interface.h"
25#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#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"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035
36/* Must be kept in sync with the beginning of struct gfs2_glock */
37struct glock_plug {
38 struct list_head gl_list;
39 unsigned long gl_flags;
40};
41
42struct greedy {
43 struct gfs2_holder gr_gh;
44 struct work_struct gr_work;
45};
46
47typedef void (*glock_examiner) (struct gfs2_glock * gl);
48
Adrian Bunk08bc2db2006-04-28 10:59:12 -040049static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
Steven Whitehouse320dd102006-05-18 16:25:27 -040050static int dump_glock(struct gfs2_glock *gl);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040051
David Teiglandb3b94fa2006-01-16 16:50:04 +000052/**
53 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
54 * @actual: the current state of the lock
55 * @requested: the lock state that was requested by the caller
56 * @flags: the modifier flags passed in by the caller
57 *
58 * Returns: 1 if the locks are compatible, 0 otherwise
59 */
60
61static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
62 int flags)
63{
64 if (actual == requested)
65 return 1;
66
67 if (flags & GL_EXACT)
68 return 0;
69
70 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
71 return 1;
72
73 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
74 return 1;
75
76 return 0;
77}
78
79/**
80 * gl_hash() - Turn glock number into hash bucket number
81 * @lock: The glock number
82 *
83 * Returns: The number of the corresponding hash bucket
84 */
85
86static 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
105static void glock_free(struct gfs2_glock *gl)
106{
107 struct gfs2_sbd *sdp = gl->gl_sbd;
108 struct inode *aspace = gl->gl_aspace;
109
110 gfs2_lm_put_lock(sdp, gl->gl_lock);
111
112 if (aspace)
113 gfs2_aspace_put(aspace);
114
115 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116}
117
118/**
119 * gfs2_glock_hold() - increment reference count on glock
120 * @gl: The glock to hold
121 *
122 */
123
124void gfs2_glock_hold(struct gfs2_glock *gl)
125{
126 kref_get(&gl->gl_ref);
127}
128
129/* All work is done after the return from kref_put() so we
130 can release the write_lock before the free. */
131
132static void kill_glock(struct kref *kref)
133{
134 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
135 struct gfs2_sbd *sdp = gl->gl_sbd;
136
137 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
138 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
139 gfs2_assert(sdp, list_empty(&gl->gl_holders));
140 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
141 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
143}
144
145/**
146 * gfs2_glock_put() - Decrement reference count on glock
147 * @gl: The glock to put
148 *
149 */
150
151int gfs2_glock_put(struct gfs2_glock *gl)
152{
153 struct gfs2_sbd *sdp = gl->gl_sbd;
154 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
155 int rv = 0;
156
Steven Whitehousef55ab262006-02-21 12:51:39 +0000157 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158
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);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400163 BUG_ON(spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 glock_free(gl);
165 rv = 1;
166 goto out;
167 }
168 write_unlock(&bucket->hb_lock);
169 out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000170 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 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
189static 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
206static 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
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400233static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
234 struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235{
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
259int 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;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400294 gl->gl_owner = NULL;
295 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296 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 ||
Steven Whitehousef45b7dd2006-07-27 13:53:53 -0400314 glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000315 gl->gl_aspace = gfs2_aspace_get(sdp);
316 if (!gl->gl_aspace) {
317 error = -ENOMEM;
318 goto fail;
319 }
320 }
321
322 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
323 if (error)
324 goto fail_aspace;
325
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326 write_lock(&bucket->hb_lock);
327 tmp = search_bucket(bucket, &name);
328 if (tmp) {
329 write_unlock(&bucket->hb_lock);
330 glock_free(gl);
331 gl = tmp;
332 } else {
333 list_add_tail(&gl->gl_list, &bucket->hb_list);
334 write_unlock(&bucket->hb_lock);
335 }
336
337 *glp = gl;
338
339 return 0;
340
341 fail_aspace:
342 if (gl->gl_aspace)
343 gfs2_aspace_put(gl->gl_aspace);
344
345 fail:
346 kmem_cache_free(gfs2_glock_cachep, gl);
347
348 return error;
349}
350
351/**
352 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
353 * @gl: the glock
354 * @state: the state we're requesting
355 * @flags: the modifier flags
356 * @gh: the holder structure
357 *
358 */
359
Steven Whitehouse190562b2006-04-20 16:57:23 -0400360void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 struct gfs2_holder *gh)
362{
363 INIT_LIST_HEAD(&gh->gh_list);
364 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500365 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400366 gh->gh_owner = current;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 gh->gh_state = state;
368 gh->gh_flags = flags;
369 gh->gh_error = 0;
370 gh->gh_iflags = 0;
371 init_completion(&gh->gh_wait);
372
373 if (gh->gh_state == LM_ST_EXCLUSIVE)
374 gh->gh_flags |= GL_LOCAL_EXCL;
375
376 gfs2_glock_hold(gl);
377}
378
379/**
380 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
381 * @state: the state we're requesting
382 * @flags: the modifier flags
383 * @gh: the holder structure
384 *
385 * Don't mess with the glock.
386 *
387 */
388
Steven Whitehouse190562b2006-04-20 16:57:23 -0400389void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390{
391 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400392 gh->gh_flags = flags;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000393 if (gh->gh_state == LM_ST_EXCLUSIVE)
394 gh->gh_flags |= GL_LOCAL_EXCL;
395
396 gh->gh_iflags &= 1 << HIF_ALLOCED;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500397 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398}
399
400/**
401 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
402 * @gh: the holder structure
403 *
404 */
405
406void gfs2_holder_uninit(struct gfs2_holder *gh)
407{
408 gfs2_glock_put(gh->gh_gl);
409 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500410 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411}
412
413/**
414 * gfs2_holder_get - get a struct gfs2_holder structure
415 * @gl: the glock
416 * @state: the state we're requesting
417 * @flags: the modifier flags
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400418 * @gfp_flags:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419 *
420 * Figure out how big an impact this function has. Either:
421 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
422 * 2) Leave it like it is
423 *
424 * Returns: the holder structure, NULL on ENOMEM
425 */
426
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400427static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
428 unsigned int state,
429 int flags, gfp_t gfp_flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430{
431 struct gfs2_holder *gh;
432
433 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
434 if (!gh)
435 return NULL;
436
437 gfs2_holder_init(gl, state, flags, gh);
438 set_bit(HIF_ALLOCED, &gh->gh_iflags);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500439 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 return gh;
441}
442
443/**
444 * gfs2_holder_put - get rid of a struct gfs2_holder structure
445 * @gh: the holder structure
446 *
447 */
448
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400449static void gfs2_holder_put(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450{
451 gfs2_holder_uninit(gh);
452 kfree(gh);
453}
454
455/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 * rq_mutex - process a mutex request in the queue
457 * @gh: the glock holder
458 *
459 * Returns: 1 if the queue is blocked
460 */
461
462static int rq_mutex(struct gfs2_holder *gh)
463{
464 struct gfs2_glock *gl = gh->gh_gl;
465
466 list_del_init(&gh->gh_list);
467 /* gh->gh_error never examined. */
468 set_bit(GLF_LOCK, &gl->gl_flags);
469 complete(&gh->gh_wait);
470
471 return 1;
472}
473
474/**
475 * rq_promote - process a promote request in the queue
476 * @gh: the glock holder
477 *
478 * Acquire a new inter-node lock, or change a lock state to more restrictive.
479 *
480 * Returns: 1 if the queue is blocked
481 */
482
483static int rq_promote(struct gfs2_holder *gh)
484{
485 struct gfs2_glock *gl = gh->gh_gl;
486 struct gfs2_sbd *sdp = gl->gl_sbd;
487 struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488
489 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
490 if (list_empty(&gl->gl_holders)) {
491 gl->gl_req_gh = gh;
492 set_bit(GLF_LOCK, &gl->gl_flags);
493 spin_unlock(&gl->gl_spin);
494
495 if (atomic_read(&sdp->sd_reclaim_count) >
496 gfs2_tune_get(sdp, gt_reclaim_limit) &&
497 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
498 gfs2_reclaim_glock(sdp);
499 gfs2_reclaim_glock(sdp);
500 }
501
502 glops->go_xmote_th(gl, gh->gh_state,
503 gh->gh_flags);
504
505 spin_lock(&gl->gl_spin);
506 }
507 return 1;
508 }
509
510 if (list_empty(&gl->gl_holders)) {
511 set_bit(HIF_FIRST, &gh->gh_iflags);
512 set_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513 } else {
514 struct gfs2_holder *next_gh;
515 if (gh->gh_flags & GL_LOCAL_EXCL)
516 return 1;
517 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
518 gh_list);
519 if (next_gh->gh_flags & GL_LOCAL_EXCL)
520 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521 }
522
523 list_move_tail(&gh->gh_list, &gl->gl_holders);
524 gh->gh_error = 0;
525 set_bit(HIF_HOLDER, &gh->gh_iflags);
526
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 complete(&gh->gh_wait);
528
529 return 0;
530}
531
532/**
533 * rq_demote - process a demote request in the queue
534 * @gh: the glock holder
535 *
536 * Returns: 1 if the queue is blocked
537 */
538
539static int rq_demote(struct gfs2_holder *gh)
540{
541 struct gfs2_glock *gl = gh->gh_gl;
542 struct gfs2_glock_operations *glops = gl->gl_ops;
543
544 if (!list_empty(&gl->gl_holders))
545 return 1;
546
547 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
548 list_del_init(&gh->gh_list);
549 gh->gh_error = 0;
550 spin_unlock(&gl->gl_spin);
551 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
552 gfs2_holder_put(gh);
553 else
554 complete(&gh->gh_wait);
555 spin_lock(&gl->gl_spin);
556 } else {
557 gl->gl_req_gh = gh;
558 set_bit(GLF_LOCK, &gl->gl_flags);
559 spin_unlock(&gl->gl_spin);
560
561 if (gh->gh_state == LM_ST_UNLOCKED ||
562 gl->gl_state != LM_ST_EXCLUSIVE)
563 glops->go_drop_th(gl);
564 else
565 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
566
567 spin_lock(&gl->gl_spin);
568 }
569
570 return 0;
571}
572
573/**
574 * rq_greedy - process a queued request to drop greedy status
575 * @gh: the glock holder
576 *
577 * Returns: 1 if the queue is blocked
578 */
579
580static int rq_greedy(struct gfs2_holder *gh)
581{
582 struct gfs2_glock *gl = gh->gh_gl;
583
584 list_del_init(&gh->gh_list);
585 /* gh->gh_error never examined. */
586 clear_bit(GLF_GREEDY, &gl->gl_flags);
587 spin_unlock(&gl->gl_spin);
588
589 gfs2_holder_uninit(gh);
590 kfree(container_of(gh, struct greedy, gr_gh));
591
592 spin_lock(&gl->gl_spin);
593
594 return 0;
595}
596
597/**
598 * run_queue - process holder structures on a glock
599 * @gl: the glock
600 *
601 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602static void run_queue(struct gfs2_glock *gl)
603{
604 struct gfs2_holder *gh;
605 int blocked = 1;
606
607 for (;;) {
608 if (test_bit(GLF_LOCK, &gl->gl_flags))
609 break;
610
611 if (!list_empty(&gl->gl_waiters1)) {
612 gh = list_entry(gl->gl_waiters1.next,
613 struct gfs2_holder, gh_list);
614
615 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
616 blocked = rq_mutex(gh);
617 else
618 gfs2_assert_warn(gl->gl_sbd, 0);
619
620 } else if (!list_empty(&gl->gl_waiters2) &&
621 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
622 gh = list_entry(gl->gl_waiters2.next,
623 struct gfs2_holder, gh_list);
624
625 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
626 blocked = rq_demote(gh);
627 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
628 blocked = rq_greedy(gh);
629 else
630 gfs2_assert_warn(gl->gl_sbd, 0);
631
632 } else if (!list_empty(&gl->gl_waiters3)) {
633 gh = list_entry(gl->gl_waiters3.next,
634 struct gfs2_holder, gh_list);
635
636 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
637 blocked = rq_promote(gh);
638 else
639 gfs2_assert_warn(gl->gl_sbd, 0);
640
641 } else
642 break;
643
644 if (blocked)
645 break;
646 }
647}
648
649/**
650 * gfs2_glmutex_lock - acquire a local lock on a glock
651 * @gl: the glock
652 *
653 * Gives caller exclusive access to manipulate a glock structure.
654 */
655
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400656static void gfs2_glmutex_lock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657{
658 struct gfs2_holder gh;
659
660 gfs2_holder_init(gl, 0, 0, &gh);
661 set_bit(HIF_MUTEX, &gh.gh_iflags);
662
663 spin_lock(&gl->gl_spin);
664 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
665 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400666 else {
667 gl->gl_owner = current;
668 gl->gl_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 complete(&gh.gh_wait);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400670 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 spin_unlock(&gl->gl_spin);
672
673 wait_for_completion(&gh.gh_wait);
674 gfs2_holder_uninit(&gh);
675}
676
677/**
678 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
679 * @gl: the glock
680 *
681 * Returns: 1 if the glock is acquired
682 */
683
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400684static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685{
686 int acquired = 1;
687
688 spin_lock(&gl->gl_spin);
689 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
690 acquired = 0;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400691 else {
692 gl->gl_owner = current;
693 gl->gl_ip = (unsigned long)__builtin_return_address(0);
694 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695 spin_unlock(&gl->gl_spin);
696
697 return acquired;
698}
699
700/**
701 * gfs2_glmutex_unlock - release a local lock on a glock
702 * @gl: the glock
703 *
704 */
705
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400706static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707{
708 spin_lock(&gl->gl_spin);
709 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400710 gl->gl_owner = NULL;
711 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 run_queue(gl);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400713 BUG_ON(!spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 spin_unlock(&gl->gl_spin);
715}
716
717/**
718 * handle_callback - add a demote request to a lock's queue
719 * @gl: the glock
720 * @state: the state the caller wants us to change to
721 *
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400722 * Note: This may fail sliently if we are out of memory.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 */
724
725static void handle_callback(struct gfs2_glock *gl, unsigned int state)
726{
727 struct gfs2_holder *gh, *new_gh = NULL;
728
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400729restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 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
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400747 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
748 if (!new_gh)
749 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
751 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
752
753 goto restart;
754 }
755
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400756out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 spin_unlock(&gl->gl_spin);
758
759 if (new_gh)
760 gfs2_holder_put(new_gh);
761}
762
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400763void gfs2_glock_inode_squish(struct inode *inode)
764{
765 struct gfs2_holder gh;
766 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
767 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
768 set_bit(HIF_DEMOTE, &gh.gh_iflags);
769 spin_lock(&gl->gl_spin);
770 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
771 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
772 run_queue(gl);
773 spin_unlock(&gl->gl_spin);
774 gfs2_holder_uninit(&gh);
775}
776
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777/**
778 * state_change - record that the glock is now in a different state
779 * @gl: the glock
780 * @new_state the new state
781 *
782 */
783
784static void state_change(struct gfs2_glock *gl, unsigned int new_state)
785{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 int held1, held2;
787
788 held1 = (gl->gl_state != LM_ST_UNLOCKED);
789 held2 = (new_state != LM_ST_UNLOCKED);
790
791 if (held1 != held2) {
David Teigland6a6b3d02006-02-23 10:11:47 +0000792 if (held2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 gfs2_glock_hold(gl);
David Teigland6a6b3d02006-02-23 10:11:47 +0000794 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 }
797
798 gl->gl_state = new_state;
799}
800
801/**
802 * xmote_bh - Called after the lock module is done acquiring a lock
803 * @gl: The glock in question
804 * @ret: the int returned from the lock module
805 *
806 */
807
808static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
809{
810 struct gfs2_sbd *sdp = gl->gl_sbd;
811 struct gfs2_glock_operations *glops = gl->gl_ops;
812 struct gfs2_holder *gh = gl->gl_req_gh;
813 int prev_state = gl->gl_state;
814 int op_done = 1;
815
816 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
817 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
818 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
819
820 state_change(gl, ret & LM_OUT_ST_MASK);
821
822 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
823 if (glops->go_inval)
824 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
825 } else if (gl->gl_state == LM_ST_DEFERRED) {
826 /* We might not want to do this here.
827 Look at moving to the inode glops. */
828 if (glops->go_inval)
829 glops->go_inval(gl, DIO_DATA);
830 }
831
832 /* Deal with each possible exit condition */
833
834 if (!gh)
835 gl->gl_stamp = jiffies;
836
837 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
838 spin_lock(&gl->gl_spin);
839 list_del_init(&gh->gh_list);
840 gh->gh_error = -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 spin_unlock(&gl->gl_spin);
842
843 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
844 spin_lock(&gl->gl_spin);
845 list_del_init(&gh->gh_list);
846 if (gl->gl_state == gh->gh_state ||
847 gl->gl_state == LM_ST_UNLOCKED)
848 gh->gh_error = 0;
849 else {
850 if (gfs2_assert_warn(sdp, gh->gh_flags &
851 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
852 fs_warn(sdp, "ret = 0x%.8X\n", ret);
853 gh->gh_error = GLR_TRYFAILED;
854 }
855 spin_unlock(&gl->gl_spin);
856
857 if (ret & LM_OUT_CANCELED)
858 handle_callback(gl, LM_ST_UNLOCKED); /* Lame */
859
860 } else if (ret & LM_OUT_CANCELED) {
861 spin_lock(&gl->gl_spin);
862 list_del_init(&gh->gh_list);
863 gh->gh_error = GLR_CANCELED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 spin_unlock(&gl->gl_spin);
865
866 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
867 spin_lock(&gl->gl_spin);
868 list_move_tail(&gh->gh_list, &gl->gl_holders);
869 gh->gh_error = 0;
870 set_bit(HIF_HOLDER, &gh->gh_iflags);
871 spin_unlock(&gl->gl_spin);
872
873 set_bit(HIF_FIRST, &gh->gh_iflags);
874
875 op_done = 0;
876
877 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
878 spin_lock(&gl->gl_spin);
879 list_del_init(&gh->gh_list);
880 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 spin_unlock(&gl->gl_spin);
882
883 } else {
884 if (gfs2_assert_withdraw(sdp, 0) == -1)
885 fs_err(sdp, "ret = 0x%.8X\n", ret);
886 }
887
888 if (glops->go_xmote_bh)
889 glops->go_xmote_bh(gl);
890
891 if (op_done) {
892 spin_lock(&gl->gl_spin);
893 gl->gl_req_gh = NULL;
894 gl->gl_req_bh = NULL;
895 clear_bit(GLF_LOCK, &gl->gl_flags);
896 run_queue(gl);
897 spin_unlock(&gl->gl_spin);
898 }
899
900 gfs2_glock_put(gl);
901
902 if (gh) {
903 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
904 gfs2_holder_put(gh);
905 else
906 complete(&gh->gh_wait);
907 }
908}
909
910/**
911 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
912 * @gl: The glock in question
913 * @state: the requested state
914 * @flags: modifier flags to the lock call
915 *
916 */
917
918void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
919{
920 struct gfs2_sbd *sdp = gl->gl_sbd;
921 struct gfs2_glock_operations *glops = gl->gl_ops;
922 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
923 LM_FLAG_NOEXP | LM_FLAG_ANY |
924 LM_FLAG_PRIORITY);
925 unsigned int lck_ret;
926
927 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
928 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
929 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
930 gfs2_assert_warn(sdp, state != gl->gl_state);
931
932 if (gl->gl_state == LM_ST_EXCLUSIVE) {
933 if (glops->go_sync)
934 glops->go_sync(gl,
935 DIO_METADATA | DIO_DATA | DIO_RELEASE);
936 }
937
938 gfs2_glock_hold(gl);
939 gl->gl_req_bh = xmote_bh;
940
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state,
942 lck_flags);
943
944 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
945 return;
946
947 if (lck_ret & LM_OUT_ASYNC)
948 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
949 else
950 xmote_bh(gl, lck_ret);
951}
952
953/**
954 * drop_bh - Called after a lock module unlock completes
955 * @gl: the glock
956 * @ret: the return status
957 *
958 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
959 * Doesn't drop the reference on the glock the top half took out
960 *
961 */
962
963static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
964{
965 struct gfs2_sbd *sdp = gl->gl_sbd;
966 struct gfs2_glock_operations *glops = gl->gl_ops;
967 struct gfs2_holder *gh = gl->gl_req_gh;
968
969 clear_bit(GLF_PREFETCH, &gl->gl_flags);
970
971 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
972 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
973 gfs2_assert_warn(sdp, !ret);
974
975 state_change(gl, LM_ST_UNLOCKED);
976
977 if (glops->go_inval)
978 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
979
980 if (gh) {
981 spin_lock(&gl->gl_spin);
982 list_del_init(&gh->gh_list);
983 gh->gh_error = 0;
984 spin_unlock(&gl->gl_spin);
985 }
986
987 if (glops->go_drop_bh)
988 glops->go_drop_bh(gl);
989
990 spin_lock(&gl->gl_spin);
991 gl->gl_req_gh = NULL;
992 gl->gl_req_bh = NULL;
993 clear_bit(GLF_LOCK, &gl->gl_flags);
994 run_queue(gl);
995 spin_unlock(&gl->gl_spin);
996
997 gfs2_glock_put(gl);
998
999 if (gh) {
1000 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1001 gfs2_holder_put(gh);
1002 else
1003 complete(&gh->gh_wait);
1004 }
1005}
1006
1007/**
1008 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1009 * @gl: the glock
1010 *
1011 */
1012
1013void gfs2_glock_drop_th(struct gfs2_glock *gl)
1014{
1015 struct gfs2_sbd *sdp = gl->gl_sbd;
1016 struct gfs2_glock_operations *glops = gl->gl_ops;
1017 unsigned int ret;
1018
1019 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1020 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1021 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1022
1023 if (gl->gl_state == LM_ST_EXCLUSIVE) {
1024 if (glops->go_sync)
1025 glops->go_sync(gl,
1026 DIO_METADATA | DIO_DATA | DIO_RELEASE);
1027 }
1028
1029 gfs2_glock_hold(gl);
1030 gl->gl_req_bh = drop_bh;
1031
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1033
1034 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1035 return;
1036
1037 if (!ret)
1038 drop_bh(gl, ret);
1039 else
1040 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1041}
1042
1043/**
1044 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1045 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1046 *
1047 * Don't cancel GL_NOCANCEL requests.
1048 */
1049
1050static void do_cancels(struct gfs2_holder *gh)
1051{
1052 struct gfs2_glock *gl = gh->gh_gl;
1053
1054 spin_lock(&gl->gl_spin);
1055
1056 while (gl->gl_req_gh != gh &&
1057 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1058 !list_empty(&gh->gh_list)) {
1059 if (gl->gl_req_bh &&
1060 !(gl->gl_req_gh &&
1061 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
1062 spin_unlock(&gl->gl_spin);
1063 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1064 msleep(100);
1065 spin_lock(&gl->gl_spin);
1066 } else {
1067 spin_unlock(&gl->gl_spin);
1068 msleep(100);
1069 spin_lock(&gl->gl_spin);
1070 }
1071 }
1072
1073 spin_unlock(&gl->gl_spin);
1074}
1075
1076/**
1077 * glock_wait_internal - wait on a glock acquisition
1078 * @gh: the glock holder
1079 *
1080 * Returns: 0 on success
1081 */
1082
1083static int glock_wait_internal(struct gfs2_holder *gh)
1084{
1085 struct gfs2_glock *gl = gh->gh_gl;
1086 struct gfs2_sbd *sdp = gl->gl_sbd;
1087 struct gfs2_glock_operations *glops = gl->gl_ops;
1088
1089 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1090 return -EIO;
1091
1092 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1093 spin_lock(&gl->gl_spin);
1094 if (gl->gl_req_gh != gh &&
1095 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1096 !list_empty(&gh->gh_list)) {
1097 list_del_init(&gh->gh_list);
1098 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 run_queue(gl);
1100 spin_unlock(&gl->gl_spin);
1101 return gh->gh_error;
1102 }
1103 spin_unlock(&gl->gl_spin);
1104 }
1105
1106 if (gh->gh_flags & LM_FLAG_PRIORITY)
1107 do_cancels(gh);
1108
1109 wait_for_completion(&gh->gh_wait);
1110
1111 if (gh->gh_error)
1112 return gh->gh_error;
1113
1114 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1115 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1116 gh->gh_state,
1117 gh->gh_flags));
1118
1119 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1120 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1121
1122 if (glops->go_lock) {
1123 gh->gh_error = glops->go_lock(gh);
1124 if (gh->gh_error) {
1125 spin_lock(&gl->gl_spin);
1126 list_del_init(&gh->gh_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127 spin_unlock(&gl->gl_spin);
1128 }
1129 }
1130
1131 spin_lock(&gl->gl_spin);
1132 gl->gl_req_gh = NULL;
1133 gl->gl_req_bh = NULL;
1134 clear_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 run_queue(gl);
1136 spin_unlock(&gl->gl_spin);
1137 }
1138
1139 return gh->gh_error;
1140}
1141
1142static inline struct gfs2_holder *
1143find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1144{
1145 struct gfs2_holder *gh;
1146
1147 list_for_each_entry(gh, head, gh_list) {
1148 if (gh->gh_owner == owner)
1149 return gh;
1150 }
1151
1152 return NULL;
1153}
1154
1155/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1157 * @gh: the holder structure to add
1158 *
1159 */
1160
1161static void add_to_queue(struct gfs2_holder *gh)
1162{
1163 struct gfs2_glock *gl = gh->gh_gl;
1164 struct gfs2_holder *existing;
1165
Steven Whitehouse190562b2006-04-20 16:57:23 -04001166 BUG_ON(!gh->gh_owner);
1167
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1169 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001170 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1171 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1172 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001173 }
1174
1175 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1176 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001177 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1178 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1179 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 }
1181
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182 if (gh->gh_flags & LM_FLAG_PRIORITY)
1183 list_add(&gh->gh_list, &gl->gl_waiters3);
1184 else
1185 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1186}
1187
1188/**
1189 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1190 * @gh: the holder structure
1191 *
1192 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1193 *
1194 * Returns: 0, GLR_TRYFAILED, or errno on failure
1195 */
1196
1197int gfs2_glock_nq(struct gfs2_holder *gh)
1198{
1199 struct gfs2_glock *gl = gh->gh_gl;
1200 struct gfs2_sbd *sdp = gl->gl_sbd;
1201 int error = 0;
1202
Steven Whitehouse320dd102006-05-18 16:25:27 -04001203restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1205 set_bit(HIF_ABORTED, &gh->gh_iflags);
1206 return -EIO;
1207 }
1208
1209 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1210
1211 spin_lock(&gl->gl_spin);
1212 add_to_queue(gh);
1213 run_queue(gl);
1214 spin_unlock(&gl->gl_spin);
1215
1216 if (!(gh->gh_flags & GL_ASYNC)) {
1217 error = glock_wait_internal(gh);
1218 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001219 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220 goto restart;
1221 }
1222 }
1223
1224 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1225
Steven Whitehouse320dd102006-05-18 16:25:27 -04001226 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1227 dump_glock(gl);
1228
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229 return error;
1230}
1231
1232/**
1233 * gfs2_glock_poll - poll to see if an async request has been completed
1234 * @gh: the holder
1235 *
1236 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1237 */
1238
1239int gfs2_glock_poll(struct gfs2_holder *gh)
1240{
1241 struct gfs2_glock *gl = gh->gh_gl;
1242 int ready = 0;
1243
1244 spin_lock(&gl->gl_spin);
1245
1246 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1247 ready = 1;
1248 else if (list_empty(&gh->gh_list)) {
1249 if (gh->gh_error == GLR_CANCELED) {
1250 spin_unlock(&gl->gl_spin);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001251 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 if (gfs2_glock_nq(gh))
1253 return 1;
1254 return 0;
1255 } else
1256 ready = 1;
1257 }
1258
1259 spin_unlock(&gl->gl_spin);
1260
1261 return ready;
1262}
1263
1264/**
1265 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1266 * @gh: the holder structure
1267 *
1268 * Returns: 0, GLR_TRYFAILED, or errno on failure
1269 */
1270
1271int gfs2_glock_wait(struct gfs2_holder *gh)
1272{
1273 int error;
1274
1275 error = glock_wait_internal(gh);
1276 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001277 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001278 gh->gh_flags &= ~GL_ASYNC;
1279 error = gfs2_glock_nq(gh);
1280 }
1281
1282 return error;
1283}
1284
1285/**
1286 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1287 * @gh: the glock holder
1288 *
1289 */
1290
1291void gfs2_glock_dq(struct gfs2_holder *gh)
1292{
1293 struct gfs2_glock *gl = gh->gh_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294 struct gfs2_glock_operations *glops = gl->gl_ops;
1295
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296 if (gh->gh_flags & GL_SYNC)
1297 set_bit(GLF_SYNC, &gl->gl_flags);
1298
1299 if (gh->gh_flags & GL_NOCACHE)
1300 handle_callback(gl, LM_ST_UNLOCKED);
1301
1302 gfs2_glmutex_lock(gl);
1303
1304 spin_lock(&gl->gl_spin);
1305 list_del_init(&gh->gh_list);
1306
1307 if (list_empty(&gl->gl_holders)) {
1308 spin_unlock(&gl->gl_spin);
1309
1310 if (glops->go_unlock)
1311 glops->go_unlock(gh);
1312
1313 if (test_bit(GLF_SYNC, &gl->gl_flags)) {
1314 if (glops->go_sync)
1315 glops->go_sync(gl, DIO_METADATA | DIO_DATA);
1316 }
1317
1318 gl->gl_stamp = jiffies;
1319
1320 spin_lock(&gl->gl_spin);
1321 }
1322
1323 clear_bit(GLF_LOCK, &gl->gl_flags);
1324 run_queue(gl);
1325 spin_unlock(&gl->gl_spin);
1326}
1327
1328/**
1329 * gfs2_glock_prefetch - Try to prefetch a glock
1330 * @gl: the glock
1331 * @state: the state to prefetch in
1332 * @flags: flags passed to go_xmote_th()
1333 *
1334 */
1335
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001336static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1337 int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338{
1339 struct gfs2_glock_operations *glops = gl->gl_ops;
1340
1341 spin_lock(&gl->gl_spin);
1342
1343 if (test_bit(GLF_LOCK, &gl->gl_flags) ||
1344 !list_empty(&gl->gl_holders) ||
1345 !list_empty(&gl->gl_waiters1) ||
1346 !list_empty(&gl->gl_waiters2) ||
1347 !list_empty(&gl->gl_waiters3) ||
1348 relaxed_state_ok(gl->gl_state, state, flags)) {
1349 spin_unlock(&gl->gl_spin);
1350 return;
1351 }
1352
1353 set_bit(GLF_PREFETCH, &gl->gl_flags);
1354 set_bit(GLF_LOCK, &gl->gl_flags);
1355 spin_unlock(&gl->gl_spin);
1356
1357 glops->go_xmote_th(gl, state, flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358}
1359
David Teiglandb3b94fa2006-01-16 16:50:04 +00001360static void greedy_work(void *data)
1361{
David Teiglande7f5c012006-04-27 11:25:45 -04001362 struct greedy *gr = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001363 struct gfs2_holder *gh = &gr->gr_gh;
1364 struct gfs2_glock *gl = gh->gh_gl;
1365 struct gfs2_glock_operations *glops = gl->gl_ops;
1366
1367 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1368
1369 if (glops->go_greedy)
1370 glops->go_greedy(gl);
1371
1372 spin_lock(&gl->gl_spin);
1373
1374 if (list_empty(&gl->gl_waiters2)) {
1375 clear_bit(GLF_GREEDY, &gl->gl_flags);
1376 spin_unlock(&gl->gl_spin);
1377 gfs2_holder_uninit(gh);
1378 kfree(gr);
1379 } else {
1380 gfs2_glock_hold(gl);
1381 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1382 run_queue(gl);
1383 spin_unlock(&gl->gl_spin);
1384 gfs2_glock_put(gl);
1385 }
1386}
1387
1388/**
1389 * gfs2_glock_be_greedy -
1390 * @gl:
1391 * @time:
1392 *
1393 * Returns: 0 if go_greedy will be called, 1 otherwise
1394 */
1395
1396int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1397{
1398 struct greedy *gr;
1399 struct gfs2_holder *gh;
1400
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001401 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1403 return 1;
1404
1405 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1406 if (!gr) {
1407 clear_bit(GLF_GREEDY, &gl->gl_flags);
1408 return 1;
1409 }
1410 gh = &gr->gr_gh;
1411
Steven Whitehouse579b78a2006-04-26 14:58:26 -04001412 gfs2_holder_init(gl, 0, 0, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 set_bit(HIF_GREEDY, &gh->gh_iflags);
1414 INIT_WORK(&gr->gr_work, greedy_work, gr);
1415
1416 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1417 schedule_delayed_work(&gr->gr_work, time);
1418
1419 return 0;
1420}
1421
1422/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1424 * @gh: the holder structure
1425 *
1426 */
1427
1428void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1429{
1430 gfs2_glock_dq(gh);
1431 gfs2_holder_uninit(gh);
1432}
1433
1434/**
1435 * gfs2_glock_nq_num - acquire a glock based on lock number
1436 * @sdp: the filesystem
1437 * @number: the lock number
1438 * @glops: the glock operations for the type of glock
1439 * @state: the state to acquire the glock in
1440 * @flags: modifier flags for the aquisition
1441 * @gh: the struct gfs2_holder
1442 *
1443 * Returns: errno
1444 */
1445
1446int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number,
1447 struct gfs2_glock_operations *glops, unsigned int state,
1448 int flags, struct gfs2_holder *gh)
1449{
1450 struct gfs2_glock *gl;
1451 int error;
1452
1453 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1454 if (!error) {
1455 error = gfs2_glock_nq_init(gl, state, flags, gh);
1456 gfs2_glock_put(gl);
1457 }
1458
1459 return error;
1460}
1461
1462/**
1463 * glock_compare - Compare two struct gfs2_glock structures for sorting
1464 * @arg_a: the first structure
1465 * @arg_b: the second structure
1466 *
1467 */
1468
1469static int glock_compare(const void *arg_a, const void *arg_b)
1470{
1471 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1472 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1473 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1474 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1475 int ret = 0;
1476
1477 if (a->ln_number > b->ln_number)
1478 ret = 1;
1479 else if (a->ln_number < b->ln_number)
1480 ret = -1;
1481 else {
1482 if (gh_a->gh_state == LM_ST_SHARED &&
1483 gh_b->gh_state == LM_ST_EXCLUSIVE)
1484 ret = 1;
1485 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1486 (gh_b->gh_flags & GL_LOCAL_EXCL))
1487 ret = 1;
1488 }
1489
1490 return ret;
1491}
1492
1493/**
1494 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1495 * @num_gh: the number of structures
1496 * @ghs: an array of struct gfs2_holder structures
1497 *
1498 * Returns: 0 on success (all glocks acquired),
1499 * errno on failure (no glocks acquired)
1500 */
1501
1502static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1503 struct gfs2_holder **p)
1504{
1505 unsigned int x;
1506 int error = 0;
1507
1508 for (x = 0; x < num_gh; x++)
1509 p[x] = &ghs[x];
1510
1511 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1512
1513 for (x = 0; x < num_gh; x++) {
1514 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1515
1516 error = gfs2_glock_nq(p[x]);
1517 if (error) {
1518 while (x--)
1519 gfs2_glock_dq(p[x]);
1520 break;
1521 }
1522 }
1523
1524 return error;
1525}
1526
1527/**
1528 * gfs2_glock_nq_m - acquire multiple glocks
1529 * @num_gh: the number of structures
1530 * @ghs: an array of struct gfs2_holder structures
1531 *
1532 * Figure out how big an impact this function has. Either:
1533 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1534 * 2) Forget async stuff and just call nq_m_sync()
1535 * 3) Leave it like it is
1536 *
1537 * Returns: 0 on success (all glocks acquired),
1538 * errno on failure (no glocks acquired)
1539 */
1540
1541int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1542{
1543 int *e;
1544 unsigned int x;
1545 int borked = 0, serious = 0;
1546 int error = 0;
1547
1548 if (!num_gh)
1549 return 0;
1550
1551 if (num_gh == 1) {
1552 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1553 return gfs2_glock_nq(ghs);
1554 }
1555
1556 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1557 if (!e)
1558 return -ENOMEM;
1559
1560 for (x = 0; x < num_gh; x++) {
1561 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1562 error = gfs2_glock_nq(&ghs[x]);
1563 if (error) {
1564 borked = 1;
1565 serious = error;
1566 num_gh = x;
1567 break;
1568 }
1569 }
1570
1571 for (x = 0; x < num_gh; x++) {
1572 error = e[x] = glock_wait_internal(&ghs[x]);
1573 if (error) {
1574 borked = 1;
1575 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1576 serious = error;
1577 }
1578 }
1579
1580 if (!borked) {
1581 kfree(e);
1582 return 0;
1583 }
1584
1585 for (x = 0; x < num_gh; x++)
1586 if (!e[x])
1587 gfs2_glock_dq(&ghs[x]);
1588
1589 if (serious)
1590 error = serious;
1591 else {
1592 for (x = 0; x < num_gh; x++)
1593 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1594 &ghs[x]);
1595 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1596 }
1597
1598 kfree(e);
1599
1600 return error;
1601}
1602
1603/**
1604 * gfs2_glock_dq_m - release multiple glocks
1605 * @num_gh: the number of structures
1606 * @ghs: an array of struct gfs2_holder structures
1607 *
1608 */
1609
1610void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1611{
1612 unsigned int x;
1613
1614 for (x = 0; x < num_gh; x++)
1615 gfs2_glock_dq(&ghs[x]);
1616}
1617
1618/**
1619 * gfs2_glock_dq_uninit_m - release multiple glocks
1620 * @num_gh: the number of structures
1621 * @ghs: an array of struct gfs2_holder structures
1622 *
1623 */
1624
1625void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1626{
1627 unsigned int x;
1628
1629 for (x = 0; x < num_gh; x++)
1630 gfs2_glock_dq_uninit(&ghs[x]);
1631}
1632
1633/**
1634 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1635 * @sdp: the filesystem
1636 * @number: the lock number
1637 * @glops: the glock operations for the type of glock
1638 * @state: the state to acquire the glock in
1639 * @flags: modifier flags for the aquisition
1640 *
1641 * Returns: errno
1642 */
1643
1644void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number,
1645 struct gfs2_glock_operations *glops,
1646 unsigned int state, int flags)
1647{
1648 struct gfs2_glock *gl;
1649 int error;
1650
1651 if (atomic_read(&sdp->sd_reclaim_count) <
1652 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1653 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1654 if (!error) {
1655 gfs2_glock_prefetch(gl, state, flags);
1656 gfs2_glock_put(gl);
1657 }
1658 }
1659}
1660
1661/**
1662 * gfs2_lvb_hold - attach a LVB from a glock
1663 * @gl: The glock in question
1664 *
1665 */
1666
1667int gfs2_lvb_hold(struct gfs2_glock *gl)
1668{
1669 int error;
1670
1671 gfs2_glmutex_lock(gl);
1672
1673 if (!atomic_read(&gl->gl_lvb_count)) {
1674 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1675 if (error) {
1676 gfs2_glmutex_unlock(gl);
1677 return error;
1678 }
1679 gfs2_glock_hold(gl);
1680 }
1681 atomic_inc(&gl->gl_lvb_count);
1682
1683 gfs2_glmutex_unlock(gl);
1684
1685 return 0;
1686}
1687
1688/**
1689 * gfs2_lvb_unhold - detach a LVB from a glock
1690 * @gl: The glock in question
1691 *
1692 */
1693
1694void gfs2_lvb_unhold(struct gfs2_glock *gl)
1695{
1696 gfs2_glock_hold(gl);
1697 gfs2_glmutex_lock(gl);
1698
1699 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1700 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1701 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1702 gl->gl_lvb = NULL;
1703 gfs2_glock_put(gl);
1704 }
1705
1706 gfs2_glmutex_unlock(gl);
1707 gfs2_glock_put(gl);
1708}
1709
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001710#if 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001711void gfs2_lvb_sync(struct gfs2_glock *gl)
1712{
1713 gfs2_glmutex_lock(gl);
1714
1715 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count));
1716 if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl)))
1717 gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1718
1719 gfs2_glmutex_unlock(gl);
1720}
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001721#endif /* 0 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722
1723static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1724 unsigned int state)
1725{
1726 struct gfs2_glock *gl;
1727
1728 gl = gfs2_glock_find(sdp, name);
1729 if (!gl)
1730 return;
1731
1732 if (gl->gl_ops->go_callback)
1733 gl->gl_ops->go_callback(gl, state);
1734 handle_callback(gl, state);
1735
1736 spin_lock(&gl->gl_spin);
1737 run_queue(gl);
1738 spin_unlock(&gl->gl_spin);
1739
1740 gfs2_glock_put(gl);
1741}
1742
1743/**
1744 * gfs2_glock_cb - Callback used by locking module
1745 * @fsdata: Pointer to the superblock
1746 * @type: Type of callback
1747 * @data: Type dependent data pointer
1748 *
1749 * Called by the locking module when it wants to tell us something.
1750 * Either we need to drop a lock, one of our ASYNC requests completed, or
1751 * a journal from another client needs to be recovered.
1752 */
1753
1754void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1755{
1756 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1757
David Teiglandb3b94fa2006-01-16 16:50:04 +00001758 switch (type) {
1759 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001760 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001761 return;
1762
1763 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001764 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001765 return;
1766
1767 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001768 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001769 return;
1770
1771 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001772 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001773 struct gfs2_glock *gl;
1774
1775 gl = gfs2_glock_find(sdp, &async->lc_name);
1776 if (gfs2_assert_warn(sdp, gl))
1777 return;
1778 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1779 gl->gl_req_bh(gl, async->lc_ret);
1780 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001781 return;
1782 }
1783
1784 case LM_CB_NEED_RECOVERY:
1785 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1786 if (sdp->sd_recoverd_process)
1787 wake_up_process(sdp->sd_recoverd_process);
1788 return;
1789
1790 case LM_CB_DROPLOCKS:
1791 gfs2_gl_hash_clear(sdp, NO_WAIT);
1792 gfs2_quota_scan(sdp);
1793 return;
1794
1795 default:
1796 gfs2_assert_warn(sdp, 0);
1797 return;
1798 }
1799}
1800
1801/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001802 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1803 * iopen glock from memory
1804 * @io_gl: the iopen glock
1805 * @state: the state into which the glock should be put
1806 *
1807 */
1808
1809void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1810{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001811
1812 if (state != LM_ST_UNLOCKED)
1813 return;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001814 /* FIXME: remove this? */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001815}
1816
1817/**
1818 * demote_ok - Check to see if it's ok to unlock a glock
1819 * @gl: the glock
1820 *
1821 * Returns: 1 if it's ok
1822 */
1823
1824static int demote_ok(struct gfs2_glock *gl)
1825{
1826 struct gfs2_sbd *sdp = gl->gl_sbd;
1827 struct gfs2_glock_operations *glops = gl->gl_ops;
1828 int demote = 1;
1829
1830 if (test_bit(GLF_STICKY, &gl->gl_flags))
1831 demote = 0;
1832 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
1833 demote = time_after_eq(jiffies,
1834 gl->gl_stamp +
1835 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1836 else if (glops->go_demote_ok)
1837 demote = glops->go_demote_ok(gl);
1838
1839 return demote;
1840}
1841
1842/**
1843 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1844 * @gl: the glock
1845 *
1846 */
1847
1848void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1849{
1850 struct gfs2_sbd *sdp = gl->gl_sbd;
1851
1852 spin_lock(&sdp->sd_reclaim_lock);
1853 if (list_empty(&gl->gl_reclaim)) {
1854 gfs2_glock_hold(gl);
1855 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1856 atomic_inc(&sdp->sd_reclaim_count);
1857 }
1858 spin_unlock(&sdp->sd_reclaim_lock);
1859
1860 wake_up(&sdp->sd_reclaim_wq);
1861}
1862
1863/**
1864 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1865 * @sdp: the filesystem
1866 *
1867 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1868 * different glock and we notice that there are a lot of glocks in the
1869 * reclaim list.
1870 *
1871 */
1872
1873void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1874{
1875 struct gfs2_glock *gl;
1876
1877 spin_lock(&sdp->sd_reclaim_lock);
1878 if (list_empty(&sdp->sd_reclaim_list)) {
1879 spin_unlock(&sdp->sd_reclaim_lock);
1880 return;
1881 }
1882 gl = list_entry(sdp->sd_reclaim_list.next,
1883 struct gfs2_glock, gl_reclaim);
1884 list_del_init(&gl->gl_reclaim);
1885 spin_unlock(&sdp->sd_reclaim_lock);
1886
1887 atomic_dec(&sdp->sd_reclaim_count);
1888 atomic_inc(&sdp->sd_reclaimed);
1889
1890 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001891 if (queue_empty(gl, &gl->gl_holders) &&
1892 gl->gl_state != LM_ST_UNLOCKED &&
1893 demote_ok(gl))
1894 handle_callback(gl, LM_ST_UNLOCKED);
1895 gfs2_glmutex_unlock(gl);
1896 }
1897
1898 gfs2_glock_put(gl);
1899}
1900
1901/**
1902 * examine_bucket - Call a function for glock in a hash bucket
1903 * @examiner: the function
1904 * @sdp: the filesystem
1905 * @bucket: the bucket
1906 *
1907 * Returns: 1 if the bucket has entries
1908 */
1909
1910static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1911 struct gfs2_gl_hash_bucket *bucket)
1912{
1913 struct glock_plug plug;
1914 struct list_head *tmp;
1915 struct gfs2_glock *gl;
1916 int entries;
1917
1918 /* Add "plug" to end of bucket list, work back up list from there */
1919 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1920 set_bit(GLF_PLUG, &plug.gl_flags);
1921
1922 write_lock(&bucket->hb_lock);
1923 list_add(&plug.gl_list, &bucket->hb_list);
1924 write_unlock(&bucket->hb_lock);
1925
1926 for (;;) {
1927 write_lock(&bucket->hb_lock);
1928
1929 for (;;) {
1930 tmp = plug.gl_list.next;
1931
1932 if (tmp == &bucket->hb_list) {
1933 list_del(&plug.gl_list);
1934 entries = !list_empty(&bucket->hb_list);
1935 write_unlock(&bucket->hb_lock);
1936 return entries;
1937 }
1938 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1939
1940 /* Move plug up list */
1941 list_move(&plug.gl_list, &gl->gl_list);
1942
1943 if (test_bit(GLF_PLUG, &gl->gl_flags))
1944 continue;
1945
1946 /* examiner() must glock_put() */
1947 gfs2_glock_hold(gl);
1948
1949 break;
1950 }
1951
1952 write_unlock(&bucket->hb_lock);
1953
1954 examiner(gl);
1955 }
1956}
1957
1958/**
1959 * scan_glock - look at a glock and see if we can reclaim it
1960 * @gl: the glock to look at
1961 *
1962 */
1963
1964static void scan_glock(struct gfs2_glock *gl)
1965{
1966 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse29937ac2006-07-06 17:58:03 -04001967 if (gl->gl_ops == &gfs2_inode_glops)
1968 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001969 if (queue_empty(gl, &gl->gl_holders) &&
1970 gl->gl_state != LM_ST_UNLOCKED &&
1971 demote_ok(gl))
1972 goto out_schedule;
Steven Whitehouse29937ac2006-07-06 17:58:03 -04001973out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001974 gfs2_glmutex_unlock(gl);
1975 }
1976
1977 gfs2_glock_put(gl);
1978
1979 return;
1980
Steven Whitehouse627add22006-07-05 13:16:19 -04001981out_schedule:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001982 gfs2_glmutex_unlock(gl);
1983 gfs2_glock_schedule_for_reclaim(gl);
1984 gfs2_glock_put(gl);
1985}
1986
1987/**
1988 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1989 * @sdp: the filesystem
1990 *
1991 */
1992
1993void gfs2_scand_internal(struct gfs2_sbd *sdp)
1994{
1995 unsigned int x;
1996
1997 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1998 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1999 cond_resched();
2000 }
2001}
2002
2003/**
2004 * clear_glock - look at a glock and see if we can free it from glock cache
2005 * @gl: the glock to look at
2006 *
2007 */
2008
2009static void clear_glock(struct gfs2_glock *gl)
2010{
2011 struct gfs2_sbd *sdp = gl->gl_sbd;
2012 int released;
2013
2014 spin_lock(&sdp->sd_reclaim_lock);
2015 if (!list_empty(&gl->gl_reclaim)) {
2016 list_del_init(&gl->gl_reclaim);
2017 atomic_dec(&sdp->sd_reclaim_count);
Steven Whitehouse190562b2006-04-20 16:57:23 -04002018 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002019 released = gfs2_glock_put(gl);
2020 gfs2_assert(sdp, !released);
Steven Whitehouse190562b2006-04-20 16:57:23 -04002021 } else {
2022 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002023 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002024
2025 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002026 if (queue_empty(gl, &gl->gl_holders) &&
2027 gl->gl_state != LM_ST_UNLOCKED)
2028 handle_callback(gl, LM_ST_UNLOCKED);
2029
2030 gfs2_glmutex_unlock(gl);
2031 }
2032
2033 gfs2_glock_put(gl);
2034}
2035
2036/**
2037 * gfs2_gl_hash_clear - Empty out the glock hash table
2038 * @sdp: the filesystem
2039 * @wait: wait until it's all gone
2040 *
2041 * Called when unmounting the filesystem, or when inter-node lock manager
2042 * requests DROPLOCKS because it is running out of capacity.
2043 */
2044
2045void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2046{
2047 unsigned long t;
2048 unsigned int x;
2049 int cont;
2050
2051 t = jiffies;
2052
2053 for (;;) {
2054 cont = 0;
2055
2056 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2057 if (examine_bucket(clear_glock, sdp,
2058 &sdp->sd_gl_hash[x]))
2059 cont = 1;
2060
2061 if (!wait || !cont)
2062 break;
2063
2064 if (time_after_eq(jiffies,
2065 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2066 fs_warn(sdp, "Unmount seems to be stalled. "
2067 "Dumping lock state...\n");
2068 gfs2_dump_lockstate(sdp);
2069 t = jiffies;
2070 }
2071
2072 /* invalidate_inodes() requires that the sb inodes list
2073 not change, but an async completion callback for an
2074 unlock can occur which does glock_put() which
2075 can call iput() which will change the sb inodes list.
2076 invalidate_inodes_mutex prevents glock_put()'s during
2077 an invalidate_inodes() */
2078
Steven Whitehousef55ab262006-02-21 12:51:39 +00002079 mutex_lock(&sdp->sd_invalidate_inodes_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002080 invalidate_inodes(sdp->sd_vfs);
Steven Whitehousef55ab262006-02-21 12:51:39 +00002081 mutex_unlock(&sdp->sd_invalidate_inodes_mutex);
Steven Whitehousefd88de562006-05-05 16:59:11 -04002082 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002083 }
2084}
2085
2086/*
2087 * Diagnostic routines to help debug distributed deadlock
2088 */
2089
2090/**
2091 * dump_holder - print information about a glock holder
2092 * @str: a string naming the type of holder
2093 * @gh: the glock holder
2094 *
2095 * Returns: 0 on success, -ENOBUFS when we run out of space
2096 */
2097
2098static int dump_holder(char *str, struct gfs2_holder *gh)
2099{
2100 unsigned int x;
2101 int error = -ENOBUFS;
2102
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002103 printk(KERN_INFO " %s\n", str);
2104 printk(KERN_INFO " owner = %ld\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002105 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002106 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2107 printk(KERN_INFO " gh_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002108 for (x = 0; x < 32; x++)
2109 if (gh->gh_flags & (1 << x))
2110 printk(" %u", x);
2111 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002112 printk(KERN_INFO " error = %d\n", gh->gh_error);
2113 printk(KERN_INFO " gh_iflags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002114 for (x = 0; x < 32; x++)
2115 if (test_bit(x, &gh->gh_iflags))
2116 printk(" %u", x);
2117 printk(" \n");
Steven Whitehoused0dc80d2006-03-29 14:36:49 -05002118 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002119
2120 error = 0;
2121
2122 return error;
2123}
2124
2125/**
2126 * dump_inode - print information about an inode
2127 * @ip: the inode
2128 *
2129 * Returns: 0 on success, -ENOBUFS when we run out of space
2130 */
2131
2132static int dump_inode(struct gfs2_inode *ip)
2133{
2134 unsigned int x;
2135 int error = -ENOBUFS;
2136
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002137 printk(KERN_INFO " Inode:\n");
2138 printk(KERN_INFO " num = %llu %llu\n",
Steven Whitehouse382066d2006-05-24 10:22:09 -04002139 (unsigned long long)ip->i_num.no_formal_ino,
2140 (unsigned long long)ip->i_num.no_addr);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002141 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002142 printk(KERN_INFO " i_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002143 for (x = 0; x < 32; x++)
2144 if (test_bit(x, &ip->i_flags))
2145 printk(" %u", x);
2146 printk(" \n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002147
2148 error = 0;
2149
2150 return error;
2151}
2152
2153/**
2154 * dump_glock - print information about a glock
2155 * @gl: the glock
2156 * @count: where we are in the buffer
2157 *
2158 * Returns: 0 on success, -ENOBUFS when we run out of space
2159 */
2160
2161static int dump_glock(struct gfs2_glock *gl)
2162{
2163 struct gfs2_holder *gh;
2164 unsigned int x;
2165 int error = -ENOBUFS;
2166
2167 spin_lock(&gl->gl_spin);
2168
Steven Whitehouse320dd102006-05-18 16:25:27 -04002169 printk(KERN_INFO "Glock (%u, %llu)\n", gl->gl_name.ln_type,
Steven Whitehouse382066d2006-05-24 10:22:09 -04002170 (unsigned long long)gl->gl_name.ln_number);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002171 printk(KERN_INFO " gl_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002172 for (x = 0; x < 32; x++)
2173 if (test_bit(x, &gl->gl_flags))
2174 printk(" %u", x);
2175 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002176 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2177 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
Steven Whitehouse320dd102006-05-18 16:25:27 -04002178 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2179 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002180 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2181 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2182 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2183 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2184 printk(KERN_INFO " le = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002185 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002186 printk(KERN_INFO " reclaim = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002187 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2188 if (gl->gl_aspace)
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002189 printk(KERN_INFO " aspace = %lu\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002190 gl->gl_aspace->i_mapping->nrpages);
2191 else
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002192 printk(KERN_INFO " aspace = no\n");
2193 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002194 if (gl->gl_req_gh) {
2195 error = dump_holder("Request", gl->gl_req_gh);
2196 if (error)
2197 goto out;
2198 }
2199 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2200 error = dump_holder("Holder", gh);
2201 if (error)
2202 goto out;
2203 }
2204 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2205 error = dump_holder("Waiter1", gh);
2206 if (error)
2207 goto out;
2208 }
2209 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2210 error = dump_holder("Waiter2", gh);
2211 if (error)
2212 goto out;
2213 }
2214 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2215 error = dump_holder("Waiter3", gh);
2216 if (error)
2217 goto out;
2218 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002219 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002220 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2221 list_empty(&gl->gl_holders)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002222 error = dump_inode(gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002223 if (error)
2224 goto out;
2225 } else {
2226 error = -ENOBUFS;
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002227 printk(KERN_INFO " Inode: busy\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002228 }
2229 }
2230
2231 error = 0;
2232
2233 out:
2234 spin_unlock(&gl->gl_spin);
2235
2236 return error;
2237}
2238
2239/**
2240 * gfs2_dump_lockstate - print out the current lockstate
2241 * @sdp: the filesystem
2242 * @ub: the buffer to copy the information into
2243 *
2244 * If @ub is NULL, dump the lockstate to the console.
2245 *
2246 */
2247
Adrian Bunk08bc2db2006-04-28 10:59:12 -04002248static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002249{
2250 struct gfs2_gl_hash_bucket *bucket;
2251 struct gfs2_glock *gl;
2252 unsigned int x;
2253 int error = 0;
2254
2255 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2256 bucket = &sdp->sd_gl_hash[x];
2257
2258 read_lock(&bucket->hb_lock);
2259
2260 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2261 if (test_bit(GLF_PLUG, &gl->gl_flags))
2262 continue;
2263
2264 error = dump_glock(gl);
2265 if (error)
2266 break;
2267 }
2268
2269 read_unlock(&bucket->hb_lock);
2270
2271 if (error)
2272 break;
2273 }
2274
2275
2276 return error;
2277}
2278