blob: eed4b6855614019b90bb514f4f28dfe38b8c6a0d [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000013#include <linux/buffer_head.h>
14#include <linux/delay.h>
15#include <linux/sort.h>
16#include <linux/jhash.h>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050017#include <linux/kallsyms.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include <linux/gfs2_ondisk.h>
Steven Whitehouse24264432006-09-11 21:40:30 -040019#include <linux/list.h>
Steven Whitehousefee852e2007-01-17 15:33:23 +000020#include <linux/wait.h>
akpm@linux-foundation.org95d97b72007-03-05 23:10:39 -080021#include <linux/module.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include <asm/uaccess.h>
Robert Peterson7c52b162007-03-16 10:26:37 +000023#include <linux/seq_file.h>
24#include <linux/debugfs.h>
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +010025#include <linux/kthread.h>
26#include <linux/freezer.h>
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050027#include <linux/workqueue.h>
28#include <linux/jiffies.h>
Steven Whitehousebc015cb2011-01-19 09:30:01 +000029#include <linux/rcupdate.h>
30#include <linux/rculist_bl.h>
31#include <linux/bit_spinlock.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"
Steven Whitehouse63997772009-06-12 08:49:20 +010044#define CREATE_TRACE_POINTS
45#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000046
Steven Whitehouse6802e342008-05-21 17:03:22 +010047struct gfs2_glock_iter {
48 int hash; /* hash bucket index */
49 struct gfs2_sbd *sdp; /* incore superblock */
50 struct gfs2_glock *gl; /* current glock struct */
51 char string[512]; /* scratch space */
Robert Peterson7c52b162007-03-16 10:26:37 +000052};
53
David Teiglandb3b94fa2006-01-16 16:50:04 +000054typedef void (*glock_examiner) (struct gfs2_glock * gl);
55
Steven Whitehouse6802e342008-05-21 17:03:22 +010056static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
57#define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0)
58static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050059
Robert Peterson7c52b162007-03-16 10:26:37 +000060static struct dentry *gfs2_root;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050061static struct workqueue_struct *glock_workqueue;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -050062struct workqueue_struct *gfs2_delete_workqueue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +000063static LIST_HEAD(lru_list);
64static atomic_t lru_count = ATOMIC_INIT(0);
Julia Lawalleb8374e2008-12-25 15:35:27 +010065static DEFINE_SPINLOCK(lru_lock);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040066
Steven Whitehouseb6397892006-09-12 10:10:01 -040067#define GFS2_GL_HASH_SHIFT 15
Steven Whitehouse087efdd2006-09-09 16:59:11 -040068#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
69#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
70
Steven Whitehousebc015cb2011-01-19 09:30:01 +000071static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
Robert Peterson04b933f2007-03-23 17:05:15 -050072static struct dentry *gfs2_root;
Steven Whitehouse087efdd2006-09-09 16:59:11 -040073
David Teiglandb3b94fa2006-01-16 16:50:04 +000074/**
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 * gl_hash() - Turn glock number into hash bucket number
76 * @lock: The glock number
77 *
78 * Returns: The number of the corresponding hash bucket
79 */
80
Steven Whitehouseb8547852006-09-07 13:12:27 -040081static unsigned int gl_hash(const struct gfs2_sbd *sdp,
82 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +000083{
84 unsigned int h;
85
Steven Whitehousecd915492006-09-04 12:49:07 -040086 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -040088 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 h &= GFS2_GL_HASH_MASK;
90
91 return h;
92}
93
Steven Whitehousebc015cb2011-01-19 09:30:01 +000094static inline void spin_lock_bucket(unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +000095{
Steven Whitehousebc015cb2011-01-19 09:30:01 +000096 struct hlist_bl_head *bl = &gl_hash_table[hash];
97 bit_spin_lock(0, (unsigned long *)bl);
98}
David Teiglandb3b94fa2006-01-16 16:50:04 +000099
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000100static inline void spin_unlock_bucket(unsigned int hash)
101{
102 struct hlist_bl_head *bl = &gl_hash_table[hash];
103 __bit_spin_unlock(0, (unsigned long *)bl);
104}
105
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000106static void gfs2_glock_dealloc(struct rcu_head *rcu)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000107{
108 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000109
110 if (gl->gl_ops->go_flags & GLOF_ASPACE)
111 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
112 else
113 kmem_cache_free(gfs2_glock_cachep, gl);
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000114}
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000115
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000116void gfs2_glock_free(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117{
118 struct gfs2_sbd *sdp = gl->gl_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000120 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000121 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
122 wake_up(&sdp->sd_glock_wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123}
124
125/**
126 * gfs2_glock_hold() - increment reference count on glock
127 * @gl: The glock to hold
128 *
129 */
130
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500131void gfs2_glock_hold(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132{
Steven Whitehoused8348de2009-02-05 10:12:38 +0000133 GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400134 atomic_inc(&gl->gl_ref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135}
136
137/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500138 * demote_ok - Check to see if it's ok to unlock a glock
139 * @gl: the glock
140 *
141 * Returns: 1 if it's ok
142 */
143
144static int demote_ok(const struct gfs2_glock *gl)
145{
146 const struct gfs2_glock_operations *glops = gl->gl_ops;
147
148 if (gl->gl_state == LM_ST_UNLOCKED)
149 return 0;
Steven Whitehousef42ab082011-04-14 16:50:31 +0100150 if (!list_empty(&gl->gl_holders))
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500151 return 0;
152 if (glops->go_demote_ok)
153 return glops->go_demote_ok(gl);
154 return 1;
155}
156
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000157
Steven Whitehouse29687a22011-03-30 16:33:25 +0100158void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
159{
160 spin_lock(&lru_lock);
161
162 if (!list_empty(&gl->gl_lru))
163 list_del_init(&gl->gl_lru);
164 else
165 atomic_inc(&lru_count);
166
167 list_add_tail(&gl->gl_lru, &lru_list);
Steven Whitehouse627c10b2011-04-14 14:09:52 +0100168 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100169 spin_unlock(&lru_lock);
170}
171
Steven Whitehousef42ab082011-04-14 16:50:31 +0100172static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
173{
174 spin_lock(&lru_lock);
175 if (!list_empty(&gl->gl_lru)) {
176 list_del_init(&gl->gl_lru);
177 atomic_dec(&lru_count);
178 clear_bit(GLF_LRU, &gl->gl_flags);
179 }
180 spin_unlock(&lru_lock);
181}
182
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500183/**
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000184 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000185 * @gl: the glock
186 *
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000187 * If the glock is demotable, then we add it (or move it) to the end
188 * of the glock LRU list.
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000189 */
190
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000191static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000192{
Steven Whitehouse29687a22011-03-30 16:33:25 +0100193 if (demote_ok(gl))
194 gfs2_glock_add_to_lru(gl);
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000195}
196
197/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500198 * gfs2_glock_put_nolock() - Decrement reference count on glock
199 * @gl: The glock to put
200 *
201 * This function should only be used if the caller has its own reference
202 * to the glock, in addition to the one it is dropping.
203 */
204
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500205void gfs2_glock_put_nolock(struct gfs2_glock *gl)
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500206{
207 if (atomic_dec_and_test(&gl->gl_ref))
208 GLOCK_BUG_ON(gl, 1);
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500209}
210
211/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 * gfs2_glock_put() - Decrement reference count on glock
213 * @gl: The glock to put
214 *
215 */
216
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000217void gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218{
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000219 struct gfs2_sbd *sdp = gl->gl_sbd;
220 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000222 if (atomic_dec_and_test(&gl->gl_ref)) {
223 spin_lock_bucket(gl->gl_hash);
224 hlist_bl_del_rcu(&gl->gl_list);
225 spin_unlock_bucket(gl->gl_hash);
Steven Whitehousef42ab082011-04-14 16:50:31 +0100226 gfs2_glock_remove_from_lru(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100227 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000228 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
229 trace_gfs2_glock_put(gl);
230 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232}
233
234/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 * search_bucket() - Find struct gfs2_glock by lock number
236 * @bucket: the bucket to search
237 * @name: The lock name
238 *
239 * Returns: NULL, or the struct gfs2_glock with the requested number
240 */
241
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400242static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400243 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400244 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245{
246 struct gfs2_glock *gl;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000247 struct hlist_bl_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000249 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 if (!lm_name_equal(&gl->gl_name, name))
251 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400252 if (gl->gl_sbd != sdp)
253 continue;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000254 if (atomic_inc_not_zero(&gl->gl_ref))
255 return gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 }
257
258 return NULL;
259}
260
261/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100262 * may_grant - check if its ok to grant a new lock
263 * @gl: The glock
264 * @gh: The lock request which we wish to grant
265 *
266 * Returns: true if its ok to grant the lock
267 */
268
269static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500270{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100271 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
272 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
273 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
274 return 0;
275 if (gl->gl_state == gh->gh_state)
276 return 1;
277 if (gh->gh_flags & GL_EXACT)
278 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100279 if (gl->gl_state == LM_ST_EXCLUSIVE) {
280 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
281 return 1;
282 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
283 return 1;
284 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100285 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
286 return 1;
287 return 0;
288}
289
290static void gfs2_holder_wake(struct gfs2_holder *gh)
291{
292 clear_bit(HIF_WAIT, &gh->gh_iflags);
293 smp_mb__after_clear_bit();
294 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
295}
296
297/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100298 * do_error - Something unexpected has happened during a lock request
299 *
300 */
301
302static inline void do_error(struct gfs2_glock *gl, const int ret)
303{
304 struct gfs2_holder *gh, *tmp;
305
306 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
307 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
308 continue;
309 if (ret & LM_OUT_ERROR)
310 gh->gh_error = -EIO;
311 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
312 gh->gh_error = GLR_TRYFAILED;
313 else
314 continue;
315 list_del_init(&gh->gh_list);
316 trace_gfs2_glock_queue(gh, 0);
317 gfs2_holder_wake(gh);
318 }
319}
320
321/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100322 * do_promote - promote as many requests as possible on the current queue
323 * @gl: The glock
324 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000325 * Returns: 1 if there is a blocked holder at the head of the list, or 2
326 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100327 */
328
329static int do_promote(struct gfs2_glock *gl)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700330__releases(&gl->gl_spin)
331__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100332{
333 const struct gfs2_glock_operations *glops = gl->gl_ops;
334 struct gfs2_holder *gh, *tmp;
335 int ret;
336
337restart:
338 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
339 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
340 continue;
341 if (may_grant(gl, gh)) {
342 if (gh->gh_list.prev == &gl->gl_holders &&
343 glops->go_lock) {
344 spin_unlock(&gl->gl_spin);
345 /* FIXME: eliminate this eventually */
346 ret = glops->go_lock(gh);
347 spin_lock(&gl->gl_spin);
348 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000349 if (ret == 1)
350 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100351 gh->gh_error = ret;
352 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100353 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100354 gfs2_holder_wake(gh);
355 goto restart;
356 }
357 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100358 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100359 gfs2_holder_wake(gh);
360 goto restart;
361 }
362 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100363 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100364 gfs2_holder_wake(gh);
365 continue;
366 }
367 if (gh->gh_list.prev == &gl->gl_holders)
368 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100369 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100370 break;
371 }
372 return 0;
373}
374
375/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100376 * find_first_waiter - find the first gh that's waiting for the glock
377 * @gl: the glock
378 */
379
380static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
381{
382 struct gfs2_holder *gh;
383
384 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
385 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
386 return gh;
387 }
388 return NULL;
389}
390
391/**
392 * state_change - record that the glock is now in a different state
393 * @gl: the glock
394 * @new_state the new state
395 *
396 */
397
398static void state_change(struct gfs2_glock *gl, unsigned int new_state)
399{
400 int held1, held2;
401
402 held1 = (gl->gl_state != LM_ST_UNLOCKED);
403 held2 = (new_state != LM_ST_UNLOCKED);
404
405 if (held1 != held2) {
406 if (held2)
407 gfs2_glock_hold(gl);
408 else
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500409 gfs2_glock_put_nolock(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100410 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100411 if (held1 && held2 && list_empty(&gl->gl_holders))
412 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100413
414 gl->gl_state = new_state;
415 gl->gl_tchange = jiffies;
416}
417
418static void gfs2_demote_wake(struct gfs2_glock *gl)
419{
420 gl->gl_demote_state = LM_ST_EXCLUSIVE;
421 clear_bit(GLF_DEMOTE, &gl->gl_flags);
422 smp_mb__after_clear_bit();
423 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
424}
425
426/**
427 * finish_xmote - The DLM has replied to one of our lock requests
428 * @gl: The glock
429 * @ret: The status from the DLM
430 *
431 */
432
433static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
434{
435 const struct gfs2_glock_operations *glops = gl->gl_ops;
436 struct gfs2_holder *gh;
437 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000438 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500439
440 spin_lock(&gl->gl_spin);
Steven Whitehouse63997772009-06-12 08:49:20 +0100441 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100442 state_change(gl, state);
443 gh = find_first_waiter(gl);
444
445 /* Demote to UN request arrived during demote to SH or DF */
446 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
447 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
448 gl->gl_target = LM_ST_UNLOCKED;
449
450 /* Check for state != intended state */
451 if (unlikely(state != gl->gl_target)) {
452 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
453 /* move to back of queue and try next entry */
454 if (ret & LM_OUT_CANCELED) {
455 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
456 list_move_tail(&gh->gh_list, &gl->gl_holders);
457 gh = find_first_waiter(gl);
458 gl->gl_target = gh->gh_state;
459 goto retry;
460 }
461 /* Some error or failed "try lock" - report it */
462 if ((ret & LM_OUT_ERROR) ||
463 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
464 gl->gl_target = gl->gl_state;
465 do_error(gl, ret);
466 goto out;
467 }
468 }
469 switch(state) {
470 /* Unlocked due to conversion deadlock, try again */
471 case LM_ST_UNLOCKED:
472retry:
473 do_xmote(gl, gh, gl->gl_target);
474 break;
475 /* Conversion fails, unlock and try again */
476 case LM_ST_SHARED:
477 case LM_ST_DEFERRED:
478 do_xmote(gl, gh, LM_ST_UNLOCKED);
479 break;
480 default: /* Everything else */
481 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
482 GLOCK_BUG_ON(gl, 1);
483 }
484 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100485 return;
486 }
487
488 /* Fast path - we got what we asked for */
489 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
490 gfs2_demote_wake(gl);
491 if (state != LM_ST_UNLOCKED) {
492 if (glops->go_xmote_bh) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100493 spin_unlock(&gl->gl_spin);
494 rv = glops->go_xmote_bh(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100495 spin_lock(&gl->gl_spin);
496 if (rv) {
497 do_error(gl, rv);
498 goto out;
499 }
500 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000501 rv = do_promote(gl);
502 if (rv == 2)
503 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100504 }
505out:
506 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000507out_locked:
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500508 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500509}
510
Steven Whitehouse6802e342008-05-21 17:03:22 +0100511/**
512 * do_xmote - Calls the DLM to change the state of a lock
513 * @gl: The lock state
514 * @gh: The holder (only for promotes)
515 * @target: The target lock state
516 *
517 */
518
519static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700520__releases(&gl->gl_spin)
521__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100522{
523 const struct gfs2_glock_operations *glops = gl->gl_ops;
524 struct gfs2_sbd *sdp = gl->gl_sbd;
525 unsigned int lck_flags = gh ? gh->gh_flags : 0;
526 int ret;
527
528 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
529 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000530 GLOCK_BUG_ON(gl, gl->gl_state == target);
531 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100532 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
533 glops->go_inval) {
534 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
535 do_error(gl, 0); /* Fail queued try locks */
536 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000537 gl->gl_req = target;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100538 spin_unlock(&gl->gl_spin);
539 if (glops->go_xmote_th)
540 glops->go_xmote_th(gl);
541 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
542 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
543 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
544
545 gfs2_glock_hold(gl);
546 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
547 gl->gl_state == LM_ST_DEFERRED) &&
548 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
549 lck_flags |= LM_FLAG_TRY_1CB;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100550
Steven Whitehouse921169c2010-11-29 12:50:38 +0000551 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
552 /* lock_dlm */
553 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
554 GLOCK_BUG_ON(gl, ret);
555 } else { /* lock_nolock */
556 finish_xmote(gl, target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100557 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
558 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100559 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000560
Steven Whitehouse6802e342008-05-21 17:03:22 +0100561 spin_lock(&gl->gl_spin);
562}
563
564/**
565 * find_first_holder - find the first "holder" gh
566 * @gl: the glock
567 */
568
569static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
570{
571 struct gfs2_holder *gh;
572
573 if (!list_empty(&gl->gl_holders)) {
574 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
575 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
576 return gh;
577 }
578 return NULL;
579}
580
581/**
582 * run_queue - do all outstanding tasks related to a glock
583 * @gl: The glock in question
584 * @nonblock: True if we must not block in run_queue
585 *
586 */
587
588static void run_queue(struct gfs2_glock *gl, const int nonblock)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700589__releases(&gl->gl_spin)
590__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100591{
592 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000593 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100594
595 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
596 return;
597
598 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
599
600 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
601 gl->gl_demote_state != gl->gl_state) {
602 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000603 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100604 if (nonblock)
605 goto out_sched;
606 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100607 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100608 gl->gl_target = gl->gl_demote_state;
609 } else {
610 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
611 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000612 ret = do_promote(gl);
613 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000614 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000615 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100616 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100617 gh = find_first_waiter(gl);
618 gl->gl_target = gh->gh_state;
619 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
620 do_error(gl, 0); /* Fail queued try locks */
621 }
622 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100623out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100624 return;
625
626out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100627 clear_bit(GLF_LOCK, &gl->gl_flags);
628 smp_mb__after_clear_bit();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100629 gfs2_glock_hold(gl);
630 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500631 gfs2_glock_put_nolock(gl);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100632 return;
633
Steven Whitehoused8348de2009-02-05 10:12:38 +0000634out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100635 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100636 smp_mb__after_clear_bit();
637 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100638}
639
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500640static void delete_work_func(struct work_struct *work)
641{
642 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
643 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000644 struct gfs2_inode *ip;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500645 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000646 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500647
Steven Whitehouse044b9412010-11-03 20:01:07 +0000648 ip = gl->gl_object;
649 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
650
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500651 if (ip)
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100652 inode = gfs2_ilookup(sdp->sd_vfs, no_addr, 1);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000653 else
654 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
655 if (inode && !IS_ERR(inode)) {
656 d_prune_aliases(inode);
657 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500658 }
659 gfs2_glock_put(gl);
660}
661
Steven Whitehouse6802e342008-05-21 17:03:22 +0100662static void glock_work_func(struct work_struct *work)
663{
664 unsigned long delay = 0;
665 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000666 int drop_ref = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100667
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000668 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100669 finish_xmote(gl, gl->gl_reply);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000670 drop_ref = 1;
671 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100672 spin_lock(&gl->gl_spin);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100673 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
674 gl->gl_state != LM_ST_UNLOCKED &&
675 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100676 unsigned long holdtime, now = jiffies;
677 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
678 if (time_before(now, holdtime))
679 delay = holdtime - now;
680 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
681 }
682 run_queue(gl, 0);
683 spin_unlock(&gl->gl_spin);
684 if (!delay ||
685 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
686 gfs2_glock_put(gl);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000687 if (drop_ref)
688 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100689}
690
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691/**
692 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
693 * @sdp: The GFS2 superblock
694 * @number: the lock number
695 * @glops: The glock_operations to use
696 * @create: If 0, don't create the glock if it doesn't exist
697 * @glp: the glock is returned here
698 *
699 * This does not lock a glock, just finds/creates structures for one.
700 *
701 * Returns: errno
702 */
703
Steven Whitehousecd915492006-09-04 12:49:07 -0400704int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400705 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 struct gfs2_glock **glp)
707{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000708 struct super_block *s = sdp->sd_vfs;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400709 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400711 unsigned int hash = gl_hash(sdp, &name);
Steven Whitehouse009d8512009-12-08 12:12:13 +0000712 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000713 struct kmem_cache *cachep;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000714
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000715 rcu_read_lock();
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400716 gl = search_bucket(hash, sdp, &name);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000717 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000719 *glp = gl;
720 if (gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721 return 0;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000722 if (!create)
723 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724
Steven Whitehouse009d8512009-12-08 12:12:13 +0000725 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000726 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000727 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000728 cachep = gfs2_glock_cachep;
729 gl = kmem_cache_alloc(cachep, GFP_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000730 if (!gl)
731 return -ENOMEM;
732
Steven Whitehouse8f052282010-01-29 15:21:27 +0000733 atomic_inc(&sdp->sd_glock_disposal);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400734 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 gl->gl_name = name;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400736 atomic_set(&gl->gl_ref, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100738 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500739 gl->gl_demote_state = LM_ST_EXCLUSIVE;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400740 gl->gl_hash = hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000741 gl->gl_ops = glops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000742 snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
743 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
744 gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500745 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400746 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747 gl->gl_sbd = sdp;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500748 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500749 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750
Steven Whitehouse009d8512009-12-08 12:12:13 +0000751 mapping = gfs2_glock2aspace(gl);
752 if (mapping) {
753 mapping->a_ops = &gfs2_meta_aops;
754 mapping->host = s->s_bdev->bd_inode;
755 mapping->flags = 0;
756 mapping_set_gfp_mask(mapping, GFP_NOFS);
757 mapping->assoc_mapping = NULL;
758 mapping->backing_dev_info = s->s_bdi;
759 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 }
761
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000762 spin_lock_bucket(hash);
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400763 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 if (tmp) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000765 spin_unlock_bucket(hash);
766 kmem_cache_free(cachep, gl);
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000767 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 gl = tmp;
769 } else {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000770 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
771 spin_unlock_bucket(hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 }
773
774 *glp = gl;
775
776 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777}
778
779/**
780 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
781 * @gl: the glock
782 * @state: the state we're requesting
783 * @flags: the modifier flags
784 * @gh: the holder structure
785 *
786 */
787
Steven Whitehouse190562b2006-04-20 16:57:23 -0400788void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 struct gfs2_holder *gh)
790{
791 INIT_LIST_HEAD(&gh->gh_list);
792 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500793 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800794 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 gh->gh_state = state;
796 gh->gh_flags = flags;
797 gh->gh_error = 0;
798 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 gfs2_glock_hold(gl);
800}
801
802/**
803 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
804 * @state: the state we're requesting
805 * @flags: the modifier flags
806 * @gh: the holder structure
807 *
808 * Don't mess with the glock.
809 *
810 */
811
Steven Whitehouse190562b2006-04-20 16:57:23 -0400812void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813{
814 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400815 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000816 gh->gh_iflags = 0;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500817 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400818 if (gh->gh_owner_pid)
819 put_pid(gh->gh_owner_pid);
820 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821}
822
823/**
824 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
825 * @gh: the holder structure
826 *
827 */
828
829void gfs2_holder_uninit(struct gfs2_holder *gh)
830{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800831 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 gfs2_glock_put(gh->gh_gl);
833 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500834 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835}
836
Steven Whitehousefe64d512009-05-19 10:01:18 +0100837/**
838 * gfs2_glock_holder_wait
839 * @word: unused
840 *
841 * This function and gfs2_glock_demote_wait both show up in the WCHAN
842 * field. Thus I've separated these otherwise identical functions in
843 * order to be more informative to the user.
844 */
845
846static int gfs2_glock_holder_wait(void *word)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000847{
848 schedule();
849 return 0;
850}
851
Steven Whitehousefe64d512009-05-19 10:01:18 +0100852static int gfs2_glock_demote_wait(void *word)
853{
854 schedule();
855 return 0;
856}
857
Steven Whitehousefee852e2007-01-17 15:33:23 +0000858static void wait_on_holder(struct gfs2_holder *gh)
859{
860 might_sleep();
Steven Whitehousefe64d512009-05-19 10:01:18 +0100861 wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100862}
863
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100864static void wait_on_demote(struct gfs2_glock *gl)
865{
866 might_sleep();
Steven Whitehousefe64d512009-05-19 10:01:18 +0100867 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000868}
869
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000871 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 * @gl: the glock
873 * @state: the state the caller wants us to change to
874 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000875 * There are only two requests that we are going to see in actual
876 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 */
878
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500879static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000880 unsigned long delay)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500882 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
883
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500884 set_bit(bit, &gl->gl_flags);
885 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000886 gl->gl_demote_state = state;
887 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100888 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
889 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100890 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500892 if (gl->gl_ops->go_callback)
893 gl->gl_ops->go_callback(gl);
Steven Whitehouse63997772009-06-12 08:49:20 +0100894 trace_gfs2_demote_rq(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895}
896
897/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100898 * gfs2_glock_wait - wait on a glock acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 * @gh: the glock holder
900 *
901 * Returns: 0 on success
902 */
903
Steven Whitehouse6802e342008-05-21 17:03:22 +0100904int gfs2_glock_wait(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905{
Steven Whitehousefee852e2007-01-17 15:33:23 +0000906 wait_on_holder(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 return gh->gh_error;
908}
909
Steven Whitehouse6802e342008-05-21 17:03:22 +0100910void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000911{
Joe Perches5e690692010-11-09 16:35:20 -0800912 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000913 va_list args;
914
915 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800916
Steven Whitehouse6802e342008-05-21 17:03:22 +0100917 if (seq) {
918 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +0000919 vsprintf(gi->string, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100920 seq_printf(seq, gi->string);
921 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800922 vaf.fmt = fmt;
923 vaf.va = &args;
924
925 printk(KERN_ERR " %pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100926 }
Joe Perches5e690692010-11-09 16:35:20 -0800927
Robert Peterson7c52b162007-03-16 10:26:37 +0000928 va_end(args);
929}
930
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 * add_to_queue - Add a holder to the wait queue (but look for recursion)
933 * @gh: the holder structure to add
934 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100935 * Eventually we should move the recursive locking trap to a
936 * debugging option or something like that. This is the fast
937 * path and needs to have the minimum number of distractions.
938 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939 */
940
Steven Whitehouse6802e342008-05-21 17:03:22 +0100941static inline void add_to_queue(struct gfs2_holder *gh)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700942__releases(&gl->gl_spin)
943__acquires(&gl->gl_spin)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944{
945 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100946 struct gfs2_sbd *sdp = gl->gl_sbd;
947 struct list_head *insert_pt = NULL;
948 struct gfs2_holder *gh2;
949 int try_lock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800951 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000952 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
953 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -0400954
Steven Whitehouse6802e342008-05-21 17:03:22 +0100955 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
956 if (test_bit(GLF_LOCK, &gl->gl_flags))
957 try_lock = 1;
958 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
959 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 }
961
Steven Whitehouse6802e342008-05-21 17:03:22 +0100962 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
963 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
964 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
965 goto trap_recursive;
966 if (try_lock &&
967 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
968 !may_grant(gl, gh)) {
969fail:
970 gh->gh_error = GLR_TRYFAILED;
971 gfs2_holder_wake(gh);
972 return;
973 }
974 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
975 continue;
976 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
977 insert_pt = &gh2->gh_list;
978 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100979 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +0000980 trace_gfs2_glock_queue(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100981 if (likely(insert_pt == NULL)) {
982 list_add_tail(&gh->gh_list, &gl->gl_holders);
983 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
984 goto do_cancel;
985 return;
986 }
987 list_add_tail(&gh->gh_list, insert_pt);
988do_cancel:
989 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
990 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
991 spin_unlock(&gl->gl_spin);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100992 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000993 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100994 spin_lock(&gl->gl_spin);
995 }
996 return;
997
998trap_recursive:
999 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
1000 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1001 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1002 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1003 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
1004 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
1005 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1006 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1007 __dump_glock(NULL, gl);
1008 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009}
1010
1011/**
1012 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1013 * @gh: the holder structure
1014 *
1015 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1016 *
1017 * Returns: 0, GLR_TRYFAILED, or errno on failure
1018 */
1019
1020int gfs2_glock_nq(struct gfs2_holder *gh)
1021{
1022 struct gfs2_glock *gl = gh->gh_gl;
1023 struct gfs2_sbd *sdp = gl->gl_sbd;
1024 int error = 0;
1025
Steven Whitehouse6802e342008-05-21 17:03:22 +01001026 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028
Steven Whitehousef42ab082011-04-14 16:50:31 +01001029 if (test_bit(GLF_LRU, &gl->gl_flags))
1030 gfs2_glock_remove_from_lru(gl);
1031
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 spin_lock(&gl->gl_spin);
1033 add_to_queue(gh);
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001034 if ((LM_FLAG_NOEXP & gh->gh_flags) &&
1035 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1036 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001037 run_queue(gl, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 spin_unlock(&gl->gl_spin);
1039
Steven Whitehouse6802e342008-05-21 17:03:22 +01001040 if (!(gh->gh_flags & GL_ASYNC))
1041 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043 return error;
1044}
1045
1046/**
1047 * gfs2_glock_poll - poll to see if an async request has been completed
1048 * @gh: the holder
1049 *
1050 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1051 */
1052
1053int gfs2_glock_poll(struct gfs2_holder *gh)
1054{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001055 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056}
1057
1058/**
1059 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1060 * @gh: the glock holder
1061 *
1062 */
1063
1064void gfs2_glock_dq(struct gfs2_holder *gh)
1065{
1066 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001067 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001068 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001069 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070
Steven Whitehouse6802e342008-05-21 17:03:22 +01001071 spin_lock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001073 handle_callback(gl, LM_ST_UNLOCKED, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001074
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 list_del_init(&gh->gh_list);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001076 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001077 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001078 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001079 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 glops->go_unlock(gh);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001081 spin_lock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001082 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001083 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001084 if (list_empty(&gl->gl_holders) &&
1085 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1086 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1087 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 }
Steven Whitehousef42ab082011-04-14 16:50:31 +01001089 if (!test_bit(GLF_LFLUSH, &gl->gl_flags))
1090 __gfs2_glock_schedule_for_reclaim(gl);
Steven Whitehouse63997772009-06-12 08:49:20 +01001091 trace_gfs2_glock_queue(gh, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001093 if (likely(fast_path))
1094 return;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001095
1096 gfs2_glock_hold(gl);
1097 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1098 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1099 delay = gl->gl_ops->go_min_hold_time;
1100 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1101 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102}
1103
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001104void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1105{
1106 struct gfs2_glock *gl = gh->gh_gl;
1107 gfs2_glock_dq(gh);
1108 wait_on_demote(gl);
1109}
1110
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1113 * @gh: the holder structure
1114 *
1115 */
1116
1117void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1118{
1119 gfs2_glock_dq(gh);
1120 gfs2_holder_uninit(gh);
1121}
1122
1123/**
1124 * gfs2_glock_nq_num - acquire a glock based on lock number
1125 * @sdp: the filesystem
1126 * @number: the lock number
1127 * @glops: the glock operations for the type of glock
1128 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001129 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 * @gh: the struct gfs2_holder
1131 *
1132 * Returns: errno
1133 */
1134
Steven Whitehousecd915492006-09-04 12:49:07 -04001135int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001136 const struct gfs2_glock_operations *glops,
1137 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138{
1139 struct gfs2_glock *gl;
1140 int error;
1141
1142 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1143 if (!error) {
1144 error = gfs2_glock_nq_init(gl, state, flags, gh);
1145 gfs2_glock_put(gl);
1146 }
1147
1148 return error;
1149}
1150
1151/**
1152 * glock_compare - Compare two struct gfs2_glock structures for sorting
1153 * @arg_a: the first structure
1154 * @arg_b: the second structure
1155 *
1156 */
1157
1158static int glock_compare(const void *arg_a, const void *arg_b)
1159{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001160 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1161 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1162 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1163 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164
1165 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001166 return 1;
1167 if (a->ln_number < b->ln_number)
1168 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001169 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 -04001170 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171}
1172
1173/**
1174 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1175 * @num_gh: the number of structures
1176 * @ghs: an array of struct gfs2_holder structures
1177 *
1178 * Returns: 0 on success (all glocks acquired),
1179 * errno on failure (no glocks acquired)
1180 */
1181
1182static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1183 struct gfs2_holder **p)
1184{
1185 unsigned int x;
1186 int error = 0;
1187
1188 for (x = 0; x < num_gh; x++)
1189 p[x] = &ghs[x];
1190
1191 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1192
1193 for (x = 0; x < num_gh; x++) {
1194 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1195
1196 error = gfs2_glock_nq(p[x]);
1197 if (error) {
1198 while (x--)
1199 gfs2_glock_dq(p[x]);
1200 break;
1201 }
1202 }
1203
1204 return error;
1205}
1206
1207/**
1208 * gfs2_glock_nq_m - acquire multiple glocks
1209 * @num_gh: the number of structures
1210 * @ghs: an array of struct gfs2_holder structures
1211 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212 *
1213 * Returns: 0 on success (all glocks acquired),
1214 * errno on failure (no glocks acquired)
1215 */
1216
1217int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1218{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001219 struct gfs2_holder *tmp[4];
1220 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221 int error = 0;
1222
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001223 switch(num_gh) {
1224 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001226 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1228 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001229 default:
1230 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001231 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001232 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1233 if (!pph)
1234 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 }
1236
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001237 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001239 if (pph != tmp)
1240 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241
1242 return error;
1243}
1244
1245/**
1246 * gfs2_glock_dq_m - release multiple glocks
1247 * @num_gh: the number of structures
1248 * @ghs: an array of struct gfs2_holder structures
1249 *
1250 */
1251
1252void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1253{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001254 while (num_gh--)
1255 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256}
1257
1258/**
1259 * gfs2_glock_dq_uninit_m - release multiple glocks
1260 * @num_gh: the number of structures
1261 * @ghs: an array of struct gfs2_holder structures
1262 *
1263 */
1264
1265void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1266{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001267 while (num_gh--)
1268 gfs2_glock_dq_uninit(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001269}
1270
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001271void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001272{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001273 unsigned long delay = 0;
1274 unsigned long holdtime;
1275 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001277 gfs2_glock_hold(gl);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001278 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001279 if (test_bit(GLF_QUEUED, &gl->gl_flags)) {
1280 if (time_before(now, holdtime))
1281 delay = holdtime - now;
1282 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1283 delay = gl->gl_ops->go_min_hold_time;
1284 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285
Steven Whitehouse6802e342008-05-21 17:03:22 +01001286 spin_lock(&gl->gl_spin);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001287 handle_callback(gl, state, delay);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001288 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001289 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1290 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291}
1292
1293/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001294 * gfs2_should_freeze - Figure out if glock should be frozen
1295 * @gl: The glock in question
1296 *
1297 * Glocks are not frozen if (a) the result of the dlm operation is
1298 * an error, (b) the locking operation was an unlock operation or
1299 * (c) if there is a "noexp" flagged request anywhere in the queue
1300 *
1301 * Returns: 1 if freezing should occur, 0 otherwise
1302 */
1303
1304static int gfs2_should_freeze(const struct gfs2_glock *gl)
1305{
1306 const struct gfs2_holder *gh;
1307
1308 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1309 return 0;
1310 if (gl->gl_target == LM_ST_UNLOCKED)
1311 return 0;
1312
1313 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1314 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1315 continue;
1316 if (LM_FLAG_NOEXP & gh->gh_flags)
1317 return 0;
1318 }
1319
1320 return 1;
1321}
1322
1323/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001324 * gfs2_glock_complete - Callback used by locking
1325 * @gl: Pointer to the glock
1326 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327 *
Steven Whitehouse47a25382010-11-30 15:49:31 +00001328 * The gl_reply field is under the gl_spin lock so that it is ok
1329 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330 */
1331
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001332void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333{
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001334 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001335
Steven Whitehouse47a25382010-11-30 15:49:31 +00001336 spin_lock(&gl->gl_spin);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001337 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001338
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001339 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001340 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001341 set_bit(GLF_FROZEN, &gl->gl_flags);
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001342 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001344 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001345 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001346
1347 spin_unlock(&gl->gl_spin);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001348 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse47a25382010-11-30 15:49:31 +00001349 smp_wmb();
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001350 gfs2_glock_hold(gl);
1351 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1352 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353}
1354
David Teiglandb3b94fa2006-01-16 16:50:04 +00001355
Dave Chinner7f8275d2010-07-19 14:56:17 +10001356static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357{
1358 struct gfs2_glock *gl;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001359 int may_demote;
1360 int nr_skipped = 0;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001361 LIST_HEAD(skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001362
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001363 if (nr == 0)
1364 goto out;
1365
1366 if (!(gfp_mask & __GFP_FS))
1367 return -1;
1368
1369 spin_lock(&lru_lock);
1370 while(nr && !list_empty(&lru_list)) {
1371 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1372 list_del_init(&gl->gl_lru);
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001373 clear_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001374 atomic_dec(&lru_count);
1375
1376 /* Test for being demotable */
1377 if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1378 gfs2_glock_hold(gl);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001379 spin_unlock(&lru_lock);
1380 spin_lock(&gl->gl_spin);
1381 may_demote = demote_ok(gl);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001382 if (may_demote) {
1383 handle_callback(gl, LM_ST_UNLOCKED, 0);
1384 nr--;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001385 }
Steven Whitehouse7e71c552009-09-22 10:56:16 +01001386 clear_bit(GLF_LOCK, &gl->gl_flags);
1387 smp_mb__after_clear_bit();
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001388 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001389 gfs2_glock_put_nolock(gl);
1390 spin_unlock(&gl->gl_spin);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001391 spin_lock(&lru_lock);
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001392 continue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001393 }
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001394 nr_skipped++;
1395 list_add(&gl->gl_lru, &skipped);
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001396 set_bit(GLF_LRU, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001398 list_splice(&skipped, &lru_list);
1399 atomic_add(nr_skipped, &lru_count);
1400 spin_unlock(&lru_lock);
1401out:
1402 return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403}
1404
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001405static struct shrinker glock_shrinker = {
1406 .shrink = gfs2_shrink_glock_memory,
1407 .seeks = DEFAULT_SEEKS,
1408};
1409
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410/**
1411 * examine_bucket - Call a function for glock in a hash bucket
1412 * @examiner: the function
1413 * @sdp: the filesystem
1414 * @bucket: the bucket
1415 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001416 */
1417
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001418static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001419 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001420{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001421 struct gfs2_glock *gl;
1422 struct hlist_bl_head *head = &gl_hash_table[hash];
1423 struct hlist_bl_node *pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001424
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001425 rcu_read_lock();
1426 hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
1427 if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
Steven Whitehouse24264432006-09-11 21:40:30 -04001428 examiner(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001429 }
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001430 rcu_read_unlock();
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001431 cond_resched();
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001432}
1433
1434static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1435{
1436 unsigned x;
1437
1438 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1439 examine_bucket(examiner, sdp, x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440}
1441
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001442
1443/**
1444 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1445 * @gl: The glock to thaw
1446 *
1447 * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
1448 * so this has to result in the ref count being dropped by one.
1449 */
1450
1451static void thaw_glock(struct gfs2_glock *gl)
1452{
1453 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1454 return;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001455 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1456 gfs2_glock_hold(gl);
1457 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1458 gfs2_glock_put(gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001459}
1460
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001462 * clear_glock - look at a glock and see if we can free it from glock cache
1463 * @gl: the glock to look at
1464 *
1465 */
1466
1467static void clear_glock(struct gfs2_glock *gl)
1468{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001469 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470
Steven Whitehouse6802e342008-05-21 17:03:22 +01001471 spin_lock(&gl->gl_spin);
Steven Whitehousec741c452010-09-29 14:20:52 +01001472 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001473 handle_callback(gl, LM_ST_UNLOCKED, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001474 spin_unlock(&gl->gl_spin);
1475 gfs2_glock_hold(gl);
1476 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1477 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001478}
1479
1480/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001481 * gfs2_glock_thaw - Thaw any frozen glocks
1482 * @sdp: The super block
1483 *
1484 */
1485
1486void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1487{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001488 glock_hash_walk(thaw_glock, sdp);
1489}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001490
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001491static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
1492{
1493 int ret;
1494 spin_lock(&gl->gl_spin);
1495 ret = __dump_glock(seq, gl);
1496 spin_unlock(&gl->gl_spin);
1497 return ret;
1498}
1499
1500static void dump_glock_func(struct gfs2_glock *gl)
1501{
1502 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001503}
1504
1505/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001506 * gfs2_gl_hash_clear - Empty out the glock hash table
1507 * @sdp: the filesystem
1508 * @wait: wait until it's all gone
1509 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001510 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001511 */
1512
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001513void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001514{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001515 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001516 flush_workqueue(glock_workqueue);
1517 wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001518 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001519}
1520
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001521void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1522{
1523 struct gfs2_glock *gl = ip->i_gl;
1524 int ret;
1525
1526 ret = gfs2_truncatei_resume(ip);
1527 gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
1528
1529 spin_lock(&gl->gl_spin);
1530 clear_bit(GLF_LOCK, &gl->gl_flags);
1531 run_queue(gl, 1);
1532 spin_unlock(&gl->gl_spin);
1533}
1534
Steven Whitehouse6802e342008-05-21 17:03:22 +01001535static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001536{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001537 switch(state) {
1538 case LM_ST_UNLOCKED:
1539 return "UN";
1540 case LM_ST_SHARED:
1541 return "SH";
1542 case LM_ST_DEFERRED:
1543 return "DF";
1544 case LM_ST_EXCLUSIVE:
1545 return "EX";
1546 }
1547 return "??";
1548}
Robert Peterson04b933f2007-03-23 17:05:15 -05001549
Steven Whitehouse6802e342008-05-21 17:03:22 +01001550static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1551{
1552 char *p = buf;
1553 if (flags & LM_FLAG_TRY)
1554 *p++ = 't';
1555 if (flags & LM_FLAG_TRY_1CB)
1556 *p++ = 'T';
1557 if (flags & LM_FLAG_NOEXP)
1558 *p++ = 'e';
1559 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001560 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001561 if (flags & LM_FLAG_PRIORITY)
1562 *p++ = 'p';
1563 if (flags & GL_ASYNC)
1564 *p++ = 'a';
1565 if (flags & GL_EXACT)
1566 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001567 if (flags & GL_NOCACHE)
1568 *p++ = 'c';
1569 if (test_bit(HIF_HOLDER, &iflags))
1570 *p++ = 'H';
1571 if (test_bit(HIF_WAIT, &iflags))
1572 *p++ = 'W';
1573 if (test_bit(HIF_FIRST, &iflags))
1574 *p++ = 'F';
1575 *p = 0;
1576 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001577}
1578
David Teiglandb3b94fa2006-01-16 16:50:04 +00001579/**
1580 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001581 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001582 * @gh: the glock holder
1583 *
1584 * Returns: 0 on success, -ENOBUFS when we run out of space
1585 */
1586
Steven Whitehouse6802e342008-05-21 17:03:22 +01001587static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001588{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001589 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001590 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001591
Steven Whitehouse6802e342008-05-21 17:03:22 +01001592 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001593 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001594 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1595 state2str(gh->gh_state),
1596 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1597 gh->gh_error,
1598 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1599 gh_owner ? gh_owner->comm : "(ended)",
1600 (void *)gh->gh_ip);
Robert Peterson7c52b162007-03-16 10:26:37 +00001601 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001602}
1603
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001604static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001605{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001606 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001607 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001608
Steven Whitehouse6802e342008-05-21 17:03:22 +01001609 if (test_bit(GLF_LOCK, gflags))
1610 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001611 if (test_bit(GLF_DEMOTE, gflags))
1612 *p++ = 'D';
1613 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1614 *p++ = 'd';
1615 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1616 *p++ = 'p';
1617 if (test_bit(GLF_DIRTY, gflags))
1618 *p++ = 'y';
1619 if (test_bit(GLF_LFLUSH, gflags))
1620 *p++ = 'f';
1621 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1622 *p++ = 'i';
1623 if (test_bit(GLF_REPLY_PENDING, gflags))
1624 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001625 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001626 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001627 if (test_bit(GLF_FROZEN, gflags))
1628 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001629 if (test_bit(GLF_QUEUED, gflags))
1630 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001631 if (test_bit(GLF_LRU, gflags))
1632 *p++ = 'L';
1633 if (gl->gl_object)
1634 *p++ = 'o';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001635 *p = 0;
1636 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001637}
1638
1639/**
Steven Whitehouse6802e342008-05-21 17:03:22 +01001640 * __dump_glock - print information about a glock
1641 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001642 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001643 *
1644 * The file format is as follows:
1645 * One line per object, capital letters are used to indicate objects
1646 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1647 * other objects are indented by a single space and follow the glock to
1648 * which they are related. Fields are indicated by lower case letters
1649 * followed by a colon and the field value, except for strings which are in
1650 * [] so that its possible to see if they are composed of spaces for
1651 * example. The field's are n = number (id of the object), f = flags,
1652 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001653 *
1654 * Returns: 0 on success, -ENOBUFS when we run out of space
1655 */
1656
Steven Whitehouse6802e342008-05-21 17:03:22 +01001657static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001658{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001659 const struct gfs2_glock_operations *glops = gl->gl_ops;
1660 unsigned long long dtime;
1661 const struct gfs2_holder *gh;
1662 char gflags_buf[32];
1663 int error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001664
Steven Whitehouse6802e342008-05-21 17:03:22 +01001665 dtime = jiffies - gl->gl_demote_time;
1666 dtime *= 1000000/HZ; /* demote time in uSec */
1667 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1668 dtime = 0;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001669 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001670 state2str(gl->gl_state),
1671 gl->gl_name.ln_type,
1672 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001673 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001674 state2str(gl->gl_target),
1675 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001676 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001677 atomic_read(&gl->gl_revokes),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001678 atomic_read(&gl->gl_ref));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001681 error = dump_holder(seq, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001682 if (error)
1683 goto out;
1684 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001685 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1686 error = glops->go_dump(seq, gl);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001687out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001688 return error;
1689}
1690
Steven Whitehouse6802e342008-05-21 17:03:22 +01001691
David Teiglandb3b94fa2006-01-16 16:50:04 +00001692
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001693
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001694int __init gfs2_glock_init(void)
1695{
1696 unsigned i;
1697 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001698 INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001699 }
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001700
Steven Whitehoused2115772010-11-03 19:58:53 +00001701 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001702 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001703 if (IS_ERR(glock_workqueue))
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001704 return PTR_ERR(glock_workqueue);
Steven Whitehoused2115772010-11-03 19:58:53 +00001705 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001706 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001707 0);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001708 if (IS_ERR(gfs2_delete_workqueue)) {
1709 destroy_workqueue(glock_workqueue);
1710 return PTR_ERR(gfs2_delete_workqueue);
1711 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001712
1713 register_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001714
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001715 return 0;
1716}
1717
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001718void gfs2_glock_exit(void)
1719{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001720 unregister_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001721 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001722 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001723}
1724
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001725static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
1726{
1727 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
1728 struct gfs2_glock, gl_list);
1729}
1730
1731static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
1732{
Steven Whitehouse7e32d022011-03-15 08:32:14 +00001733 return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001734 struct gfs2_glock, gl_list);
1735}
1736
Steven Whitehouse6802e342008-05-21 17:03:22 +01001737static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Robert Peterson7c52b162007-03-16 10:26:37 +00001738{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001739 struct gfs2_glock *gl;
1740
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001741 do {
1742 gl = gi->gl;
1743 if (gl) {
1744 gi->gl = glock_hash_next(gl);
1745 } else {
1746 gi->gl = glock_hash_chain(gi->hash);
1747 }
1748 while (gi->gl == NULL) {
1749 gi->hash++;
1750 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1751 rcu_read_unlock();
1752 return 1;
1753 }
1754 gi->gl = glock_hash_chain(gi->hash);
1755 }
1756 /* Skip entries for other sb and dead entries */
1757 } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
Abhijith Dasa947e032007-08-21 09:57:29 -05001758
Robert Peterson7c52b162007-03-16 10:26:37 +00001759 return 0;
1760}
1761
Steven Whitehouse6802e342008-05-21 17:03:22 +01001762static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Robert Peterson7c52b162007-03-16 10:26:37 +00001763{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001764 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001765 loff_t n = *pos;
1766
Steven Whitehouse6802e342008-05-21 17:03:22 +01001767 gi->hash = 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001768 rcu_read_lock();
Robert Peterson7c52b162007-03-16 10:26:37 +00001769
Steven Whitehouse6802e342008-05-21 17:03:22 +01001770 do {
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001771 if (gfs2_glock_iter_next(gi))
Robert Peterson7c52b162007-03-16 10:26:37 +00001772 return NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001773 } while (n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001774
Steven Whitehouse6802e342008-05-21 17:03:22 +01001775 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001776}
1777
Steven Whitehouse6802e342008-05-21 17:03:22 +01001778static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001779 loff_t *pos)
1780{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001781 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001782
1783 (*pos)++;
1784
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001785 if (gfs2_glock_iter_next(gi))
Robert Peterson7c52b162007-03-16 10:26:37 +00001786 return NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001787
Steven Whitehouse6802e342008-05-21 17:03:22 +01001788 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001789}
1790
Steven Whitehouse6802e342008-05-21 17:03:22 +01001791static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001792{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001793 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001794
1795 if (gi->gl)
1796 rcu_read_unlock();
1797 gi->gl = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001798}
1799
Steven Whitehouse6802e342008-05-21 17:03:22 +01001800static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001801{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001802 return dump_glock(seq, iter_ptr);
Robert Peterson7c52b162007-03-16 10:26:37 +00001803}
1804
Denis Cheng4ef29002007-07-31 18:31:11 +08001805static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001806 .start = gfs2_glock_seq_start,
1807 .next = gfs2_glock_seq_next,
1808 .stop = gfs2_glock_seq_stop,
1809 .show = gfs2_glock_seq_show,
1810};
1811
1812static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1813{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001814 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1815 sizeof(struct gfs2_glock_iter));
1816 if (ret == 0) {
1817 struct seq_file *seq = file->private_data;
1818 struct gfs2_glock_iter *gi = seq->private;
1819 gi->sdp = inode->i_private;
1820 }
1821 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00001822}
1823
1824static const struct file_operations gfs2_debug_fops = {
1825 .owner = THIS_MODULE,
1826 .open = gfs2_debugfs_open,
1827 .read = seq_read,
1828 .llseek = seq_lseek,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001829 .release = seq_release_private,
Robert Peterson7c52b162007-03-16 10:26:37 +00001830};
1831
1832int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1833{
Robert Peterson5f882092007-04-18 11:41:11 -05001834 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1835 if (!sdp->debugfs_dir)
1836 return -ENOMEM;
1837 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1838 S_IFREG | S_IRUGO,
1839 sdp->debugfs_dir, sdp,
1840 &gfs2_debug_fops);
1841 if (!sdp->debugfs_dentry_glocks)
Robert Peterson7c52b162007-03-16 10:26:37 +00001842 return -ENOMEM;
1843
1844 return 0;
1845}
1846
1847void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1848{
Robert Peterson5f882092007-04-18 11:41:11 -05001849 if (sdp && sdp->debugfs_dir) {
1850 if (sdp->debugfs_dentry_glocks) {
1851 debugfs_remove(sdp->debugfs_dentry_glocks);
1852 sdp->debugfs_dentry_glocks = NULL;
1853 }
1854 debugfs_remove(sdp->debugfs_dir);
1855 sdp->debugfs_dir = NULL;
1856 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001857}
1858
1859int gfs2_register_debugfs(void)
1860{
1861 gfs2_root = debugfs_create_dir("gfs2", NULL);
1862 return gfs2_root ? 0 : -ENOMEM;
1863}
1864
1865void gfs2_unregister_debugfs(void)
1866{
1867 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05001868 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001869}