blob: 13cba6e3ef6ab0039d25270f6a8b1702a32a8ecf [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
Joe Perchesd77d1b52014-03-06 12:10:45 -080010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
David Teiglandb3b94fa2006-01-16 16:50:04 +000012#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000015#include <linux/buffer_head.h>
16#include <linux/delay.h>
17#include <linux/sort.h>
18#include <linux/jhash.h>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050019#include <linux/kallsyms.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
Steven Whitehouse24264432006-09-11 21:40:30 -040021#include <linux/list.h>
Steven Whitehousefee852e2007-01-17 15:33:23 +000022#include <linux/wait.h>
akpm@linux-foundation.org95d97b72007-03-05 23:10:39 -080023#include <linux/module.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include <asm/uaccess.h>
Robert Peterson7c52b162007-03-16 10:26:37 +000025#include <linux/seq_file.h>
26#include <linux/debugfs.h>
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +010027#include <linux/kthread.h>
28#include <linux/freezer.h>
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050029#include <linux/workqueue.h>
30#include <linux/jiffies.h>
Steven Whitehousebc015cb2011-01-19 09:30:01 +000031#include <linux/rcupdate.h>
32#include <linux/rculist_bl.h>
33#include <linux/bit_spinlock.h>
Steven Whitehousea2457692012-01-20 10:38:36 +000034#include <linux/percpu.h>
Steven Whitehouse4506a512013-02-01 20:36:03 +000035#include <linux/list_sort.h>
Steven Whitehousee66cf162013-10-15 15:18:08 +010036#include <linux/lockref.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000037
38#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050039#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000040#include "glock.h"
41#include "glops.h"
42#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000043#include "lops.h"
44#include "meta_io.h"
45#include "quota.h"
46#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050047#include "util.h"
Steven Whitehouse813e0c42008-11-18 13:38:48 +000048#include "bmap.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010049#define CREATE_TRACE_POINTS
50#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000051
Steven Whitehouse6802e342008-05-21 17:03:22 +010052struct gfs2_glock_iter {
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +010053 int hash; /* hash bucket index */
54 unsigned nhash; /* Index within current bucket */
55 struct gfs2_sbd *sdp; /* incore superblock */
56 struct gfs2_glock *gl; /* current glock struct */
57 loff_t last_pos; /* last position */
Robert Peterson7c52b162007-03-16 10:26:37 +000058};
59
David Teiglandb3b94fa2006-01-16 16:50:04 +000060typedef void (*glock_examiner) (struct gfs2_glock * gl);
61
Steven Whitehouse6802e342008-05-21 17:03:22 +010062static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050063
Robert Peterson7c52b162007-03-16 10:26:37 +000064static struct dentry *gfs2_root;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050065static struct workqueue_struct *glock_workqueue;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -050066struct workqueue_struct *gfs2_delete_workqueue;
Steven Whitehouse97cc1022008-11-20 13:39:47 +000067static LIST_HEAD(lru_list);
68static atomic_t lru_count = ATOMIC_INIT(0);
Julia Lawalleb8374e2008-12-25 15:35:27 +010069static DEFINE_SPINLOCK(lru_lock);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040070
Steven Whitehouseb6397892006-09-12 10:10:01 -040071#define GFS2_GL_HASH_SHIFT 15
Steven Whitehouse087efdd2006-09-09 16:59:11 -040072#define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT)
73#define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1)
74
Steven Whitehousebc015cb2011-01-19 09:30:01 +000075static struct hlist_bl_head gl_hash_table[GFS2_GL_HASH_SIZE];
Robert Peterson04b933f2007-03-23 17:05:15 -050076static struct dentry *gfs2_root;
Steven Whitehouse087efdd2006-09-09 16:59:11 -040077
David Teiglandb3b94fa2006-01-16 16:50:04 +000078/**
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 * gl_hash() - Turn glock number into hash bucket number
80 * @lock: The glock number
81 *
82 * Returns: The number of the corresponding hash bucket
83 */
84
Steven Whitehouseb8547852006-09-07 13:12:27 -040085static unsigned int gl_hash(const struct gfs2_sbd *sdp,
86 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +000087{
88 unsigned int h;
89
Steven Whitehousecd915492006-09-04 12:49:07 -040090 h = jhash(&name->ln_number, sizeof(u64), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 h = jhash(&name->ln_type, sizeof(unsigned int), h);
Steven Whitehouseb8547852006-09-07 13:12:27 -040092 h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
David Teiglandb3b94fa2006-01-16 16:50:04 +000093 h &= GFS2_GL_HASH_MASK;
94
95 return h;
96}
97
Steven Whitehousebc015cb2011-01-19 09:30:01 +000098static inline void spin_lock_bucket(unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +000099{
Christoph Hellwig1879fd62011-04-25 14:01:36 -0400100 hlist_bl_lock(&gl_hash_table[hash]);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000101}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000103static inline void spin_unlock_bucket(unsigned int hash)
104{
Christoph Hellwig1879fd62011-04-25 14:01:36 -0400105 hlist_bl_unlock(&gl_hash_table[hash]);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000106}
107
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000108static void gfs2_glock_dealloc(struct rcu_head *rcu)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000109{
110 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000111
David Teiglanddba2d702012-11-14 13:46:53 -0500112 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000113 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
David Teiglanddba2d702012-11-14 13:46:53 -0500114 } else {
David Teigland4e2f8842012-11-14 13:47:37 -0500115 kfree(gl->gl_lksb.sb_lvbptr);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000116 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglanddba2d702012-11-14 13:46:53 -0500117 }
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000118}
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000119
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000120void gfs2_glock_free(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121{
Bob Peterson15562c42015-03-16 11:52:05 -0500122 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000124 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000125 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
126 wake_up(&sdp->sd_glock_wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127}
128
129/**
130 * gfs2_glock_hold() - increment reference count on glock
131 * @gl: The glock to hold
132 *
133 */
134
Steven Whitehousee66cf162013-10-15 15:18:08 +0100135static void gfs2_glock_hold(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136{
Steven Whitehousee66cf162013-10-15 15:18:08 +0100137 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
138 lockref_get(&gl->gl_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139}
140
141/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500142 * demote_ok - Check to see if it's ok to unlock a glock
143 * @gl: the glock
144 *
145 * Returns: 1 if it's ok
146 */
147
148static int demote_ok(const struct gfs2_glock *gl)
149{
150 const struct gfs2_glock_operations *glops = gl->gl_ops;
151
152 if (gl->gl_state == LM_ST_UNLOCKED)
153 return 0;
Steven Whitehousef42ab082011-04-14 16:50:31 +0100154 if (!list_empty(&gl->gl_holders))
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500155 return 0;
156 if (glops->go_demote_ok)
157 return glops->go_demote_ok(gl);
158 return 1;
159}
160
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000161
Steven Whitehouse29687a22011-03-30 16:33:25 +0100162void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
163{
164 spin_lock(&lru_lock);
165
166 if (!list_empty(&gl->gl_lru))
167 list_del_init(&gl->gl_lru);
168 else
169 atomic_inc(&lru_count);
170
171 list_add_tail(&gl->gl_lru, &lru_list);
Steven Whitehouse627c10b2011-04-14 14:09:52 +0100172 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100173 spin_unlock(&lru_lock);
174}
175
Bob Peterson8f6cb402015-01-05 13:25:10 -0500176static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
Steven Whitehousef42ab082011-04-14 16:50:31 +0100177{
Bob Peterson8f6cb402015-01-05 13:25:10 -0500178 spin_lock(&lru_lock);
Steven Whitehousef42ab082011-04-14 16:50:31 +0100179 if (!list_empty(&gl->gl_lru)) {
180 list_del_init(&gl->gl_lru);
181 atomic_dec(&lru_count);
182 clear_bit(GLF_LRU, &gl->gl_flags);
183 }
184 spin_unlock(&lru_lock);
185}
186
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500187/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188 * gfs2_glock_put() - Decrement reference count on glock
189 * @gl: The glock to put
190 *
191 */
192
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000193void gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194{
Bob Peterson15562c42015-03-16 11:52:05 -0500195 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000196 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197
Steven Whitehousee66cf162013-10-15 15:18:08 +0100198 if (lockref_put_or_lock(&gl->gl_lockref))
199 return;
200
201 lockref_mark_dead(&gl->gl_lockref);
202
Bob Peterson8f6cb402015-01-05 13:25:10 -0500203 gfs2_glock_remove_from_lru(gl);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100204 spin_unlock(&gl->gl_lockref.lock);
205 spin_lock_bucket(gl->gl_hash);
206 hlist_bl_del_rcu(&gl->gl_list);
207 spin_unlock_bucket(gl->gl_hash);
208 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
209 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
210 trace_gfs2_glock_put(gl);
211 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212}
213
214/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 * search_bucket() - Find struct gfs2_glock by lock number
216 * @bucket: the bucket to search
217 * @name: The lock name
218 *
219 * Returns: NULL, or the struct gfs2_glock with the requested number
220 */
221
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400222static struct gfs2_glock *search_bucket(unsigned int hash,
Steven Whitehoused6a53722006-08-30 11:16:23 -0400223 const struct lm_lockname *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224{
225 struct gfs2_glock *gl;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000226 struct hlist_bl_node *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000228 hlist_bl_for_each_entry_rcu(gl, h, &gl_hash_table[hash], gl_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 if (!lm_name_equal(&gl->gl_name, name))
230 continue;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100231 if (lockref_get_not_dead(&gl->gl_lockref))
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000232 return gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233 }
234
235 return NULL;
236}
237
238/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100239 * may_grant - check if its ok to grant a new lock
240 * @gl: The glock
241 * @gh: The lock request which we wish to grant
242 *
243 * Returns: true if its ok to grant the lock
244 */
245
246static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500247{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100248 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
249 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
250 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
251 return 0;
252 if (gl->gl_state == gh->gh_state)
253 return 1;
254 if (gh->gh_flags & GL_EXACT)
255 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100256 if (gl->gl_state == LM_ST_EXCLUSIVE) {
257 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
258 return 1;
259 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
260 return 1;
261 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100262 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
263 return 1;
264 return 0;
265}
266
267static void gfs2_holder_wake(struct gfs2_holder *gh)
268{
269 clear_bit(HIF_WAIT, &gh->gh_iflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100270 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100271 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
272}
273
274/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100275 * do_error - Something unexpected has happened during a lock request
276 *
277 */
278
279static inline void do_error(struct gfs2_glock *gl, const int ret)
280{
281 struct gfs2_holder *gh, *tmp;
282
283 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
284 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
285 continue;
286 if (ret & LM_OUT_ERROR)
287 gh->gh_error = -EIO;
288 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
289 gh->gh_error = GLR_TRYFAILED;
290 else
291 continue;
292 list_del_init(&gh->gh_list);
293 trace_gfs2_glock_queue(gh, 0);
294 gfs2_holder_wake(gh);
295 }
296}
297
298/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100299 * do_promote - promote as many requests as possible on the current queue
300 * @gl: The glock
301 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000302 * Returns: 1 if there is a blocked holder at the head of the list, or 2
303 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100304 */
305
306static int do_promote(struct gfs2_glock *gl)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700307__releases(&gl->gl_spin)
308__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100309{
310 const struct gfs2_glock_operations *glops = gl->gl_ops;
311 struct gfs2_holder *gh, *tmp;
312 int ret;
313
314restart:
315 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
316 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
317 continue;
318 if (may_grant(gl, gh)) {
319 if (gh->gh_list.prev == &gl->gl_holders &&
320 glops->go_lock) {
321 spin_unlock(&gl->gl_spin);
322 /* FIXME: eliminate this eventually */
323 ret = glops->go_lock(gh);
324 spin_lock(&gl->gl_spin);
325 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000326 if (ret == 1)
327 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100328 gh->gh_error = ret;
329 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100330 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100331 gfs2_holder_wake(gh);
332 goto restart;
333 }
334 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100335 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100336 gfs2_holder_wake(gh);
337 goto restart;
338 }
339 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100340 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100341 gfs2_holder_wake(gh);
342 continue;
343 }
344 if (gh->gh_list.prev == &gl->gl_holders)
345 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100346 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100347 break;
348 }
349 return 0;
350}
351
352/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100353 * find_first_waiter - find the first gh that's waiting for the glock
354 * @gl: the glock
355 */
356
357static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
358{
359 struct gfs2_holder *gh;
360
361 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
362 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
363 return gh;
364 }
365 return NULL;
366}
367
368/**
369 * state_change - record that the glock is now in a different state
370 * @gl: the glock
371 * @new_state the new state
372 *
373 */
374
375static void state_change(struct gfs2_glock *gl, unsigned int new_state)
376{
377 int held1, held2;
378
379 held1 = (gl->gl_state != LM_ST_UNLOCKED);
380 held2 = (new_state != LM_ST_UNLOCKED);
381
382 if (held1 != held2) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100383 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100384 if (held2)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100385 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100386 else
Steven Whitehousee66cf162013-10-15 15:18:08 +0100387 gl->gl_lockref.count--;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100388 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100389 if (held1 && held2 && list_empty(&gl->gl_holders))
390 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100391
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400392 if (new_state != gl->gl_target)
393 /* shorten our minimum hold time */
394 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
395 GL_GLOCK_MIN_HOLD);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100396 gl->gl_state = new_state;
397 gl->gl_tchange = jiffies;
398}
399
400static void gfs2_demote_wake(struct gfs2_glock *gl)
401{
402 gl->gl_demote_state = LM_ST_EXCLUSIVE;
403 clear_bit(GLF_DEMOTE, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100404 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100405 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
406}
407
408/**
409 * finish_xmote - The DLM has replied to one of our lock requests
410 * @gl: The glock
411 * @ret: The status from the DLM
412 *
413 */
414
415static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
416{
417 const struct gfs2_glock_operations *glops = gl->gl_ops;
418 struct gfs2_holder *gh;
419 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000420 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500421
422 spin_lock(&gl->gl_spin);
Steven Whitehouse63997772009-06-12 08:49:20 +0100423 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100424 state_change(gl, state);
425 gh = find_first_waiter(gl);
426
427 /* Demote to UN request arrived during demote to SH or DF */
428 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
429 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
430 gl->gl_target = LM_ST_UNLOCKED;
431
432 /* Check for state != intended state */
433 if (unlikely(state != gl->gl_target)) {
434 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
435 /* move to back of queue and try next entry */
436 if (ret & LM_OUT_CANCELED) {
437 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
438 list_move_tail(&gh->gh_list, &gl->gl_holders);
439 gh = find_first_waiter(gl);
440 gl->gl_target = gh->gh_state;
441 goto retry;
442 }
443 /* Some error or failed "try lock" - report it */
444 if ((ret & LM_OUT_ERROR) ||
445 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
446 gl->gl_target = gl->gl_state;
447 do_error(gl, ret);
448 goto out;
449 }
450 }
451 switch(state) {
452 /* Unlocked due to conversion deadlock, try again */
453 case LM_ST_UNLOCKED:
454retry:
455 do_xmote(gl, gh, gl->gl_target);
456 break;
457 /* Conversion fails, unlock and try again */
458 case LM_ST_SHARED:
459 case LM_ST_DEFERRED:
460 do_xmote(gl, gh, LM_ST_UNLOCKED);
461 break;
462 default: /* Everything else */
Joe Perchesd77d1b52014-03-06 12:10:45 -0800463 pr_err("wanted %u got %u\n", gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100464 GLOCK_BUG_ON(gl, 1);
465 }
466 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100467 return;
468 }
469
470 /* Fast path - we got what we asked for */
471 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
472 gfs2_demote_wake(gl);
473 if (state != LM_ST_UNLOCKED) {
474 if (glops->go_xmote_bh) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100475 spin_unlock(&gl->gl_spin);
476 rv = glops->go_xmote_bh(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100477 spin_lock(&gl->gl_spin);
478 if (rv) {
479 do_error(gl, rv);
480 goto out;
481 }
482 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000483 rv = do_promote(gl);
484 if (rv == 2)
485 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100486 }
487out:
488 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000489out_locked:
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500490 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500491}
492
Steven Whitehouse6802e342008-05-21 17:03:22 +0100493/**
494 * do_xmote - Calls the DLM to change the state of a lock
495 * @gl: The lock state
496 * @gh: The holder (only for promotes)
497 * @target: The target lock state
498 *
499 */
500
501static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700502__releases(&gl->gl_spin)
503__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100504{
505 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500506 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100507 unsigned int lck_flags = gh ? gh->gh_flags : 0;
508 int ret;
509
510 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
511 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000512 GLOCK_BUG_ON(gl, gl->gl_state == target);
513 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100514 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
515 glops->go_inval) {
516 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
517 do_error(gl, 0); /* Fail queued try locks */
518 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000519 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000520 set_bit(GLF_BLOCKING, &gl->gl_flags);
521 if ((gl->gl_req == LM_ST_UNLOCKED) ||
522 (gl->gl_state == LM_ST_EXCLUSIVE) ||
523 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
524 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100525 spin_unlock(&gl->gl_spin);
Bob Peterson06dfc302012-10-24 14:41:05 -0400526 if (glops->go_sync)
527 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100528 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
529 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
530 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
531
532 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000533 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
534 /* lock_dlm */
535 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
David Teiglanddba2d702012-11-14 13:46:53 -0500536 if (ret) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800537 pr_err("lm_lock ret %d\n", ret);
David Teiglanddba2d702012-11-14 13:46:53 -0500538 GLOCK_BUG_ON(gl, 1);
539 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000540 } else { /* lock_nolock */
541 finish_xmote(gl, target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100542 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
543 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100544 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000545
Steven Whitehouse6802e342008-05-21 17:03:22 +0100546 spin_lock(&gl->gl_spin);
547}
548
549/**
550 * find_first_holder - find the first "holder" gh
551 * @gl: the glock
552 */
553
554static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
555{
556 struct gfs2_holder *gh;
557
558 if (!list_empty(&gl->gl_holders)) {
559 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
560 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
561 return gh;
562 }
563 return NULL;
564}
565
566/**
567 * run_queue - do all outstanding tasks related to a glock
568 * @gl: The glock in question
569 * @nonblock: True if we must not block in run_queue
570 *
571 */
572
573static void run_queue(struct gfs2_glock *gl, const int nonblock)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700574__releases(&gl->gl_spin)
575__acquires(&gl->gl_spin)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100576{
577 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000578 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100579
580 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
581 return;
582
583 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
584
585 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
586 gl->gl_demote_state != gl->gl_state) {
587 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000588 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100589 if (nonblock)
590 goto out_sched;
591 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100592 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100593 gl->gl_target = gl->gl_demote_state;
594 } else {
595 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
596 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000597 ret = do_promote(gl);
598 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000599 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000600 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100601 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100602 gh = find_first_waiter(gl);
603 gl->gl_target = gh->gh_state;
604 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
605 do_error(gl, 0); /* Fail queued try locks */
606 }
607 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100608out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100609 return;
610
611out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100612 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100613 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100614 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100615 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100616 gl->gl_lockref.count--;
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100617 return;
618
Steven Whitehoused8348de2009-02-05 10:12:38 +0000619out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100620 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100621 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100622 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100623}
624
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500625static void delete_work_func(struct work_struct *work)
626{
627 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500628 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000629 struct gfs2_inode *ip;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500630 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000631 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500632
Steven Whitehouse044b9412010-11-03 20:01:07 +0000633 ip = gl->gl_object;
634 /* Note: Unsafe to dereference ip as we don't hold right refs/locks */
635
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500636 if (ip)
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100637 inode = gfs2_ilookup(sdp->sd_vfs, no_addr, 1);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000638 else
639 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
640 if (inode && !IS_ERR(inode)) {
641 d_prune_aliases(inode);
642 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500643 }
644 gfs2_glock_put(gl);
645}
646
Steven Whitehouse6802e342008-05-21 17:03:22 +0100647static void glock_work_func(struct work_struct *work)
648{
649 unsigned long delay = 0;
650 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000651 int drop_ref = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100652
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000653 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100654 finish_xmote(gl, gl->gl_reply);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000655 drop_ref = 1;
656 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100657 spin_lock(&gl->gl_spin);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400658 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100659 gl->gl_state != LM_ST_UNLOCKED &&
660 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100661 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400662
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400663 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100664 if (time_before(now, holdtime))
665 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400666
667 if (!delay) {
668 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
669 set_bit(GLF_DEMOTE, &gl->gl_flags);
670 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100671 }
672 run_queue(gl, 0);
673 spin_unlock(&gl->gl_spin);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400674 if (!delay)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100675 gfs2_glock_put(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400676 else {
677 if (gl->gl_name.ln_type != LM_TYPE_INODE)
678 delay = 0;
679 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
680 gfs2_glock_put(gl);
681 }
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000682 if (drop_ref)
683 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100684}
685
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686/**
687 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
688 * @sdp: The GFS2 superblock
689 * @number: the lock number
690 * @glops: The glock_operations to use
691 * @create: If 0, don't create the glock if it doesn't exist
692 * @glp: the glock is returned here
693 *
694 * This does not lock a glock, just finds/creates structures for one.
695 *
696 * Returns: errno
697 */
698
Steven Whitehousecd915492006-09-04 12:49:07 -0400699int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400700 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701 struct gfs2_glock **glp)
702{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000703 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500704 struct lm_lockname name = { .ln_number = number,
705 .ln_type = glops->go_type,
706 .ln_sbd = sdp };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 struct gfs2_glock *gl, *tmp;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400708 unsigned int hash = gl_hash(sdp, &name);
Steven Whitehouse009d8512009-12-08 12:12:13 +0000709 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000710 struct kmem_cache *cachep;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000712 rcu_read_lock();
Bob Peterson15562c42015-03-16 11:52:05 -0500713 gl = search_bucket(hash, &name);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000714 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000716 *glp = gl;
717 if (gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 return 0;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000719 if (!create)
720 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721
Steven Whitehouse009d8512009-12-08 12:12:13 +0000722 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000723 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000724 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000725 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100726 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000727 if (!gl)
728 return -ENOMEM;
729
David Teiglanddba2d702012-11-14 13:46:53 -0500730 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500731
732 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100733 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500734 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500735 kmem_cache_free(cachep, gl);
736 return -ENOMEM;
737 }
David Teiglanddba2d702012-11-14 13:46:53 -0500738 }
739
Steven Whitehouse8f052282010-01-29 15:21:27 +0000740 atomic_inc(&sdp->sd_glock_disposal);
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400741 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100743 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100745 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500746 gl->gl_demote_state = LM_ST_EXCLUSIVE;
Steven Whitehouse37b2fa62006-09-08 13:35:56 -0400747 gl->gl_hash = hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 gl->gl_ops = glops;
Steven Whitehousea2457692012-01-20 10:38:36 +0000749 gl->gl_dstamp = ktime_set(0, 0);
750 preempt_disable();
751 /* We use the global stats to estimate the initial per-glock stats */
752 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
753 preempt_enable();
754 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
755 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500756 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400757 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400758 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500759 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500760 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761
Steven Whitehouse009d8512009-12-08 12:12:13 +0000762 mapping = gfs2_glock2aspace(gl);
763 if (mapping) {
764 mapping->a_ops = &gfs2_meta_aops;
765 mapping->host = s->s_bdev->bd_inode;
766 mapping->flags = 0;
767 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800768 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000769 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770 }
771
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000772 spin_lock_bucket(hash);
Bob Peterson15562c42015-03-16 11:52:05 -0500773 tmp = search_bucket(hash, &name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 if (tmp) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000775 spin_unlock_bucket(hash);
David Teigland4e2f8842012-11-14 13:47:37 -0500776 kfree(gl->gl_lksb.sb_lvbptr);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000777 kmem_cache_free(cachep, gl);
Steven Whitehousefc0e38d2011-03-09 10:58:04 +0000778 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 gl = tmp;
780 } else {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000781 hlist_bl_add_head_rcu(&gl->gl_list, &gl_hash_table[hash]);
782 spin_unlock_bucket(hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 }
784
785 *glp = gl;
786
787 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788}
789
790/**
791 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
792 * @gl: the glock
793 * @state: the state we're requesting
794 * @flags: the modifier flags
795 * @gh: the holder structure
796 *
797 */
798
Steven Whitehouse190562b2006-04-20 16:57:23 -0400799void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 struct gfs2_holder *gh)
801{
802 INIT_LIST_HEAD(&gh->gh_list);
803 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200804 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800805 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 gh->gh_state = state;
807 gh->gh_flags = flags;
808 gh->gh_error = 0;
809 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 gfs2_glock_hold(gl);
811}
812
813/**
814 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
815 * @state: the state we're requesting
816 * @flags: the modifier flags
817 * @gh: the holder structure
818 *
819 * Don't mess with the glock.
820 *
821 */
822
Steven Whitehouse190562b2006-04-20 16:57:23 -0400823void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824{
825 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400826 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000827 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200828 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100829 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400830 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831}
832
833/**
834 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
835 * @gh: the holder structure
836 *
837 */
838
839void gfs2_holder_uninit(struct gfs2_holder *gh)
840{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800841 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 gfs2_glock_put(gh->gh_gl);
843 gh->gh_gl = NULL;
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500844 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845}
846
Steven Whitehousefe64d512009-05-19 10:01:18 +0100847/**
Bob Peterson07a79042012-08-09 12:48:44 -0500848 * gfs2_glock_wait - wait on a glock acquisition
849 * @gh: the glock holder
850 *
851 * Returns: 0 on success
852 */
853
854int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000855{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400856 unsigned long time1 = jiffies;
857
Steven Whitehousefee852e2007-01-17 15:33:23 +0000858 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000859 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400860 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
861 /* Lengthen the minimum hold time. */
862 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
863 GL_GLOCK_HOLD_INCR,
864 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500865 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100866}
867
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000869 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870 * @gl: the glock
871 * @state: the state the caller wants us to change to
872 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000873 * There are only two requests that we are going to see in actual
874 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 */
876
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500877static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100878 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500880 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
881
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500882 set_bit(bit, &gl->gl_flags);
883 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000884 gl->gl_demote_state = state;
885 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100886 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
887 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100888 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500890 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100891 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100892 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893}
894
Steven Whitehouse6802e342008-05-21 17:03:22 +0100895void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000896{
Joe Perches5e690692010-11-09 16:35:20 -0800897 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000898 va_list args;
899
900 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800901
Steven Whitehouse6802e342008-05-21 17:03:22 +0100902 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100903 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100904 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800905 vaf.fmt = fmt;
906 vaf.va = &args;
907
Joe Perchesd77d1b52014-03-06 12:10:45 -0800908 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100909 }
Joe Perches5e690692010-11-09 16:35:20 -0800910
Robert Peterson7c52b162007-03-16 10:26:37 +0000911 va_end(args);
912}
913
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915 * add_to_queue - Add a holder to the wait queue (but look for recursion)
916 * @gh: the holder structure to add
917 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100918 * Eventually we should move the recursive locking trap to a
919 * debugging option or something like that. This is the fast
920 * path and needs to have the minimum number of distractions.
921 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000922 */
923
Steven Whitehouse6802e342008-05-21 17:03:22 +0100924static inline void add_to_queue(struct gfs2_holder *gh)
Harvey Harrison55ba4742008-10-24 11:31:12 -0700925__releases(&gl->gl_spin)
926__acquires(&gl->gl_spin)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927{
928 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -0500929 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100930 struct list_head *insert_pt = NULL;
931 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -0500932 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800934 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000935 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
936 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -0400937
Steven Whitehouse6802e342008-05-21 17:03:22 +0100938 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
939 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -0500940 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100941 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
942 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 }
944
Steven Whitehouse6802e342008-05-21 17:03:22 +0100945 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
946 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
947 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
948 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -0500949 if (try_futile &&
950 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100951fail:
952 gh->gh_error = GLR_TRYFAILED;
953 gfs2_holder_wake(gh);
954 return;
955 }
956 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
957 continue;
958 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
959 insert_pt = &gh2->gh_list;
960 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100961 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +0000962 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +0000963 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
964 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100965 if (likely(insert_pt == NULL)) {
966 list_add_tail(&gh->gh_list, &gl->gl_holders);
967 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
968 goto do_cancel;
969 return;
970 }
971 list_add_tail(&gh->gh_list, insert_pt);
972do_cancel:
973 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
974 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
975 spin_unlock(&gl->gl_spin);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100976 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000977 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100978 spin_lock(&gl->gl_spin);
979 }
980 return;
981
982trap_recursive:
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800983 pr_err("original: %pSR\n", (void *)gh2->gh_ip);
984 pr_err("pid: %d\n", pid_nr(gh2->gh_owner_pid));
985 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +0100986 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800987 pr_err("new: %pSR\n", (void *)gh->gh_ip);
988 pr_err("pid: %d\n", pid_nr(gh->gh_owner_pid));
989 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +0100990 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +0100991 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100992 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993}
994
995/**
996 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
997 * @gh: the holder structure
998 *
999 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1000 *
1001 * Returns: 0, GLR_TRYFAILED, or errno on failure
1002 */
1003
1004int gfs2_glock_nq(struct gfs2_holder *gh)
1005{
1006 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001007 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 int error = 0;
1009
Steven Whitehouse6802e342008-05-21 17:03:22 +01001010 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012
Steven Whitehousef42ab082011-04-14 16:50:31 +01001013 if (test_bit(GLF_LRU, &gl->gl_flags))
1014 gfs2_glock_remove_from_lru(gl);
1015
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 spin_lock(&gl->gl_spin);
1017 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -04001018 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1019 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001020 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -04001021 gl->gl_lockref.count++;
1022 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1023 gl->gl_lockref.count--;
1024 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001025 run_queue(gl, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 spin_unlock(&gl->gl_spin);
1027
Steven Whitehouse6802e342008-05-21 17:03:22 +01001028 if (!(gh->gh_flags & GL_ASYNC))
1029 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031 return error;
1032}
1033
1034/**
1035 * gfs2_glock_poll - poll to see if an async request has been completed
1036 * @gh: the holder
1037 *
1038 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1039 */
1040
1041int gfs2_glock_poll(struct gfs2_holder *gh)
1042{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001043 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044}
1045
1046/**
1047 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1048 * @gh: the glock holder
1049 *
1050 */
1051
1052void gfs2_glock_dq(struct gfs2_holder *gh)
1053{
1054 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001055 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001056 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001057 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001058
Steven Whitehouse6802e342008-05-21 17:03:22 +01001059 spin_lock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001060 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001061 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 list_del_init(&gh->gh_list);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001064 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001065 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001066 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001067 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 glops->go_unlock(gh);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001069 spin_lock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001070 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001071 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001072 if (list_empty(&gl->gl_holders) &&
1073 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1074 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1075 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076 }
Bob Petersone7ccaf52015-06-12 13:15:54 -05001077 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl) &&
1078 (glops->go_flags & GLOF_LRU))
Bob Peterson4abb6ad2012-08-09 12:48:43 -05001079 gfs2_glock_add_to_lru(gl);
1080
Steven Whitehouse63997772009-06-12 08:49:20 +01001081 trace_gfs2_glock_queue(gh, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001083 if (likely(fast_path))
1084 return;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001085
1086 gfs2_glock_hold(gl);
1087 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001088 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1089 gl->gl_name.ln_type == LM_TYPE_INODE)
1090 delay = gl->gl_hold_time;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001091 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1092 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093}
1094
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001095void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1096{
1097 struct gfs2_glock *gl = gh->gh_gl;
1098 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001099 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001100 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001101}
1102
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1105 * @gh: the holder structure
1106 *
1107 */
1108
1109void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1110{
1111 gfs2_glock_dq(gh);
1112 gfs2_holder_uninit(gh);
1113}
1114
1115/**
1116 * gfs2_glock_nq_num - acquire a glock based on lock number
1117 * @sdp: the filesystem
1118 * @number: the lock number
1119 * @glops: the glock operations for the type of glock
1120 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001121 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001122 * @gh: the struct gfs2_holder
1123 *
1124 * Returns: errno
1125 */
1126
Steven Whitehousecd915492006-09-04 12:49:07 -04001127int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001128 const struct gfs2_glock_operations *glops,
1129 unsigned int state, int flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130{
1131 struct gfs2_glock *gl;
1132 int error;
1133
1134 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1135 if (!error) {
1136 error = gfs2_glock_nq_init(gl, state, flags, gh);
1137 gfs2_glock_put(gl);
1138 }
1139
1140 return error;
1141}
1142
1143/**
1144 * glock_compare - Compare two struct gfs2_glock structures for sorting
1145 * @arg_a: the first structure
1146 * @arg_b: the second structure
1147 *
1148 */
1149
1150static int glock_compare(const void *arg_a, const void *arg_b)
1151{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001152 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1153 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1154 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1155 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156
1157 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001158 return 1;
1159 if (a->ln_number < b->ln_number)
1160 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001161 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 -04001162 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163}
1164
1165/**
1166 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1167 * @num_gh: the number of structures
1168 * @ghs: an array of struct gfs2_holder structures
1169 *
1170 * Returns: 0 on success (all glocks acquired),
1171 * errno on failure (no glocks acquired)
1172 */
1173
1174static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1175 struct gfs2_holder **p)
1176{
1177 unsigned int x;
1178 int error = 0;
1179
1180 for (x = 0; x < num_gh; x++)
1181 p[x] = &ghs[x];
1182
1183 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1184
1185 for (x = 0; x < num_gh; x++) {
1186 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1187
1188 error = gfs2_glock_nq(p[x]);
1189 if (error) {
1190 while (x--)
1191 gfs2_glock_dq(p[x]);
1192 break;
1193 }
1194 }
1195
1196 return error;
1197}
1198
1199/**
1200 * gfs2_glock_nq_m - acquire multiple glocks
1201 * @num_gh: the number of structures
1202 * @ghs: an array of struct gfs2_holder structures
1203 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 *
1205 * Returns: 0 on success (all glocks acquired),
1206 * errno on failure (no glocks acquired)
1207 */
1208
1209int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1210{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001211 struct gfs2_holder *tmp[4];
1212 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 int error = 0;
1214
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001215 switch(num_gh) {
1216 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001218 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1220 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001221 default:
1222 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001224 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1225 if (!pph)
1226 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 }
1228
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001229 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001230
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001231 if (pph != tmp)
1232 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001233
1234 return error;
1235}
1236
1237/**
1238 * gfs2_glock_dq_m - release multiple glocks
1239 * @num_gh: the number of structures
1240 * @ghs: an array of struct gfs2_holder structures
1241 *
1242 */
1243
1244void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1245{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001246 while (num_gh--)
1247 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248}
1249
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001250void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001251{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001252 unsigned long delay = 0;
1253 unsigned long holdtime;
1254 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001256 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001257 holdtime = gl->gl_tchange + gl->gl_hold_time;
1258 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1259 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001260 if (time_before(now, holdtime))
1261 delay = holdtime - now;
1262 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001263 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001264 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265
Steven Whitehouse6802e342008-05-21 17:03:22 +01001266 spin_lock(&gl->gl_spin);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001267 handle_callback(gl, state, delay, true);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001268 spin_unlock(&gl->gl_spin);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001269 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1270 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271}
1272
1273/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001274 * gfs2_should_freeze - Figure out if glock should be frozen
1275 * @gl: The glock in question
1276 *
1277 * Glocks are not frozen if (a) the result of the dlm operation is
1278 * an error, (b) the locking operation was an unlock operation or
1279 * (c) if there is a "noexp" flagged request anywhere in the queue
1280 *
1281 * Returns: 1 if freezing should occur, 0 otherwise
1282 */
1283
1284static int gfs2_should_freeze(const struct gfs2_glock *gl)
1285{
1286 const struct gfs2_holder *gh;
1287
1288 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1289 return 0;
1290 if (gl->gl_target == LM_ST_UNLOCKED)
1291 return 0;
1292
1293 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1294 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1295 continue;
1296 if (LM_FLAG_NOEXP & gh->gh_flags)
1297 return 0;
1298 }
1299
1300 return 1;
1301}
1302
1303/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001304 * gfs2_glock_complete - Callback used by locking
1305 * @gl: Pointer to the glock
1306 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307 *
Steven Whitehouse47a25382010-11-30 15:49:31 +00001308 * The gl_reply field is under the gl_spin lock so that it is ok
1309 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 */
1311
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001312void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001313{
Bob Peterson15562c42015-03-16 11:52:05 -05001314 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001315
Steven Whitehouse47a25382010-11-30 15:49:31 +00001316 spin_lock(&gl->gl_spin);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001317 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001318
David Teiglande0c2a9a2012-01-09 17:18:05 -05001319 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001320 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001321 set_bit(GLF_FROZEN, &gl->gl_flags);
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001322 spin_unlock(&gl->gl_spin);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001324 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001326
Steven Whitehousee66cf162013-10-15 15:18:08 +01001327 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001328 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001329 spin_unlock(&gl->gl_spin);
1330
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001331 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1332 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333}
1334
Steven Whitehouse4506a512013-02-01 20:36:03 +00001335static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1336{
1337 struct gfs2_glock *gla, *glb;
1338
1339 gla = list_entry(a, struct gfs2_glock, gl_lru);
1340 glb = list_entry(b, struct gfs2_glock, gl_lru);
1341
1342 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1343 return 1;
1344 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1345 return -1;
1346
1347 return 0;
1348}
1349
1350/**
1351 * gfs2_dispose_glock_lru - Demote a list of glocks
1352 * @list: The list to dispose of
1353 *
1354 * Disposing of glocks may involve disk accesses, so that here we sort
1355 * the glocks by number (i.e. disk location of the inodes) so that if
1356 * there are any such accesses, they'll be sent in order (mostly).
1357 *
1358 * Must be called under the lru_lock, but may drop and retake this
1359 * lock. While the lru_lock is dropped, entries may vanish from the
1360 * list, but no new entries will appear on the list (since it is
1361 * private)
1362 */
1363
1364static void gfs2_dispose_glock_lru(struct list_head *list)
1365__releases(&lru_lock)
1366__acquires(&lru_lock)
1367{
1368 struct gfs2_glock *gl;
1369
1370 list_sort(NULL, list, glock_cmp);
1371
1372 while(!list_empty(list)) {
1373 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1374 list_del_init(&gl->gl_lru);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001375 if (!spin_trylock(&gl->gl_spin)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001376add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001377 list_add(&gl->gl_lru, &lru_list);
1378 atomic_inc(&lru_count);
1379 continue;
1380 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001381 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
1382 spin_unlock(&gl->gl_spin);
1383 goto add_back_to_lru;
1384 }
Steven Whitehouse4506a512013-02-01 20:36:03 +00001385 clear_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001386 gl->gl_lockref.count++;
Steven Whitehouse4506a512013-02-01 20:36:03 +00001387 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001388 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001389 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse4506a512013-02-01 20:36:03 +00001390 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +01001391 gl->gl_lockref.count--;
Steven Whitehouse4506a512013-02-01 20:36:03 +00001392 spin_unlock(&gl->gl_spin);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001393 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001394 }
1395}
1396
Steven Whitehouse2a005852012-12-14 12:28:30 +00001397/**
1398 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1399 * @nr: The number of entries to scan
1400 *
Steven Whitehouse4506a512013-02-01 20:36:03 +00001401 * This function selects the entries on the LRU which are able to
1402 * be demoted, and then kicks off the process by calling
1403 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001404 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001405
Dave Chinner1ab6c492013-08-28 10:18:09 +10001406static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001407{
1408 struct gfs2_glock *gl;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001409 LIST_HEAD(skipped);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001410 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001411 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001412
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001413 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001414 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001415 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001416
1417 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001418 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a512013-02-01 20:36:03 +00001419 list_move(&gl->gl_lru, &dispose);
1420 atomic_dec(&lru_count);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001421 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001422 continue;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001423 }
Steven Whitehouse4506a512013-02-01 20:36:03 +00001424
1425 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001426 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001427 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001428 if (!list_empty(&dispose))
1429 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001430 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001431
1432 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001433}
1434
Dave Chinner1ab6c492013-08-28 10:18:09 +10001435static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1436 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001437{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001438 if (!(sc->gfp_mask & __GFP_FS))
1439 return SHRINK_STOP;
1440 return gfs2_scan_glock_lru(sc->nr_to_scan);
1441}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001442
Dave Chinner1ab6c492013-08-28 10:18:09 +10001443static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1444 struct shrink_control *sc)
1445{
Glauber Costa55f841c2013-08-28 10:17:53 +10001446 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001447}
1448
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001449static struct shrinker glock_shrinker = {
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001450 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001451 .count_objects = gfs2_glock_shrink_count,
1452 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001453};
1454
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455/**
1456 * examine_bucket - Call a function for glock in a hash bucket
1457 * @examiner: the function
1458 * @sdp: the filesystem
1459 * @bucket: the bucket
1460 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461 */
1462
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001463static void examine_bucket(glock_examiner examiner, const struct gfs2_sbd *sdp,
Steven Whitehouse37b2fa62006-09-08 13:35:56 -04001464 unsigned int hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001465{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001466 struct gfs2_glock *gl;
1467 struct hlist_bl_head *head = &gl_hash_table[hash];
1468 struct hlist_bl_node *pos;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001470 rcu_read_lock();
1471 hlist_bl_for_each_entry_rcu(gl, pos, head, gl_list) {
Bob Peterson15562c42015-03-16 11:52:05 -05001472 if ((gl->gl_name.ln_sbd == sdp) && lockref_get_not_dead(&gl->gl_lockref))
Steven Whitehouse24264432006-09-11 21:40:30 -04001473 examiner(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001474 }
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001475 rcu_read_unlock();
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001476 cond_resched();
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001477}
1478
1479static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
1480{
1481 unsigned x;
1482
1483 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
1484 examine_bucket(examiner, sdp, x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485}
1486
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001487
1488/**
1489 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1490 * @gl: The glock to thaw
1491 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001492 */
1493
1494static void thaw_glock(struct gfs2_glock *gl)
1495{
1496 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
Steven Whitehouse7286b312013-08-20 09:35:09 +01001497 goto out;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001498 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse7286b312013-08-20 09:35:09 +01001499 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) {
1500out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001501 gfs2_glock_put(gl);
Steven Whitehouse7286b312013-08-20 09:35:09 +01001502 }
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001503}
1504
David Teiglandb3b94fa2006-01-16 16:50:04 +00001505/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001506 * clear_glock - look at a glock and see if we can free it from glock cache
1507 * @gl: the glock to look at
1508 *
1509 */
1510
1511static void clear_glock(struct gfs2_glock *gl)
1512{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001513 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001514
Steven Whitehouse6802e342008-05-21 17:03:22 +01001515 spin_lock(&gl->gl_spin);
Steven Whitehousec741c452010-09-29 14:20:52 +01001516 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001517 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001518 spin_unlock(&gl->gl_spin);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001519 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1520 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001521}
1522
1523/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001524 * gfs2_glock_thaw - Thaw any frozen glocks
1525 * @sdp: The super block
1526 *
1527 */
1528
1529void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1530{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001531 glock_hash_walk(thaw_glock, sdp);
1532}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001533
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001534static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001535{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001536 spin_lock(&gl->gl_spin);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001537 gfs2_dump_glock(seq, gl);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001538 spin_unlock(&gl->gl_spin);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001539}
1540
1541static void dump_glock_func(struct gfs2_glock *gl)
1542{
1543 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001544}
1545
1546/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547 * gfs2_gl_hash_clear - Empty out the glock hash table
1548 * @sdp: the filesystem
1549 * @wait: wait until it's all gone
1550 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001551 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 */
1553
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001554void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001555{
David Teiglandfb6791d2012-11-13 10:58:56 -05001556 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001557 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001558 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001559 flush_workqueue(glock_workqueue);
1560 wait_event(sdp->sd_glock_wait, atomic_read(&sdp->sd_glock_disposal) == 0);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001561 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001562}
1563
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001564void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1565{
1566 struct gfs2_glock *gl = ip->i_gl;
1567 int ret;
1568
1569 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001570 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001571
1572 spin_lock(&gl->gl_spin);
1573 clear_bit(GLF_LOCK, &gl->gl_flags);
1574 run_queue(gl, 1);
1575 spin_unlock(&gl->gl_spin);
1576}
1577
Steven Whitehouse6802e342008-05-21 17:03:22 +01001578static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001579{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001580 switch(state) {
1581 case LM_ST_UNLOCKED:
1582 return "UN";
1583 case LM_ST_SHARED:
1584 return "SH";
1585 case LM_ST_DEFERRED:
1586 return "DF";
1587 case LM_ST_EXCLUSIVE:
1588 return "EX";
1589 }
1590 return "??";
1591}
Robert Peterson04b933f2007-03-23 17:05:15 -05001592
Steven Whitehouse6802e342008-05-21 17:03:22 +01001593static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
1594{
1595 char *p = buf;
1596 if (flags & LM_FLAG_TRY)
1597 *p++ = 't';
1598 if (flags & LM_FLAG_TRY_1CB)
1599 *p++ = 'T';
1600 if (flags & LM_FLAG_NOEXP)
1601 *p++ = 'e';
1602 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001603 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001604 if (flags & LM_FLAG_PRIORITY)
1605 *p++ = 'p';
1606 if (flags & GL_ASYNC)
1607 *p++ = 'a';
1608 if (flags & GL_EXACT)
1609 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001610 if (flags & GL_NOCACHE)
1611 *p++ = 'c';
1612 if (test_bit(HIF_HOLDER, &iflags))
1613 *p++ = 'H';
1614 if (test_bit(HIF_WAIT, &iflags))
1615 *p++ = 'W';
1616 if (test_bit(HIF_FIRST, &iflags))
1617 *p++ = 'F';
1618 *p = 0;
1619 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001620}
1621
David Teiglandb3b94fa2006-01-16 16:50:04 +00001622/**
1623 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001624 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001625 * @gh: the glock holder
1626 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001627 */
1628
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001629static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001630{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001631 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001632 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001633
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001634 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001635 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001636 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001637 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1638 state2str(gh->gh_state),
1639 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1640 gh->gh_error,
1641 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1642 gh_owner ? gh_owner->comm : "(ended)",
1643 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001644 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001645}
1646
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001647static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001648{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001649 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001650 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001651
Steven Whitehouse6802e342008-05-21 17:03:22 +01001652 if (test_bit(GLF_LOCK, gflags))
1653 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001654 if (test_bit(GLF_DEMOTE, gflags))
1655 *p++ = 'D';
1656 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1657 *p++ = 'd';
1658 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1659 *p++ = 'p';
1660 if (test_bit(GLF_DIRTY, gflags))
1661 *p++ = 'y';
1662 if (test_bit(GLF_LFLUSH, gflags))
1663 *p++ = 'f';
1664 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1665 *p++ = 'i';
1666 if (test_bit(GLF_REPLY_PENDING, gflags))
1667 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001668 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001669 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001670 if (test_bit(GLF_FROZEN, gflags))
1671 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001672 if (test_bit(GLF_QUEUED, gflags))
1673 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001674 if (test_bit(GLF_LRU, gflags))
1675 *p++ = 'L';
1676 if (gl->gl_object)
1677 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001678 if (test_bit(GLF_BLOCKING, gflags))
1679 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001680 *p = 0;
1681 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001682}
1683
1684/**
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +01001685 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001686 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001687 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001688 *
1689 * The file format is as follows:
1690 * One line per object, capital letters are used to indicate objects
1691 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1692 * other objects are indented by a single space and follow the glock to
1693 * which they are related. Fields are indicated by lower case letters
1694 * followed by a colon and the field value, except for strings which are in
1695 * [] so that its possible to see if they are composed of spaces for
1696 * example. The field's are n = number (id of the object), f = flags,
1697 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001698 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001699 */
1700
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001701void gfs2_dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001702{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001703 const struct gfs2_glock_operations *glops = gl->gl_ops;
1704 unsigned long long dtime;
1705 const struct gfs2_holder *gh;
1706 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001707
Steven Whitehouse6802e342008-05-21 17:03:22 +01001708 dtime = jiffies - gl->gl_demote_time;
1709 dtime *= 1000000/HZ; /* demote time in uSec */
1710 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1711 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001712 gfs2_print_dbg(seq, "G: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d v:%d r:%d m:%ld\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001713 state2str(gl->gl_state),
1714 gl->gl_name.ln_type,
1715 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001716 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001717 state2str(gl->gl_target),
1718 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001719 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001720 atomic_read(&gl->gl_revokes),
Steven Whitehousee66cf162013-10-15 15:18:08 +01001721 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001723 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1724 dump_holder(seq, gh);
1725
Steven Whitehouse6802e342008-05-21 17:03:22 +01001726 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001727 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728}
1729
Steven Whitehousea2457692012-01-20 10:38:36 +00001730static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1731{
1732 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001733
Steven Whitehousea2457692012-01-20 10:38:36 +00001734 seq_printf(seq, "G: n:%u/%llx rtt:%lld/%lld rttb:%lld/%lld irt:%lld/%lld dcnt: %lld qcnt: %lld\n",
1735 gl->gl_name.ln_type,
1736 (unsigned long long)gl->gl_name.ln_number,
1737 (long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1738 (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1739 (long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1740 (long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1741 (long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1742 (long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1743 (long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1744 (long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
1745 return 0;
1746}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001747
Steven Whitehousea2457692012-01-20 10:38:36 +00001748static const char *gfs2_gltype[] = {
1749 "type",
1750 "reserved",
1751 "nondisk",
1752 "inode",
1753 "rgrp",
1754 "meta",
1755 "iopen",
1756 "flock",
1757 "plock",
1758 "quota",
1759 "journal",
1760};
1761
1762static const char *gfs2_stype[] = {
1763 [GFS2_LKS_SRTT] = "srtt",
1764 [GFS2_LKS_SRTTVAR] = "srttvar",
1765 [GFS2_LKS_SRTTB] = "srttb",
1766 [GFS2_LKS_SRTTVARB] = "srttvarb",
1767 [GFS2_LKS_SIRT] = "sirt",
1768 [GFS2_LKS_SIRTVAR] = "sirtvar",
1769 [GFS2_LKS_DCOUNT] = "dlm",
1770 [GFS2_LKS_QCOUNT] = "queue",
1771};
1772
1773#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1774
1775static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1776{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001777 struct gfs2_sbd *sdp = seq->private;
1778 loff_t pos = *(loff_t *)iter_ptr;
1779 unsigned index = pos >> 3;
1780 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001781 s64 value;
1782 int i;
1783
1784 if (index == 0 && subindex != 0)
1785 return 0;
1786
1787 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1788 (index == 0) ? "cpu": gfs2_stype[subindex]);
1789
1790 for_each_possible_cpu(i) {
1791 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
1792 if (index == 0) {
1793 value = i;
1794 } else {
1795 value = lkstats->lkstats[index - 1].stats[subindex];
1796 }
1797 seq_printf(seq, " %15lld", (long long)value);
1798 }
1799 seq_putc(seq, '\n');
1800 return 0;
1801}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001802
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001803int __init gfs2_glock_init(void)
1804{
1805 unsigned i;
1806 for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001807 INIT_HLIST_BL_HEAD(&gl_hash_table[i]);
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001808 }
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001809
Steven Whitehoused2115772010-11-03 19:58:53 +00001810 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001811 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001812 if (!glock_workqueue)
1813 return -ENOMEM;
Steven Whitehoused2115772010-11-03 19:58:53 +00001814 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001815 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001816 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001817 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001818 destroy_workqueue(glock_workqueue);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001819 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001820 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001821
1822 register_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001823
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001824 return 0;
1825}
1826
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001827void gfs2_glock_exit(void)
1828{
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001829 unregister_shrinker(&glock_shrinker);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001830 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001831 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001832}
1833
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001834static inline struct gfs2_glock *glock_hash_chain(unsigned hash)
1835{
1836 return hlist_bl_entry(hlist_bl_first_rcu(&gl_hash_table[hash]),
1837 struct gfs2_glock, gl_list);
1838}
1839
1840static inline struct gfs2_glock *glock_hash_next(struct gfs2_glock *gl)
1841{
Steven Whitehouse7e32d022011-03-15 08:32:14 +00001842 return hlist_bl_entry(rcu_dereference(gl->gl_list.next),
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001843 struct gfs2_glock, gl_list);
1844}
1845
Steven Whitehouse6802e342008-05-21 17:03:22 +01001846static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Robert Peterson7c52b162007-03-16 10:26:37 +00001847{
Steven Whitehouse7b08fc62007-07-24 13:53:36 +01001848 struct gfs2_glock *gl;
1849
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001850 do {
1851 gl = gi->gl;
1852 if (gl) {
1853 gi->gl = glock_hash_next(gl);
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001854 gi->nhash++;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001855 } else {
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001856 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1857 rcu_read_unlock();
1858 return 1;
1859 }
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001860 gi->gl = glock_hash_chain(gi->hash);
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001861 gi->nhash = 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001862 }
1863 while (gi->gl == NULL) {
1864 gi->hash++;
1865 if (gi->hash >= GFS2_GL_HASH_SIZE) {
1866 rcu_read_unlock();
1867 return 1;
1868 }
1869 gi->gl = glock_hash_chain(gi->hash);
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001870 gi->nhash = 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001871 }
1872 /* Skip entries for other sb and dead entries */
Bob Peterson15562c42015-03-16 11:52:05 -05001873 } while (gi->sdp != gi->gl->gl_name.ln_sbd ||
Michal Nazarewicze3c42692013-11-12 13:30:05 +01001874 __lockref_is_dead(&gi->gl->gl_lockref));
Abhijith Dasa947e032007-08-21 09:57:29 -05001875
Robert Peterson7c52b162007-03-16 10:26:37 +00001876 return 0;
1877}
1878
Steven Whitehouse6802e342008-05-21 17:03:22 +01001879static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Robert Peterson7c52b162007-03-16 10:26:37 +00001880{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001881 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001882 loff_t n = *pos;
1883
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001884 if (gi->last_pos <= *pos)
1885 n = gi->nhash + (*pos - gi->last_pos);
1886 else
1887 gi->hash = 0;
1888
1889 gi->nhash = 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001890 rcu_read_lock();
Robert Peterson7c52b162007-03-16 10:26:37 +00001891
Steven Whitehouse6802e342008-05-21 17:03:22 +01001892 do {
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001893 if (gfs2_glock_iter_next(gi))
Robert Peterson7c52b162007-03-16 10:26:37 +00001894 return NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001895 } while (n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001896
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001897 gi->last_pos = *pos;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001898 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001899}
1900
Steven Whitehouse6802e342008-05-21 17:03:22 +01001901static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001902 loff_t *pos)
1903{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001904 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001905
1906 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001907 gi->last_pos = *pos;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001908 if (gfs2_glock_iter_next(gi))
Robert Peterson7c52b162007-03-16 10:26:37 +00001909 return NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001910
Steven Whitehouse6802e342008-05-21 17:03:22 +01001911 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001912}
1913
Steven Whitehouse6802e342008-05-21 17:03:22 +01001914static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001915{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001916 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001917
1918 if (gi->gl)
1919 rcu_read_unlock();
1920 gi->gl = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001921}
1922
Steven Whitehouse6802e342008-05-21 17:03:22 +01001923static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001924{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001925 dump_glock(seq, iter_ptr);
1926 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00001927}
1928
Steven Whitehousea2457692012-01-20 10:38:36 +00001929static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
1930{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001931 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00001932 if (*pos >= GFS2_NR_SBSTATS)
1933 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001934 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001935}
1936
1937static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
1938 loff_t *pos)
1939{
Steven Whitehousea2457692012-01-20 10:38:36 +00001940 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001941 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00001942 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001943 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001944}
1945
1946static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
1947{
1948 preempt_enable();
1949}
1950
Denis Cheng4ef29002007-07-31 18:31:11 +08001951static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001952 .start = gfs2_glock_seq_start,
1953 .next = gfs2_glock_seq_next,
1954 .stop = gfs2_glock_seq_stop,
1955 .show = gfs2_glock_seq_show,
1956};
1957
Steven Whitehousea2457692012-01-20 10:38:36 +00001958static const struct seq_operations gfs2_glstats_seq_ops = {
1959 .start = gfs2_glock_seq_start,
1960 .next = gfs2_glock_seq_next,
1961 .stop = gfs2_glock_seq_stop,
1962 .show = gfs2_glstats_seq_show,
1963};
1964
1965static const struct seq_operations gfs2_sbstats_seq_ops = {
1966 .start = gfs2_sbstats_seq_start,
1967 .next = gfs2_sbstats_seq_next,
1968 .stop = gfs2_sbstats_seq_stop,
1969 .show = gfs2_sbstats_seq_show,
1970};
1971
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001972#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
1973
Steven Whitehousea2457692012-01-20 10:38:36 +00001974static int gfs2_glocks_open(struct inode *inode, struct file *file)
Robert Peterson7c52b162007-03-16 10:26:37 +00001975{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001976 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1977 sizeof(struct gfs2_glock_iter));
1978 if (ret == 0) {
1979 struct seq_file *seq = file->private_data;
1980 struct gfs2_glock_iter *gi = seq->private;
1981 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001982 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01001983 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001984 seq->size = GFS2_SEQ_GOODSIZE;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001985 }
1986 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00001987}
1988
Steven Whitehousea2457692012-01-20 10:38:36 +00001989static int gfs2_glstats_open(struct inode *inode, struct file *file)
1990{
1991 int ret = seq_open_private(file, &gfs2_glstats_seq_ops,
1992 sizeof(struct gfs2_glock_iter));
1993 if (ret == 0) {
1994 struct seq_file *seq = file->private_data;
1995 struct gfs2_glock_iter *gi = seq->private;
1996 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001997 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01001998 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001999 seq->size = GFS2_SEQ_GOODSIZE;
Steven Whitehousea2457692012-01-20 10:38:36 +00002000 }
2001 return ret;
2002}
2003
2004static int gfs2_sbstats_open(struct inode *inode, struct file *file)
2005{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002006 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002007 if (ret == 0) {
2008 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002009 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00002010 }
2011 return ret;
2012}
2013
2014static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002015 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00002016 .open = gfs2_glocks_open,
2017 .read = seq_read,
2018 .llseek = seq_lseek,
2019 .release = seq_release_private,
2020};
2021
2022static const struct file_operations gfs2_glstats_fops = {
2023 .owner = THIS_MODULE,
2024 .open = gfs2_glstats_open,
2025 .read = seq_read,
2026 .llseek = seq_lseek,
2027 .release = seq_release_private,
2028};
2029
2030static const struct file_operations gfs2_sbstats_fops = {
2031 .owner = THIS_MODULE,
2032 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00002033 .read = seq_read,
2034 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002035 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00002036};
2037
2038int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2039{
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002040 struct dentry *dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002041
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002042 dent = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2043 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002044 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002045 sdp->debugfs_dir = dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002046
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002047 dent = debugfs_create_file("glocks",
2048 S_IFREG | S_IRUGO,
2049 sdp->debugfs_dir, sdp,
2050 &gfs2_glocks_fops);
2051 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002052 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002053 sdp->debugfs_dentry_glocks = dent;
2054
2055 dent = debugfs_create_file("glstats",
2056 S_IFREG | S_IRUGO,
2057 sdp->debugfs_dir, sdp,
2058 &gfs2_glstats_fops);
2059 if (IS_ERR_OR_NULL(dent))
2060 goto fail;
2061 sdp->debugfs_dentry_glstats = dent;
2062
2063 dent = debugfs_create_file("sbstats",
2064 S_IFREG | S_IRUGO,
2065 sdp->debugfs_dir, sdp,
2066 &gfs2_sbstats_fops);
2067 if (IS_ERR_OR_NULL(dent))
2068 goto fail;
2069 sdp->debugfs_dentry_sbstats = dent;
Robert Peterson7c52b162007-03-16 10:26:37 +00002070
2071 return 0;
Steven Whitehousea2457692012-01-20 10:38:36 +00002072fail:
2073 gfs2_delete_debugfs_file(sdp);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002074 return dent ? PTR_ERR(dent) : -ENOMEM;
Robert Peterson7c52b162007-03-16 10:26:37 +00002075}
2076
2077void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2078{
Steven Whitehousea2457692012-01-20 10:38:36 +00002079 if (sdp->debugfs_dir) {
Robert Peterson5f882092007-04-18 11:41:11 -05002080 if (sdp->debugfs_dentry_glocks) {
2081 debugfs_remove(sdp->debugfs_dentry_glocks);
2082 sdp->debugfs_dentry_glocks = NULL;
2083 }
Steven Whitehousea2457692012-01-20 10:38:36 +00002084 if (sdp->debugfs_dentry_glstats) {
2085 debugfs_remove(sdp->debugfs_dentry_glstats);
2086 sdp->debugfs_dentry_glstats = NULL;
2087 }
2088 if (sdp->debugfs_dentry_sbstats) {
2089 debugfs_remove(sdp->debugfs_dentry_sbstats);
2090 sdp->debugfs_dentry_sbstats = NULL;
2091 }
Robert Peterson5f882092007-04-18 11:41:11 -05002092 debugfs_remove(sdp->debugfs_dir);
2093 sdp->debugfs_dir = NULL;
2094 }
Robert Peterson7c52b162007-03-16 10:26:37 +00002095}
2096
2097int gfs2_register_debugfs(void)
2098{
2099 gfs2_root = debugfs_create_dir("gfs2", NULL);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002100 if (IS_ERR(gfs2_root))
2101 return PTR_ERR(gfs2_root);
Robert Peterson7c52b162007-03-16 10:26:37 +00002102 return gfs2_root ? 0 : -ENOMEM;
2103}
2104
2105void gfs2_unregister_debugfs(void)
2106{
2107 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002108 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002109}