blob: cf54b0b001fdc3c18961007214d6aaacaaa36c40 [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>
Steven Whitehouse24264432006-09-11 21:40:30 -040021#include <linux/list.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include <asm/uaccess.h>
23
24#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "lm_interface.h"
26#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "glock.h"
28#include "glops.h"
29#include "inode.h"
30#include "lm.h"
31#include "lops.h"
32#include "meta_io.h"
33#include "quota.h"
34#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050035#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
David Teiglandb3b94fa2006-01-16 16:50:04 +000037struct greedy {
38 struct gfs2_holder gr_gh;
39 struct work_struct gr_work;
40};
41
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040042struct gfs2_gl_hash_bucket {
Steven Whitehouseb6397892006-09-12 10:10:01 -040043 struct hlist_head hb_list;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040044};
45
David Teiglandb3b94fa2006-01-16 16:50:04 +000046typedef void (*glock_examiner) (struct gfs2_glock * gl);
47
Adrian Bunk08bc2db2006-04-28 10:59:12 -040048static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
Steven Whitehouse320dd102006-05-18 16:25:27 -040049static int dump_glock(struct gfs2_glock *gl);
Steven Whitehouse24264432006-09-11 21:40:30 -040050static int dump_inode(struct gfs2_inode *ip);
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 */
100static inline rwlock_t *gl_lock_addr(x)
101{
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{
182 kref_get(&gl->gl_ref);
183}
184
185/* All work is done after the return from kref_put() so we
186 can release the write_lock before the free. */
187
188static void kill_glock(struct kref *kref)
189{
190 struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref);
191 struct gfs2_sbd *sdp = gl->gl_sbd;
192
193 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
194 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
195 gfs2_assert(sdp, list_empty(&gl->gl_holders));
196 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
197 gfs2_assert(sdp, list_empty(&gl->gl_waiters2));
198 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
199}
200
201/**
202 * gfs2_glock_put() - Decrement reference count on glock
203 * @gl: The glock to put
204 *
205 */
206
207int gfs2_glock_put(struct gfs2_glock *gl)
208{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 int rv = 0;
210
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400211 write_lock(gl_lock_addr(gl->gl_hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 if (kref_put(&gl->gl_ref, kill_glock)) {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400213 hlist_del(&gl->gl_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400214 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse190562b2006-04-20 16:57:23 -0400215 BUG_ON(spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 glock_free(gl);
217 rv = 1;
218 goto out;
219 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400220 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehousea2242db2006-08-24 17:03:05 -0400221out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 return rv;
223}
224
225/**
226 * queue_empty - check to see if a glock's queue is empty
227 * @gl: the glock
228 * @head: the head of the queue to check
229 *
230 * This function protects the list in the event that a process already
231 * has a holder on the list and is adding a second holder for itself.
232 * The glmutex lock is what generally prevents processes from working
233 * on the same glock at once, but the special case of adding a second
234 * holder for yourself ("recursive" locking) doesn't involve locking
235 * glmutex, making the spin lock necessary.
236 *
237 * Returns: 1 if the queue is empty
238 */
239
240static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head)
241{
242 int empty;
243 spin_lock(&gl->gl_spin);
244 empty = list_empty(head);
245 spin_unlock(&gl->gl_spin);
246 return empty;
247}
248
249/**
250 * search_bucket() - Find struct gfs2_glock by lock number
251 * @bucket: the bucket to search
252 * @name: The lock name
253 *
254 * Returns: NULL, or the struct gfs2_glock with the requested number
255 */
256
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400257static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400258 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400259 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260{
261 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -0400262 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263
Steven Whitehouseb6397892006-09-12 10:10:01 -0400264 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265 if (!lm_name_equal(&gl->gl_name, name))
266 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400267 if (gl->gl_sbd != sdp)
268 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269
270 kref_get(&gl->gl_ref);
271
272 return gl;
273 }
274
275 return NULL;
276}
277
278/**
279 * gfs2_glock_find() - Find glock by lock number
280 * @sdp: The GFS2 superblock
281 * @name: The lock name
282 *
283 * Returns: NULL, or the struct gfs2_glock with the requested number
284 */
285
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400286static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400287 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400289 unsigned int hash = gl_hash(sdp, name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 struct gfs2_glock *gl;
291
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400292 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400293 gl = search_bucket(hash, sdp, name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400294 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295
296 return gl;
297}
298
299/**
300 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
301 * @sdp: The GFS2 superblock
302 * @number: the lock number
303 * @glops: The glock_operations to use
304 * @create: If 0, don't create the glock if it doesn't exist
305 * @glp: the glock is returned here
306 *
307 * This does not lock a glock, just finds/creates structures for one.
308 *
309 * Returns: errno
310 */
311
Steven Whitehousecd915492006-09-04 12:49:07 -0400312int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400313 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314 struct gfs2_glock **glp)
315{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400316 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400318 unsigned int hash = gl_hash(sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 int error;
320
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400321 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400322 gl = search_bucket(hash, sdp, &name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400323 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000324
325 if (gl || !create) {
326 *glp = gl;
327 return 0;
328 }
329
330 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
331 if (!gl)
332 return -ENOMEM;
333
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400334 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 gl->gl_name = name;
336 kref_init(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400338 gl->gl_hash = hash;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400339 gl->gl_owner = NULL;
340 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341 gl->gl_ops = glops;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400342 gl->gl_req_gh = NULL;
343 gl->gl_req_bh = NULL;
344 gl->gl_vn = 0;
345 gl->gl_stamp = jiffies;
346 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347 gl->gl_sbd = sdp;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400348 gl->gl_aspace = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350
351 /* If this glock protects actual on-disk data or metadata blocks,
352 create a VFS inode to manage the pages/buffers holding them. */
Steven Whitehouse50299962006-09-04 09:49:55 -0400353 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 gl->gl_aspace = gfs2_aspace_get(sdp);
355 if (!gl->gl_aspace) {
356 error = -ENOMEM;
357 goto fail;
358 }
359 }
360
361 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
362 if (error)
363 goto fail_aspace;
364
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400365 write_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400366 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 if (tmp) {
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400368 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000369 glock_free(gl);
370 gl = tmp;
371 } else {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400372 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400373 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 }
375
376 *glp = gl;
377
378 return 0;
379
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400380fail_aspace:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 if (gl->gl_aspace)
382 gfs2_aspace_put(gl->gl_aspace);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400383fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 return error;
386}
387
388/**
389 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
390 * @gl: the glock
391 * @state: the state we're requesting
392 * @flags: the modifier flags
393 * @gh: the holder structure
394 *
395 */
396
Steven Whitehouse190562b2006-04-20 16:57:23 -0400397void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398 struct gfs2_holder *gh)
399{
400 INIT_LIST_HEAD(&gh->gh_list);
401 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500402 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400403 gh->gh_owner = current;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 gh->gh_state = state;
405 gh->gh_flags = flags;
406 gh->gh_error = 0;
407 gh->gh_iflags = 0;
408 init_completion(&gh->gh_wait);
409
410 if (gh->gh_state == LM_ST_EXCLUSIVE)
411 gh->gh_flags |= GL_LOCAL_EXCL;
412
413 gfs2_glock_hold(gl);
414}
415
416/**
417 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
418 * @state: the state we're requesting
419 * @flags: the modifier flags
420 * @gh: the holder structure
421 *
422 * Don't mess with the glock.
423 *
424 */
425
Steven Whitehouse190562b2006-04-20 16:57:23 -0400426void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427{
428 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400429 gh->gh_flags = flags;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430 if (gh->gh_state == LM_ST_EXCLUSIVE)
431 gh->gh_flags |= GL_LOCAL_EXCL;
432
433 gh->gh_iflags &= 1 << HIF_ALLOCED;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500434 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435}
436
437/**
438 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
439 * @gh: the holder structure
440 *
441 */
442
443void gfs2_holder_uninit(struct gfs2_holder *gh)
444{
445 gfs2_glock_put(gh->gh_gl);
446 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500447 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448}
449
450/**
451 * gfs2_holder_get - get a struct gfs2_holder structure
452 * @gl: the glock
453 * @state: the state we're requesting
454 * @flags: the modifier flags
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400455 * @gfp_flags:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 *
457 * Figure out how big an impact this function has. Either:
458 * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd
459 * 2) Leave it like it is
460 *
461 * Returns: the holder structure, NULL on ENOMEM
462 */
463
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400464static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl,
465 unsigned int state,
466 int flags, gfp_t gfp_flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467{
468 struct gfs2_holder *gh;
469
470 gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags);
471 if (!gh)
472 return NULL;
473
474 gfs2_holder_init(gl, state, flags, gh);
475 set_bit(HIF_ALLOCED, &gh->gh_iflags);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500476 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 return gh;
478}
479
480/**
481 * gfs2_holder_put - get rid of a struct gfs2_holder structure
482 * @gh: the holder structure
483 *
484 */
485
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400486static void gfs2_holder_put(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487{
488 gfs2_holder_uninit(gh);
489 kfree(gh);
490}
491
492/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 * rq_mutex - process a mutex request in the queue
494 * @gh: the glock holder
495 *
496 * Returns: 1 if the queue is blocked
497 */
498
499static int rq_mutex(struct gfs2_holder *gh)
500{
501 struct gfs2_glock *gl = gh->gh_gl;
502
503 list_del_init(&gh->gh_list);
504 /* gh->gh_error never examined. */
505 set_bit(GLF_LOCK, &gl->gl_flags);
506 complete(&gh->gh_wait);
507
508 return 1;
509}
510
511/**
512 * rq_promote - process a promote request in the queue
513 * @gh: the glock holder
514 *
515 * Acquire a new inter-node lock, or change a lock state to more restrictive.
516 *
517 * Returns: 1 if the queue is blocked
518 */
519
520static int rq_promote(struct gfs2_holder *gh)
521{
522 struct gfs2_glock *gl = gh->gh_gl;
523 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400524 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525
526 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
527 if (list_empty(&gl->gl_holders)) {
528 gl->gl_req_gh = gh;
529 set_bit(GLF_LOCK, &gl->gl_flags);
530 spin_unlock(&gl->gl_spin);
531
532 if (atomic_read(&sdp->sd_reclaim_count) >
533 gfs2_tune_get(sdp, gt_reclaim_limit) &&
534 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
535 gfs2_reclaim_glock(sdp);
536 gfs2_reclaim_glock(sdp);
537 }
538
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400539 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 spin_lock(&gl->gl_spin);
541 }
542 return 1;
543 }
544
545 if (list_empty(&gl->gl_holders)) {
546 set_bit(HIF_FIRST, &gh->gh_iflags);
547 set_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 } else {
549 struct gfs2_holder *next_gh;
550 if (gh->gh_flags & GL_LOCAL_EXCL)
551 return 1;
552 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
553 gh_list);
554 if (next_gh->gh_flags & GL_LOCAL_EXCL)
555 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000556 }
557
558 list_move_tail(&gh->gh_list, &gl->gl_holders);
559 gh->gh_error = 0;
560 set_bit(HIF_HOLDER, &gh->gh_iflags);
561
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562 complete(&gh->gh_wait);
563
564 return 0;
565}
566
567/**
568 * rq_demote - process a demote request in the queue
569 * @gh: the glock holder
570 *
571 * Returns: 1 if the queue is blocked
572 */
573
574static int rq_demote(struct gfs2_holder *gh)
575{
576 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400577 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578
579 if (!list_empty(&gl->gl_holders))
580 return 1;
581
582 if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) {
583 list_del_init(&gh->gh_list);
584 gh->gh_error = 0;
585 spin_unlock(&gl->gl_spin);
586 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
587 gfs2_holder_put(gh);
588 else
589 complete(&gh->gh_wait);
590 spin_lock(&gl->gl_spin);
591 } else {
592 gl->gl_req_gh = gh;
593 set_bit(GLF_LOCK, &gl->gl_flags);
594 spin_unlock(&gl->gl_spin);
595
596 if (gh->gh_state == LM_ST_UNLOCKED ||
597 gl->gl_state != LM_ST_EXCLUSIVE)
598 glops->go_drop_th(gl);
599 else
600 glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags);
601
602 spin_lock(&gl->gl_spin);
603 }
604
605 return 0;
606}
607
608/**
609 * rq_greedy - process a queued request to drop greedy status
610 * @gh: the glock holder
611 *
612 * Returns: 1 if the queue is blocked
613 */
614
615static int rq_greedy(struct gfs2_holder *gh)
616{
617 struct gfs2_glock *gl = gh->gh_gl;
618
619 list_del_init(&gh->gh_list);
620 /* gh->gh_error never examined. */
621 clear_bit(GLF_GREEDY, &gl->gl_flags);
622 spin_unlock(&gl->gl_spin);
623
624 gfs2_holder_uninit(gh);
625 kfree(container_of(gh, struct greedy, gr_gh));
626
627 spin_lock(&gl->gl_spin);
628
629 return 0;
630}
631
632/**
633 * run_queue - process holder structures on a glock
634 * @gl: the glock
635 *
636 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637static void run_queue(struct gfs2_glock *gl)
638{
639 struct gfs2_holder *gh;
640 int blocked = 1;
641
642 for (;;) {
643 if (test_bit(GLF_LOCK, &gl->gl_flags))
644 break;
645
646 if (!list_empty(&gl->gl_waiters1)) {
647 gh = list_entry(gl->gl_waiters1.next,
648 struct gfs2_holder, gh_list);
649
650 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
651 blocked = rq_mutex(gh);
652 else
653 gfs2_assert_warn(gl->gl_sbd, 0);
654
655 } else if (!list_empty(&gl->gl_waiters2) &&
656 !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) {
657 gh = list_entry(gl->gl_waiters2.next,
658 struct gfs2_holder, gh_list);
659
660 if (test_bit(HIF_DEMOTE, &gh->gh_iflags))
661 blocked = rq_demote(gh);
662 else if (test_bit(HIF_GREEDY, &gh->gh_iflags))
663 blocked = rq_greedy(gh);
664 else
665 gfs2_assert_warn(gl->gl_sbd, 0);
666
667 } else if (!list_empty(&gl->gl_waiters3)) {
668 gh = list_entry(gl->gl_waiters3.next,
669 struct gfs2_holder, gh_list);
670
671 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
672 blocked = rq_promote(gh);
673 else
674 gfs2_assert_warn(gl->gl_sbd, 0);
675
676 } else
677 break;
678
679 if (blocked)
680 break;
681 }
682}
683
684/**
685 * gfs2_glmutex_lock - acquire a local lock on a glock
686 * @gl: the glock
687 *
688 * Gives caller exclusive access to manipulate a glock structure.
689 */
690
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400691static void gfs2_glmutex_lock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692{
693 struct gfs2_holder gh;
694
695 gfs2_holder_init(gl, 0, 0, &gh);
696 set_bit(HIF_MUTEX, &gh.gh_iflags);
697
698 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400699 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400701 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400702 gl->gl_owner = current;
703 gl->gl_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 complete(&gh.gh_wait);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400705 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 spin_unlock(&gl->gl_spin);
707
708 wait_for_completion(&gh.gh_wait);
709 gfs2_holder_uninit(&gh);
710}
711
712/**
713 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
714 * @gl: the glock
715 *
716 * Returns: 1 if the glock is acquired
717 */
718
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400719static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720{
721 int acquired = 1;
722
723 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400724 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 acquired = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400726 } else {
Steven Whitehouse320dd102006-05-18 16:25:27 -0400727 gl->gl_owner = current;
728 gl->gl_ip = (unsigned long)__builtin_return_address(0);
729 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 spin_unlock(&gl->gl_spin);
731
732 return acquired;
733}
734
735/**
736 * gfs2_glmutex_unlock - release a local lock on a glock
737 * @gl: the glock
738 *
739 */
740
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400741static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742{
743 spin_lock(&gl->gl_spin);
744 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400745 gl->gl_owner = NULL;
746 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 run_queue(gl);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400748 BUG_ON(!spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 spin_unlock(&gl->gl_spin);
750}
751
752/**
753 * handle_callback - add a demote request to a lock's queue
754 * @gl: the glock
755 * @state: the state the caller wants us to change to
756 *
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400757 * Note: This may fail sliently if we are out of memory.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000758 */
759
760static void handle_callback(struct gfs2_glock *gl, unsigned int state)
761{
762 struct gfs2_holder *gh, *new_gh = NULL;
763
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400764restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765 spin_lock(&gl->gl_spin);
766
767 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
768 if (test_bit(HIF_DEMOTE, &gh->gh_iflags) &&
769 gl->gl_req_gh != gh) {
770 if (gh->gh_state != state)
771 gh->gh_state = LM_ST_UNLOCKED;
772 goto out;
773 }
774 }
775
776 if (new_gh) {
777 list_add_tail(&new_gh->gh_list, &gl->gl_waiters2);
778 new_gh = NULL;
779 } else {
780 spin_unlock(&gl->gl_spin);
781
Steven Whitehouseaf18ddb2006-06-24 15:42:21 -0400782 new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_KERNEL);
783 if (!new_gh)
784 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 set_bit(HIF_DEMOTE, &new_gh->gh_iflags);
786 set_bit(HIF_DEALLOC, &new_gh->gh_iflags);
787
788 goto restart;
789 }
790
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400791out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792 spin_unlock(&gl->gl_spin);
793
794 if (new_gh)
795 gfs2_holder_put(new_gh);
796}
797
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400798void gfs2_glock_inode_squish(struct inode *inode)
799{
800 struct gfs2_holder gh;
801 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
802 gfs2_holder_init(gl, LM_ST_UNLOCKED, 0, &gh);
803 set_bit(HIF_DEMOTE, &gh.gh_iflags);
804 spin_lock(&gl->gl_spin);
805 gfs2_assert(inode->i_sb->s_fs_info, list_empty(&gl->gl_holders));
806 list_add_tail(&gh.gh_list, &gl->gl_waiters2);
807 run_queue(gl);
808 spin_unlock(&gl->gl_spin);
Steven Whitehouse5dd9fea2006-07-28 14:52:33 -0400809 wait_for_completion(&gh.gh_wait);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400810 gfs2_holder_uninit(&gh);
811}
812
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813/**
814 * state_change - record that the glock is now in a different state
815 * @gl: the glock
816 * @new_state the new state
817 *
818 */
819
820static void state_change(struct gfs2_glock *gl, unsigned int new_state)
821{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 int held1, held2;
823
824 held1 = (gl->gl_state != LM_ST_UNLOCKED);
825 held2 = (new_state != LM_ST_UNLOCKED);
826
827 if (held1 != held2) {
David Teigland6a6b3d02006-02-23 10:11:47 +0000828 if (held2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 gfs2_glock_hold(gl);
David Teigland6a6b3d02006-02-23 10:11:47 +0000830 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 }
833
834 gl->gl_state = new_state;
835}
836
837/**
838 * xmote_bh - Called after the lock module is done acquiring a lock
839 * @gl: The glock in question
840 * @ret: the int returned from the lock module
841 *
842 */
843
844static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
845{
846 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400847 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 struct gfs2_holder *gh = gl->gl_req_gh;
849 int prev_state = gl->gl_state;
850 int op_done = 1;
851
852 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
853 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
854 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
855
856 state_change(gl, ret & LM_OUT_ST_MASK);
857
858 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
859 if (glops->go_inval)
860 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
861 } else if (gl->gl_state == LM_ST_DEFERRED) {
862 /* We might not want to do this here.
863 Look at moving to the inode glops. */
864 if (glops->go_inval)
865 glops->go_inval(gl, DIO_DATA);
866 }
867
868 /* Deal with each possible exit condition */
869
870 if (!gh)
871 gl->gl_stamp = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
873 spin_lock(&gl->gl_spin);
874 list_del_init(&gh->gh_list);
875 gh->gh_error = -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) {
878 spin_lock(&gl->gl_spin);
879 list_del_init(&gh->gh_list);
880 if (gl->gl_state == gh->gh_state ||
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400881 gl->gl_state == LM_ST_UNLOCKED) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 gh->gh_error = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400883 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 if (gfs2_assert_warn(sdp, gh->gh_flags &
885 (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1)
886 fs_warn(sdp, "ret = 0x%.8X\n", ret);
887 gh->gh_error = GLR_TRYFAILED;
888 }
889 spin_unlock(&gl->gl_spin);
890
891 if (ret & LM_OUT_CANCELED)
Steven Whitehouse50299962006-09-04 09:49:55 -0400892 handle_callback(gl, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893
894 } else if (ret & LM_OUT_CANCELED) {
895 spin_lock(&gl->gl_spin);
896 list_del_init(&gh->gh_list);
897 gh->gh_error = GLR_CANCELED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 spin_unlock(&gl->gl_spin);
899
900 } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
901 spin_lock(&gl->gl_spin);
902 list_move_tail(&gh->gh_list, &gl->gl_holders);
903 gh->gh_error = 0;
904 set_bit(HIF_HOLDER, &gh->gh_iflags);
905 spin_unlock(&gl->gl_spin);
906
907 set_bit(HIF_FIRST, &gh->gh_iflags);
908
909 op_done = 0;
910
911 } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
912 spin_lock(&gl->gl_spin);
913 list_del_init(&gh->gh_list);
914 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 spin_unlock(&gl->gl_spin);
916
917 } else {
918 if (gfs2_assert_withdraw(sdp, 0) == -1)
919 fs_err(sdp, "ret = 0x%.8X\n", ret);
920 }
921
922 if (glops->go_xmote_bh)
923 glops->go_xmote_bh(gl);
924
925 if (op_done) {
926 spin_lock(&gl->gl_spin);
927 gl->gl_req_gh = NULL;
928 gl->gl_req_bh = NULL;
929 clear_bit(GLF_LOCK, &gl->gl_flags);
930 run_queue(gl);
931 spin_unlock(&gl->gl_spin);
932 }
933
934 gfs2_glock_put(gl);
935
936 if (gh) {
937 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
938 gfs2_holder_put(gh);
939 else
940 complete(&gh->gh_wait);
941 }
942}
943
944/**
945 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
946 * @gl: The glock in question
947 * @state: the requested state
948 * @flags: modifier flags to the lock call
949 *
950 */
951
952void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags)
953{
954 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400955 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
957 LM_FLAG_NOEXP | LM_FLAG_ANY |
958 LM_FLAG_PRIORITY);
959 unsigned int lck_ret;
960
961 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
962 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
963 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
964 gfs2_assert_warn(sdp, state != gl->gl_state);
965
Steven Whitehouse50299962006-09-04 09:49:55 -0400966 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
967 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968
969 gfs2_glock_hold(gl);
970 gl->gl_req_bh = xmote_bh;
971
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400972 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973
974 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
975 return;
976
977 if (lck_ret & LM_OUT_ASYNC)
978 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
979 else
980 xmote_bh(gl, lck_ret);
981}
982
983/**
984 * drop_bh - Called after a lock module unlock completes
985 * @gl: the glock
986 * @ret: the return status
987 *
988 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
989 * Doesn't drop the reference on the glock the top half took out
990 *
991 */
992
993static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
994{
995 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400996 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997 struct gfs2_holder *gh = gl->gl_req_gh;
998
999 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1000
1001 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1002 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1003 gfs2_assert_warn(sdp, !ret);
1004
1005 state_change(gl, LM_ST_UNLOCKED);
1006
1007 if (glops->go_inval)
1008 glops->go_inval(gl, DIO_METADATA | DIO_DATA);
1009
1010 if (gh) {
1011 spin_lock(&gl->gl_spin);
1012 list_del_init(&gh->gh_list);
1013 gh->gh_error = 0;
1014 spin_unlock(&gl->gl_spin);
1015 }
1016
1017 if (glops->go_drop_bh)
1018 glops->go_drop_bh(gl);
1019
1020 spin_lock(&gl->gl_spin);
1021 gl->gl_req_gh = NULL;
1022 gl->gl_req_bh = NULL;
1023 clear_bit(GLF_LOCK, &gl->gl_flags);
1024 run_queue(gl);
1025 spin_unlock(&gl->gl_spin);
1026
1027 gfs2_glock_put(gl);
1028
1029 if (gh) {
1030 if (test_bit(HIF_DEALLOC, &gh->gh_iflags))
1031 gfs2_holder_put(gh);
1032 else
1033 complete(&gh->gh_wait);
1034 }
1035}
1036
1037/**
1038 * gfs2_glock_drop_th - call into the lock module to unlock a lock
1039 * @gl: the glock
1040 *
1041 */
1042
1043void gfs2_glock_drop_th(struct gfs2_glock *gl)
1044{
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 unsigned int ret;
1048
1049 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1050 gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders));
1051 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
1052
Steven Whitehouse50299962006-09-04 09:49:55 -04001053 if (gl->gl_state == LM_ST_EXCLUSIVE && glops->go_sync)
1054 glops->go_sync(gl, DIO_METADATA | DIO_DATA | DIO_RELEASE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055
1056 gfs2_glock_hold(gl);
1057 gl->gl_req_bh = drop_bh;
1058
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
1060
1061 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
1062 return;
1063
1064 if (!ret)
1065 drop_bh(gl, ret);
1066 else
1067 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
1068}
1069
1070/**
1071 * do_cancels - cancel requests for locks stuck waiting on an expire flag
1072 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
1073 *
1074 * Don't cancel GL_NOCANCEL requests.
1075 */
1076
1077static void do_cancels(struct gfs2_holder *gh)
1078{
1079 struct gfs2_glock *gl = gh->gh_gl;
1080
1081 spin_lock(&gl->gl_spin);
1082
1083 while (gl->gl_req_gh != gh &&
1084 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1085 !list_empty(&gh->gh_list)) {
Steven Whitehouse50299962006-09-04 09:49:55 -04001086 if (gl->gl_req_bh && !(gl->gl_req_gh &&
1087 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 spin_unlock(&gl->gl_spin);
1089 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
1090 msleep(100);
1091 spin_lock(&gl->gl_spin);
1092 } else {
1093 spin_unlock(&gl->gl_spin);
1094 msleep(100);
1095 spin_lock(&gl->gl_spin);
1096 }
1097 }
1098
1099 spin_unlock(&gl->gl_spin);
1100}
1101
1102/**
1103 * glock_wait_internal - wait on a glock acquisition
1104 * @gh: the glock holder
1105 *
1106 * Returns: 0 on success
1107 */
1108
1109static int glock_wait_internal(struct gfs2_holder *gh)
1110{
1111 struct gfs2_glock *gl = gh->gh_gl;
1112 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001113 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114
1115 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1116 return -EIO;
1117
1118 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1119 spin_lock(&gl->gl_spin);
1120 if (gl->gl_req_gh != gh &&
1121 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1122 !list_empty(&gh->gh_list)) {
1123 list_del_init(&gh->gh_list);
1124 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 run_queue(gl);
1126 spin_unlock(&gl->gl_spin);
1127 return gh->gh_error;
1128 }
1129 spin_unlock(&gl->gl_spin);
1130 }
1131
1132 if (gh->gh_flags & LM_FLAG_PRIORITY)
1133 do_cancels(gh);
1134
1135 wait_for_completion(&gh->gh_wait);
1136
1137 if (gh->gh_error)
1138 return gh->gh_error;
1139
1140 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001141 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 gh->gh_flags));
1143
1144 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1145 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1146
1147 if (glops->go_lock) {
1148 gh->gh_error = glops->go_lock(gh);
1149 if (gh->gh_error) {
1150 spin_lock(&gl->gl_spin);
1151 list_del_init(&gh->gh_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 spin_unlock(&gl->gl_spin);
1153 }
1154 }
1155
1156 spin_lock(&gl->gl_spin);
1157 gl->gl_req_gh = NULL;
1158 gl->gl_req_bh = NULL;
1159 clear_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 run_queue(gl);
1161 spin_unlock(&gl->gl_spin);
1162 }
1163
1164 return gh->gh_error;
1165}
1166
1167static inline struct gfs2_holder *
1168find_holder_by_owner(struct list_head *head, struct task_struct *owner)
1169{
1170 struct gfs2_holder *gh;
1171
1172 list_for_each_entry(gh, head, gh_list) {
1173 if (gh->gh_owner == owner)
1174 return gh;
1175 }
1176
1177 return NULL;
1178}
1179
1180/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1182 * @gh: the holder structure to add
1183 *
1184 */
1185
1186static void add_to_queue(struct gfs2_holder *gh)
1187{
1188 struct gfs2_glock *gl = gh->gh_gl;
1189 struct gfs2_holder *existing;
1190
Steven Whitehouse190562b2006-04-20 16:57:23 -04001191 BUG_ON(!gh->gh_owner);
1192
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner);
1194 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001195 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001196 printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid);
1197 printk(KERN_INFO "lock type : %d lock state : %d\n",
1198 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001199 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
Abhijith Das86384602006-08-25 11:13:37 -05001200 printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid);
1201 printk(KERN_INFO "lock type : %d lock state : %d\n",
1202 gl->gl_name.ln_type, gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001203 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 }
1205
1206 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner);
1207 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001208 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1209 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1210 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 }
1212
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 if (gh->gh_flags & LM_FLAG_PRIORITY)
1214 list_add(&gh->gh_list, &gl->gl_waiters3);
1215 else
1216 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1217}
1218
1219/**
1220 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1221 * @gh: the holder structure
1222 *
1223 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1224 *
1225 * Returns: 0, GLR_TRYFAILED, or errno on failure
1226 */
1227
1228int gfs2_glock_nq(struct gfs2_holder *gh)
1229{
1230 struct gfs2_glock *gl = gh->gh_gl;
1231 struct gfs2_sbd *sdp = gl->gl_sbd;
1232 int error = 0;
1233
Steven Whitehouse320dd102006-05-18 16:25:27 -04001234restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1236 set_bit(HIF_ABORTED, &gh->gh_iflags);
1237 return -EIO;
1238 }
1239
1240 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1241
1242 spin_lock(&gl->gl_spin);
1243 add_to_queue(gh);
1244 run_queue(gl);
1245 spin_unlock(&gl->gl_spin);
1246
1247 if (!(gh->gh_flags & GL_ASYNC)) {
1248 error = glock_wait_internal(gh);
1249 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001250 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001251 goto restart;
1252 }
1253 }
1254
1255 clear_bit(GLF_PREFETCH, &gl->gl_flags);
1256
Steven Whitehouse320dd102006-05-18 16:25:27 -04001257 if (error == GLR_TRYFAILED && (gh->gh_flags & GL_DUMP))
1258 dump_glock(gl);
1259
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260 return error;
1261}
1262
1263/**
1264 * gfs2_glock_poll - poll to see if an async request has been completed
1265 * @gh: the holder
1266 *
1267 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1268 */
1269
1270int gfs2_glock_poll(struct gfs2_holder *gh)
1271{
1272 struct gfs2_glock *gl = gh->gh_gl;
1273 int ready = 0;
1274
1275 spin_lock(&gl->gl_spin);
1276
1277 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1278 ready = 1;
1279 else if (list_empty(&gh->gh_list)) {
1280 if (gh->gh_error == GLR_CANCELED) {
1281 spin_unlock(&gl->gl_spin);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001282 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283 if (gfs2_glock_nq(gh))
1284 return 1;
1285 return 0;
1286 } else
1287 ready = 1;
1288 }
1289
1290 spin_unlock(&gl->gl_spin);
1291
1292 return ready;
1293}
1294
1295/**
1296 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1297 * @gh: the holder structure
1298 *
1299 * Returns: 0, GLR_TRYFAILED, or errno on failure
1300 */
1301
1302int gfs2_glock_wait(struct gfs2_holder *gh)
1303{
1304 int error;
1305
1306 error = glock_wait_internal(gh);
1307 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001308 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 gh->gh_flags &= ~GL_ASYNC;
1310 error = gfs2_glock_nq(gh);
1311 }
1312
1313 return error;
1314}
1315
1316/**
1317 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1318 * @gh: the glock holder
1319 *
1320 */
1321
1322void gfs2_glock_dq(struct gfs2_holder *gh)
1323{
1324 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001325 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001326
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327 if (gh->gh_flags & GL_NOCACHE)
1328 handle_callback(gl, LM_ST_UNLOCKED);
1329
1330 gfs2_glmutex_lock(gl);
1331
1332 spin_lock(&gl->gl_spin);
1333 list_del_init(&gh->gh_list);
1334
1335 if (list_empty(&gl->gl_holders)) {
1336 spin_unlock(&gl->gl_spin);
1337
1338 if (glops->go_unlock)
1339 glops->go_unlock(gh);
1340
David Teiglandb3b94fa2006-01-16 16:50:04 +00001341 gl->gl_stamp = jiffies;
1342
1343 spin_lock(&gl->gl_spin);
1344 }
1345
1346 clear_bit(GLF_LOCK, &gl->gl_flags);
1347 run_queue(gl);
1348 spin_unlock(&gl->gl_spin);
1349}
1350
1351/**
1352 * gfs2_glock_prefetch - Try to prefetch a glock
1353 * @gl: the glock
1354 * @state: the state to prefetch in
1355 * @flags: flags passed to go_xmote_th()
1356 *
1357 */
1358
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001359static void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state,
1360 int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001361{
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001362 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001363
1364 spin_lock(&gl->gl_spin);
1365
Steven Whitehouse50299962006-09-04 09:49:55 -04001366 if (test_bit(GLF_LOCK, &gl->gl_flags) || !list_empty(&gl->gl_holders) ||
1367 !list_empty(&gl->gl_waiters1) || !list_empty(&gl->gl_waiters2) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001368 !list_empty(&gl->gl_waiters3) ||
1369 relaxed_state_ok(gl->gl_state, state, flags)) {
1370 spin_unlock(&gl->gl_spin);
1371 return;
1372 }
1373
1374 set_bit(GLF_PREFETCH, &gl->gl_flags);
1375 set_bit(GLF_LOCK, &gl->gl_flags);
1376 spin_unlock(&gl->gl_spin);
1377
1378 glops->go_xmote_th(gl, state, flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001379}
1380
David Teiglandb3b94fa2006-01-16 16:50:04 +00001381static void greedy_work(void *data)
1382{
David Teiglande7f5c012006-04-27 11:25:45 -04001383 struct greedy *gr = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 struct gfs2_holder *gh = &gr->gr_gh;
1385 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001386 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387
1388 clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1389
1390 if (glops->go_greedy)
1391 glops->go_greedy(gl);
1392
1393 spin_lock(&gl->gl_spin);
1394
1395 if (list_empty(&gl->gl_waiters2)) {
1396 clear_bit(GLF_GREEDY, &gl->gl_flags);
1397 spin_unlock(&gl->gl_spin);
1398 gfs2_holder_uninit(gh);
1399 kfree(gr);
1400 } else {
1401 gfs2_glock_hold(gl);
1402 list_add_tail(&gh->gh_list, &gl->gl_waiters2);
1403 run_queue(gl);
1404 spin_unlock(&gl->gl_spin);
1405 gfs2_glock_put(gl);
1406 }
1407}
1408
1409/**
1410 * gfs2_glock_be_greedy -
1411 * @gl:
1412 * @time:
1413 *
1414 * Returns: 0 if go_greedy will be called, 1 otherwise
1415 */
1416
1417int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time)
1418{
1419 struct greedy *gr;
1420 struct gfs2_holder *gh;
1421
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001422 if (!time || gl->gl_sbd->sd_args.ar_localcaching ||
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423 test_and_set_bit(GLF_GREEDY, &gl->gl_flags))
1424 return 1;
1425
1426 gr = kmalloc(sizeof(struct greedy), GFP_KERNEL);
1427 if (!gr) {
1428 clear_bit(GLF_GREEDY, &gl->gl_flags);
1429 return 1;
1430 }
1431 gh = &gr->gr_gh;
1432
Steven Whitehouse579b78a2006-04-26 14:58:26 -04001433 gfs2_holder_init(gl, 0, 0, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001434 set_bit(HIF_GREEDY, &gh->gh_iflags);
1435 INIT_WORK(&gr->gr_work, greedy_work, gr);
1436
1437 set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags);
1438 schedule_delayed_work(&gr->gr_work, time);
1439
1440 return 0;
1441}
1442
1443/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001444 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1445 * @gh: the holder structure
1446 *
1447 */
1448
1449void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1450{
1451 gfs2_glock_dq(gh);
1452 gfs2_holder_uninit(gh);
1453}
1454
1455/**
1456 * gfs2_glock_nq_num - acquire a glock based on lock number
1457 * @sdp: the filesystem
1458 * @number: the lock number
1459 * @glops: the glock operations for the type of glock
1460 * @state: the state to acquire the glock in
1461 * @flags: modifier flags for the aquisition
1462 * @gh: the struct gfs2_holder
1463 *
1464 * Returns: errno
1465 */
1466
Steven Whitehousecd915492006-09-04 12:49:07 -04001467int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001468 const struct gfs2_glock_operations *glops,
1469 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470{
1471 struct gfs2_glock *gl;
1472 int error;
1473
1474 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1475 if (!error) {
1476 error = gfs2_glock_nq_init(gl, state, flags, gh);
1477 gfs2_glock_put(gl);
1478 }
1479
1480 return error;
1481}
1482
1483/**
1484 * glock_compare - Compare two struct gfs2_glock structures for sorting
1485 * @arg_a: the first structure
1486 * @arg_b: the second structure
1487 *
1488 */
1489
1490static int glock_compare(const void *arg_a, const void *arg_b)
1491{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001492 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1493 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1494 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1495 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001496
1497 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001498 return 1;
1499 if (a->ln_number < b->ln_number)
1500 return -1;
1501 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1502 return 1;
1503 if (!(gh_a->gh_flags & GL_LOCAL_EXCL) && (gh_b->gh_flags & GL_LOCAL_EXCL))
1504 return 1;
1505 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001506}
1507
1508/**
1509 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1510 * @num_gh: the number of structures
1511 * @ghs: an array of struct gfs2_holder structures
1512 *
1513 * Returns: 0 on success (all glocks acquired),
1514 * errno on failure (no glocks acquired)
1515 */
1516
1517static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1518 struct gfs2_holder **p)
1519{
1520 unsigned int x;
1521 int error = 0;
1522
1523 for (x = 0; x < num_gh; x++)
1524 p[x] = &ghs[x];
1525
1526 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1527
1528 for (x = 0; x < num_gh; x++) {
1529 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1530
1531 error = gfs2_glock_nq(p[x]);
1532 if (error) {
1533 while (x--)
1534 gfs2_glock_dq(p[x]);
1535 break;
1536 }
1537 }
1538
1539 return error;
1540}
1541
1542/**
1543 * gfs2_glock_nq_m - acquire multiple glocks
1544 * @num_gh: the number of structures
1545 * @ghs: an array of struct gfs2_holder structures
1546 *
1547 * Figure out how big an impact this function has. Either:
1548 * 1) Replace this code with code that calls gfs2_glock_prefetch()
1549 * 2) Forget async stuff and just call nq_m_sync()
1550 * 3) Leave it like it is
1551 *
1552 * Returns: 0 on success (all glocks acquired),
1553 * errno on failure (no glocks acquired)
1554 */
1555
1556int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1557{
1558 int *e;
1559 unsigned int x;
1560 int borked = 0, serious = 0;
1561 int error = 0;
1562
1563 if (!num_gh)
1564 return 0;
1565
1566 if (num_gh == 1) {
1567 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1568 return gfs2_glock_nq(ghs);
1569 }
1570
1571 e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1572 if (!e)
1573 return -ENOMEM;
1574
1575 for (x = 0; x < num_gh; x++) {
1576 ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC;
1577 error = gfs2_glock_nq(&ghs[x]);
1578 if (error) {
1579 borked = 1;
1580 serious = error;
1581 num_gh = x;
1582 break;
1583 }
1584 }
1585
1586 for (x = 0; x < num_gh; x++) {
1587 error = e[x] = glock_wait_internal(&ghs[x]);
1588 if (error) {
1589 borked = 1;
1590 if (error != GLR_TRYFAILED && error != GLR_CANCELED)
1591 serious = error;
1592 }
1593 }
1594
1595 if (!borked) {
1596 kfree(e);
1597 return 0;
1598 }
1599
1600 for (x = 0; x < num_gh; x++)
1601 if (!e[x])
1602 gfs2_glock_dq(&ghs[x]);
1603
1604 if (serious)
1605 error = serious;
1606 else {
1607 for (x = 0; x < num_gh; x++)
1608 gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags,
1609 &ghs[x]);
1610 error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e);
1611 }
1612
1613 kfree(e);
1614
1615 return error;
1616}
1617
1618/**
1619 * gfs2_glock_dq_m - release multiple glocks
1620 * @num_gh: the number of structures
1621 * @ghs: an array of struct gfs2_holder structures
1622 *
1623 */
1624
1625void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1626{
1627 unsigned int x;
1628
1629 for (x = 0; x < num_gh; x++)
1630 gfs2_glock_dq(&ghs[x]);
1631}
1632
1633/**
1634 * gfs2_glock_dq_uninit_m - release multiple glocks
1635 * @num_gh: the number of structures
1636 * @ghs: an array of struct gfs2_holder structures
1637 *
1638 */
1639
1640void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1641{
1642 unsigned int x;
1643
1644 for (x = 0; x < num_gh; x++)
1645 gfs2_glock_dq_uninit(&ghs[x]);
1646}
1647
1648/**
1649 * gfs2_glock_prefetch_num - prefetch a glock based on lock number
1650 * @sdp: the filesystem
1651 * @number: the lock number
1652 * @glops: the glock operations for the type of glock
1653 * @state: the state to acquire the glock in
1654 * @flags: modifier flags for the aquisition
1655 *
1656 * Returns: errno
1657 */
1658
Steven Whitehousecd915492006-09-04 12:49:07 -04001659void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001660 const struct gfs2_glock_operations *glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001661 unsigned int state, int flags)
1662{
1663 struct gfs2_glock *gl;
1664 int error;
1665
1666 if (atomic_read(&sdp->sd_reclaim_count) <
1667 gfs2_tune_get(sdp, gt_reclaim_limit)) {
1668 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1669 if (!error) {
1670 gfs2_glock_prefetch(gl, state, flags);
1671 gfs2_glock_put(gl);
1672 }
1673 }
1674}
1675
1676/**
1677 * gfs2_lvb_hold - attach a LVB from a glock
1678 * @gl: The glock in question
1679 *
1680 */
1681
1682int gfs2_lvb_hold(struct gfs2_glock *gl)
1683{
1684 int error;
1685
1686 gfs2_glmutex_lock(gl);
1687
1688 if (!atomic_read(&gl->gl_lvb_count)) {
1689 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1690 if (error) {
1691 gfs2_glmutex_unlock(gl);
1692 return error;
1693 }
1694 gfs2_glock_hold(gl);
1695 }
1696 atomic_inc(&gl->gl_lvb_count);
1697
1698 gfs2_glmutex_unlock(gl);
1699
1700 return 0;
1701}
1702
1703/**
1704 * gfs2_lvb_unhold - detach a LVB from a glock
1705 * @gl: The glock in question
1706 *
1707 */
1708
1709void gfs2_lvb_unhold(struct gfs2_glock *gl)
1710{
1711 gfs2_glock_hold(gl);
1712 gfs2_glmutex_lock(gl);
1713
1714 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1715 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1716 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1717 gl->gl_lvb = NULL;
1718 gfs2_glock_put(gl);
1719 }
1720
1721 gfs2_glmutex_unlock(gl);
1722 gfs2_glock_put(gl);
1723}
1724
David Teiglandb3b94fa2006-01-16 16:50:04 +00001725static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1726 unsigned int state)
1727{
1728 struct gfs2_glock *gl;
1729
1730 gl = gfs2_glock_find(sdp, name);
1731 if (!gl)
1732 return;
1733
1734 if (gl->gl_ops->go_callback)
1735 gl->gl_ops->go_callback(gl, state);
1736 handle_callback(gl, state);
1737
1738 spin_lock(&gl->gl_spin);
1739 run_queue(gl);
1740 spin_unlock(&gl->gl_spin);
1741
1742 gfs2_glock_put(gl);
1743}
1744
1745/**
1746 * gfs2_glock_cb - Callback used by locking module
Steven Whitehouse1c089c32006-09-07 15:50:20 -04001747 * @sdp: Pointer to the superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +00001748 * @type: Type of callback
1749 * @data: Type dependent data pointer
1750 *
1751 * Called by the locking module when it wants to tell us something.
1752 * Either we need to drop a lock, one of our ASYNC requests completed, or
1753 * a journal from another client needs to be recovered.
1754 */
1755
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001756void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001757{
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001758 struct gfs2_sbd *sdp = cb_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001759
David Teiglandb3b94fa2006-01-16 16:50:04 +00001760 switch (type) {
1761 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001762 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001763 return;
1764
1765 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001766 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767 return;
1768
1769 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001770 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001771 return;
1772
1773 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001774 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001775 struct gfs2_glock *gl;
1776
1777 gl = gfs2_glock_find(sdp, &async->lc_name);
1778 if (gfs2_assert_warn(sdp, gl))
1779 return;
1780 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1781 gl->gl_req_bh(gl, async->lc_ret);
1782 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001783 return;
1784 }
1785
1786 case LM_CB_NEED_RECOVERY:
1787 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1788 if (sdp->sd_recoverd_process)
1789 wake_up_process(sdp->sd_recoverd_process);
1790 return;
1791
1792 case LM_CB_DROPLOCKS:
1793 gfs2_gl_hash_clear(sdp, NO_WAIT);
1794 gfs2_quota_scan(sdp);
1795 return;
1796
1797 default:
1798 gfs2_assert_warn(sdp, 0);
1799 return;
1800 }
1801}
1802
1803/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001804 * demote_ok - Check to see if it's ok to unlock a glock
1805 * @gl: the glock
1806 *
1807 * Returns: 1 if it's ok
1808 */
1809
1810static int demote_ok(struct gfs2_glock *gl)
1811{
1812 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001813 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001814 int demote = 1;
1815
1816 if (test_bit(GLF_STICKY, &gl->gl_flags))
1817 demote = 0;
1818 else if (test_bit(GLF_PREFETCH, &gl->gl_flags))
Steven Whitehouse50299962006-09-04 09:49:55 -04001819 demote = time_after_eq(jiffies, gl->gl_stamp +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001820 gfs2_tune_get(sdp, gt_prefetch_secs) * HZ);
1821 else if (glops->go_demote_ok)
1822 demote = glops->go_demote_ok(gl);
1823
1824 return demote;
1825}
1826
1827/**
1828 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1829 * @gl: the glock
1830 *
1831 */
1832
1833void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1834{
1835 struct gfs2_sbd *sdp = gl->gl_sbd;
1836
1837 spin_lock(&sdp->sd_reclaim_lock);
1838 if (list_empty(&gl->gl_reclaim)) {
1839 gfs2_glock_hold(gl);
1840 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1841 atomic_inc(&sdp->sd_reclaim_count);
1842 }
1843 spin_unlock(&sdp->sd_reclaim_lock);
1844
1845 wake_up(&sdp->sd_reclaim_wq);
1846}
1847
1848/**
1849 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1850 * @sdp: the filesystem
1851 *
1852 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1853 * different glock and we notice that there are a lot of glocks in the
1854 * reclaim list.
1855 *
1856 */
1857
1858void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1859{
1860 struct gfs2_glock *gl;
1861
1862 spin_lock(&sdp->sd_reclaim_lock);
1863 if (list_empty(&sdp->sd_reclaim_list)) {
1864 spin_unlock(&sdp->sd_reclaim_lock);
1865 return;
1866 }
1867 gl = list_entry(sdp->sd_reclaim_list.next,
1868 struct gfs2_glock, gl_reclaim);
1869 list_del_init(&gl->gl_reclaim);
1870 spin_unlock(&sdp->sd_reclaim_lock);
1871
1872 atomic_dec(&sdp->sd_reclaim_count);
1873 atomic_inc(&sdp->sd_reclaimed);
1874
1875 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001876 if (queue_empty(gl, &gl->gl_holders) &&
Steven Whitehouse50299962006-09-04 09:49:55 -04001877 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878 handle_callback(gl, LM_ST_UNLOCKED);
1879 gfs2_glmutex_unlock(gl);
1880 }
1881
1882 gfs2_glock_put(gl);
1883}
1884
1885/**
1886 * examine_bucket - Call a function for glock in a hash bucket
1887 * @examiner: the function
1888 * @sdp: the filesystem
1889 * @bucket: the bucket
1890 *
1891 * Returns: 1 if the bucket has entries
1892 */
1893
1894static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001895 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001896{
Steven Whitehouse24264432006-09-11 21:40:30 -04001897 struct gfs2_glock *gl, *prev = NULL;
1898 int has_entries = 0;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001899 struct hlist_head *head = &gl_hash_table[hash].hb_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001900
Steven Whitehouse24264432006-09-11 21:40:30 -04001901 read_lock(gl_lock_addr(hash));
Steven Whitehouseb6397892006-09-12 10:10:01 -04001902 /* Can't use hlist_for_each_entry - don't want prefetch here */
1903 if (hlist_empty(head))
Steven Whitehouse24264432006-09-11 21:40:30 -04001904 goto out;
1905 has_entries = 1;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001906 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1907 while(1) {
Steven Whitehouse24264432006-09-11 21:40:30 -04001908 if (gl->gl_sbd == sdp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001909 gfs2_glock_hold(gl);
Steven Whitehouse24264432006-09-11 21:40:30 -04001910 read_unlock(gl_lock_addr(hash));
1911 if (prev)
1912 gfs2_glock_put(prev);
1913 prev = gl;
1914 examiner(gl);
1915 read_lock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001916 }
Steven Whitehouseb6397892006-09-12 10:10:01 -04001917 if (gl->gl_list.next == NULL)
1918 break;
Steven Whitehouse24264432006-09-11 21:40:30 -04001919 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001920 }
Steven Whitehouse24264432006-09-11 21:40:30 -04001921out:
1922 read_unlock(gl_lock_addr(hash));
1923 if (prev)
1924 gfs2_glock_put(prev);
1925 return has_entries;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001926}
1927
1928/**
1929 * scan_glock - look at a glock and see if we can reclaim it
1930 * @gl: the glock to look at
1931 *
1932 */
1933
1934static void scan_glock(struct gfs2_glock *gl)
1935{
Steven Whitehousea2242db2006-08-24 17:03:05 -04001936 if (gl->gl_ops == &gfs2_inode_glops)
Steven Whitehouse24264432006-09-11 21:40:30 -04001937 return;
Steven Whitehousea2242db2006-08-24 17:03:05 -04001938
David Teiglandb3b94fa2006-01-16 16:50:04 +00001939 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 if (queue_empty(gl, &gl->gl_holders) &&
Steven Whitehouse24264432006-09-11 21:40:30 -04001941 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001942 goto out_schedule;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001943 gfs2_glmutex_unlock(gl);
1944 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001945 return;
1946
Steven Whitehouse627add22006-07-05 13:16:19 -04001947out_schedule:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001948 gfs2_glmutex_unlock(gl);
1949 gfs2_glock_schedule_for_reclaim(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001950}
1951
1952/**
1953 * gfs2_scand_internal - Look for glocks and inodes to toss from memory
1954 * @sdp: the filesystem
1955 *
1956 */
1957
1958void gfs2_scand_internal(struct gfs2_sbd *sdp)
1959{
1960 unsigned int x;
1961
Steven Whitehouse94610612006-09-09 18:59:27 -04001962 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001963 examine_bucket(scan_glock, sdp, x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001964}
1965
1966/**
1967 * clear_glock - look at a glock and see if we can free it from glock cache
1968 * @gl: the glock to look at
1969 *
1970 */
1971
1972static void clear_glock(struct gfs2_glock *gl)
1973{
1974 struct gfs2_sbd *sdp = gl->gl_sbd;
1975 int released;
1976
1977 spin_lock(&sdp->sd_reclaim_lock);
1978 if (!list_empty(&gl->gl_reclaim)) {
1979 list_del_init(&gl->gl_reclaim);
1980 atomic_dec(&sdp->sd_reclaim_count);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001981 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001982 released = gfs2_glock_put(gl);
1983 gfs2_assert(sdp, !released);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001984 } else {
1985 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001986 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001987
1988 if (gfs2_glmutex_trylock(gl)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001989 if (queue_empty(gl, &gl->gl_holders) &&
1990 gl->gl_state != LM_ST_UNLOCKED)
1991 handle_callback(gl, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001992 gfs2_glmutex_unlock(gl);
1993 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001994}
1995
1996/**
1997 * gfs2_gl_hash_clear - Empty out the glock hash table
1998 * @sdp: the filesystem
1999 * @wait: wait until it's all gone
2000 *
2001 * Called when unmounting the filesystem, or when inter-node lock manager
2002 * requests DROPLOCKS because it is running out of capacity.
2003 */
2004
2005void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
2006{
2007 unsigned long t;
2008 unsigned int x;
2009 int cont;
2010
2011 t = jiffies;
2012
2013 for (;;) {
2014 cont = 0;
Steven Whitehouse24264432006-09-11 21:40:30 -04002015 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
2016 if (examine_bucket(clear_glock, sdp, x))
David Teiglandb3b94fa2006-01-16 16:50:04 +00002017 cont = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04002018 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002019
2020 if (!wait || !cont)
2021 break;
2022
2023 if (time_after_eq(jiffies,
2024 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
2025 fs_warn(sdp, "Unmount seems to be stalled. "
2026 "Dumping lock state...\n");
2027 gfs2_dump_lockstate(sdp);
2028 t = jiffies;
2029 }
2030
David Teiglandb3b94fa2006-01-16 16:50:04 +00002031 invalidate_inodes(sdp->sd_vfs);
Steven Whitehousefd88de562006-05-05 16:59:11 -04002032 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002033 }
2034}
2035
2036/*
2037 * Diagnostic routines to help debug distributed deadlock
2038 */
2039
2040/**
2041 * dump_holder - print information about a glock holder
2042 * @str: a string naming the type of holder
2043 * @gh: the glock holder
2044 *
2045 * Returns: 0 on success, -ENOBUFS when we run out of space
2046 */
2047
2048static int dump_holder(char *str, struct gfs2_holder *gh)
2049{
2050 unsigned int x;
2051 int error = -ENOBUFS;
2052
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002053 printk(KERN_INFO " %s\n", str);
2054 printk(KERN_INFO " owner = %ld\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002055 (gh->gh_owner) ? (long)gh->gh_owner->pid : -1);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002056 printk(KERN_INFO " gh_state = %u\n", gh->gh_state);
2057 printk(KERN_INFO " gh_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002058 for (x = 0; x < 32; x++)
2059 if (gh->gh_flags & (1 << x))
2060 printk(" %u", x);
2061 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002062 printk(KERN_INFO " error = %d\n", gh->gh_error);
2063 printk(KERN_INFO " gh_iflags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002064 for (x = 0; x < 32; x++)
2065 if (test_bit(x, &gh->gh_iflags))
2066 printk(" %u", x);
2067 printk(" \n");
Steven Whitehoused0dc80d2006-03-29 14:36:49 -05002068 print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002069
2070 error = 0;
2071
2072 return error;
2073}
2074
2075/**
2076 * dump_inode - print information about an inode
2077 * @ip: the inode
2078 *
2079 * Returns: 0 on success, -ENOBUFS when we run out of space
2080 */
2081
2082static int dump_inode(struct gfs2_inode *ip)
2083{
2084 unsigned int x;
2085 int error = -ENOBUFS;
2086
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002087 printk(KERN_INFO " Inode:\n");
2088 printk(KERN_INFO " num = %llu %llu\n",
Steven Whitehouse382066d2006-05-24 10:22:09 -04002089 (unsigned long long)ip->i_num.no_formal_ino,
2090 (unsigned long long)ip->i_num.no_addr);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002091 printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode));
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002092 printk(KERN_INFO " i_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002093 for (x = 0; x < 32; x++)
2094 if (test_bit(x, &ip->i_flags))
2095 printk(" %u", x);
2096 printk(" \n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002097
2098 error = 0;
2099
2100 return error;
2101}
2102
2103/**
2104 * dump_glock - print information about a glock
2105 * @gl: the glock
2106 * @count: where we are in the buffer
2107 *
2108 * Returns: 0 on success, -ENOBUFS when we run out of space
2109 */
2110
2111static int dump_glock(struct gfs2_glock *gl)
2112{
2113 struct gfs2_holder *gh;
2114 unsigned int x;
2115 int error = -ENOBUFS;
2116
2117 spin_lock(&gl->gl_spin);
2118
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002119 printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type,
Steven Whitehouse382066d2006-05-24 10:22:09 -04002120 (unsigned long long)gl->gl_name.ln_number);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002121 printk(KERN_INFO " gl_flags =");
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002122 for (x = 0; x < 32; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002123 if (test_bit(x, &gl->gl_flags))
2124 printk(" %u", x);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002125 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002126 printk(" \n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002127 printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount));
2128 printk(KERN_INFO " gl_state = %u\n", gl->gl_state);
Steven Whitehouse320dd102006-05-18 16:25:27 -04002129 printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm);
2130 print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip);
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002131 printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
2132 printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
2133 printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
2134 printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no");
2135 printk(KERN_INFO " le = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002136 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002137 printk(KERN_INFO " reclaim = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00002138 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
2139 if (gl->gl_aspace)
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002140 printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
Russell Cattelan88721872006-08-10 11:08:40 -05002141 gl->gl_aspace->i_mapping->nrpages);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002142 else
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002143 printk(KERN_INFO " aspace = no\n");
2144 printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002145 if (gl->gl_req_gh) {
2146 error = dump_holder("Request", gl->gl_req_gh);
2147 if (error)
2148 goto out;
2149 }
2150 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
2151 error = dump_holder("Holder", gh);
2152 if (error)
2153 goto out;
2154 }
2155 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
2156 error = dump_holder("Waiter1", gh);
2157 if (error)
2158 goto out;
2159 }
2160 list_for_each_entry(gh, &gl->gl_waiters2, gh_list) {
2161 error = dump_holder("Waiter2", gh);
2162 if (error)
2163 goto out;
2164 }
2165 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
2166 error = dump_holder("Waiter3", gh);
2167 if (error)
2168 goto out;
2169 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002170 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002171 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
2172 list_empty(&gl->gl_holders)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002173 error = dump_inode(gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002174 if (error)
2175 goto out;
2176 } else {
2177 error = -ENOBUFS;
Steven Whitehoused92a8d42006-02-27 10:57:14 -05002178 printk(KERN_INFO " Inode: busy\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00002179 }
2180 }
2181
2182 error = 0;
2183
Steven Whitehousea91ea692006-09-04 12:04:26 -04002184out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002185 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002186 return error;
2187}
2188
2189/**
2190 * gfs2_dump_lockstate - print out the current lockstate
2191 * @sdp: the filesystem
2192 * @ub: the buffer to copy the information into
2193 *
2194 * If @ub is NULL, dump the lockstate to the console.
2195 *
2196 */
2197
Adrian Bunk08bc2db2006-04-28 10:59:12 -04002198static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002199{
David Teiglandb3b94fa2006-01-16 16:50:04 +00002200 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -04002201 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002202 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 Whitehouse087efdd2006-09-09 16:59:11 -04002207 read_lock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002208
Steven Whitehouseb6397892006-09-12 10:10:01 -04002209 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002210 if (gl->gl_sbd != sdp)
2211 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002212
2213 error = dump_glock(gl);
2214 if (error)
2215 break;
2216 }
2217
Steven Whitehouse087efdd2006-09-09 16:59:11 -04002218 read_unlock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00002219
2220 if (error)
2221 break;
2222 }
2223
2224
2225 return error;
2226}
2227
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002228int __init gfs2_glock_init(void)
2229{
2230 unsigned i;
2231 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehouseb6397892006-09-12 10:10:01 -04002232 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002233 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -04002234#ifdef GL_HASH_LOCK_SZ
2235 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2236 rwlock_init(&gl_hash_locks[i]);
2237 }
2238#endif
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002239 return 0;
2240}
2241