blob: efd44d5645d835e74172dc5b507929106ce7f619 [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{
Ross Lagerwallb568ed32019-03-27 17:09:17 +0000139 if (!(gl->gl_ops->go_flags & GLOF_LRU))
140 return;
141
Steven Whitehouse29687a22011-03-30 16:33:25 +0100142 spin_lock(&lru_lock);
143
Ross Lagerwallb568ed32019-03-27 17:09:17 +0000144 list_del(&gl->gl_lru);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100145 list_add_tail(&gl->gl_lru, &lru_list);
Ross Lagerwallb568ed32019-03-27 17:09:17 +0000146
147 if (!test_bit(GLF_LRU, &gl->gl_flags)) {
148 set_bit(GLF_LRU, &gl->gl_flags);
149 atomic_inc(&lru_count);
150 }
151
Steven Whitehouse29687a22011-03-30 16:33:25 +0100152 spin_unlock(&lru_lock);
153}
154
Bob Peterson8f6cb402015-01-05 13:25:10 -0500155static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
Steven Whitehousef42ab082011-04-14 16:50:31 +0100156{
Bob Peterson8f6cb402015-01-05 13:25:10 -0500157 spin_lock(&lru_lock);
Ross Lagerwallb568ed32019-03-27 17:09:17 +0000158 if (test_bit(GLF_LRU, &gl->gl_flags)) {
Steven Whitehousef42ab082011-04-14 16:50:31 +0100159 list_del_init(&gl->gl_lru);
160 atomic_dec(&lru_count);
161 clear_bit(GLF_LRU, &gl->gl_flags);
162 }
163 spin_unlock(&lru_lock);
164}
165
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500166/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167 * gfs2_glock_put() - Decrement reference count on glock
168 * @gl: The glock to put
169 *
170 */
171
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000172void gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173{
Bob Peterson15562c42015-03-16 11:52:05 -0500174 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000175 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176
Steven Whitehousee66cf162013-10-15 15:18:08 +0100177 if (lockref_put_or_lock(&gl->gl_lockref))
178 return;
179
180 lockref_mark_dead(&gl->gl_lockref);
181
Bob Peterson8f6cb402015-01-05 13:25:10 -0500182 gfs2_glock_remove_from_lru(gl);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100183 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500184 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100185 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
186 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
187 trace_gfs2_glock_put(gl);
188 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189}
190
191/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100192 * may_grant - check if its ok to grant a new lock
193 * @gl: The glock
194 * @gh: The lock request which we wish to grant
195 *
196 * Returns: true if its ok to grant the lock
197 */
198
199static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500200{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100201 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
202 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
203 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
204 return 0;
205 if (gl->gl_state == gh->gh_state)
206 return 1;
207 if (gh->gh_flags & GL_EXACT)
208 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100209 if (gl->gl_state == LM_ST_EXCLUSIVE) {
210 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
211 return 1;
212 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
213 return 1;
214 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100215 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
216 return 1;
217 return 0;
218}
219
220static void gfs2_holder_wake(struct gfs2_holder *gh)
221{
222 clear_bit(HIF_WAIT, &gh->gh_iflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100223 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100224 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
225}
226
227/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100228 * do_error - Something unexpected has happened during a lock request
229 *
230 */
231
Denys Vlasenkoa527b382016-04-12 12:39:12 -0400232static void do_error(struct gfs2_glock *gl, const int ret)
Steven Whitehoused5341a92010-07-23 14:05:51 +0100233{
234 struct gfs2_holder *gh, *tmp;
235
236 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
237 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
238 continue;
239 if (ret & LM_OUT_ERROR)
240 gh->gh_error = -EIO;
241 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
242 gh->gh_error = GLR_TRYFAILED;
243 else
244 continue;
245 list_del_init(&gh->gh_list);
246 trace_gfs2_glock_queue(gh, 0);
247 gfs2_holder_wake(gh);
248 }
249}
250
251/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100252 * do_promote - promote as many requests as possible on the current queue
253 * @gl: The glock
254 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000255 * Returns: 1 if there is a blocked holder at the head of the list, or 2
256 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100257 */
258
259static int do_promote(struct gfs2_glock *gl)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500260__releases(&gl->gl_lockref.lock)
261__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100262{
263 const struct gfs2_glock_operations *glops = gl->gl_ops;
264 struct gfs2_holder *gh, *tmp;
265 int ret;
266
267restart:
268 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
269 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
270 continue;
271 if (may_grant(gl, gh)) {
272 if (gh->gh_list.prev == &gl->gl_holders &&
273 glops->go_lock) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500274 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100275 /* FIXME: eliminate this eventually */
276 ret = glops->go_lock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500277 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100278 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000279 if (ret == 1)
280 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100281 gh->gh_error = ret;
282 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100283 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100284 gfs2_holder_wake(gh);
285 goto restart;
286 }
287 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100288 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100289 gfs2_holder_wake(gh);
290 goto restart;
291 }
292 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100293 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100294 gfs2_holder_wake(gh);
295 continue;
296 }
297 if (gh->gh_list.prev == &gl->gl_holders)
298 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100299 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100300 break;
301 }
302 return 0;
303}
304
305/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100306 * find_first_waiter - find the first gh that's waiting for the glock
307 * @gl: the glock
308 */
309
310static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
311{
312 struct gfs2_holder *gh;
313
314 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
315 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
316 return gh;
317 }
318 return NULL;
319}
320
321/**
322 * state_change - record that the glock is now in a different state
323 * @gl: the glock
324 * @new_state the new state
325 *
326 */
327
328static void state_change(struct gfs2_glock *gl, unsigned int new_state)
329{
330 int held1, held2;
331
332 held1 = (gl->gl_state != LM_ST_UNLOCKED);
333 held2 = (new_state != LM_ST_UNLOCKED);
334
335 if (held1 != held2) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100336 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100337 if (held2)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100338 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100339 else
Steven Whitehousee66cf162013-10-15 15:18:08 +0100340 gl->gl_lockref.count--;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100341 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100342 if (held1 && held2 && list_empty(&gl->gl_holders))
343 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100344
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400345 if (new_state != gl->gl_target)
346 /* shorten our minimum hold time */
347 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
348 GL_GLOCK_MIN_HOLD);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100349 gl->gl_state = new_state;
350 gl->gl_tchange = jiffies;
351}
352
353static void gfs2_demote_wake(struct gfs2_glock *gl)
354{
355 gl->gl_demote_state = LM_ST_EXCLUSIVE;
356 clear_bit(GLF_DEMOTE, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100357 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100358 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
359}
360
361/**
362 * finish_xmote - The DLM has replied to one of our lock requests
363 * @gl: The glock
364 * @ret: The status from the DLM
365 *
366 */
367
368static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
369{
370 const struct gfs2_glock_operations *glops = gl->gl_ops;
371 struct gfs2_holder *gh;
372 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000373 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500374
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500375 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse63997772009-06-12 08:49:20 +0100376 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100377 state_change(gl, state);
378 gh = find_first_waiter(gl);
379
380 /* Demote to UN request arrived during demote to SH or DF */
381 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
382 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
383 gl->gl_target = LM_ST_UNLOCKED;
384
385 /* Check for state != intended state */
386 if (unlikely(state != gl->gl_target)) {
387 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
388 /* move to back of queue and try next entry */
389 if (ret & LM_OUT_CANCELED) {
390 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
391 list_move_tail(&gh->gh_list, &gl->gl_holders);
392 gh = find_first_waiter(gl);
393 gl->gl_target = gh->gh_state;
394 goto retry;
395 }
396 /* Some error or failed "try lock" - report it */
397 if ((ret & LM_OUT_ERROR) ||
398 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
399 gl->gl_target = gl->gl_state;
400 do_error(gl, ret);
401 goto out;
402 }
403 }
404 switch(state) {
405 /* Unlocked due to conversion deadlock, try again */
406 case LM_ST_UNLOCKED:
407retry:
408 do_xmote(gl, gh, gl->gl_target);
409 break;
410 /* Conversion fails, unlock and try again */
411 case LM_ST_SHARED:
412 case LM_ST_DEFERRED:
413 do_xmote(gl, gh, LM_ST_UNLOCKED);
414 break;
415 default: /* Everything else */
Joe Perchesd77d1b52014-03-06 12:10:45 -0800416 pr_err("wanted %u got %u\n", gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100417 GLOCK_BUG_ON(gl, 1);
418 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500419 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100420 return;
421 }
422
423 /* Fast path - we got what we asked for */
424 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
425 gfs2_demote_wake(gl);
426 if (state != LM_ST_UNLOCKED) {
427 if (glops->go_xmote_bh) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500428 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100429 rv = glops->go_xmote_bh(gl, gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500430 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100431 if (rv) {
432 do_error(gl, rv);
433 goto out;
434 }
435 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000436 rv = do_promote(gl);
437 if (rv == 2)
438 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100439 }
440out:
441 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000442out_locked:
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500443 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500444}
445
Steven Whitehouse6802e342008-05-21 17:03:22 +0100446/**
447 * do_xmote - Calls the DLM to change the state of a lock
448 * @gl: The lock state
449 * @gh: The holder (only for promotes)
450 * @target: The target lock state
451 *
452 */
453
454static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500455__releases(&gl->gl_lockref.lock)
456__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100457{
458 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500459 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Bob Petersonb58bf402015-07-24 09:45:43 -0500460 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100461 int ret;
462
463 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
464 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000465 GLOCK_BUG_ON(gl, gl->gl_state == target);
466 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100467 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
468 glops->go_inval) {
469 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
470 do_error(gl, 0); /* Fail queued try locks */
471 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000472 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000473 set_bit(GLF_BLOCKING, &gl->gl_flags);
474 if ((gl->gl_req == LM_ST_UNLOCKED) ||
475 (gl->gl_state == LM_ST_EXCLUSIVE) ||
476 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
477 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500478 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson06dfc302012-10-24 14:41:05 -0400479 if (glops->go_sync)
480 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100481 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
482 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
483 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
484
485 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000486 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
487 /* lock_dlm */
488 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400489 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
490 target == LM_ST_UNLOCKED &&
491 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
492 finish_xmote(gl, target);
493 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
494 gfs2_glock_put(gl);
495 }
496 else if (ret) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800497 pr_err("lm_lock ret %d\n", ret);
David Teiglanddba2d702012-11-14 13:46:53 -0500498 GLOCK_BUG_ON(gl, 1);
499 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000500 } else { /* lock_nolock */
501 finish_xmote(gl, target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100502 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
503 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100504 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000505
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500506 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100507}
508
509/**
510 * find_first_holder - find the first "holder" gh
511 * @gl: the glock
512 */
513
514static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
515{
516 struct gfs2_holder *gh;
517
518 if (!list_empty(&gl->gl_holders)) {
519 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
520 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
521 return gh;
522 }
523 return NULL;
524}
525
526/**
527 * run_queue - do all outstanding tasks related to a glock
528 * @gl: The glock in question
529 * @nonblock: True if we must not block in run_queue
530 *
531 */
532
533static void run_queue(struct gfs2_glock *gl, const int nonblock)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500534__releases(&gl->gl_lockref.lock)
535__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100536{
537 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000538 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100539
540 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
541 return;
542
543 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
544
545 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
546 gl->gl_demote_state != gl->gl_state) {
547 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000548 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100549 if (nonblock)
550 goto out_sched;
551 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100552 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100553 gl->gl_target = gl->gl_demote_state;
554 } else {
555 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
556 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000557 ret = do_promote(gl);
558 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000559 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000560 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100561 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100562 gh = find_first_waiter(gl);
563 gl->gl_target = gh->gh_state;
564 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
565 do_error(gl, 0); /* Fail queued try locks */
566 }
567 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100568out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100569 return;
570
571out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100572 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100573 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100574 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100575 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100576 gl->gl_lockref.count--;
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100577 return;
578
Steven Whitehoused8348de2009-02-05 10:12:38 +0000579out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100580 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100581 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100582 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100583}
584
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500585static void delete_work_func(struct work_struct *work)
586{
587 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500588 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500589 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000590 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500591
Bob Petersona4923862015-12-07 16:24:27 -0600592 /* If someone's using this glock to create a new dinode, the block must
593 have been freed by another node, then re-used, in which case our
594 iopen callback is too late after the fact. Ignore it. */
595 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
596 goto out;
597
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500598 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000599 if (inode && !IS_ERR(inode)) {
600 d_prune_aliases(inode);
601 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500602 }
Bob Petersona4923862015-12-07 16:24:27 -0600603out:
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500604 gfs2_glock_put(gl);
605}
606
Steven Whitehouse6802e342008-05-21 17:03:22 +0100607static void glock_work_func(struct work_struct *work)
608{
609 unsigned long delay = 0;
610 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000611 int drop_ref = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100612
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000613 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100614 finish_xmote(gl, gl->gl_reply);
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000615 drop_ref = 1;
616 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500617 spin_lock(&gl->gl_lockref.lock);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400618 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100619 gl->gl_state != LM_ST_UNLOCKED &&
620 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100621 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400622
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400623 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100624 if (time_before(now, holdtime))
625 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400626
627 if (!delay) {
628 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
629 set_bit(GLF_DEMOTE, &gl->gl_flags);
630 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100631 }
632 run_queue(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500633 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400634 if (!delay)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100635 gfs2_glock_put(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400636 else {
637 if (gl->gl_name.ln_type != LM_TYPE_INODE)
638 delay = 0;
639 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
640 gfs2_glock_put(gl);
641 }
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000642 if (drop_ref)
643 gfs2_glock_put(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100644}
645
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646/**
647 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
648 * @sdp: The GFS2 superblock
649 * @number: the lock number
650 * @glops: The glock_operations to use
651 * @create: If 0, don't create the glock if it doesn't exist
652 * @glp: the glock is returned here
653 *
654 * This does not lock a glock, just finds/creates structures for one.
655 *
656 * Returns: errno
657 */
658
Steven Whitehousecd915492006-09-04 12:49:07 -0400659int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400660 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661 struct gfs2_glock **glp)
662{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000663 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500664 struct lm_lockname name = { .ln_number = number,
665 .ln_type = glops->go_type,
666 .ln_sbd = sdp };
Bob Peterson88ffbf32015-03-16 11:02:46 -0500667 struct gfs2_glock *gl, *tmp = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000668 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000669 struct kmem_cache *cachep;
Bob Peterson88ffbf32015-03-16 11:02:46 -0500670 int ret, tries = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671
Andrew Price4cf918c2017-02-22 12:05:03 -0500672 rcu_read_lock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500673 gl = rhashtable_lookup_fast(&gl_hash_table, &name, ht_parms);
674 if (gl && !lockref_get_not_dead(&gl->gl_lockref))
675 gl = NULL;
Andrew Price4cf918c2017-02-22 12:05:03 -0500676 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000678 *glp = gl;
679 if (gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680 return 0;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000681 if (!create)
682 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683
Steven Whitehouse009d8512009-12-08 12:12:13 +0000684 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000685 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000686 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000687 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100688 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689 if (!gl)
690 return -ENOMEM;
691
David Teiglanddba2d702012-11-14 13:46:53 -0500692 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500693
694 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100695 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500696 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500697 kmem_cache_free(cachep, gl);
698 return -ENOMEM;
699 }
David Teiglanddba2d702012-11-14 13:46:53 -0500700 }
701
Steven Whitehouse8f052282010-01-29 15:21:27 +0000702 atomic_inc(&sdp->sd_glock_disposal);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500703 gl->gl_node.next = NULL;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400704 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100706 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100708 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500709 gl->gl_demote_state = LM_ST_EXCLUSIVE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710 gl->gl_ops = glops;
Steven Whitehousea2457692012-01-20 10:38:36 +0000711 gl->gl_dstamp = ktime_set(0, 0);
712 preempt_disable();
713 /* We use the global stats to estimate the initial per-glock stats */
714 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
715 preempt_enable();
716 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
717 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500718 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400719 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400720 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500721 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500722 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723
Steven Whitehouse009d8512009-12-08 12:12:13 +0000724 mapping = gfs2_glock2aspace(gl);
725 if (mapping) {
726 mapping->a_ops = &gfs2_meta_aops;
727 mapping->host = s->s_bdev->bd_inode;
728 mapping->flags = 0;
729 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800730 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000731 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 }
733
Bob Peterson88ffbf32015-03-16 11:02:46 -0500734again:
735 ret = rhashtable_lookup_insert_fast(&gl_hash_table, &gl->gl_node,
736 ht_parms);
737 if (ret == 0) {
738 *glp = gl;
739 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 }
741
Bob Peterson88ffbf32015-03-16 11:02:46 -0500742 if (ret == -EEXIST) {
743 ret = 0;
Andrew Price4cf918c2017-02-22 12:05:03 -0500744 rcu_read_lock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500745 tmp = rhashtable_lookup_fast(&gl_hash_table, &name, ht_parms);
746 if (tmp == NULL || !lockref_get_not_dead(&tmp->gl_lockref)) {
747 if (++tries < 100) {
Andrew Price4cf918c2017-02-22 12:05:03 -0500748 rcu_read_unlock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500749 cond_resched();
750 goto again;
751 }
752 tmp = NULL;
753 ret = -ENOMEM;
754 }
Andrew Price4cf918c2017-02-22 12:05:03 -0500755 rcu_read_unlock();
Bob Peterson88ffbf32015-03-16 11:02:46 -0500756 } else {
757 WARN_ON_ONCE(ret);
758 }
759 kfree(gl->gl_lksb.sb_lvbptr);
760 kmem_cache_free(cachep, gl);
761 atomic_dec(&sdp->sd_glock_disposal);
762 *glp = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763
Bob Peterson88ffbf32015-03-16 11:02:46 -0500764 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000765}
766
767/**
768 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
769 * @gl: the glock
770 * @state: the state we're requesting
771 * @flags: the modifier flags
772 * @gh: the holder structure
773 *
774 */
775
Bob Petersonb58bf402015-07-24 09:45:43 -0500776void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 struct gfs2_holder *gh)
778{
779 INIT_LIST_HEAD(&gh->gh_list);
780 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200781 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800782 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783 gh->gh_state = state;
784 gh->gh_flags = flags;
785 gh->gh_error = 0;
786 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 gfs2_glock_hold(gl);
788}
789
790/**
791 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
792 * @state: the state we're requesting
793 * @flags: the modifier flags
794 * @gh: the holder structure
795 *
796 * Don't mess with the glock.
797 *
798 */
799
Bob Petersonb58bf402015-07-24 09:45:43 -0500800void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801{
802 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400803 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000804 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200805 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100806 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400807 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808}
809
810/**
811 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
812 * @gh: the holder structure
813 *
814 */
815
816void gfs2_holder_uninit(struct gfs2_holder *gh)
817{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800818 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819 gfs2_glock_put(gh->gh_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500820 gfs2_holder_mark_uninitialized(gh);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500821 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822}
823
Steven Whitehousefe64d512009-05-19 10:01:18 +0100824/**
Bob Peterson07a79042012-08-09 12:48:44 -0500825 * gfs2_glock_wait - wait on a glock acquisition
826 * @gh: the glock holder
827 *
828 * Returns: 0 on success
829 */
830
831int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000832{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400833 unsigned long time1 = jiffies;
834
Steven Whitehousefee852e2007-01-17 15:33:23 +0000835 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000836 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400837 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
838 /* Lengthen the minimum hold time. */
839 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
840 GL_GLOCK_HOLD_INCR,
841 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500842 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100843}
844
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000846 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847 * @gl: the glock
848 * @state: the state the caller wants us to change to
849 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000850 * There are only two requests that we are going to see in actual
851 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 */
853
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500854static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100855 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500857 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
858
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500859 set_bit(bit, &gl->gl_flags);
860 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000861 gl->gl_demote_state = state;
862 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100863 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
864 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100865 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000866 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500867 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100868 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100869 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870}
871
Steven Whitehouse6802e342008-05-21 17:03:22 +0100872void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000873{
Joe Perches5e690692010-11-09 16:35:20 -0800874 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000875 va_list args;
876
877 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800878
Steven Whitehouse6802e342008-05-21 17:03:22 +0100879 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100880 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100881 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800882 vaf.fmt = fmt;
883 vaf.va = &args;
884
Joe Perchesd77d1b52014-03-06 12:10:45 -0800885 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100886 }
Joe Perches5e690692010-11-09 16:35:20 -0800887
Robert Peterson7c52b162007-03-16 10:26:37 +0000888 va_end(args);
889}
890
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 * add_to_queue - Add a holder to the wait queue (but look for recursion)
893 * @gh: the holder structure to add
894 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100895 * Eventually we should move the recursive locking trap to a
896 * debugging option or something like that. This is the fast
897 * path and needs to have the minimum number of distractions.
898 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 */
900
Steven Whitehouse6802e342008-05-21 17:03:22 +0100901static inline void add_to_queue(struct gfs2_holder *gh)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500902__releases(&gl->gl_lockref.lock)
903__acquires(&gl->gl_lockref.lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904{
905 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -0500906 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100907 struct list_head *insert_pt = NULL;
908 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -0500909 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800911 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +0000912 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
913 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -0400914
Steven Whitehouse6802e342008-05-21 17:03:22 +0100915 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
916 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -0500917 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100918 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
919 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 }
921
Steven Whitehouse6802e342008-05-21 17:03:22 +0100922 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
923 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
924 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
925 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -0500926 if (try_futile &&
927 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100928fail:
929 gh->gh_error = GLR_TRYFAILED;
930 gfs2_holder_wake(gh);
931 return;
932 }
933 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
934 continue;
935 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
936 insert_pt = &gh2->gh_list;
937 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100938 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +0000939 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +0000940 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
941 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100942 if (likely(insert_pt == NULL)) {
943 list_add_tail(&gh->gh_list, &gl->gl_holders);
944 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
945 goto do_cancel;
946 return;
947 }
948 list_add_tail(&gh->gh_list, insert_pt);
949do_cancel:
950 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
951 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500952 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +0100953 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000954 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500955 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100956 }
957 return;
958
959trap_recursive:
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800960 pr_err("original: %pSR\n", (void *)gh2->gh_ip);
961 pr_err("pid: %d\n", pid_nr(gh2->gh_owner_pid));
962 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +0100963 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800964 pr_err("new: %pSR\n", (void *)gh->gh_ip);
965 pr_err("pid: %d\n", pid_nr(gh->gh_owner_pid));
966 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +0100967 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +0100968 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100969 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970}
971
972/**
973 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
974 * @gh: the holder structure
975 *
976 * if (gh->gh_flags & GL_ASYNC), this never returns an error
977 *
978 * Returns: 0, GLR_TRYFAILED, or errno on failure
979 */
980
981int gfs2_glock_nq(struct gfs2_holder *gh)
982{
983 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -0500984 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 int error = 0;
986
Steven Whitehouse6802e342008-05-21 17:03:22 +0100987 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000989
Steven Whitehousef42ab082011-04-14 16:50:31 +0100990 if (test_bit(GLF_LRU, &gl->gl_flags))
991 gfs2_glock_remove_from_lru(gl);
992
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500993 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000994 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -0400995 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
996 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +0100997 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -0400998 gl->gl_lockref.count++;
999 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1000 gl->gl_lockref.count--;
1001 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001002 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001003 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004
Steven Whitehouse6802e342008-05-21 17:03:22 +01001005 if (!(gh->gh_flags & GL_ASYNC))
1006 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 return error;
1009}
1010
1011/**
1012 * gfs2_glock_poll - poll to see if an async request has been completed
1013 * @gh: the holder
1014 *
1015 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1016 */
1017
1018int gfs2_glock_poll(struct gfs2_holder *gh)
1019{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001020 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021}
1022
1023/**
1024 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1025 * @gh: the glock holder
1026 *
1027 */
1028
1029void gfs2_glock_dq(struct gfs2_holder *gh)
1030{
1031 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001032 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001033 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001034 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001036 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001038 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 list_del_init(&gh->gh_list);
Bob Peterson7508abc2015-12-18 11:54:55 -06001041 clear_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001042 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001043 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001044 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001045 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046 glops->go_unlock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001047 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001048 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001049 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001050 if (list_empty(&gl->gl_holders) &&
1051 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1052 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1053 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001054 }
Ross Lagerwallb568ed32019-03-27 17:09:17 +00001055 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
Bob Peterson4abb6ad2012-08-09 12:48:43 -05001056 gfs2_glock_add_to_lru(gl);
1057
Steven Whitehouse63997772009-06-12 08:49:20 +01001058 trace_gfs2_glock_queue(gh, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001059 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001060 if (likely(fast_path))
1061 return;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001062
1063 gfs2_glock_hold(gl);
1064 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001065 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1066 gl->gl_name.ln_type == LM_TYPE_INODE)
1067 delay = gl->gl_hold_time;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001068 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1069 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070}
1071
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001072void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1073{
1074 struct gfs2_glock *gl = gh->gh_gl;
1075 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001076 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001077 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001078}
1079
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1082 * @gh: the holder structure
1083 *
1084 */
1085
1086void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1087{
1088 gfs2_glock_dq(gh);
1089 gfs2_holder_uninit(gh);
1090}
1091
1092/**
1093 * gfs2_glock_nq_num - acquire a glock based on lock number
1094 * @sdp: the filesystem
1095 * @number: the lock number
1096 * @glops: the glock operations for the type of glock
1097 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001098 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 * @gh: the struct gfs2_holder
1100 *
1101 * Returns: errno
1102 */
1103
Steven Whitehousecd915492006-09-04 12:49:07 -04001104int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001105 const struct gfs2_glock_operations *glops,
Bob Petersonb58bf402015-07-24 09:45:43 -05001106 unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107{
1108 struct gfs2_glock *gl;
1109 int error;
1110
1111 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1112 if (!error) {
1113 error = gfs2_glock_nq_init(gl, state, flags, gh);
1114 gfs2_glock_put(gl);
1115 }
1116
1117 return error;
1118}
1119
1120/**
1121 * glock_compare - Compare two struct gfs2_glock structures for sorting
1122 * @arg_a: the first structure
1123 * @arg_b: the second structure
1124 *
1125 */
1126
1127static int glock_compare(const void *arg_a, const void *arg_b)
1128{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001129 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1130 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1131 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1132 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133
1134 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001135 return 1;
1136 if (a->ln_number < b->ln_number)
1137 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001138 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 -04001139 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140}
1141
1142/**
1143 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1144 * @num_gh: the number of structures
1145 * @ghs: an array of struct gfs2_holder structures
1146 *
1147 * Returns: 0 on success (all glocks acquired),
1148 * errno on failure (no glocks acquired)
1149 */
1150
1151static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1152 struct gfs2_holder **p)
1153{
1154 unsigned int x;
1155 int error = 0;
1156
1157 for (x = 0; x < num_gh; x++)
1158 p[x] = &ghs[x];
1159
1160 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1161
1162 for (x = 0; x < num_gh; x++) {
1163 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1164
1165 error = gfs2_glock_nq(p[x]);
1166 if (error) {
1167 while (x--)
1168 gfs2_glock_dq(p[x]);
1169 break;
1170 }
1171 }
1172
1173 return error;
1174}
1175
1176/**
1177 * gfs2_glock_nq_m - acquire multiple glocks
1178 * @num_gh: the number of structures
1179 * @ghs: an array of struct gfs2_holder structures
1180 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001181 *
1182 * Returns: 0 on success (all glocks acquired),
1183 * errno on failure (no glocks acquired)
1184 */
1185
1186int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1187{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001188 struct gfs2_holder *tmp[4];
1189 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190 int error = 0;
1191
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001192 switch(num_gh) {
1193 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001195 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1197 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001198 default:
1199 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001201 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1202 if (!pph)
1203 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 }
1205
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001206 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001208 if (pph != tmp)
1209 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001210
1211 return error;
1212}
1213
1214/**
1215 * gfs2_glock_dq_m - release multiple glocks
1216 * @num_gh: the number of structures
1217 * @ghs: an array of struct gfs2_holder structures
1218 *
1219 */
1220
1221void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1222{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001223 while (num_gh--)
1224 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001225}
1226
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001227void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001228{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001229 unsigned long delay = 0;
1230 unsigned long holdtime;
1231 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001233 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001234 holdtime = gl->gl_tchange + gl->gl_hold_time;
1235 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1236 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001237 if (time_before(now, holdtime))
1238 delay = holdtime - now;
1239 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001240 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001241 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001242
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001243 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001244 handle_callback(gl, state, delay, true);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001245 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001246 if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1247 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248}
1249
1250/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001251 * gfs2_should_freeze - Figure out if glock should be frozen
1252 * @gl: The glock in question
1253 *
1254 * Glocks are not frozen if (a) the result of the dlm operation is
1255 * an error, (b) the locking operation was an unlock operation or
1256 * (c) if there is a "noexp" flagged request anywhere in the queue
1257 *
1258 * Returns: 1 if freezing should occur, 0 otherwise
1259 */
1260
1261static int gfs2_should_freeze(const struct gfs2_glock *gl)
1262{
1263 const struct gfs2_holder *gh;
1264
1265 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1266 return 0;
1267 if (gl->gl_target == LM_ST_UNLOCKED)
1268 return 0;
1269
1270 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1271 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1272 continue;
1273 if (LM_FLAG_NOEXP & gh->gh_flags)
1274 return 0;
1275 }
1276
1277 return 1;
1278}
1279
1280/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001281 * gfs2_glock_complete - Callback used by locking
1282 * @gl: Pointer to the glock
1283 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001284 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001285 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
Steven Whitehouse47a25382010-11-30 15:49:31 +00001286 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287 */
1288
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001289void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290{
Bob Peterson15562c42015-03-16 11:52:05 -05001291 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001292
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001293 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001294 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001295
David Teiglande0c2a9a2012-01-09 17:18:05 -05001296 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001297 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001298 set_bit(GLF_FROZEN, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001299 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001301 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001302 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001303
Steven Whitehousee66cf162013-10-15 15:18:08 +01001304 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001305 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001306 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001307
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001308 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1309 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310}
1311
Steven Whitehouse4506a512013-02-01 20:36:03 +00001312static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1313{
1314 struct gfs2_glock *gla, *glb;
1315
1316 gla = list_entry(a, struct gfs2_glock, gl_lru);
1317 glb = list_entry(b, struct gfs2_glock, gl_lru);
1318
1319 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1320 return 1;
1321 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1322 return -1;
1323
1324 return 0;
1325}
1326
1327/**
1328 * gfs2_dispose_glock_lru - Demote a list of glocks
1329 * @list: The list to dispose of
1330 *
1331 * Disposing of glocks may involve disk accesses, so that here we sort
1332 * the glocks by number (i.e. disk location of the inodes) so that if
1333 * there are any such accesses, they'll be sent in order (mostly).
1334 *
1335 * Must be called under the lru_lock, but may drop and retake this
1336 * lock. While the lru_lock is dropped, entries may vanish from the
1337 * list, but no new entries will appear on the list (since it is
1338 * private)
1339 */
1340
1341static void gfs2_dispose_glock_lru(struct list_head *list)
1342__releases(&lru_lock)
1343__acquires(&lru_lock)
1344{
1345 struct gfs2_glock *gl;
1346
1347 list_sort(NULL, list, glock_cmp);
1348
1349 while(!list_empty(list)) {
1350 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1351 list_del_init(&gl->gl_lru);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001352 if (!spin_trylock(&gl->gl_lockref.lock)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001353add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001354 list_add(&gl->gl_lru, &lru_list);
Ross Lagerwallb568ed32019-03-27 17:09:17 +00001355 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001356 atomic_inc(&lru_count);
1357 continue;
1358 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001359 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001360 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001361 goto add_back_to_lru;
1362 }
Steven Whitehousee66cf162013-10-15 15:18:08 +01001363 gl->gl_lockref.count++;
Steven Whitehouse4506a512013-02-01 20:36:03 +00001364 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001365 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001366 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Steven Whitehouse4506a512013-02-01 20:36:03 +00001367 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +01001368 gl->gl_lockref.count--;
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001369 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001370 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001371 }
1372}
1373
Steven Whitehouse2a005852012-12-14 12:28:30 +00001374/**
1375 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1376 * @nr: The number of entries to scan
1377 *
Steven Whitehouse4506a512013-02-01 20:36:03 +00001378 * This function selects the entries on the LRU which are able to
1379 * be demoted, and then kicks off the process by calling
1380 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001381 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001382
Dave Chinner1ab6c492013-08-28 10:18:09 +10001383static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384{
1385 struct gfs2_glock *gl;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001386 LIST_HEAD(skipped);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001387 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001388 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001389
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001390 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001391 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001392 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001393
1394 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001395 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a512013-02-01 20:36:03 +00001396 list_move(&gl->gl_lru, &dispose);
1397 atomic_dec(&lru_count);
Ross Lagerwallb568ed32019-03-27 17:09:17 +00001398 clear_bit(GLF_LRU, &gl->gl_flags);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001399 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001400 continue;
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001401 }
Steven Whitehouse4506a512013-02-01 20:36:03 +00001402
1403 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001405 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a512013-02-01 20:36:03 +00001406 if (!list_empty(&dispose))
1407 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001408 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001409
1410 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001411}
1412
Dave Chinner1ab6c492013-08-28 10:18:09 +10001413static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1414 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001415{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001416 if (!(sc->gfp_mask & __GFP_FS))
1417 return SHRINK_STOP;
1418 return gfs2_scan_glock_lru(sc->nr_to_scan);
1419}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001420
Dave Chinner1ab6c492013-08-28 10:18:09 +10001421static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1422 struct shrink_control *sc)
1423{
Glauber Costa55f841c2013-08-28 10:17:53 +10001424 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425}
1426
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001427static struct shrinker glock_shrinker = {
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001428 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001429 .count_objects = gfs2_glock_shrink_count,
1430 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001431};
1432
David Teiglandb3b94fa2006-01-16 16:50:04 +00001433/**
1434 * examine_bucket - Call a function for glock in a hash bucket
1435 * @examiner: the function
1436 * @sdp: the filesystem
1437 * @bucket: the bucket
1438 *
Herbert Xu25579692017-05-23 21:53:36 -04001439 * Note that the function can be called multiple times on the same
1440 * object. So the user must ensure that the function can cope with
1441 * that.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001442 */
1443
Bob Peterson88ffbf32015-03-16 11:02:46 -05001444static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001446 struct gfs2_glock *gl;
Herbert Xu25579692017-05-23 21:53:36 -04001447 struct rhashtable_iter iter;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448
Herbert Xu25579692017-05-23 21:53:36 -04001449 rhashtable_walk_enter(&gl_hash_table, &iter);
1450
1451 do {
1452 gl = ERR_PTR(rhashtable_walk_start(&iter));
1453 if (gl)
1454 continue;
1455
1456 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl))
Bob Peterson88ffbf32015-03-16 11:02:46 -05001457 if ((gl->gl_name.ln_sbd == sdp) &&
1458 lockref_get_not_dead(&gl->gl_lockref))
1459 examiner(gl);
Herbert Xu25579692017-05-23 21:53:36 -04001460
1461 rhashtable_walk_stop(&iter);
1462 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1463
1464 rhashtable_walk_exit(&iter);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001465}
1466
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001467/**
1468 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1469 * @gl: The glock to thaw
1470 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001471 */
1472
1473static void thaw_glock(struct gfs2_glock *gl)
1474{
1475 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
Steven Whitehouse7286b312013-08-20 09:35:09 +01001476 goto out;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001477 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Steven Whitehouse7286b312013-08-20 09:35:09 +01001478 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) {
1479out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001480 gfs2_glock_put(gl);
Steven Whitehouse7286b312013-08-20 09:35:09 +01001481 }
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001482}
1483
David Teiglandb3b94fa2006-01-16 16:50:04 +00001484/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485 * clear_glock - look at a glock and see if we can free it from glock cache
1486 * @gl: the glock to look at
1487 *
1488 */
1489
1490static void clear_glock(struct gfs2_glock *gl)
1491{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001492 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001493
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001494 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousec741c452010-09-29 14:20:52 +01001495 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001496 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001497 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001498 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1499 gfs2_glock_put(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500}
1501
1502/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001503 * gfs2_glock_thaw - Thaw any frozen glocks
1504 * @sdp: The super block
1505 *
1506 */
1507
1508void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1509{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001510 glock_hash_walk(thaw_glock, sdp);
1511}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001512
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001513static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001514{
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001515 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001516 gfs2_dump_glock(seq, gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001517 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001518}
1519
1520static void dump_glock_func(struct gfs2_glock *gl)
1521{
1522 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001523}
1524
1525/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001526 * gfs2_gl_hash_clear - Empty out the glock hash table
1527 * @sdp: the filesystem
1528 * @wait: wait until it's all gone
1529 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001530 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531 */
1532
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001533void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001534{
David Teiglandfb6791d2012-11-13 10:58:56 -05001535 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001536 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001537 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001538 flush_workqueue(glock_workqueue);
Bob Peterson2aba1b52015-05-19 09:11:23 -05001539 wait_event_timeout(sdp->sd_glock_wait,
1540 atomic_read(&sdp->sd_glock_disposal) == 0,
1541 HZ * 600);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001542 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001543}
1544
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001545void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1546{
1547 struct gfs2_glock *gl = ip->i_gl;
1548 int ret;
1549
1550 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001551 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001552
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001553 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001554 clear_bit(GLF_LOCK, &gl->gl_flags);
1555 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001556 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001557}
1558
Steven Whitehouse6802e342008-05-21 17:03:22 +01001559static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001560{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001561 switch(state) {
1562 case LM_ST_UNLOCKED:
1563 return "UN";
1564 case LM_ST_SHARED:
1565 return "SH";
1566 case LM_ST_DEFERRED:
1567 return "DF";
1568 case LM_ST_EXCLUSIVE:
1569 return "EX";
1570 }
1571 return "??";
1572}
Robert Peterson04b933f2007-03-23 17:05:15 -05001573
Bob Petersonb58bf402015-07-24 09:45:43 -05001574static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
Steven Whitehouse6802e342008-05-21 17:03:22 +01001575{
1576 char *p = buf;
1577 if (flags & LM_FLAG_TRY)
1578 *p++ = 't';
1579 if (flags & LM_FLAG_TRY_1CB)
1580 *p++ = 'T';
1581 if (flags & LM_FLAG_NOEXP)
1582 *p++ = 'e';
1583 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001584 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001585 if (flags & LM_FLAG_PRIORITY)
1586 *p++ = 'p';
1587 if (flags & GL_ASYNC)
1588 *p++ = 'a';
1589 if (flags & GL_EXACT)
1590 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001591 if (flags & GL_NOCACHE)
1592 *p++ = 'c';
1593 if (test_bit(HIF_HOLDER, &iflags))
1594 *p++ = 'H';
1595 if (test_bit(HIF_WAIT, &iflags))
1596 *p++ = 'W';
1597 if (test_bit(HIF_FIRST, &iflags))
1598 *p++ = 'F';
1599 *p = 0;
1600 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001601}
1602
David Teiglandb3b94fa2006-01-16 16:50:04 +00001603/**
1604 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001605 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 * @gh: the glock holder
1607 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001608 */
1609
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001610static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001611{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001612 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001613 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001614
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001615 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001616 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001617 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001618 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1619 state2str(gh->gh_state),
1620 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1621 gh->gh_error,
1622 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1623 gh_owner ? gh_owner->comm : "(ended)",
1624 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001625 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001626}
1627
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001628static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001629{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001630 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001631 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001632
Steven Whitehouse6802e342008-05-21 17:03:22 +01001633 if (test_bit(GLF_LOCK, gflags))
1634 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001635 if (test_bit(GLF_DEMOTE, gflags))
1636 *p++ = 'D';
1637 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1638 *p++ = 'd';
1639 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1640 *p++ = 'p';
1641 if (test_bit(GLF_DIRTY, gflags))
1642 *p++ = 'y';
1643 if (test_bit(GLF_LFLUSH, gflags))
1644 *p++ = 'f';
1645 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1646 *p++ = 'i';
1647 if (test_bit(GLF_REPLY_PENDING, gflags))
1648 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001649 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001650 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001651 if (test_bit(GLF_FROZEN, gflags))
1652 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001653 if (test_bit(GLF_QUEUED, gflags))
1654 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001655 if (test_bit(GLF_LRU, gflags))
1656 *p++ = 'L';
1657 if (gl->gl_object)
1658 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001659 if (test_bit(GLF_BLOCKING, gflags))
1660 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001661 *p = 0;
1662 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001663}
1664
1665/**
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +01001666 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001667 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001668 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001669 *
1670 * The file format is as follows:
1671 * One line per object, capital letters are used to indicate objects
1672 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1673 * other objects are indented by a single space and follow the glock to
1674 * which they are related. Fields are indicated by lower case letters
1675 * followed by a colon and the field value, except for strings which are in
1676 * [] so that its possible to see if they are composed of spaces for
1677 * example. The field's are n = number (id of the object), f = flags,
1678 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680 */
1681
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001682void gfs2_dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001683{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001684 const struct gfs2_glock_operations *glops = gl->gl_ops;
1685 unsigned long long dtime;
1686 const struct gfs2_holder *gh;
1687 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001688
Steven Whitehouse6802e342008-05-21 17:03:22 +01001689 dtime = jiffies - gl->gl_demote_time;
1690 dtime *= 1000000/HZ; /* demote time in uSec */
1691 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1692 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001693 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 +01001694 state2str(gl->gl_state),
1695 gl->gl_name.ln_type,
1696 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001697 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001698 state2str(gl->gl_target),
1699 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001700 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001701 atomic_read(&gl->gl_revokes),
Steven Whitehousee66cf162013-10-15 15:18:08 +01001702 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001703
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001704 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1705 dump_holder(seq, gh);
1706
Steven Whitehouse6802e342008-05-21 17:03:22 +01001707 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001708 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709}
1710
Steven Whitehousea2457692012-01-20 10:38:36 +00001711static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1712{
1713 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001714
Ben Hutchings4d207132015-08-27 12:51:45 -05001715 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 +00001716 gl->gl_name.ln_type,
1717 (unsigned long long)gl->gl_name.ln_number,
Ben Hutchings4d207132015-08-27 12:51:45 -05001718 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1719 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1720 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1721 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1722 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1723 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1724 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1725 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001726 return 0;
1727}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728
Steven Whitehousea2457692012-01-20 10:38:36 +00001729static const char *gfs2_gltype[] = {
1730 "type",
1731 "reserved",
1732 "nondisk",
1733 "inode",
1734 "rgrp",
1735 "meta",
1736 "iopen",
1737 "flock",
1738 "plock",
1739 "quota",
1740 "journal",
1741};
1742
1743static const char *gfs2_stype[] = {
1744 [GFS2_LKS_SRTT] = "srtt",
1745 [GFS2_LKS_SRTTVAR] = "srttvar",
1746 [GFS2_LKS_SRTTB] = "srttb",
1747 [GFS2_LKS_SRTTVARB] = "srttvarb",
1748 [GFS2_LKS_SIRT] = "sirt",
1749 [GFS2_LKS_SIRTVAR] = "sirtvar",
1750 [GFS2_LKS_DCOUNT] = "dlm",
1751 [GFS2_LKS_QCOUNT] = "queue",
1752};
1753
1754#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1755
1756static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1757{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001758 struct gfs2_sbd *sdp = seq->private;
1759 loff_t pos = *(loff_t *)iter_ptr;
1760 unsigned index = pos >> 3;
1761 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001762 int i;
1763
1764 if (index == 0 && subindex != 0)
1765 return 0;
1766
1767 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1768 (index == 0) ? "cpu": gfs2_stype[subindex]);
1769
1770 for_each_possible_cpu(i) {
1771 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
Andreas Gruenbacher8f7e0a82015-08-27 13:02:54 -05001772
1773 if (index == 0)
1774 seq_printf(seq, " %15u", i);
1775 else
1776 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1777 lkstats[index - 1].stats[subindex]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001778 }
1779 seq_putc(seq, '\n');
1780 return 0;
1781}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001782
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001783int __init gfs2_glock_init(void)
1784{
Bob Peterson88ffbf32015-03-16 11:02:46 -05001785 int ret;
1786
1787 ret = rhashtable_init(&gl_hash_table, &ht_parms);
1788 if (ret < 0)
1789 return ret;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001790
Steven Whitehoused2115772010-11-03 19:58:53 +00001791 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001792 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001793 if (!glock_workqueue) {
1794 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001795 return -ENOMEM;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001796 }
Steven Whitehoused2115772010-11-03 19:58:53 +00001797 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001798 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001799 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001800 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001801 destroy_workqueue(glock_workqueue);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001802 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001803 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001804 }
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001805
Chao Yue0d735c2016-09-21 12:09:40 -05001806 ret = register_shrinker(&glock_shrinker);
1807 if (ret) {
1808 destroy_workqueue(gfs2_delete_workqueue);
1809 destroy_workqueue(glock_workqueue);
1810 rhashtable_destroy(&gl_hash_table);
1811 return ret;
1812 }
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001813
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001814 return 0;
1815}
1816
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001817void gfs2_glock_exit(void)
1818{
Steven Whitehouse97cc1022008-11-20 13:39:47 +00001819 unregister_shrinker(&glock_shrinker);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001820 rhashtable_destroy(&gl_hash_table);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001821 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001822 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001823}
1824
Bob Peterson88ffbf32015-03-16 11:02:46 -05001825static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001826{
Dan Carpenter97766c62016-12-14 08:02:03 -06001827 while ((gi->gl = rhashtable_walk_next(&gi->hti))) {
Bob Peterson88ffbf32015-03-16 11:02:46 -05001828 if (IS_ERR(gi->gl)) {
1829 if (PTR_ERR(gi->gl) == -EAGAIN)
1830 continue;
1831 gi->gl = NULL;
Dan Carpenter97766c62016-12-14 08:02:03 -06001832 return;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001833 }
Dan Carpenter97766c62016-12-14 08:02:03 -06001834 /* Skip entries for other sb and dead entries */
1835 if (gi->sdp == gi->gl->gl_name.ln_sbd &&
1836 !__lockref_is_dead(&gi->gl->gl_lockref))
1837 return;
1838 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001839}
1840
Steven Whitehouse6802e342008-05-21 17:03:22 +01001841static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Robert Peterson7c52b162007-03-16 10:26:37 +00001842{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001843 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001844 loff_t n = *pos;
1845
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001846 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
1847 if (rhashtable_walk_start(&gi->hti) != 0)
Bob Peterson88ffbf32015-03-16 11:02:46 -05001848 return NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001849
Steven Whitehouse6802e342008-05-21 17:03:22 +01001850 do {
Bob Peterson88ffbf32015-03-16 11:02:46 -05001851 gfs2_glock_iter_next(gi);
1852 } while (gi->gl && n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001853
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001854 gi->last_pos = *pos;
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001855
Steven Whitehouse6802e342008-05-21 17:03:22 +01001856 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001857}
1858
Steven Whitehouse6802e342008-05-21 17:03:22 +01001859static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001860 loff_t *pos)
1861{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001862 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001863
1864 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001865 gi->last_pos = *pos;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001866 gfs2_glock_iter_next(gi);
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001867
Steven Whitehouse6802e342008-05-21 17:03:22 +01001868 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001869}
1870
Steven Whitehouse6802e342008-05-21 17:03:22 +01001871static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001872{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001873 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001874
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001875 gi->gl = NULL;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001876 rhashtable_walk_stop(&gi->hti);
Andreas Gruenbachere2f803482017-09-19 07:15:35 -05001877 rhashtable_walk_exit(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00001878}
1879
Steven Whitehouse6802e342008-05-21 17:03:22 +01001880static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001881{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001882 dump_glock(seq, iter_ptr);
1883 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00001884}
1885
Steven Whitehousea2457692012-01-20 10:38:36 +00001886static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
1887{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001888 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00001889 if (*pos >= GFS2_NR_SBSTATS)
1890 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001891 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001892}
1893
1894static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
1895 loff_t *pos)
1896{
Steven Whitehousea2457692012-01-20 10:38:36 +00001897 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001898 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00001899 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001900 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001901}
1902
1903static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
1904{
1905 preempt_enable();
1906}
1907
Denis Cheng4ef29002007-07-31 18:31:11 +08001908static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001909 .start = gfs2_glock_seq_start,
1910 .next = gfs2_glock_seq_next,
1911 .stop = gfs2_glock_seq_stop,
1912 .show = gfs2_glock_seq_show,
1913};
1914
Steven Whitehousea2457692012-01-20 10:38:36 +00001915static const struct seq_operations gfs2_glstats_seq_ops = {
1916 .start = gfs2_glock_seq_start,
1917 .next = gfs2_glock_seq_next,
1918 .stop = gfs2_glock_seq_stop,
1919 .show = gfs2_glstats_seq_show,
1920};
1921
1922static const struct seq_operations gfs2_sbstats_seq_ops = {
1923 .start = gfs2_sbstats_seq_start,
1924 .next = gfs2_sbstats_seq_next,
1925 .stop = gfs2_sbstats_seq_stop,
1926 .show = gfs2_sbstats_seq_show,
1927};
1928
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001929#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
1930
Steven Whitehousea2457692012-01-20 10:38:36 +00001931static int gfs2_glocks_open(struct inode *inode, struct file *file)
Robert Peterson7c52b162007-03-16 10:26:37 +00001932{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001933 int ret = seq_open_private(file, &gfs2_glock_seq_ops,
1934 sizeof(struct gfs2_glock_iter));
1935 if (ret == 0) {
1936 struct seq_file *seq = file->private_data;
1937 struct gfs2_glock_iter *gi = seq->private;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001938
Steven Whitehouse6802e342008-05-21 17:03:22 +01001939 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001940 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01001941 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001942 seq->size = GFS2_SEQ_GOODSIZE;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001943 gi->gl = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001944 }
1945 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00001946}
1947
Bob Peterson88ffbf32015-03-16 11:02:46 -05001948static int gfs2_glocks_release(struct inode *inode, struct file *file)
1949{
1950 struct seq_file *seq = file->private_data;
1951 struct gfs2_glock_iter *gi = seq->private;
1952
1953 gi->gl = NULL;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001954 return seq_release_private(inode, file);
1955}
1956
Steven Whitehousea2457692012-01-20 10:38:36 +00001957static int gfs2_glstats_open(struct inode *inode, struct file *file)
1958{
1959 int ret = seq_open_private(file, &gfs2_glstats_seq_ops,
1960 sizeof(struct gfs2_glock_iter));
1961 if (ret == 0) {
1962 struct seq_file *seq = file->private_data;
1963 struct gfs2_glock_iter *gi = seq->private;
1964 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001965 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01001966 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01001967 seq->size = GFS2_SEQ_GOODSIZE;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001968 gi->gl = NULL;
Steven Whitehousea2457692012-01-20 10:38:36 +00001969 }
1970 return ret;
1971}
1972
1973static int gfs2_sbstats_open(struct inode *inode, struct file *file)
1974{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001975 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00001976 if (ret == 0) {
1977 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001978 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00001979 }
1980 return ret;
1981}
1982
1983static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00001984 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00001985 .open = gfs2_glocks_open,
1986 .read = seq_read,
1987 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05001988 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00001989};
1990
1991static const struct file_operations gfs2_glstats_fops = {
1992 .owner = THIS_MODULE,
1993 .open = gfs2_glstats_open,
1994 .read = seq_read,
1995 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05001996 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00001997};
1998
1999static const struct file_operations gfs2_sbstats_fops = {
2000 .owner = THIS_MODULE,
2001 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00002002 .read = seq_read,
2003 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002004 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00002005};
2006
2007int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2008{
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002009 struct dentry *dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002010
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002011 dent = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2012 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002013 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002014 sdp->debugfs_dir = dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002015
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002016 dent = debugfs_create_file("glocks",
2017 S_IFREG | S_IRUGO,
2018 sdp->debugfs_dir, sdp,
2019 &gfs2_glocks_fops);
2020 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002021 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002022 sdp->debugfs_dentry_glocks = dent;
2023
2024 dent = debugfs_create_file("glstats",
2025 S_IFREG | S_IRUGO,
2026 sdp->debugfs_dir, sdp,
2027 &gfs2_glstats_fops);
2028 if (IS_ERR_OR_NULL(dent))
2029 goto fail;
2030 sdp->debugfs_dentry_glstats = dent;
2031
2032 dent = debugfs_create_file("sbstats",
2033 S_IFREG | S_IRUGO,
2034 sdp->debugfs_dir, sdp,
2035 &gfs2_sbstats_fops);
2036 if (IS_ERR_OR_NULL(dent))
2037 goto fail;
2038 sdp->debugfs_dentry_sbstats = dent;
Robert Peterson7c52b162007-03-16 10:26:37 +00002039
2040 return 0;
Steven Whitehousea2457692012-01-20 10:38:36 +00002041fail:
2042 gfs2_delete_debugfs_file(sdp);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002043 return dent ? PTR_ERR(dent) : -ENOMEM;
Robert Peterson7c52b162007-03-16 10:26:37 +00002044}
2045
2046void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2047{
Steven Whitehousea2457692012-01-20 10:38:36 +00002048 if (sdp->debugfs_dir) {
Robert Peterson5f882092007-04-18 11:41:11 -05002049 if (sdp->debugfs_dentry_glocks) {
2050 debugfs_remove(sdp->debugfs_dentry_glocks);
2051 sdp->debugfs_dentry_glocks = NULL;
2052 }
Steven Whitehousea2457692012-01-20 10:38:36 +00002053 if (sdp->debugfs_dentry_glstats) {
2054 debugfs_remove(sdp->debugfs_dentry_glstats);
2055 sdp->debugfs_dentry_glstats = NULL;
2056 }
2057 if (sdp->debugfs_dentry_sbstats) {
2058 debugfs_remove(sdp->debugfs_dentry_sbstats);
2059 sdp->debugfs_dentry_sbstats = NULL;
2060 }
Robert Peterson5f882092007-04-18 11:41:11 -05002061 debugfs_remove(sdp->debugfs_dir);
2062 sdp->debugfs_dir = NULL;
2063 }
Robert Peterson7c52b162007-03-16 10:26:37 +00002064}
2065
2066int gfs2_register_debugfs(void)
2067{
2068 gfs2_root = debugfs_create_dir("gfs2", NULL);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002069 if (IS_ERR(gfs2_root))
2070 return PTR_ERR(gfs2_root);
Robert Peterson7c52b162007-03-16 10:26:37 +00002071 return gfs2_root ? 0 : -ENOMEM;
2072}
2073
2074void gfs2_unregister_debugfs(void)
2075{
2076 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002077 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002078}