blob: 0c6c690901406436d2fd5e0e1c5fc2d83cac7931 [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
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000148 /* assert_spin_locked(&gl->gl_spin); */
149
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500150 if (gl->gl_state == LM_ST_UNLOCKED)
151 return 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000152 if (test_bit(GLF_LFLUSH, &gl->gl_flags))
153 return 0;
154 if ((gl->gl_name.ln_type != LM_TYPE_INODE) &&
155 !list_empty(&gl->gl_holders))
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500156 return 0;
157 if (glops->go_demote_ok)
158 return glops->go_demote_ok(gl);
159 return 1;
160}
161
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000162
Steven Whitehouse29687a22011-03-30 16:33:25 +0100163void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
164{
165 spin_lock(&lru_lock);
166
167 if (!list_empty(&gl->gl_lru))
168 list_del_init(&gl->gl_lru);
169 else
170 atomic_inc(&lru_count);
171
172 list_add_tail(&gl->gl_lru, &lru_list);
Steven Whitehouse627c10b2011-04-14 14:09:52 +0100173 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100174 spin_unlock(&lru_lock);
175}
176
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500177/**
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000178 * __gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000179 * @gl: the glock
180 *
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000181 * If the glock is demotable, then we add it (or move it) to the end
182 * of the glock LRU list.
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000183 */
184
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000185static void __gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000186{
Steven Whitehouse29687a22011-03-30 16:33:25 +0100187 if (demote_ok(gl))
188 gfs2_glock_add_to_lru(gl);
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000189}
190
191/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500192 * gfs2_glock_put_nolock() - Decrement reference count on glock
193 * @gl: The glock to put
194 *
195 * This function should only be used if the caller has its own reference
196 * to the glock, in addition to the one it is dropping.
197 */
198
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500199void gfs2_glock_put_nolock(struct gfs2_glock *gl)
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500200{
201 if (atomic_dec_and_test(&gl->gl_ref))
202 GLOCK_BUG_ON(gl, 1);
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500203}
204
205/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 * gfs2_glock_put() - Decrement reference count on glock
207 * @gl: The glock to put
208 *
209 */
210
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000211void gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212{
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000213 struct gfs2_sbd *sdp = gl->gl_sbd;
214 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000216 if (atomic_dec_and_test(&gl->gl_ref)) {
217 spin_lock_bucket(gl->gl_hash);
218 hlist_bl_del_rcu(&gl->gl_list);
219 spin_unlock_bucket(gl->gl_hash);
220 spin_lock(&lru_lock);
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000221 if (!list_empty(&gl->gl_lru)) {
222 list_del_init(&gl->gl_lru);
223 atomic_dec(&lru_count);
224 }
225 spin_unlock(&lru_lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100226 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000227 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
228 trace_gfs2_glock_put(gl);
229 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231}
232
233/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 * search_bucket() - Find struct gfs2_glock by lock number
235 * @bucket: the bucket to search
236 * @name: The lock name
237 *
238 * Returns: NULL, or the struct gfs2_glock with the requested number
239 */
240
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400241static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400242 const struct gfs2_sbd *sdp,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400243 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244{
245 struct gfs2_glock *gl;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000246 struct hlist_bl_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000248 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 if (!lm_name_equal(&gl->gl_name, name))
250 continue;
Steven Whitehouse899be4d2006-08-30 12:50:28 -0400251 if (gl->gl_sbd != sdp)
252 continue;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000253 if (atomic_inc_not_zero(&gl->gl_ref))
254 return gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255 }
256
257 return NULL;
258}
259
260/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100261 * may_grant - check if its ok to grant a new lock
262 * @gl: The glock
263 * @gh: The lock request which we wish to grant
264 *
265 * Returns: true if its ok to grant the lock
266 */
267
268static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500269{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100270 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
271 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
272 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
273 return 0;
274 if (gl->gl_state == gh->gh_state)
275 return 1;
276 if (gh->gh_flags & GL_EXACT)
277 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100278 if (gl->gl_state == LM_ST_EXCLUSIVE) {
279 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
280 return 1;
281 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
282 return 1;
283 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100284 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
285 return 1;
286 return 0;
287}
288
289static void gfs2_holder_wake(struct gfs2_holder *gh)
290{
291 clear_bit(HIF_WAIT, &gh->gh_iflags);
292 smp_mb__after_clear_bit();
293 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
294}
295
296/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100297 * do_error - Something unexpected has happened during a lock request
298 *
299 */
300
301static inline void do_error(struct gfs2_glock *gl, const int ret)
302{
303 struct gfs2_holder *gh, *tmp;
304
305 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
306 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
307 continue;
308 if (ret & LM_OUT_ERROR)
309 gh->gh_error = -EIO;
310 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
311 gh->gh_error = GLR_TRYFAILED;
312 else
313 continue;
314 list_del_init(&gh->gh_list);
315 trace_gfs2_glock_queue(gh, 0);
316 gfs2_holder_wake(gh);
317 }
318}
319
320/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100321 * do_promote - promote as many requests as possible on the current queue
322 * @gl: The glock
323 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000324 * Returns: 1 if there is a blocked holder at the head of the list, or 2
325 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100326 */
327
328static int do_promote(struct gfs2_glock *gl)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700329__releases(&gl->gl_spin)
330__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100331{
332 const struct gfs2_glock_operations *glops = gl->gl_ops;
333 struct gfs2_holder *gh, *tmp;
334 int ret;
335
336restart:
337 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
338 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
339 continue;
340 if (may_grant(gl, gh)) {
341 if (gh->gh_list.prev == &gl->gl_holders &&
342 glops->go_lock) {
343 spin_unlock(&gl->gl_spin);
344 /* FIXME: eliminate this eventually */
345 ret = glops->go_lock(gh);
346 spin_lock(&gl->gl_spin);
347 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000348 if (ret == 1)
349 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100350 gh->gh_error = ret;
351 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100352 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100353 gfs2_holder_wake(gh);
354 goto restart;
355 }
356 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100357 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100358 gfs2_holder_wake(gh);
359 goto restart;
360 }
361 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100362 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100363 gfs2_holder_wake(gh);
364 continue;
365 }
366 if (gh->gh_list.prev == &gl->gl_holders)
367 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100368 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100369 break;
370 }
371 return 0;
372}
373
374/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100375 * find_first_waiter - find the first gh that's waiting for the glock
376 * @gl: the glock
377 */
378
379static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
380{
381 struct gfs2_holder *gh;
382
383 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
384 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
385 return gh;
386 }
387 return NULL;
388}
389
390/**
391 * state_change - record that the glock is now in a different state
392 * @gl: the glock
393 * @new_state the new state
394 *
395 */
396
397static void state_change(struct gfs2_glock *gl, unsigned int new_state)
398{
399 int held1, held2;
400
401 held1 = (gl->gl_state != LM_ST_UNLOCKED);
402 held2 = (new_state != LM_ST_UNLOCKED);
403
404 if (held1 != held2) {
405 if (held2)
406 gfs2_glock_hold(gl);
407 else
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500408 gfs2_glock_put_nolock(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100409 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100410 if (held1 && held2 && list_empty(&gl->gl_holders))
411 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100412
413 gl->gl_state = new_state;
414 gl->gl_tchange = jiffies;
415}
416
417static void gfs2_demote_wake(struct gfs2_glock *gl)
418{
419 gl->gl_demote_state = LM_ST_EXCLUSIVE;
420 clear_bit(GLF_DEMOTE, &gl->gl_flags);
421 smp_mb__after_clear_bit();
422 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
423}
424
425/**
426 * finish_xmote - The DLM has replied to one of our lock requests
427 * @gl: The glock
428 * @ret: The status from the DLM
429 *
430 */
431
432static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
433{
434 const struct gfs2_glock_operations *glops = gl->gl_ops;
435 struct gfs2_holder *gh;
436 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000437 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500438
439 spin_lock(&gl->gl_spin);
Steven Whitehouse63997772009-06-12 08:49:20 +0100440 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100441 state_change(gl, state);
442 gh = find_first_waiter(gl);
443
444 /* Demote to UN request arrived during demote to SH or DF */
445 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
446 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
447 gl->gl_target = LM_ST_UNLOCKED;
448
449 /* Check for state != intended state */
450 if (unlikely(state != gl->gl_target)) {
451 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
452 /* move to back of queue and try next entry */
453 if (ret & LM_OUT_CANCELED) {
454 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
455 list_move_tail(&gh->gh_list, &gl->gl_holders);
456 gh = find_first_waiter(gl);
457 gl->gl_target = gh->gh_state;
458 goto retry;
459 }
460 /* Some error or failed "try lock" - report it */
461 if ((ret & LM_OUT_ERROR) ||
462 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
463 gl->gl_target = gl->gl_state;
464 do_error(gl, ret);
465 goto out;
466 }
467 }
468 switch(state) {
469 /* Unlocked due to conversion deadlock, try again */
470 case LM_ST_UNLOCKED:
471retry:
472 do_xmote(gl, gh, gl->gl_target);
473 break;
474 /* Conversion fails, unlock and try again */
475 case LM_ST_SHARED:
476 case LM_ST_DEFERRED:
477 do_xmote(gl, gh, LM_ST_UNLOCKED);
478 break;
479 default: /* Everything else */
480 printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state);
481 GLOCK_BUG_ON(gl, 1);
482 }
483 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100484 return;
485 }
486
487 /* Fast path - we got what we asked for */
488 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
489 gfs2_demote_wake(gl);
490 if (state != LM_ST_UNLOCKED) {
491 if (glops->go_xmote_bh) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100492 spin_unlock(&gl->gl_spin);
493 rv = glops->go_xmote_bh(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100494 spin_lock(&gl->gl_spin);
495 if (rv) {
496 do_error(gl, rv);
497 goto out;
498 }
499 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000500 rv = do_promote(gl);
501 if (rv == 2)
502 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100503 }
504out:
505 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000506out_locked:
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500507 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500508}
509
Steven Whitehouse6802e342008-05-21 17:03:22 +0100510/**
511 * do_xmote - Calls the DLM to change the state of a lock
512 * @gl: The lock state
513 * @gh: The holder (only for promotes)
514 * @target: The target lock state
515 *
516 */
517
518static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700519__releases(&gl->gl_spin)
520__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100521{
522 const struct gfs2_glock_operations *glops = gl->gl_ops;
523 struct gfs2_sbd *sdp = gl->gl_sbd;
524 unsigned int lck_flags = gh ? gh->gh_flags : 0;
525 int ret;
526
527 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
528 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000529 GLOCK_BUG_ON(gl, gl->gl_state == target);
530 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100531 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
532 glops->go_inval) {
533 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
534 do_error(gl, 0); /* Fail queued try locks */
535 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000536 gl->gl_req = target;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100537 spin_unlock(&gl->gl_spin);
538 if (glops->go_xmote_th)
539 glops->go_xmote_th(gl);
540 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
541 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
542 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
543
544 gfs2_glock_hold(gl);
545 if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED ||
546 gl->gl_state == LM_ST_DEFERRED) &&
547 !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
548 lck_flags |= LM_FLAG_TRY_1CB;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100549
Steven Whitehouse921169c2010-11-29 12:50:38 +0000550 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
551 /* lock_dlm */
552 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
553 GLOCK_BUG_ON(gl, ret);
554 } else { /* lock_nolock */
555 finish_xmote(gl, target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100556 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
557 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100558 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000559
Steven Whitehouse6802e342008-05-21 17:03:22 +0100560 spin_lock(&gl->gl_spin);
561}
562
563/**
564 * find_first_holder - find the first "holder" gh
565 * @gl: the glock
566 */
567
568static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
569{
570 struct gfs2_holder *gh;
571
572 if (!list_empty(&gl->gl_holders)) {
573 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
574 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
575 return gh;
576 }
577 return NULL;
578}
579
580/**
581 * run_queue - do all outstanding tasks related to a glock
582 * @gl: The glock in question
583 * @nonblock: True if we must not block in run_queue
584 *
585 */
586
587static void run_queue(struct gfs2_glock *gl, const int nonblock)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700588__releases(&gl->gl_spin)
589__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100590{
591 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000592 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100593
594 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
595 return;
596
597 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
598
599 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
600 gl->gl_demote_state != gl->gl_state) {
601 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000602 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100603 if (nonblock)
604 goto out_sched;
605 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100606 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100607 gl->gl_target = gl->gl_demote_state;
608 } else {
609 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
610 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000611 ret = do_promote(gl);
612 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000613 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000614 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100615 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100616 gh = find_first_waiter(gl);
617 gl->gl_target = gh->gh_state;
618 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
619 do_error(gl, 0); /* Fail queued try locks */
620 }
621 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100622out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100623 return;
624
625out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100626 clear_bit(GLF_LOCK, &gl->gl_flags);
627 smp_mb__after_clear_bit();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100628 gfs2_glock_hold(gl);
629 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500630 gfs2_glock_put_nolock(gl);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100631 return;
632
Steven Whitehoused8348de2009-02-05 10:12:38 +0000633out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100634 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100635 smp_mb__after_clear_bit();
636 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100637}
638
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500639static void delete_work_func(struct work_struct *work)
640{
641 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
642 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000643 struct gfs2_inode *ip;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500644 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000645 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500646
Steven Whitehouse044b9412010-11-03 20:01:07 +0000647 ip = gl->gl_object;
648 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
649
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500650 if (ip)
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500651 inode = gfs2_ilookup(sdp->sd_vfs, no_addr);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000652 else
653 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
654 if (inode && !IS_ERR(inode)) {
655 d_prune_aliases(inode);
656 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500657 }
658 gfs2_glock_put(gl);
659}
660
Steven Whitehouse6802e342008-05-21 17:03:22 +0100661static void glock_work_func(struct work_struct *work)
662{
663 unsigned long delay = 0;
664 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000665 int drop_ref = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100666
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000667 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100668 finish_xmote(gl, gl->gl_reply);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000669 drop_ref = 1;
670 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100671 spin_lock(&gl->gl_spin);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100672 if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
673 gl->gl_state != LM_ST_UNLOCKED &&
674 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100675 unsigned long holdtime, now = jiffies;
676 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
677 if (time_before(now, holdtime))
678 delay = holdtime - now;
679 set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags);
680 }
681 run_queue(gl, 0);
682 spin_unlock(&gl->gl_spin);
683 if (!delay ||
684 queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
685 gfs2_glock_put(gl);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000686 if (drop_ref)
687 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100688}
689
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690/**
691 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
692 * @sdp: The GFS2 superblock
693 * @number: the lock number
694 * @glops: The glock_operations to use
695 * @create: If 0, don't create the glock if it doesn't exist
696 * @glp: the glock is returned here
697 *
698 * This does not lock a glock, just finds/creates structures for one.
699 *
700 * Returns: errno
701 */
702
Steven Whitehousecd915492006-09-04 12:49:07 -0400703int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400704 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 struct gfs2_glock **glp)
706{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000707 struct super_block *s = sdp->sd_vfs;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400708 struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400710 unsigned int hash = gl_hash(sdp, &name);
Steven Whitehouse009d8512009-12-08 12:12:13 +0000711 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000712 struct kmem_cache *cachep;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000714 rcu_read_lock();
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400715 gl = search_bucket(hash, sdp, &name);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000716 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000718 *glp = gl;
719 if (gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 return 0;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000721 if (!create)
722 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723
Steven Whitehouse009d8512009-12-08 12:12:13 +0000724 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000725 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000726 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000727 cachep = gfs2_glock_cachep;
728 gl = kmem_cache_alloc(cachep, GFP_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 if (!gl)
730 return -ENOMEM;
731
Steven Whitehouse8f052282010-01-29 15:21:27 +0000732 atomic_inc(&sdp->sd_glock_disposal);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400733 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 gl->gl_name = name;
Steven Whitehouse16feb9f2006-09-13 10:43:37 -0400735 atomic_set(&gl->gl_ref, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100737 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500738 gl->gl_demote_state = LM_ST_EXCLUSIVE;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400739 gl->gl_hash = hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 gl->gl_ops = glops;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000741 snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
742 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
743 gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500744 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400745 gl->gl_object = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 gl->gl_sbd = sdp;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500747 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500748 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749
Steven Whitehouse009d8512009-12-08 12:12:13 +0000750 mapping = gfs2_glock2aspace(gl);
751 if (mapping) {
752 mapping->a_ops = &gfs2_meta_aops;
753 mapping->host = s->s_bdev->bd_inode;
754 mapping->flags = 0;
755 mapping_set_gfp_mask(mapping, GFP_NOFS);
756 mapping->assoc_mapping = NULL;
757 mapping->backing_dev_info = s->s_bdi;
758 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 }
760
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000761 spin_lock_bucket(hash);
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400762 tmp = search_bucket(hash, sdp, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 if (tmp) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000764 spin_unlock_bucket(hash);
765 kmem_cache_free(cachep, gl);
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000766 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 gl = tmp;
768 } else {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000769 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
770 spin_unlock_bucket(hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771 }
772
773 *glp = gl;
774
775 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776}
777
778/**
779 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
780 * @gl: the glock
781 * @state: the state we're requesting
782 * @flags: the modifier flags
783 * @gh: the holder structure
784 *
785 */
786
Steven Whitehouse190562b2006-04-20 16:57:23 -0400787void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 struct gfs2_holder *gh)
789{
790 INIT_LIST_HEAD(&gh->gh_list);
791 gh->gh_gl = gl;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500792 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800793 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794 gh->gh_state = state;
795 gh->gh_flags = flags;
796 gh->gh_error = 0;
797 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 gfs2_glock_hold(gl);
799}
800
801/**
802 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
803 * @state: the state we're requesting
804 * @flags: the modifier flags
805 * @gh: the holder structure
806 *
807 * Don't mess with the glock.
808 *
809 */
810
Steven Whitehouse190562b2006-04-20 16:57:23 -0400811void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812{
813 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400814 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000815 gh->gh_iflags = 0;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500816 gh->gh_ip = (unsigned long)__builtin_return_address(0);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400817 if (gh->gh_owner_pid)
818 put_pid(gh->gh_owner_pid);
819 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000820}
821
822/**
823 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
824 * @gh: the holder structure
825 *
826 */
827
828void gfs2_holder_uninit(struct gfs2_holder *gh)
829{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800830 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 gfs2_glock_put(gh->gh_gl);
832 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500833 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834}
835
Steven Whitehousefe64d512009-05-19 10:01:18 +0100836/**
837 * gfs2_glock_holder_wait
838 * @word: unused
839 *
840 * This function and gfs2_glock_demote_wait both show up in the WCHAN
841 * field. Thus I've separated these otherwise identical functions in
842 * order to be more informative to the user.
843 */
844
845static int gfs2_glock_holder_wait(void *word)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000846{
847 schedule();
848 return 0;
849}
850
Steven Whitehousefe64d512009-05-19 10:01:18 +0100851static int gfs2_glock_demote_wait(void *word)
852{
853 schedule();
854 return 0;
855}
856
Steven Whitehousefee852e2007-01-17 15:33:23 +0000857static void wait_on_holder(struct gfs2_holder *gh)
858{
859 might_sleep();
Steven Whitehousefe64d512009-05-19 10:01:18 +0100860 wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100861}
862
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100863static void wait_on_demote(struct gfs2_glock *gl)
864{
865 might_sleep();
Steven Whitehousefe64d512009-05-19 10:01:18 +0100866 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000867}
868
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000870 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 * @gl: the glock
872 * @state: the state the caller wants us to change to
873 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000874 * There are only two requests that we are going to see in actual
875 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 */
877
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500878static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000879 unsigned long delay)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500881 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
882
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500883 set_bit(bit, &gl->gl_flags);
884 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000885 gl->gl_demote_state = state;
886 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100887 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
888 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100889 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500891 if (gl->gl_ops->go_callback)
892 gl->gl_ops->go_callback(gl);
Steven Whitehouse63997772009-06-12 08:49:20 +0100893 trace_gfs2_demote_rq(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894}
895
896/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100897 * gfs2_glock_wait - wait on a glock acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 * @gh: the glock holder
899 *
900 * Returns: 0 on success
901 */
902
Steven Whitehouse6802e342008-05-21 17:03:22 +0100903int gfs2_glock_wait(struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904{
Steven Whitehousefee852e2007-01-17 15:33:23 +0000905 wait_on_holder(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906 return gh->gh_error;
907}
908
Steven Whitehouse6802e342008-05-21 17:03:22 +0100909void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000910{
Joe Perches5e690692010-11-09 16:35:20 -0800911 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000912 va_list args;
913
914 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800915
Steven Whitehouse6802e342008-05-21 17:03:22 +0100916 if (seq) {
917 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +0000918 vsprintf(gi->string, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100919 seq_printf(seq, gi->string);
920 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800921 vaf.fmt = fmt;
922 vaf.va = &args;
923
924 printk(KERN_ERR " %pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100925 }
Joe Perches5e690692010-11-09 16:35:20 -0800926
Robert Peterson7c52b162007-03-16 10:26:37 +0000927 va_end(args);
928}
929
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 * add_to_queue - Add a holder to the wait queue (but look for recursion)
932 * @gh: the holder structure to add
933 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100934 * Eventually we should move the recursive locking trap to a
935 * debugging option or something like that. This is the fast
936 * path and needs to have the minimum number of distractions.
937 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 */
939
Steven Whitehouse6802e342008-05-21 17:03:22 +0100940static inline void add_to_queue(struct gfs2_holder *gh)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700941__releases(&gl->gl_spin)
942__acquires(&gl->gl_spin)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943{
944 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100945 struct gfs2_sbd *sdp = gl->gl_sbd;
946 struct list_head *insert_pt = NULL;
947 struct gfs2_holder *gh2;
948 int try_lock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800950 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000951 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
952 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -0400953
Steven Whitehouse6802e342008-05-21 17:03:22 +0100954 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
955 if (test_bit(GLF_LOCK, &gl->gl_flags))
956 try_lock = 1;
957 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
958 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 }
960
Steven Whitehouse6802e342008-05-21 17:03:22 +0100961 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
962 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
963 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
964 goto trap_recursive;
965 if (try_lock &&
966 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) &&
967 !may_grant(gl, gh)) {
968fail:
969 gh->gh_error = GLR_TRYFAILED;
970 gfs2_holder_wake(gh);
971 return;
972 }
973 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
974 continue;
975 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
976 insert_pt = &gh2->gh_list;
977 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100978 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +0000979 trace_gfs2_glock_queue(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100980 if (likely(insert_pt == NULL)) {
981 list_add_tail(&gh->gh_list, &gl->gl_holders);
982 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
983 goto do_cancel;
984 return;
985 }
986 list_add_tail(&gh->gh_list, insert_pt);
987do_cancel:
988 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
989 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
990 spin_unlock(&gl->gl_spin);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100991 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000992 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100993 spin_lock(&gl->gl_spin);
994 }
995 return;
996
997trap_recursive:
998 print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip);
999 printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1000 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1001 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1002 print_symbol(KERN_ERR "new: %s\n", gh->gh_ip);
1003 printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid));
1004 printk(KERN_ERR "lock type: %d req lock state : %d\n",
1005 gh->gh_gl->gl_name.ln_type, gh->gh_state);
1006 __dump_glock(NULL, gl);
1007 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008}
1009
1010/**
1011 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1012 * @gh: the holder structure
1013 *
1014 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1015 *
1016 * Returns: 0, GLR_TRYFAILED, or errno on failure
1017 */
1018
1019int gfs2_glock_nq(struct gfs2_holder *gh)
1020{
1021 struct gfs2_glock *gl = gh->gh_gl;
1022 struct gfs2_sbd *sdp = gl->gl_sbd;
1023 int error = 0;
1024
Steven Whitehouse6802e342008-05-21 17:03:22 +01001025 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 spin_lock(&gl->gl_spin);
1029 add_to_queue(gh);
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001030 if ((LM_FLAG_NOEXP & gh->gh_flags) &&
1031 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1032 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001033 run_queue(gl, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 spin_unlock(&gl->gl_spin);
1035
Steven Whitehouse6802e342008-05-21 17:03:22 +01001036 if (!(gh->gh_flags & GL_ASYNC))
1037 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 return error;
1040}
1041
1042/**
1043 * gfs2_glock_poll - poll to see if an async request has been completed
1044 * @gh: the holder
1045 *
1046 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1047 */
1048
1049int gfs2_glock_poll(struct gfs2_holder *gh)
1050{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001051 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052}
1053
1054/**
1055 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1056 * @gh: the glock holder
1057 *
1058 */
1059
1060void gfs2_glock_dq(struct gfs2_holder *gh)
1061{
1062 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001063 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001064 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001065 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066
Steven Whitehouse6802e342008-05-21 17:03:22 +01001067 spin_lock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001069 handle_callback(gl, LM_ST_UNLOCKED, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 list_del_init(&gh->gh_list);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001072 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001073 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001074 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001075 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076 glops->go_unlock(gh);
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001077 spin_lock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001078 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001079 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001080 if (list_empty(&gl->gl_holders) &&
1081 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1082 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1083 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 }
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001085 __gfs2_glock_schedule_for_reclaim(gl);
Steven Whitehouse63997772009-06-12 08:49:20 +01001086 trace_gfs2_glock_queue(gh, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001088 if (likely(fast_path))
1089 return;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001090
1091 gfs2_glock_hold(gl);
1092 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1093 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1094 delay = gl->gl_ops->go_min_hold_time;
1095 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1096 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097}
1098
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001099void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1100{
1101 struct gfs2_glock *gl = gh->gh_gl;
1102 gfs2_glock_dq(gh);
1103 wait_on_demote(gl);
1104}
1105
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1108 * @gh: the holder structure
1109 *
1110 */
1111
1112void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1113{
1114 gfs2_glock_dq(gh);
1115 gfs2_holder_uninit(gh);
1116}
1117
1118/**
1119 * gfs2_glock_nq_num - acquire a glock based on lock number
1120 * @sdp: the filesystem
1121 * @number: the lock number
1122 * @glops: the glock operations for the type of glock
1123 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001124 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 * @gh: the struct gfs2_holder
1126 *
1127 * Returns: errno
1128 */
1129
Steven Whitehousecd915492006-09-04 12:49:07 -04001130int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001131 const struct gfs2_glock_operations *glops,
1132 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133{
1134 struct gfs2_glock *gl;
1135 int error;
1136
1137 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1138 if (!error) {
1139 error = gfs2_glock_nq_init(gl, state, flags, gh);
1140 gfs2_glock_put(gl);
1141 }
1142
1143 return error;
1144}
1145
1146/**
1147 * glock_compare - Compare two struct gfs2_glock structures for sorting
1148 * @arg_a: the first structure
1149 * @arg_b: the second structure
1150 *
1151 */
1152
1153static int glock_compare(const void *arg_a, const void *arg_b)
1154{
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001155 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1156 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1157 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1158 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001159
1160 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001161 return 1;
1162 if (a->ln_number < b->ln_number)
1163 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001164 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001165 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166}
1167
1168/**
1169 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1170 * @num_gh: the number of structures
1171 * @ghs: an array of struct gfs2_holder structures
1172 *
1173 * Returns: 0 on success (all glocks acquired),
1174 * errno on failure (no glocks acquired)
1175 */
1176
1177static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1178 struct gfs2_holder **p)
1179{
1180 unsigned int x;
1181 int error = 0;
1182
1183 for (x = 0; x < num_gh; x++)
1184 p[x] = &ghs[x];
1185
1186 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1187
1188 for (x = 0; x < num_gh; x++) {
1189 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1190
1191 error = gfs2_glock_nq(p[x]);
1192 if (error) {
1193 while (x--)
1194 gfs2_glock_dq(p[x]);
1195 break;
1196 }
1197 }
1198
1199 return error;
1200}
1201
1202/**
1203 * gfs2_glock_nq_m - acquire multiple glocks
1204 * @num_gh: the number of structures
1205 * @ghs: an array of struct gfs2_holder structures
1206 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207 *
1208 * Returns: 0 on success (all glocks acquired),
1209 * errno on failure (no glocks acquired)
1210 */
1211
1212int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1213{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001214 struct gfs2_holder *tmp[4];
1215 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216 int error = 0;
1217
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001218 switch(num_gh) {
1219 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001221 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1223 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001224 default:
1225 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001226 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001227 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1228 if (!pph)
1229 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001230 }
1231
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001232 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001233
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001234 if (pph != tmp)
1235 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236
1237 return error;
1238}
1239
1240/**
1241 * gfs2_glock_dq_m - release multiple glocks
1242 * @num_gh: the number of structures
1243 * @ghs: an array of struct gfs2_holder structures
1244 *
1245 */
1246
1247void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1248{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001249 while (num_gh--)
1250 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001251}
1252
1253/**
1254 * gfs2_glock_dq_uninit_m - release multiple glocks
1255 * @num_gh: the number of structures
1256 * @ghs: an array of struct gfs2_holder structures
1257 *
1258 */
1259
1260void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1261{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001262 while (num_gh--)
1263 gfs2_glock_dq_uninit(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264}
1265
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001266void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001267{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001268 unsigned long delay = 0;
1269 unsigned long holdtime;
1270 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001272 gfs2_glock_hold(gl);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001273 holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001274 if (test_bit(GLF_QUEUED, &gl->gl_flags)) {
1275 if (time_before(now, holdtime))
1276 delay = holdtime - now;
1277 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
1278 delay = gl->gl_ops->go_min_hold_time;
1279 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280
Steven Whitehouse6802e342008-05-21 17:03:22 +01001281 spin_lock(&gl->gl_spin);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001282 handle_callback(gl, state, delay);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001283 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001284 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1285 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286}
1287
1288/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001289 * gfs2_should_freeze - Figure out if glock should be frozen
1290 * @gl: The glock in question
1291 *
1292 * Glocks are not frozen if (a) the result of the dlm operation is
1293 * an error, (b) the locking operation was an unlock operation or
1294 * (c) if there is a "noexp" flagged request anywhere in the queue
1295 *
1296 * Returns: 1 if freezing should occur, 0 otherwise
1297 */
1298
1299static int gfs2_should_freeze(const struct gfs2_glock *gl)
1300{
1301 const struct gfs2_holder *gh;
1302
1303 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1304 return 0;
1305 if (gl->gl_target == LM_ST_UNLOCKED)
1306 return 0;
1307
1308 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1309 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1310 continue;
1311 if (LM_FLAG_NOEXP & gh->gh_flags)
1312 return 0;
1313 }
1314
1315 return 1;
1316}
1317
1318/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001319 * gfs2_glock_complete - Callback used by locking
1320 * @gl: Pointer to the glock
1321 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322 *
Steven Whitehouse47a25382010-11-30 15:49:31 +00001323 * The gl_reply field is under the gl_spin lock so that it is ok
1324 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 */
1326
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001327void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001328{
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001329 struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001330
Steven Whitehouse47a25382010-11-30 15:49:31 +00001331 spin_lock(&gl->gl_spin);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001332 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001333
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001334 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001335 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001336 set_bit(GLF_FROZEN, &gl->gl_flags);
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001337 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001338 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001339 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001341
1342 spin_unlock(&gl->gl_spin);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001343 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse47a25382010-11-30 15:49:31 +00001344 smp_wmb();
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001345 gfs2_glock_hold(gl);
1346 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1347 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348}
1349
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350
Dave Chinner7f8275d2010-07-19 14:56:17 +10001351static int gfs2_shrink_glock_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352{
1353 struct gfs2_glock *gl;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001354 int may_demote;
1355 int nr_skipped = 0;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001356 LIST_HEAD(skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001358 if (nr == 0)
1359 goto out;
1360
1361 if (!(gfp_mask & __GFP_FS))
1362 return -1;
1363
1364 spin_lock(&lru_lock);
1365 while(nr && !list_empty(&lru_list)) {
1366 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
1367 list_del_init(&gl->gl_lru);
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001368 clear_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001369 atomic_dec(&lru_count);
1370
1371 /* Test for being demotable */
1372 if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1373 gfs2_glock_hold(gl);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001374 spin_unlock(&lru_lock);
1375 spin_lock(&gl->gl_spin);
1376 may_demote = demote_ok(gl);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001377 if (may_demote) {
1378 handle_callback(gl, LM_ST_UNLOCKED, 0);
1379 nr--;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001380 }
Steven Whitehouse7e71c552009-09-22 10:56:16 +01001381 clear_bit(GLF_LOCK, &gl->gl_flags);
1382 smp_mb__after_clear_bit();
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001383 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001384 gfs2_glock_put_nolock(gl);
1385 spin_unlock(&gl->gl_spin);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001386 spin_lock(&lru_lock);
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001387 continue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001388 }
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001389 nr_skipped++;
1390 list_add(&gl->gl_lru, &skipped);
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001391 set_bit(GLF_LRU, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001393 list_splice(&skipped, &lru_list);
1394 atomic_add(nr_skipped, &lru_count);
1395 spin_unlock(&lru_lock);
1396out:
1397 return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398}
1399
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001400static struct shrinker glock_shrinker = {
1401 .shrink = gfs2_shrink_glock_memory,
1402 .seeks = DEFAULT_SEEKS,
1403};
1404
David Teiglandb3b94fa2006-01-16 16:50:04 +00001405/**
1406 * examine_bucket - Call a function for glock in a hash bucket
1407 * @examiner: the function
1408 * @sdp: the filesystem
1409 * @bucket: the bucket
1410 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001411 */
1412
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001413static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001414 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001415{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001416 struct gfs2_glock *gl;
1417 struct hlist_bl_head *head = &gl_hash_table[hash];
1418 struct hlist_bl_node *pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001420 rcu_read_lock();
1421 hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
1422 if ((gl->gl_sbd == sdp) && atomic_read(&gl->gl_ref))
Steven Whitehouse24264432006-09-11 21:40:30 -04001423 examiner(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001424 }
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001425 rcu_read_unlock();
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001426 cond_resched();
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001427}
1428
1429static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1430{
1431 unsigned x;
1432
1433 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1434 examine_bucket(examiner, sdp, x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001435}
1436
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001437
1438/**
1439 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1440 * @gl: The glock to thaw
1441 *
1442 * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
1443 * so this has to result in the ref count being dropped by one.
1444 */
1445
1446static void thaw_glock(struct gfs2_glock *gl)
1447{
1448 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
1449 return;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001450 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1451 gfs2_glock_hold(gl);
1452 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1453 gfs2_glock_put(gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001454}
1455
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001457 * clear_glock - look at a glock and see if we can free it from glock cache
1458 * @gl: the glock to look at
1459 *
1460 */
1461
1462static void clear_glock(struct gfs2_glock *gl)
1463{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001464 spin_lock(&lru_lock);
1465 if (!list_empty(&gl->gl_lru)) {
1466 list_del_init(&gl->gl_lru);
1467 atomic_dec(&lru_count);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001468 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001469 spin_unlock(&lru_lock);
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;
Bob Peterson48189722010-02-23 12:20:00 -05001669 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%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),
1677 atomic_read(&gl->gl_ref));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001678
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001680 error = dump_holder(seq, gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001681 if (error)
1682 goto out;
1683 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001684 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
1685 error = glops->go_dump(seq, gl);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001686out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001687 return error;
1688}
1689
Steven Whitehouse6802e342008-05-21 17:03:22 +01001690
David Teiglandb3b94fa2006-01-16 16:50:04 +00001691
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001692
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001693int __init gfs2_glock_init(void)
1694{
1695 unsigned i;
1696 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001697 INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001698 }
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001699
Steven Whitehoused2115772010-11-03 19:58:53 +00001700 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001701 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001702 if (IS_ERR(glock_workqueue))
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001703 return PTR_ERR(glock_workqueue);
Steven Whitehoused2115772010-11-03 19:58:53 +00001704 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001705 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001706 0);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001707 if (IS_ERR(gfs2_delete_workqueue)) {
1708 destroy_workqueue(glock_workqueue);
1709 return PTR_ERR(gfs2_delete_workqueue);
1710 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001711
1712 register_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001713
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001714 return 0;
1715}
1716
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001717void gfs2_glock_exit(void)
1718{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001719 unregister_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001720 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001721 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001722}
1723
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001724static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
1725{
1726 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
1727 struct gfs2_glock, gl_list);
1728}
1729
1730static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
1731{
Steven Whitehouse7e32d022011-03-15 08:32:14 +00001732 return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001733 struct gfs2_glock, gl_list);
1734}
1735
Steven Whitehouse6802e342008-05-21 17:03:22 +01001736static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Robert Peterson7c52b162007-03-16 10:26:37 +00001737{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001738 struct gfs2_glock *gl;
1739
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001740 do {
1741 gl = gi->gl;
1742 if (gl) {
1743 gi->gl = glock_hash_next(gl);
1744 } else {
1745 gi->gl = glock_hash_chain(gi->hash);
1746 }
1747 while (gi->gl == NULL) {
1748 gi->hash++;
1749 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1750 rcu_read_unlock();
1751 return 1;
1752 }
1753 gi->gl = glock_hash_chain(gi->hash);
1754 }
1755 /* Skip entries for other sb and dead entries */
1756 } while (gi->sdp != gi->gl->gl_sbd || atomic_read(&gi->gl->gl_ref) == 0);
Abhijith Dasa947e032007-08-21 09:57:29 -05001757
Robert Peterson7c52b162007-03-16 10:26:37 +00001758 return 0;
1759}
1760
Steven Whitehouse6802e342008-05-21 17:03:22 +01001761static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Robert Peterson7c52b162007-03-16 10:26:37 +00001762{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001763 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001764 loff_t n = *pos;
1765
Steven Whitehouse6802e342008-05-21 17:03:22 +01001766 gi->hash = 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001767 rcu_read_lock();
Robert Peterson7c52b162007-03-16 10:26:37 +00001768
Steven Whitehouse6802e342008-05-21 17:03:22 +01001769 do {
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001770 if (gfs2_glock_iter_next(gi))
Robert Peterson7c52b162007-03-16 10:26:37 +00001771 return NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001772 } while (n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001773
Steven Whitehouse6802e342008-05-21 17:03:22 +01001774 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001775}
1776
Steven Whitehouse6802e342008-05-21 17:03:22 +01001777static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001778 loff_t *pos)
1779{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001780 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001781
1782 (*pos)++;
1783
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001784 if (gfs2_glock_iter_next(gi))
Robert Peterson7c52b162007-03-16 10:26:37 +00001785 return NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001786
Steven Whitehouse6802e342008-05-21 17:03:22 +01001787 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001788}
1789
Steven Whitehouse6802e342008-05-21 17:03:22 +01001790static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001791{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001792 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001793
1794 if (gi->gl)
1795 rcu_read_unlock();
1796 gi->gl = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001797}
1798
Steven Whitehouse6802e342008-05-21 17:03:22 +01001799static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001800{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001801 return dump_glock(seq, iter_ptr);
Robert Peterson7c52b162007-03-16 10:26:37 +00001802}
1803
Denis Cheng4ef29002007-07-31 18:31:11 +08001804static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001805 .start = gfs2_glock_seq_start,
1806 .next = gfs2_glock_seq_next,
1807 .stop = gfs2_glock_seq_stop,
1808 .show = gfs2_glock_seq_show,
1809};
1810
1811static int gfs2_debugfs_open(struct inode *inode, struct file *file)
1812{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001813 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1814 sizeof(struct gfs2_glock_iter));
1815 if (ret == 0) {
1816 struct seq_file *seq = file->private_data;
1817 struct gfs2_glock_iter *gi = seq->private;
1818 gi->sdp = inode->i_private;
1819 }
1820 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00001821}
1822
1823static const struct file_operations gfs2_debug_fops = {
1824 .owner = THIS_MODULE,
1825 .open = gfs2_debugfs_open,
1826 .read = seq_read,
1827 .llseek = seq_lseek,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001828 .release = seq_release_private,
Robert Peterson7c52b162007-03-16 10:26:37 +00001829};
1830
1831int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
1832{
Robert Peterson5f882092007-04-18 11:41:11 -05001833 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
1834 if (!sdp->debugfs_dir)
1835 return -ENOMEM;
1836 sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
1837 S_IFREG | S_IRUGO,
1838 sdp->debugfs_dir, sdp,
1839 &gfs2_debug_fops);
1840 if (!sdp->debugfs_dentry_glocks)
Robert Peterson7c52b162007-03-16 10:26:37 +00001841 return -ENOMEM;
1842
1843 return 0;
1844}
1845
1846void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
1847{
Robert Peterson5f882092007-04-18 11:41:11 -05001848 if (sdp && sdp->debugfs_dir) {
1849 if (sdp->debugfs_dentry_glocks) {
1850 debugfs_remove(sdp->debugfs_dentry_glocks);
1851 sdp->debugfs_dentry_glocks = NULL;
1852 }
1853 debugfs_remove(sdp->debugfs_dir);
1854 sdp->debugfs_dir = NULL;
1855 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001856}
1857
1858int gfs2_register_debugfs(void)
1859{
1860 gfs2_root = debugfs_create_dir("gfs2", NULL);
1861 return gfs2_root ? 0 : -ENOMEM;
1862}
1863
1864void gfs2_unregister_debugfs(void)
1865{
1866 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05001867 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001868}