blob: 3d949187fed05d81f54dbaf9d4ebeed8385d1cbd [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>
Robert Peterson7c52b162007-03-16 10:26:37 +000026#include <linux/seq_file.h>
27#include <linux/debugfs.h>
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +010028#include <linux/kthread.h>
29#include <linux/freezer.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
31#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050032#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "glock.h"
34#include "glops.h"
35#include "inode.h"
36#include "lm.h"
37#include "lops.h"
38#include "meta_io.h"
39#include "quota.h"
40#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050041#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000042
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040043struct gfs2_gl_hash_bucket {
Steven Whitehouseb6397892006-09-12 10:10:01 -040044 struct hlist_head hb_list;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040045};
46
Robert Peterson7c52b162007-03-16 10:26:37 +000047struct glock_iter {
48 int hash; /* hash bucket index */
49 struct gfs2_sbd *sdp; /* incore superblock */
50 struct gfs2_glock *gl; /* current glock struct */
Robert Peterson7c52b162007-03-16 10:26:37 +000051 struct seq_file *seq; /* sequence file for debugfs */
52 char string[512]; /* scratch space */
53};
54
David Teiglandb3b94fa2006-01-16 16:50:04 +000055typedef void (*glock_examiner) (struct gfs2_glock * gl);
56
Adrian Bunk08bc2db2006-04-28 10:59:12 -040057static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
Robert Peterson04b933f2007-03-23 17:05:15 -050058static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +000059static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh);
Steven Whitehouseb5d32be2007-01-22 12:15:34 -050060static void gfs2_glock_drop_th(struct gfs2_glock *gl);
Steven Whitehouse61be0842007-01-29 11:51:45 +000061static DECLARE_RWSEM(gfs2_umount_flush_sem);
Robert Peterson7c52b162007-03-16 10:26:37 +000062static struct dentry *gfs2_root;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +010063static struct task_struct *scand_process;
64static unsigned int scand_secs = 5;
Adrian Bunk08bc2db2006-04-28 10:59:12 -040065
Steven Whitehouseb6397892006-09-12 10:10:01 -040066#define GFS2_GL_HASH_SHIFT 15
Steven Whitehouse087efdd2006-09-09 16:59:11 -040067#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
68#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
69
Steven Whitehouse85d1da62006-09-07 14:40:21 -040070static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
Robert Peterson04b933f2007-03-23 17:05:15 -050071static struct dentry *gfs2_root;
Steven Whitehouse087efdd2006-09-09 16:59:11 -040072
73/*
74 * Despite what you might think, the numbers below are not arbitrary :-)
75 * They are taken from the ipv4 routing hash code, which is well tested
76 * and thus should be nearly optimal. Later on we might tweek the numbers
77 * but for now this should be fine.
78 *
79 * The reason for putting the locks in a separate array from the list heads
80 * is that we can have fewer locks than list heads and save memory. We use
81 * the same hash function for both, but with a different hash mask.
82 */
83#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
84 defined(CONFIG_PROVE_LOCKING)
85
86#ifdef CONFIG_LOCKDEP
87# define GL_HASH_LOCK_SZ 256
88#else
89# if NR_CPUS >= 32
90# define GL_HASH_LOCK_SZ 4096
91# elif NR_CPUS >= 16
92# define GL_HASH_LOCK_SZ 2048
93# elif NR_CPUS >= 8
94# define GL_HASH_LOCK_SZ 1024
95# elif NR_CPUS >= 4
96# define GL_HASH_LOCK_SZ 512
97# else
98# define GL_HASH_LOCK_SZ 256
99# endif
100#endif
101
102/* We never want more locks than chains */
103#if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
104# undef GL_HASH_LOCK_SZ
105# define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
106#endif
107
108static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
109
110static inline rwlock_t *gl_lock_addr(unsigned int x)
111{
Steven Whitehouse94610612006-09-09 18:59:27 -0400112 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400113}
114#else /* not SMP, so no spinlocks required */
Randy Dunlap0ac23062006-11-28 22:29:19 -0800115static inline rwlock_t *gl_lock_addr(unsigned int x)
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400116{
117 return NULL;
118}
119#endif
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400120
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121/**
122 * relaxed_state_ok - is a requested lock compatible with the current lock mode?
123 * @actual: the current state of the lock
124 * @requested: the lock state that was requested by the caller
125 * @flags: the modifier flags passed in by the caller
126 *
127 * Returns: 1 if the locks are compatible, 0 otherwise
128 */
129
130static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
131 int flags)
132{
133 if (actual == requested)
134 return 1;
135
136 if (flags & GL_EXACT)
137 return 0;
138
139 if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
140 return 1;
141
142 if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
143 return 1;
144
145 return 0;
146}
147
148/**
149 * gl_hash() - Turn glock number into hash bucket number
150 * @lock: The glock number
151 *
152 * Returns: The number of the corresponding hash bucket
153 */
154
Steven Whitehouseb8547852006-09-07 13:12:27 -0400155static unsigned int gl_hash(const struct gfs2_sbd *sdp,
156 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157{
158 unsigned int h;
159
Steven Whitehousecd915492006-09-04 12:49:07 -0400160 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -0400162 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 h &= GFS2_GL_HASH_MASK;
164
165 return h;
166}
167
168/**
169 * glock_free() - Perform a few checks and then release struct gfs2_glock
170 * @gl: The glock to release
171 *
172 * Also calls lock module to release its internal structure for this glock.
173 *
174 */
175
176static void glock_free(struct gfs2_glock *gl)
177{
178 struct gfs2_sbd *sdp = gl->gl_sbd;
179 struct inode *aspace = gl->gl_aspace;
180
181 gfs2_lm_put_lock(sdp, gl->gl_lock);
182
183 if (aspace)
184 gfs2_aspace_put(aspace);
185
186 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187}
188
189/**
190 * gfs2_glock_hold() - increment reference count on glock
191 * @gl: The glock to hold
192 *
193 */
194
195void gfs2_glock_hold(struct gfs2_glock *gl)
196{
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400197 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198}
199
200/**
201 * gfs2_glock_put() - Decrement reference count on glock
202 * @gl: The glock to put
203 *
204 */
205
206int gfs2_glock_put(struct gfs2_glock *gl)
207{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 int rv = 0;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400209 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400211 write_lock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400212 if (atomic_dec_and_test(&gl->gl_ref)) {
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));
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400216 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
217 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
218 gfs2_assert(sdp, list_empty(&gl->gl_holders));
219 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400220 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 glock_free(gl);
222 rv = 1;
223 goto out;
224 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400225 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehousea2242db2006-08-24 17:03:05 -0400226out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227 return rv;
228}
229
230/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 * search_bucket() - Find struct gfs2_glock by lock number
232 * @bucket: the bucket to search
233 * @name: The lock name
234 *
235 * Returns: NULL, or the struct gfs2_glock with the requested number
236 */
237
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400238static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400239 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400240 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241{
242 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -0400243 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244
Steven Whitehouseb6397892006-09-12 10:10:01 -0400245 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 if (!lm_name_equal(&gl->gl_name, name))
247 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400248 if (gl->gl_sbd != sdp)
249 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400251 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252
253 return gl;
254 }
255
256 return NULL;
257}
258
259/**
260 * gfs2_glock_find() - Find glock by lock number
261 * @sdp: The GFS2 superblock
262 * @name: The lock name
263 *
264 * Returns: NULL, or the struct gfs2_glock with the requested number
265 */
266
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400267static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400268 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400270 unsigned int hash = gl_hash(sdp, name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 struct gfs2_glock *gl;
272
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400273 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400274 gl = search_bucket(hash, sdp, name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400275 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276
277 return gl;
278}
279
280/**
281 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
282 * @sdp: The GFS2 superblock
283 * @number: the lock number
284 * @glops: The glock_operations to use
285 * @create: If 0, don't create the glock if it doesn't exist
286 * @glp: the glock is returned here
287 *
288 * This does not lock a glock, just finds/creates structures for one.
289 *
290 * Returns: errno
291 */
292
Steven Whitehousecd915492006-09-04 12:49:07 -0400293int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400294 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295 struct gfs2_glock **glp)
296{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400297 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400299 unsigned int hash = gl_hash(sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300 int error;
301
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400302 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400303 gl = search_bucket(hash, sdp, &name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400304 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305
306 if (gl || !create) {
307 *glp = gl;
308 return 0;
309 }
310
311 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
312 if (!gl)
313 return -ENOMEM;
314
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400315 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 gl->gl_name = name;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400317 atomic_set(&gl->gl_ref, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400319 gl->gl_hash = hash;
Robert Peterson04b933f2007-03-23 17:05:15 -0500320 gl->gl_owner_pid = 0;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400321 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 gl->gl_ops = glops;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400323 gl->gl_req_gh = NULL;
324 gl->gl_req_bh = NULL;
325 gl->gl_vn = 0;
326 gl->gl_stamp = jiffies;
327 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 gl->gl_sbd = sdp;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400329 gl->gl_aspace = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000330 lops_init_le(&gl->gl_le, &gfs2_glock_lops);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331
332 /* If this glock protects actual on-disk data or metadata blocks,
333 create a VFS inode to manage the pages/buffers holding them. */
Steven Whitehouse50299962006-09-04 09:49:55 -0400334 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 gl->gl_aspace = gfs2_aspace_get(sdp);
336 if (!gl->gl_aspace) {
337 error = -ENOMEM;
338 goto fail;
339 }
340 }
341
342 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
343 if (error)
344 goto fail_aspace;
345
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400346 write_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400347 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 if (tmp) {
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400349 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 glock_free(gl);
351 gl = tmp;
352 } else {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400353 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400354 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355 }
356
357 *glp = gl;
358
359 return 0;
360
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400361fail_aspace:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 if (gl->gl_aspace)
363 gfs2_aspace_put(gl->gl_aspace);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400364fail:
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400365 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 return error;
367}
368
369/**
370 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
371 * @gl: the glock
372 * @state: the state we're requesting
373 * @flags: the modifier flags
374 * @gh: the holder structure
375 *
376 */
377
Steven Whitehouse190562b2006-04-20 16:57:23 -0400378void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000379 struct gfs2_holder *gh)
380{
381 INIT_LIST_HEAD(&gh->gh_list);
382 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500383 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Robert Peterson04b933f2007-03-23 17:05:15 -0500384 gh->gh_owner_pid = current->pid;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 gh->gh_state = state;
386 gh->gh_flags = flags;
387 gh->gh_error = 0;
388 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 gfs2_glock_hold(gl);
390}
391
392/**
393 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
394 * @state: the state we're requesting
395 * @flags: the modifier flags
396 * @gh: the holder structure
397 *
398 * Don't mess with the glock.
399 *
400 */
401
Steven Whitehouse190562b2006-04-20 16:57:23 -0400402void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403{
404 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400405 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000406 gh->gh_iflags = 0;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500407 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408}
409
410/**
411 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
412 * @gh: the holder structure
413 *
414 */
415
416void gfs2_holder_uninit(struct gfs2_holder *gh)
417{
418 gfs2_glock_put(gh->gh_gl);
419 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500420 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000421}
422
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000423static void gfs2_holder_wake(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424{
Steven Whitehousefee852e2007-01-17 15:33:23 +0000425 clear_bit(HIF_WAIT, &gh->gh_iflags);
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100426 smp_mb__after_clear_bit();
Steven Whitehousefee852e2007-01-17 15:33:23 +0000427 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
428}
429
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100430static int just_schedule(void *word)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000431{
432 schedule();
433 return 0;
434}
435
436static void wait_on_holder(struct gfs2_holder *gh)
437{
438 might_sleep();
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100439 wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
440}
441
442static void gfs2_demote_wake(struct gfs2_glock *gl)
443{
444 clear_bit(GLF_DEMOTE, &gl->gl_flags);
445 smp_mb__after_clear_bit();
446 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
447}
448
449static void wait_on_demote(struct gfs2_glock *gl)
450{
451 might_sleep();
452 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000453}
454
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456 * rq_mutex - process a mutex request in the queue
457 * @gh: the glock holder
458 *
459 * Returns: 1 if the queue is blocked
460 */
461
462static int rq_mutex(struct gfs2_holder *gh)
463{
464 struct gfs2_glock *gl = gh->gh_gl;
465
466 list_del_init(&gh->gh_list);
467 /* gh->gh_error never examined. */
468 set_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehoused043e192007-01-23 16:56:36 -0500469 clear_bit(HIF_WAIT, &gh->gh_iflags);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000470 smp_mb();
471 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000472
473 return 1;
474}
475
476/**
477 * rq_promote - process a promote request in the queue
478 * @gh: the glock holder
479 *
480 * Acquire a new inter-node lock, or change a lock state to more restrictive.
481 *
482 * Returns: 1 if the queue is blocked
483 */
484
485static int rq_promote(struct gfs2_holder *gh)
486{
487 struct gfs2_glock *gl = gh->gh_gl;
488 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489
490 if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
491 if (list_empty(&gl->gl_holders)) {
492 gl->gl_req_gh = gh;
493 set_bit(GLF_LOCK, &gl->gl_flags);
494 spin_unlock(&gl->gl_spin);
495
496 if (atomic_read(&sdp->sd_reclaim_count) >
497 gfs2_tune_get(sdp, gt_reclaim_limit) &&
498 !(gh->gh_flags & LM_FLAG_PRIORITY)) {
499 gfs2_reclaim_glock(sdp);
500 gfs2_reclaim_glock(sdp);
501 }
502
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000503 gfs2_glock_xmote_th(gh->gh_gl, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000504 spin_lock(&gl->gl_spin);
505 }
506 return 1;
507 }
508
509 if (list_empty(&gl->gl_holders)) {
510 set_bit(HIF_FIRST, &gh->gh_iflags);
511 set_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 } else {
513 struct gfs2_holder *next_gh;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500514 if (gh->gh_state == LM_ST_EXCLUSIVE)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 return 1;
516 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
517 gh_list);
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500518 if (next_gh->gh_state == LM_ST_EXCLUSIVE)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 }
521
522 list_move_tail(&gh->gh_list, &gl->gl_holders);
523 gh->gh_error = 0;
524 set_bit(HIF_HOLDER, &gh->gh_iflags);
525
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000526 gfs2_holder_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527
528 return 0;
529}
530
531/**
532 * rq_demote - process a demote request in the queue
533 * @gh: the glock holder
534 *
535 * Returns: 1 if the queue is blocked
536 */
537
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000538static int rq_demote(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 if (!list_empty(&gl->gl_holders))
541 return 1;
542
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000543 if (gl->gl_state == gl->gl_demote_state ||
544 gl->gl_state == LM_ST_UNLOCKED) {
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100545 gfs2_demote_wake(gl);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000546 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 }
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000548 set_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000549 if (gl->gl_demote_state == LM_ST_UNLOCKED ||
Steven Whitehouse87124e52007-07-23 09:54:36 +0100550 gl->gl_state != LM_ST_EXCLUSIVE) {
551 spin_unlock(&gl->gl_spin);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000552 gfs2_glock_drop_th(gl);
Steven Whitehouse87124e52007-07-23 09:54:36 +0100553 } else {
554 spin_unlock(&gl->gl_spin);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000555 gfs2_glock_xmote_th(gl, NULL);
Steven Whitehouse87124e52007-07-23 09:54:36 +0100556 }
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000557 spin_lock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558
559 return 0;
560}
561
562/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 * run_queue - process holder structures on a glock
564 * @gl: the glock
565 *
566 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567static void run_queue(struct gfs2_glock *gl)
568{
569 struct gfs2_holder *gh;
570 int blocked = 1;
571
572 for (;;) {
573 if (test_bit(GLF_LOCK, &gl->gl_flags))
574 break;
575
576 if (!list_empty(&gl->gl_waiters1)) {
577 gh = list_entry(gl->gl_waiters1.next,
578 struct gfs2_holder, gh_list);
579
580 if (test_bit(HIF_MUTEX, &gh->gh_iflags))
581 blocked = rq_mutex(gh);
582 else
583 gfs2_assert_warn(gl->gl_sbd, 0);
584
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000585 } else if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
586 blocked = rq_demote(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587 } else if (!list_empty(&gl->gl_waiters3)) {
588 gh = list_entry(gl->gl_waiters3.next,
589 struct gfs2_holder, gh_list);
590
591 if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
592 blocked = rq_promote(gh);
593 else
594 gfs2_assert_warn(gl->gl_sbd, 0);
595
596 } else
597 break;
598
599 if (blocked)
600 break;
601 }
602}
603
604/**
605 * gfs2_glmutex_lock - acquire a local lock on a glock
606 * @gl: the glock
607 *
608 * Gives caller exclusive access to manipulate a glock structure.
609 */
610
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400611static void gfs2_glmutex_lock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612{
613 struct gfs2_holder gh;
614
615 gfs2_holder_init(gl, 0, 0, &gh);
616 set_bit(HIF_MUTEX, &gh.gh_iflags);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000617 if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
618 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619
620 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400621 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400623 } else {
Robert Peterson04b933f2007-03-23 17:05:15 -0500624 gl->gl_owner_pid = current->pid;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400625 gl->gl_ip = (unsigned long)__builtin_return_address(0);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000626 clear_bit(HIF_WAIT, &gh.gh_iflags);
627 smp_mb();
628 wake_up_bit(&gh.gh_iflags, HIF_WAIT);
Steven Whitehouse320dd102006-05-18 16:25:27 -0400629 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 spin_unlock(&gl->gl_spin);
631
Steven Whitehousefee852e2007-01-17 15:33:23 +0000632 wait_on_holder(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 gfs2_holder_uninit(&gh);
634}
635
636/**
637 * gfs2_glmutex_trylock - try to acquire a local lock on a glock
638 * @gl: the glock
639 *
640 * Returns: 1 if the glock is acquired
641 */
642
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400643static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644{
645 int acquired = 1;
646
647 spin_lock(&gl->gl_spin);
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400648 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649 acquired = 0;
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400650 } else {
Robert Peterson04b933f2007-03-23 17:05:15 -0500651 gl->gl_owner_pid = current->pid;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400652 gl->gl_ip = (unsigned long)__builtin_return_address(0);
653 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 spin_unlock(&gl->gl_spin);
655
656 return acquired;
657}
658
659/**
660 * gfs2_glmutex_unlock - release a local lock on a glock
661 * @gl: the glock
662 *
663 */
664
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400665static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666{
667 spin_lock(&gl->gl_spin);
668 clear_bit(GLF_LOCK, &gl->gl_flags);
Robert Peterson04b933f2007-03-23 17:05:15 -0500669 gl->gl_owner_pid = 0;
Steven Whitehouse320dd102006-05-18 16:25:27 -0400670 gl->gl_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 run_queue(gl);
Steven Whitehouse190562b2006-04-20 16:57:23 -0400672 BUG_ON(!spin_is_locked(&gl->gl_spin));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 spin_unlock(&gl->gl_spin);
674}
675
676/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000677 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678 * @gl: the glock
679 * @state: the state the caller wants us to change to
680 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000681 * There are only two requests that we are going to see in actual
682 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683 */
684
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100685static void handle_callback(struct gfs2_glock *gl, unsigned int state, int remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 spin_lock(&gl->gl_spin);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000688 if (test_and_set_bit(GLF_DEMOTE, &gl->gl_flags) == 0) {
689 gl->gl_demote_state = state;
690 gl->gl_demote_time = jiffies;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100691 if (remote && gl->gl_ops->go_type == LM_TYPE_IOPEN &&
692 gl->gl_object) {
693 struct inode *inode = igrab(gl->gl_object);
694 spin_unlock(&gl->gl_spin);
695 if (inode) {
696 d_prune_aliases(inode);
697 iput(inode);
698 }
699 return;
700 }
Josef Whiter26caee52007-07-23 10:02:40 +0100701 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
702 gl->gl_demote_state != state) {
703 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706}
707
708/**
709 * state_change - record that the glock is now in a different state
710 * @gl: the glock
711 * @new_state the new state
712 *
713 */
714
715static void state_change(struct gfs2_glock *gl, unsigned int new_state)
716{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 int held1, held2;
718
719 held1 = (gl->gl_state != LM_ST_UNLOCKED);
720 held2 = (new_state != LM_ST_UNLOCKED);
721
722 if (held1 != held2) {
David Teigland6a6b3d02006-02-23 10:11:47 +0000723 if (held2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 gfs2_glock_hold(gl);
David Teigland6a6b3d02006-02-23 10:11:47 +0000725 else
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 }
728
729 gl->gl_state = new_state;
730}
731
732/**
733 * xmote_bh - Called after the lock module is done acquiring a lock
734 * @gl: The glock in question
735 * @ret: the int returned from the lock module
736 *
737 */
738
739static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
740{
741 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400742 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000743 struct gfs2_holder *gh = gl->gl_req_gh;
744 int prev_state = gl->gl_state;
745 int op_done = 1;
746
747 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500748 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
750
751 state_change(gl, ret & LM_OUT_ST_MASK);
752
753 if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
754 if (glops->go_inval)
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500755 glops->go_inval(gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 } else if (gl->gl_state == LM_ST_DEFERRED) {
757 /* We might not want to do this here.
758 Look at moving to the inode glops. */
759 if (glops->go_inval)
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500760 glops->go_inval(gl, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 }
762
763 /* Deal with each possible exit condition */
764
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000765 if (!gh) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 gl->gl_stamp = jiffies;
Steven Whitehouse87124e52007-07-23 09:54:36 +0100767 if (ret & LM_OUT_CANCELED) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000768 op_done = 0;
Steven Whitehouse87124e52007-07-23 09:54:36 +0100769 } else {
770 spin_lock(&gl->gl_spin);
771 if (gl->gl_state != gl->gl_demote_state) {
772 gl->gl_req_bh = NULL;
773 spin_unlock(&gl->gl_spin);
774 gfs2_glock_drop_th(gl);
775 gfs2_glock_put(gl);
776 return;
777 }
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100778 gfs2_demote_wake(gl);
Steven Whitehouse87124e52007-07-23 09:54:36 +0100779 spin_unlock(&gl->gl_spin);
780 }
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000781 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 spin_lock(&gl->gl_spin);
783 list_del_init(&gh->gh_list);
784 gh->gh_error = -EIO;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000785 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
786 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gh->gh_error = GLR_CANCELED;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000788 if (ret & LM_OUT_CANCELED)
789 goto out;
790 if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
791 list_add_tail(&gh->gh_list, &gl->gl_holders);
792 gh->gh_error = 0;
793 set_bit(HIF_HOLDER, &gh->gh_iflags);
794 set_bit(HIF_FIRST, &gh->gh_iflags);
795 op_done = 0;
796 goto out;
797 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 gh->gh_error = GLR_TRYFAILED;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000799 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
800 goto out;
801 gh->gh_error = -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 if (gfs2_assert_withdraw(sdp, 0) == -1)
803 fs_err(sdp, "ret = 0x%.8X\n", ret);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000804out:
805 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 }
807
808 if (glops->go_xmote_bh)
809 glops->go_xmote_bh(gl);
810
811 if (op_done) {
812 spin_lock(&gl->gl_spin);
813 gl->gl_req_gh = NULL;
814 gl->gl_req_bh = NULL;
815 clear_bit(GLF_LOCK, &gl->gl_flags);
816 run_queue(gl);
817 spin_unlock(&gl->gl_spin);
818 }
819
820 gfs2_glock_put(gl);
821
Steven Whitehousefee852e2007-01-17 15:33:23 +0000822 if (gh)
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000823 gfs2_holder_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824}
825
826/**
827 * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
828 * @gl: The glock in question
829 * @state: the requested state
830 * @flags: modifier flags to the lock call
831 *
832 */
833
Steven Whitehouse87124e52007-07-23 09:54:36 +0100834static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835{
836 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000837 int flags = gh ? gh->gh_flags : 0;
838 unsigned state = gh ? gh->gh_state : gl->gl_demote_state;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400839 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840 int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
841 LM_FLAG_NOEXP | LM_FLAG_ANY |
842 LM_FLAG_PRIORITY);
843 unsigned int lck_ret;
844
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500845 if (glops->go_xmote_th)
846 glops->go_xmote_th(gl);
847
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500849 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
851 gfs2_assert_warn(sdp, state != gl->gl_state);
852
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 gfs2_glock_hold(gl);
854 gl->gl_req_bh = xmote_bh;
855
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400856 lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857
858 if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
859 return;
860
861 if (lck_ret & LM_OUT_ASYNC)
862 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
863 else
864 xmote_bh(gl, lck_ret);
865}
866
867/**
868 * drop_bh - Called after a lock module unlock completes
869 * @gl: the glock
870 * @ret: the return status
871 *
872 * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
873 * Doesn't drop the reference on the glock the top half took out
874 *
875 */
876
877static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
878{
879 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400880 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 struct gfs2_holder *gh = gl->gl_req_gh;
882
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500884 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 gfs2_assert_warn(sdp, !ret);
886
887 state_change(gl, LM_ST_UNLOCKED);
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100888 gfs2_demote_wake(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889
890 if (glops->go_inval)
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500891 glops->go_inval(gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892
893 if (gh) {
894 spin_lock(&gl->gl_spin);
895 list_del_init(&gh->gh_list);
896 gh->gh_error = 0;
897 spin_unlock(&gl->gl_spin);
898 }
899
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 spin_lock(&gl->gl_spin);
901 gl->gl_req_gh = NULL;
902 gl->gl_req_bh = NULL;
903 clear_bit(GLF_LOCK, &gl->gl_flags);
904 run_queue(gl);
905 spin_unlock(&gl->gl_spin);
906
907 gfs2_glock_put(gl);
908
Steven Whitehousefee852e2007-01-17 15:33:23 +0000909 if (gh)
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000910 gfs2_holder_wake(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911}
912
913/**
914 * gfs2_glock_drop_th - call into the lock module to unlock a lock
915 * @gl: the glock
916 *
917 */
918
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500919static void gfs2_glock_drop_th(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920{
921 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400922 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 unsigned int ret;
924
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500925 if (glops->go_drop_th)
926 glops->go_drop_th(gl);
927
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse12132932007-01-22 13:09:04 -0500929 gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
931
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 gfs2_glock_hold(gl);
933 gl->gl_req_bh = drop_bh;
934
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
936
937 if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
938 return;
939
940 if (!ret)
941 drop_bh(gl, ret);
942 else
943 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
944}
945
946/**
947 * do_cancels - cancel requests for locks stuck waiting on an expire flag
948 * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
949 *
950 * Don't cancel GL_NOCANCEL requests.
951 */
952
953static void do_cancels(struct gfs2_holder *gh)
954{
955 struct gfs2_glock *gl = gh->gh_gl;
956
957 spin_lock(&gl->gl_spin);
958
959 while (gl->gl_req_gh != gh &&
960 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
961 !list_empty(&gh->gh_list)) {
Steven Whitehouse50299962006-09-04 09:49:55 -0400962 if (gl->gl_req_bh && !(gl->gl_req_gh &&
963 (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964 spin_unlock(&gl->gl_spin);
965 gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
966 msleep(100);
967 spin_lock(&gl->gl_spin);
968 } else {
969 spin_unlock(&gl->gl_spin);
970 msleep(100);
971 spin_lock(&gl->gl_spin);
972 }
973 }
974
975 spin_unlock(&gl->gl_spin);
976}
977
978/**
979 * glock_wait_internal - wait on a glock acquisition
980 * @gh: the glock holder
981 *
982 * Returns: 0 on success
983 */
984
985static int glock_wait_internal(struct gfs2_holder *gh)
986{
987 struct gfs2_glock *gl = gh->gh_gl;
988 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400989 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990
991 if (test_bit(HIF_ABORTED, &gh->gh_iflags))
992 return -EIO;
993
994 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
995 spin_lock(&gl->gl_spin);
996 if (gl->gl_req_gh != gh &&
997 !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
998 !list_empty(&gh->gh_list)) {
999 list_del_init(&gh->gh_list);
1000 gh->gh_error = GLR_TRYFAILED;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 run_queue(gl);
1002 spin_unlock(&gl->gl_spin);
1003 return gh->gh_error;
1004 }
1005 spin_unlock(&gl->gl_spin);
1006 }
1007
1008 if (gh->gh_flags & LM_FLAG_PRIORITY)
1009 do_cancels(gh);
1010
Steven Whitehousefee852e2007-01-17 15:33:23 +00001011 wait_on_holder(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 if (gh->gh_error)
1013 return gh->gh_error;
1014
1015 gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001016 gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 gh->gh_flags));
1018
1019 if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1020 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1021
1022 if (glops->go_lock) {
1023 gh->gh_error = glops->go_lock(gh);
1024 if (gh->gh_error) {
1025 spin_lock(&gl->gl_spin);
1026 list_del_init(&gh->gh_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 spin_unlock(&gl->gl_spin);
1028 }
1029 }
1030
1031 spin_lock(&gl->gl_spin);
1032 gl->gl_req_gh = NULL;
1033 gl->gl_req_bh = NULL;
1034 clear_bit(GLF_LOCK, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035 run_queue(gl);
1036 spin_unlock(&gl->gl_spin);
1037 }
1038
1039 return gh->gh_error;
1040}
1041
1042static inline struct gfs2_holder *
Robert Peterson04b933f2007-03-23 17:05:15 -05001043find_holder_by_owner(struct list_head *head, pid_t pid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044{
1045 struct gfs2_holder *gh;
1046
1047 list_for_each_entry(gh, head, gh_list) {
Robert Peterson04b933f2007-03-23 17:05:15 -05001048 if (gh->gh_owner_pid == pid)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 return gh;
1050 }
1051
1052 return NULL;
1053}
1054
Robert Peterson7c52b162007-03-16 10:26:37 +00001055static void print_dbg(struct glock_iter *gi, const char *fmt, ...)
1056{
1057 va_list args;
1058
1059 va_start(args, fmt);
1060 if (gi) {
1061 vsprintf(gi->string, fmt, args);
1062 seq_printf(gi->seq, gi->string);
1063 }
1064 else
1065 vprintk(fmt, args);
1066 va_end(args);
1067}
1068
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1071 * @gh: the holder structure to add
1072 *
1073 */
1074
1075static void add_to_queue(struct gfs2_holder *gh)
1076{
1077 struct gfs2_glock *gl = gh->gh_gl;
1078 struct gfs2_holder *existing;
1079
Robert Peterson04b933f2007-03-23 17:05:15 -05001080 BUG_ON(!gh->gh_owner_pid);
Steven Whitehousefee852e2007-01-17 15:33:23 +00001081 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1082 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -04001083
Robert Peterson04b933f2007-03-23 17:05:15 -05001084 existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001086 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
Robert Peterson04b933f2007-03-23 17:05:15 -05001087 printk(KERN_INFO "pid : %d\n", existing->gh_owner_pid);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001088 printk(KERN_INFO "lock type : %d lock state : %d\n",
Abhijith Das86384602006-08-25 11:13:37 -05001089 existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001090 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
Robert Peterson04b933f2007-03-23 17:05:15 -05001091 printk(KERN_INFO "pid : %d\n", gh->gh_owner_pid);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001092 printk(KERN_INFO "lock type : %d lock state : %d\n",
Abhijith Das86384602006-08-25 11:13:37 -05001093 gl->gl_name.ln_type, gl->gl_state);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001094 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001095 }
1096
Robert Peterson04b933f2007-03-23 17:05:15 -05001097 existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 if (existing) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -04001099 print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip);
1100 print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1101 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102 }
1103
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 if (gh->gh_flags & LM_FLAG_PRIORITY)
1105 list_add(&gh->gh_list, &gl->gl_waiters3);
1106 else
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001107 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108}
1109
1110/**
1111 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1112 * @gh: the holder structure
1113 *
1114 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1115 *
1116 * Returns: 0, GLR_TRYFAILED, or errno on failure
1117 */
1118
1119int gfs2_glock_nq(struct gfs2_holder *gh)
1120{
1121 struct gfs2_glock *gl = gh->gh_gl;
1122 struct gfs2_sbd *sdp = gl->gl_sbd;
1123 int error = 0;
1124
Steven Whitehouse320dd102006-05-18 16:25:27 -04001125restart:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1127 set_bit(HIF_ABORTED, &gh->gh_iflags);
1128 return -EIO;
1129 }
1130
1131 set_bit(HIF_PROMOTE, &gh->gh_iflags);
1132
1133 spin_lock(&gl->gl_spin);
1134 add_to_queue(gh);
1135 run_queue(gl);
1136 spin_unlock(&gl->gl_spin);
1137
1138 if (!(gh->gh_flags & GL_ASYNC)) {
1139 error = glock_wait_internal(gh);
1140 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001141 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142 goto restart;
1143 }
1144 }
1145
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146 return error;
1147}
1148
1149/**
1150 * gfs2_glock_poll - poll to see if an async request has been completed
1151 * @gh: the holder
1152 *
1153 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1154 */
1155
1156int gfs2_glock_poll(struct gfs2_holder *gh)
1157{
1158 struct gfs2_glock *gl = gh->gh_gl;
1159 int ready = 0;
1160
1161 spin_lock(&gl->gl_spin);
1162
1163 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1164 ready = 1;
1165 else if (list_empty(&gh->gh_list)) {
1166 if (gh->gh_error == GLR_CANCELED) {
1167 spin_unlock(&gl->gl_spin);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001168 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169 if (gfs2_glock_nq(gh))
1170 return 1;
1171 return 0;
1172 } else
1173 ready = 1;
1174 }
1175
1176 spin_unlock(&gl->gl_spin);
1177
1178 return ready;
1179}
1180
1181/**
1182 * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1183 * @gh: the holder structure
1184 *
1185 * Returns: 0, GLR_TRYFAILED, or errno on failure
1186 */
1187
1188int gfs2_glock_wait(struct gfs2_holder *gh)
1189{
1190 int error;
1191
1192 error = glock_wait_internal(gh);
1193 if (error == GLR_CANCELED) {
Steven Whitehouse190562b2006-04-20 16:57:23 -04001194 msleep(100);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 gh->gh_flags &= ~GL_ASYNC;
1196 error = gfs2_glock_nq(gh);
1197 }
1198
1199 return error;
1200}
1201
1202/**
1203 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1204 * @gh: the glock holder
1205 *
1206 */
1207
1208void gfs2_glock_dq(struct gfs2_holder *gh)
1209{
1210 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001211 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 if (gh->gh_flags & GL_NOCACHE)
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001214 handle_callback(gl, LM_ST_UNLOCKED, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215
1216 gfs2_glmutex_lock(gl);
1217
1218 spin_lock(&gl->gl_spin);
1219 list_del_init(&gh->gh_list);
1220
1221 if (list_empty(&gl->gl_holders)) {
1222 spin_unlock(&gl->gl_spin);
1223
1224 if (glops->go_unlock)
1225 glops->go_unlock(gh);
1226
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 spin_lock(&gl->gl_spin);
Steven Whitehouse3b8249f2007-03-16 09:40:31 +00001228 gl->gl_stamp = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229 }
1230
1231 clear_bit(GLF_LOCK, &gl->gl_flags);
1232 run_queue(gl);
1233 spin_unlock(&gl->gl_spin);
1234}
1235
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001236void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1237{
1238 struct gfs2_glock *gl = gh->gh_gl;
1239 gfs2_glock_dq(gh);
1240 wait_on_demote(gl);
1241}
1242
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1245 * @gh: the holder structure
1246 *
1247 */
1248
1249void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1250{
1251 gfs2_glock_dq(gh);
1252 gfs2_holder_uninit(gh);
1253}
1254
1255/**
1256 * gfs2_glock_nq_num - acquire a glock based on lock number
1257 * @sdp: the filesystem
1258 * @number: the lock number
1259 * @glops: the glock operations for the type of glock
1260 * @state: the state to acquire the glock in
1261 * @flags: modifier flags for the aquisition
1262 * @gh: the struct gfs2_holder
1263 *
1264 * Returns: errno
1265 */
1266
Steven Whitehousecd915492006-09-04 12:49:07 -04001267int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001268 const struct gfs2_glock_operations *glops,
1269 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270{
1271 struct gfs2_glock *gl;
1272 int error;
1273
1274 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1275 if (!error) {
1276 error = gfs2_glock_nq_init(gl, state, flags, gh);
1277 gfs2_glock_put(gl);
1278 }
1279
1280 return error;
1281}
1282
1283/**
1284 * glock_compare - Compare two struct gfs2_glock structures for sorting
1285 * @arg_a: the first structure
1286 * @arg_b: the second structure
1287 *
1288 */
1289
1290static int glock_compare(const void *arg_a, const void *arg_b)
1291{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001292 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1293 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1294 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1295 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296
1297 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001298 return 1;
1299 if (a->ln_number < b->ln_number)
1300 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001301 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 -04001302 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303}
1304
1305/**
1306 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1307 * @num_gh: the number of structures
1308 * @ghs: an array of struct gfs2_holder structures
1309 *
1310 * Returns: 0 on success (all glocks acquired),
1311 * errno on failure (no glocks acquired)
1312 */
1313
1314static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1315 struct gfs2_holder **p)
1316{
1317 unsigned int x;
1318 int error = 0;
1319
1320 for (x = 0; x < num_gh; x++)
1321 p[x] = &ghs[x];
1322
1323 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1324
1325 for (x = 0; x < num_gh; x++) {
1326 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1327
1328 error = gfs2_glock_nq(p[x]);
1329 if (error) {
1330 while (x--)
1331 gfs2_glock_dq(p[x]);
1332 break;
1333 }
1334 }
1335
1336 return error;
1337}
1338
1339/**
1340 * gfs2_glock_nq_m - acquire multiple glocks
1341 * @num_gh: the number of structures
1342 * @ghs: an array of struct gfs2_holder structures
1343 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344 *
1345 * Returns: 0 on success (all glocks acquired),
1346 * errno on failure (no glocks acquired)
1347 */
1348
1349int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1350{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001351 struct gfs2_holder *tmp[4];
1352 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353 int error = 0;
1354
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001355 switch(num_gh) {
1356 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001358 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001359 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1360 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001361 default:
1362 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001363 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001364 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1365 if (!pph)
1366 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 }
1368
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001369 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001371 if (pph != tmp)
1372 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373
1374 return error;
1375}
1376
1377/**
1378 * gfs2_glock_dq_m - release multiple glocks
1379 * @num_gh: the number of structures
1380 * @ghs: an array of struct gfs2_holder structures
1381 *
1382 */
1383
1384void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1385{
1386 unsigned int x;
1387
1388 for (x = 0; x < num_gh; x++)
1389 gfs2_glock_dq(&ghs[x]);
1390}
1391
1392/**
1393 * gfs2_glock_dq_uninit_m - release multiple glocks
1394 * @num_gh: the number of structures
1395 * @ghs: an array of struct gfs2_holder structures
1396 *
1397 */
1398
1399void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1400{
1401 unsigned int x;
1402
1403 for (x = 0; x < num_gh; x++)
1404 gfs2_glock_dq_uninit(&ghs[x]);
1405}
1406
1407/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408 * gfs2_lvb_hold - attach a LVB from a glock
1409 * @gl: The glock in question
1410 *
1411 */
1412
1413int gfs2_lvb_hold(struct gfs2_glock *gl)
1414{
1415 int error;
1416
1417 gfs2_glmutex_lock(gl);
1418
1419 if (!atomic_read(&gl->gl_lvb_count)) {
1420 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1421 if (error) {
1422 gfs2_glmutex_unlock(gl);
1423 return error;
1424 }
1425 gfs2_glock_hold(gl);
1426 }
1427 atomic_inc(&gl->gl_lvb_count);
1428
1429 gfs2_glmutex_unlock(gl);
1430
1431 return 0;
1432}
1433
1434/**
1435 * gfs2_lvb_unhold - detach a LVB from a glock
1436 * @gl: The glock in question
1437 *
1438 */
1439
1440void gfs2_lvb_unhold(struct gfs2_glock *gl)
1441{
1442 gfs2_glock_hold(gl);
1443 gfs2_glmutex_lock(gl);
1444
1445 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1446 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1447 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1448 gl->gl_lvb = NULL;
1449 gfs2_glock_put(gl);
1450 }
1451
1452 gfs2_glmutex_unlock(gl);
1453 gfs2_glock_put(gl);
1454}
1455
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1457 unsigned int state)
1458{
1459 struct gfs2_glock *gl;
1460
1461 gl = gfs2_glock_find(sdp, name);
1462 if (!gl)
1463 return;
1464
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001465 handle_callback(gl, state, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466
1467 spin_lock(&gl->gl_spin);
1468 run_queue(gl);
1469 spin_unlock(&gl->gl_spin);
1470
1471 gfs2_glock_put(gl);
1472}
1473
1474/**
1475 * gfs2_glock_cb - Callback used by locking module
Steven Whitehouse1c089c32006-09-07 15:50:20 -04001476 * @sdp: Pointer to the superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +00001477 * @type: Type of callback
1478 * @data: Type dependent data pointer
1479 *
1480 * Called by the locking module when it wants to tell us something.
1481 * Either we need to drop a lock, one of our ASYNC requests completed, or
1482 * a journal from another client needs to be recovered.
1483 */
1484
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001485void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001486{
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001487 struct gfs2_sbd *sdp = cb_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001488
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489 switch (type) {
1490 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001491 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492 return;
1493
1494 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001495 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001496 return;
1497
1498 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001499 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500 return;
1501
1502 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001503 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001504 struct gfs2_glock *gl;
1505
Steven Whitehouse61be0842007-01-29 11:51:45 +00001506 down_read(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001507 gl = gfs2_glock_find(sdp, &async->lc_name);
1508 if (gfs2_assert_warn(sdp, gl))
1509 return;
1510 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1511 gl->gl_req_bh(gl, async->lc_ret);
1512 gfs2_glock_put(gl);
Steven Whitehouse61be0842007-01-29 11:51:45 +00001513 up_read(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001514 return;
1515 }
1516
1517 case LM_CB_NEED_RECOVERY:
1518 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1519 if (sdp->sd_recoverd_process)
1520 wake_up_process(sdp->sd_recoverd_process);
1521 return;
1522
1523 case LM_CB_DROPLOCKS:
1524 gfs2_gl_hash_clear(sdp, NO_WAIT);
1525 gfs2_quota_scan(sdp);
1526 return;
1527
1528 default:
1529 gfs2_assert_warn(sdp, 0);
1530 return;
1531 }
1532}
1533
1534/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001535 * demote_ok - Check to see if it's ok to unlock a glock
1536 * @gl: the glock
1537 *
1538 * Returns: 1 if it's ok
1539 */
1540
1541static int demote_ok(struct gfs2_glock *gl)
1542{
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001543 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001544 int demote = 1;
1545
1546 if (test_bit(GLF_STICKY, &gl->gl_flags))
1547 demote = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548 else if (glops->go_demote_ok)
1549 demote = glops->go_demote_ok(gl);
1550
1551 return demote;
1552}
1553
1554/**
1555 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1556 * @gl: the glock
1557 *
1558 */
1559
1560void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1561{
1562 struct gfs2_sbd *sdp = gl->gl_sbd;
1563
1564 spin_lock(&sdp->sd_reclaim_lock);
1565 if (list_empty(&gl->gl_reclaim)) {
1566 gfs2_glock_hold(gl);
1567 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1568 atomic_inc(&sdp->sd_reclaim_count);
1569 }
1570 spin_unlock(&sdp->sd_reclaim_lock);
1571
1572 wake_up(&sdp->sd_reclaim_wq);
1573}
1574
1575/**
1576 * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1577 * @sdp: the filesystem
1578 *
1579 * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1580 * different glock and we notice that there are a lot of glocks in the
1581 * reclaim list.
1582 *
1583 */
1584
1585void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1586{
1587 struct gfs2_glock *gl;
1588
1589 spin_lock(&sdp->sd_reclaim_lock);
1590 if (list_empty(&sdp->sd_reclaim_list)) {
1591 spin_unlock(&sdp->sd_reclaim_lock);
1592 return;
1593 }
1594 gl = list_entry(sdp->sd_reclaim_list.next,
1595 struct gfs2_glock, gl_reclaim);
1596 list_del_init(&gl->gl_reclaim);
1597 spin_unlock(&sdp->sd_reclaim_lock);
1598
1599 atomic_dec(&sdp->sd_reclaim_count);
1600 atomic_inc(&sdp->sd_reclaimed);
1601
1602 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse12132932007-01-22 13:09:04 -05001603 if (list_empty(&gl->gl_holders) &&
Steven Whitehouse50299962006-09-04 09:49:55 -04001604 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001605 handle_callback(gl, LM_ST_UNLOCKED, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 gfs2_glmutex_unlock(gl);
1607 }
1608
1609 gfs2_glock_put(gl);
1610}
1611
1612/**
1613 * examine_bucket - Call a function for glock in a hash bucket
1614 * @examiner: the function
1615 * @sdp: the filesystem
1616 * @bucket: the bucket
1617 *
1618 * Returns: 1 if the bucket has entries
1619 */
1620
1621static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001622 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001623{
Steven Whitehouse24264432006-09-11 21:40:30 -04001624 struct gfs2_glock *gl, *prev = NULL;
1625 int has_entries = 0;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001626 struct hlist_head *head = &gl_hash_table[hash].hb_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001627
Steven Whitehouse24264432006-09-11 21:40:30 -04001628 read_lock(gl_lock_addr(hash));
Steven Whitehouseb6397892006-09-12 10:10:01 -04001629 /* Can't use hlist_for_each_entry - don't want prefetch here */
1630 if (hlist_empty(head))
Steven Whitehouse24264432006-09-11 21:40:30 -04001631 goto out;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001632 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1633 while(1) {
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001634 if (!sdp || gl->gl_sbd == sdp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001635 gfs2_glock_hold(gl);
Steven Whitehouse24264432006-09-11 21:40:30 -04001636 read_unlock(gl_lock_addr(hash));
1637 if (prev)
1638 gfs2_glock_put(prev);
1639 prev = gl;
1640 examiner(gl);
Steven Whitehousea8336342006-09-14 13:57:38 -04001641 has_entries = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04001642 read_lock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001643 }
Steven Whitehouseb6397892006-09-12 10:10:01 -04001644 if (gl->gl_list.next == NULL)
1645 break;
Steven Whitehouse24264432006-09-11 21:40:30 -04001646 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001647 }
Steven Whitehouse24264432006-09-11 21:40:30 -04001648out:
1649 read_unlock(gl_lock_addr(hash));
1650 if (prev)
1651 gfs2_glock_put(prev);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001652 cond_resched();
Steven Whitehouse24264432006-09-11 21:40:30 -04001653 return has_entries;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001654}
1655
1656/**
1657 * scan_glock - look at a glock and see if we can reclaim it
1658 * @gl: the glock to look at
1659 *
1660 */
1661
1662static void scan_glock(struct gfs2_glock *gl)
1663{
Steven Whitehouseb0041572006-11-23 10:51:34 -05001664 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
Steven Whitehouse24264432006-09-11 21:40:30 -04001665 return;
Steven Whitehousea2242db2006-08-24 17:03:05 -04001666
David Teiglandb3b94fa2006-01-16 16:50:04 +00001667 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse12132932007-01-22 13:09:04 -05001668 if (list_empty(&gl->gl_holders) &&
Steven Whitehouse24264432006-09-11 21:40:30 -04001669 gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001670 goto out_schedule;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001671 gfs2_glmutex_unlock(gl);
1672 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001673 return;
1674
Steven Whitehouse627add22006-07-05 13:16:19 -04001675out_schedule:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001676 gfs2_glmutex_unlock(gl);
1677 gfs2_glock_schedule_for_reclaim(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001678}
1679
1680/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001681 * clear_glock - look at a glock and see if we can free it from glock cache
1682 * @gl: the glock to look at
1683 *
1684 */
1685
1686static void clear_glock(struct gfs2_glock *gl)
1687{
1688 struct gfs2_sbd *sdp = gl->gl_sbd;
1689 int released;
1690
1691 spin_lock(&sdp->sd_reclaim_lock);
1692 if (!list_empty(&gl->gl_reclaim)) {
1693 list_del_init(&gl->gl_reclaim);
1694 atomic_dec(&sdp->sd_reclaim_count);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001695 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001696 released = gfs2_glock_put(gl);
1697 gfs2_assert(sdp, !released);
Steven Whitehouse190562b2006-04-20 16:57:23 -04001698 } else {
1699 spin_unlock(&sdp->sd_reclaim_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001700 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701
1702 if (gfs2_glmutex_trylock(gl)) {
Steven Whitehouse90101c32007-01-23 13:20:41 -05001703 if (list_empty(&gl->gl_holders) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001704 gl->gl_state != LM_ST_UNLOCKED)
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001705 handle_callback(gl, LM_ST_UNLOCKED, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001706 gfs2_glmutex_unlock(gl);
1707 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001708}
1709
1710/**
1711 * gfs2_gl_hash_clear - Empty out the glock hash table
1712 * @sdp: the filesystem
1713 * @wait: wait until it's all gone
1714 *
1715 * Called when unmounting the filesystem, or when inter-node lock manager
1716 * requests DROPLOCKS because it is running out of capacity.
1717 */
1718
1719void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
1720{
1721 unsigned long t;
1722 unsigned int x;
1723 int cont;
1724
1725 t = jiffies;
1726
1727 for (;;) {
1728 cont = 0;
Steven Whitehouse24264432006-09-11 21:40:30 -04001729 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001730 if (examine_bucket(clear_glock, sdp, x))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731 cont = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04001732 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001733
1734 if (!wait || !cont)
1735 break;
1736
1737 if (time_after_eq(jiffies,
1738 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1739 fs_warn(sdp, "Unmount seems to be stalled. "
1740 "Dumping lock state...\n");
1741 gfs2_dump_lockstate(sdp);
1742 t = jiffies;
1743 }
1744
Steven Whitehouse61be0842007-01-29 11:51:45 +00001745 down_write(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001746 invalidate_inodes(sdp->sd_vfs);
Steven Whitehouse61be0842007-01-29 11:51:45 +00001747 up_write(&gfs2_umount_flush_sem);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001748 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001749 }
1750}
1751
1752/*
1753 * Diagnostic routines to help debug distributed deadlock
1754 */
1755
Robert Peterson04b933f2007-03-23 17:05:15 -05001756static void gfs2_print_symbol(struct glock_iter *gi, const char *fmt,
1757 unsigned long address)
1758{
Robert Peterson7a0079d2007-04-17 11:37:11 -05001759 char buffer[KSYM_SYMBOL_LEN];
Robert Peterson04b933f2007-03-23 17:05:15 -05001760
Robert Peterson7a0079d2007-04-17 11:37:11 -05001761 sprint_symbol(buffer, address);
1762 print_dbg(gi, fmt, buffer);
Robert Peterson04b933f2007-03-23 17:05:15 -05001763}
1764
David Teiglandb3b94fa2006-01-16 16:50:04 +00001765/**
1766 * dump_holder - print information about a glock holder
1767 * @str: a string naming the type of holder
1768 * @gh: the glock holder
1769 *
1770 * Returns: 0 on success, -ENOBUFS when we run out of space
1771 */
1772
Robert Peterson7c52b162007-03-16 10:26:37 +00001773static int dump_holder(struct glock_iter *gi, char *str,
1774 struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001775{
1776 unsigned int x;
Robert Peterson04b933f2007-03-23 17:05:15 -05001777 struct task_struct *gh_owner;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001778
Robert Peterson7c52b162007-03-16 10:26:37 +00001779 print_dbg(gi, " %s\n", str);
Robert Peterson04b933f2007-03-23 17:05:15 -05001780 if (gh->gh_owner_pid) {
1781 print_dbg(gi, " owner = %ld ", (long)gh->gh_owner_pid);
1782 gh_owner = find_task_by_pid(gh->gh_owner_pid);
1783 if (gh_owner)
1784 print_dbg(gi, "(%s)\n", gh_owner->comm);
1785 else
1786 print_dbg(gi, "(ended)\n");
1787 } else
1788 print_dbg(gi, " owner = -1\n");
Robert Peterson7c52b162007-03-16 10:26:37 +00001789 print_dbg(gi, " gh_state = %u\n", gh->gh_state);
1790 print_dbg(gi, " gh_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001791 for (x = 0; x < 32; x++)
1792 if (gh->gh_flags & (1 << x))
Robert Peterson7c52b162007-03-16 10:26:37 +00001793 print_dbg(gi, " %u", x);
1794 print_dbg(gi, " \n");
1795 print_dbg(gi, " error = %d\n", gh->gh_error);
1796 print_dbg(gi, " gh_iflags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001797 for (x = 0; x < 32; x++)
1798 if (test_bit(x, &gh->gh_iflags))
Robert Peterson7c52b162007-03-16 10:26:37 +00001799 print_dbg(gi, " %u", x);
1800 print_dbg(gi, " \n");
Robert Peterson04b933f2007-03-23 17:05:15 -05001801 gfs2_print_symbol(gi, " initialized at: %s\n", gh->gh_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001802
Robert Peterson7c52b162007-03-16 10:26:37 +00001803 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001804}
1805
1806/**
1807 * dump_inode - print information about an inode
1808 * @ip: the inode
1809 *
1810 * Returns: 0 on success, -ENOBUFS when we run out of space
1811 */
1812
Robert Peterson7c52b162007-03-16 10:26:37 +00001813static int dump_inode(struct glock_iter *gi, struct gfs2_inode *ip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001814{
1815 unsigned int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001816
Robert Peterson7c52b162007-03-16 10:26:37 +00001817 print_dbg(gi, " Inode:\n");
1818 print_dbg(gi, " num = %llu/%llu\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001819 (unsigned long long)ip->i_no_formal_ino,
1820 (unsigned long long)ip->i_no_addr);
Robert Peterson7c52b162007-03-16 10:26:37 +00001821 print_dbg(gi, " type = %u\n", IF2DT(ip->i_inode.i_mode));
1822 print_dbg(gi, " i_flags =");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001823 for (x = 0; x < 32; x++)
1824 if (test_bit(x, &ip->i_flags))
Robert Peterson7c52b162007-03-16 10:26:37 +00001825 print_dbg(gi, " %u", x);
1826 print_dbg(gi, " \n");
1827 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001828}
1829
1830/**
1831 * dump_glock - print information about a glock
1832 * @gl: the glock
1833 * @count: where we are in the buffer
1834 *
1835 * Returns: 0 on success, -ENOBUFS when we run out of space
1836 */
1837
Robert Peterson7c52b162007-03-16 10:26:37 +00001838static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001839{
1840 struct gfs2_holder *gh;
1841 unsigned int x;
1842 int error = -ENOBUFS;
Robert Peterson04b933f2007-03-23 17:05:15 -05001843 struct task_struct *gl_owner;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001844
1845 spin_lock(&gl->gl_spin);
1846
Abhijith Dasa947e032007-08-21 09:57:29 -05001847 print_dbg(gi, "Glock 0x%p (%u, 0x%llx)\n", gl, gl->gl_name.ln_type,
Robert Peterson7c52b162007-03-16 10:26:37 +00001848 (unsigned long long)gl->gl_name.ln_number);
1849 print_dbg(gi, " gl_flags =");
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001850 for (x = 0; x < 32; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001851 if (test_bit(x, &gl->gl_flags))
Robert Peterson7c52b162007-03-16 10:26:37 +00001852 print_dbg(gi, " %u", x);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001853 }
Robert Peterson04b933f2007-03-23 17:05:15 -05001854 if (!test_bit(GLF_LOCK, &gl->gl_flags))
1855 print_dbg(gi, " (unlocked)");
Robert Peterson7c52b162007-03-16 10:26:37 +00001856 print_dbg(gi, " \n");
1857 print_dbg(gi, " gl_ref = %d\n", atomic_read(&gl->gl_ref));
1858 print_dbg(gi, " gl_state = %u\n", gl->gl_state);
Robert Peterson04b933f2007-03-23 17:05:15 -05001859 if (gl->gl_owner_pid) {
1860 gl_owner = find_task_by_pid(gl->gl_owner_pid);
1861 if (gl_owner)
1862 print_dbg(gi, " gl_owner = pid %d (%s)\n",
1863 gl->gl_owner_pid, gl_owner->comm);
1864 else
1865 print_dbg(gi, " gl_owner = %d (ended)\n",
1866 gl->gl_owner_pid);
1867 } else
1868 print_dbg(gi, " gl_owner = -1\n");
Robert Peterson7c52b162007-03-16 10:26:37 +00001869 print_dbg(gi, " gl_ip = %lu\n", gl->gl_ip);
1870 print_dbg(gi, " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
1871 print_dbg(gi, " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
1872 print_dbg(gi, " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
1873 print_dbg(gi, " object = %s\n", (gl->gl_object) ? "yes" : "no");
1874 print_dbg(gi, " le = %s\n",
David Teiglandb3b94fa2006-01-16 16:50:04 +00001875 (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
Robert Peterson7c52b162007-03-16 10:26:37 +00001876 print_dbg(gi, " reclaim = %s\n",
1877 (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878 if (gl->gl_aspace)
Robert Peterson7c52b162007-03-16 10:26:37 +00001879 print_dbg(gi, " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
1880 gl->gl_aspace->i_mapping->nrpages);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001881 else
Robert Peterson7c52b162007-03-16 10:26:37 +00001882 print_dbg(gi, " aspace = no\n");
1883 print_dbg(gi, " ail = %d\n", atomic_read(&gl->gl_ail_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001884 if (gl->gl_req_gh) {
Robert Peterson7c52b162007-03-16 10:26:37 +00001885 error = dump_holder(gi, "Request", gl->gl_req_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001886 if (error)
1887 goto out;
1888 }
1889 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
Robert Peterson7c52b162007-03-16 10:26:37 +00001890 error = dump_holder(gi, "Holder", gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001891 if (error)
1892 goto out;
1893 }
1894 list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
Robert Peterson7c52b162007-03-16 10:26:37 +00001895 error = dump_holder(gi, "Waiter1", gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001896 if (error)
1897 goto out;
1898 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001899 list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
Robert Peterson7c52b162007-03-16 10:26:37 +00001900 error = dump_holder(gi, "Waiter3", gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001901 if (error)
1902 goto out;
1903 }
Steven Whitehouse3b8249f2007-03-16 09:40:31 +00001904 if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
1905 print_dbg(gi, " Demotion req to state %u (%llu uS ago)\n",
Robert Petersoncd81a4b2007-05-14 12:42:18 -05001906 gl->gl_demote_state, (unsigned long long)
1907 (jiffies - gl->gl_demote_time)*(1000000/HZ));
Steven Whitehouse3b8249f2007-03-16 09:40:31 +00001908 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001909 if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001910 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
Robert Peterson7c52b162007-03-16 10:26:37 +00001911 list_empty(&gl->gl_holders)) {
1912 error = dump_inode(gi, gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001913 if (error)
1914 goto out;
1915 } else {
1916 error = -ENOBUFS;
Robert Peterson7c52b162007-03-16 10:26:37 +00001917 print_dbg(gi, " Inode: busy\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +00001918 }
1919 }
1920
1921 error = 0;
1922
Steven Whitehousea91ea692006-09-04 12:04:26 -04001923out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001924 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001925 return error;
1926}
1927
1928/**
1929 * gfs2_dump_lockstate - print out the current lockstate
1930 * @sdp: the filesystem
1931 * @ub: the buffer to copy the information into
1932 *
1933 * If @ub is NULL, dump the lockstate to the console.
1934 *
1935 */
1936
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001937static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001938{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001939 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001940 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001941 unsigned int x;
1942 int error = 0;
1943
1944 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001945
Steven Whitehouse087efdd2006-09-09 16:59:11 -04001946 read_lock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001947
Steven Whitehouseb6397892006-09-12 10:10:01 -04001948 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001949 if (gl->gl_sbd != sdp)
1950 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001951
Robert Peterson7c52b162007-03-16 10:26:37 +00001952 error = dump_glock(NULL, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001953 if (error)
1954 break;
1955 }
1956
Steven Whitehouse087efdd2006-09-09 16:59:11 -04001957 read_unlock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001958
1959 if (error)
1960 break;
1961 }
1962
1963
1964 return error;
1965}
1966
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001967/**
1968 * gfs2_scand - Look for cached glocks and inodes to toss from memory
1969 * @sdp: Pointer to GFS2 superblock
1970 *
1971 * One of these daemons runs, finding candidates to add to sd_reclaim_list.
1972 * See gfs2_glockd()
1973 */
1974
1975static int gfs2_scand(void *data)
1976{
1977 unsigned x;
1978 unsigned delay;
1979
1980 while (!kthread_should_stop()) {
1981 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1982 examine_bucket(scan_glock, NULL, x);
1983 if (freezing(current))
1984 refrigerator();
1985 delay = scand_secs;
1986 if (delay < 1)
1987 delay = 1;
1988 schedule_timeout_interruptible(delay * HZ);
1989 }
1990
1991 return 0;
1992}
1993
1994
1995
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001996int __init gfs2_glock_init(void)
1997{
1998 unsigned i;
1999 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehouseb6397892006-09-12 10:10:01 -04002000 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002001 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -04002002#ifdef GL_HASH_LOCK_SZ
2003 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2004 rwlock_init(&gl_hash_locks[i]);
2005 }
2006#endif
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01002007
2008 scand_process = kthread_run(gfs2_scand, NULL, "gfs2_scand");
2009 if (IS_ERR(scand_process))
2010 return PTR_ERR(scand_process);
2011
Steven Whitehouse85d1da62006-09-07 14:40:21 -04002012 return 0;
2013}
2014
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01002015void gfs2_glock_exit(void)
2016{
2017 kthread_stop(scand_process);
2018}
2019
2020module_param(scand_secs, uint, S_IRUGO|S_IWUSR);
2021MODULE_PARM_DESC(scand_secs, "The number of seconds between scand runs");
2022
Robert Peterson7c52b162007-03-16 10:26:37 +00002023static int gfs2_glock_iter_next(struct glock_iter *gi)
2024{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002025 struct gfs2_glock *gl;
2026
Abhijith Dasa947e032007-08-21 09:57:29 -05002027restart:
Robert Peterson7a0079d2007-04-17 11:37:11 -05002028 read_lock(gl_lock_addr(gi->hash));
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002029 gl = gi->gl;
2030 if (gl) {
Abhijith Dasa947e032007-08-21 09:57:29 -05002031 gi->gl = hlist_entry(gl->gl_list.next,
2032 struct gfs2_glock, gl_list);
Robert Peterson7c52b162007-03-16 10:26:37 +00002033 if (gi->gl)
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002034 gfs2_glock_hold(gi->gl);
Robert Peterson7c52b162007-03-16 10:26:37 +00002035 }
Robert Peterson7a0079d2007-04-17 11:37:11 -05002036 read_unlock(gl_lock_addr(gi->hash));
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002037 if (gl)
2038 gfs2_glock_put(gl);
Abhijith Dasa947e032007-08-21 09:57:29 -05002039 if (gl && gi->gl == NULL)
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002040 gi->hash++;
Abhijith Dasa947e032007-08-21 09:57:29 -05002041 while(gi->gl == NULL) {
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002042 if (gi->hash >= GFS2_GL_HASH_SIZE)
2043 return 1;
2044 read_lock(gl_lock_addr(gi->hash));
2045 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
2046 struct gfs2_glock, gl_list);
2047 if (gi->gl)
2048 gfs2_glock_hold(gi->gl);
2049 read_unlock(gl_lock_addr(gi->hash));
Abhijith Dasa947e032007-08-21 09:57:29 -05002050 gi->hash++;
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002051 }
Abhijith Dasa947e032007-08-21 09:57:29 -05002052
2053 if (gi->sdp != gi->gl->gl_sbd)
2054 goto restart;
2055
Robert Peterson7c52b162007-03-16 10:26:37 +00002056 return 0;
2057}
2058
2059static void gfs2_glock_iter_free(struct glock_iter *gi)
2060{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002061 if (gi->gl)
2062 gfs2_glock_put(gi->gl);
Robert Peterson7c52b162007-03-16 10:26:37 +00002063 kfree(gi);
2064}
2065
2066static struct glock_iter *gfs2_glock_iter_init(struct gfs2_sbd *sdp)
2067{
2068 struct glock_iter *gi;
2069
2070 gi = kmalloc(sizeof (*gi), GFP_KERNEL);
2071 if (!gi)
2072 return NULL;
2073
2074 gi->sdp = sdp;
2075 gi->hash = 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00002076 gi->seq = NULL;
Abhijith Dasa947e032007-08-21 09:57:29 -05002077 gi->gl = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002078 memset(gi->string, 0, sizeof(gi->string));
2079
Abhijith Dasa947e032007-08-21 09:57:29 -05002080 if (gfs2_glock_iter_next(gi)) {
Robert Peterson7c52b162007-03-16 10:26:37 +00002081 gfs2_glock_iter_free(gi);
2082 return NULL;
2083 }
2084
2085 return gi;
2086}
2087
2088static void *gfs2_glock_seq_start(struct seq_file *file, loff_t *pos)
2089{
2090 struct glock_iter *gi;
2091 loff_t n = *pos;
2092
2093 gi = gfs2_glock_iter_init(file->private);
2094 if (!gi)
2095 return NULL;
2096
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002097 while(n--) {
Robert Peterson7c52b162007-03-16 10:26:37 +00002098 if (gfs2_glock_iter_next(gi)) {
2099 gfs2_glock_iter_free(gi);
2100 return NULL;
2101 }
2102 }
2103
2104 return gi;
2105}
2106
2107static void *gfs2_glock_seq_next(struct seq_file *file, void *iter_ptr,
2108 loff_t *pos)
2109{
2110 struct glock_iter *gi = iter_ptr;
2111
2112 (*pos)++;
2113
2114 if (gfs2_glock_iter_next(gi)) {
2115 gfs2_glock_iter_free(gi);
2116 return NULL;
2117 }
2118
2119 return gi;
2120}
2121
2122static void gfs2_glock_seq_stop(struct seq_file *file, void *iter_ptr)
2123{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01002124 struct glock_iter *gi = iter_ptr;
2125 if (gi)
2126 gfs2_glock_iter_free(gi);
Robert Peterson7c52b162007-03-16 10:26:37 +00002127}
2128
2129static int gfs2_glock_seq_show(struct seq_file *file, void *iter_ptr)
2130{
2131 struct glock_iter *gi = iter_ptr;
2132
2133 gi->seq = file;
2134 dump_glock(gi, gi->gl);
2135
2136 return 0;
2137}
2138
Denis Cheng4ef29002007-07-31 18:31:11 +08002139static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002140 .start = gfs2_glock_seq_start,
2141 .next = gfs2_glock_seq_next,
2142 .stop = gfs2_glock_seq_stop,
2143 .show = gfs2_glock_seq_show,
2144};
2145
2146static int gfs2_debugfs_open(struct inode *inode, struct file *file)
2147{
2148 struct seq_file *seq;
2149 int ret;
2150
2151 ret = seq_open(file, &gfs2_glock_seq_ops);
2152 if (ret)
2153 return ret;
2154
2155 seq = file->private_data;
2156 seq->private = inode->i_private;
2157
2158 return 0;
2159}
2160
2161static const struct file_operations gfs2_debug_fops = {
2162 .owner = THIS_MODULE,
2163 .open = gfs2_debugfs_open,
2164 .read = seq_read,
2165 .llseek = seq_lseek,
2166 .release = seq_release
2167};
2168
2169int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2170{
Robert Peterson5f882092007-04-18 11:41:11 -05002171 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2172 if (!sdp->debugfs_dir)
2173 return -ENOMEM;
2174 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
2175 S_IFREG | S_IRUGO,
2176 sdp->debugfs_dir, sdp,
2177 &gfs2_debug_fops);
2178 if (!sdp->debugfs_dentry_glocks)
Robert Peterson7c52b162007-03-16 10:26:37 +00002179 return -ENOMEM;
2180
2181 return 0;
2182}
2183
2184void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2185{
Robert Peterson5f882092007-04-18 11:41:11 -05002186 if (sdp && sdp->debugfs_dir) {
2187 if (sdp->debugfs_dentry_glocks) {
2188 debugfs_remove(sdp->debugfs_dentry_glocks);
2189 sdp->debugfs_dentry_glocks = NULL;
2190 }
2191 debugfs_remove(sdp->debugfs_dir);
2192 sdp->debugfs_dir = NULL;
2193 }
Robert Peterson7c52b162007-03-16 10:26:37 +00002194}
2195
2196int gfs2_register_debugfs(void)
2197{
2198 gfs2_root = debugfs_create_dir("gfs2", NULL);
2199 return gfs2_root ? 0 : -ENOMEM;
2200}
2201
2202void gfs2_unregister_debugfs(void)
2203{
2204 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002205 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002206}