blob: 6b983aef785d6970b5eb6f1e8e75c539e250801c [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersoncf45b752008-01-31 10:31:39 -06003 * Copyright (C) 2004-2008 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>
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050030#include <linux/workqueue.h>
31#include <linux/jiffies.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
33#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035#include "glock.h"
36#include "glops.h"
37#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000038#include "lops.h"
39#include "meta_io.h"
40#include "quota.h"
41#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050042#include "util.h"
Steven Whitehouse813e0c42008-11-18 13:38:48 +000043#include "bmap.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000044
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040045struct gfs2_gl_hash_bucket {
Steven Whitehouseb6397892006-09-12 10:10:01 -040046 struct hlist_head hb_list;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -040047};
48
Steven Whitehouse6802e342008-05-21 17:03:22 +010049struct gfs2_glock_iter {
50 int hash; /* hash bucket index */
51 struct gfs2_sbd *sdp; /* incore superblock */
52 struct gfs2_glock *gl; /* current glock struct */
53 char string[512]; /* scratch space */
Robert Peterson7c52b162007-03-16 10:26:37 +000054};
55
David Teiglandb3b94fa2006-01-16 16:50:04 +000056typedef void (*glock_examiner) (struct gfs2_glock * gl);
57
Adrian Bunk08bc2db2006-04-28 10:59:12 -040058static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
Steven Whitehouse6802e342008-05-21 17:03:22 +010059static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
60#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
61static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050062
Steven Whitehouse61be0842007-01-29 11:51:45 +000063static DECLARE_RWSEM(gfs2_umount_flush_sem);
Robert Peterson7c52b162007-03-16 10:26:37 +000064static struct dentry *gfs2_root;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050065static struct workqueue_struct *glock_workqueue;
Steven Whitehouse97cc1022008-11-20 13:39:47 +000066static LIST_HEAD(lru_list);
67static atomic_t lru_count = ATOMIC_INIT(0);
Julia Lawalleb8374e2008-12-25 15:35:27 +010068static DEFINE_SPINLOCK(lru_lock);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040069
Steven Whitehouseb6397892006-09-12 10:10:01 -040070#define GFS2_GL_HASH_SHIFT 15
Steven Whitehouse087efdd2006-09-09 16:59:11 -040071#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
72#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
73
Steven Whitehouse85d1da62006-09-07 14:40:21 -040074static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
Robert Peterson04b933f2007-03-23 17:05:15 -050075static struct dentry *gfs2_root;
Steven Whitehouse087efdd2006-09-09 16:59:11 -040076
77/*
78 * Despite what you might think, the numbers below are not arbitrary :-)
79 * They are taken from the ipv4 routing hash code, which is well tested
80 * and thus should be nearly optimal. Later on we might tweek the numbers
81 * but for now this should be fine.
82 *
83 * The reason for putting the locks in a separate array from the list heads
84 * is that we can have fewer locks than list heads and save memory. We use
85 * the same hash function for both, but with a different hash mask.
86 */
87#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
88 defined(CONFIG_PROVE_LOCKING)
89
90#ifdef CONFIG_LOCKDEP
91# define GL_HASH_LOCK_SZ 256
92#else
93# if NR_CPUS >= 32
94# define GL_HASH_LOCK_SZ 4096
95# elif NR_CPUS >= 16
96# define GL_HASH_LOCK_SZ 2048
97# elif NR_CPUS >= 8
98# define GL_HASH_LOCK_SZ 1024
99# elif NR_CPUS >= 4
100# define GL_HASH_LOCK_SZ 512
101# else
102# define GL_HASH_LOCK_SZ 256
103# endif
104#endif
105
106/* We never want more locks than chains */
107#if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
108# undef GL_HASH_LOCK_SZ
109# define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
110#endif
111
112static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
113
114static inline rwlock_t *gl_lock_addr(unsigned int x)
115{
Steven Whitehouse94610612006-09-09 18:59:27 -0400116 return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400117}
118#else /* not SMP, so no spinlocks required */
Randy Dunlap0ac23062006-11-28 22:29:19 -0800119static inline rwlock_t *gl_lock_addr(unsigned int x)
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400120{
121 return NULL;
122}
123#endif
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400124
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 * gl_hash() - Turn glock number into hash bucket number
127 * @lock: The glock number
128 *
129 * Returns: The number of the corresponding hash bucket
130 */
131
Steven Whitehouseb8547852006-09-07 13:12:27 -0400132static unsigned int gl_hash(const struct gfs2_sbd *sdp,
133 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134{
135 unsigned int h;
136
Steven Whitehousecd915492006-09-04 12:49:07 -0400137 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -0400139 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 h &= GFS2_GL_HASH_MASK;
141
142 return h;
143}
144
145/**
146 * glock_free() - Perform a few checks and then release struct gfs2_glock
147 * @gl: The glock to release
148 *
149 * Also calls lock module to release its internal structure for this glock.
150 *
151 */
152
153static void glock_free(struct gfs2_glock *gl)
154{
155 struct gfs2_sbd *sdp = gl->gl_sbd;
156 struct inode *aspace = gl->gl_aspace;
157
Steven Whitehouse048bca22008-05-23 14:46:04 +0100158 if (sdp->sd_lockstruct.ls_ops->lm_put_lock)
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000159 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl->gl_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160
161 if (aspace)
162 gfs2_aspace_put(aspace);
163
164 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165}
166
167/**
168 * gfs2_glock_hold() - increment reference count on glock
169 * @gl: The glock to hold
170 *
171 */
172
Adrian Bunk048786f2008-01-29 00:11:34 +0200173static void gfs2_glock_hold(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174{
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400175 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176}
177
178/**
Steven Whitehouse97cc1022008-11-20 13:39:47 +0000179 * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
180 * @gl: the glock
181 *
182 */
183
184static void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
185{
186 spin_lock(&lru_lock);
187 if (list_empty(&gl->gl_lru) && gl->gl_state != LM_ST_UNLOCKED) {
188 list_add_tail(&gl->gl_lru, &lru_list);
189 atomic_inc(&lru_count);
190 }
191 spin_unlock(&lru_lock);
192}
193
194/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 * gfs2_glock_put() - Decrement reference count on glock
196 * @gl: The glock to put
197 *
198 */
199
200int gfs2_glock_put(struct gfs2_glock *gl)
201{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 int rv = 0;
203
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400204 write_lock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400205 if (atomic_dec_and_test(&gl->gl_ref)) {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400206 hlist_del(&gl->gl_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400207 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse97cc1022008-11-20 13:39:47 +0000208 spin_lock(&lru_lock);
209 if (!list_empty(&gl->gl_lru)) {
210 list_del_init(&gl->gl_lru);
211 atomic_dec(&lru_count);
212 }
213 spin_unlock(&lru_lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100214 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_UNLOCKED);
Steven Whitehouse97cc1022008-11-20 13:39:47 +0000215 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_lru));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100216 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 glock_free(gl);
218 rv = 1;
219 goto out;
220 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400221 write_unlock(gl_lock_addr(gl->gl_hash));
Steven Whitehouse97cc1022008-11-20 13:39:47 +0000222 /* 1 for being hashed, 1 for having state != LM_ST_UNLOCKED */
223 if (atomic_read(&gl->gl_ref) == 2)
224 gfs2_glock_schedule_for_reclaim(gl);
Steven Whitehousea2242db2006-08-24 17:03:05 -0400225out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000226 return rv;
227}
228
229/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 * search_bucket() - Find struct gfs2_glock by lock number
231 * @bucket: the bucket to search
232 * @name: The lock name
233 *
234 * Returns: NULL, or the struct gfs2_glock with the requested number
235 */
236
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400237static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400238 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400239 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240{
241 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -0400242 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243
Steven Whitehouseb6397892006-09-12 10:10:01 -0400244 hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 if (!lm_name_equal(&gl->gl_name, name))
246 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400247 if (gl->gl_sbd != sdp)
248 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400250 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251
252 return gl;
253 }
254
255 return NULL;
256}
257
258/**
259 * gfs2_glock_find() - Find glock by lock number
260 * @sdp: The GFS2 superblock
261 * @name: The lock name
262 *
263 * Returns: NULL, or the struct gfs2_glock with the requested number
264 */
265
Steven Whitehouse85d1da62006-09-07 14:40:21 -0400266static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400267 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400269 unsigned int hash = gl_hash(sdp, name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 struct gfs2_glock *gl;
271
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400272 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400273 gl = search_bucket(hash, sdp, name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400274 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275
276 return gl;
277}
278
Steven Whitehouse6802e342008-05-21 17:03:22 +0100279/**
280 * may_grant - check if its ok to grant a new lock
281 * @gl: The glock
282 * @gh: The lock request which we wish to grant
283 *
284 * Returns: true if its ok to grant the lock
285 */
286
287static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500288{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100289 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
290 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
291 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
292 return 0;
293 if (gl->gl_state == gh->gh_state)
294 return 1;
295 if (gh->gh_flags & GL_EXACT)
296 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100297 if (gl->gl_state == LM_ST_EXCLUSIVE) {
298 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
299 return 1;
300 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
301 return 1;
302 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100303 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
304 return 1;
305 return 0;
306}
307
308static void gfs2_holder_wake(struct gfs2_holder *gh)
309{
310 clear_bit(HIF_WAIT, &gh->gh_iflags);
311 smp_mb__after_clear_bit();
312 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
313}
314
315/**
316 * do_promote - promote as many requests as possible on the current queue
317 * @gl: The glock
318 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000319 * Returns: 1 if there is a blocked holder at the head of the list, or 2
320 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100321 */
322
323static int do_promote(struct gfs2_glock *gl)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700324__releases(&gl->gl_spin)
325__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100326{
327 const struct gfs2_glock_operations *glops = gl->gl_ops;
328 struct gfs2_holder *gh, *tmp;
329 int ret;
330
331restart:
332 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
333 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
334 continue;
335 if (may_grant(gl, gh)) {
336 if (gh->gh_list.prev == &gl->gl_holders &&
337 glops->go_lock) {
338 spin_unlock(&gl->gl_spin);
339 /* FIXME: eliminate this eventually */
340 ret = glops->go_lock(gh);
341 spin_lock(&gl->gl_spin);
342 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000343 if (ret == 1)
344 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100345 gh->gh_error = ret;
346 list_del_init(&gh->gh_list);
347 gfs2_holder_wake(gh);
348 goto restart;
349 }
350 set_bit(HIF_HOLDER, &gh->gh_iflags);
351 gfs2_holder_wake(gh);
352 goto restart;
353 }
354 set_bit(HIF_HOLDER, &gh->gh_iflags);
355 gfs2_holder_wake(gh);
356 continue;
357 }
358 if (gh->gh_list.prev == &gl->gl_holders)
359 return 1;
360 break;
361 }
362 return 0;
363}
364
365/**
366 * do_error - Something unexpected has happened during a lock request
367 *
368 */
369
370static inline void do_error(struct gfs2_glock *gl, const int ret)
371{
372 struct gfs2_holder *gh, *tmp;
373
374 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
375 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
376 continue;
377 if (ret & LM_OUT_ERROR)
378 gh->gh_error = -EIO;
379 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
380 gh->gh_error = GLR_TRYFAILED;
381 else
382 continue;
383 list_del_init(&gh->gh_list);
384 gfs2_holder_wake(gh);
385 }
386}
387
388/**
389 * find_first_waiter - find the first gh that's waiting for the glock
390 * @gl: the glock
391 */
392
393static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
394{
395 struct gfs2_holder *gh;
396
397 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
398 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
399 return gh;
400 }
401 return NULL;
402}
403
404/**
405 * state_change - record that the glock is now in a different state
406 * @gl: the glock
407 * @new_state the new state
408 *
409 */
410
411static void state_change(struct gfs2_glock *gl, unsigned int new_state)
412{
413 int held1, held2;
414
415 held1 = (gl->gl_state != LM_ST_UNLOCKED);
416 held2 = (new_state != LM_ST_UNLOCKED);
417
418 if (held1 != held2) {
419 if (held2)
420 gfs2_glock_hold(gl);
421 else
422 gfs2_glock_put(gl);
423 }
424
425 gl->gl_state = new_state;
426 gl->gl_tchange = jiffies;
427}
428
429static void gfs2_demote_wake(struct gfs2_glock *gl)
430{
431 gl->gl_demote_state = LM_ST_EXCLUSIVE;
432 clear_bit(GLF_DEMOTE, &gl->gl_flags);
433 smp_mb__after_clear_bit();
434 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
435}
436
437/**
438 * finish_xmote - The DLM has replied to one of our lock requests
439 * @gl: The glock
440 * @ret: The status from the DLM
441 *
442 */
443
444static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
445{
446 const struct gfs2_glock_operations *glops = gl->gl_ops;
447 struct gfs2_holder *gh;
448 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000449 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500450
451 spin_lock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100452 state_change(gl, state);
453 gh = find_first_waiter(gl);
454
455 /* Demote to UN request arrived during demote to SH or DF */
456 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
457 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
458 gl->gl_target = LM_ST_UNLOCKED;
459
460 /* Check for state != intended state */
461 if (unlikely(state != gl->gl_target)) {
462 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
463 /* move to back of queue and try next entry */
464 if (ret & LM_OUT_CANCELED) {
465 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
466 list_move_tail(&gh->gh_list, &gl->gl_holders);
467 gh = find_first_waiter(gl);
468 gl->gl_target = gh->gh_state;
469 goto retry;
470 }
471 /* Some error or failed "try lock" - report it */
472 if ((ret & LM_OUT_ERROR) ||
473 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
474 gl->gl_target = gl->gl_state;
475 do_error(gl, ret);
476 goto out;
477 }
478 }
479 switch(state) {
480 /* Unlocked due to conversion deadlock, try again */
481 case LM_ST_UNLOCKED:
482retry:
483 do_xmote(gl, gh, gl->gl_target);
484 break;
485 /* Conversion fails, unlock and try again */
486 case LM_ST_SHARED:
487 case LM_ST_DEFERRED:
488 do_xmote(gl, gh, LM_ST_UNLOCKED);
489 break;
490 default: /* Everything else */
491 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
492 GLOCK_BUG_ON(gl, 1);
493 }
494 spin_unlock(&gl->gl_spin);
495 gfs2_glock_put(gl);
496 return;
497 }
498
499 /* Fast path - we got what we asked for */
500 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
501 gfs2_demote_wake(gl);
502 if (state != LM_ST_UNLOCKED) {
503 if (glops->go_xmote_bh) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100504 spin_unlock(&gl->gl_spin);
505 rv = glops->go_xmote_bh(gl, gh);
506 if (rv == -EAGAIN)
507 return;
508 spin_lock(&gl->gl_spin);
509 if (rv) {
510 do_error(gl, rv);
511 goto out;
512 }
513 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000514 rv = do_promote(gl);
515 if (rv == 2)
516 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100517 }
518out:
519 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000520out_locked:
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500521 spin_unlock(&gl->gl_spin);
522 gfs2_glock_put(gl);
523}
524
Steven Whitehouse6802e342008-05-21 17:03:22 +0100525static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
526 unsigned int cur_state, unsigned int req_state,
527 unsigned int flags)
528{
529 int ret = LM_OUT_ERROR;
Steven Whitehouse048bca22008-05-23 14:46:04 +0100530
531 if (!sdp->sd_lockstruct.ls_ops->lm_lock)
532 return req_state == LM_ST_UNLOCKED ? 0 : req_state;
533
Steven Whitehouse6802e342008-05-21 17:03:22 +0100534 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
535 ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock, cur_state,
536 req_state, flags);
537 return ret;
538}
539
540/**
541 * do_xmote - Calls the DLM to change the state of a lock
542 * @gl: The lock state
543 * @gh: The holder (only for promotes)
544 * @target: The target lock state
545 *
546 */
547
548static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700549__releases(&gl->gl_spin)
550__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100551{
552 const struct gfs2_glock_operations *glops = gl->gl_ops;
553 struct gfs2_sbd *sdp = gl->gl_sbd;
554 unsigned int lck_flags = gh ? gh->gh_flags : 0;
555 int ret;
556
557 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
558 LM_FLAG_PRIORITY);
559 BUG_ON(gl->gl_state == target);
560 BUG_ON(gl->gl_state == gl->gl_target);
561 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
562 glops->go_inval) {
563 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
564 do_error(gl, 0); /* Fail queued try locks */
565 }
566 spin_unlock(&gl->gl_spin);
567 if (glops->go_xmote_th)
568 glops->go_xmote_th(gl);
569 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
570 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
571 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
572
573 gfs2_glock_hold(gl);
574 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
575 gl->gl_state == LM_ST_DEFERRED) &&
576 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
577 lck_flags |= LM_FLAG_TRY_1CB;
578 ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, target, lck_flags);
579
580 if (!(ret & LM_OUT_ASYNC)) {
581 finish_xmote(gl, ret);
582 gfs2_glock_hold(gl);
583 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
584 gfs2_glock_put(gl);
585 } else {
586 GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC);
587 }
588 spin_lock(&gl->gl_spin);
589}
590
591/**
592 * find_first_holder - find the first "holder" gh
593 * @gl: the glock
594 */
595
596static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
597{
598 struct gfs2_holder *gh;
599
600 if (!list_empty(&gl->gl_holders)) {
601 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
602 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
603 return gh;
604 }
605 return NULL;
606}
607
608/**
609 * run_queue - do all outstanding tasks related to a glock
610 * @gl: The glock in question
611 * @nonblock: True if we must not block in run_queue
612 *
613 */
614
615static void run_queue(struct gfs2_glock *gl, const int nonblock)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700616__releases(&gl->gl_spin)
617__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100618{
619 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000620 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100621
622 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
623 return;
624
625 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
626
627 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
628 gl->gl_demote_state != gl->gl_state) {
629 if (find_first_holder(gl))
630 goto out;
631 if (nonblock)
632 goto out_sched;
633 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100634 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100635 gl->gl_target = gl->gl_demote_state;
636 } else {
637 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
638 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000639 ret = do_promote(gl);
640 if (ret == 0)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100641 goto out;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000642 if (ret == 2)
643 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100644 gh = find_first_waiter(gl);
645 gl->gl_target = gh->gh_state;
646 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
647 do_error(gl, 0); /* Fail queued try locks */
648 }
649 do_xmote(gl, gh, gl->gl_target);
650 return;
651
652out_sched:
653 gfs2_glock_hold(gl);
654 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
655 gfs2_glock_put(gl);
656out:
657 clear_bit(GLF_LOCK, &gl->gl_flags);
658}
659
660static void glock_work_func(struct work_struct *work)
661{
662 unsigned long delay = 0;
663 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
664
665 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags))
666 finish_xmote(gl, gl->gl_reply);
667 spin_lock(&gl->gl_spin);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100668 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
669 gl->gl_state != LM_ST_UNLOCKED &&
670 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100671 unsigned long holdtime, now = jiffies;
672 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
673 if (time_before(now, holdtime))
674 delay = holdtime - now;
675 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
676 }
677 run_queue(gl, 0);
678 spin_unlock(&gl->gl_spin);
679 if (!delay ||
680 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
681 gfs2_glock_put(gl);
682}
683
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000684static int gfs2_lm_get_lock(struct gfs2_sbd *sdp, struct lm_lockname *name,
685 void **lockp)
686{
687 int error = -EIO;
Steven Whitehouse048bca22008-05-23 14:46:04 +0100688 if (!sdp->sd_lockstruct.ls_ops->lm_get_lock)
689 return 0;
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000690 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
691 error = sdp->sd_lockstruct.ls_ops->lm_get_lock(
692 sdp->sd_lockstruct.ls_lockspace, name, lockp);
693 return error;
694}
695
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696/**
697 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
698 * @sdp: The GFS2 superblock
699 * @number: the lock number
700 * @glops: The glock_operations to use
701 * @create: If 0, don't create the glock if it doesn't exist
702 * @glp: the glock is returned here
703 *
704 * This does not lock a glock, just finds/creates structures for one.
705 *
706 * Returns: errno
707 */
708
Steven Whitehousecd915492006-09-04 12:49:07 -0400709int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400710 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711 struct gfs2_glock **glp)
712{
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400713 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400715 unsigned int hash = gl_hash(sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 int error;
717
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400718 read_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400719 gl = search_bucket(hash, sdp, &name);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400720 read_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721
722 if (gl || !create) {
723 *glp = gl;
724 return 0;
725 }
726
727 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
728 if (!gl)
729 return -ENOMEM;
730
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400731 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 gl->gl_name = name;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400733 atomic_set(&gl->gl_ref, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100735 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500736 gl->gl_demote_state = LM_ST_EXCLUSIVE;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400737 gl->gl_hash = hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000738 gl->gl_ops = glops;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400739 gl->gl_stamp = jiffies;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500740 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400741 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 gl->gl_sbd = sdp;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400743 gl->gl_aspace = NULL;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500744 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000745
746 /* If this glock protects actual on-disk data or metadata blocks,
747 create a VFS inode to manage the pages/buffers holding them. */
Steven Whitehouse50299962006-09-04 09:49:55 -0400748 if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 gl->gl_aspace = gfs2_aspace_get(sdp);
750 if (!gl->gl_aspace) {
751 error = -ENOMEM;
752 goto fail;
753 }
754 }
755
756 error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
757 if (error)
758 goto fail_aspace;
759
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400760 write_lock(gl_lock_addr(hash));
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400761 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762 if (tmp) {
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400763 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 glock_free(gl);
765 gl = tmp;
766 } else {
Steven Whitehouseb6397892006-09-12 10:10:01 -0400767 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
Steven Whitehouse087efdd2006-09-09 16:59:11 -0400768 write_unlock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000769 }
770
771 *glp = gl;
772
773 return 0;
774
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400775fail_aspace:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 if (gl->gl_aspace)
777 gfs2_aspace_put(gl->gl_aspace);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400778fail:
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400779 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 return error;
781}
782
783/**
784 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
785 * @gl: the glock
786 * @state: the state we're requesting
787 * @flags: the modifier flags
788 * @gh: the holder structure
789 *
790 */
791
Steven Whitehouse190562b2006-04-20 16:57:23 -0400792void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 struct gfs2_holder *gh)
794{
795 INIT_LIST_HEAD(&gh->gh_list);
796 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500797 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800798 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 gh->gh_state = state;
800 gh->gh_flags = flags;
801 gh->gh_error = 0;
802 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803 gfs2_glock_hold(gl);
804}
805
806/**
807 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
808 * @state: the state we're requesting
809 * @flags: the modifier flags
810 * @gh: the holder structure
811 *
812 * Don't mess with the glock.
813 *
814 */
815
Steven Whitehouse190562b2006-04-20 16:57:23 -0400816void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817{
818 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400819 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000820 gh->gh_iflags = 0;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500821 gh->gh_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822}
823
824/**
825 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
826 * @gh: the holder structure
827 *
828 */
829
830void gfs2_holder_uninit(struct gfs2_holder *gh)
831{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800832 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 gfs2_glock_put(gh->gh_gl);
834 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500835 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836}
837
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100838static int just_schedule(void *word)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000839{
840 schedule();
841 return 0;
842}
843
844static void wait_on_holder(struct gfs2_holder *gh)
845{
846 might_sleep();
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100847 wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
848}
849
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100850static void wait_on_demote(struct gfs2_glock *gl)
851{
852 might_sleep();
853 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000854}
855
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000857 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 * @gl: the glock
859 * @state: the state the caller wants us to change to
860 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000861 * There are only two requests that we are going to see in actual
862 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000863 */
864
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500865static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse97cc1022008-11-20 13:39:47 +0000866 unsigned long delay)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500868 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
869
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500870 set_bit(bit, &gl->gl_flags);
871 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000872 gl->gl_demote_state = state;
873 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100874 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
875 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100876 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878}
879
880/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100881 * gfs2_glock_wait - wait on a glock acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 * @gh: the glock holder
883 *
884 * Returns: 0 on success
885 */
886
Steven Whitehouse6802e342008-05-21 17:03:22 +0100887int gfs2_glock_wait(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888{
Steven Whitehousefee852e2007-01-17 15:33:23 +0000889 wait_on_holder(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 return gh->gh_error;
891}
892
Steven Whitehouse6802e342008-05-21 17:03:22 +0100893void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000894{
895 va_list args;
896
897 va_start(args, fmt);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100898 if (seq) {
899 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +0000900 vsprintf(gi->string, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100901 seq_printf(seq, gi->string);
902 } else {
903 printk(KERN_ERR " ");
Robert Peterson7c52b162007-03-16 10:26:37 +0000904 vprintk(fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100905 }
Robert Peterson7c52b162007-03-16 10:26:37 +0000906 va_end(args);
907}
908
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 * add_to_queue - Add a holder to the wait queue (but look for recursion)
911 * @gh: the holder structure to add
912 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100913 * Eventually we should move the recursive locking trap to a
914 * debugging option or something like that. This is the fast
915 * path and needs to have the minimum number of distractions.
916 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 */
918
Steven Whitehouse6802e342008-05-21 17:03:22 +0100919static inline void add_to_queue(struct gfs2_holder *gh)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700920__releases(&gl->gl_spin)
921__acquires(&gl->gl_spin)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922{
923 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100924 struct gfs2_sbd *sdp = gl->gl_sbd;
925 struct list_head *insert_pt = NULL;
926 struct gfs2_holder *gh2;
927 int try_lock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800929 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000930 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
931 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -0400932
Steven Whitehouse6802e342008-05-21 17:03:22 +0100933 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
934 if (test_bit(GLF_LOCK, &gl->gl_flags))
935 try_lock = 1;
936 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
937 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 }
939
Steven Whitehouse6802e342008-05-21 17:03:22 +0100940 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
941 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
942 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
943 goto trap_recursive;
944 if (try_lock &&
945 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
946 !may_grant(gl, gh)) {
947fail:
948 gh->gh_error = GLR_TRYFAILED;
949 gfs2_holder_wake(gh);
950 return;
951 }
952 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
953 continue;
954 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
955 insert_pt = &gh2->gh_list;
956 }
957 if (likely(insert_pt == NULL)) {
958 list_add_tail(&gh->gh_list, &gl->gl_holders);
959 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
960 goto do_cancel;
961 return;
962 }
963 list_add_tail(&gh->gh_list, insert_pt);
964do_cancel:
965 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
966 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
967 spin_unlock(&gl->gl_spin);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100968 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
969 sdp->sd_lockstruct.ls_ops->lm_cancel(gl->gl_lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100970 spin_lock(&gl->gl_spin);
971 }
972 return;
973
974trap_recursive:
975 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
976 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
977 printk(KERN_ERR "lock type: %d req lock state : %d\n",
978 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
979 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
980 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
981 printk(KERN_ERR "lock type: %d req lock state : %d\n",
982 gh->gh_gl->gl_name.ln_type, gh->gh_state);
983 __dump_glock(NULL, gl);
984 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985}
986
987/**
988 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
989 * @gh: the holder structure
990 *
991 * if (gh->gh_flags & GL_ASYNC), this never returns an error
992 *
993 * Returns: 0, GLR_TRYFAILED, or errno on failure
994 */
995
996int gfs2_glock_nq(struct gfs2_holder *gh)
997{
998 struct gfs2_glock *gl = gh->gh_gl;
999 struct gfs2_sbd *sdp = gl->gl_sbd;
1000 int error = 0;
1001
Steven Whitehouse6802e342008-05-21 17:03:22 +01001002 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 spin_lock(&gl->gl_spin);
1006 add_to_queue(gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001007 run_queue(gl, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 spin_unlock(&gl->gl_spin);
1009
Steven Whitehouse6802e342008-05-21 17:03:22 +01001010 if (!(gh->gh_flags & GL_ASYNC))
1011 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 return error;
1014}
1015
1016/**
1017 * gfs2_glock_poll - poll to see if an async request has been completed
1018 * @gh: the holder
1019 *
1020 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1021 */
1022
1023int gfs2_glock_poll(struct gfs2_holder *gh)
1024{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001025 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026}
1027
1028/**
1029 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1030 * @gh: the glock holder
1031 *
1032 */
1033
1034void gfs2_glock_dq(struct gfs2_holder *gh)
1035{
1036 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001037 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001038 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001039 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040
Steven Whitehouse6802e342008-05-21 17:03:22 +01001041 spin_lock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001043 handle_callback(gl, LM_ST_UNLOCKED, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 list_del_init(&gh->gh_list);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001046 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001047 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001048 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001049 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 glops->go_unlock(gh);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001051 spin_lock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001052 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001053 }
Steven Whitehouse3b8249f2007-03-16 09:40:31 +00001054 gl->gl_stamp = jiffies;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001055 if (list_empty(&gl->gl_holders) &&
1056 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1057 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1058 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001061 if (likely(fast_path))
1062 return;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001063
1064 gfs2_glock_hold(gl);
1065 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1066 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1067 delay = gl->gl_ops->go_min_hold_time;
1068 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1069 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070}
1071
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001072void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1073{
1074 struct gfs2_glock *gl = gh->gh_gl;
1075 gfs2_glock_dq(gh);
1076 wait_on_demote(gl);
1077}
1078
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1081 * @gh: the holder structure
1082 *
1083 */
1084
1085void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1086{
1087 gfs2_glock_dq(gh);
1088 gfs2_holder_uninit(gh);
1089}
1090
1091/**
1092 * gfs2_glock_nq_num - acquire a glock based on lock number
1093 * @sdp: the filesystem
1094 * @number: the lock number
1095 * @glops: the glock operations for the type of glock
1096 * @state: the state to acquire the glock in
1097 * @flags: modifier flags for the aquisition
1098 * @gh: the struct gfs2_holder
1099 *
1100 * Returns: errno
1101 */
1102
Steven Whitehousecd915492006-09-04 12:49:07 -04001103int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001104 const struct gfs2_glock_operations *glops,
1105 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106{
1107 struct gfs2_glock *gl;
1108 int error;
1109
1110 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1111 if (!error) {
1112 error = gfs2_glock_nq_init(gl, state, flags, gh);
1113 gfs2_glock_put(gl);
1114 }
1115
1116 return error;
1117}
1118
1119/**
1120 * glock_compare - Compare two struct gfs2_glock structures for sorting
1121 * @arg_a: the first structure
1122 * @arg_b: the second structure
1123 *
1124 */
1125
1126static int glock_compare(const void *arg_a, const void *arg_b)
1127{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001128 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1129 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1130 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1131 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132
1133 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001134 return 1;
1135 if (a->ln_number < b->ln_number)
1136 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001137 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 -04001138 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139}
1140
1141/**
1142 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1143 * @num_gh: the number of structures
1144 * @ghs: an array of struct gfs2_holder structures
1145 *
1146 * Returns: 0 on success (all glocks acquired),
1147 * errno on failure (no glocks acquired)
1148 */
1149
1150static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1151 struct gfs2_holder **p)
1152{
1153 unsigned int x;
1154 int error = 0;
1155
1156 for (x = 0; x < num_gh; x++)
1157 p[x] = &ghs[x];
1158
1159 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1160
1161 for (x = 0; x < num_gh; x++) {
1162 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1163
1164 error = gfs2_glock_nq(p[x]);
1165 if (error) {
1166 while (x--)
1167 gfs2_glock_dq(p[x]);
1168 break;
1169 }
1170 }
1171
1172 return error;
1173}
1174
1175/**
1176 * gfs2_glock_nq_m - acquire multiple glocks
1177 * @num_gh: the number of structures
1178 * @ghs: an array of struct gfs2_holder structures
1179 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 *
1181 * Returns: 0 on success (all glocks acquired),
1182 * errno on failure (no glocks acquired)
1183 */
1184
1185int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1186{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001187 struct gfs2_holder *tmp[4];
1188 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 int error = 0;
1190
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001191 switch(num_gh) {
1192 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001194 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1196 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001197 default:
1198 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001200 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1201 if (!pph)
1202 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 }
1204
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001205 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001207 if (pph != tmp)
1208 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209
1210 return error;
1211}
1212
1213/**
1214 * gfs2_glock_dq_m - release multiple glocks
1215 * @num_gh: the number of structures
1216 * @ghs: an array of struct gfs2_holder structures
1217 *
1218 */
1219
1220void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1221{
1222 unsigned int x;
1223
1224 for (x = 0; x < num_gh; x++)
1225 gfs2_glock_dq(&ghs[x]);
1226}
1227
1228/**
1229 * gfs2_glock_dq_uninit_m - release multiple glocks
1230 * @num_gh: the number of structures
1231 * @ghs: an array of struct gfs2_holder structures
1232 *
1233 */
1234
1235void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1236{
1237 unsigned int x;
1238
1239 for (x = 0; x < num_gh; x++)
1240 gfs2_glock_dq_uninit(&ghs[x]);
1241}
1242
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001243static int gfs2_lm_hold_lvb(struct gfs2_sbd *sdp, void *lock, char **lvbp)
1244{
1245 int error = -EIO;
Steven Whitehouse048bca22008-05-23 14:46:04 +01001246 if (!sdp->sd_lockstruct.ls_ops->lm_hold_lvb)
1247 return 0;
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001248 if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
1249 error = sdp->sd_lockstruct.ls_ops->lm_hold_lvb(lock, lvbp);
1250 return error;
1251}
1252
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 * gfs2_lvb_hold - attach a LVB from a glock
1255 * @gl: The glock in question
1256 *
1257 */
1258
1259int gfs2_lvb_hold(struct gfs2_glock *gl)
1260{
1261 int error;
1262
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263 if (!atomic_read(&gl->gl_lvb_count)) {
1264 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001265 if (error)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001266 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267 gfs2_glock_hold(gl);
1268 }
1269 atomic_inc(&gl->gl_lvb_count);
1270
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 return 0;
1272}
1273
1274/**
1275 * gfs2_lvb_unhold - detach a LVB from a glock
1276 * @gl: The glock in question
1277 *
1278 */
1279
1280void gfs2_lvb_unhold(struct gfs2_glock *gl)
1281{
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001282 struct gfs2_sbd *sdp = gl->gl_sbd;
1283
David Teiglandb3b94fa2006-01-16 16:50:04 +00001284 gfs2_glock_hold(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1286 if (atomic_dec_and_test(&gl->gl_lvb_count)) {
Steven Whitehouse048bca22008-05-23 14:46:04 +01001287 if (sdp->sd_lockstruct.ls_ops->lm_unhold_lvb)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001288 sdp->sd_lockstruct.ls_ops->lm_unhold_lvb(gl->gl_lock, gl->gl_lvb);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289 gl->gl_lvb = NULL;
1290 gfs2_glock_put(gl);
1291 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292 gfs2_glock_put(gl);
1293}
1294
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1296 unsigned int state)
1297{
1298 struct gfs2_glock *gl;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001299 unsigned long delay = 0;
1300 unsigned long holdtime;
1301 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001302
1303 gl = gfs2_glock_find(sdp, name);
1304 if (!gl)
1305 return;
1306
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001307 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
1308 if (time_before(now, holdtime))
1309 delay = holdtime - now;
Steven Whitehousedff52572008-09-02 13:33:17 +01001310 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1311 delay = gl->gl_ops->go_min_hold_time;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312
Steven Whitehouse6802e342008-05-21 17:03:22 +01001313 spin_lock(&gl->gl_spin);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001314 handle_callback(gl, state, delay);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001315 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001316 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1317 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318}
1319
Steven Whitehouse2bfb6442008-11-26 13:30:49 +00001320static void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
1321{
1322 struct gfs2_jdesc *jd;
1323
1324 spin_lock(&sdp->sd_jindex_spin);
1325 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
1326 if (jd->jd_jid != jid)
1327 continue;
1328 jd->jd_dirty = 1;
1329 break;
1330 }
1331 spin_unlock(&sdp->sd_jindex_spin);
1332}
1333
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334/**
1335 * gfs2_glock_cb - Callback used by locking module
Steven Whitehouse1c089c32006-09-07 15:50:20 -04001336 * @sdp: Pointer to the superblock
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337 * @type: Type of callback
1338 * @data: Type dependent data pointer
1339 *
1340 * Called by the locking module when it wants to tell us something.
1341 * Either we need to drop a lock, one of our ASYNC requests completed, or
1342 * a journal from another client needs to be recovered.
1343 */
1344
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001345void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346{
Steven Whitehouse9b47c112006-09-08 10:17:58 -04001347 struct gfs2_sbd *sdp = cb_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348
David Teiglandb3b94fa2006-01-16 16:50:04 +00001349 switch (type) {
1350 case LM_CB_NEED_E:
David Teiglande7f5c012006-04-27 11:25:45 -04001351 blocking_cb(sdp, data, LM_ST_UNLOCKED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 return;
1353
1354 case LM_CB_NEED_D:
David Teiglande7f5c012006-04-27 11:25:45 -04001355 blocking_cb(sdp, data, LM_ST_DEFERRED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356 return;
1357
1358 case LM_CB_NEED_S:
David Teiglande7f5c012006-04-27 11:25:45 -04001359 blocking_cb(sdp, data, LM_ST_SHARED);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001360 return;
1361
1362 case LM_CB_ASYNC: {
David Teiglande7f5c012006-04-27 11:25:45 -04001363 struct lm_async_cb *async = data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001364 struct gfs2_glock *gl;
1365
Steven Whitehouse61be0842007-01-29 11:51:45 +00001366 down_read(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 gl = gfs2_glock_find(sdp, &async->lc_name);
1368 if (gfs2_assert_warn(sdp, gl))
1369 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001370 gl->gl_reply = async->lc_ret;
1371 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001372 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1373 gfs2_glock_put(gl);
Steven Whitehouse61be0842007-01-29 11:51:45 +00001374 up_read(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001375 return;
1376 }
1377
1378 case LM_CB_NEED_RECOVERY:
1379 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1380 if (sdp->sd_recoverd_process)
1381 wake_up_process(sdp->sd_recoverd_process);
1382 return;
1383
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 default:
1385 gfs2_assert_warn(sdp, 0);
1386 return;
1387 }
1388}
1389
1390/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 * demote_ok - Check to see if it's ok to unlock a glock
1392 * @gl: the glock
1393 *
1394 * Returns: 1 if it's ok
1395 */
1396
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001397static int demote_ok(const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398{
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001399 const struct gfs2_glock_operations *glops = gl->gl_ops;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001401 if (gl->gl_state == LM_ST_UNLOCKED)
1402 return 0;
1403 if (!list_empty(&gl->gl_holders))
1404 return 0;
1405 if (glops->go_demote_ok)
1406 return glops->go_demote_ok(gl);
1407 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408}
1409
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001411static int gfs2_shrink_glock_memory(int nr, gfp_t gfp_mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001412{
1413 struct gfs2_glock *gl;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001414 int may_demote;
1415 int nr_skipped = 0;
1416 int got_ref = 0;
1417 LIST_HEAD(skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001419 if (nr == 0)
1420 goto out;
1421
1422 if (!(gfp_mask & __GFP_FS))
1423 return -1;
1424
1425 spin_lock(&lru_lock);
1426 while(nr && !list_empty(&lru_list)) {
1427 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1428 list_del_init(&gl->gl_lru);
1429 atomic_dec(&lru_count);
1430
1431 /* Test for being demotable */
1432 if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1433 gfs2_glock_hold(gl);
1434 got_ref = 1;
1435 spin_unlock(&lru_lock);
1436 spin_lock(&gl->gl_spin);
1437 may_demote = demote_ok(gl);
1438 spin_unlock(&gl->gl_spin);
1439 clear_bit(GLF_LOCK, &gl->gl_flags);
1440 if (may_demote) {
1441 handle_callback(gl, LM_ST_UNLOCKED, 0);
1442 nr--;
1443 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1444 gfs2_glock_put(gl);
1445 }
1446 spin_lock(&lru_lock);
1447 if (may_demote)
1448 continue;
1449 }
1450 if (list_empty(&gl->gl_lru) &&
1451 (atomic_read(&gl->gl_ref) <= (2 + got_ref))) {
1452 nr_skipped++;
1453 list_add(&gl->gl_lru, &skipped);
1454 }
1455 if (got_ref) {
1456 spin_unlock(&lru_lock);
1457 gfs2_glock_put(gl);
1458 spin_lock(&lru_lock);
1459 got_ref = 0;
1460 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001462 list_splice(&skipped, &lru_list);
1463 atomic_add(nr_skipped, &lru_count);
1464 spin_unlock(&lru_lock);
1465out:
1466 return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467}
1468
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001469static struct shrinker glock_shrinker = {
1470 .shrink = gfs2_shrink_glock_memory,
1471 .seeks = DEFAULT_SEEKS,
1472};
1473
David Teiglandb3b94fa2006-01-16 16:50:04 +00001474/**
1475 * examine_bucket - Call a function for glock in a hash bucket
1476 * @examiner: the function
1477 * @sdp: the filesystem
1478 * @bucket: the bucket
1479 *
1480 * Returns: 1 if the bucket has entries
1481 */
1482
1483static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001484 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485{
Steven Whitehouse24264432006-09-11 21:40:30 -04001486 struct gfs2_glock *gl, *prev = NULL;
1487 int has_entries = 0;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001488 struct hlist_head *head = &gl_hash_table[hash].hb_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489
Steven Whitehouse24264432006-09-11 21:40:30 -04001490 read_lock(gl_lock_addr(hash));
Steven Whitehouseb6397892006-09-12 10:10:01 -04001491 /* Can't use hlist_for_each_entry - don't want prefetch here */
1492 if (hlist_empty(head))
Steven Whitehouse24264432006-09-11 21:40:30 -04001493 goto out;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001494 gl = list_entry(head->first, struct gfs2_glock, gl_list);
1495 while(1) {
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001496 if (!sdp || gl->gl_sbd == sdp) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497 gfs2_glock_hold(gl);
Steven Whitehouse24264432006-09-11 21:40:30 -04001498 read_unlock(gl_lock_addr(hash));
1499 if (prev)
1500 gfs2_glock_put(prev);
1501 prev = gl;
1502 examiner(gl);
Steven Whitehousea8336342006-09-14 13:57:38 -04001503 has_entries = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04001504 read_lock(gl_lock_addr(hash));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001505 }
Steven Whitehouseb6397892006-09-12 10:10:01 -04001506 if (gl->gl_list.next == NULL)
1507 break;
Steven Whitehouse24264432006-09-11 21:40:30 -04001508 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001509 }
Steven Whitehouse24264432006-09-11 21:40:30 -04001510out:
1511 read_unlock(gl_lock_addr(hash));
1512 if (prev)
1513 gfs2_glock_put(prev);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001514 cond_resched();
Steven Whitehouse24264432006-09-11 21:40:30 -04001515 return has_entries;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001516}
1517
1518/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001519 * clear_glock - look at a glock and see if we can free it from glock cache
1520 * @gl: the glock to look at
1521 *
1522 */
1523
1524static void clear_glock(struct gfs2_glock *gl)
1525{
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001526 spin_lock(&lru_lock);
1527 if (!list_empty(&gl->gl_lru)) {
1528 list_del_init(&gl->gl_lru);
1529 atomic_dec(&lru_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001530 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001531 spin_unlock(&lru_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001532
Steven Whitehouse6802e342008-05-21 17:03:22 +01001533 spin_lock(&gl->gl_spin);
1534 if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001535 handle_callback(gl, LM_ST_UNLOCKED, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001536 spin_unlock(&gl->gl_spin);
1537 gfs2_glock_hold(gl);
1538 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1539 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001540}
1541
1542/**
1543 * gfs2_gl_hash_clear - Empty out the glock hash table
1544 * @sdp: the filesystem
1545 * @wait: wait until it's all gone
1546 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001547 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548 */
1549
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001550void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001551{
1552 unsigned long t;
1553 unsigned int x;
1554 int cont;
1555
1556 t = jiffies;
1557
1558 for (;;) {
1559 cont = 0;
Steven Whitehouse24264432006-09-11 21:40:30 -04001560 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
Steven Whitehouse907b9bc2006-09-25 09:26:04 -04001561 if (examine_bucket(clear_glock, sdp, x))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001562 cont = 1;
Steven Whitehouse24264432006-09-11 21:40:30 -04001563 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001564
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001565 if (!cont)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001566 break;
1567
1568 if (time_after_eq(jiffies,
1569 t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1570 fs_warn(sdp, "Unmount seems to be stalled. "
1571 "Dumping lock state...\n");
1572 gfs2_dump_lockstate(sdp);
1573 t = jiffies;
1574 }
1575
Steven Whitehouse61be0842007-01-29 11:51:45 +00001576 down_write(&gfs2_umount_flush_sem);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001577 invalidate_inodes(sdp->sd_vfs);
Steven Whitehouse61be0842007-01-29 11:51:45 +00001578 up_write(&gfs2_umount_flush_sem);
Steven Whitehousefd88de562006-05-05 16:59:11 -04001579 msleep(10);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001580 }
1581}
1582
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001583void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1584{
1585 struct gfs2_glock *gl = ip->i_gl;
1586 int ret;
1587
1588 ret = gfs2_truncatei_resume(ip);
1589 gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
1590
1591 spin_lock(&gl->gl_spin);
1592 clear_bit(GLF_LOCK, &gl->gl_flags);
1593 run_queue(gl, 1);
1594 spin_unlock(&gl->gl_spin);
1595}
1596
Steven Whitehouse6802e342008-05-21 17:03:22 +01001597static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001598{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001599 switch(state) {
1600 case LM_ST_UNLOCKED:
1601 return "UN";
1602 case LM_ST_SHARED:
1603 return "SH";
1604 case LM_ST_DEFERRED:
1605 return "DF";
1606 case LM_ST_EXCLUSIVE:
1607 return "EX";
1608 }
1609 return "??";
1610}
Robert Peterson04b933f2007-03-23 17:05:15 -05001611
Steven Whitehouse6802e342008-05-21 17:03:22 +01001612static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1613{
1614 char *p = buf;
1615 if (flags & LM_FLAG_TRY)
1616 *p++ = 't';
1617 if (flags & LM_FLAG_TRY_1CB)
1618 *p++ = 'T';
1619 if (flags & LM_FLAG_NOEXP)
1620 *p++ = 'e';
1621 if (flags & LM_FLAG_ANY)
1622 *p++ = 'a';
1623 if (flags & LM_FLAG_PRIORITY)
1624 *p++ = 'p';
1625 if (flags & GL_ASYNC)
1626 *p++ = 'a';
1627 if (flags & GL_EXACT)
1628 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001629 if (flags & GL_NOCACHE)
1630 *p++ = 'c';
1631 if (test_bit(HIF_HOLDER, &iflags))
1632 *p++ = 'H';
1633 if (test_bit(HIF_WAIT, &iflags))
1634 *p++ = 'W';
1635 if (test_bit(HIF_FIRST, &iflags))
1636 *p++ = 'F';
1637 *p = 0;
1638 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001639}
1640
David Teiglandb3b94fa2006-01-16 16:50:04 +00001641/**
1642 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001643 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001644 * @gh: the glock holder
1645 *
1646 * Returns: 0 on success, -ENOBUFS when we run out of space
1647 */
1648
Steven Whitehouse6802e342008-05-21 17:03:22 +01001649static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001650{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001651 struct task_struct *gh_owner = NULL;
1652 char buffer[KSYM_SYMBOL_LEN];
1653 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001654
Steven Whitehouse6802e342008-05-21 17:03:22 +01001655 sprint_symbol(buffer, gh->gh_ip);
1656 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001657 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001658 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n",
1659 state2str(gh->gh_state),
1660 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1661 gh->gh_error,
1662 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1663 gh_owner ? gh_owner->comm : "(ended)", buffer);
Robert Peterson7c52b162007-03-16 10:26:37 +00001664 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001665}
1666
Steven Whitehouse6802e342008-05-21 17:03:22 +01001667static const char *gflags2str(char *buf, const unsigned long *gflags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001668{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001669 char *p = buf;
1670 if (test_bit(GLF_LOCK, gflags))
1671 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001672 if (test_bit(GLF_DEMOTE, gflags))
1673 *p++ = 'D';
1674 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1675 *p++ = 'd';
1676 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1677 *p++ = 'p';
1678 if (test_bit(GLF_DIRTY, gflags))
1679 *p++ = 'y';
1680 if (test_bit(GLF_LFLUSH, gflags))
1681 *p++ = 'f';
1682 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1683 *p++ = 'i';
1684 if (test_bit(GLF_REPLY_PENDING, gflags))
1685 *p++ = 'r';
1686 *p = 0;
1687 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001688}
1689
1690/**
Steven Whitehouse6802e342008-05-21 17:03:22 +01001691 * __dump_glock - print information about a glock
1692 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001693 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001694 *
1695 * The file format is as follows:
1696 * One line per object, capital letters are used to indicate objects
1697 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1698 * other objects are indented by a single space and follow the glock to
1699 * which they are related. Fields are indicated by lower case letters
1700 * followed by a colon and the field value, except for strings which are in
1701 * [] so that its possible to see if they are composed of spaces for
1702 * example. The field's are n = number (id of the object), f = flags,
1703 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001704 *
1705 * Returns: 0 on success, -ENOBUFS when we run out of space
1706 */
1707
Steven Whitehouse6802e342008-05-21 17:03:22 +01001708static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001710 const struct gfs2_glock_operations *glops = gl->gl_ops;
1711 unsigned long long dtime;
1712 const struct gfs2_holder *gh;
1713 char gflags_buf[32];
1714 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001715
Steven Whitehouse6802e342008-05-21 17:03:22 +01001716 dtime = jiffies - gl->gl_demote_time;
1717 dtime *= 1000000/HZ; /* demote time in uSec */
1718 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1719 dtime = 0;
1720 gfs2_print_dbg(seq, "G: s:%s n:%u/%llu f:%s t:%s d:%s/%llu l:%d a:%d r:%d\n",
1721 state2str(gl->gl_state),
1722 gl->gl_name.ln_type,
1723 (unsigned long long)gl->gl_name.ln_number,
1724 gflags2str(gflags_buf, &gl->gl_flags),
1725 state2str(gl->gl_target),
1726 state2str(gl->gl_demote_state), dtime,
1727 atomic_read(&gl->gl_lvb_count),
1728 atomic_read(&gl->gl_ail_count),
1729 atomic_read(&gl->gl_ref));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001730
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001732 error = dump_holder(seq, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001733 if (error)
1734 goto out;
1735 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001736 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1737 error = glops->go_dump(seq, gl);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001738out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001739 return error;
1740}
1741
Steven Whitehouse6802e342008-05-21 17:03:22 +01001742static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1743{
1744 int ret;
1745 spin_lock(&gl->gl_spin);
1746 ret = __dump_glock(seq, gl);
1747 spin_unlock(&gl->gl_spin);
1748 return ret;
1749}
1750
David Teiglandb3b94fa2006-01-16 16:50:04 +00001751/**
1752 * gfs2_dump_lockstate - print out the current lockstate
1753 * @sdp: the filesystem
1754 * @ub: the buffer to copy the information into
1755 *
1756 * If @ub is NULL, dump the lockstate to the console.
1757 *
1758 */
1759
Adrian Bunk08bc2db2006-04-28 10:59:12 -04001760static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001761{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001762 struct gfs2_glock *gl;
Steven Whitehouseb6397892006-09-12 10:10:01 -04001763 struct hlist_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001764 unsigned int x;
1765 int error = 0;
1766
1767 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001768
Steven Whitehouse087efdd2006-09-09 16:59:11 -04001769 read_lock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001770
Steven Whitehouseb6397892006-09-12 10:10:01 -04001771 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001772 if (gl->gl_sbd != sdp)
1773 continue;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001774
Robert Peterson7c52b162007-03-16 10:26:37 +00001775 error = dump_glock(NULL, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001776 if (error)
1777 break;
1778 }
1779
Steven Whitehouse087efdd2006-09-09 16:59:11 -04001780 read_unlock(gl_lock_addr(x));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001781
1782 if (error)
1783 break;
1784 }
1785
1786
1787 return error;
1788}
1789
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001790
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001791int __init gfs2_glock_init(void)
1792{
1793 unsigned i;
1794 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehouseb6397892006-09-12 10:10:01 -04001795 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001796 }
Steven Whitehouse087efdd2006-09-09 16:59:11 -04001797#ifdef GL_HASH_LOCK_SZ
1798 for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
1799 rwlock_init(&gl_hash_locks[i]);
1800 }
1801#endif
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001802
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001803 glock_workqueue = create_workqueue("glock_workqueue");
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001804 if (IS_ERR(glock_workqueue))
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001805 return PTR_ERR(glock_workqueue);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001806
1807 register_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001808
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001809 return 0;
1810}
1811
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001812void gfs2_glock_exit(void)
1813{
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001814 unregister_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001815 destroy_workqueue(glock_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001816}
1817
Steven Whitehouse6802e342008-05-21 17:03:22 +01001818static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Robert Peterson7c52b162007-03-16 10:26:37 +00001819{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001820 struct gfs2_glock *gl;
1821
Abhijith Dasa947e032007-08-21 09:57:29 -05001822restart:
Robert Peterson7a0079d2007-04-17 11:37:11 -05001823 read_lock(gl_lock_addr(gi->hash));
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001824 gl = gi->gl;
1825 if (gl) {
Abhijith Dasa947e032007-08-21 09:57:29 -05001826 gi->gl = hlist_entry(gl->gl_list.next,
1827 struct gfs2_glock, gl_list);
Steven Whitehousec1e817d2008-07-22 22:58:03 +01001828 } else {
1829 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1830 struct gfs2_glock, gl_list);
Robert Peterson7c52b162007-03-16 10:26:37 +00001831 }
Steven Whitehousec1e817d2008-07-22 22:58:03 +01001832 if (gi->gl)
1833 gfs2_glock_hold(gi->gl);
Robert Peterson7a0079d2007-04-17 11:37:11 -05001834 read_unlock(gl_lock_addr(gi->hash));
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001835 if (gl)
1836 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001837 while (gi->gl == NULL) {
Steven Whitehousec1e817d2008-07-22 22:58:03 +01001838 gi->hash++;
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001839 if (gi->hash >= GFS2_GL_HASH_SIZE)
1840 return 1;
1841 read_lock(gl_lock_addr(gi->hash));
1842 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
1843 struct gfs2_glock, gl_list);
1844 if (gi->gl)
1845 gfs2_glock_hold(gi->gl);
1846 read_unlock(gl_lock_addr(gi->hash));
1847 }
Abhijith Dasa947e032007-08-21 09:57:29 -05001848
1849 if (gi->sdp != gi->gl->gl_sbd)
1850 goto restart;
1851
Robert Peterson7c52b162007-03-16 10:26:37 +00001852 return 0;
1853}
1854
Steven Whitehouse6802e342008-05-21 17:03:22 +01001855static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi)
Robert Peterson7c52b162007-03-16 10:26:37 +00001856{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001857 if (gi->gl)
1858 gfs2_glock_put(gi->gl);
Abhijith Dasa947e032007-08-21 09:57:29 -05001859 gi->gl = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001860}
1861
Steven Whitehouse6802e342008-05-21 17:03:22 +01001862static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Robert Peterson7c52b162007-03-16 10:26:37 +00001863{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001864 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001865 loff_t n = *pos;
1866
Steven Whitehouse6802e342008-05-21 17:03:22 +01001867 gi->hash = 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00001868
Steven Whitehouse6802e342008-05-21 17:03:22 +01001869 do {
Robert Peterson7c52b162007-03-16 10:26:37 +00001870 if (gfs2_glock_iter_next(gi)) {
1871 gfs2_glock_iter_free(gi);
1872 return NULL;
1873 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001874 } while (n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001875
Steven Whitehouse6802e342008-05-21 17:03:22 +01001876 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001877}
1878
Steven Whitehouse6802e342008-05-21 17:03:22 +01001879static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001880 loff_t *pos)
1881{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001882 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001883
1884 (*pos)++;
1885
1886 if (gfs2_glock_iter_next(gi)) {
1887 gfs2_glock_iter_free(gi);
1888 return NULL;
1889 }
1890
Steven Whitehouse6802e342008-05-21 17:03:22 +01001891 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001892}
1893
Steven Whitehouse6802e342008-05-21 17:03:22 +01001894static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001895{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001896 struct gfs2_glock_iter *gi = seq->private;
1897 gfs2_glock_iter_free(gi);
Robert Peterson7c52b162007-03-16 10:26:37 +00001898}
1899
Steven Whitehouse6802e342008-05-21 17:03:22 +01001900static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001901{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001902 return dump_glock(seq, iter_ptr);
Robert Peterson7c52b162007-03-16 10:26:37 +00001903}
1904
Denis Cheng4ef29002007-07-31 18:31:11 +08001905static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001906 .start = gfs2_glock_seq_start,
1907 .next = gfs2_glock_seq_next,
1908 .stop = gfs2_glock_seq_stop,
1909 .show = gfs2_glock_seq_show,
1910};
1911
1912static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1913{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001914 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1915 sizeof(struct gfs2_glock_iter));
1916 if (ret == 0) {
1917 struct seq_file *seq = file->private_data;
1918 struct gfs2_glock_iter *gi = seq->private;
1919 gi->sdp = inode->i_private;
1920 }
1921 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00001922}
1923
1924static const struct file_operations gfs2_debug_fops = {
1925 .owner = THIS_MODULE,
1926 .open = gfs2_debugfs_open,
1927 .read = seq_read,
1928 .llseek = seq_lseek,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001929 .release = seq_release_private,
Robert Peterson7c52b162007-03-16 10:26:37 +00001930};
1931
1932int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1933{
Robert Peterson5f882092007-04-18 11:41:11 -05001934 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1935 if (!sdp->debugfs_dir)
1936 return -ENOMEM;
1937 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1938 S_IFREG | S_IRUGO,
1939 sdp->debugfs_dir, sdp,
1940 &gfs2_debug_fops);
1941 if (!sdp->debugfs_dentry_glocks)
Robert Peterson7c52b162007-03-16 10:26:37 +00001942 return -ENOMEM;
1943
1944 return 0;
1945}
1946
1947void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1948{
Robert Peterson5f882092007-04-18 11:41:11 -05001949 if (sdp && sdp->debugfs_dir) {
1950 if (sdp->debugfs_dentry_glocks) {
1951 debugfs_remove(sdp->debugfs_dentry_glocks);
1952 sdp->debugfs_dentry_glocks = NULL;
1953 }
1954 debugfs_remove(sdp->debugfs_dir);
1955 sdp->debugfs_dir = NULL;
1956 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001957}
1958
1959int gfs2_register_debugfs(void)
1960{
1961 gfs2_root = debugfs_create_dir("gfs2", NULL);
1962 return gfs2_root ? 0 : -ENOMEM;
1963}
1964
1965void gfs2_unregister_debugfs(void)
1966{
1967 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05001968 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001969}