blob: f7cae1629c6cf64d9bf49a2fa9d43b9c89fc9e6e [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>
Bob Peterson88ffbf32015-03-16 11:02:46 -050037#include <linux/rhashtable.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000038
39#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050040#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000041#include "glock.h"
42#include "glops.h"
43#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000044#include "lops.h"
45#include "meta_io.h"
46#include "quota.h"
47#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050048#include "util.h"
Steven Whitehouse813e0c42008-11-18 13:38:48 +000049#include "bmap.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010050#define CREATE_TRACE_POINTS
51#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000052
Steven Whitehouse6802e342008-05-21 17:03:22 +010053struct gfs2_glock_iter {
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +010054 struct gfs2_sbd *sdp; /* incore superblock */
Bob Peterson88ffbf32015-03-16 11:02:46 -050055 struct rhashtable_iter hti; /* rhashtable iterator */
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +010056 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
Fabian Frederick47a9a522016-08-02 12:05:27 -050072#define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT)
Steven Whitehouse087efdd2006-09-09 16:59:11 -040073
Bob Peterson88ffbf32015-03-16 11:02:46 -050074static struct rhashtable_params ht_parms = {
75 .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
76 .key_len = sizeof(struct lm_lockname),
77 .key_offset = offsetof(struct gfs2_glock, gl_name),
78 .head_offset = offsetof(struct gfs2_glock, gl_node),
79};
Steven Whitehouse087efdd2006-09-09 16:59:11 -040080
Bob Peterson88ffbf32015-03-16 11:02:46 -050081static struct rhashtable gl_hash_table;
David Teiglandb3b94fa2006-01-16 16:50:04 +000082
Andreas Gruenbacher25b2ee62017-07-07 13:22:05 -050083static void gfs2_glock_dealloc(struct rcu_head *rcu)
David Teiglandb3b94fa2006-01-16 16:50:04 +000084{
Andreas Gruenbacher25b2ee62017-07-07 13:22:05 -050085 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
Steven Whitehousebc015cb2011-01-19 09:30:01 +000086
David Teiglanddba2d702012-11-14 13:46:53 -050087 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +000088 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
David Teiglanddba2d702012-11-14 13:46:53 -050089 } else {
David Teigland4e2f8842012-11-14 13:47:37 -050090 kfree(gl->gl_lksb.sb_lvbptr);
Steven Whitehousebc015cb2011-01-19 09:30:01 +000091 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglanddba2d702012-11-14 13:46:53 -050092 }
Andreas Gruenbacher25b2ee62017-07-07 13:22:05 -050093}
94
95void gfs2_glock_free(struct gfs2_glock *gl)
96{
97 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
98
99 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000100 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
101 wake_up(&sdp->sd_glock_wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102}
103
104/**
105 * gfs2_glock_hold() - increment reference count on glock
106 * @gl: The glock to hold
107 *
108 */
109
Steven Whitehousee66cf162013-10-15 15:18:08 +0100110static void gfs2_glock_hold(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111{
Steven Whitehousee66cf162013-10-15 15:18:08 +0100112 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
113 lockref_get(&gl->gl_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114}
115
116/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500117 * demote_ok - Check to see if it's ok to unlock a glock
118 * @gl: the glock
119 *
120 * Returns: 1 if it's ok
121 */
122
123static int demote_ok(const struct gfs2_glock *gl)
124{
125 const struct gfs2_glock_operations *glops = gl->gl_ops;
126
127 if (gl->gl_state == LM_ST_UNLOCKED)
128 return 0;
Steven Whitehousef42ab082011-04-14 16:50:31 +0100129 if (!list_empty(&gl->gl_holders))
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500130 return 0;
131 if (glops->go_demote_ok)
132 return glops->go_demote_ok(gl);
133 return 1;
134}
135
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000136
Steven Whitehouse29687a22011-03-30 16:33:25 +0100137void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
138{
139 spin_lock(&lru_lock);
140
141 if (!list_empty(&gl->gl_lru))
142 list_del_init(&gl->gl_lru);
143 else
144 atomic_inc(&lru_count);
145
146 list_add_tail(&gl->gl_lru, &lru_list);
Steven Whitehouse627c10b2011-04-14 14:09:52 +0100147 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100148 spin_unlock(&lru_lock);
149}
150
Bob Peterson8f6cb402015-01-05 13:25:10 -0500151static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
Steven Whitehousef42ab082011-04-14 16:50:31 +0100152{
Bob Peterson8f6cb402015-01-05 13:25:10 -0500153 spin_lock(&lru_lock);
Steven Whitehousef42ab082011-04-14 16:50:31 +0100154 if (!list_empty(&gl->gl_lru)) {
155 list_del_init(&gl->gl_lru);
156 atomic_dec(&lru_count);
157 clear_bit(GLF_LRU, &gl->gl_flags);
158 }
159 spin_unlock(&lru_lock);
160}
161
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500162/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 * gfs2_glock_put() - Decrement reference count on glock
164 * @gl: The glock to put
165 *
166 */
167
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000168void gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169{
Bob Peterson15562c42015-03-16 11:52:05 -0500170 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000171 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172
Steven Whitehousee66cf162013-10-15 15:18:08 +0100173 if (lockref_put_or_lock(&gl->gl_lockref))
174 return;
175
176 lockref_mark_dead(&gl->gl_lockref);
177
Bob Peterson8f6cb402015-01-05 13:25:10 -0500178 gfs2_glock_remove_from_lru(gl);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100179 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500180 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100181 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
182 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
183 trace_gfs2_glock_put(gl);
184 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185}
186
187/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100188 * may_grant - check if its ok to grant a new lock
189 * @gl: The glock
190 * @gh: The lock request which we wish to grant
191 *
192 * Returns: true if its ok to grant the lock
193 */
194
195static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500196{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100197 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
198 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
199 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
200 return 0;
201 if (gl->gl_state == gh->gh_state)
202 return 1;
203 if (gh->gh_flags & GL_EXACT)
204 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100205 if (gl->gl_state == LM_ST_EXCLUSIVE) {
206 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
207 return 1;
208 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
209 return 1;
210 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100211 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
212 return 1;
213 return 0;
214}
215
216static void gfs2_holder_wake(struct gfs2_holder *gh)
217{
218 clear_bit(HIF_WAIT, &gh->gh_iflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100219 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100220 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
221}
222
223/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100224 * do_error - Something unexpected has happened during a lock request
225 *
226 */
227
Denys Vlasenkoa527b382016-04-12 12:39:12 -0400228static void do_error(struct gfs2_glock *gl, const int ret)
Steven Whitehoused5341a92010-07-23 14:05:51 +0100229{
230 struct gfs2_holder *gh, *tmp;
231
232 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
233 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
234 continue;
235 if (ret & LM_OUT_ERROR)
236 gh->gh_error = -EIO;
237 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
238 gh->gh_error = GLR_TRYFAILED;
239 else
240 continue;
241 list_del_init(&gh->gh_list);
242 trace_gfs2_glock_queue(gh, 0);
243 gfs2_holder_wake(gh);
244 }
245}
246
247/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100248 * do_promote - promote as many requests as possible on the current queue
249 * @gl: The glock
250 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000251 * Returns: 1 if there is a blocked holder at the head of the list, or 2
252 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100253 */
254
255static int do_promote(struct gfs2_glock *gl)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500256__releases(&gl->gl_lockref.lock)
257__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100258{
259 const struct gfs2_glock_operations *glops = gl->gl_ops;
260 struct gfs2_holder *gh, *tmp;
261 int ret;
262
263restart:
264 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
265 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
266 continue;
267 if (may_grant(gl, gh)) {
268 if (gh->gh_list.prev == &gl->gl_holders &&
269 glops->go_lock) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500270 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100271 /* FIXME: eliminate this eventually */
272 ret = glops->go_lock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500273 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100274 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000275 if (ret == 1)
276 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100277 gh->gh_error = ret;
278 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100279 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100280 gfs2_holder_wake(gh);
281 goto restart;
282 }
283 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100284 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100285 gfs2_holder_wake(gh);
286 goto restart;
287 }
288 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100289 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100290 gfs2_holder_wake(gh);
291 continue;
292 }
293 if (gh->gh_list.prev == &gl->gl_holders)
294 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100295 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100296 break;
297 }
298 return 0;
299}
300
301/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100302 * find_first_waiter - find the first gh that's waiting for the glock
303 * @gl: the glock
304 */
305
306static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
307{
308 struct gfs2_holder *gh;
309
310 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
311 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
312 return gh;
313 }
314 return NULL;
315}
316
317/**
318 * state_change - record that the glock is now in a different state
319 * @gl: the glock
320 * @new_state the new state
321 *
322 */
323
324static void state_change(struct gfs2_glock *gl, unsigned int new_state)
325{
326 int held1, held2;
327
328 held1 = (gl->gl_state != LM_ST_UNLOCKED);
329 held2 = (new_state != LM_ST_UNLOCKED);
330
331 if (held1 != held2) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100332 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100333 if (held2)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100334 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100335 else
Steven Whitehousee66cf162013-10-15 15:18:08 +0100336 gl->gl_lockref.count--;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100337 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100338 if (held1 && held2 && list_empty(&gl->gl_holders))
339 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100340
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400341 if (new_state != gl->gl_target)
342 /* shorten our minimum hold time */
343 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
344 GL_GLOCK_MIN_HOLD);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100345 gl->gl_state = new_state;
346 gl->gl_tchange = jiffies;
347}
348
349static void gfs2_demote_wake(struct gfs2_glock *gl)
350{
351 gl->gl_demote_state = LM_ST_EXCLUSIVE;
352 clear_bit(GLF_DEMOTE, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100353 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100354 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
355}
356
357/**
358 * finish_xmote - The DLM has replied to one of our lock requests
359 * @gl: The glock
360 * @ret: The status from the DLM
361 *
362 */
363
364static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
365{
366 const struct gfs2_glock_operations *glops = gl->gl_ops;
367 struct gfs2_holder *gh;
368 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000369 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500370
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500371 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse63997772009-06-12 08:49:20 +0100372 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100373 state_change(gl, state);
374 gh = find_first_waiter(gl);
375
376 /* Demote to UN request arrived during demote to SH or DF */
377 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
378 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
379 gl->gl_target = LM_ST_UNLOCKED;
380
381 /* Check for state != intended state */
382 if (unlikely(state != gl->gl_target)) {
383 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
384 /* move to back of queue and try next entry */
385 if (ret & LM_OUT_CANCELED) {
386 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
387 list_move_tail(&gh->gh_list, &gl->gl_holders);
388 gh = find_first_waiter(gl);
389 gl->gl_target = gh->gh_state;
390 goto retry;
391 }
392 /* Some error or failed "try lock" - report it */
393 if ((ret & LM_OUT_ERROR) ||
394 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
395 gl->gl_target = gl->gl_state;
396 do_error(gl, ret);
397 goto out;
398 }
399 }
400 switch(state) {
401 /* Unlocked due to conversion deadlock, try again */
402 case LM_ST_UNLOCKED:
403retry:
404 do_xmote(gl, gh, gl->gl_target);
405 break;
406 /* Conversion fails, unlock and try again */
407 case LM_ST_SHARED:
408 case LM_ST_DEFERRED:
409 do_xmote(gl, gh, LM_ST_UNLOCKED);
410 break;
411 default: /* Everything else */
Joe Perchesd77d1b52014-03-06 12:10:45 -0800412 pr_err("wanted %u got %u\n", gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100413 GLOCK_BUG_ON(gl, 1);
414 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500415 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100416 return;
417 }
418
419 /* Fast path - we got what we asked for */
420 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
421 gfs2_demote_wake(gl);
422 if (state != LM_ST_UNLOCKED) {
423 if (glops->go_xmote_bh) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500424 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100425 rv = glops->go_xmote_bh(gl, gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500426 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100427 if (rv) {
428 do_error(gl, rv);
429 goto out;
430 }
431 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000432 rv = do_promote(gl);
433 if (rv == 2)
434 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100435 }
436out:
437 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000438out_locked:
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500439 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500440}
441
Steven Whitehouse6802e342008-05-21 17:03:22 +0100442/**
443 * do_xmote - Calls the DLM to change the state of a lock
444 * @gl: The lock state
445 * @gh: The holder (only for promotes)
446 * @target: The target lock state
447 *
448 */
449
450static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500451__releases(&gl->gl_lockref.lock)
452__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100453{
454 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500455 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Bob Petersonb58bf402015-07-24 09:45:43 -0500456 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100457 int ret;
458
459 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
460 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000461 GLOCK_BUG_ON(gl, gl->gl_state == target);
462 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100463 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
464 glops->go_inval) {
465 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
466 do_error(gl, 0); /* Fail queued try locks */
467 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000468 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000469 set_bit(GLF_BLOCKING, &gl->gl_flags);
470 if ((gl->gl_req == LM_ST_UNLOCKED) ||
471 (gl->gl_state == LM_ST_EXCLUSIVE) ||
472 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
473 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500474 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson06dfc302012-10-24 14:41:05 -0400475 if (glops->go_sync)
476 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100477 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
478 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
479 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
480
481 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000482 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
483 /* lock_dlm */
484 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400485 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
486 target == LM_ST_UNLOCKED &&
487 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
488 finish_xmote(gl, target);
489 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
490 gfs2_glock_put(gl);
491 }
492 else if (ret) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800493 pr_err("lm_lock ret %d\n", ret);
David Teiglanddba2d702012-11-14 13:46:53 -0500494 GLOCK_BUG_ON(gl, 1);
495 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000496 } else { /* lock_nolock */
497 finish_xmote(gl, target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100498 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
499 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100500 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000501
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500502 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100503}
504
505/**
506 * find_first_holder - find the first "holder" gh
507 * @gl: the glock
508 */
509
510static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
511{
512 struct gfs2_holder *gh;
513
514 if (!list_empty(&gl->gl_holders)) {
515 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
516 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
517 return gh;
518 }
519 return NULL;
520}
521
522/**
523 * run_queue - do all outstanding tasks related to a glock
524 * @gl: The glock in question
525 * @nonblock: True if we must not block in run_queue
526 *
527 */
528
529static void run_queue(struct gfs2_glock *gl, const int nonblock)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500530__releases(&gl->gl_lockref.lock)
531__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100532{
533 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000534 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100535
536 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
537 return;
538
539 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
540
541 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
542 gl->gl_demote_state != gl->gl_state) {
543 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000544 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100545 if (nonblock)
546 goto out_sched;
547 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100548 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100549 gl->gl_target = gl->gl_demote_state;
550 } else {
551 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
552 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000553 ret = do_promote(gl);
554 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000555 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000556 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100557 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100558 gh = find_first_waiter(gl);
559 gl->gl_target = gh->gh_state;
560 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
561 do_error(gl, 0); /* Fail queued try locks */
562 }
563 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100564out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100565 return;
566
567out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100568 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100569 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100570 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100571 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100572 gl->gl_lockref.count--;
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100573 return;
574
Steven Whitehoused8348de2009-02-05 10:12:38 +0000575out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100576 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100577 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100578 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100579}
580
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500581static void delete_work_func(struct work_struct *work)
582{
583 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500584 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500585 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000586 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500587
Bob Petersona4923862015-12-07 16:24:27 -0600588 /* If someone's using this glock to create a new dinode, the block must
589 have been freed by another node, then re-used, in which case our
590 iopen callback is too late after the fact. Ignore it. */
591 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
592 goto out;
593
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500594 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000595 if (inode && !IS_ERR(inode)) {
596 d_prune_aliases(inode);
597 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500598 }
Bob Petersona4923862015-12-07 16:24:27 -0600599out:
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500600 gfs2_glock_put(gl);
601}
602
Steven Whitehouse6802e342008-05-21 17:03:22 +0100603static void glock_work_func(struct work_struct *work)
604{
605 unsigned long delay = 0;
606 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000607 int drop_ref = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100608
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000609 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100610 finish_xmote(gl, gl->gl_reply);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000611 drop_ref = 1;
612 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500613 spin_lock(&gl->gl_lockref.lock);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400614 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100615 gl->gl_state != LM_ST_UNLOCKED &&
616 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100617 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400618
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400619 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100620 if (time_before(now, holdtime))
621 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400622
623 if (!delay) {
624 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
625 set_bit(GLF_DEMOTE, &gl->gl_flags);
626 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100627 }
628 run_queue(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500629 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400630 if (!delay)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100631 gfs2_glock_put(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400632 else {
633 if (gl->gl_name.ln_type != LM_TYPE_INODE)
634 delay = 0;
635 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
636 gfs2_glock_put(gl);
637 }
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000638 if (drop_ref)
639 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100640}
641
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642/**
643 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
644 * @sdp: The GFS2 superblock
645 * @number: the lock number
646 * @glops: The glock_operations to use
647 * @create: If 0, don't create the glock if it doesn't exist
648 * @glp: the glock is returned here
649 *
650 * This does not lock a glock, just finds/creates structures for one.
651 *
652 * Returns: errno
653 */
654
Steven Whitehousecd915492006-09-04 12:49:07 -0400655int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400656 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 struct gfs2_glock **glp)
658{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000659 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500660 struct lm_lockname name = { .ln_number = number,
661 .ln_type = glops->go_type,
662 .ln_sbd = sdp };
Bob Peterson88ffbf32015-03-16 11:02:46 -0500663 struct gfs2_glock *gl, *tmp = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000664 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000665 struct kmem_cache *cachep;
Bob Peterson88ffbf32015-03-16 11:02:46 -0500666 int ret, tries = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667
Andrew Price4cf918c2017-02-22 12:05:03 -0500668 rcu_read_lock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500669 gl = rhashtable_lookup_fast(&gl_hash_table, &name, ht_parms);
670 if (gl && !lockref_get_not_dead(&gl->gl_lockref))
671 gl = NULL;
Andrew Price4cf918c2017-02-22 12:05:03 -0500672 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000674 *glp = gl;
675 if (gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676 return 0;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000677 if (!create)
678 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679
Steven Whitehouse009d8512009-12-08 12:12:13 +0000680 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000681 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000682 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000683 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100684 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 if (!gl)
686 return -ENOMEM;
687
David Teiglanddba2d702012-11-14 13:46:53 -0500688 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500689
690 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100691 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500692 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500693 kmem_cache_free(cachep, gl);
694 return -ENOMEM;
695 }
David Teiglanddba2d702012-11-14 13:46:53 -0500696 }
697
Steven Whitehouse8f052282010-01-29 15:21:27 +0000698 atomic_inc(&sdp->sd_glock_disposal);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500699 gl->gl_node.next = NULL;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400700 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100702 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100704 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500705 gl->gl_demote_state = LM_ST_EXCLUSIVE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 gl->gl_ops = glops;
Steven Whitehousea2457692012-01-20 10:38:36 +0000707 gl->gl_dstamp = ktime_set(0, 0);
708 preempt_disable();
709 /* We use the global stats to estimate the initial per-glock stats */
710 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
711 preempt_enable();
712 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
713 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500714 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400715 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400716 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500717 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500718 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719
Steven Whitehouse009d8512009-12-08 12:12:13 +0000720 mapping = gfs2_glock2aspace(gl);
721 if (mapping) {
722 mapping->a_ops = &gfs2_meta_aops;
723 mapping->host = s->s_bdev->bd_inode;
724 mapping->flags = 0;
725 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800726 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000727 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728 }
729
Bob Peterson88ffbf32015-03-16 11:02:46 -0500730again:
731 ret = rhashtable_lookup_insert_fast(&gl_hash_table, &gl->gl_node,
732 ht_parms);
733 if (ret == 0) {
734 *glp = gl;
735 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 }
737
Bob Peterson88ffbf32015-03-16 11:02:46 -0500738 if (ret == -EEXIST) {
739 ret = 0;
Andrew Price4cf918c2017-02-22 12:05:03 -0500740 rcu_read_lock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500741 tmp = rhashtable_lookup_fast(&gl_hash_table, &name, ht_parms);
742 if (tmp == NULL || !lockref_get_not_dead(&tmp->gl_lockref)) {
743 if (++tries < 100) {
Andrew Price4cf918c2017-02-22 12:05:03 -0500744 rcu_read_unlock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500745 cond_resched();
746 goto again;
747 }
748 tmp = NULL;
749 ret = -ENOMEM;
750 }
Andrew Price4cf918c2017-02-22 12:05:03 -0500751 rcu_read_unlock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500752 } else {
753 WARN_ON_ONCE(ret);
754 }
755 kfree(gl->gl_lksb.sb_lvbptr);
756 kmem_cache_free(cachep, gl);
757 atomic_dec(&sdp->sd_glock_disposal);
758 *glp = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759
Bob Peterson88ffbf32015-03-16 11:02:46 -0500760 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761}
762
763/**
764 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
765 * @gl: the glock
766 * @state: the state we're requesting
767 * @flags: the modifier flags
768 * @gh: the holder structure
769 *
770 */
771
Bob Petersonb58bf402015-07-24 09:45:43 -0500772void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 struct gfs2_holder *gh)
774{
775 INIT_LIST_HEAD(&gh->gh_list);
776 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200777 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800778 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 gh->gh_state = state;
780 gh->gh_flags = flags;
781 gh->gh_error = 0;
782 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 gfs2_glock_hold(gl);
784}
785
786/**
787 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
788 * @state: the state we're requesting
789 * @flags: the modifier flags
790 * @gh: the holder structure
791 *
792 * Don't mess with the glock.
793 *
794 */
795
Bob Petersonb58bf402015-07-24 09:45:43 -0500796void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797{
798 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400799 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000800 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200801 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100802 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400803 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804}
805
806/**
807 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
808 * @gh: the holder structure
809 *
810 */
811
812void gfs2_holder_uninit(struct gfs2_holder *gh)
813{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800814 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 gfs2_glock_put(gh->gh_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500816 gfs2_holder_mark_uninitialized(gh);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500817 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818}
819
Steven Whitehousefe64d512009-05-19 10:01:18 +0100820/**
Bob Peterson07a79042012-08-09 12:48:44 -0500821 * gfs2_glock_wait - wait on a glock acquisition
822 * @gh: the glock holder
823 *
824 * Returns: 0 on success
825 */
826
827int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000828{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400829 unsigned long time1 = jiffies;
830
Steven Whitehousefee852e2007-01-17 15:33:23 +0000831 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000832 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400833 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
834 /* Lengthen the minimum hold time. */
835 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
836 GL_GLOCK_HOLD_INCR,
837 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500838 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100839}
840
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000842 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 * @gl: the glock
844 * @state: the state the caller wants us to change to
845 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000846 * There are only two requests that we are going to see in actual
847 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 */
849
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500850static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100851 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500853 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
854
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500855 set_bit(bit, &gl->gl_flags);
856 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000857 gl->gl_demote_state = state;
858 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100859 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
860 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100861 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500863 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100864 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100865 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866}
867
Steven Whitehouse6802e342008-05-21 17:03:22 +0100868void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000869{
Joe Perches5e690692010-11-09 16:35:20 -0800870 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000871 va_list args;
872
873 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800874
Steven Whitehouse6802e342008-05-21 17:03:22 +0100875 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100876 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100877 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800878 vaf.fmt = fmt;
879 vaf.va = &args;
880
Joe Perchesd77d1b52014-03-06 12:10:45 -0800881 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100882 }
Joe Perches5e690692010-11-09 16:35:20 -0800883
Robert Peterson7c52b162007-03-16 10:26:37 +0000884 va_end(args);
885}
886
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 * add_to_queue - Add a holder to the wait queue (but look for recursion)
889 * @gh: the holder structure to add
890 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100891 * Eventually we should move the recursive locking trap to a
892 * debugging option or something like that. This is the fast
893 * path and needs to have the minimum number of distractions.
894 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 */
896
Steven Whitehouse6802e342008-05-21 17:03:22 +0100897static inline void add_to_queue(struct gfs2_holder *gh)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500898__releases(&gl->gl_lockref.lock)
899__acquires(&gl->gl_lockref.lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900{
901 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -0500902 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100903 struct list_head *insert_pt = NULL;
904 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -0500905 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800907 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000908 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
909 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -0400910
Steven Whitehouse6802e342008-05-21 17:03:22 +0100911 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
912 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -0500913 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100914 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
915 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916 }
917
Steven Whitehouse6802e342008-05-21 17:03:22 +0100918 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
919 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
920 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
921 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -0500922 if (try_futile &&
923 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100924fail:
925 gh->gh_error = GLR_TRYFAILED;
926 gfs2_holder_wake(gh);
927 return;
928 }
929 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
930 continue;
931 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
932 insert_pt = &gh2->gh_list;
933 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100934 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +0000935 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +0000936 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
937 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100938 if (likely(insert_pt == NULL)) {
939 list_add_tail(&gh->gh_list, &gl->gl_holders);
940 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
941 goto do_cancel;
942 return;
943 }
944 list_add_tail(&gh->gh_list, insert_pt);
945do_cancel:
946 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
947 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500948 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100949 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000950 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500951 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100952 }
953 return;
954
955trap_recursive:
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800956 pr_err("original: %pSR\n", (void *)gh2->gh_ip);
957 pr_err("pid: %d\n", pid_nr(gh2->gh_owner_pid));
958 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +0100959 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800960 pr_err("new: %pSR\n", (void *)gh->gh_ip);
961 pr_err("pid: %d\n", pid_nr(gh->gh_owner_pid));
962 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +0100963 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +0100964 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100965 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966}
967
968/**
969 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
970 * @gh: the holder structure
971 *
972 * if (gh->gh_flags & GL_ASYNC), this never returns an error
973 *
974 * Returns: 0, GLR_TRYFAILED, or errno on failure
975 */
976
977int gfs2_glock_nq(struct gfs2_holder *gh)
978{
979 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -0500980 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 int error = 0;
982
Steven Whitehouse6802e342008-05-21 17:03:22 +0100983 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985
Steven Whitehousef42ab082011-04-14 16:50:31 +0100986 if (test_bit(GLF_LRU, &gl->gl_flags))
987 gfs2_glock_remove_from_lru(gl);
988
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500989 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -0400991 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
992 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +0100993 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -0400994 gl->gl_lockref.count++;
995 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
996 gl->gl_lockref.count--;
997 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100998 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500999 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000
Steven Whitehouse6802e342008-05-21 17:03:22 +01001001 if (!(gh->gh_flags & GL_ASYNC))
1002 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 return error;
1005}
1006
1007/**
1008 * gfs2_glock_poll - poll to see if an async request has been completed
1009 * @gh: the holder
1010 *
1011 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1012 */
1013
1014int gfs2_glock_poll(struct gfs2_holder *gh)
1015{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001016 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017}
1018
1019/**
1020 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1021 * @gh: the glock holder
1022 *
1023 */
1024
1025void gfs2_glock_dq(struct gfs2_holder *gh)
1026{
1027 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001028 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001029 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001030 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001032 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001034 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036 list_del_init(&gh->gh_list);
Bob Peterson7508abc2015-12-18 11:54:55 -06001037 clear_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001038 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001039 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001040 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001041 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 glops->go_unlock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001043 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001044 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001045 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001046 if (list_empty(&gl->gl_holders) &&
1047 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1048 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1049 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 }
Bob Petersone7ccaf52015-06-12 13:15:54 -05001051 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl) &&
1052 (glops->go_flags & GLOF_LRU))
Bob Peterson4abb6ad2012-08-09 12:48:43 -05001053 gfs2_glock_add_to_lru(gl);
1054
Steven Whitehouse63997772009-06-12 08:49:20 +01001055 trace_gfs2_glock_queue(gh, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001056 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001057 if (likely(fast_path))
1058 return;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001059
1060 gfs2_glock_hold(gl);
1061 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001062 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1063 gl->gl_name.ln_type == LM_TYPE_INODE)
1064 delay = gl->gl_hold_time;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001065 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1066 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067}
1068
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001069void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1070{
1071 struct gfs2_glock *gl = gh->gh_gl;
1072 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001073 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001074 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001075}
1076
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001078 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1079 * @gh: the holder structure
1080 *
1081 */
1082
1083void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1084{
1085 gfs2_glock_dq(gh);
1086 gfs2_holder_uninit(gh);
1087}
1088
1089/**
1090 * gfs2_glock_nq_num - acquire a glock based on lock number
1091 * @sdp: the filesystem
1092 * @number: the lock number
1093 * @glops: the glock operations for the type of glock
1094 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001095 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 * @gh: the struct gfs2_holder
1097 *
1098 * Returns: errno
1099 */
1100
Steven Whitehousecd915492006-09-04 12:49:07 -04001101int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001102 const struct gfs2_glock_operations *glops,
Bob Petersonb58bf402015-07-24 09:45:43 -05001103 unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104{
1105 struct gfs2_glock *gl;
1106 int error;
1107
1108 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1109 if (!error) {
1110 error = gfs2_glock_nq_init(gl, state, flags, gh);
1111 gfs2_glock_put(gl);
1112 }
1113
1114 return error;
1115}
1116
1117/**
1118 * glock_compare - Compare two struct gfs2_glock structures for sorting
1119 * @arg_a: the first structure
1120 * @arg_b: the second structure
1121 *
1122 */
1123
1124static int glock_compare(const void *arg_a, const void *arg_b)
1125{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001126 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1127 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1128 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1129 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130
1131 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001132 return 1;
1133 if (a->ln_number < b->ln_number)
1134 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001135 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 -04001136 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001137}
1138
1139/**
1140 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1141 * @num_gh: the number of structures
1142 * @ghs: an array of struct gfs2_holder structures
1143 *
1144 * Returns: 0 on success (all glocks acquired),
1145 * errno on failure (no glocks acquired)
1146 */
1147
1148static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1149 struct gfs2_holder **p)
1150{
1151 unsigned int x;
1152 int error = 0;
1153
1154 for (x = 0; x < num_gh; x++)
1155 p[x] = &ghs[x];
1156
1157 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1158
1159 for (x = 0; x < num_gh; x++) {
1160 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1161
1162 error = gfs2_glock_nq(p[x]);
1163 if (error) {
1164 while (x--)
1165 gfs2_glock_dq(p[x]);
1166 break;
1167 }
1168 }
1169
1170 return error;
1171}
1172
1173/**
1174 * gfs2_glock_nq_m - acquire multiple glocks
1175 * @num_gh: the number of structures
1176 * @ghs: an array of struct gfs2_holder structures
1177 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001178 *
1179 * Returns: 0 on success (all glocks acquired),
1180 * errno on failure (no glocks acquired)
1181 */
1182
1183int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1184{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001185 struct gfs2_holder *tmp[4];
1186 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001187 int error = 0;
1188
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001189 switch(num_gh) {
1190 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001191 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001192 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1194 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001195 default:
1196 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001198 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1199 if (!pph)
1200 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 }
1202
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001203 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001205 if (pph != tmp)
1206 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207
1208 return error;
1209}
1210
1211/**
1212 * gfs2_glock_dq_m - release multiple glocks
1213 * @num_gh: the number of structures
1214 * @ghs: an array of struct gfs2_holder structures
1215 *
1216 */
1217
1218void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1219{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001220 while (num_gh--)
1221 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222}
1223
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001224void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001225{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001226 unsigned long delay = 0;
1227 unsigned long holdtime;
1228 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001230 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001231 holdtime = gl->gl_tchange + gl->gl_hold_time;
1232 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1233 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001234 if (time_before(now, holdtime))
1235 delay = holdtime - now;
1236 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001237 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001238 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001239
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001240 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001241 handle_callback(gl, state, delay, true);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001242 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001243 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1244 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001245}
1246
1247/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001248 * gfs2_should_freeze - Figure out if glock should be frozen
1249 * @gl: The glock in question
1250 *
1251 * Glocks are not frozen if (a) the result of the dlm operation is
1252 * an error, (b) the locking operation was an unlock operation or
1253 * (c) if there is a "noexp" flagged request anywhere in the queue
1254 *
1255 * Returns: 1 if freezing should occur, 0 otherwise
1256 */
1257
1258static int gfs2_should_freeze(const struct gfs2_glock *gl)
1259{
1260 const struct gfs2_holder *gh;
1261
1262 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1263 return 0;
1264 if (gl->gl_target == LM_ST_UNLOCKED)
1265 return 0;
1266
1267 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1268 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1269 continue;
1270 if (LM_FLAG_NOEXP & gh->gh_flags)
1271 return 0;
1272 }
1273
1274 return 1;
1275}
1276
1277/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001278 * gfs2_glock_complete - Callback used by locking
1279 * @gl: Pointer to the glock
1280 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001282 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
Steven Whitehouse47a25382010-11-30 15:49:31 +00001283 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001284 */
1285
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001286void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287{
Bob Peterson15562c42015-03-16 11:52:05 -05001288 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001289
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001290 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001291 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001292
David Teiglande0c2a9a2012-01-09 17:18:05 -05001293 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001294 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001295 set_bit(GLF_FROZEN, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001296 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001297 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001298 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001299 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001300
Steven Whitehousee66cf162013-10-15 15:18:08 +01001301 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001302 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001303 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001304
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001305 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1306 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307}
1308
Steven Whitehouse4506a512013-02-01 20:36:03 +00001309static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1310{
1311 struct gfs2_glock *gla, *glb;
1312
1313 gla = list_entry(a, struct gfs2_glock, gl_lru);
1314 glb = list_entry(b, struct gfs2_glock, gl_lru);
1315
1316 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1317 return 1;
1318 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1319 return -1;
1320
1321 return 0;
1322}
1323
1324/**
1325 * gfs2_dispose_glock_lru - Demote a list of glocks
1326 * @list: The list to dispose of
1327 *
1328 * Disposing of glocks may involve disk accesses, so that here we sort
1329 * the glocks by number (i.e. disk location of the inodes) so that if
1330 * there are any such accesses, they'll be sent in order (mostly).
1331 *
1332 * Must be called under the lru_lock, but may drop and retake this
1333 * lock. While the lru_lock is dropped, entries may vanish from the
1334 * list, but no new entries will appear on the list (since it is
1335 * private)
1336 */
1337
1338static void gfs2_dispose_glock_lru(struct list_head *list)
1339__releases(&lru_lock)
1340__acquires(&lru_lock)
1341{
1342 struct gfs2_glock *gl;
1343
1344 list_sort(NULL, list, glock_cmp);
1345
1346 while(!list_empty(list)) {
1347 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1348 list_del_init(&gl->gl_lru);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001349 if (!spin_trylock(&gl->gl_lockref.lock)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001350add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001351 list_add(&gl->gl_lru, &lru_list);
1352 atomic_inc(&lru_count);
1353 continue;
1354 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001355 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001356 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001357 goto add_back_to_lru;
1358 }
Steven Whitehouse4506a512013-02-01 20:36:03 +00001359 clear_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001360 gl->gl_lockref.count++;
Steven Whitehouse4506a512013-02-01 20:36:03 +00001361 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001362 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001363 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse4506a512013-02-01 20:36:03 +00001364 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +01001365 gl->gl_lockref.count--;
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001366 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001367 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001368 }
1369}
1370
Steven Whitehouse2a005852012-12-14 12:28:30 +00001371/**
1372 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1373 * @nr: The number of entries to scan
1374 *
Steven Whitehouse4506a512013-02-01 20:36:03 +00001375 * This function selects the entries on the LRU which are able to
1376 * be demoted, and then kicks off the process by calling
1377 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001378 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001379
Dave Chinner1ab6c492013-08-28 10:18:09 +10001380static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001381{
1382 struct gfs2_glock *gl;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001383 LIST_HEAD(skipped);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001384 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001385 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001386
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001387 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001388 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001389 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001390
1391 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001392 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a512013-02-01 20:36:03 +00001393 list_move(&gl->gl_lru, &dispose);
1394 atomic_dec(&lru_count);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001395 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001396 continue;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001397 }
Steven Whitehouse4506a512013-02-01 20:36:03 +00001398
1399 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001401 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001402 if (!list_empty(&dispose))
1403 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001404 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001405
1406 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001407}
1408
Dave Chinner1ab6c492013-08-28 10:18:09 +10001409static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1410 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001411{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001412 if (!(sc->gfp_mask & __GFP_FS))
1413 return SHRINK_STOP;
1414 return gfs2_scan_glock_lru(sc->nr_to_scan);
1415}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001416
Dave Chinner1ab6c492013-08-28 10:18:09 +10001417static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1418 struct shrink_control *sc)
1419{
Glauber Costa55f841c2013-08-28 10:17:53 +10001420 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001421}
1422
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001423static struct shrinker glock_shrinker = {
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001424 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001425 .count_objects = gfs2_glock_shrink_count,
1426 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001427};
1428
David Teiglandb3b94fa2006-01-16 16:50:04 +00001429/**
1430 * examine_bucket - Call a function for glock in a hash bucket
1431 * @examiner: the function
1432 * @sdp: the filesystem
1433 * @bucket: the bucket
1434 *
Herbert Xu25579692017-05-23 21:53:36 -04001435 * Note that the function can be called multiple times on the same
1436 * object. So the user must ensure that the function can cope with
1437 * that.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001438 */
1439
Bob Peterson88ffbf32015-03-16 11:02:46 -05001440static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001441{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001442 struct gfs2_glock *gl;
Herbert Xu25579692017-05-23 21:53:36 -04001443 struct rhashtable_iter iter;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001444
Herbert Xu25579692017-05-23 21:53:36 -04001445 rhashtable_walk_enter(&gl_hash_table, &iter);
1446
1447 do {
1448 gl = ERR_PTR(rhashtable_walk_start(&iter));
1449 if (gl)
1450 continue;
1451
1452 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl))
Bob Peterson88ffbf32015-03-16 11:02:46 -05001453 if ((gl->gl_name.ln_sbd == sdp) &&
1454 lockref_get_not_dead(&gl->gl_lockref))
1455 examiner(gl);
Herbert Xu25579692017-05-23 21:53:36 -04001456
1457 rhashtable_walk_stop(&iter);
1458 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1459
1460 rhashtable_walk_exit(&iter);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001461}
1462
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001463/**
1464 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1465 * @gl: The glock to thaw
1466 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001467 */
1468
1469static void thaw_glock(struct gfs2_glock *gl)
1470{
1471 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
Steven Whitehouse7286b312013-08-20 09:35:09 +01001472 goto out;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001473 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse7286b312013-08-20 09:35:09 +01001474 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) {
1475out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001476 gfs2_glock_put(gl);
Steven Whitehouse7286b312013-08-20 09:35:09 +01001477 }
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001478}
1479
David Teiglandb3b94fa2006-01-16 16:50:04 +00001480/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001481 * clear_glock - look at a glock and see if we can free it from glock cache
1482 * @gl: the glock to look at
1483 *
1484 */
1485
1486static void clear_glock(struct gfs2_glock *gl)
1487{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001488 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001490 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousec741c452010-09-29 14:20:52 +01001491 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001492 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001493 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001494 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1495 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001496}
1497
1498/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001499 * gfs2_glock_thaw - Thaw any frozen glocks
1500 * @sdp: The super block
1501 *
1502 */
1503
1504void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1505{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001506 glock_hash_walk(thaw_glock, sdp);
1507}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001508
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001509static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001510{
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001511 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001512 gfs2_dump_glock(seq, gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001513 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001514}
1515
1516static void dump_glock_func(struct gfs2_glock *gl)
1517{
1518 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001519}
1520
1521/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001522 * gfs2_gl_hash_clear - Empty out the glock hash table
1523 * @sdp: the filesystem
1524 * @wait: wait until it's all gone
1525 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001526 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001527 */
1528
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001529void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001530{
David Teiglandfb6791d2012-11-13 10:58:56 -05001531 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001532 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001533 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001534 flush_workqueue(glock_workqueue);
Bob Peterson2aba1b52015-05-19 09:11:23 -05001535 wait_event_timeout(sdp->sd_glock_wait,
1536 atomic_read(&sdp->sd_glock_disposal) == 0,
1537 HZ * 600);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001538 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001539}
1540
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001541void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1542{
1543 struct gfs2_glock *gl = ip->i_gl;
1544 int ret;
1545
1546 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001547 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001548
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001549 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001550 clear_bit(GLF_LOCK, &gl->gl_flags);
1551 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001552 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001553}
1554
Steven Whitehouse6802e342008-05-21 17:03:22 +01001555static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001556{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001557 switch(state) {
1558 case LM_ST_UNLOCKED:
1559 return "UN";
1560 case LM_ST_SHARED:
1561 return "SH";
1562 case LM_ST_DEFERRED:
1563 return "DF";
1564 case LM_ST_EXCLUSIVE:
1565 return "EX";
1566 }
1567 return "??";
1568}
Robert Peterson04b933f2007-03-23 17:05:15 -05001569
Bob Petersonb58bf402015-07-24 09:45:43 -05001570static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
Steven Whitehouse6802e342008-05-21 17:03:22 +01001571{
1572 char *p = buf;
1573 if (flags & LM_FLAG_TRY)
1574 *p++ = 't';
1575 if (flags & LM_FLAG_TRY_1CB)
1576 *p++ = 'T';
1577 if (flags & LM_FLAG_NOEXP)
1578 *p++ = 'e';
1579 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001580 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001581 if (flags & LM_FLAG_PRIORITY)
1582 *p++ = 'p';
1583 if (flags & GL_ASYNC)
1584 *p++ = 'a';
1585 if (flags & GL_EXACT)
1586 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001587 if (flags & GL_NOCACHE)
1588 *p++ = 'c';
1589 if (test_bit(HIF_HOLDER, &iflags))
1590 *p++ = 'H';
1591 if (test_bit(HIF_WAIT, &iflags))
1592 *p++ = 'W';
1593 if (test_bit(HIF_FIRST, &iflags))
1594 *p++ = 'F';
1595 *p = 0;
1596 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001597}
1598
David Teiglandb3b94fa2006-01-16 16:50:04 +00001599/**
1600 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001601 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001602 * @gh: the glock holder
1603 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001604 */
1605
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001606static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001607{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001608 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001609 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001610
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001611 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001612 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001613 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001614 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1615 state2str(gh->gh_state),
1616 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1617 gh->gh_error,
1618 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1619 gh_owner ? gh_owner->comm : "(ended)",
1620 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001621 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001622}
1623
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001624static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001625{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001626 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001627 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001628
Steven Whitehouse6802e342008-05-21 17:03:22 +01001629 if (test_bit(GLF_LOCK, gflags))
1630 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001631 if (test_bit(GLF_DEMOTE, gflags))
1632 *p++ = 'D';
1633 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1634 *p++ = 'd';
1635 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1636 *p++ = 'p';
1637 if (test_bit(GLF_DIRTY, gflags))
1638 *p++ = 'y';
1639 if (test_bit(GLF_LFLUSH, gflags))
1640 *p++ = 'f';
1641 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1642 *p++ = 'i';
1643 if (test_bit(GLF_REPLY_PENDING, gflags))
1644 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001645 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001646 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001647 if (test_bit(GLF_FROZEN, gflags))
1648 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001649 if (test_bit(GLF_QUEUED, gflags))
1650 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001651 if (test_bit(GLF_LRU, gflags))
1652 *p++ = 'L';
1653 if (gl->gl_object)
1654 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001655 if (test_bit(GLF_BLOCKING, gflags))
1656 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001657 *p = 0;
1658 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001659}
1660
1661/**
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +01001662 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001663 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001664 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001665 *
1666 * The file format is as follows:
1667 * One line per object, capital letters are used to indicate objects
1668 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1669 * other objects are indented by a single space and follow the glock to
1670 * which they are related. Fields are indicated by lower case letters
1671 * followed by a colon and the field value, except for strings which are in
1672 * [] so that its possible to see if they are composed of spaces for
1673 * example. The field's are n = number (id of the object), f = flags,
1674 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001675 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001676 */
1677
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001678void gfs2_dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001680 const struct gfs2_glock_operations *glops = gl->gl_ops;
1681 unsigned long long dtime;
1682 const struct gfs2_holder *gh;
1683 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001684
Steven Whitehouse6802e342008-05-21 17:03:22 +01001685 dtime = jiffies - gl->gl_demote_time;
1686 dtime *= 1000000/HZ; /* demote time in uSec */
1687 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1688 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001689 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 +01001690 state2str(gl->gl_state),
1691 gl->gl_name.ln_type,
1692 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001693 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001694 state2str(gl->gl_target),
1695 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001696 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001697 atomic_read(&gl->gl_revokes),
Steven Whitehousee66cf162013-10-15 15:18:08 +01001698 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001699
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001700 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1701 dump_holder(seq, gh);
1702
Steven Whitehouse6802e342008-05-21 17:03:22 +01001703 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001704 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001705}
1706
Steven Whitehousea2457692012-01-20 10:38:36 +00001707static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1708{
1709 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001710
Ben Hutchings4d207132015-08-27 12:51:45 -05001711 seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
Steven Whitehousea2457692012-01-20 10:38:36 +00001712 gl->gl_name.ln_type,
1713 (unsigned long long)gl->gl_name.ln_number,
Ben Hutchings4d207132015-08-27 12:51:45 -05001714 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1715 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1716 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1717 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1718 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1719 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1720 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1721 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001722 return 0;
1723}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001724
Steven Whitehousea2457692012-01-20 10:38:36 +00001725static const char *gfs2_gltype[] = {
1726 "type",
1727 "reserved",
1728 "nondisk",
1729 "inode",
1730 "rgrp",
1731 "meta",
1732 "iopen",
1733 "flock",
1734 "plock",
1735 "quota",
1736 "journal",
1737};
1738
1739static const char *gfs2_stype[] = {
1740 [GFS2_LKS_SRTT] = "srtt",
1741 [GFS2_LKS_SRTTVAR] = "srttvar",
1742 [GFS2_LKS_SRTTB] = "srttb",
1743 [GFS2_LKS_SRTTVARB] = "srttvarb",
1744 [GFS2_LKS_SIRT] = "sirt",
1745 [GFS2_LKS_SIRTVAR] = "sirtvar",
1746 [GFS2_LKS_DCOUNT] = "dlm",
1747 [GFS2_LKS_QCOUNT] = "queue",
1748};
1749
1750#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1751
1752static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1753{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001754 struct gfs2_sbd *sdp = seq->private;
1755 loff_t pos = *(loff_t *)iter_ptr;
1756 unsigned index = pos >> 3;
1757 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001758 int i;
1759
1760 if (index == 0 && subindex != 0)
1761 return 0;
1762
1763 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1764 (index == 0) ? "cpu": gfs2_stype[subindex]);
1765
1766 for_each_possible_cpu(i) {
1767 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
Andreas Gruenbacher8f7e0a82015-08-27 13:02:54 -05001768
1769 if (index == 0)
1770 seq_printf(seq, " %15u", i);
1771 else
1772 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1773 lkstats[index - 1].stats[subindex]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001774 }
1775 seq_putc(seq, '\n');
1776 return 0;
1777}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001778
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001779int __init gfs2_glock_init(void)
1780{
Bob Peterson88ffbf32015-03-16 11:02:46 -05001781 int ret;
1782
1783 ret = rhashtable_init(&gl_hash_table, &ht_parms);
1784 if (ret < 0)
1785 return ret;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001786
Steven Whitehoused2115772010-11-03 19:58:53 +00001787 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001788 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001789 if (!glock_workqueue) {
1790 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001791 return -ENOMEM;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001792 }
Steven Whitehoused2115772010-11-03 19:58:53 +00001793 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001794 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001795 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001796 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001797 destroy_workqueue(glock_workqueue);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001798 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001799 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001800 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001801
Chao Yue0d735c2016-09-21 12:09:40 -05001802 ret = register_shrinker(&glock_shrinker);
1803 if (ret) {
1804 destroy_workqueue(gfs2_delete_workqueue);
1805 destroy_workqueue(glock_workqueue);
1806 rhashtable_destroy(&gl_hash_table);
1807 return ret;
1808 }
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001809
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001810 return 0;
1811}
1812
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001813void gfs2_glock_exit(void)
1814{
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001815 unregister_shrinker(&glock_shrinker);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001816 rhashtable_destroy(&gl_hash_table);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001817 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001818 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001819}
1820
Bob Peterson88ffbf32015-03-16 11:02:46 -05001821static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001822{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001823 do {
Bob Peterson88ffbf32015-03-16 11:02:46 -05001824 gi->gl = rhashtable_walk_next(&gi->hti);
1825 if (IS_ERR(gi->gl)) {
1826 if (PTR_ERR(gi->gl) == -EAGAIN)
1827 continue;
1828 gi->gl = NULL;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001829 }
1830 /* Skip entries for other sb and dead entries */
Bob Peterson88ffbf32015-03-16 11:02:46 -05001831 } while ((gi->gl) && ((gi->sdp != gi->gl->gl_name.ln_sbd) ||
1832 __lockref_is_dead(&gi->gl->gl_lockref)));
Robert Peterson7c52b162007-03-16 10:26:37 +00001833}
1834
Steven Whitehouse6802e342008-05-21 17:03:22 +01001835static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Robert Peterson7c52b162007-03-16 10:26:37 +00001836{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001837 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001838 loff_t n = *pos;
1839
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001840 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
1841 if (rhashtable_walk_start(&gi->hti) != 0)
Bob Peterson88ffbf32015-03-16 11:02:46 -05001842 return NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001843
Steven Whitehouse6802e342008-05-21 17:03:22 +01001844 do {
Bob Peterson88ffbf32015-03-16 11:02:46 -05001845 gfs2_glock_iter_next(gi);
1846 } while (gi->gl && n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001847
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001848 gi->last_pos = *pos;
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001849
Steven Whitehouse6802e342008-05-21 17:03:22 +01001850 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001851}
1852
Steven Whitehouse6802e342008-05-21 17:03:22 +01001853static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001854 loff_t *pos)
1855{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001856 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001857
1858 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001859 gi->last_pos = *pos;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001860 gfs2_glock_iter_next(gi);
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001861
Steven Whitehouse6802e342008-05-21 17:03:22 +01001862 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001863}
1864
Steven Whitehouse6802e342008-05-21 17:03:22 +01001865static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001866{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001867 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001868
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001869 gi->gl = NULL;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001870 rhashtable_walk_stop(&gi->hti);
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001871 rhashtable_walk_exit(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00001872}
1873
Steven Whitehouse6802e342008-05-21 17:03:22 +01001874static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001875{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001876 dump_glock(seq, iter_ptr);
1877 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00001878}
1879
Steven Whitehousea2457692012-01-20 10:38:36 +00001880static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
1881{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001882 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00001883 if (*pos >= GFS2_NR_SBSTATS)
1884 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001885 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001886}
1887
1888static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
1889 loff_t *pos)
1890{
Steven Whitehousea2457692012-01-20 10:38:36 +00001891 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001892 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00001893 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001894 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001895}
1896
1897static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
1898{
1899 preempt_enable();
1900}
1901
Denis Cheng4ef29002007-07-31 18:31:11 +08001902static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001903 .start = gfs2_glock_seq_start,
1904 .next = gfs2_glock_seq_next,
1905 .stop = gfs2_glock_seq_stop,
1906 .show = gfs2_glock_seq_show,
1907};
1908
Steven Whitehousea2457692012-01-20 10:38:36 +00001909static const struct seq_operations gfs2_glstats_seq_ops = {
1910 .start = gfs2_glock_seq_start,
1911 .next = gfs2_glock_seq_next,
1912 .stop = gfs2_glock_seq_stop,
1913 .show = gfs2_glstats_seq_show,
1914};
1915
1916static const struct seq_operations gfs2_sbstats_seq_ops = {
1917 .start = gfs2_sbstats_seq_start,
1918 .next = gfs2_sbstats_seq_next,
1919 .stop = gfs2_sbstats_seq_stop,
1920 .show = gfs2_sbstats_seq_show,
1921};
1922
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001923#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
1924
Steven Whitehousea2457692012-01-20 10:38:36 +00001925static int gfs2_glocks_open(struct inode *inode, struct file *file)
Robert Peterson7c52b162007-03-16 10:26:37 +00001926{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001927 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1928 sizeof(struct gfs2_glock_iter));
1929 if (ret == 0) {
1930 struct seq_file *seq = file->private_data;
1931 struct gfs2_glock_iter *gi = seq->private;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001932
Steven Whitehouse6802e342008-05-21 17:03:22 +01001933 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001934 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01001935 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001936 seq->size = GFS2_SEQ_GOODSIZE;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001937 gi->gl = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001938 }
1939 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00001940}
1941
Bob Peterson88ffbf32015-03-16 11:02:46 -05001942static int gfs2_glocks_release(struct inode *inode, struct file *file)
1943{
1944 struct seq_file *seq = file->private_data;
1945 struct gfs2_glock_iter *gi = seq->private;
1946
1947 gi->gl = NULL;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001948 return seq_release_private(inode, file);
1949}
1950
Steven Whitehousea2457692012-01-20 10:38:36 +00001951static int gfs2_glstats_open(struct inode *inode, struct file *file)
1952{
1953 int ret = seq_open_private(file, &gfs2_glstats_seq_ops,
1954 sizeof(struct gfs2_glock_iter));
1955 if (ret == 0) {
1956 struct seq_file *seq = file->private_data;
1957 struct gfs2_glock_iter *gi = seq->private;
1958 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001959 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01001960 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001961 seq->size = GFS2_SEQ_GOODSIZE;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001962 gi->gl = NULL;
Steven Whitehousea2457692012-01-20 10:38:36 +00001963 }
1964 return ret;
1965}
1966
1967static int gfs2_sbstats_open(struct inode *inode, struct file *file)
1968{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001969 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00001970 if (ret == 0) {
1971 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001972 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00001973 }
1974 return ret;
1975}
1976
1977static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001978 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00001979 .open = gfs2_glocks_open,
1980 .read = seq_read,
1981 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05001982 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00001983};
1984
1985static const struct file_operations gfs2_glstats_fops = {
1986 .owner = THIS_MODULE,
1987 .open = gfs2_glstats_open,
1988 .read = seq_read,
1989 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05001990 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00001991};
1992
1993static const struct file_operations gfs2_sbstats_fops = {
1994 .owner = THIS_MODULE,
1995 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00001996 .read = seq_read,
1997 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001998 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00001999};
2000
2001int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2002{
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002003 struct dentry *dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002004
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002005 dent = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2006 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002007 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002008 sdp->debugfs_dir = dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002009
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002010 dent = debugfs_create_file("glocks",
2011 S_IFREG | S_IRUGO,
2012 sdp->debugfs_dir, sdp,
2013 &gfs2_glocks_fops);
2014 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002015 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002016 sdp->debugfs_dentry_glocks = dent;
2017
2018 dent = debugfs_create_file("glstats",
2019 S_IFREG | S_IRUGO,
2020 sdp->debugfs_dir, sdp,
2021 &gfs2_glstats_fops);
2022 if (IS_ERR_OR_NULL(dent))
2023 goto fail;
2024 sdp->debugfs_dentry_glstats = dent;
2025
2026 dent = debugfs_create_file("sbstats",
2027 S_IFREG | S_IRUGO,
2028 sdp->debugfs_dir, sdp,
2029 &gfs2_sbstats_fops);
2030 if (IS_ERR_OR_NULL(dent))
2031 goto fail;
2032 sdp->debugfs_dentry_sbstats = dent;
Robert Peterson7c52b162007-03-16 10:26:37 +00002033
2034 return 0;
Steven Whitehousea2457692012-01-20 10:38:36 +00002035fail:
2036 gfs2_delete_debugfs_file(sdp);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002037 return dent ? PTR_ERR(dent) : -ENOMEM;
Robert Peterson7c52b162007-03-16 10:26:37 +00002038}
2039
2040void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2041{
Steven Whitehousea2457692012-01-20 10:38:36 +00002042 if (sdp->debugfs_dir) {
Robert Peterson5f882092007-04-18 11:41:11 -05002043 if (sdp->debugfs_dentry_glocks) {
2044 debugfs_remove(sdp->debugfs_dentry_glocks);
2045 sdp->debugfs_dentry_glocks = NULL;
2046 }
Steven Whitehousea2457692012-01-20 10:38:36 +00002047 if (sdp->debugfs_dentry_glstats) {
2048 debugfs_remove(sdp->debugfs_dentry_glstats);
2049 sdp->debugfs_dentry_glstats = NULL;
2050 }
2051 if (sdp->debugfs_dentry_sbstats) {
2052 debugfs_remove(sdp->debugfs_dentry_sbstats);
2053 sdp->debugfs_dentry_sbstats = NULL;
2054 }
Robert Peterson5f882092007-04-18 11:41:11 -05002055 debugfs_remove(sdp->debugfs_dir);
2056 sdp->debugfs_dir = NULL;
2057 }
Robert Peterson7c52b162007-03-16 10:26:37 +00002058}
2059
2060int gfs2_register_debugfs(void)
2061{
2062 gfs2_root = debugfs_create_dir("gfs2", NULL);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002063 if (IS_ERR(gfs2_root))
2064 return PTR_ERR(gfs2_root);
Robert Peterson7c52b162007-03-16 10:26:37 +00002065 return gfs2_root ? 0 : -ENOMEM;
2066}
2067
2068void gfs2_unregister_debugfs(void)
2069{
2070 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002071 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002072}