blob: 12accb08fe02d3e701fa49e79d9e1a1c7519f495 [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>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050018#include <linux/kallsyms.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
Steven Whitehouse24264432006-09-11 21:40:30 -040020#include <linux/list.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020021#include <linux/lm_interface.h>
Steven Whitehousefee852e2007-01-17 15:33:23 +000022#include <linux/wait.h>
akpm@linux-foundation.org95d97b72007-03-05 23:10:39 -080023#include <linux/module.h>
Steven Whitehouse61be0842007-01-29 11:51:45 +000024#include <linux/rwsem.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include <asm/uaccess.h>
26
27#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "glock.h"
30#include "glops.h"
31#include "inode.h"
32#include "lm.h"
33#include "lops.h"
34#include "meta_io.h"
35#include "quota.h"
36#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050037#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000038
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040039struct gfs2_gl_hash_bucket {
Steven Whitehouseb6397892006-09-12 10:10:01 -040040 struct hlist_head hb_list;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040041};
42
David Teiglandb3b94fa2006-01-16 16:50:04 +000043typedef void (*glock_examiner) (struct gfs2_glock * gl);
44
Adrian Bunk08bc2db2006-04-28 10:59:12 -040045static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
Steven Whitehouse320dd102006-05-18 16:25:27 -040046static int dump_glock(struct gfs2_glock *gl);
Steven Whitehouse24264432006-09-11 21:40:30 -040047static int dump_inode(struct gfs2_inode *ip);
Steven Whitehouseb5d32be2007-01-22 12:15:34 -050048static void gfs2_glock_xmote_th(struct gfs2_holder *gh);
49static void gfs2_glock_drop_th(struct gfs2_glock *gl);
Steven Whitehouse61be0842007-01-29 11:51:45 +000050static DECLARE_RWSEM(gfs2_umount_flush_sem);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040051
Steven Whitehouseb6397892006-09-12 10:10:01 -040052#define GFS2_GL_HASH_SHIFT 15
Steven Whitehouse087efdd2006-09-09 16:59:11 -040053#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
54#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
55
Steven Whitehouse85d1da62006-09-07 14:40:21 -040056static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
Steven Whitehouse087efdd2006-09-09 16:59:11 -040057
58/*
59 * Despite what you might think, the numbers below are not arbitrary :-)
60 * They are taken from the ipv4 routing hash code, which is well tested
61 * and thus should be nearly optimal. Later on we might tweek the numbers
62 * but for now this should be fine.
63 *
64 * The reason for putting the locks in a separate array from the list heads
65 * is that we can have fewer locks than list heads and save memory. We use
66 * the same hash function for both, but with a different hash mask.
67 */
68#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
69 defined(CONFIG_PROVE_LOCKING)
70
71#ifdef CONFIG_LOCKDEP
72# define GL_HASH_LOCK_SZ 256
73#else
74# if NR_CPUS >= 32
75# define GL_HASH_LOCK_SZ 4096
76# elif NR_CPUS >= 16
77# define GL_HASH_LOCK_SZ 2048
78# elif NR_CPUS >= 8
79# define GL_HASH_LOCK_SZ 1024
80# elif NR_CPUS >= 4
81# define GL_HASH_LOCK_SZ 512
82# else
83# define GL_HASH_LOCK_SZ 256
84# endif
85#endif
86
87/* We never want more locks than chains */
88#if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
89# undef GL_HASH_LOCK_SZ
90# define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
91#endif
92
93static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
94
95static inline rwlock_t *gl_lock_addr(unsigned int x)
96{
Steven Whitehouse94610612006-09-09 18:59:27 -040097 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
Steven Whitehouse087efdd2006-09-09 16:59:11 -040098}
99#else /* not SMP, so no spinlocks required */
Randy Dunlap0ac23062006-11-28 22:29:19 -0800100static inline rwlock_t *gl_lock_addr(unsigned int x)
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400101{
102 return NULL;
103}
104#endif
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400105
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106/**
107 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
108 * @actual: the current state of the lock
109 * @requested: the lock state that was requested by the caller
110 * @flags: the modifier flags passed in by the caller
111 *
112 * Returns: 1 if the locks are compatible, 0 otherwise
113 */
114
115static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
116 int flags)
117{
118 if (actual == requested)
119 return 1;
120
121 if (flags & GL_EXACT)
122 return 0;
123
124 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
125 return 1;
126
127 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
128 return 1;
129
130 return 0;
131}
132
133/**
134 * gl_hash() - Turn glock number into hash bucket number
135 * @lock: The glock number
136 *
137 * Returns: The number of the corresponding hash bucket
138 */
139
Steven Whitehouseb8547852006-09-07 13:12:27 -0400140static unsigned int gl_hash(const struct gfs2_sbd *sdp,
141 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142{
143 unsigned int h;
144
Steven Whitehousecd915492006-09-04 12:49:07 -0400145 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -0400147 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 h &= GFS2_GL_HASH_MASK;
149
150 return h;
151}
152
153/**
154 * glock_free() - Perform a few checks and then release struct gfs2_glock
155 * @gl: The glock to release
156 *
157 * Also calls lock module to release its internal structure for this glock.
158 *
159 */
160
161static void glock_free(struct gfs2_glock *gl)
162{
163 struct gfs2_sbd *sdp = gl->gl_sbd;
164 struct inode *aspace = gl->gl_aspace;
165
166 gfs2_lm_put_lock(sdp, gl->gl_lock);
167
168 if (aspace)
169 gfs2_aspace_put(aspace);
170
171 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172}
173
174/**
175 * gfs2_glock_hold() - increment reference count on glock
176 * @gl: The glock to hold
177 *
178 */
179
180void gfs2_glock_hold(struct gfs2_glock *gl)
181{
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400182 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183}
184
185/**
186 * gfs2_glock_put() - Decrement reference count on glock
187 * @gl: The glock to put
188 *
189 */
190
191int gfs2_glock_put(struct gfs2_glock *gl)
192{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193 int rv = 0;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400194 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400196 write_lock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400197 if (atomic_dec_and_test(&gl->gl_ref)) {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400198 hlist_del(&gl->gl_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400199 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse190562b2006-04-20 16:57:23 -0400200 BUG_ON(spin_is_locked(&gl->gl_spin));
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400201 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
202 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
203 gfs2_assert(sdp, list_empty(&gl->gl_holders));
204 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
205 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
206 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207 glock_free(gl);
208 rv = 1;
209 goto out;
210 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400211 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehousea2242db2006-08-24 17:03:05 -0400212out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 return rv;
214}
215
216/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 * search_bucket() - Find struct gfs2_glock by lock number
218 * @bucket: the bucket to search
219 * @name: The lock name
220 *
221 * Returns: NULL, or the struct gfs2_glock with the requested number
222 */
223
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400224static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400225 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400226 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227{
228 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -0400229 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230
Steven Whitehouseb6397892006-09-12 10:10:01 -0400231 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 if (!lm_name_equal(&gl->gl_name, name))
233 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400234 if (gl->gl_sbd != sdp)
235 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400237 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000238
239 return gl;
240 }
241
242 return NULL;
243}
244
245/**
246 * gfs2_glock_find() - Find glock by lock number
247 * @sdp: The GFS2 superblock
248 * @name: The lock name
249 *
250 * Returns: NULL, or the struct gfs2_glock with the requested number
251 */
252
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400253static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400254 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400256 unsigned int hash = gl_hash(sdp, name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 struct gfs2_glock *gl;
258
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400259 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400260 gl = search_bucket(hash, sdp, name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400261 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262
263 return gl;
264}
265
266/**
267 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
268 * @sdp: The GFS2 superblock
269 * @number: the lock number
270 * @glops: The glock_operations to use
271 * @create: If 0, don't create the glock if it doesn't exist
272 * @glp: the glock is returned here
273 *
274 * This does not lock a glock, just finds/creates structures for one.
275 *
276 * Returns: errno
277 */
278
Steven Whitehousecd915492006-09-04 12:49:07 -0400279int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400280 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 struct gfs2_glock **glp)
282{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400283 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400285 unsigned int hash = gl_hash(sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 int error;
287
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400288 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400289 gl = search_bucket(hash, sdp, &name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400290 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291
292 if (gl || !create) {
293 *glp = gl;
294 return 0;
295 }
296
297 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
298 if (!gl)
299 return -ENOMEM;
300
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400301 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 gl->gl_name = name;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400303 atomic_set(&gl->gl_ref, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400305 gl->gl_hash = hash;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400306 gl->gl_owner = NULL;
307 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 gl->gl_ops = glops;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400309 gl->gl_req_gh = NULL;
310 gl->gl_req_bh = NULL;
311 gl->gl_vn = 0;
312 gl->gl_stamp = jiffies;
313 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 gl->gl_sbd = sdp;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400315 gl->gl_aspace = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317
318 /* If this glock protects actual on-disk data or metadata blocks,
319 create a VFS inode to manage the pages/buffers holding them. */
Steven Whitehouse50299962006-09-04 09:49:55 -0400320 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 gl->gl_aspace = gfs2_aspace_get(sdp);
322 if (!gl->gl_aspace) {
323 error = -ENOMEM;
324 goto fail;
325 }
326 }
327
328 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
329 if (error)
330 goto fail_aspace;
331
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400332 write_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400333 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334 if (tmp) {
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400335 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 glock_free(gl);
337 gl = tmp;
338 } else {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400339 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400340 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 }
342
343 *glp = gl;
344
345 return 0;
346
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400347fail_aspace:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 if (gl->gl_aspace)
349 gfs2_aspace_put(gl->gl_aspace);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400350fail:
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400351 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 return error;
353}
354
355/**
356 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
357 * @gl: the glock
358 * @state: the state we're requesting
359 * @flags: the modifier flags
360 * @gh: the holder structure
361 *
362 */
363
Steven Whitehouse190562b2006-04-20 16:57:23 -0400364void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365 struct gfs2_holder *gh)
366{
367 INIT_LIST_HEAD(&gh->gh_list);
368 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500369 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400370 gh->gh_owner = current;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 gh->gh_state = state;
372 gh->gh_flags = flags;
373 gh->gh_error = 0;
374 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375 gfs2_glock_hold(gl);
376}
377
378/**
379 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
380 * @state: the state we're requesting
381 * @flags: the modifier flags
382 * @gh: the holder structure
383 *
384 * Don't mess with the glock.
385 *
386 */
387
Steven Whitehouse190562b2006-04-20 16:57:23 -0400388void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389{
390 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400391 gh->gh_flags = flags;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392 gh->gh_iflags &= 1 << HIF_ALLOCED;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500393 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394}
395
396/**
397 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
398 * @gh: the holder structure
399 *
400 */
401
402void gfs2_holder_uninit(struct gfs2_holder *gh)
403{
404 gfs2_glock_put(gh->gh_gl);
405 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500406 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407}
408
409/**
410 * gfs2_holder_get - get a struct gfs2_holder structure
411 * @gl: the glock
412 * @state: the state we're requesting
413 * @flags: the modifier flags
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400414 * @gfp_flags:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415 *
416 * Figure out how big an impact this function has. Either:
417 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
418 * 2) Leave it like it is
419 *
420 * Returns: the holder structure, NULL on ENOMEM
421 */
422
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400423static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
424 unsigned int state,
425 int flags, gfp_t gfp_flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426{
427 struct gfs2_holder *gh;
428
429 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
430 if (!gh)
431 return NULL;
432
433 gfs2_holder_init(gl, state, flags, gh);
434 set_bit(HIF_ALLOCED, &gh->gh_iflags);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500435 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000436 return gh;
437}
438
439/**
440 * gfs2_holder_put - get rid of a struct gfs2_holder structure
441 * @gh: the holder structure
442 *
443 */
444
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400445static void gfs2_holder_put(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446{
447 gfs2_holder_uninit(gh);
448 kfree(gh);
449}
450
Steven Whitehousefee852e2007-01-17 15:33:23 +0000451static void gfs2_holder_dispose_or_wake(struct gfs2_holder *gh)
452{
453 if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) {
454 gfs2_holder_put(gh);
455 return;
456 }
457 clear_bit(HIF_WAIT, &gh->gh_iflags);
458 smp_mb();
459 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
460}
461
462static int holder_wait(void *word)
463{
464 schedule();
465 return 0;
466}
467
468static void wait_on_holder(struct gfs2_holder *gh)
469{
470 might_sleep();
471 wait_on_bit(&gh->gh_iflags, HIF_WAIT, holder_wait, TASK_UNINTERRUPTIBLE);
472}
473
David Teiglandb3b94fa2006-01-16 16:50:04 +0000474/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 * rq_mutex - process a mutex request in the queue
476 * @gh: the glock holder
477 *
478 * Returns: 1 if the queue is blocked
479 */
480
481static int rq_mutex(struct gfs2_holder *gh)
482{
483 struct gfs2_glock *gl = gh->gh_gl;
484
485 list_del_init(&gh->gh_list);
486 /* gh->gh_error never examined. */
487 set_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehoused043e192007-01-23 16:56:36 -0500488 clear_bit(HIF_WAIT, &gh->gh_iflags);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000489 smp_mb();
490 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491
492 return 1;
493}
494
495/**
496 * rq_promote - process a promote request in the queue
497 * @gh: the glock holder
498 *
499 * Acquire a new inter-node lock, or change a lock state to more restrictive.
500 *
501 * Returns: 1 if the queue is blocked
502 */
503
504static int rq_promote(struct gfs2_holder *gh)
505{
506 struct gfs2_glock *gl = gh->gh_gl;
507 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508
509 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
510 if (list_empty(&gl->gl_holders)) {
511 gl->gl_req_gh = gh;
512 set_bit(GLF_LOCK, &gl->gl_flags);
513 spin_unlock(&gl->gl_spin);
514
515 if (atomic_read(&sdp->sd_reclaim_count) >
516 gfs2_tune_get(sdp, gt_reclaim_limit) &&
517 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
518 gfs2_reclaim_glock(sdp);
519 gfs2_reclaim_glock(sdp);
520 }
521
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500522 gfs2_glock_xmote_th(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 spin_lock(&gl->gl_spin);
524 }
525 return 1;
526 }
527
528 if (list_empty(&gl->gl_holders)) {
529 set_bit(HIF_FIRST, &gh->gh_iflags);
530 set_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 } else {
532 struct gfs2_holder *next_gh;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500533 if (gh->gh_state == LM_ST_EXCLUSIVE)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 return 1;
535 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
536 gh_list);
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500537 if (next_gh->gh_state == LM_ST_EXCLUSIVE)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 }
540
541 list_move_tail(&gh->gh_list, &gl->gl_holders);
542 gh->gh_error = 0;
543 set_bit(HIF_HOLDER, &gh->gh_iflags);
544
Steven Whitehousefee852e2007-01-17 15:33:23 +0000545 gfs2_holder_dispose_or_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546
547 return 0;
548}
549
550/**
551 * rq_demote - process a demote request in the queue
552 * @gh: the glock holder
553 *
554 * Returns: 1 if the queue is blocked
555 */
556
557static int rq_demote(struct gfs2_holder *gh)
558{
559 struct gfs2_glock *gl = gh->gh_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560
561 if (!list_empty(&gl->gl_holders))
562 return 1;
563
564 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
565 list_del_init(&gh->gh_list);
566 gh->gh_error = 0;
567 spin_unlock(&gl->gl_spin);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000568 gfs2_holder_dispose_or_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 spin_lock(&gl->gl_spin);
570 } else {
571 gl->gl_req_gh = gh;
572 set_bit(GLF_LOCK, &gl->gl_flags);
573 spin_unlock(&gl->gl_spin);
574
575 if (gh->gh_state == LM_ST_UNLOCKED ||
576 gl->gl_state != LM_ST_EXCLUSIVE)
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500577 gfs2_glock_drop_th(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 else
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500579 gfs2_glock_xmote_th(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580
581 spin_lock(&gl->gl_spin);
582 }
583
584 return 0;
585}
586
587/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588 * run_queue - process holder structures on a glock
589 * @gl: the glock
590 *
591 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592static void run_queue(struct gfs2_glock *gl)
593{
594 struct gfs2_holder *gh;
595 int blocked = 1;
596
597 for (;;) {
598 if (test_bit(GLF_LOCK, &gl->gl_flags))
599 break;
600
601 if (!list_empty(&gl->gl_waiters1)) {
602 gh = list_entry(gl->gl_waiters1.next,
603 struct gfs2_holder, gh_list);
604
605 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
606 blocked = rq_mutex(gh);
607 else
608 gfs2_assert_warn(gl->gl_sbd, 0);
609
610 } else if (!list_empty(&gl->gl_waiters2) &&
611 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
612 gh = list_entry(gl->gl_waiters2.next,
613 struct gfs2_holder, gh_list);
614
615 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
616 blocked = rq_demote(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 else
618 gfs2_assert_warn(gl->gl_sbd, 0);
619
620 } else if (!list_empty(&gl->gl_waiters3)) {
621 gh = list_entry(gl->gl_waiters3.next,
622 struct gfs2_holder, gh_list);
623
624 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
625 blocked = rq_promote(gh);
626 else
627 gfs2_assert_warn(gl->gl_sbd, 0);
628
629 } else
630 break;
631
632 if (blocked)
633 break;
634 }
635}
636
637/**
638 * gfs2_glmutex_lock - acquire a local lock on a glock
639 * @gl: the glock
640 *
641 * Gives caller exclusive access to manipulate a glock structure.
642 */
643
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400644static void gfs2_glmutex_lock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000645{
646 struct gfs2_holder gh;
647
648 gfs2_holder_init(gl, 0, 0, &gh);
649 set_bit(HIF_MUTEX, &gh.gh_iflags);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000650 if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
651 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652
653 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400654 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000655 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400656 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400657 gl->gl_owner = current;
658 gl->gl_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000659 clear_bit(HIF_WAIT, &gh.gh_iflags);
660 smp_mb();
661 wake_up_bit(&gh.gh_iflags, HIF_WAIT);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400662 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 spin_unlock(&gl->gl_spin);
664
Steven Whitehousefee852e2007-01-17 15:33:23 +0000665 wait_on_holder(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 gfs2_holder_uninit(&gh);
667}
668
669/**
670 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
671 * @gl: the glock
672 *
673 * Returns: 1 if the glock is acquired
674 */
675
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400676static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677{
678 int acquired = 1;
679
680 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400681 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682 acquired = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400683 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400684 gl->gl_owner = current;
685 gl->gl_ip = (unsigned long)__builtin_return_address(0);
686 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 spin_unlock(&gl->gl_spin);
688
689 return acquired;
690}
691
692/**
693 * gfs2_glmutex_unlock - release a local lock on a glock
694 * @gl: the glock
695 *
696 */
697
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400698static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699{
700 spin_lock(&gl->gl_spin);
701 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400702 gl->gl_owner = NULL;
703 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 run_queue(gl);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400705 BUG_ON(!spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 spin_unlock(&gl->gl_spin);
707}
708
709/**
710 * handle_callback - add a demote request to a lock's queue
711 * @gl: the glock
712 * @state: the state the caller wants us to change to
713 *
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400714 * Note: This may fail sliently if we are out of memory.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715 */
716
717static void handle_callback(struct gfs2_glock *gl, unsigned int state)
718{
719 struct gfs2_holder *gh, *new_gh = NULL;
720
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400721restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722 spin_lock(&gl->gl_spin);
723
724 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
725 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
726 gl->gl_req_gh != gh) {
727 if (gh->gh_state != state)
728 gh->gh_state = LM_ST_UNLOCKED;
729 goto out;
730 }
731 }
732
733 if (new_gh) {
734 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
735 new_gh = NULL;
736 } else {
737 spin_unlock(&gl->gl_spin);
738
Steven Whitehouseab923032006-11-15 15:17:03 -0500739 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_NOFS);
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400740 if (!new_gh)
741 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
743 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000744 set_bit(HIF_WAIT, &new_gh->gh_iflags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745
746 goto restart;
747 }
748
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400749out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750 spin_unlock(&gl->gl_spin);
751
752 if (new_gh)
753 gfs2_holder_put(new_gh);
754}
755
756/**
757 * state_change - record that the glock is now in a different state
758 * @gl: the glock
759 * @new_state the new state
760 *
761 */
762
763static void state_change(struct gfs2_glock *gl, unsigned int new_state)
764{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 int held1, held2;
766
767 held1 = (gl->gl_state != LM_ST_UNLOCKED);
768 held2 = (new_state != LM_ST_UNLOCKED);
769
770 if (held1 != held2) {
David Teigland6a6b3d02006-02-23 10:11:47 +0000771 if (held2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 gfs2_glock_hold(gl);
David Teigland6a6b3d02006-02-23 10:11:47 +0000773 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775 }
776
777 gl->gl_state = new_state;
778}
779
780/**
781 * xmote_bh - Called after the lock module is done acquiring a lock
782 * @gl: The glock in question
783 * @ret: the int returned from the lock module
784 *
785 */
786
787static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
788{
789 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400790 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 struct gfs2_holder *gh = gl->gl_req_gh;
792 int prev_state = gl->gl_state;
793 int op_done = 1;
794
795 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500796 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
798
799 state_change(gl, ret & LM_OUT_ST_MASK);
800
801 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
802 if (glops->go_inval)
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500803 glops->go_inval(gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 } else if (gl->gl_state == LM_ST_DEFERRED) {
805 /* We might not want to do this here.
806 Look at moving to the inode glops. */
807 if (glops->go_inval)
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500808 glops->go_inval(gl, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 }
810
811 /* Deal with each possible exit condition */
812
813 if (!gh)
814 gl->gl_stamp = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
816 spin_lock(&gl->gl_spin);
817 list_del_init(&gh->gh_list);
818 gh->gh_error = -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
821 spin_lock(&gl->gl_spin);
822 list_del_init(&gh->gh_list);
823 if (gl->gl_state == gh->gh_state ||
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400824 gl->gl_state == LM_ST_UNLOCKED) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 gh->gh_error = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400826 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 if (gfs2_assert_warn(sdp, gh->gh_flags &
828 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
829 fs_warn(sdp, "ret = 0x%.8X\n", ret);
830 gh->gh_error = GLR_TRYFAILED;
831 }
832 spin_unlock(&gl->gl_spin);
833
834 if (ret & LM_OUT_CANCELED)
Steven Whitehouse50299962006-09-04 09:49:55 -0400835 handle_callback(gl, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836
837 } else if (ret & LM_OUT_CANCELED) {
838 spin_lock(&gl->gl_spin);
839 list_del_init(&gh->gh_list);
840 gh->gh_error = GLR_CANCELED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 spin_unlock(&gl->gl_spin);
842
843 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
844 spin_lock(&gl->gl_spin);
845 list_move_tail(&gh->gh_list, &gl->gl_holders);
846 gh->gh_error = 0;
847 set_bit(HIF_HOLDER, &gh->gh_iflags);
848 spin_unlock(&gl->gl_spin);
849
850 set_bit(HIF_FIRST, &gh->gh_iflags);
851
852 op_done = 0;
853
854 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
855 spin_lock(&gl->gl_spin);
856 list_del_init(&gh->gh_list);
857 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 spin_unlock(&gl->gl_spin);
859
860 } else {
861 if (gfs2_assert_withdraw(sdp, 0) == -1)
862 fs_err(sdp, "ret = 0x%.8X\n", ret);
863 }
864
865 if (glops->go_xmote_bh)
866 glops->go_xmote_bh(gl);
867
868 if (op_done) {
869 spin_lock(&gl->gl_spin);
870 gl->gl_req_gh = NULL;
871 gl->gl_req_bh = NULL;
872 clear_bit(GLF_LOCK, &gl->gl_flags);
873 run_queue(gl);
874 spin_unlock(&gl->gl_spin);
875 }
876
877 gfs2_glock_put(gl);
878
Steven Whitehousefee852e2007-01-17 15:33:23 +0000879 if (gh)
880 gfs2_holder_dispose_or_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881}
882
883/**
884 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
885 * @gl: The glock in question
886 * @state: the requested state
887 * @flags: modifier flags to the lock call
888 *
889 */
890
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500891void gfs2_glock_xmote_th(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892{
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500893 struct gfs2_glock *gl = gh->gh_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500895 int flags = gh->gh_flags;
896 unsigned state = gh->gh_state;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400897 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
899 LM_FLAG_NOEXP | LM_FLAG_ANY |
900 LM_FLAG_PRIORITY);
901 unsigned int lck_ret;
902
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500903 if (glops->go_xmote_th)
904 glops->go_xmote_th(gl);
905
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500907 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
909 gfs2_assert_warn(sdp, state != gl->gl_state);
910
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 gfs2_glock_hold(gl);
912 gl->gl_req_bh = xmote_bh;
913
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400914 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915
916 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
917 return;
918
919 if (lck_ret & LM_OUT_ASYNC)
920 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
921 else
922 xmote_bh(gl, lck_ret);
923}
924
925/**
926 * drop_bh - Called after a lock module unlock completes
927 * @gl: the glock
928 * @ret: the return status
929 *
930 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
931 * Doesn't drop the reference on the glock the top half took out
932 *
933 */
934
935static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
936{
937 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400938 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 struct gfs2_holder *gh = gl->gl_req_gh;
940
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500942 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 gfs2_assert_warn(sdp, !ret);
944
945 state_change(gl, LM_ST_UNLOCKED);
946
947 if (glops->go_inval)
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500948 glops->go_inval(gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949
950 if (gh) {
951 spin_lock(&gl->gl_spin);
952 list_del_init(&gh->gh_list);
953 gh->gh_error = 0;
954 spin_unlock(&gl->gl_spin);
955 }
956
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957 spin_lock(&gl->gl_spin);
958 gl->gl_req_gh = NULL;
959 gl->gl_req_bh = NULL;
960 clear_bit(GLF_LOCK, &gl->gl_flags);
961 run_queue(gl);
962 spin_unlock(&gl->gl_spin);
963
964 gfs2_glock_put(gl);
965
Steven Whitehousefee852e2007-01-17 15:33:23 +0000966 if (gh)
967 gfs2_holder_dispose_or_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968}
969
970/**
971 * gfs2_glock_drop_th - call into the lock module to unlock a lock
972 * @gl: the glock
973 *
974 */
975
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500976static void gfs2_glock_drop_th(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977{
978 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400979 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 unsigned int ret;
981
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500982 if (glops->go_drop_th)
983 glops->go_drop_th(gl);
984
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500986 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
988
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989 gfs2_glock_hold(gl);
990 gl->gl_req_bh = drop_bh;
991
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
993
994 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
995 return;
996
997 if (!ret)
998 drop_bh(gl, ret);
999 else
1000 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1001}
1002
1003/**
1004 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1005 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1006 *
1007 * Don't cancel GL_NOCANCEL requests.
1008 */
1009
1010static void do_cancels(struct gfs2_holder *gh)
1011{
1012 struct gfs2_glock *gl = gh->gh_gl;
1013
1014 spin_lock(&gl->gl_spin);
1015
1016 while (gl->gl_req_gh != gh &&
1017 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1018 !list_empty(&gh->gh_list)) {
Steven Whitehouse50299962006-09-04 09:49:55 -04001019 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1020 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021 spin_unlock(&gl->gl_spin);
1022 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1023 msleep(100);
1024 spin_lock(&gl->gl_spin);
1025 } else {
1026 spin_unlock(&gl->gl_spin);
1027 msleep(100);
1028 spin_lock(&gl->gl_spin);
1029 }
1030 }
1031
1032 spin_unlock(&gl->gl_spin);
1033}
1034
1035/**
1036 * glock_wait_internal - wait on a glock acquisition
1037 * @gh: the glock holder
1038 *
1039 * Returns: 0 on success
1040 */
1041
1042static int glock_wait_internal(struct gfs2_holder *gh)
1043{
1044 struct gfs2_glock *gl = gh->gh_gl;
1045 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001046 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047
1048 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1049 return -EIO;
1050
1051 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1052 spin_lock(&gl->gl_spin);
1053 if (gl->gl_req_gh != gh &&
1054 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1055 !list_empty(&gh->gh_list)) {
1056 list_del_init(&gh->gh_list);
1057 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058 run_queue(gl);
1059 spin_unlock(&gl->gl_spin);
1060 return gh->gh_error;
1061 }
1062 spin_unlock(&gl->gl_spin);
1063 }
1064
1065 if (gh->gh_flags & LM_FLAG_PRIORITY)
1066 do_cancels(gh);
1067
Steven Whitehousefee852e2007-01-17 15:33:23 +00001068 wait_on_holder(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069 if (gh->gh_error)
1070 return gh->gh_error;
1071
1072 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001073 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074 gh->gh_flags));
1075
1076 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1077 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1078
1079 if (glops->go_lock) {
1080 gh->gh_error = glops->go_lock(gh);
1081 if (gh->gh_error) {
1082 spin_lock(&gl->gl_spin);
1083 list_del_init(&gh->gh_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 spin_unlock(&gl->gl_spin);
1085 }
1086 }
1087
1088 spin_lock(&gl->gl_spin);
1089 gl->gl_req_gh = NULL;
1090 gl->gl_req_bh = NULL;
1091 clear_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 run_queue(gl);
1093 spin_unlock(&gl->gl_spin);
1094 }
1095
1096 return gh->gh_error;
1097}
1098
1099static inline struct gfs2_holder *
1100find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1101{
1102 struct gfs2_holder *gh;
1103
1104 list_for_each_entry(gh, head, gh_list) {
1105 if (gh->gh_owner == owner)
1106 return gh;
1107 }
1108
1109 return NULL;
1110}
1111
1112/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1114 * @gh: the holder structure to add
1115 *
1116 */
1117
1118static void add_to_queue(struct gfs2_holder *gh)
1119{
1120 struct gfs2_glock *gl = gh->gh_gl;
1121 struct gfs2_holder *existing;
1122
Steven Whitehouse190562b2006-04-20 16:57:23 -04001123 BUG_ON(!gh->gh_owner);
Steven Whitehousefee852e2007-01-17 15:33:23 +00001124 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1125 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -04001126
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1128 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001129 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001130 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001131 printk(KERN_INFO "lock type : %d lock state : %d\n",
Abhijith Das86384602006-08-25 11:13:37 -05001132 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001133 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001134 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001135 printk(KERN_INFO "lock type : %d lock state : %d\n",
Abhijith Das86384602006-08-25 11:13:37 -05001136 gl->gl_name.ln_type, gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001137 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 }
1139
1140 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1141 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001142 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1143 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1144 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145 }
1146
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 if (gh->gh_flags & LM_FLAG_PRIORITY)
1148 list_add(&gh->gh_list, &gl->gl_waiters3);
1149 else
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001150 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151}
1152
1153/**
1154 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1155 * @gh: the holder structure
1156 *
1157 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1158 *
1159 * Returns: 0, GLR_TRYFAILED, or errno on failure
1160 */
1161
1162int gfs2_glock_nq(struct gfs2_holder *gh)
1163{
1164 struct gfs2_glock *gl = gh->gh_gl;
1165 struct gfs2_sbd *sdp = gl->gl_sbd;
1166 int error = 0;
1167
Steven Whitehouse320dd102006-05-18 16:25:27 -04001168restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1170 set_bit(HIF_ABORTED, &gh->gh_iflags);
1171 return -EIO;
1172 }
1173
1174 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1175
1176 spin_lock(&gl->gl_spin);
1177 add_to_queue(gh);
1178 run_queue(gl);
1179 spin_unlock(&gl->gl_spin);
1180
1181 if (!(gh->gh_flags & GL_ASYNC)) {
1182 error = glock_wait_internal(gh);
1183 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001184 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001185 goto restart;
1186 }
1187 }
1188
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 return error;
1190}
1191
1192/**
1193 * gfs2_glock_poll - poll to see if an async request has been completed
1194 * @gh: the holder
1195 *
1196 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1197 */
1198
1199int gfs2_glock_poll(struct gfs2_holder *gh)
1200{
1201 struct gfs2_glock *gl = gh->gh_gl;
1202 int ready = 0;
1203
1204 spin_lock(&gl->gl_spin);
1205
1206 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1207 ready = 1;
1208 else if (list_empty(&gh->gh_list)) {
1209 if (gh->gh_error == GLR_CANCELED) {
1210 spin_unlock(&gl->gl_spin);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001211 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212 if (gfs2_glock_nq(gh))
1213 return 1;
1214 return 0;
1215 } else
1216 ready = 1;
1217 }
1218
1219 spin_unlock(&gl->gl_spin);
1220
1221 return ready;
1222}
1223
1224/**
1225 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1226 * @gh: the holder structure
1227 *
1228 * Returns: 0, GLR_TRYFAILED, or errno on failure
1229 */
1230
1231int gfs2_glock_wait(struct gfs2_holder *gh)
1232{
1233 int error;
1234
1235 error = glock_wait_internal(gh);
1236 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001237 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238 gh->gh_flags &= ~GL_ASYNC;
1239 error = gfs2_glock_nq(gh);
1240 }
1241
1242 return error;
1243}
1244
1245/**
1246 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1247 * @gh: the glock holder
1248 *
1249 */
1250
1251void gfs2_glock_dq(struct gfs2_holder *gh)
1252{
1253 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001254 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256 if (gh->gh_flags & GL_NOCACHE)
1257 handle_callback(gl, LM_ST_UNLOCKED);
1258
1259 gfs2_glmutex_lock(gl);
1260
1261 spin_lock(&gl->gl_spin);
1262 list_del_init(&gh->gh_list);
1263
1264 if (list_empty(&gl->gl_holders)) {
1265 spin_unlock(&gl->gl_spin);
1266
1267 if (glops->go_unlock)
1268 glops->go_unlock(gh);
1269
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270 gl->gl_stamp = jiffies;
1271
1272 spin_lock(&gl->gl_spin);
1273 }
1274
1275 clear_bit(GLF_LOCK, &gl->gl_flags);
1276 run_queue(gl);
1277 spin_unlock(&gl->gl_spin);
1278}
1279
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1282 * @gh: the holder structure
1283 *
1284 */
1285
1286void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1287{
1288 gfs2_glock_dq(gh);
1289 gfs2_holder_uninit(gh);
1290}
1291
1292/**
1293 * gfs2_glock_nq_num - acquire a glock based on lock number
1294 * @sdp: the filesystem
1295 * @number: the lock number
1296 * @glops: the glock operations for the type of glock
1297 * @state: the state to acquire the glock in
1298 * @flags: modifier flags for the aquisition
1299 * @gh: the struct gfs2_holder
1300 *
1301 * Returns: errno
1302 */
1303
Steven Whitehousecd915492006-09-04 12:49:07 -04001304int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001305 const struct gfs2_glock_operations *glops,
1306 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307{
1308 struct gfs2_glock *gl;
1309 int error;
1310
1311 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1312 if (!error) {
1313 error = gfs2_glock_nq_init(gl, state, flags, gh);
1314 gfs2_glock_put(gl);
1315 }
1316
1317 return error;
1318}
1319
1320/**
1321 * glock_compare - Compare two struct gfs2_glock structures for sorting
1322 * @arg_a: the first structure
1323 * @arg_b: the second structure
1324 *
1325 */
1326
1327static int glock_compare(const void *arg_a, const void *arg_b)
1328{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001329 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1330 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1331 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1332 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333
1334 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001335 return 1;
1336 if (a->ln_number < b->ln_number)
1337 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001338 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001339 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340}
1341
1342/**
1343 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1344 * @num_gh: the number of structures
1345 * @ghs: an array of struct gfs2_holder structures
1346 *
1347 * Returns: 0 on success (all glocks acquired),
1348 * errno on failure (no glocks acquired)
1349 */
1350
1351static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1352 struct gfs2_holder **p)
1353{
1354 unsigned int x;
1355 int error = 0;
1356
1357 for (x = 0; x < num_gh; x++)
1358 p[x] = &ghs[x];
1359
1360 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1361
1362 for (x = 0; x < num_gh; x++) {
1363 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1364
1365 error = gfs2_glock_nq(p[x]);
1366 if (error) {
1367 while (x--)
1368 gfs2_glock_dq(p[x]);
1369 break;
1370 }
1371 }
1372
1373 return error;
1374}
1375
1376/**
1377 * gfs2_glock_nq_m - acquire multiple glocks
1378 * @num_gh: the number of structures
1379 * @ghs: an array of struct gfs2_holder structures
1380 *
1381 * Figure out how big an impact this function has. Either:
1382 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1383 * 2) Forget async stuff and just call nq_m_sync()
1384 * 3) Leave it like it is
1385 *
1386 * Returns: 0 on success (all glocks acquired),
1387 * errno on failure (no glocks acquired)
1388 */
1389
1390int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1391{
1392 int *e;
1393 unsigned int x;
1394 int borked = 0, serious = 0;
1395 int error = 0;
1396
1397 if (!num_gh)
1398 return 0;
1399
1400 if (num_gh == 1) {
1401 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1402 return gfs2_glock_nq(ghs);
1403 }
1404
1405 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1406 if (!e)
1407 return -ENOMEM;
1408
1409 for (x = 0; x < num_gh; x++) {
1410 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1411 error = gfs2_glock_nq(&ghs[x]);
1412 if (error) {
1413 borked = 1;
1414 serious = error;
1415 num_gh = x;
1416 break;
1417 }
1418 }
1419
1420 for (x = 0; x < num_gh; x++) {
1421 error = e[x] = glock_wait_internal(&ghs[x]);
1422 if (error) {
1423 borked = 1;
1424 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1425 serious = error;
1426 }
1427 }
1428
1429 if (!borked) {
1430 kfree(e);
1431 return 0;
1432 }
1433
1434 for (x = 0; x < num_gh; x++)
1435 if (!e[x])
1436 gfs2_glock_dq(&ghs[x]);
1437
1438 if (serious)
1439 error = serious;
1440 else {
1441 for (x = 0; x < num_gh; x++)
1442 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1443 &ghs[x]);
1444 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1445 }
1446
1447 kfree(e);
1448
1449 return error;
1450}
1451
1452/**
1453 * gfs2_glock_dq_m - release multiple glocks
1454 * @num_gh: the number of structures
1455 * @ghs: an array of struct gfs2_holder structures
1456 *
1457 */
1458
1459void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1460{
1461 unsigned int x;
1462
1463 for (x = 0; x < num_gh; x++)
1464 gfs2_glock_dq(&ghs[x]);
1465}
1466
1467/**
1468 * gfs2_glock_dq_uninit_m - release multiple glocks
1469 * @num_gh: the number of structures
1470 * @ghs: an array of struct gfs2_holder structures
1471 *
1472 */
1473
1474void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1475{
1476 unsigned int x;
1477
1478 for (x = 0; x < num_gh; x++)
1479 gfs2_glock_dq_uninit(&ghs[x]);
1480}
1481
1482/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001483 * gfs2_lvb_hold - attach a LVB from a glock
1484 * @gl: The glock in question
1485 *
1486 */
1487
1488int gfs2_lvb_hold(struct gfs2_glock *gl)
1489{
1490 int error;
1491
1492 gfs2_glmutex_lock(gl);
1493
1494 if (!atomic_read(&gl->gl_lvb_count)) {
1495 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1496 if (error) {
1497 gfs2_glmutex_unlock(gl);
1498 return error;
1499 }
1500 gfs2_glock_hold(gl);
1501 }
1502 atomic_inc(&gl->gl_lvb_count);
1503
1504 gfs2_glmutex_unlock(gl);
1505
1506 return 0;
1507}
1508
1509/**
1510 * gfs2_lvb_unhold - detach a LVB from a glock
1511 * @gl: The glock in question
1512 *
1513 */
1514
1515void gfs2_lvb_unhold(struct gfs2_glock *gl)
1516{
1517 gfs2_glock_hold(gl);
1518 gfs2_glmutex_lock(gl);
1519
1520 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1521 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1522 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1523 gl->gl_lvb = NULL;
1524 gfs2_glock_put(gl);
1525 }
1526
1527 gfs2_glmutex_unlock(gl);
1528 gfs2_glock_put(gl);
1529}
1530
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1532 unsigned int state)
1533{
1534 struct gfs2_glock *gl;
1535
1536 gl = gfs2_glock_find(sdp, name);
1537 if (!gl)
1538 return;
1539
David Teiglandb3b94fa2006-01-16 16:50:04 +00001540 handle_callback(gl, state);
1541
1542 spin_lock(&gl->gl_spin);
1543 run_queue(gl);
1544 spin_unlock(&gl->gl_spin);
1545
1546 gfs2_glock_put(gl);
1547}
1548
1549/**
1550 * gfs2_glock_cb - Callback used by locking module
Steven Whitehouse1c089c32006-09-07 15:50:20 -04001551 * @sdp: Pointer to the superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 * @type: Type of callback
1553 * @data: Type dependent data pointer
1554 *
1555 * Called by the locking module when it wants to tell us something.
1556 * Either we need to drop a lock, one of our ASYNC requests completed, or
1557 * a journal from another client needs to be recovered.
1558 */
1559
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001560void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001561{
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001562 struct gfs2_sbd *sdp = cb_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001563
David Teiglandb3b94fa2006-01-16 16:50:04 +00001564 switch (type) {
1565 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001566 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001567 return;
1568
1569 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001570 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001571 return;
1572
1573 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001574 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001575 return;
1576
1577 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001578 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001579 struct gfs2_glock *gl;
1580
Steven Whitehouse61be0842007-01-29 11:51:45 +00001581 down_read(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001582 gl = gfs2_glock_find(sdp, &async->lc_name);
1583 if (gfs2_assert_warn(sdp, gl))
1584 return;
1585 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1586 gl->gl_req_bh(gl, async->lc_ret);
1587 gfs2_glock_put(gl);
Steven Whitehouse61be0842007-01-29 11:51:45 +00001588 up_read(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001589 return;
1590 }
1591
1592 case LM_CB_NEED_RECOVERY:
1593 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1594 if (sdp->sd_recoverd_process)
1595 wake_up_process(sdp->sd_recoverd_process);
1596 return;
1597
1598 case LM_CB_DROPLOCKS:
1599 gfs2_gl_hash_clear(sdp, NO_WAIT);
1600 gfs2_quota_scan(sdp);
1601 return;
1602
1603 default:
1604 gfs2_assert_warn(sdp, 0);
1605 return;
1606 }
1607}
1608
1609/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001610 * demote_ok - Check to see if it's ok to unlock a glock
1611 * @gl: the glock
1612 *
1613 * Returns: 1 if it's ok
1614 */
1615
1616static int demote_ok(struct gfs2_glock *gl)
1617{
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001618 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001619 int demote = 1;
1620
1621 if (test_bit(GLF_STICKY, &gl->gl_flags))
1622 demote = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001623 else if (glops->go_demote_ok)
1624 demote = glops->go_demote_ok(gl);
1625
1626 return demote;
1627}
1628
1629/**
1630 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1631 * @gl: the glock
1632 *
1633 */
1634
1635void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1636{
1637 struct gfs2_sbd *sdp = gl->gl_sbd;
1638
1639 spin_lock(&sdp->sd_reclaim_lock);
1640 if (list_empty(&gl->gl_reclaim)) {
1641 gfs2_glock_hold(gl);
1642 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1643 atomic_inc(&sdp->sd_reclaim_count);
1644 }
1645 spin_unlock(&sdp->sd_reclaim_lock);
1646
1647 wake_up(&sdp->sd_reclaim_wq);
1648}
1649
1650/**
1651 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1652 * @sdp: the filesystem
1653 *
1654 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1655 * different glock and we notice that there are a lot of glocks in the
1656 * reclaim list.
1657 *
1658 */
1659
1660void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1661{
1662 struct gfs2_glock *gl;
1663
1664 spin_lock(&sdp->sd_reclaim_lock);
1665 if (list_empty(&sdp->sd_reclaim_list)) {
1666 spin_unlock(&sdp->sd_reclaim_lock);
1667 return;
1668 }
1669 gl = list_entry(sdp->sd_reclaim_list.next,
1670 struct gfs2_glock, gl_reclaim);
1671 list_del_init(&gl->gl_reclaim);
1672 spin_unlock(&sdp->sd_reclaim_lock);
1673
1674 atomic_dec(&sdp->sd_reclaim_count);
1675 atomic_inc(&sdp->sd_reclaimed);
1676
1677 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse12132932007-01-22 13:09:04 -05001678 if (list_empty(&gl->gl_holders) &&
Steven Whitehouse50299962006-09-04 09:49:55 -04001679 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680 handle_callback(gl, LM_ST_UNLOCKED);
1681 gfs2_glmutex_unlock(gl);
1682 }
1683
1684 gfs2_glock_put(gl);
1685}
1686
1687/**
1688 * examine_bucket - Call a function for glock in a hash bucket
1689 * @examiner: the function
1690 * @sdp: the filesystem
1691 * @bucket: the bucket
1692 *
1693 * Returns: 1 if the bucket has entries
1694 */
1695
1696static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001697 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001698{
Steven Whitehouse24264432006-09-11 21:40:30 -04001699 struct gfs2_glock *gl, *prev = NULL;
1700 int has_entries = 0;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001701 struct hlist_head *head = &gl_hash_table[hash].hb_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001702
Steven Whitehouse24264432006-09-11 21:40:30 -04001703 read_lock(gl_lock_addr(hash));
Steven Whitehouseb6397892006-09-12 10:10:01 -04001704 /* Can't use hlist_for_each_entry - don't want prefetch here */
1705 if (hlist_empty(head))
Steven Whitehouse24264432006-09-11 21:40:30 -04001706 goto out;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001707 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1708 while(1) {
Steven Whitehouse24264432006-09-11 21:40:30 -04001709 if (gl->gl_sbd == sdp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001710 gfs2_glock_hold(gl);
Steven Whitehouse24264432006-09-11 21:40:30 -04001711 read_unlock(gl_lock_addr(hash));
1712 if (prev)
1713 gfs2_glock_put(prev);
1714 prev = gl;
1715 examiner(gl);
Steven Whitehousea8336342006-09-14 13:57:38 -04001716 has_entries = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04001717 read_lock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001718 }
Steven Whitehouseb6397892006-09-12 10:10:01 -04001719 if (gl->gl_list.next == NULL)
1720 break;
Steven Whitehouse24264432006-09-11 21:40:30 -04001721 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722 }
Steven Whitehouse24264432006-09-11 21:40:30 -04001723out:
1724 read_unlock(gl_lock_addr(hash));
1725 if (prev)
1726 gfs2_glock_put(prev);
1727 return has_entries;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728}
1729
1730/**
1731 * scan_glock - look at a glock and see if we can reclaim it
1732 * @gl: the glock to look at
1733 *
1734 */
1735
1736static void scan_glock(struct gfs2_glock *gl)
1737{
Steven Whitehouseb0041572006-11-23 10:51:34 -05001738 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
Steven Whitehouse24264432006-09-11 21:40:30 -04001739 return;
Steven Whitehousea2242db2006-08-24 17:03:05 -04001740
David Teiglandb3b94fa2006-01-16 16:50:04 +00001741 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse12132932007-01-22 13:09:04 -05001742 if (list_empty(&gl->gl_holders) &&
Steven Whitehouse24264432006-09-11 21:40:30 -04001743 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001744 goto out_schedule;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001745 gfs2_glmutex_unlock(gl);
1746 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001747 return;
1748
Steven Whitehouse627add22006-07-05 13:16:19 -04001749out_schedule:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001750 gfs2_glmutex_unlock(gl);
1751 gfs2_glock_schedule_for_reclaim(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001752}
1753
1754/**
1755 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1756 * @sdp: the filesystem
1757 *
1758 */
1759
1760void gfs2_scand_internal(struct gfs2_sbd *sdp)
1761{
1762 unsigned int x;
1763
Steven Whitehouse94610612006-09-09 18:59:27 -04001764 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001765 examine_bucket(scan_glock, sdp, x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001766}
1767
1768/**
1769 * clear_glock - look at a glock and see if we can free it from glock cache
1770 * @gl: the glock to look at
1771 *
1772 */
1773
1774static void clear_glock(struct gfs2_glock *gl)
1775{
1776 struct gfs2_sbd *sdp = gl->gl_sbd;
1777 int released;
1778
1779 spin_lock(&sdp->sd_reclaim_lock);
1780 if (!list_empty(&gl->gl_reclaim)) {
1781 list_del_init(&gl->gl_reclaim);
1782 atomic_dec(&sdp->sd_reclaim_count);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001783 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001784 released = gfs2_glock_put(gl);
1785 gfs2_assert(sdp, !released);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001786 } else {
1787 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001788 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001789
1790 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse90101c32007-01-23 13:20:41 -05001791 if (list_empty(&gl->gl_holders) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001792 gl->gl_state != LM_ST_UNLOCKED)
1793 handle_callback(gl, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001794 gfs2_glmutex_unlock(gl);
1795 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001796}
1797
1798/**
1799 * gfs2_gl_hash_clear - Empty out the glock hash table
1800 * @sdp: the filesystem
1801 * @wait: wait until it's all gone
1802 *
1803 * Called when unmounting the filesystem, or when inter-node lock manager
1804 * requests DROPLOCKS because it is running out of capacity.
1805 */
1806
1807void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
1808{
1809 unsigned long t;
1810 unsigned int x;
1811 int cont;
1812
1813 t = jiffies;
1814
1815 for (;;) {
1816 cont = 0;
Steven Whitehouse24264432006-09-11 21:40:30 -04001817 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001818 if (examine_bucket(clear_glock, sdp, x))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001819 cont = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04001820 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001821
1822 if (!wait || !cont)
1823 break;
1824
1825 if (time_after_eq(jiffies,
1826 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1827 fs_warn(sdp, "Unmount seems to be stalled. "
1828 "Dumping lock state...\n");
1829 gfs2_dump_lockstate(sdp);
1830 t = jiffies;
1831 }
1832
Steven Whitehouse61be0842007-01-29 11:51:45 +00001833 down_write(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001834 invalidate_inodes(sdp->sd_vfs);
Steven Whitehouse61be0842007-01-29 11:51:45 +00001835 up_write(&gfs2_umount_flush_sem);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001836 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837 }
1838}
1839
1840/*
1841 * Diagnostic routines to help debug distributed deadlock
1842 */
1843
1844/**
1845 * dump_holder - print information about a glock holder
1846 * @str: a string naming the type of holder
1847 * @gh: the glock holder
1848 *
1849 * Returns: 0 on success, -ENOBUFS when we run out of space
1850 */
1851
1852static int dump_holder(char *str, struct gfs2_holder *gh)
1853{
1854 unsigned int x;
1855 int error = -ENOBUFS;
1856
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001857 printk(KERN_INFO " %s\n", str);
1858 printk(KERN_INFO " owner = %ld\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00001859 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001860 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
1861 printk(KERN_INFO " gh_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001862 for (x = 0; x < 32; x++)
1863 if (gh->gh_flags & (1 << x))
1864 printk(" %u", x);
1865 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001866 printk(KERN_INFO " error = %d\n", gh->gh_error);
1867 printk(KERN_INFO " gh_iflags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001868 for (x = 0; x < 32; x++)
1869 if (test_bit(x, &gh->gh_iflags))
1870 printk(" %u", x);
1871 printk(" \n");
Steven Whitehoused0dc80d2006-03-29 14:36:49 -05001872 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001873
1874 error = 0;
1875
1876 return error;
1877}
1878
1879/**
1880 * dump_inode - print information about an inode
1881 * @ip: the inode
1882 *
1883 * Returns: 0 on success, -ENOBUFS when we run out of space
1884 */
1885
1886static int dump_inode(struct gfs2_inode *ip)
1887{
1888 unsigned int x;
1889 int error = -ENOBUFS;
1890
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001891 printk(KERN_INFO " Inode:\n");
1892 printk(KERN_INFO " num = %llu %llu\n",
Steven Whitehouse382066d2006-05-24 10:22:09 -04001893 (unsigned long long)ip->i_num.no_formal_ino,
1894 (unsigned long long)ip->i_num.no_addr);
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001895 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_inode.i_mode));
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001896 printk(KERN_INFO " i_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001897 for (x = 0; x < 32; x++)
1898 if (test_bit(x, &ip->i_flags))
1899 printk(" %u", x);
1900 printk(" \n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001901
1902 error = 0;
1903
1904 return error;
1905}
1906
1907/**
1908 * dump_glock - print information about a glock
1909 * @gl: the glock
1910 * @count: where we are in the buffer
1911 *
1912 * Returns: 0 on success, -ENOBUFS when we run out of space
1913 */
1914
1915static int dump_glock(struct gfs2_glock *gl)
1916{
1917 struct gfs2_holder *gh;
1918 unsigned int x;
1919 int error = -ENOBUFS;
1920
1921 spin_lock(&gl->gl_spin);
1922
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001923 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
Steven Whitehouse382066d2006-05-24 10:22:09 -04001924 (unsigned long long)gl->gl_name.ln_number);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001925 printk(KERN_INFO " gl_flags =");
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001926 for (x = 0; x < 32; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001927 if (test_bit(x, &gl->gl_flags))
1928 printk(" %u", x);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001929 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001930 printk(" \n");
Steven Whitehouse16feb9f2006-09-13 10:43:37 -04001931 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref));
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001932 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
Steven Whitehouse320dd102006-05-18 16:25:27 -04001933 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
1934 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001935 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
1936 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
1937 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
1938 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
1939 printk(KERN_INFO " le = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001941 printk(KERN_INFO " reclaim = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00001942 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
1943 if (gl->gl_aspace)
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001944 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
Russell Cattelan88721872006-08-10 11:08:40 -05001945 gl->gl_aspace->i_mapping->nrpages);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001946 else
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001947 printk(KERN_INFO " aspace = no\n");
1948 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001949 if (gl->gl_req_gh) {
1950 error = dump_holder("Request", gl->gl_req_gh);
1951 if (error)
1952 goto out;
1953 }
1954 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1955 error = dump_holder("Holder", gh);
1956 if (error)
1957 goto out;
1958 }
1959 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
1960 error = dump_holder("Waiter1", gh);
1961 if (error)
1962 goto out;
1963 }
1964 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
1965 error = dump_holder("Waiter2", gh);
1966 if (error)
1967 goto out;
1968 }
1969 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
1970 error = dump_holder("Waiter3", gh);
1971 if (error)
1972 goto out;
1973 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001974 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001975 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
1976 list_empty(&gl->gl_holders)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001977 error = dump_inode(gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001978 if (error)
1979 goto out;
1980 } else {
1981 error = -ENOBUFS;
Steven Whitehoused92a8d42006-02-27 10:57:14 -05001982 printk(KERN_INFO " Inode: busy\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001983 }
1984 }
1985
1986 error = 0;
1987
Steven Whitehousea91ea692006-09-04 12:04:26 -04001988out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001989 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001990 return error;
1991}
1992
1993/**
1994 * gfs2_dump_lockstate - print out the current lockstate
1995 * @sdp: the filesystem
1996 * @ub: the buffer to copy the information into
1997 *
1998 * If @ub is NULL, dump the lockstate to the console.
1999 *
2000 */
2001
Adrian Bunk08bc2db2006-04-28 10:59:12 -04002002static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002003{
David Teiglandb3b94fa2006-01-16 16:50:04 +00002004 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -04002005 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002006 unsigned int x;
2007 int error = 0;
2008
2009 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002010
Steven Whitehouse087efdd2006-09-09 16:59:11 -04002011 read_lock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002012
Steven Whitehouseb6397892006-09-12 10:10:01 -04002013 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002014 if (gl->gl_sbd != sdp)
2015 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002016
2017 error = dump_glock(gl);
2018 if (error)
2019 break;
2020 }
2021
Steven Whitehouse087efdd2006-09-09 16:59:11 -04002022 read_unlock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002023
2024 if (error)
2025 break;
2026 }
2027
2028
2029 return error;
2030}
2031
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002032int __init gfs2_glock_init(void)
2033{
2034 unsigned i;
2035 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehouseb6397892006-09-12 10:10:01 -04002036 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002037 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -04002038#ifdef GL_HASH_LOCK_SZ
2039 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2040 rwlock_init(&gl_hash_locks[i]);
2041 }
2042#endif
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002043 return 0;
2044}
2045