blob: 00769674f2eabe62c1fe9514b308e190a201daad [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
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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
Steven Whitehouseb8547852006-09-07 13:12:27 -040086static unsigned int gl_hash(const struct gfs2_sbd *sdp,
87 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +000088{
89 unsigned int h;
90
Steven Whitehousecd915492006-09-04 12:49:07 -040091 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -040093 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +000094 h &= GFS2_GL_HASH_MASK;
95
96 return h;
97}
98
99/**
100 * glock_free() - Perform a few checks and then release struct gfs2_glock
101 * @gl: The glock to release
102 *
103 * Also calls lock module to release its internal structure for this glock.
104 *
105 */
106
107static void glock_free(struct gfs2_glock *gl)
108{
109 struct gfs2_sbd *sdp = gl->gl_sbd;
110 struct inode *aspace = gl->gl_aspace;
111
112 gfs2_lm_put_lock(sdp, gl->gl_lock);
113
114 if (aspace)
115 gfs2_aspace_put(aspace);
116
117 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118}
119
120/**
121 * gfs2_glock_hold() - increment reference count on glock
122 * @gl: The glock to hold
123 *
124 */
125
126void gfs2_glock_hold(struct gfs2_glock *gl)
127{
128 kref_get(&gl->gl_ref);
129}
130
131/* All work is done after the return from kref_put() so we
132 can release the write_lock before the free. */
133
134static void kill_glock(struct kref *kref)
135{
136 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
137 struct gfs2_sbd *sdp = gl->gl_sbd;
138
139 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
140 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
141 gfs2_assert(sdp, list_empty(&gl->gl_holders));
142 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
143 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
144 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
145}
146
147/**
148 * gfs2_glock_put() - Decrement reference count on glock
149 * @gl: The glock to put
150 *
151 */
152
153int gfs2_glock_put(struct gfs2_glock *gl)
154{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155 struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket;
156 int rv = 0;
157
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 write_lock(&bucket->hb_lock);
159 if (kref_put(&gl->gl_ref, kill_glock)) {
160 list_del_init(&gl->gl_list);
161 write_unlock(&bucket->hb_lock);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400162 BUG_ON(spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 glock_free(gl);
164 rv = 1;
165 goto out;
166 }
167 write_unlock(&bucket->hb_lock);
Steven Whitehousea2242db2006-08-24 17:03:05 -0400168out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 return rv;
170}
171
172/**
173 * queue_empty - check to see if a glock's queue is empty
174 * @gl: the glock
175 * @head: the head of the queue to check
176 *
177 * This function protects the list in the event that a process already
178 * has a holder on the list and is adding a second holder for itself.
179 * The glmutex lock is what generally prevents processes from working
180 * on the same glock at once, but the special case of adding a second
181 * holder for yourself ("recursive" locking) doesn't involve locking
182 * glmutex, making the spin lock necessary.
183 *
184 * Returns: 1 if the queue is empty
185 */
186
187static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
188{
189 int empty;
190 spin_lock(&gl->gl_spin);
191 empty = list_empty(head);
192 spin_unlock(&gl->gl_spin);
193 return empty;
194}
195
196/**
197 * search_bucket() - Find struct gfs2_glock by lock number
198 * @bucket: the bucket to search
199 * @name: The lock name
200 *
201 * Returns: NULL, or the struct gfs2_glock with the requested number
202 */
203
204static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400205 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400206 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207{
208 struct gfs2_glock *gl;
209
210 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
211 if (test_bit(GLF_PLUG, &gl->gl_flags))
212 continue;
213 if (!lm_name_equal(&gl->gl_name, name))
214 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400215 if (gl->gl_sbd != sdp)
216 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217
218 kref_get(&gl->gl_ref);
219
220 return gl;
221 }
222
223 return NULL;
224}
225
226/**
227 * gfs2_glock_find() - Find glock by lock number
228 * @sdp: The GFS2 superblock
229 * @name: The lock name
230 *
231 * Returns: NULL, or the struct gfs2_glock with the requested number
232 */
233
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400234static struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400235 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236{
Steven Whitehouseb8547852006-09-07 13:12:27 -0400237 struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(sdp, name)];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238 struct gfs2_glock *gl;
239
240 read_lock(&bucket->hb_lock);
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400241 gl = search_bucket(bucket, sdp, name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 read_unlock(&bucket->hb_lock);
243
244 return gl;
245}
246
247/**
248 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
249 * @sdp: The GFS2 superblock
250 * @number: the lock number
251 * @glops: The glock_operations to use
252 * @create: If 0, don't create the glock if it doesn't exist
253 * @glp: the glock is returned here
254 *
255 * This does not lock a glock, just finds/creates structures for one.
256 *
257 * Returns: errno
258 */
259
Steven Whitehousecd915492006-09-04 12:49:07 -0400260int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400261 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 struct gfs2_glock **glp)
263{
264 struct lm_lockname name;
265 struct gfs2_glock *gl, *tmp;
266 struct gfs2_gl_hash_bucket *bucket;
267 int error;
268
269 name.ln_number = number;
270 name.ln_type = glops->go_type;
Steven Whitehouseb8547852006-09-07 13:12:27 -0400271 bucket = &sdp->sd_gl_hash[gl_hash(sdp, &name)];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272
273 read_lock(&bucket->hb_lock);
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400274 gl = search_bucket(bucket, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275 read_unlock(&bucket->hb_lock);
276
277 if (gl || !create) {
278 *glp = gl;
279 return 0;
280 }
281
282 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
283 if (!gl)
284 return -ENOMEM;
285
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400286 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 gl->gl_name = name;
288 kref_init(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400290 gl->gl_owner = NULL;
291 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 gl->gl_ops = glops;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400293 gl->gl_req_gh = NULL;
294 gl->gl_req_bh = NULL;
295 gl->gl_vn = 0;
296 gl->gl_stamp = jiffies;
297 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 gl->gl_bucket = bucket;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 gl->gl_sbd = sdp;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400300 gl->gl_aspace = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302
303 /* If this glock protects actual on-disk data or metadata blocks,
304 create a VFS inode to manage the pages/buffers holding them. */
Steven Whitehouse50299962006-09-04 09:49:55 -0400305 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306 gl->gl_aspace = gfs2_aspace_get(sdp);
307 if (!gl->gl_aspace) {
308 error = -ENOMEM;
309 goto fail;
310 }
311 }
312
313 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
314 if (error)
315 goto fail_aspace;
316
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 write_lock(&bucket->hb_lock);
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400318 tmp = search_bucket(bucket, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 if (tmp) {
320 write_unlock(&bucket->hb_lock);
321 glock_free(gl);
322 gl = tmp;
323 } else {
324 list_add_tail(&gl->gl_list, &bucket->hb_list);
325 write_unlock(&bucket->hb_lock);
326 }
327
328 *glp = gl;
329
330 return 0;
331
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400332fail_aspace:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 if (gl->gl_aspace)
334 gfs2_aspace_put(gl->gl_aspace);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400335fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 return error;
338}
339
340/**
341 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
342 * @gl: the glock
343 * @state: the state we're requesting
344 * @flags: the modifier flags
345 * @gh: the holder structure
346 *
347 */
348
Steven Whitehouse190562b2006-04-20 16:57:23 -0400349void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 struct gfs2_holder *gh)
351{
352 INIT_LIST_HEAD(&gh->gh_list);
353 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500354 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400355 gh->gh_owner = current;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356 gh->gh_state = state;
357 gh->gh_flags = flags;
358 gh->gh_error = 0;
359 gh->gh_iflags = 0;
360 init_completion(&gh->gh_wait);
361
362 if (gh->gh_state == LM_ST_EXCLUSIVE)
363 gh->gh_flags |= GL_LOCAL_EXCL;
364
365 gfs2_glock_hold(gl);
366}
367
368/**
369 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
370 * @state: the state we're requesting
371 * @flags: the modifier flags
372 * @gh: the holder structure
373 *
374 * Don't mess with the glock.
375 *
376 */
377
Steven Whitehouse190562b2006-04-20 16:57:23 -0400378void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379{
380 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400381 gh->gh_flags = flags;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382 if (gh->gh_state == LM_ST_EXCLUSIVE)
383 gh->gh_flags |= GL_LOCAL_EXCL;
384
385 gh->gh_iflags &= 1 << HIF_ALLOCED;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500386 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387}
388
389/**
390 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
391 * @gh: the holder structure
392 *
393 */
394
395void gfs2_holder_uninit(struct gfs2_holder *gh)
396{
397 gfs2_glock_put(gh->gh_gl);
398 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500399 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400}
401
402/**
403 * gfs2_holder_get - get a struct gfs2_holder structure
404 * @gl: the glock
405 * @state: the state we're requesting
406 * @flags: the modifier flags
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400407 * @gfp_flags:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 *
409 * Figure out how big an impact this function has. Either:
410 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
411 * 2) Leave it like it is
412 *
413 * Returns: the holder structure, NULL on ENOMEM
414 */
415
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400416static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
417 unsigned int state,
418 int flags, gfp_t gfp_flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419{
420 struct gfs2_holder *gh;
421
422 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
423 if (!gh)
424 return NULL;
425
426 gfs2_holder_init(gl, state, flags, gh);
427 set_bit(HIF_ALLOCED, &gh->gh_iflags);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500428 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429 return gh;
430}
431
432/**
433 * gfs2_holder_put - get rid of a struct gfs2_holder structure
434 * @gh: the holder structure
435 *
436 */
437
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400438static void gfs2_holder_put(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439{
440 gfs2_holder_uninit(gh);
441 kfree(gh);
442}
443
444/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 * rq_mutex - process a mutex request in the queue
446 * @gh: the glock holder
447 *
448 * Returns: 1 if the queue is blocked
449 */
450
451static int rq_mutex(struct gfs2_holder *gh)
452{
453 struct gfs2_glock *gl = gh->gh_gl;
454
455 list_del_init(&gh->gh_list);
456 /* gh->gh_error never examined. */
457 set_bit(GLF_LOCK, &gl->gl_flags);
458 complete(&gh->gh_wait);
459
460 return 1;
461}
462
463/**
464 * rq_promote - process a promote request in the queue
465 * @gh: the glock holder
466 *
467 * Acquire a new inter-node lock, or change a lock state to more restrictive.
468 *
469 * Returns: 1 if the queue is blocked
470 */
471
472static int rq_promote(struct gfs2_holder *gh)
473{
474 struct gfs2_glock *gl = gh->gh_gl;
475 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400476 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477
478 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
479 if (list_empty(&gl->gl_holders)) {
480 gl->gl_req_gh = gh;
481 set_bit(GLF_LOCK, &gl->gl_flags);
482 spin_unlock(&gl->gl_spin);
483
484 if (atomic_read(&sdp->sd_reclaim_count) >
485 gfs2_tune_get(sdp, gt_reclaim_limit) &&
486 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
487 gfs2_reclaim_glock(sdp);
488 gfs2_reclaim_glock(sdp);
489 }
490
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400491 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000492 spin_lock(&gl->gl_spin);
493 }
494 return 1;
495 }
496
497 if (list_empty(&gl->gl_holders)) {
498 set_bit(HIF_FIRST, &gh->gh_iflags);
499 set_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500 } else {
501 struct gfs2_holder *next_gh;
502 if (gh->gh_flags & GL_LOCAL_EXCL)
503 return 1;
504 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
505 gh_list);
506 if (next_gh->gh_flags & GL_LOCAL_EXCL)
507 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 }
509
510 list_move_tail(&gh->gh_list, &gl->gl_holders);
511 gh->gh_error = 0;
512 set_bit(HIF_HOLDER, &gh->gh_iflags);
513
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514 complete(&gh->gh_wait);
515
516 return 0;
517}
518
519/**
520 * rq_demote - process a demote request in the queue
521 * @gh: the glock holder
522 *
523 * Returns: 1 if the queue is blocked
524 */
525
526static int rq_demote(struct gfs2_holder *gh)
527{
528 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400529 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
531 if (!list_empty(&gl->gl_holders))
532 return 1;
533
534 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
535 list_del_init(&gh->gh_list);
536 gh->gh_error = 0;
537 spin_unlock(&gl->gl_spin);
538 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
539 gfs2_holder_put(gh);
540 else
541 complete(&gh->gh_wait);
542 spin_lock(&gl->gl_spin);
543 } else {
544 gl->gl_req_gh = gh;
545 set_bit(GLF_LOCK, &gl->gl_flags);
546 spin_unlock(&gl->gl_spin);
547
548 if (gh->gh_state == LM_ST_UNLOCKED ||
549 gl->gl_state != LM_ST_EXCLUSIVE)
550 glops->go_drop_th(gl);
551 else
552 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
553
554 spin_lock(&gl->gl_spin);
555 }
556
557 return 0;
558}
559
560/**
561 * rq_greedy - process a queued request to drop greedy status
562 * @gh: the glock holder
563 *
564 * Returns: 1 if the queue is blocked
565 */
566
567static int rq_greedy(struct gfs2_holder *gh)
568{
569 struct gfs2_glock *gl = gh->gh_gl;
570
571 list_del_init(&gh->gh_list);
572 /* gh->gh_error never examined. */
573 clear_bit(GLF_GREEDY, &gl->gl_flags);
574 spin_unlock(&gl->gl_spin);
575
576 gfs2_holder_uninit(gh);
577 kfree(container_of(gh, struct greedy, gr_gh));
578
579 spin_lock(&gl->gl_spin);
580
581 return 0;
582}
583
584/**
585 * run_queue - process holder structures on a glock
586 * @gl: the glock
587 *
588 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589static void run_queue(struct gfs2_glock *gl)
590{
591 struct gfs2_holder *gh;
592 int blocked = 1;
593
594 for (;;) {
595 if (test_bit(GLF_LOCK, &gl->gl_flags))
596 break;
597
598 if (!list_empty(&gl->gl_waiters1)) {
599 gh = list_entry(gl->gl_waiters1.next,
600 struct gfs2_holder, gh_list);
601
602 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
603 blocked = rq_mutex(gh);
604 else
605 gfs2_assert_warn(gl->gl_sbd, 0);
606
607 } else if (!list_empty(&gl->gl_waiters2) &&
608 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
609 gh = list_entry(gl->gl_waiters2.next,
610 struct gfs2_holder, gh_list);
611
612 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
613 blocked = rq_demote(gh);
614 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
615 blocked = rq_greedy(gh);
616 else
617 gfs2_assert_warn(gl->gl_sbd, 0);
618
619 } else if (!list_empty(&gl->gl_waiters3)) {
620 gh = list_entry(gl->gl_waiters3.next,
621 struct gfs2_holder, gh_list);
622
623 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
624 blocked = rq_promote(gh);
625 else
626 gfs2_assert_warn(gl->gl_sbd, 0);
627
628 } else
629 break;
630
631 if (blocked)
632 break;
633 }
634}
635
636/**
637 * gfs2_glmutex_lock - acquire a local lock on a glock
638 * @gl: the glock
639 *
640 * Gives caller exclusive access to manipulate a glock structure.
641 */
642
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400643static void gfs2_glmutex_lock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644{
645 struct gfs2_holder gh;
646
647 gfs2_holder_init(gl, 0, 0, &gh);
648 set_bit(HIF_MUTEX, &gh.gh_iflags);
649
650 spin_lock(&gl->gl_spin);
651 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
652 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400653 else {
654 gl->gl_owner = current;
655 gl->gl_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 complete(&gh.gh_wait);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400657 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 spin_unlock(&gl->gl_spin);
659
660 wait_for_completion(&gh.gh_wait);
661 gfs2_holder_uninit(&gh);
662}
663
664/**
665 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
666 * @gl: the glock
667 *
668 * Returns: 1 if the glock is acquired
669 */
670
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400671static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672{
673 int acquired = 1;
674
675 spin_lock(&gl->gl_spin);
676 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
677 acquired = 0;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400678 else {
679 gl->gl_owner = current;
680 gl->gl_ip = (unsigned long)__builtin_return_address(0);
681 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 spin_unlock(&gl->gl_spin);
683
684 return acquired;
685}
686
687/**
688 * gfs2_glmutex_unlock - release a local lock on a glock
689 * @gl: the glock
690 *
691 */
692
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400693static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694{
695 spin_lock(&gl->gl_spin);
696 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400697 gl->gl_owner = NULL;
698 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699 run_queue(gl);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400700 BUG_ON(!spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701 spin_unlock(&gl->gl_spin);
702}
703
704/**
705 * handle_callback - add a demote request to a lock's queue
706 * @gl: the glock
707 * @state: the state the caller wants us to change to
708 *
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400709 * Note: This may fail sliently if we are out of memory.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 */
711
712static void handle_callback(struct gfs2_glock *gl, unsigned int state)
713{
714 struct gfs2_holder *gh, *new_gh = NULL;
715
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 spin_lock(&gl->gl_spin);
718
719 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
720 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
721 gl->gl_req_gh != gh) {
722 if (gh->gh_state != state)
723 gh->gh_state = LM_ST_UNLOCKED;
724 goto out;
725 }
726 }
727
728 if (new_gh) {
729 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
730 new_gh = NULL;
731 } else {
732 spin_unlock(&gl->gl_spin);
733
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400734 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
735 if (!new_gh)
736 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
738 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
739
740 goto restart;
741 }
742
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400743out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 spin_unlock(&gl->gl_spin);
745
746 if (new_gh)
747 gfs2_holder_put(new_gh);
748}
749
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400750void gfs2_glock_inode_squish(struct inode *inode)
751{
752 struct gfs2_holder gh;
753 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
754 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
755 set_bit(HIF_DEMOTE, &gh.gh_iflags);
756 spin_lock(&gl->gl_spin);
757 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
758 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
759 run_queue(gl);
760 spin_unlock(&gl->gl_spin);
Steven Whitehouse5dd9fea2006-07-28 14:52:33 -0400761 wait_for_completion(&gh.gh_wait);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400762 gfs2_holder_uninit(&gh);
763}
764
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765/**
766 * state_change - record that the glock is now in a different state
767 * @gl: the glock
768 * @new_state the new state
769 *
770 */
771
772static void state_change(struct gfs2_glock *gl, unsigned int new_state)
773{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 int held1, held2;
775
776 held1 = (gl->gl_state != LM_ST_UNLOCKED);
777 held2 = (new_state != LM_ST_UNLOCKED);
778
779 if (held1 != held2) {
David Teigland6a6b3d02006-02-23 10:11:47 +0000780 if (held2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 gfs2_glock_hold(gl);
David Teigland6a6b3d02006-02-23 10:11:47 +0000782 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 }
785
786 gl->gl_state = new_state;
787}
788
789/**
790 * xmote_bh - Called after the lock module is done acquiring a lock
791 * @gl: The glock in question
792 * @ret: the int returned from the lock module
793 *
794 */
795
796static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
797{
798 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400799 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 struct gfs2_holder *gh = gl->gl_req_gh;
801 int prev_state = gl->gl_state;
802 int op_done = 1;
803
804 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
805 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
806 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
807
808 state_change(gl, ret & LM_OUT_ST_MASK);
809
810 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
811 if (glops->go_inval)
812 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
813 } else if (gl->gl_state == LM_ST_DEFERRED) {
814 /* We might not want to do this here.
815 Look at moving to the inode glops. */
816 if (glops->go_inval)
817 glops->go_inval(gl, DIO_DATA);
818 }
819
820 /* Deal with each possible exit condition */
821
822 if (!gh)
823 gl->gl_stamp = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
825 spin_lock(&gl->gl_spin);
826 list_del_init(&gh->gh_list);
827 gh->gh_error = -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
830 spin_lock(&gl->gl_spin);
831 list_del_init(&gh->gh_list);
832 if (gl->gl_state == gh->gh_state ||
833 gl->gl_state == LM_ST_UNLOCKED)
834 gh->gh_error = 0;
835 else {
836 if (gfs2_assert_warn(sdp, gh->gh_flags &
837 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
838 fs_warn(sdp, "ret = 0x%.8X\n", ret);
839 gh->gh_error = GLR_TRYFAILED;
840 }
841 spin_unlock(&gl->gl_spin);
842
843 if (ret & LM_OUT_CANCELED)
Steven Whitehouse50299962006-09-04 09:49:55 -0400844 handle_callback(gl, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845
846 } else if (ret & LM_OUT_CANCELED) {
847 spin_lock(&gl->gl_spin);
848 list_del_init(&gh->gh_list);
849 gh->gh_error = GLR_CANCELED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 spin_unlock(&gl->gl_spin);
851
852 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
853 spin_lock(&gl->gl_spin);
854 list_move_tail(&gh->gh_list, &gl->gl_holders);
855 gh->gh_error = 0;
856 set_bit(HIF_HOLDER, &gh->gh_iflags);
857 spin_unlock(&gl->gl_spin);
858
859 set_bit(HIF_FIRST, &gh->gh_iflags);
860
861 op_done = 0;
862
863 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
864 spin_lock(&gl->gl_spin);
865 list_del_init(&gh->gh_list);
866 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 spin_unlock(&gl->gl_spin);
868
869 } else {
870 if (gfs2_assert_withdraw(sdp, 0) == -1)
871 fs_err(sdp, "ret = 0x%.8X\n", ret);
872 }
873
874 if (glops->go_xmote_bh)
875 glops->go_xmote_bh(gl);
876
877 if (op_done) {
878 spin_lock(&gl->gl_spin);
879 gl->gl_req_gh = NULL;
880 gl->gl_req_bh = NULL;
881 clear_bit(GLF_LOCK, &gl->gl_flags);
882 run_queue(gl);
883 spin_unlock(&gl->gl_spin);
884 }
885
886 gfs2_glock_put(gl);
887
888 if (gh) {
889 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
890 gfs2_holder_put(gh);
891 else
892 complete(&gh->gh_wait);
893 }
894}
895
896/**
897 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
898 * @gl: The glock in question
899 * @state: the requested state
900 * @flags: modifier flags to the lock call
901 *
902 */
903
904void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
905{
906 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400907 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
909 LM_FLAG_NOEXP | LM_FLAG_ANY |
910 LM_FLAG_PRIORITY);
911 unsigned int lck_ret;
912
913 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
914 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
915 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
916 gfs2_assert_warn(sdp, state != gl->gl_state);
917
Steven Whitehouse50299962006-09-04 09:49:55 -0400918 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
919 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920
921 gfs2_glock_hold(gl);
922 gl->gl_req_bh = xmote_bh;
923
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400924 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925
926 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
927 return;
928
929 if (lck_ret & LM_OUT_ASYNC)
930 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
931 else
932 xmote_bh(gl, lck_ret);
933}
934
935/**
936 * drop_bh - Called after a lock module unlock completes
937 * @gl: the glock
938 * @ret: the return status
939 *
940 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
941 * Doesn't drop the reference on the glock the top half took out
942 *
943 */
944
945static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
946{
947 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400948 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 struct gfs2_holder *gh = gl->gl_req_gh;
950
951 clear_bit(GLF_PREFETCH, &gl->gl_flags);
952
953 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
954 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
955 gfs2_assert_warn(sdp, !ret);
956
957 state_change(gl, LM_ST_UNLOCKED);
958
959 if (glops->go_inval)
960 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
961
962 if (gh) {
963 spin_lock(&gl->gl_spin);
964 list_del_init(&gh->gh_list);
965 gh->gh_error = 0;
966 spin_unlock(&gl->gl_spin);
967 }
968
969 if (glops->go_drop_bh)
970 glops->go_drop_bh(gl);
971
972 spin_lock(&gl->gl_spin);
973 gl->gl_req_gh = NULL;
974 gl->gl_req_bh = NULL;
975 clear_bit(GLF_LOCK, &gl->gl_flags);
976 run_queue(gl);
977 spin_unlock(&gl->gl_spin);
978
979 gfs2_glock_put(gl);
980
981 if (gh) {
982 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
983 gfs2_holder_put(gh);
984 else
985 complete(&gh->gh_wait);
986 }
987}
988
989/**
990 * gfs2_glock_drop_th - call into the lock module to unlock a lock
991 * @gl: the glock
992 *
993 */
994
995void gfs2_glock_drop_th(struct gfs2_glock *gl)
996{
997 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400998 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 unsigned int ret;
1000
1001 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1002 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1003 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1004
Steven Whitehouse50299962006-09-04 09:49:55 -04001005 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1006 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007
1008 gfs2_glock_hold(gl);
1009 gl->gl_req_bh = drop_bh;
1010
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1012
1013 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1014 return;
1015
1016 if (!ret)
1017 drop_bh(gl, ret);
1018 else
1019 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1020}
1021
1022/**
1023 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1024 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1025 *
1026 * Don't cancel GL_NOCANCEL requests.
1027 */
1028
1029static void do_cancels(struct gfs2_holder *gh)
1030{
1031 struct gfs2_glock *gl = gh->gh_gl;
1032
1033 spin_lock(&gl->gl_spin);
1034
1035 while (gl->gl_req_gh != gh &&
1036 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1037 !list_empty(&gh->gh_list)) {
Steven Whitehouse50299962006-09-04 09:49:55 -04001038 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1039 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 spin_unlock(&gl->gl_spin);
1041 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1042 msleep(100);
1043 spin_lock(&gl->gl_spin);
1044 } else {
1045 spin_unlock(&gl->gl_spin);
1046 msleep(100);
1047 spin_lock(&gl->gl_spin);
1048 }
1049 }
1050
1051 spin_unlock(&gl->gl_spin);
1052}
1053
1054/**
1055 * glock_wait_internal - wait on a glock acquisition
1056 * @gh: the glock holder
1057 *
1058 * Returns: 0 on success
1059 */
1060
1061static int glock_wait_internal(struct gfs2_holder *gh)
1062{
1063 struct gfs2_glock *gl = gh->gh_gl;
1064 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001065 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066
1067 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1068 return -EIO;
1069
1070 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1071 spin_lock(&gl->gl_spin);
1072 if (gl->gl_req_gh != gh &&
1073 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1074 !list_empty(&gh->gh_list)) {
1075 list_del_init(&gh->gh_list);
1076 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 run_queue(gl);
1078 spin_unlock(&gl->gl_spin);
1079 return gh->gh_error;
1080 }
1081 spin_unlock(&gl->gl_spin);
1082 }
1083
1084 if (gh->gh_flags & LM_FLAG_PRIORITY)
1085 do_cancels(gh);
1086
1087 wait_for_completion(&gh->gh_wait);
1088
1089 if (gh->gh_error)
1090 return gh->gh_error;
1091
1092 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1093 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state,
1094 gh->gh_state,
1095 gh->gh_flags));
1096
1097 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1098 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1099
1100 if (glops->go_lock) {
1101 gh->gh_error = glops->go_lock(gh);
1102 if (gh->gh_error) {
1103 spin_lock(&gl->gl_spin);
1104 list_del_init(&gh->gh_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 spin_unlock(&gl->gl_spin);
1106 }
1107 }
1108
1109 spin_lock(&gl->gl_spin);
1110 gl->gl_req_gh = NULL;
1111 gl->gl_req_bh = NULL;
1112 clear_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 run_queue(gl);
1114 spin_unlock(&gl->gl_spin);
1115 }
1116
1117 return gh->gh_error;
1118}
1119
1120static inline struct gfs2_holder *
1121find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1122{
1123 struct gfs2_holder *gh;
1124
1125 list_for_each_entry(gh, head, gh_list) {
1126 if (gh->gh_owner == owner)
1127 return gh;
1128 }
1129
1130 return NULL;
1131}
1132
1133/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1135 * @gh: the holder structure to add
1136 *
1137 */
1138
1139static void add_to_queue(struct gfs2_holder *gh)
1140{
1141 struct gfs2_glock *gl = gh->gh_gl;
1142 struct gfs2_holder *existing;
1143
Steven Whitehouse190562b2006-04-20 16:57:23 -04001144 BUG_ON(!gh->gh_owner);
1145
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1147 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001148 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001149 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1150 printk(KERN_INFO "lock type : %d lock state : %d\n",
1151 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001152 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001153 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1154 printk(KERN_INFO "lock type : %d lock state : %d\n",
1155 gl->gl_name.ln_type, gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001156 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 }
1158
1159 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1160 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001161 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1162 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1163 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164 }
1165
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 if (gh->gh_flags & LM_FLAG_PRIORITY)
1167 list_add(&gh->gh_list, &gl->gl_waiters3);
1168 else
1169 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1170}
1171
1172/**
1173 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1174 * @gh: the holder structure
1175 *
1176 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1177 *
1178 * Returns: 0, GLR_TRYFAILED, or errno on failure
1179 */
1180
1181int gfs2_glock_nq(struct gfs2_holder *gh)
1182{
1183 struct gfs2_glock *gl = gh->gh_gl;
1184 struct gfs2_sbd *sdp = gl->gl_sbd;
1185 int error = 0;
1186
Steven Whitehouse320dd102006-05-18 16:25:27 -04001187restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1189 set_bit(HIF_ABORTED, &gh->gh_iflags);
1190 return -EIO;
1191 }
1192
1193 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1194
1195 spin_lock(&gl->gl_spin);
1196 add_to_queue(gh);
1197 run_queue(gl);
1198 spin_unlock(&gl->gl_spin);
1199
1200 if (!(gh->gh_flags & GL_ASYNC)) {
1201 error = glock_wait_internal(gh);
1202 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001203 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 goto restart;
1205 }
1206 }
1207
1208 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1209
Steven Whitehouse320dd102006-05-18 16:25:27 -04001210 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1211 dump_glock(gl);
1212
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 return error;
1214}
1215
1216/**
1217 * gfs2_glock_poll - poll to see if an async request has been completed
1218 * @gh: the holder
1219 *
1220 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1221 */
1222
1223int gfs2_glock_poll(struct gfs2_holder *gh)
1224{
1225 struct gfs2_glock *gl = gh->gh_gl;
1226 int ready = 0;
1227
1228 spin_lock(&gl->gl_spin);
1229
1230 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1231 ready = 1;
1232 else if (list_empty(&gh->gh_list)) {
1233 if (gh->gh_error == GLR_CANCELED) {
1234 spin_unlock(&gl->gl_spin);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001235 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236 if (gfs2_glock_nq(gh))
1237 return 1;
1238 return 0;
1239 } else
1240 ready = 1;
1241 }
1242
1243 spin_unlock(&gl->gl_spin);
1244
1245 return ready;
1246}
1247
1248/**
1249 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1250 * @gh: the holder structure
1251 *
1252 * Returns: 0, GLR_TRYFAILED, or errno on failure
1253 */
1254
1255int gfs2_glock_wait(struct gfs2_holder *gh)
1256{
1257 int error;
1258
1259 error = glock_wait_internal(gh);
1260 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001261 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001262 gh->gh_flags &= ~GL_ASYNC;
1263 error = gfs2_glock_nq(gh);
1264 }
1265
1266 return error;
1267}
1268
1269/**
1270 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1271 * @gh: the glock holder
1272 *
1273 */
1274
1275void gfs2_glock_dq(struct gfs2_holder *gh)
1276{
1277 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001278 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280 if (gh->gh_flags & GL_NOCACHE)
1281 handle_callback(gl, LM_ST_UNLOCKED);
1282
1283 gfs2_glmutex_lock(gl);
1284
1285 spin_lock(&gl->gl_spin);
1286 list_del_init(&gh->gh_list);
1287
1288 if (list_empty(&gl->gl_holders)) {
1289 spin_unlock(&gl->gl_spin);
1290
1291 if (glops->go_unlock)
1292 glops->go_unlock(gh);
1293
David Teiglandb3b94fa2006-01-16 16:50:04 +00001294 gl->gl_stamp = jiffies;
1295
1296 spin_lock(&gl->gl_spin);
1297 }
1298
1299 clear_bit(GLF_LOCK, &gl->gl_flags);
1300 run_queue(gl);
1301 spin_unlock(&gl->gl_spin);
1302}
1303
1304/**
1305 * gfs2_glock_prefetch - Try to prefetch a glock
1306 * @gl: the glock
1307 * @state: the state to prefetch in
1308 * @flags: flags passed to go_xmote_th()
1309 *
1310 */
1311
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001312static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1313 int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314{
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001315 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001316
1317 spin_lock(&gl->gl_spin);
1318
Steven Whitehouse50299962006-09-04 09:49:55 -04001319 if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
1320 !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321 !list_empty(&gl->gl_waiters3) ||
1322 relaxed_state_ok(gl->gl_state, state, flags)) {
1323 spin_unlock(&gl->gl_spin);
1324 return;
1325 }
1326
1327 set_bit(GLF_PREFETCH, &gl->gl_flags);
1328 set_bit(GLF_LOCK, &gl->gl_flags);
1329 spin_unlock(&gl->gl_spin);
1330
1331 glops->go_xmote_th(gl, state, flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332}
1333
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334static void greedy_work(void *data)
1335{
David Teiglande7f5c012006-04-27 11:25:45 -04001336 struct greedy *gr = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337 struct gfs2_holder *gh = &gr->gr_gh;
1338 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001339 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340
1341 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1342
1343 if (glops->go_greedy)
1344 glops->go_greedy(gl);
1345
1346 spin_lock(&gl->gl_spin);
1347
1348 if (list_empty(&gl->gl_waiters2)) {
1349 clear_bit(GLF_GREEDY, &gl->gl_flags);
1350 spin_unlock(&gl->gl_spin);
1351 gfs2_holder_uninit(gh);
1352 kfree(gr);
1353 } else {
1354 gfs2_glock_hold(gl);
1355 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1356 run_queue(gl);
1357 spin_unlock(&gl->gl_spin);
1358 gfs2_glock_put(gl);
1359 }
1360}
1361
1362/**
1363 * gfs2_glock_be_greedy -
1364 * @gl:
1365 * @time:
1366 *
1367 * Returns: 0 if go_greedy will be called, 1 otherwise
1368 */
1369
1370int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1371{
1372 struct greedy *gr;
1373 struct gfs2_holder *gh;
1374
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001375 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1377 return 1;
1378
1379 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1380 if (!gr) {
1381 clear_bit(GLF_GREEDY, &gl->gl_flags);
1382 return 1;
1383 }
1384 gh = &gr->gr_gh;
1385
Steven Whitehouse579b78a2006-04-26 14:58:26 -04001386 gfs2_holder_init(gl, 0, 0, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 set_bit(HIF_GREEDY, &gh->gh_iflags);
1388 INIT_WORK(&gr->gr_work, greedy_work, gr);
1389
1390 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1391 schedule_delayed_work(&gr->gr_work, time);
1392
1393 return 0;
1394}
1395
1396/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1398 * @gh: the holder structure
1399 *
1400 */
1401
1402void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1403{
1404 gfs2_glock_dq(gh);
1405 gfs2_holder_uninit(gh);
1406}
1407
1408/**
1409 * gfs2_glock_nq_num - acquire a glock based on lock number
1410 * @sdp: the filesystem
1411 * @number: the lock number
1412 * @glops: the glock operations for the type of glock
1413 * @state: the state to acquire the glock in
1414 * @flags: modifier flags for the aquisition
1415 * @gh: the struct gfs2_holder
1416 *
1417 * Returns: errno
1418 */
1419
Steven Whitehousecd915492006-09-04 12:49:07 -04001420int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001421 const struct gfs2_glock_operations *glops,
1422 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423{
1424 struct gfs2_glock *gl;
1425 int error;
1426
1427 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1428 if (!error) {
1429 error = gfs2_glock_nq_init(gl, state, flags, gh);
1430 gfs2_glock_put(gl);
1431 }
1432
1433 return error;
1434}
1435
1436/**
1437 * glock_compare - Compare two struct gfs2_glock structures for sorting
1438 * @arg_a: the first structure
1439 * @arg_b: the second structure
1440 *
1441 */
1442
1443static int glock_compare(const void *arg_a, const void *arg_b)
1444{
1445 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1446 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1447 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1448 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1449 int ret = 0;
1450
1451 if (a->ln_number > b->ln_number)
1452 ret = 1;
1453 else if (a->ln_number < b->ln_number)
1454 ret = -1;
1455 else {
1456 if (gh_a->gh_state == LM_ST_SHARED &&
1457 gh_b->gh_state == LM_ST_EXCLUSIVE)
1458 ret = 1;
1459 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1460 (gh_b->gh_flags & GL_LOCAL_EXCL))
1461 ret = 1;
1462 }
1463
1464 return ret;
1465}
1466
1467/**
1468 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1469 * @num_gh: the number of structures
1470 * @ghs: an array of struct gfs2_holder structures
1471 *
1472 * Returns: 0 on success (all glocks acquired),
1473 * errno on failure (no glocks acquired)
1474 */
1475
1476static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1477 struct gfs2_holder **p)
1478{
1479 unsigned int x;
1480 int error = 0;
1481
1482 for (x = 0; x < num_gh; x++)
1483 p[x] = &ghs[x];
1484
1485 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1486
1487 for (x = 0; x < num_gh; x++) {
1488 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1489
1490 error = gfs2_glock_nq(p[x]);
1491 if (error) {
1492 while (x--)
1493 gfs2_glock_dq(p[x]);
1494 break;
1495 }
1496 }
1497
1498 return error;
1499}
1500
1501/**
1502 * gfs2_glock_nq_m - acquire multiple glocks
1503 * @num_gh: the number of structures
1504 * @ghs: an array of struct gfs2_holder structures
1505 *
1506 * Figure out how big an impact this function has. Either:
1507 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1508 * 2) Forget async stuff and just call nq_m_sync()
1509 * 3) Leave it like it is
1510 *
1511 * Returns: 0 on success (all glocks acquired),
1512 * errno on failure (no glocks acquired)
1513 */
1514
1515int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1516{
1517 int *e;
1518 unsigned int x;
1519 int borked = 0, serious = 0;
1520 int error = 0;
1521
1522 if (!num_gh)
1523 return 0;
1524
1525 if (num_gh == 1) {
1526 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1527 return gfs2_glock_nq(ghs);
1528 }
1529
1530 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1531 if (!e)
1532 return -ENOMEM;
1533
1534 for (x = 0; x < num_gh; x++) {
1535 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1536 error = gfs2_glock_nq(&ghs[x]);
1537 if (error) {
1538 borked = 1;
1539 serious = error;
1540 num_gh = x;
1541 break;
1542 }
1543 }
1544
1545 for (x = 0; x < num_gh; x++) {
1546 error = e[x] = glock_wait_internal(&ghs[x]);
1547 if (error) {
1548 borked = 1;
1549 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1550 serious = error;
1551 }
1552 }
1553
1554 if (!borked) {
1555 kfree(e);
1556 return 0;
1557 }
1558
1559 for (x = 0; x < num_gh; x++)
1560 if (!e[x])
1561 gfs2_glock_dq(&ghs[x]);
1562
1563 if (serious)
1564 error = serious;
1565 else {
1566 for (x = 0; x < num_gh; x++)
1567 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1568 &ghs[x]);
1569 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1570 }
1571
1572 kfree(e);
1573
1574 return error;
1575}
1576
1577/**
1578 * gfs2_glock_dq_m - release multiple glocks
1579 * @num_gh: the number of structures
1580 * @ghs: an array of struct gfs2_holder structures
1581 *
1582 */
1583
1584void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1585{
1586 unsigned int x;
1587
1588 for (x = 0; x < num_gh; x++)
1589 gfs2_glock_dq(&ghs[x]);
1590}
1591
1592/**
1593 * gfs2_glock_dq_uninit_m - release multiple glocks
1594 * @num_gh: the number of structures
1595 * @ghs: an array of struct gfs2_holder structures
1596 *
1597 */
1598
1599void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1600{
1601 unsigned int x;
1602
1603 for (x = 0; x < num_gh; x++)
1604 gfs2_glock_dq_uninit(&ghs[x]);
1605}
1606
1607/**
1608 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1609 * @sdp: the filesystem
1610 * @number: the lock number
1611 * @glops: the glock operations for the type of glock
1612 * @state: the state to acquire the glock in
1613 * @flags: modifier flags for the aquisition
1614 *
1615 * Returns: errno
1616 */
1617
Steven Whitehousecd915492006-09-04 12:49:07 -04001618void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001619 const struct gfs2_glock_operations *glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001620 unsigned int state, int flags)
1621{
1622 struct gfs2_glock *gl;
1623 int error;
1624
1625 if (atomic_read(&sdp->sd_reclaim_count) <
1626 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1627 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1628 if (!error) {
1629 gfs2_glock_prefetch(gl, state, flags);
1630 gfs2_glock_put(gl);
1631 }
1632 }
1633}
1634
1635/**
1636 * gfs2_lvb_hold - attach a LVB from a glock
1637 * @gl: The glock in question
1638 *
1639 */
1640
1641int gfs2_lvb_hold(struct gfs2_glock *gl)
1642{
1643 int error;
1644
1645 gfs2_glmutex_lock(gl);
1646
1647 if (!atomic_read(&gl->gl_lvb_count)) {
1648 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1649 if (error) {
1650 gfs2_glmutex_unlock(gl);
1651 return error;
1652 }
1653 gfs2_glock_hold(gl);
1654 }
1655 atomic_inc(&gl->gl_lvb_count);
1656
1657 gfs2_glmutex_unlock(gl);
1658
1659 return 0;
1660}
1661
1662/**
1663 * gfs2_lvb_unhold - detach a LVB from a glock
1664 * @gl: The glock in question
1665 *
1666 */
1667
1668void gfs2_lvb_unhold(struct gfs2_glock *gl)
1669{
1670 gfs2_glock_hold(gl);
1671 gfs2_glmutex_lock(gl);
1672
1673 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1674 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1675 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1676 gl->gl_lvb = NULL;
1677 gfs2_glock_put(gl);
1678 }
1679
1680 gfs2_glmutex_unlock(gl);
1681 gfs2_glock_put(gl);
1682}
1683
David Teiglandb3b94fa2006-01-16 16:50:04 +00001684static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1685 unsigned int state)
1686{
1687 struct gfs2_glock *gl;
1688
1689 gl = gfs2_glock_find(sdp, name);
1690 if (!gl)
1691 return;
1692
1693 if (gl->gl_ops->go_callback)
1694 gl->gl_ops->go_callback(gl, state);
1695 handle_callback(gl, state);
1696
1697 spin_lock(&gl->gl_spin);
1698 run_queue(gl);
1699 spin_unlock(&gl->gl_spin);
1700
1701 gfs2_glock_put(gl);
1702}
1703
1704/**
1705 * gfs2_glock_cb - Callback used by locking module
1706 * @fsdata: Pointer to the superblock
1707 * @type: Type of callback
1708 * @data: Type dependent data pointer
1709 *
1710 * Called by the locking module when it wants to tell us something.
1711 * Either we need to drop a lock, one of our ASYNC requests completed, or
1712 * a journal from another client needs to be recovered.
1713 */
1714
1715void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data)
1716{
1717 struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata;
1718
David Teiglandb3b94fa2006-01-16 16:50:04 +00001719 switch (type) {
1720 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001721 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722 return;
1723
1724 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001725 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001726 return;
1727
1728 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001729 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001730 return;
1731
1732 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001733 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001734 struct gfs2_glock *gl;
1735
1736 gl = gfs2_glock_find(sdp, &async->lc_name);
1737 if (gfs2_assert_warn(sdp, gl))
1738 return;
1739 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1740 gl->gl_req_bh(gl, async->lc_ret);
1741 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001742 return;
1743 }
1744
1745 case LM_CB_NEED_RECOVERY:
1746 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1747 if (sdp->sd_recoverd_process)
1748 wake_up_process(sdp->sd_recoverd_process);
1749 return;
1750
1751 case LM_CB_DROPLOCKS:
1752 gfs2_gl_hash_clear(sdp, NO_WAIT);
1753 gfs2_quota_scan(sdp);
1754 return;
1755
1756 default:
1757 gfs2_assert_warn(sdp, 0);
1758 return;
1759 }
1760}
1761
1762/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001763 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1764 * iopen glock from memory
1765 * @io_gl: the iopen glock
1766 * @state: the state into which the glock should be put
1767 *
1768 */
1769
1770void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1771{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001772
1773 if (state != LM_ST_UNLOCKED)
1774 return;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001775 /* FIXME: remove this? */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001776}
1777
1778/**
1779 * demote_ok - Check to see if it's ok to unlock a glock
1780 * @gl: the glock
1781 *
1782 * Returns: 1 if it's ok
1783 */
1784
1785static int demote_ok(struct gfs2_glock *gl)
1786{
1787 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001788 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001789 int demote = 1;
1790
1791 if (test_bit(GLF_STICKY, &gl->gl_flags))
1792 demote = 0;
1793 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
Steven Whitehouse50299962006-09-04 09:49:55 -04001794 demote = time_after_eq(jiffies, gl->gl_stamp +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001795 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1796 else if (glops->go_demote_ok)
1797 demote = glops->go_demote_ok(gl);
1798
1799 return demote;
1800}
1801
1802/**
1803 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1804 * @gl: the glock
1805 *
1806 */
1807
1808void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1809{
1810 struct gfs2_sbd *sdp = gl->gl_sbd;
1811
1812 spin_lock(&sdp->sd_reclaim_lock);
1813 if (list_empty(&gl->gl_reclaim)) {
1814 gfs2_glock_hold(gl);
1815 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1816 atomic_inc(&sdp->sd_reclaim_count);
1817 }
1818 spin_unlock(&sdp->sd_reclaim_lock);
1819
1820 wake_up(&sdp->sd_reclaim_wq);
1821}
1822
1823/**
1824 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1825 * @sdp: the filesystem
1826 *
1827 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1828 * different glock and we notice that there are a lot of glocks in the
1829 * reclaim list.
1830 *
1831 */
1832
1833void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1834{
1835 struct gfs2_glock *gl;
1836
1837 spin_lock(&sdp->sd_reclaim_lock);
1838 if (list_empty(&sdp->sd_reclaim_list)) {
1839 spin_unlock(&sdp->sd_reclaim_lock);
1840 return;
1841 }
1842 gl = list_entry(sdp->sd_reclaim_list.next,
1843 struct gfs2_glock, gl_reclaim);
1844 list_del_init(&gl->gl_reclaim);
1845 spin_unlock(&sdp->sd_reclaim_lock);
1846
1847 atomic_dec(&sdp->sd_reclaim_count);
1848 atomic_inc(&sdp->sd_reclaimed);
1849
1850 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001851 if (queue_empty(gl, &gl->gl_holders) &&
Steven Whitehouse50299962006-09-04 09:49:55 -04001852 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001853 handle_callback(gl, LM_ST_UNLOCKED);
1854 gfs2_glmutex_unlock(gl);
1855 }
1856
1857 gfs2_glock_put(gl);
1858}
1859
1860/**
1861 * examine_bucket - Call a function for glock in a hash bucket
1862 * @examiner: the function
1863 * @sdp: the filesystem
1864 * @bucket: the bucket
1865 *
1866 * Returns: 1 if the bucket has entries
1867 */
1868
1869static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1870 struct gfs2_gl_hash_bucket *bucket)
1871{
1872 struct glock_plug plug;
1873 struct list_head *tmp;
1874 struct gfs2_glock *gl;
1875 int entries;
1876
1877 /* Add "plug" to end of bucket list, work back up list from there */
1878 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1879 set_bit(GLF_PLUG, &plug.gl_flags);
1880
1881 write_lock(&bucket->hb_lock);
1882 list_add(&plug.gl_list, &bucket->hb_list);
1883 write_unlock(&bucket->hb_lock);
1884
1885 for (;;) {
1886 write_lock(&bucket->hb_lock);
1887
1888 for (;;) {
1889 tmp = plug.gl_list.next;
1890
1891 if (tmp == &bucket->hb_list) {
1892 list_del(&plug.gl_list);
1893 entries = !list_empty(&bucket->hb_list);
1894 write_unlock(&bucket->hb_lock);
1895 return entries;
1896 }
1897 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1898
1899 /* Move plug up list */
1900 list_move(&plug.gl_list, &gl->gl_list);
1901
1902 if (test_bit(GLF_PLUG, &gl->gl_flags))
1903 continue;
1904
1905 /* examiner() must glock_put() */
1906 gfs2_glock_hold(gl);
1907
1908 break;
1909 }
1910
1911 write_unlock(&bucket->hb_lock);
1912
1913 examiner(gl);
1914 }
1915}
1916
1917/**
1918 * scan_glock - look at a glock and see if we can reclaim it
1919 * @gl: the glock to look at
1920 *
1921 */
1922
1923static void scan_glock(struct gfs2_glock *gl)
1924{
Steven Whitehousea2242db2006-08-24 17:03:05 -04001925 if (gl->gl_ops == &gfs2_inode_glops)
1926 goto out;
1927
David Teiglandb3b94fa2006-01-16 16:50:04 +00001928 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001929 if (queue_empty(gl, &gl->gl_holders) &&
1930 gl->gl_state != LM_ST_UNLOCKED &&
1931 demote_ok(gl))
1932 goto out_schedule;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001933 gfs2_glmutex_unlock(gl);
1934 }
Steven Whitehousea2242db2006-08-24 17:03:05 -04001935out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001936 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001937 return;
1938
Steven Whitehouse627add22006-07-05 13:16:19 -04001939out_schedule:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 gfs2_glmutex_unlock(gl);
1941 gfs2_glock_schedule_for_reclaim(gl);
1942 gfs2_glock_put(gl);
1943}
1944
1945/**
1946 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1947 * @sdp: the filesystem
1948 *
1949 */
1950
1951void gfs2_scand_internal(struct gfs2_sbd *sdp)
1952{
1953 unsigned int x;
1954
1955 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1956 examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]);
1957 cond_resched();
1958 }
1959}
1960
1961/**
1962 * clear_glock - look at a glock and see if we can free it from glock cache
1963 * @gl: the glock to look at
1964 *
1965 */
1966
1967static void clear_glock(struct gfs2_glock *gl)
1968{
1969 struct gfs2_sbd *sdp = gl->gl_sbd;
1970 int released;
1971
1972 spin_lock(&sdp->sd_reclaim_lock);
1973 if (!list_empty(&gl->gl_reclaim)) {
1974 list_del_init(&gl->gl_reclaim);
1975 atomic_dec(&sdp->sd_reclaim_count);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001976 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001977 released = gfs2_glock_put(gl);
1978 gfs2_assert(sdp, !released);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001979 } else {
1980 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001981 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001982
1983 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001984 if (queue_empty(gl, &gl->gl_holders) &&
1985 gl->gl_state != LM_ST_UNLOCKED)
1986 handle_callback(gl, LM_ST_UNLOCKED);
1987
1988 gfs2_glmutex_unlock(gl);
1989 }
1990
1991 gfs2_glock_put(gl);
1992}
1993
1994/**
1995 * gfs2_gl_hash_clear - Empty out the glock hash table
1996 * @sdp: the filesystem
1997 * @wait: wait until it's all gone
1998 *
1999 * Called when unmounting the filesystem, or when inter-node lock manager
2000 * requests DROPLOCKS because it is running out of capacity.
2001 */
2002
2003void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2004{
2005 unsigned long t;
2006 unsigned int x;
2007 int cont;
2008
2009 t = jiffies;
2010
2011 for (;;) {
2012 cont = 0;
2013
2014 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
Steven Whitehouse50299962006-09-04 09:49:55 -04002015 if (examine_bucket(clear_glock, sdp, &sdp->sd_gl_hash[x]))
David Teiglandb3b94fa2006-01-16 16:50:04 +00002016 cont = 1;
2017
2018 if (!wait || !cont)
2019 break;
2020
2021 if (time_after_eq(jiffies,
2022 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2023 fs_warn(sdp, "Unmount seems to be stalled. "
2024 "Dumping lock state...\n");
2025 gfs2_dump_lockstate(sdp);
2026 t = jiffies;
2027 }
2028
David Teiglandb3b94fa2006-01-16 16:50:04 +00002029 invalidate_inodes(sdp->sd_vfs);
Steven Whitehousefd88de562006-05-05 16:59:11 -04002030 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002031 }
2032}
2033
2034/*
2035 * Diagnostic routines to help debug distributed deadlock
2036 */
2037
2038/**
2039 * dump_holder - print information about a glock holder
2040 * @str: a string naming the type of holder
2041 * @gh: the glock holder
2042 *
2043 * Returns: 0 on success, -ENOBUFS when we run out of space
2044 */
2045
2046static int dump_holder(char *str, struct gfs2_holder *gh)
2047{
2048 unsigned int x;
2049 int error = -ENOBUFS;
2050
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002051 printk(KERN_INFO " %s\n", str);
2052 printk(KERN_INFO " owner = %ld\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002053 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002054 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2055 printk(KERN_INFO " gh_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002056 for (x = 0; x < 32; x++)
2057 if (gh->gh_flags & (1 << x))
2058 printk(" %u", x);
2059 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002060 printk(KERN_INFO " error = %d\n", gh->gh_error);
2061 printk(KERN_INFO " gh_iflags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002062 for (x = 0; x < 32; x++)
2063 if (test_bit(x, &gh->gh_iflags))
2064 printk(" %u", x);
2065 printk(" \n");
Steven Whitehoused0dc80d2006-03-29 14:36:49 -05002066 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002067
2068 error = 0;
2069
2070 return error;
2071}
2072
2073/**
2074 * dump_inode - print information about an inode
2075 * @ip: the inode
2076 *
2077 * Returns: 0 on success, -ENOBUFS when we run out of space
2078 */
2079
2080static int dump_inode(struct gfs2_inode *ip)
2081{
2082 unsigned int x;
2083 int error = -ENOBUFS;
2084
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002085 printk(KERN_INFO " Inode:\n");
2086 printk(KERN_INFO " num = %llu %llu\n",
Steven Whitehouse382066d2006-05-24 10:22:09 -04002087 (unsigned long long)ip->i_num.no_formal_ino,
2088 (unsigned long long)ip->i_num.no_addr);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002089 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002090 printk(KERN_INFO " i_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002091 for (x = 0; x < 32; x++)
2092 if (test_bit(x, &ip->i_flags))
2093 printk(" %u", x);
2094 printk(" \n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002095
2096 error = 0;
2097
2098 return error;
2099}
2100
2101/**
2102 * dump_glock - print information about a glock
2103 * @gl: the glock
2104 * @count: where we are in the buffer
2105 *
2106 * Returns: 0 on success, -ENOBUFS when we run out of space
2107 */
2108
2109static int dump_glock(struct gfs2_glock *gl)
2110{
2111 struct gfs2_holder *gh;
2112 unsigned int x;
2113 int error = -ENOBUFS;
2114
2115 spin_lock(&gl->gl_spin);
2116
Russell Cattelan88721872006-08-10 11:08:40 -05002117 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n",
2118 gl,
2119 gl->gl_name.ln_type,
Steven Whitehouse382066d2006-05-24 10:22:09 -04002120 (unsigned long long)gl->gl_name.ln_number);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002121 printk(KERN_INFO " gl_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002122 for (x = 0; x < 32; x++)
2123 if (test_bit(x, &gl->gl_flags))
2124 printk(" %u", x);
2125 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002126 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2127 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
Steven Whitehouse320dd102006-05-18 16:25:27 -04002128 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2129 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002130 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2131 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2132 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2133 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2134 printk(KERN_INFO " le = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002135 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002136 printk(KERN_INFO " reclaim = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002137 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2138 if (gl->gl_aspace)
Russell Cattelan88721872006-08-10 11:08:40 -05002139 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n",
2140 gl->gl_aspace,
2141 gl->gl_aspace->i_mapping->nrpages);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002142 else
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002143 printk(KERN_INFO " aspace = no\n");
2144 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002145 if (gl->gl_req_gh) {
2146 error = dump_holder("Request", gl->gl_req_gh);
2147 if (error)
2148 goto out;
2149 }
2150 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2151 error = dump_holder("Holder", gh);
2152 if (error)
2153 goto out;
2154 }
2155 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2156 error = dump_holder("Waiter1", gh);
2157 if (error)
2158 goto out;
2159 }
2160 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2161 error = dump_holder("Waiter2", gh);
2162 if (error)
2163 goto out;
2164 }
2165 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2166 error = dump_holder("Waiter3", gh);
2167 if (error)
2168 goto out;
2169 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002170 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002171 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2172 list_empty(&gl->gl_holders)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002173 error = dump_inode(gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002174 if (error)
2175 goto out;
2176 } else {
2177 error = -ENOBUFS;
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002178 printk(KERN_INFO " Inode: busy\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002179 }
2180 }
2181
2182 error = 0;
2183
Steven Whitehousea91ea692006-09-04 12:04:26 -04002184out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002185 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002186 return error;
2187}
2188
2189/**
2190 * gfs2_dump_lockstate - print out the current lockstate
2191 * @sdp: the filesystem
2192 * @ub: the buffer to copy the information into
2193 *
2194 * If @ub is NULL, dump the lockstate to the console.
2195 *
2196 */
2197
Adrian Bunk08bc2db2006-04-28 10:59:12 -04002198static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002199{
2200 struct gfs2_gl_hash_bucket *bucket;
2201 struct gfs2_glock *gl;
2202 unsigned int x;
2203 int error = 0;
2204
2205 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2206 bucket = &sdp->sd_gl_hash[x];
2207
2208 read_lock(&bucket->hb_lock);
2209
2210 list_for_each_entry(gl, &bucket->hb_list, gl_list) {
2211 if (test_bit(GLF_PLUG, &gl->gl_flags))
2212 continue;
2213
2214 error = dump_glock(gl);
2215 if (error)
2216 break;
2217 }
2218
2219 read_unlock(&bucket->hb_lock);
2220
2221 if (error)
2222 break;
2223 }
2224
2225
2226 return error;
2227}
2228