blob: 49512696160e0a2760f3f8bcd17211c6e3cf88a7 [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
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040047struct gfs2_gl_hash_bucket {
48 struct list_head hb_list;
49};
50
David Teiglandb3b94fa2006-01-16 16:50:04 +000051typedef void (*glock_examiner) (struct gfs2_glock * gl);
52
Adrian Bunk08bc2db2006-04-28 10:59:12 -040053static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
Steven Whitehouse320dd102006-05-18 16:25:27 -040054static int dump_glock(struct gfs2_glock *gl);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040055
Steven Whitehouse85d1da62006-09-07 14:40:21 -040056static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040057static rwlock_t gl_hash_locks[GFS2_GL_HASH_SIZE];
Steven Whitehouse85d1da62006-09-07 14:40:21 -040058
David Teiglandb3b94fa2006-01-16 16:50:04 +000059/**
60 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
61 * @actual: the current state of the lock
62 * @requested: the lock state that was requested by the caller
63 * @flags: the modifier flags passed in by the caller
64 *
65 * Returns: 1 if the locks are compatible, 0 otherwise
66 */
67
68static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
69 int flags)
70{
71 if (actual == requested)
72 return 1;
73
74 if (flags & GL_EXACT)
75 return 0;
76
77 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
78 return 1;
79
80 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
81 return 1;
82
83 return 0;
84}
85
86/**
87 * gl_hash() - Turn glock number into hash bucket number
88 * @lock: The glock number
89 *
90 * Returns: The number of the corresponding hash bucket
91 */
92
Steven Whitehouseb8547852006-09-07 13:12:27 -040093static unsigned int gl_hash(const struct gfs2_sbd *sdp,
94 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +000095{
96 unsigned int h;
97
Steven Whitehousecd915492006-09-04 12:49:07 -040098 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -0400100 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101 h &= GFS2_GL_HASH_MASK;
102
103 return h;
104}
105
106/**
107 * glock_free() - Perform a few checks and then release struct gfs2_glock
108 * @gl: The glock to release
109 *
110 * Also calls lock module to release its internal structure for this glock.
111 *
112 */
113
114static void glock_free(struct gfs2_glock *gl)
115{
116 struct gfs2_sbd *sdp = gl->gl_sbd;
117 struct inode *aspace = gl->gl_aspace;
118
119 gfs2_lm_put_lock(sdp, gl->gl_lock);
120
121 if (aspace)
122 gfs2_aspace_put(aspace);
123
124 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125}
126
127/**
128 * gfs2_glock_hold() - increment reference count on glock
129 * @gl: The glock to hold
130 *
131 */
132
133void gfs2_glock_hold(struct gfs2_glock *gl)
134{
135 kref_get(&gl->gl_ref);
136}
137
138/* All work is done after the return from kref_put() so we
139 can release the write_lock before the free. */
140
141static void kill_glock(struct kref *kref)
142{
143 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
144 struct gfs2_sbd *sdp = gl->gl_sbd;
145
146 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
147 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
148 gfs2_assert(sdp, list_empty(&gl->gl_holders));
149 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
150 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
151 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
152}
153
154/**
155 * gfs2_glock_put() - Decrement reference count on glock
156 * @gl: The glock to put
157 *
158 */
159
160int gfs2_glock_put(struct gfs2_glock *gl)
161{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 int rv = 0;
163
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400164 write_lock(&gl_hash_locks[gl->gl_hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165 if (kref_put(&gl->gl_ref, kill_glock)) {
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400166 list_del_init(&gl_hash_table[gl->gl_hash].hb_list);
167 write_unlock(&gl_hash_locks[gl->gl_hash]);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400168 BUG_ON(spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 glock_free(gl);
170 rv = 1;
171 goto out;
172 }
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400173 write_unlock(&gl_hash_locks[gl->gl_hash]);
Steven Whitehousea2242db2006-08-24 17:03:05 -0400174out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 return rv;
176}
177
178/**
179 * queue_empty - check to see if a glock's queue is empty
180 * @gl: the glock
181 * @head: the head of the queue to check
182 *
183 * This function protects the list in the event that a process already
184 * has a holder on the list and is adding a second holder for itself.
185 * The glmutex lock is what generally prevents processes from working
186 * on the same glock at once, but the special case of adding a second
187 * holder for yourself ("recursive" locking) doesn't involve locking
188 * glmutex, making the spin lock necessary.
189 *
190 * Returns: 1 if the queue is empty
191 */
192
193static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
194{
195 int empty;
196 spin_lock(&gl->gl_spin);
197 empty = list_empty(head);
198 spin_unlock(&gl->gl_spin);
199 return empty;
200}
201
202/**
203 * search_bucket() - Find struct gfs2_glock by lock number
204 * @bucket: the bucket to search
205 * @name: The lock name
206 *
207 * Returns: NULL, or the struct gfs2_glock with the requested number
208 */
209
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400210static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400211 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400212 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213{
214 struct gfs2_glock *gl;
215
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400216 list_for_each_entry(gl, &gl_hash_table[hash].hb_list, gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 if (test_bit(GLF_PLUG, &gl->gl_flags))
218 continue;
219 if (!lm_name_equal(&gl->gl_name, name))
220 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400221 if (gl->gl_sbd != sdp)
222 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223
224 kref_get(&gl->gl_ref);
225
226 return gl;
227 }
228
229 return NULL;
230}
231
232/**
233 * gfs2_glock_find() - Find glock by lock number
234 * @sdp: The GFS2 superblock
235 * @name: The lock name
236 *
237 * Returns: NULL, or the struct gfs2_glock with the requested number
238 */
239
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400240static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400241 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400243 unsigned int hash = gl_hash(sdp, name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244 struct gfs2_glock *gl;
245
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400246 read_lock(&gl_hash_locks[hash]);
247 gl = search_bucket(hash, sdp, name);
248 read_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249
250 return gl;
251}
252
253/**
254 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
255 * @sdp: The GFS2 superblock
256 * @number: the lock number
257 * @glops: The glock_operations to use
258 * @create: If 0, don't create the glock if it doesn't exist
259 * @glp: the glock is returned here
260 *
261 * This does not lock a glock, just finds/creates structures for one.
262 *
263 * Returns: errno
264 */
265
Steven Whitehousecd915492006-09-04 12:49:07 -0400266int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400267 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 struct gfs2_glock **glp)
269{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400270 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400272 unsigned int hash = gl_hash(sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 int error;
274
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400275 read_lock(&gl_hash_locks[hash]);
276 gl = search_bucket(hash, sdp, &name);
277 read_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278
279 if (gl || !create) {
280 *glp = gl;
281 return 0;
282 }
283
284 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
285 if (!gl)
286 return -ENOMEM;
287
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400288 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289 gl->gl_name = name;
290 kref_init(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400292 gl->gl_hash = hash;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400293 gl->gl_owner = NULL;
294 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 gl->gl_ops = glops;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400296 gl->gl_req_gh = NULL;
297 gl->gl_req_bh = NULL;
298 gl->gl_vn = 0;
299 gl->gl_stamp = jiffies;
300 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 gl->gl_sbd = sdp;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400302 gl->gl_aspace = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304
305 /* If this glock protects actual on-disk data or metadata blocks,
306 create a VFS inode to manage the pages/buffers holding them. */
Steven Whitehouse50299962006-09-04 09:49:55 -0400307 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 gl->gl_aspace = gfs2_aspace_get(sdp);
309 if (!gl->gl_aspace) {
310 error = -ENOMEM;
311 goto fail;
312 }
313 }
314
315 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
316 if (error)
317 goto fail_aspace;
318
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400319 write_lock(&gl_hash_locks[hash]);
320 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 if (tmp) {
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400322 write_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000323 glock_free(gl);
324 gl = tmp;
325 } else {
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400326 list_add_tail(&gl->gl_list, &gl_hash_table[hash].hb_list);
327 write_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 }
329
330 *glp = gl;
331
332 return 0;
333
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400334fail_aspace:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 if (gl->gl_aspace)
336 gfs2_aspace_put(gl->gl_aspace);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400337fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339 return error;
340}
341
342/**
343 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
344 * @gl: the glock
345 * @state: the state we're requesting
346 * @flags: the modifier flags
347 * @gh: the holder structure
348 *
349 */
350
Steven Whitehouse190562b2006-04-20 16:57:23 -0400351void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 struct gfs2_holder *gh)
353{
354 INIT_LIST_HEAD(&gh->gh_list);
355 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500356 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400357 gh->gh_owner = current;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 gh->gh_state = state;
359 gh->gh_flags = flags;
360 gh->gh_error = 0;
361 gh->gh_iflags = 0;
362 init_completion(&gh->gh_wait);
363
364 if (gh->gh_state == LM_ST_EXCLUSIVE)
365 gh->gh_flags |= GL_LOCAL_EXCL;
366
367 gfs2_glock_hold(gl);
368}
369
370/**
371 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
372 * @state: the state we're requesting
373 * @flags: the modifier flags
374 * @gh: the holder structure
375 *
376 * Don't mess with the glock.
377 *
378 */
379
Steven Whitehouse190562b2006-04-20 16:57:23 -0400380void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381{
382 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400383 gh->gh_flags = flags;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 if (gh->gh_state == LM_ST_EXCLUSIVE)
385 gh->gh_flags |= GL_LOCAL_EXCL;
386
387 gh->gh_iflags &= 1 << HIF_ALLOCED;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500388 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389}
390
391/**
392 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
393 * @gh: the holder structure
394 *
395 */
396
397void gfs2_holder_uninit(struct gfs2_holder *gh)
398{
399 gfs2_glock_put(gh->gh_gl);
400 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500401 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402}
403
404/**
405 * gfs2_holder_get - get a struct gfs2_holder structure
406 * @gl: the glock
407 * @state: the state we're requesting
408 * @flags: the modifier flags
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400409 * @gfp_flags:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410 *
411 * Figure out how big an impact this function has. Either:
412 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
413 * 2) Leave it like it is
414 *
415 * Returns: the holder structure, NULL on ENOMEM
416 */
417
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400418static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
419 unsigned int state,
420 int flags, gfp_t gfp_flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421{
422 struct gfs2_holder *gh;
423
424 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
425 if (!gh)
426 return NULL;
427
428 gfs2_holder_init(gl, state, flags, gh);
429 set_bit(HIF_ALLOCED, &gh->gh_iflags);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500430 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431 return gh;
432}
433
434/**
435 * gfs2_holder_put - get rid of a struct gfs2_holder structure
436 * @gh: the holder structure
437 *
438 */
439
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400440static void gfs2_holder_put(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441{
442 gfs2_holder_uninit(gh);
443 kfree(gh);
444}
445
446/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447 * rq_mutex - process a mutex request in the queue
448 * @gh: the glock holder
449 *
450 * Returns: 1 if the queue is blocked
451 */
452
453static int rq_mutex(struct gfs2_holder *gh)
454{
455 struct gfs2_glock *gl = gh->gh_gl;
456
457 list_del_init(&gh->gh_list);
458 /* gh->gh_error never examined. */
459 set_bit(GLF_LOCK, &gl->gl_flags);
460 complete(&gh->gh_wait);
461
462 return 1;
463}
464
465/**
466 * rq_promote - process a promote request in the queue
467 * @gh: the glock holder
468 *
469 * Acquire a new inter-node lock, or change a lock state to more restrictive.
470 *
471 * Returns: 1 if the queue is blocked
472 */
473
474static int rq_promote(struct gfs2_holder *gh)
475{
476 struct gfs2_glock *gl = gh->gh_gl;
477 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400478 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479
480 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
481 if (list_empty(&gl->gl_holders)) {
482 gl->gl_req_gh = gh;
483 set_bit(GLF_LOCK, &gl->gl_flags);
484 spin_unlock(&gl->gl_spin);
485
486 if (atomic_read(&sdp->sd_reclaim_count) >
487 gfs2_tune_get(sdp, gt_reclaim_limit) &&
488 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
489 gfs2_reclaim_glock(sdp);
490 gfs2_reclaim_glock(sdp);
491 }
492
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400493 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 spin_lock(&gl->gl_spin);
495 }
496 return 1;
497 }
498
499 if (list_empty(&gl->gl_holders)) {
500 set_bit(HIF_FIRST, &gh->gh_iflags);
501 set_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502 } else {
503 struct gfs2_holder *next_gh;
504 if (gh->gh_flags & GL_LOCAL_EXCL)
505 return 1;
506 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
507 gh_list);
508 if (next_gh->gh_flags & GL_LOCAL_EXCL)
509 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 }
511
512 list_move_tail(&gh->gh_list, &gl->gl_holders);
513 gh->gh_error = 0;
514 set_bit(HIF_HOLDER, &gh->gh_iflags);
515
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 complete(&gh->gh_wait);
517
518 return 0;
519}
520
521/**
522 * rq_demote - process a demote request in the queue
523 * @gh: the glock holder
524 *
525 * Returns: 1 if the queue is blocked
526 */
527
528static int rq_demote(struct gfs2_holder *gh)
529{
530 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400531 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532
533 if (!list_empty(&gl->gl_holders))
534 return 1;
535
536 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
537 list_del_init(&gh->gh_list);
538 gh->gh_error = 0;
539 spin_unlock(&gl->gl_spin);
540 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
541 gfs2_holder_put(gh);
542 else
543 complete(&gh->gh_wait);
544 spin_lock(&gl->gl_spin);
545 } else {
546 gl->gl_req_gh = gh;
547 set_bit(GLF_LOCK, &gl->gl_flags);
548 spin_unlock(&gl->gl_spin);
549
550 if (gh->gh_state == LM_ST_UNLOCKED ||
551 gl->gl_state != LM_ST_EXCLUSIVE)
552 glops->go_drop_th(gl);
553 else
554 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
555
556 spin_lock(&gl->gl_spin);
557 }
558
559 return 0;
560}
561
562/**
563 * rq_greedy - process a queued request to drop greedy status
564 * @gh: the glock holder
565 *
566 * Returns: 1 if the queue is blocked
567 */
568
569static int rq_greedy(struct gfs2_holder *gh)
570{
571 struct gfs2_glock *gl = gh->gh_gl;
572
573 list_del_init(&gh->gh_list);
574 /* gh->gh_error never examined. */
575 clear_bit(GLF_GREEDY, &gl->gl_flags);
576 spin_unlock(&gl->gl_spin);
577
578 gfs2_holder_uninit(gh);
579 kfree(container_of(gh, struct greedy, gr_gh));
580
581 spin_lock(&gl->gl_spin);
582
583 return 0;
584}
585
586/**
587 * run_queue - process holder structures on a glock
588 * @gl: the glock
589 *
590 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591static void run_queue(struct gfs2_glock *gl)
592{
593 struct gfs2_holder *gh;
594 int blocked = 1;
595
596 for (;;) {
597 if (test_bit(GLF_LOCK, &gl->gl_flags))
598 break;
599
600 if (!list_empty(&gl->gl_waiters1)) {
601 gh = list_entry(gl->gl_waiters1.next,
602 struct gfs2_holder, gh_list);
603
604 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
605 blocked = rq_mutex(gh);
606 else
607 gfs2_assert_warn(gl->gl_sbd, 0);
608
609 } else if (!list_empty(&gl->gl_waiters2) &&
610 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
611 gh = list_entry(gl->gl_waiters2.next,
612 struct gfs2_holder, gh_list);
613
614 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
615 blocked = rq_demote(gh);
616 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
617 blocked = rq_greedy(gh);
618 else
619 gfs2_assert_warn(gl->gl_sbd, 0);
620
621 } else if (!list_empty(&gl->gl_waiters3)) {
622 gh = list_entry(gl->gl_waiters3.next,
623 struct gfs2_holder, gh_list);
624
625 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
626 blocked = rq_promote(gh);
627 else
628 gfs2_assert_warn(gl->gl_sbd, 0);
629
630 } else
631 break;
632
633 if (blocked)
634 break;
635 }
636}
637
638/**
639 * gfs2_glmutex_lock - acquire a local lock on a glock
640 * @gl: the glock
641 *
642 * Gives caller exclusive access to manipulate a glock structure.
643 */
644
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400645static void gfs2_glmutex_lock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646{
647 struct gfs2_holder gh;
648
649 gfs2_holder_init(gl, 0, 0, &gh);
650 set_bit(HIF_MUTEX, &gh.gh_iflags);
651
652 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400653 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400655 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400656 gl->gl_owner = current;
657 gl->gl_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 complete(&gh.gh_wait);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400659 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 spin_unlock(&gl->gl_spin);
661
662 wait_for_completion(&gh.gh_wait);
663 gfs2_holder_uninit(&gh);
664}
665
666/**
667 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
668 * @gl: the glock
669 *
670 * Returns: 1 if the glock is acquired
671 */
672
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400673static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674{
675 int acquired = 1;
676
677 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400678 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 acquired = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400680 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400681 gl->gl_owner = current;
682 gl->gl_ip = (unsigned long)__builtin_return_address(0);
683 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684 spin_unlock(&gl->gl_spin);
685
686 return acquired;
687}
688
689/**
690 * gfs2_glmutex_unlock - release a local lock on a glock
691 * @gl: the glock
692 *
693 */
694
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400695static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696{
697 spin_lock(&gl->gl_spin);
698 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400699 gl->gl_owner = NULL;
700 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701 run_queue(gl);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400702 BUG_ON(!spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703 spin_unlock(&gl->gl_spin);
704}
705
706/**
707 * handle_callback - add a demote request to a lock's queue
708 * @gl: the glock
709 * @state: the state the caller wants us to change to
710 *
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400711 * Note: This may fail sliently if we are out of memory.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 */
713
714static void handle_callback(struct gfs2_glock *gl, unsigned int state)
715{
716 struct gfs2_holder *gh, *new_gh = NULL;
717
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400718restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 spin_lock(&gl->gl_spin);
720
721 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
722 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
723 gl->gl_req_gh != gh) {
724 if (gh->gh_state != state)
725 gh->gh_state = LM_ST_UNLOCKED;
726 goto out;
727 }
728 }
729
730 if (new_gh) {
731 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
732 new_gh = NULL;
733 } else {
734 spin_unlock(&gl->gl_spin);
735
Steven Whitehouseaf18ddb82006-06-24 15:42:21 -0400736 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
737 if (!new_gh)
738 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
740 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
741
742 goto restart;
743 }
744
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400745out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 spin_unlock(&gl->gl_spin);
747
748 if (new_gh)
749 gfs2_holder_put(new_gh);
750}
751
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400752void gfs2_glock_inode_squish(struct inode *inode)
753{
754 struct gfs2_holder gh;
755 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
756 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
757 set_bit(HIF_DEMOTE, &gh.gh_iflags);
758 spin_lock(&gl->gl_spin);
759 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
760 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
761 run_queue(gl);
762 spin_unlock(&gl->gl_spin);
Steven Whitehouse5dd9fea2006-07-28 14:52:33 -0400763 wait_for_completion(&gh.gh_wait);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400764 gfs2_holder_uninit(&gh);
765}
766
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767/**
768 * state_change - record that the glock is now in a different state
769 * @gl: the glock
770 * @new_state the new state
771 *
772 */
773
774static void state_change(struct gfs2_glock *gl, unsigned int new_state)
775{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 int held1, held2;
777
778 held1 = (gl->gl_state != LM_ST_UNLOCKED);
779 held2 = (new_state != LM_ST_UNLOCKED);
780
781 if (held1 != held2) {
David Teigland6a6b3d02006-02-23 10:11:47 +0000782 if (held2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 gfs2_glock_hold(gl);
David Teigland6a6b3d02006-02-23 10:11:47 +0000784 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 }
787
788 gl->gl_state = new_state;
789}
790
791/**
792 * xmote_bh - Called after the lock module is done acquiring a lock
793 * @gl: The glock in question
794 * @ret: the int returned from the lock module
795 *
796 */
797
798static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
799{
800 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400801 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 struct gfs2_holder *gh = gl->gl_req_gh;
803 int prev_state = gl->gl_state;
804 int op_done = 1;
805
806 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
807 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
808 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
809
810 state_change(gl, ret & LM_OUT_ST_MASK);
811
812 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
813 if (glops->go_inval)
814 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
815 } else if (gl->gl_state == LM_ST_DEFERRED) {
816 /* We might not want to do this here.
817 Look at moving to the inode glops. */
818 if (glops->go_inval)
819 glops->go_inval(gl, DIO_DATA);
820 }
821
822 /* Deal with each possible exit condition */
823
824 if (!gh)
825 gl->gl_stamp = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
827 spin_lock(&gl->gl_spin);
828 list_del_init(&gh->gh_list);
829 gh->gh_error = -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
832 spin_lock(&gl->gl_spin);
833 list_del_init(&gh->gh_list);
834 if (gl->gl_state == gh->gh_state ||
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400835 gl->gl_state == LM_ST_UNLOCKED) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 gh->gh_error = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400837 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 if (gfs2_assert_warn(sdp, gh->gh_flags &
839 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
840 fs_warn(sdp, "ret = 0x%.8X\n", ret);
841 gh->gh_error = GLR_TRYFAILED;
842 }
843 spin_unlock(&gl->gl_spin);
844
845 if (ret & LM_OUT_CANCELED)
Steven Whitehouse50299962006-09-04 09:49:55 -0400846 handle_callback(gl, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847
848 } else if (ret & LM_OUT_CANCELED) {
849 spin_lock(&gl->gl_spin);
850 list_del_init(&gh->gh_list);
851 gh->gh_error = GLR_CANCELED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 spin_unlock(&gl->gl_spin);
853
854 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
855 spin_lock(&gl->gl_spin);
856 list_move_tail(&gh->gh_list, &gl->gl_holders);
857 gh->gh_error = 0;
858 set_bit(HIF_HOLDER, &gh->gh_iflags);
859 spin_unlock(&gl->gl_spin);
860
861 set_bit(HIF_FIRST, &gh->gh_iflags);
862
863 op_done = 0;
864
865 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
866 spin_lock(&gl->gl_spin);
867 list_del_init(&gh->gh_list);
868 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869 spin_unlock(&gl->gl_spin);
870
871 } else {
872 if (gfs2_assert_withdraw(sdp, 0) == -1)
873 fs_err(sdp, "ret = 0x%.8X\n", ret);
874 }
875
876 if (glops->go_xmote_bh)
877 glops->go_xmote_bh(gl);
878
879 if (op_done) {
880 spin_lock(&gl->gl_spin);
881 gl->gl_req_gh = NULL;
882 gl->gl_req_bh = NULL;
883 clear_bit(GLF_LOCK, &gl->gl_flags);
884 run_queue(gl);
885 spin_unlock(&gl->gl_spin);
886 }
887
888 gfs2_glock_put(gl);
889
890 if (gh) {
891 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
892 gfs2_holder_put(gh);
893 else
894 complete(&gh->gh_wait);
895 }
896}
897
898/**
899 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
900 * @gl: The glock in question
901 * @state: the requested state
902 * @flags: modifier flags to the lock call
903 *
904 */
905
906void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
907{
908 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400909 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
911 LM_FLAG_NOEXP | LM_FLAG_ANY |
912 LM_FLAG_PRIORITY);
913 unsigned int lck_ret;
914
915 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
916 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
917 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
918 gfs2_assert_warn(sdp, state != gl->gl_state);
919
Steven Whitehouse50299962006-09-04 09:49:55 -0400920 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
921 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922
923 gfs2_glock_hold(gl);
924 gl->gl_req_bh = xmote_bh;
925
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400926 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927
928 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
929 return;
930
931 if (lck_ret & LM_OUT_ASYNC)
932 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
933 else
934 xmote_bh(gl, lck_ret);
935}
936
937/**
938 * drop_bh - Called after a lock module unlock completes
939 * @gl: the glock
940 * @ret: the return status
941 *
942 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
943 * Doesn't drop the reference on the glock the top half took out
944 *
945 */
946
947static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
948{
949 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400950 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 struct gfs2_holder *gh = gl->gl_req_gh;
952
953 clear_bit(GLF_PREFETCH, &gl->gl_flags);
954
955 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
956 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
957 gfs2_assert_warn(sdp, !ret);
958
959 state_change(gl, LM_ST_UNLOCKED);
960
961 if (glops->go_inval)
962 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
963
964 if (gh) {
965 spin_lock(&gl->gl_spin);
966 list_del_init(&gh->gh_list);
967 gh->gh_error = 0;
968 spin_unlock(&gl->gl_spin);
969 }
970
971 if (glops->go_drop_bh)
972 glops->go_drop_bh(gl);
973
974 spin_lock(&gl->gl_spin);
975 gl->gl_req_gh = NULL;
976 gl->gl_req_bh = NULL;
977 clear_bit(GLF_LOCK, &gl->gl_flags);
978 run_queue(gl);
979 spin_unlock(&gl->gl_spin);
980
981 gfs2_glock_put(gl);
982
983 if (gh) {
984 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
985 gfs2_holder_put(gh);
986 else
987 complete(&gh->gh_wait);
988 }
989}
990
991/**
992 * gfs2_glock_drop_th - call into the lock module to unlock a lock
993 * @gl: the glock
994 *
995 */
996
997void gfs2_glock_drop_th(struct gfs2_glock *gl)
998{
999 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001000 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 unsigned int ret;
1002
1003 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1004 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1005 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1006
Steven Whitehouse50299962006-09-04 09:49:55 -04001007 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1008 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009
1010 gfs2_glock_hold(gl);
1011 gl->gl_req_bh = drop_bh;
1012
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1014
1015 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1016 return;
1017
1018 if (!ret)
1019 drop_bh(gl, ret);
1020 else
1021 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1022}
1023
1024/**
1025 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1026 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1027 *
1028 * Don't cancel GL_NOCANCEL requests.
1029 */
1030
1031static void do_cancels(struct gfs2_holder *gh)
1032{
1033 struct gfs2_glock *gl = gh->gh_gl;
1034
1035 spin_lock(&gl->gl_spin);
1036
1037 while (gl->gl_req_gh != gh &&
1038 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1039 !list_empty(&gh->gh_list)) {
Steven Whitehouse50299962006-09-04 09:49:55 -04001040 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1041 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 spin_unlock(&gl->gl_spin);
1043 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1044 msleep(100);
1045 spin_lock(&gl->gl_spin);
1046 } else {
1047 spin_unlock(&gl->gl_spin);
1048 msleep(100);
1049 spin_lock(&gl->gl_spin);
1050 }
1051 }
1052
1053 spin_unlock(&gl->gl_spin);
1054}
1055
1056/**
1057 * glock_wait_internal - wait on a glock acquisition
1058 * @gh: the glock holder
1059 *
1060 * Returns: 0 on success
1061 */
1062
1063static int glock_wait_internal(struct gfs2_holder *gh)
1064{
1065 struct gfs2_glock *gl = gh->gh_gl;
1066 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001067 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068
1069 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1070 return -EIO;
1071
1072 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1073 spin_lock(&gl->gl_spin);
1074 if (gl->gl_req_gh != gh &&
1075 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1076 !list_empty(&gh->gh_list)) {
1077 list_del_init(&gh->gh_list);
1078 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 run_queue(gl);
1080 spin_unlock(&gl->gl_spin);
1081 return gh->gh_error;
1082 }
1083 spin_unlock(&gl->gl_spin);
1084 }
1085
1086 if (gh->gh_flags & LM_FLAG_PRIORITY)
1087 do_cancels(gh);
1088
1089 wait_for_completion(&gh->gh_wait);
1090
1091 if (gh->gh_error)
1092 return gh->gh_error;
1093
1094 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001095 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 gh->gh_flags));
1097
1098 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1099 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1100
1101 if (glops->go_lock) {
1102 gh->gh_error = glops->go_lock(gh);
1103 if (gh->gh_error) {
1104 spin_lock(&gl->gl_spin);
1105 list_del_init(&gh->gh_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 spin_unlock(&gl->gl_spin);
1107 }
1108 }
1109
1110 spin_lock(&gl->gl_spin);
1111 gl->gl_req_gh = NULL;
1112 gl->gl_req_bh = NULL;
1113 clear_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 run_queue(gl);
1115 spin_unlock(&gl->gl_spin);
1116 }
1117
1118 return gh->gh_error;
1119}
1120
1121static inline struct gfs2_holder *
1122find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1123{
1124 struct gfs2_holder *gh;
1125
1126 list_for_each_entry(gh, head, gh_list) {
1127 if (gh->gh_owner == owner)
1128 return gh;
1129 }
1130
1131 return NULL;
1132}
1133
1134/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1136 * @gh: the holder structure to add
1137 *
1138 */
1139
1140static void add_to_queue(struct gfs2_holder *gh)
1141{
1142 struct gfs2_glock *gl = gh->gh_gl;
1143 struct gfs2_holder *existing;
1144
Steven Whitehouse190562b2006-04-20 16:57:23 -04001145 BUG_ON(!gh->gh_owner);
1146
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1148 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001149 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001150 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1151 printk(KERN_INFO "lock type : %d lock state : %d\n",
1152 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001153 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001154 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1155 printk(KERN_INFO "lock type : %d lock state : %d\n",
1156 gl->gl_name.ln_type, gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001157 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 }
1159
1160 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1161 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001162 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1163 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1164 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 }
1166
David Teiglandb3b94fa2006-01-16 16:50:04 +00001167 if (gh->gh_flags & LM_FLAG_PRIORITY)
1168 list_add(&gh->gh_list, &gl->gl_waiters3);
1169 else
1170 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1171}
1172
1173/**
1174 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1175 * @gh: the holder structure
1176 *
1177 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1178 *
1179 * Returns: 0, GLR_TRYFAILED, or errno on failure
1180 */
1181
1182int gfs2_glock_nq(struct gfs2_holder *gh)
1183{
1184 struct gfs2_glock *gl = gh->gh_gl;
1185 struct gfs2_sbd *sdp = gl->gl_sbd;
1186 int error = 0;
1187
Steven Whitehouse320dd102006-05-18 16:25:27 -04001188restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1190 set_bit(HIF_ABORTED, &gh->gh_iflags);
1191 return -EIO;
1192 }
1193
1194 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1195
1196 spin_lock(&gl->gl_spin);
1197 add_to_queue(gh);
1198 run_queue(gl);
1199 spin_unlock(&gl->gl_spin);
1200
1201 if (!(gh->gh_flags & GL_ASYNC)) {
1202 error = glock_wait_internal(gh);
1203 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001204 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 goto restart;
1206 }
1207 }
1208
1209 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1210
Steven Whitehouse320dd102006-05-18 16:25:27 -04001211 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1212 dump_glock(gl);
1213
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214 return error;
1215}
1216
1217/**
1218 * gfs2_glock_poll - poll to see if an async request has been completed
1219 * @gh: the holder
1220 *
1221 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1222 */
1223
1224int gfs2_glock_poll(struct gfs2_holder *gh)
1225{
1226 struct gfs2_glock *gl = gh->gh_gl;
1227 int ready = 0;
1228
1229 spin_lock(&gl->gl_spin);
1230
1231 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1232 ready = 1;
1233 else if (list_empty(&gh->gh_list)) {
1234 if (gh->gh_error == GLR_CANCELED) {
1235 spin_unlock(&gl->gl_spin);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001236 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 if (gfs2_glock_nq(gh))
1238 return 1;
1239 return 0;
1240 } else
1241 ready = 1;
1242 }
1243
1244 spin_unlock(&gl->gl_spin);
1245
1246 return ready;
1247}
1248
1249/**
1250 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1251 * @gh: the holder structure
1252 *
1253 * Returns: 0, GLR_TRYFAILED, or errno on failure
1254 */
1255
1256int gfs2_glock_wait(struct gfs2_holder *gh)
1257{
1258 int error;
1259
1260 error = glock_wait_internal(gh);
1261 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001262 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263 gh->gh_flags &= ~GL_ASYNC;
1264 error = gfs2_glock_nq(gh);
1265 }
1266
1267 return error;
1268}
1269
1270/**
1271 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1272 * @gh: the glock holder
1273 *
1274 */
1275
1276void gfs2_glock_dq(struct gfs2_holder *gh)
1277{
1278 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001279 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 if (gh->gh_flags & GL_NOCACHE)
1282 handle_callback(gl, LM_ST_UNLOCKED);
1283
1284 gfs2_glmutex_lock(gl);
1285
1286 spin_lock(&gl->gl_spin);
1287 list_del_init(&gh->gh_list);
1288
1289 if (list_empty(&gl->gl_holders)) {
1290 spin_unlock(&gl->gl_spin);
1291
1292 if (glops->go_unlock)
1293 glops->go_unlock(gh);
1294
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295 gl->gl_stamp = jiffies;
1296
1297 spin_lock(&gl->gl_spin);
1298 }
1299
1300 clear_bit(GLF_LOCK, &gl->gl_flags);
1301 run_queue(gl);
1302 spin_unlock(&gl->gl_spin);
1303}
1304
1305/**
1306 * gfs2_glock_prefetch - Try to prefetch a glock
1307 * @gl: the glock
1308 * @state: the state to prefetch in
1309 * @flags: flags passed to go_xmote_th()
1310 *
1311 */
1312
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001313static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1314 int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315{
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001316 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317
1318 spin_lock(&gl->gl_spin);
1319
Steven Whitehouse50299962006-09-04 09:49:55 -04001320 if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
1321 !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322 !list_empty(&gl->gl_waiters3) ||
1323 relaxed_state_ok(gl->gl_state, state, flags)) {
1324 spin_unlock(&gl->gl_spin);
1325 return;
1326 }
1327
1328 set_bit(GLF_PREFETCH, &gl->gl_flags);
1329 set_bit(GLF_LOCK, &gl->gl_flags);
1330 spin_unlock(&gl->gl_spin);
1331
1332 glops->go_xmote_th(gl, state, flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333}
1334
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335static void greedy_work(void *data)
1336{
David Teiglande7f5c012006-04-27 11:25:45 -04001337 struct greedy *gr = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 struct gfs2_holder *gh = &gr->gr_gh;
1339 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001340 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001341
1342 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1343
1344 if (glops->go_greedy)
1345 glops->go_greedy(gl);
1346
1347 spin_lock(&gl->gl_spin);
1348
1349 if (list_empty(&gl->gl_waiters2)) {
1350 clear_bit(GLF_GREEDY, &gl->gl_flags);
1351 spin_unlock(&gl->gl_spin);
1352 gfs2_holder_uninit(gh);
1353 kfree(gr);
1354 } else {
1355 gfs2_glock_hold(gl);
1356 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1357 run_queue(gl);
1358 spin_unlock(&gl->gl_spin);
1359 gfs2_glock_put(gl);
1360 }
1361}
1362
1363/**
1364 * gfs2_glock_be_greedy -
1365 * @gl:
1366 * @time:
1367 *
1368 * Returns: 0 if go_greedy will be called, 1 otherwise
1369 */
1370
1371int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1372{
1373 struct greedy *gr;
1374 struct gfs2_holder *gh;
1375
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001376 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001377 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1378 return 1;
1379
1380 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1381 if (!gr) {
1382 clear_bit(GLF_GREEDY, &gl->gl_flags);
1383 return 1;
1384 }
1385 gh = &gr->gr_gh;
1386
Steven Whitehouse579b78a2006-04-26 14:58:26 -04001387 gfs2_holder_init(gl, 0, 0, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 set_bit(HIF_GREEDY, &gh->gh_iflags);
1389 INIT_WORK(&gr->gr_work, greedy_work, gr);
1390
1391 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1392 schedule_delayed_work(&gr->gr_work, time);
1393
1394 return 0;
1395}
1396
1397/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1399 * @gh: the holder structure
1400 *
1401 */
1402
1403void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1404{
1405 gfs2_glock_dq(gh);
1406 gfs2_holder_uninit(gh);
1407}
1408
1409/**
1410 * gfs2_glock_nq_num - acquire a glock based on lock number
1411 * @sdp: the filesystem
1412 * @number: the lock number
1413 * @glops: the glock operations for the type of glock
1414 * @state: the state to acquire the glock in
1415 * @flags: modifier flags for the aquisition
1416 * @gh: the struct gfs2_holder
1417 *
1418 * Returns: errno
1419 */
1420
Steven Whitehousecd915492006-09-04 12:49:07 -04001421int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001422 const struct gfs2_glock_operations *glops,
1423 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001424{
1425 struct gfs2_glock *gl;
1426 int error;
1427
1428 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1429 if (!error) {
1430 error = gfs2_glock_nq_init(gl, state, flags, gh);
1431 gfs2_glock_put(gl);
1432 }
1433
1434 return error;
1435}
1436
1437/**
1438 * glock_compare - Compare two struct gfs2_glock structures for sorting
1439 * @arg_a: the first structure
1440 * @arg_b: the second structure
1441 *
1442 */
1443
1444static int glock_compare(const void *arg_a, const void *arg_b)
1445{
1446 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1447 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1448 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1449 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1450 int ret = 0;
1451
1452 if (a->ln_number > b->ln_number)
1453 ret = 1;
1454 else if (a->ln_number < b->ln_number)
1455 ret = -1;
1456 else {
1457 if (gh_a->gh_state == LM_ST_SHARED &&
1458 gh_b->gh_state == LM_ST_EXCLUSIVE)
1459 ret = 1;
1460 else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) &&
1461 (gh_b->gh_flags & GL_LOCAL_EXCL))
1462 ret = 1;
1463 }
1464
1465 return ret;
1466}
1467
1468/**
1469 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1470 * @num_gh: the number of structures
1471 * @ghs: an array of struct gfs2_holder structures
1472 *
1473 * Returns: 0 on success (all glocks acquired),
1474 * errno on failure (no glocks acquired)
1475 */
1476
1477static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1478 struct gfs2_holder **p)
1479{
1480 unsigned int x;
1481 int error = 0;
1482
1483 for (x = 0; x < num_gh; x++)
1484 p[x] = &ghs[x];
1485
1486 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1487
1488 for (x = 0; x < num_gh; x++) {
1489 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1490
1491 error = gfs2_glock_nq(p[x]);
1492 if (error) {
1493 while (x--)
1494 gfs2_glock_dq(p[x]);
1495 break;
1496 }
1497 }
1498
1499 return error;
1500}
1501
1502/**
1503 * gfs2_glock_nq_m - acquire multiple glocks
1504 * @num_gh: the number of structures
1505 * @ghs: an array of struct gfs2_holder structures
1506 *
1507 * Figure out how big an impact this function has. Either:
1508 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1509 * 2) Forget async stuff and just call nq_m_sync()
1510 * 3) Leave it like it is
1511 *
1512 * Returns: 0 on success (all glocks acquired),
1513 * errno on failure (no glocks acquired)
1514 */
1515
1516int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1517{
1518 int *e;
1519 unsigned int x;
1520 int borked = 0, serious = 0;
1521 int error = 0;
1522
1523 if (!num_gh)
1524 return 0;
1525
1526 if (num_gh == 1) {
1527 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1528 return gfs2_glock_nq(ghs);
1529 }
1530
1531 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1532 if (!e)
1533 return -ENOMEM;
1534
1535 for (x = 0; x < num_gh; x++) {
1536 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1537 error = gfs2_glock_nq(&ghs[x]);
1538 if (error) {
1539 borked = 1;
1540 serious = error;
1541 num_gh = x;
1542 break;
1543 }
1544 }
1545
1546 for (x = 0; x < num_gh; x++) {
1547 error = e[x] = glock_wait_internal(&ghs[x]);
1548 if (error) {
1549 borked = 1;
1550 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1551 serious = error;
1552 }
1553 }
1554
1555 if (!borked) {
1556 kfree(e);
1557 return 0;
1558 }
1559
1560 for (x = 0; x < num_gh; x++)
1561 if (!e[x])
1562 gfs2_glock_dq(&ghs[x]);
1563
1564 if (serious)
1565 error = serious;
1566 else {
1567 for (x = 0; x < num_gh; x++)
1568 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1569 &ghs[x]);
1570 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1571 }
1572
1573 kfree(e);
1574
1575 return error;
1576}
1577
1578/**
1579 * gfs2_glock_dq_m - release multiple glocks
1580 * @num_gh: the number of structures
1581 * @ghs: an array of struct gfs2_holder structures
1582 *
1583 */
1584
1585void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1586{
1587 unsigned int x;
1588
1589 for (x = 0; x < num_gh; x++)
1590 gfs2_glock_dq(&ghs[x]);
1591}
1592
1593/**
1594 * gfs2_glock_dq_uninit_m - release multiple glocks
1595 * @num_gh: the number of structures
1596 * @ghs: an array of struct gfs2_holder structures
1597 *
1598 */
1599
1600void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1601{
1602 unsigned int x;
1603
1604 for (x = 0; x < num_gh; x++)
1605 gfs2_glock_dq_uninit(&ghs[x]);
1606}
1607
1608/**
1609 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1610 * @sdp: the filesystem
1611 * @number: the lock number
1612 * @glops: the glock operations for the type of glock
1613 * @state: the state to acquire the glock in
1614 * @flags: modifier flags for the aquisition
1615 *
1616 * Returns: errno
1617 */
1618
Steven Whitehousecd915492006-09-04 12:49:07 -04001619void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001620 const struct gfs2_glock_operations *glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001621 unsigned int state, int flags)
1622{
1623 struct gfs2_glock *gl;
1624 int error;
1625
1626 if (atomic_read(&sdp->sd_reclaim_count) <
1627 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1628 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1629 if (!error) {
1630 gfs2_glock_prefetch(gl, state, flags);
1631 gfs2_glock_put(gl);
1632 }
1633 }
1634}
1635
1636/**
1637 * gfs2_lvb_hold - attach a LVB from a glock
1638 * @gl: The glock in question
1639 *
1640 */
1641
1642int gfs2_lvb_hold(struct gfs2_glock *gl)
1643{
1644 int error;
1645
1646 gfs2_glmutex_lock(gl);
1647
1648 if (!atomic_read(&gl->gl_lvb_count)) {
1649 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1650 if (error) {
1651 gfs2_glmutex_unlock(gl);
1652 return error;
1653 }
1654 gfs2_glock_hold(gl);
1655 }
1656 atomic_inc(&gl->gl_lvb_count);
1657
1658 gfs2_glmutex_unlock(gl);
1659
1660 return 0;
1661}
1662
1663/**
1664 * gfs2_lvb_unhold - detach a LVB from a glock
1665 * @gl: The glock in question
1666 *
1667 */
1668
1669void gfs2_lvb_unhold(struct gfs2_glock *gl)
1670{
1671 gfs2_glock_hold(gl);
1672 gfs2_glmutex_lock(gl);
1673
1674 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1675 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1676 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1677 gl->gl_lvb = NULL;
1678 gfs2_glock_put(gl);
1679 }
1680
1681 gfs2_glmutex_unlock(gl);
1682 gfs2_glock_put(gl);
1683}
1684
David Teiglandb3b94fa2006-01-16 16:50:04 +00001685static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1686 unsigned int state)
1687{
1688 struct gfs2_glock *gl;
1689
1690 gl = gfs2_glock_find(sdp, name);
1691 if (!gl)
1692 return;
1693
1694 if (gl->gl_ops->go_callback)
1695 gl->gl_ops->go_callback(gl, state);
1696 handle_callback(gl, state);
1697
1698 spin_lock(&gl->gl_spin);
1699 run_queue(gl);
1700 spin_unlock(&gl->gl_spin);
1701
1702 gfs2_glock_put(gl);
1703}
1704
1705/**
1706 * gfs2_glock_cb - Callback used by locking module
Steven Whitehouse1c089c32006-09-07 15:50:20 -04001707 * @sdp: Pointer to the superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +00001708 * @type: Type of callback
1709 * @data: Type dependent data pointer
1710 *
1711 * Called by the locking module when it wants to tell us something.
1712 * Either we need to drop a lock, one of our ASYNC requests completed, or
1713 * a journal from another client needs to be recovered.
1714 */
1715
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001716void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001717{
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001718 struct gfs2_sbd *sdp = cb_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001719
David Teiglandb3b94fa2006-01-16 16:50:04 +00001720 switch (type) {
1721 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001722 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001723 return;
1724
1725 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001726 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001727 return;
1728
1729 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001730 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731 return;
1732
1733 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001734 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001735 struct gfs2_glock *gl;
1736
1737 gl = gfs2_glock_find(sdp, &async->lc_name);
1738 if (gfs2_assert_warn(sdp, gl))
1739 return;
1740 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1741 gl->gl_req_bh(gl, async->lc_ret);
1742 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001743 return;
1744 }
1745
1746 case LM_CB_NEED_RECOVERY:
1747 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1748 if (sdp->sd_recoverd_process)
1749 wake_up_process(sdp->sd_recoverd_process);
1750 return;
1751
1752 case LM_CB_DROPLOCKS:
1753 gfs2_gl_hash_clear(sdp, NO_WAIT);
1754 gfs2_quota_scan(sdp);
1755 return;
1756
1757 default:
1758 gfs2_assert_warn(sdp, 0);
1759 return;
1760 }
1761}
1762
1763/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001764 * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an
1765 * iopen glock from memory
1766 * @io_gl: the iopen glock
1767 * @state: the state into which the glock should be put
1768 *
1769 */
1770
1771void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state)
1772{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001773
1774 if (state != LM_ST_UNLOCKED)
1775 return;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001776 /* FIXME: remove this? */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777}
1778
1779/**
1780 * demote_ok - Check to see if it's ok to unlock a glock
1781 * @gl: the glock
1782 *
1783 * Returns: 1 if it's ok
1784 */
1785
1786static int demote_ok(struct gfs2_glock *gl)
1787{
1788 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001789 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001790 int demote = 1;
1791
1792 if (test_bit(GLF_STICKY, &gl->gl_flags))
1793 demote = 0;
1794 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
Steven Whitehouse50299962006-09-04 09:49:55 -04001795 demote = time_after_eq(jiffies, gl->gl_stamp +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001796 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1797 else if (glops->go_demote_ok)
1798 demote = glops->go_demote_ok(gl);
1799
1800 return demote;
1801}
1802
1803/**
1804 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1805 * @gl: the glock
1806 *
1807 */
1808
1809void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1810{
1811 struct gfs2_sbd *sdp = gl->gl_sbd;
1812
1813 spin_lock(&sdp->sd_reclaim_lock);
1814 if (list_empty(&gl->gl_reclaim)) {
1815 gfs2_glock_hold(gl);
1816 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1817 atomic_inc(&sdp->sd_reclaim_count);
1818 }
1819 spin_unlock(&sdp->sd_reclaim_lock);
1820
1821 wake_up(&sdp->sd_reclaim_wq);
1822}
1823
1824/**
1825 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1826 * @sdp: the filesystem
1827 *
1828 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1829 * different glock and we notice that there are a lot of glocks in the
1830 * reclaim list.
1831 *
1832 */
1833
1834void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1835{
1836 struct gfs2_glock *gl;
1837
1838 spin_lock(&sdp->sd_reclaim_lock);
1839 if (list_empty(&sdp->sd_reclaim_list)) {
1840 spin_unlock(&sdp->sd_reclaim_lock);
1841 return;
1842 }
1843 gl = list_entry(sdp->sd_reclaim_list.next,
1844 struct gfs2_glock, gl_reclaim);
1845 list_del_init(&gl->gl_reclaim);
1846 spin_unlock(&sdp->sd_reclaim_lock);
1847
1848 atomic_dec(&sdp->sd_reclaim_count);
1849 atomic_inc(&sdp->sd_reclaimed);
1850
1851 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001852 if (queue_empty(gl, &gl->gl_holders) &&
Steven Whitehouse50299962006-09-04 09:49:55 -04001853 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001854 handle_callback(gl, LM_ST_UNLOCKED);
1855 gfs2_glmutex_unlock(gl);
1856 }
1857
1858 gfs2_glock_put(gl);
1859}
1860
1861/**
1862 * examine_bucket - Call a function for glock in a hash bucket
1863 * @examiner: the function
1864 * @sdp: the filesystem
1865 * @bucket: the bucket
1866 *
1867 * Returns: 1 if the bucket has entries
1868 */
1869
1870static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001871 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001872{
1873 struct glock_plug plug;
1874 struct list_head *tmp;
1875 struct gfs2_glock *gl;
1876 int entries;
1877
1878 /* Add "plug" to end of bucket list, work back up list from there */
1879 memset(&plug.gl_flags, 0, sizeof(unsigned long));
1880 set_bit(GLF_PLUG, &plug.gl_flags);
1881
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001882 write_lock(&gl_hash_locks[hash]);
1883 list_add(&plug.gl_list, &gl_hash_table[hash].hb_list);
1884 write_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885
1886 for (;;) {
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001887 write_lock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001888
1889 for (;;) {
1890 tmp = plug.gl_list.next;
1891
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001892 if (tmp == &gl_hash_table[hash].hb_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001893 list_del(&plug.gl_list);
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001894 entries = !list_empty(&gl_hash_table[hash].hb_list);
1895 write_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001896 return entries;
1897 }
1898 gl = list_entry(tmp, struct gfs2_glock, gl_list);
1899
1900 /* Move plug up list */
1901 list_move(&plug.gl_list, &gl->gl_list);
1902
1903 if (test_bit(GLF_PLUG, &gl->gl_flags))
1904 continue;
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001905 if (gl->gl_sbd != sdp)
1906 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001907
1908 /* examiner() must glock_put() */
1909 gfs2_glock_hold(gl);
1910
1911 break;
1912 }
1913
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001914 write_unlock(&gl_hash_locks[hash]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001915
1916 examiner(gl);
1917 }
1918}
1919
1920/**
1921 * scan_glock - look at a glock and see if we can reclaim it
1922 * @gl: the glock to look at
1923 *
1924 */
1925
1926static void scan_glock(struct gfs2_glock *gl)
1927{
Steven Whitehousea2242db2006-08-24 17:03:05 -04001928 if (gl->gl_ops == &gfs2_inode_glops)
1929 goto out;
1930
David Teiglandb3b94fa2006-01-16 16:50:04 +00001931 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001932 if (queue_empty(gl, &gl->gl_holders) &&
1933 gl->gl_state != LM_ST_UNLOCKED &&
1934 demote_ok(gl))
1935 goto out_schedule;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001936 gfs2_glmutex_unlock(gl);
1937 }
Steven Whitehousea2242db2006-08-24 17:03:05 -04001938out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001939 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 return;
1941
Steven Whitehouse627add22006-07-05 13:16:19 -04001942out_schedule:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001943 gfs2_glmutex_unlock(gl);
1944 gfs2_glock_schedule_for_reclaim(gl);
1945 gfs2_glock_put(gl);
1946}
1947
1948/**
1949 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1950 * @sdp: the filesystem
1951 *
1952 */
1953
1954void gfs2_scand_internal(struct gfs2_sbd *sdp)
1955{
1956 unsigned int x;
1957
1958 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001959 examine_bucket(scan_glock, sdp, x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001960 cond_resched();
1961 }
1962}
1963
1964/**
1965 * clear_glock - look at a glock and see if we can free it from glock cache
1966 * @gl: the glock to look at
1967 *
1968 */
1969
1970static void clear_glock(struct gfs2_glock *gl)
1971{
1972 struct gfs2_sbd *sdp = gl->gl_sbd;
1973 int released;
1974
1975 spin_lock(&sdp->sd_reclaim_lock);
1976 if (!list_empty(&gl->gl_reclaim)) {
1977 list_del_init(&gl->gl_reclaim);
1978 atomic_dec(&sdp->sd_reclaim_count);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001979 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001980 released = gfs2_glock_put(gl);
1981 gfs2_assert(sdp, !released);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001982 } else {
1983 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001984 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001985
1986 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001987 if (queue_empty(gl, &gl->gl_holders) &&
1988 gl->gl_state != LM_ST_UNLOCKED)
1989 handle_callback(gl, LM_ST_UNLOCKED);
1990
1991 gfs2_glmutex_unlock(gl);
1992 }
1993
1994 gfs2_glock_put(gl);
1995}
1996
1997/**
1998 * gfs2_gl_hash_clear - Empty out the glock hash table
1999 * @sdp: the filesystem
2000 * @wait: wait until it's all gone
2001 *
2002 * Called when unmounting the filesystem, or when inter-node lock manager
2003 * requests DROPLOCKS because it is running out of capacity.
2004 */
2005
2006void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2007{
2008 unsigned long t;
2009 unsigned int x;
2010 int cont;
2011
2012 t = jiffies;
2013
2014 for (;;) {
2015 cont = 0;
2016
2017 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04002018 if (examine_bucket(clear_glock, sdp, x))
David Teiglandb3b94fa2006-01-16 16:50:04 +00002019 cont = 1;
2020
2021 if (!wait || !cont)
2022 break;
2023
2024 if (time_after_eq(jiffies,
2025 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2026 fs_warn(sdp, "Unmount seems to be stalled. "
2027 "Dumping lock state...\n");
2028 gfs2_dump_lockstate(sdp);
2029 t = jiffies;
2030 }
2031
David Teiglandb3b94fa2006-01-16 16:50:04 +00002032 invalidate_inodes(sdp->sd_vfs);
Steven Whitehousefd88de562006-05-05 16:59:11 -04002033 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002034 }
2035}
2036
2037/*
2038 * Diagnostic routines to help debug distributed deadlock
2039 */
2040
2041/**
2042 * dump_holder - print information about a glock holder
2043 * @str: a string naming the type of holder
2044 * @gh: the glock holder
2045 *
2046 * Returns: 0 on success, -ENOBUFS when we run out of space
2047 */
2048
2049static int dump_holder(char *str, struct gfs2_holder *gh)
2050{
2051 unsigned int x;
2052 int error = -ENOBUFS;
2053
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002054 printk(KERN_INFO " %s\n", str);
2055 printk(KERN_INFO " owner = %ld\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002056 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002057 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2058 printk(KERN_INFO " gh_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002059 for (x = 0; x < 32; x++)
2060 if (gh->gh_flags & (1 << x))
2061 printk(" %u", x);
2062 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002063 printk(KERN_INFO " error = %d\n", gh->gh_error);
2064 printk(KERN_INFO " gh_iflags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002065 for (x = 0; x < 32; x++)
2066 if (test_bit(x, &gh->gh_iflags))
2067 printk(" %u", x);
2068 printk(" \n");
Steven Whitehoused0dc80d2006-03-29 14:36:49 -05002069 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002070
2071 error = 0;
2072
2073 return error;
2074}
2075
2076/**
2077 * dump_inode - print information about an inode
2078 * @ip: the inode
2079 *
2080 * Returns: 0 on success, -ENOBUFS when we run out of space
2081 */
2082
2083static int dump_inode(struct gfs2_inode *ip)
2084{
2085 unsigned int x;
2086 int error = -ENOBUFS;
2087
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002088 printk(KERN_INFO " Inode:\n");
2089 printk(KERN_INFO " num = %llu %llu\n",
Steven Whitehouse382066d2006-05-24 10:22:09 -04002090 (unsigned long long)ip->i_num.no_formal_ino,
2091 (unsigned long long)ip->i_num.no_addr);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002092 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002093 printk(KERN_INFO " i_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002094 for (x = 0; x < 32; x++)
2095 if (test_bit(x, &ip->i_flags))
2096 printk(" %u", x);
2097 printk(" \n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002098
2099 error = 0;
2100
2101 return error;
2102}
2103
2104/**
2105 * dump_glock - print information about a glock
2106 * @gl: the glock
2107 * @count: where we are in the buffer
2108 *
2109 * Returns: 0 on success, -ENOBUFS when we run out of space
2110 */
2111
2112static int dump_glock(struct gfs2_glock *gl)
2113{
2114 struct gfs2_holder *gh;
2115 unsigned int x;
2116 int error = -ENOBUFS;
2117
2118 spin_lock(&gl->gl_spin);
2119
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002120 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
Steven Whitehouse382066d2006-05-24 10:22:09 -04002121 (unsigned long long)gl->gl_name.ln_number);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002122 printk(KERN_INFO " gl_flags =");
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002123 for (x = 0; x < 32; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002124 if (test_bit(x, &gl->gl_flags))
2125 printk(" %u", x);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002126 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002127 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002128 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2129 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
Steven Whitehouse320dd102006-05-18 16:25:27 -04002130 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2131 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002132 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2133 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2134 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2135 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2136 printk(KERN_INFO " le = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002137 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002138 printk(KERN_INFO " reclaim = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002139 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2140 if (gl->gl_aspace)
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002141 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
Russell Cattelan88721872006-08-10 11:08:40 -05002142 gl->gl_aspace->i_mapping->nrpages);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002143 else
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002144 printk(KERN_INFO " aspace = no\n");
2145 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002146 if (gl->gl_req_gh) {
2147 error = dump_holder("Request", gl->gl_req_gh);
2148 if (error)
2149 goto out;
2150 }
2151 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2152 error = dump_holder("Holder", gh);
2153 if (error)
2154 goto out;
2155 }
2156 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2157 error = dump_holder("Waiter1", gh);
2158 if (error)
2159 goto out;
2160 }
2161 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2162 error = dump_holder("Waiter2", gh);
2163 if (error)
2164 goto out;
2165 }
2166 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2167 error = dump_holder("Waiter3", gh);
2168 if (error)
2169 goto out;
2170 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002171 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002172 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2173 list_empty(&gl->gl_holders)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002174 error = dump_inode(gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002175 if (error)
2176 goto out;
2177 } else {
2178 error = -ENOBUFS;
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002179 printk(KERN_INFO " Inode: busy\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002180 }
2181 }
2182
2183 error = 0;
2184
Steven Whitehousea91ea692006-09-04 12:04:26 -04002185out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002186 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002187 return error;
2188}
2189
2190/**
2191 * gfs2_dump_lockstate - print out the current lockstate
2192 * @sdp: the filesystem
2193 * @ub: the buffer to copy the information into
2194 *
2195 * If @ub is NULL, dump the lockstate to the console.
2196 *
2197 */
2198
Adrian Bunk08bc2db2006-04-28 10:59:12 -04002199static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002200{
David Teiglandb3b94fa2006-01-16 16:50:04 +00002201 struct gfs2_glock *gl;
2202 unsigned int x;
2203 int error = 0;
2204
2205 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002206
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04002207 read_lock(&gl_hash_locks[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002208
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04002209 list_for_each_entry(gl, &gl_hash_table[x].hb_list, gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002210 if (test_bit(GLF_PLUG, &gl->gl_flags))
2211 continue;
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002212 if (gl->gl_sbd != sdp)
2213 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002214
2215 error = dump_glock(gl);
2216 if (error)
2217 break;
2218 }
2219
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04002220 read_unlock(&gl_hash_locks[x]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002221
2222 if (error)
2223 break;
2224 }
2225
2226
2227 return error;
2228}
2229
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002230int __init gfs2_glock_init(void)
2231{
2232 unsigned i;
2233 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04002234 rwlock_init(&gl_hash_locks[i]);
2235 INIT_LIST_HEAD(&gl_hash_table[i].hb_list);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002236 }
2237 return 0;
2238}
2239