blob: 11066d8647d29320dcb30d063536aa4f218cf5ec [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>
Andreas Gruenbacher05154802017-08-01 11:18:26 -050018#include <linux/hash.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019#include <linux/jhash.h>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050020#include <linux/kallsyms.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include <linux/gfs2_ondisk.h>
Steven Whitehouse24264432006-09-11 21:40:30 -040022#include <linux/list.h>
Steven Whitehousefee852e2007-01-17 15:33:23 +000023#include <linux/wait.h>
akpm@linux-foundation.org95d97b72007-03-05 23:10:39 -080024#include <linux/module.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Robert Peterson7c52b162007-03-16 10:26:37 +000026#include <linux/seq_file.h>
27#include <linux/debugfs.h>
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +010028#include <linux/kthread.h>
29#include <linux/freezer.h>
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050030#include <linux/workqueue.h>
31#include <linux/jiffies.h>
Steven Whitehousebc015cb2011-01-19 09:30:01 +000032#include <linux/rcupdate.h>
33#include <linux/rculist_bl.h>
34#include <linux/bit_spinlock.h>
Steven Whitehousea2457692012-01-20 10:38:36 +000035#include <linux/percpu.h>
Steven Whitehouse4506a5192013-02-01 20:36:03 +000036#include <linux/list_sort.h>
Steven Whitehousee66cf162013-10-15 15:18:08 +010037#include <linux/lockref.h>
Bob Peterson88ffbf32015-03-16 11:02:46 -050038#include <linux/rhashtable.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
40#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050041#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000042#include "glock.h"
43#include "glops.h"
44#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000045#include "lops.h"
46#include "meta_io.h"
47#include "quota.h"
48#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050049#include "util.h"
Steven Whitehouse813e0c42008-11-18 13:38:48 +000050#include "bmap.h"
Steven Whitehouse63997772009-06-12 08:49:20 +010051#define CREATE_TRACE_POINTS
52#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000053
Steven Whitehouse6802e342008-05-21 17:03:22 +010054struct gfs2_glock_iter {
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +010055 struct gfs2_sbd *sdp; /* incore superblock */
Bob Peterson88ffbf32015-03-16 11:02:46 -050056 struct rhashtable_iter hti; /* rhashtable iterator */
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +010057 struct gfs2_glock *gl; /* current glock struct */
58 loff_t last_pos; /* last position */
Robert Peterson7c52b162007-03-16 10:26:37 +000059};
60
David Teiglandb3b94fa2006-01-16 16:50:04 +000061typedef void (*glock_examiner) (struct gfs2_glock * gl);
62
Steven Whitehouse6802e342008-05-21 17:03:22 +010063static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050064
Robert Peterson7c52b162007-03-16 10:26:37 +000065static struct dentry *gfs2_root;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -050066static struct workqueue_struct *glock_workqueue;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -050067struct workqueue_struct *gfs2_delete_workqueue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +000068static LIST_HEAD(lru_list);
69static atomic_t lru_count = ATOMIC_INIT(0);
Julia Lawalleb8374e2008-12-25 15:35:27 +010070static DEFINE_SPINLOCK(lru_lock);
Adrian Bunk08bc2db2006-04-28 10:59:12 -040071
Steven Whitehouseb6397892006-09-12 10:10:01 -040072#define GFS2_GL_HASH_SHIFT 15
Fabian Frederick47a9a522016-08-02 12:05:27 -050073#define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT)
Steven Whitehouse087efdd2006-09-09 16:59:11 -040074
Arvind Yadavd296b152017-08-30 07:50:03 -050075static const struct rhashtable_params ht_parms = {
Bob Peterson88ffbf32015-03-16 11:02:46 -050076 .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
Andreas Gruenbacher972b0442017-03-16 09:54:57 -040077 .key_len = offsetofend(struct lm_lockname, ln_type),
Bob Peterson88ffbf32015-03-16 11:02:46 -050078 .key_offset = offsetof(struct gfs2_glock, gl_name),
79 .head_offset = offsetof(struct gfs2_glock, gl_node),
80};
Steven Whitehouse087efdd2006-09-09 16:59:11 -040081
Bob Peterson88ffbf32015-03-16 11:02:46 -050082static struct rhashtable gl_hash_table;
David Teiglandb3b94fa2006-01-16 16:50:04 +000083
Andreas Gruenbacher05154802017-08-01 11:18:26 -050084#define GLOCK_WAIT_TABLE_BITS 12
85#define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS)
86static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned;
87
88struct wait_glock_queue {
89 struct lm_lockname *name;
90 wait_queue_entry_t wait;
91};
92
93static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode,
94 int sync, void *key)
95{
96 struct wait_glock_queue *wait_glock =
97 container_of(wait, struct wait_glock_queue, wait);
98 struct lm_lockname *wait_name = wait_glock->name;
99 struct lm_lockname *wake_name = key;
100
101 if (wake_name->ln_sbd != wait_name->ln_sbd ||
102 wake_name->ln_number != wait_name->ln_number ||
103 wake_name->ln_type != wait_name->ln_type)
104 return 0;
105 return autoremove_wake_function(wait, mode, sync, key);
106}
107
108static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name)
109{
110 u32 hash = jhash2((u32 *)name, sizeof(*name) / 4, 0);
111
112 return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS);
113}
114
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500115/**
116 * wake_up_glock - Wake up waiters on a glock
117 * @gl: the glock
118 */
119static void wake_up_glock(struct gfs2_glock *gl)
120{
121 wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
122
123 if (waitqueue_active(wq))
124 __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
125}
126
Andreas Gruenbacher961ae1d2017-07-07 13:22:05 -0500127static void gfs2_glock_dealloc(struct rcu_head *rcu)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128{
Andreas Gruenbacher961ae1d2017-07-07 13:22:05 -0500129 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000130
David Teiglanddba2d702012-11-14 13:46:53 -0500131 if (gl->gl_ops->go_flags & GLOF_ASPACE) {
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000132 kmem_cache_free(gfs2_glock_aspace_cachep, gl);
David Teiglanddba2d702012-11-14 13:46:53 -0500133 } else {
David Teigland4e2f8842012-11-14 13:47:37 -0500134 kfree(gl->gl_lksb.sb_lvbptr);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000135 kmem_cache_free(gfs2_glock_cachep, gl);
David Teiglanddba2d702012-11-14 13:46:53 -0500136 }
Andreas Gruenbacher961ae1d2017-07-07 13:22:05 -0500137}
138
139void gfs2_glock_free(struct gfs2_glock *gl)
140{
141 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
142
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500143 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
144 smp_mb();
145 wake_up_glock(gl);
Andreas Gruenbacher961ae1d2017-07-07 13:22:05 -0500146 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000147 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
148 wake_up(&sdp->sd_glock_wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149}
150
151/**
152 * gfs2_glock_hold() - increment reference count on glock
153 * @gl: The glock to hold
154 *
155 */
156
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -0500157void gfs2_glock_hold(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158{
Steven Whitehousee66cf162013-10-15 15:18:08 +0100159 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
160 lockref_get(&gl->gl_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161}
162
163/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500164 * demote_ok - Check to see if it's ok to unlock a glock
165 * @gl: the glock
166 *
167 * Returns: 1 if it's ok
168 */
169
170static int demote_ok(const struct gfs2_glock *gl)
171{
172 const struct gfs2_glock_operations *glops = gl->gl_ops;
173
174 if (gl->gl_state == LM_ST_UNLOCKED)
175 return 0;
Steven Whitehousef42ab082011-04-14 16:50:31 +0100176 if (!list_empty(&gl->gl_holders))
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500177 return 0;
178 if (glops->go_demote_ok)
179 return glops->go_demote_ok(gl);
180 return 1;
181}
182
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000183
Steven Whitehouse29687a22011-03-30 16:33:25 +0100184void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
185{
186 spin_lock(&lru_lock);
187
188 if (!list_empty(&gl->gl_lru))
189 list_del_init(&gl->gl_lru);
190 else
191 atomic_inc(&lru_count);
192
193 list_add_tail(&gl->gl_lru, &lru_list);
Steven Whitehouse627c10b2011-04-14 14:09:52 +0100194 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100195 spin_unlock(&lru_lock);
196}
197
Bob Peterson8f6cb402015-01-05 13:25:10 -0500198static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
Steven Whitehousef42ab082011-04-14 16:50:31 +0100199{
Bob Peterson645ebd42017-07-26 10:57:35 -0500200 if (!(gl->gl_ops->go_flags & GLOF_LRU))
201 return;
202
Bob Peterson8f6cb402015-01-05 13:25:10 -0500203 spin_lock(&lru_lock);
Steven Whitehousef42ab082011-04-14 16:50:31 +0100204 if (!list_empty(&gl->gl_lru)) {
205 list_del_init(&gl->gl_lru);
206 atomic_dec(&lru_count);
207 clear_bit(GLF_LRU, &gl->gl_flags);
208 }
209 spin_unlock(&lru_lock);
210}
211
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500212/*
213 * Enqueue the glock on the work queue. Passes one glock reference on to the
214 * work queue.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 */
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500216static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
217 if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) {
218 /*
219 * We are holding the lockref spinlock, and the work was still
220 * queued above. The queued work (glock_work_func) takes that
221 * spinlock before dropping its glock reference(s), so it
222 * cannot have dropped them in the meantime.
223 */
224 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
225 gl->gl_lockref.count--;
226 }
227}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500229static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
230 spin_lock(&gl->gl_lockref.lock);
231 __gfs2_glock_queue_work(gl, delay);
232 spin_unlock(&gl->gl_lockref.lock);
233}
234
235static void __gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236{
Bob Peterson15562c42015-03-16 11:52:05 -0500237 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000238 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239
Steven Whitehousee66cf162013-10-15 15:18:08 +0100240 lockref_mark_dead(&gl->gl_lockref);
241
Bob Peterson8f6cb402015-01-05 13:25:10 -0500242 gfs2_glock_remove_from_lru(gl);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100243 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100244 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
245 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
246 trace_gfs2_glock_put(gl);
247 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248}
249
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -0500250/*
251 * Cause the glock to be put in work queue context.
252 */
253void gfs2_glock_queue_put(struct gfs2_glock *gl)
254{
255 gfs2_glock_queue_work(gl, 0);
256}
257
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258/**
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500259 * gfs2_glock_put() - Decrement reference count on glock
260 * @gl: The glock to put
261 *
262 */
263
264void gfs2_glock_put(struct gfs2_glock *gl)
265{
266 if (lockref_put_or_lock(&gl->gl_lockref))
267 return;
268
269 __gfs2_glock_put(gl);
270}
271
272/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100273 * may_grant - check if its ok to grant a new lock
274 * @gl: The glock
275 * @gh: The lock request which we wish to grant
276 *
277 * Returns: true if its ok to grant the lock
278 */
279
280static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500281{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100282 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
283 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
284 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
285 return 0;
286 if (gl->gl_state == gh->gh_state)
287 return 1;
288 if (gh->gh_flags & GL_EXACT)
289 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100290 if (gl->gl_state == LM_ST_EXCLUSIVE) {
291 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
292 return 1;
293 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
294 return 1;
295 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100296 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
297 return 1;
298 return 0;
299}
300
301static void gfs2_holder_wake(struct gfs2_holder *gh)
302{
303 clear_bit(HIF_WAIT, &gh->gh_iflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100304 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100305 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
306}
307
308/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100309 * do_error - Something unexpected has happened during a lock request
310 *
311 */
312
Denys Vlasenkoa527b382016-04-12 12:39:12 -0400313static void do_error(struct gfs2_glock *gl, const int ret)
Steven Whitehoused5341a92010-07-23 14:05:51 +0100314{
315 struct gfs2_holder *gh, *tmp;
316
317 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
318 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
319 continue;
320 if (ret & LM_OUT_ERROR)
321 gh->gh_error = -EIO;
322 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
323 gh->gh_error = GLR_TRYFAILED;
324 else
325 continue;
326 list_del_init(&gh->gh_list);
327 trace_gfs2_glock_queue(gh, 0);
328 gfs2_holder_wake(gh);
329 }
330}
331
332/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100333 * do_promote - promote as many requests as possible on the current queue
334 * @gl: The glock
335 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000336 * Returns: 1 if there is a blocked holder at the head of the list, or 2
337 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100338 */
339
340static int do_promote(struct gfs2_glock *gl)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500341__releases(&gl->gl_lockref.lock)
342__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100343{
344 const struct gfs2_glock_operations *glops = gl->gl_ops;
345 struct gfs2_holder *gh, *tmp;
346 int ret;
347
348restart:
349 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
350 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
351 continue;
352 if (may_grant(gl, gh)) {
353 if (gh->gh_list.prev == &gl->gl_holders &&
354 glops->go_lock) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500355 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100356 /* FIXME: eliminate this eventually */
357 ret = glops->go_lock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500358 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100359 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000360 if (ret == 1)
361 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100362 gh->gh_error = ret;
363 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100364 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100365 gfs2_holder_wake(gh);
366 goto restart;
367 }
368 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100369 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100370 gfs2_holder_wake(gh);
371 goto restart;
372 }
373 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100374 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100375 gfs2_holder_wake(gh);
376 continue;
377 }
378 if (gh->gh_list.prev == &gl->gl_holders)
379 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100380 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100381 break;
382 }
383 return 0;
384}
385
386/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100387 * find_first_waiter - find the first gh that's waiting for the glock
388 * @gl: the glock
389 */
390
391static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
392{
393 struct gfs2_holder *gh;
394
395 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
396 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
397 return gh;
398 }
399 return NULL;
400}
401
402/**
403 * state_change - record that the glock is now in a different state
404 * @gl: the glock
405 * @new_state the new state
406 *
407 */
408
409static void state_change(struct gfs2_glock *gl, unsigned int new_state)
410{
411 int held1, held2;
412
413 held1 = (gl->gl_state != LM_ST_UNLOCKED);
414 held2 = (new_state != LM_ST_UNLOCKED);
415
416 if (held1 != held2) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100417 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100418 if (held2)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100419 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100420 else
Steven Whitehousee66cf162013-10-15 15:18:08 +0100421 gl->gl_lockref.count--;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100422 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100423 if (held1 && held2 && list_empty(&gl->gl_holders))
424 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100425
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400426 if (new_state != gl->gl_target)
427 /* shorten our minimum hold time */
428 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
429 GL_GLOCK_MIN_HOLD);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100430 gl->gl_state = new_state;
431 gl->gl_tchange = jiffies;
432}
433
434static void gfs2_demote_wake(struct gfs2_glock *gl)
435{
436 gl->gl_demote_state = LM_ST_EXCLUSIVE;
437 clear_bit(GLF_DEMOTE, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100438 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100439 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
440}
441
442/**
443 * finish_xmote - The DLM has replied to one of our lock requests
444 * @gl: The glock
445 * @ret: The status from the DLM
446 *
447 */
448
449static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
450{
451 const struct gfs2_glock_operations *glops = gl->gl_ops;
452 struct gfs2_holder *gh;
453 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000454 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500455
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500456 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse63997772009-06-12 08:49:20 +0100457 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100458 state_change(gl, state);
459 gh = find_first_waiter(gl);
460
461 /* Demote to UN request arrived during demote to SH or DF */
462 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
463 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
464 gl->gl_target = LM_ST_UNLOCKED;
465
466 /* Check for state != intended state */
467 if (unlikely(state != gl->gl_target)) {
468 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
469 /* move to back of queue and try next entry */
470 if (ret & LM_OUT_CANCELED) {
471 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
472 list_move_tail(&gh->gh_list, &gl->gl_holders);
473 gh = find_first_waiter(gl);
474 gl->gl_target = gh->gh_state;
475 goto retry;
476 }
477 /* Some error or failed "try lock" - report it */
478 if ((ret & LM_OUT_ERROR) ||
479 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
480 gl->gl_target = gl->gl_state;
481 do_error(gl, ret);
482 goto out;
483 }
484 }
485 switch(state) {
486 /* Unlocked due to conversion deadlock, try again */
487 case LM_ST_UNLOCKED:
488retry:
489 do_xmote(gl, gh, gl->gl_target);
490 break;
491 /* Conversion fails, unlock and try again */
492 case LM_ST_SHARED:
493 case LM_ST_DEFERRED:
494 do_xmote(gl, gh, LM_ST_UNLOCKED);
495 break;
496 default: /* Everything else */
Joe Perchesd77d1b52014-03-06 12:10:45 -0800497 pr_err("wanted %u got %u\n", gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100498 GLOCK_BUG_ON(gl, 1);
499 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500500 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100501 return;
502 }
503
504 /* Fast path - we got what we asked for */
505 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
506 gfs2_demote_wake(gl);
507 if (state != LM_ST_UNLOCKED) {
508 if (glops->go_xmote_bh) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500509 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100510 rv = glops->go_xmote_bh(gl, gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500511 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100512 if (rv) {
513 do_error(gl, rv);
514 goto out;
515 }
516 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000517 rv = do_promote(gl);
518 if (rv == 2)
519 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100520 }
521out:
522 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000523out_locked:
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500524 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500525}
526
Steven Whitehouse6802e342008-05-21 17:03:22 +0100527/**
528 * do_xmote - Calls the DLM to change the state of a lock
529 * @gl: The lock state
530 * @gh: The holder (only for promotes)
531 * @target: The target lock state
532 *
533 */
534
535static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500536__releases(&gl->gl_lockref.lock)
537__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100538{
539 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500540 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Bob Petersonb58bf402015-07-24 09:45:43 -0500541 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100542 int ret;
543
Bob Petersoned175452017-05-05 09:43:02 -0500544 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) &&
545 target != LM_ST_UNLOCKED)
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500546 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100547 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
548 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000549 GLOCK_BUG_ON(gl, gl->gl_state == target);
550 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100551 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
552 glops->go_inval) {
553 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
554 do_error(gl, 0); /* Fail queued try locks */
555 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000556 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000557 set_bit(GLF_BLOCKING, &gl->gl_flags);
558 if ((gl->gl_req == LM_ST_UNLOCKED) ||
559 (gl->gl_state == LM_ST_EXCLUSIVE) ||
560 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
561 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500562 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson06dfc302012-10-24 14:41:05 -0400563 if (glops->go_sync)
564 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100565 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
566 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
567 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
568
569 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000570 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
571 /* lock_dlm */
572 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400573 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
574 target == LM_ST_UNLOCKED &&
575 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
576 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500577 gfs2_glock_queue_work(gl, 0);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400578 }
579 else if (ret) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800580 pr_err("lm_lock ret %d\n", ret);
Bob Petersoned175452017-05-05 09:43:02 -0500581 GLOCK_BUG_ON(gl, !test_bit(SDF_SHUTDOWN,
582 &sdp->sd_flags));
David Teiglanddba2d702012-11-14 13:46:53 -0500583 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000584 } else { /* lock_nolock */
585 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500586 gfs2_glock_queue_work(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100587 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000588
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500589 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100590}
591
592/**
593 * find_first_holder - find the first "holder" gh
594 * @gl: the glock
595 */
596
597static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
598{
599 struct gfs2_holder *gh;
600
601 if (!list_empty(&gl->gl_holders)) {
602 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
603 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
604 return gh;
605 }
606 return NULL;
607}
608
609/**
610 * run_queue - do all outstanding tasks related to a glock
611 * @gl: The glock in question
612 * @nonblock: True if we must not block in run_queue
613 *
614 */
615
616static void run_queue(struct gfs2_glock *gl, const int nonblock)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500617__releases(&gl->gl_lockref.lock)
618__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100619{
620 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000621 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100622
623 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
624 return;
625
626 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
627
628 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
629 gl->gl_demote_state != gl->gl_state) {
630 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000631 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100632 if (nonblock)
633 goto out_sched;
634 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100635 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100636 gl->gl_target = gl->gl_demote_state;
637 } else {
638 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
639 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000640 ret = do_promote(gl);
641 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000642 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000643 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100644 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100645 gh = find_first_waiter(gl);
646 gl->gl_target = gh->gh_state;
647 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
648 do_error(gl, 0); /* Fail queued try locks */
649 }
650 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100651out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100652 return;
653
654out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100655 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100656 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100657 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500658 __gfs2_glock_queue_work(gl, 0);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100659 return;
660
Steven Whitehoused8348de2009-02-05 10:12:38 +0000661out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100662 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100663 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100664 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100665}
666
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500667static void delete_work_func(struct work_struct *work)
668{
669 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500670 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500671 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000672 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500673
Bob Petersona4923862015-12-07 16:24:27 -0600674 /* If someone's using this glock to create a new dinode, the block must
675 have been freed by another node, then re-used, in which case our
676 iopen callback is too late after the fact. Ignore it. */
677 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
678 goto out;
679
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500680 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000681 if (inode && !IS_ERR(inode)) {
682 d_prune_aliases(inode);
683 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500684 }
Bob Petersona4923862015-12-07 16:24:27 -0600685out:
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500686 gfs2_glock_put(gl);
687}
688
Steven Whitehouse6802e342008-05-21 17:03:22 +0100689static void glock_work_func(struct work_struct *work)
690{
691 unsigned long delay = 0;
692 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500693 unsigned int drop_refs = 1;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100694
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000695 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100696 finish_xmote(gl, gl->gl_reply);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500697 drop_refs++;
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000698 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500699 spin_lock(&gl->gl_lockref.lock);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400700 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100701 gl->gl_state != LM_ST_UNLOCKED &&
702 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100703 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400704
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400705 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100706 if (time_before(now, holdtime))
707 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400708
709 if (!delay) {
710 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
711 set_bit(GLF_DEMOTE, &gl->gl_flags);
712 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100713 }
714 run_queue(gl, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500715 if (delay) {
716 /* Keep one glock reference for the work we requeue. */
717 drop_refs--;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400718 if (gl->gl_name.ln_type != LM_TYPE_INODE)
719 delay = 0;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500720 __gfs2_glock_queue_work(gl, delay);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400721 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500722
723 /*
724 * Drop the remaining glock references manually here. (Mind that
725 * __gfs2_glock_queue_work depends on the lockref spinlock begin held
726 * here as well.)
727 */
728 gl->gl_lockref.count -= drop_refs;
729 if (!gl->gl_lockref.count) {
730 __gfs2_glock_put(gl);
731 return;
732 }
733 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100734}
735
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500736static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
737 struct gfs2_glock *new)
738{
739 struct wait_glock_queue wait;
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500740 wait_queue_head_t *wq = glock_waitqueue(name);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500741 struct gfs2_glock *gl;
742
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500743 wait.name = name;
744 init_wait(&wait.wait);
745 wait.wait.func = glock_wake_function;
746
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500747again:
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500748 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500749 rcu_read_lock();
750 if (new) {
751 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
752 &new->gl_node, ht_parms);
753 if (IS_ERR(gl))
754 goto out;
755 } else {
756 gl = rhashtable_lookup_fast(&gl_hash_table,
757 name, ht_parms);
758 }
759 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
760 rcu_read_unlock();
761 schedule();
762 goto again;
763 }
764out:
765 rcu_read_unlock();
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500766 finish_wait(wq, &wait.wait);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500767 return gl;
768}
769
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770/**
771 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
772 * @sdp: The GFS2 superblock
773 * @number: the lock number
774 * @glops: The glock_operations to use
775 * @create: If 0, don't create the glock if it doesn't exist
776 * @glp: the glock is returned here
777 *
778 * This does not lock a glock, just finds/creates structures for one.
779 *
780 * Returns: errno
781 */
782
Steven Whitehousecd915492006-09-04 12:49:07 -0400783int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400784 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785 struct gfs2_glock **glp)
786{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000787 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500788 struct lm_lockname name = { .ln_number = number,
789 .ln_type = glops->go_type,
790 .ln_sbd = sdp };
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100791 struct gfs2_glock *gl, *tmp;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000792 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000793 struct kmem_cache *cachep;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100794 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500796 gl = find_insert_glock(&name, NULL);
797 if (gl) {
798 *glp = gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 return 0;
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500800 }
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000801 if (!create)
802 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803
Steven Whitehouse009d8512009-12-08 12:12:13 +0000804 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000805 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000806 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000807 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100808 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 if (!gl)
810 return -ENOMEM;
811
David Teiglanddba2d702012-11-14 13:46:53 -0500812 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500813
814 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100815 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500816 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500817 kmem_cache_free(cachep, gl);
818 return -ENOMEM;
819 }
David Teiglanddba2d702012-11-14 13:46:53 -0500820 }
821
Steven Whitehouse8f052282010-01-29 15:21:27 +0000822 atomic_inc(&sdp->sd_glock_disposal);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500823 gl->gl_node.next = NULL;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400824 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100826 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100828 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500829 gl->gl_demote_state = LM_ST_EXCLUSIVE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 gl->gl_ops = glops;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100831 gl->gl_dstamp = 0;
Steven Whitehousea2457692012-01-20 10:38:36 +0000832 preempt_disable();
833 /* We use the global stats to estimate the initial per-glock stats */
834 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
835 preempt_enable();
836 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
837 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500838 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400839 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400840 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500841 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500842 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843
Steven Whitehouse009d8512009-12-08 12:12:13 +0000844 mapping = gfs2_glock2aspace(gl);
845 if (mapping) {
846 mapping->a_ops = &gfs2_meta_aops;
847 mapping->host = s->s_bdev->bd_inode;
848 mapping->flags = 0;
849 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800850 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000851 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852 }
853
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500854 tmp = find_insert_glock(&name, gl);
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100855 if (!tmp) {
Bob Peterson88ffbf32015-03-16 11:02:46 -0500856 *glp = gl;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100857 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 }
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100859 if (IS_ERR(tmp)) {
860 ret = PTR_ERR(tmp);
861 goto out_free;
862 }
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500863 *glp = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100865out_free:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500866 kfree(gl->gl_lksb.sb_lvbptr);
867 kmem_cache_free(cachep, gl);
868 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100870out:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500871 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872}
873
874/**
875 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
876 * @gl: the glock
877 * @state: the state we're requesting
878 * @flags: the modifier flags
879 * @gh: the holder structure
880 *
881 */
882
Bob Petersonb58bf402015-07-24 09:45:43 -0500883void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000884 struct gfs2_holder *gh)
885{
886 INIT_LIST_HEAD(&gh->gh_list);
887 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200888 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800889 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 gh->gh_state = state;
891 gh->gh_flags = flags;
892 gh->gh_error = 0;
893 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 gfs2_glock_hold(gl);
895}
896
897/**
898 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
899 * @state: the state we're requesting
900 * @flags: the modifier flags
901 * @gh: the holder structure
902 *
903 * Don't mess with the glock.
904 *
905 */
906
Bob Petersonb58bf402015-07-24 09:45:43 -0500907void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000908{
909 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400910 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000911 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200912 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100913 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400914 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000915}
916
917/**
918 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
919 * @gh: the holder structure
920 *
921 */
922
923void gfs2_holder_uninit(struct gfs2_holder *gh)
924{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800925 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 gfs2_glock_put(gh->gh_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500927 gfs2_holder_mark_uninitialized(gh);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500928 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929}
930
Steven Whitehousefe64d512009-05-19 10:01:18 +0100931/**
Bob Peterson07a79042012-08-09 12:48:44 -0500932 * gfs2_glock_wait - wait on a glock acquisition
933 * @gh: the glock holder
934 *
935 * Returns: 0 on success
936 */
937
938int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000939{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400940 unsigned long time1 = jiffies;
941
Steven Whitehousefee852e2007-01-17 15:33:23 +0000942 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000943 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400944 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
945 /* Lengthen the minimum hold time. */
946 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
947 GL_GLOCK_HOLD_INCR,
948 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500949 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100950}
951
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000953 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954 * @gl: the glock
955 * @state: the state the caller wants us to change to
956 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000957 * There are only two requests that we are going to see in actual
958 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 */
960
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500961static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100962 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500964 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
965
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500966 set_bit(bit, &gl->gl_flags);
967 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000968 gl->gl_demote_state = state;
969 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100970 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
971 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100972 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000973 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500974 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100975 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100976 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977}
978
Steven Whitehouse6802e342008-05-21 17:03:22 +0100979void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000980{
Joe Perches5e690692010-11-09 16:35:20 -0800981 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000982 va_list args;
983
984 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800985
Steven Whitehouse6802e342008-05-21 17:03:22 +0100986 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100987 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100988 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800989 vaf.fmt = fmt;
990 vaf.va = &args;
991
Joe Perchesd77d1b52014-03-06 12:10:45 -0800992 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100993 }
Joe Perches5e690692010-11-09 16:35:20 -0800994
Robert Peterson7c52b162007-03-16 10:26:37 +0000995 va_end(args);
996}
997
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1000 * @gh: the holder structure to add
1001 *
Steven Whitehouse6802e342008-05-21 17:03:22 +01001002 * Eventually we should move the recursive locking trap to a
1003 * debugging option or something like that. This is the fast
1004 * path and needs to have the minimum number of distractions.
1005 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006 */
1007
Steven Whitehouse6802e342008-05-21 17:03:22 +01001008static inline void add_to_queue(struct gfs2_holder *gh)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001009__releases(&gl->gl_lockref.lock)
1010__acquires(&gl->gl_lockref.lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011{
1012 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001013 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001014 struct list_head *insert_pt = NULL;
1015 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001016 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001018 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +00001019 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1020 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -04001021
Steven Whitehouse6802e342008-05-21 17:03:22 +01001022 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1023 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -05001024 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001025 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1026 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 }
1028
Steven Whitehouse6802e342008-05-21 17:03:22 +01001029 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1030 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
1031 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
1032 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001033 if (try_futile &&
1034 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001035fail:
1036 gh->gh_error = GLR_TRYFAILED;
1037 gfs2_holder_wake(gh);
1038 return;
1039 }
1040 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1041 continue;
1042 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
1043 insert_pt = &gh2->gh_list;
1044 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001045 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +00001046 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +00001047 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1048 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001049 if (likely(insert_pt == NULL)) {
1050 list_add_tail(&gh->gh_list, &gl->gl_holders);
1051 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
1052 goto do_cancel;
1053 return;
1054 }
1055 list_add_tail(&gh->gh_list, insert_pt);
1056do_cancel:
1057 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
1058 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001059 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +01001060 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001061 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001062 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001063 }
1064 return;
1065
1066trap_recursive:
Fabian Frederickfc554ed2014-03-05 22:06:42 +08001067 pr_err("original: %pSR\n", (void *)gh2->gh_ip);
1068 pr_err("pid: %d\n", pid_nr(gh2->gh_owner_pid));
1069 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001070 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Fabian Frederickfc554ed2014-03-05 22:06:42 +08001071 pr_err("new: %pSR\n", (void *)gh->gh_ip);
1072 pr_err("pid: %d\n", pid_nr(gh->gh_owner_pid));
1073 pr_err("lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001074 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +01001075 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001076 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077}
1078
1079/**
1080 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1081 * @gh: the holder structure
1082 *
1083 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1084 *
1085 * Returns: 0, GLR_TRYFAILED, or errno on failure
1086 */
1087
1088int gfs2_glock_nq(struct gfs2_holder *gh)
1089{
1090 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001091 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 int error = 0;
1093
Steven Whitehouse6802e342008-05-21 17:03:22 +01001094 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001095 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096
Steven Whitehousef42ab082011-04-14 16:50:31 +01001097 if (test_bit(GLF_LRU, &gl->gl_flags))
1098 gfs2_glock_remove_from_lru(gl);
1099
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001100 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -04001102 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1103 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001104 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -04001105 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001106 __gfs2_glock_queue_work(gl, 0);
Bob Peterson01b172b2014-03-12 10:32:20 -04001107 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001108 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001109 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110
Steven Whitehouse6802e342008-05-21 17:03:22 +01001111 if (!(gh->gh_flags & GL_ASYNC))
1112 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 return error;
1115}
1116
1117/**
1118 * gfs2_glock_poll - poll to see if an async request has been completed
1119 * @gh: the holder
1120 *
1121 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1122 */
1123
1124int gfs2_glock_poll(struct gfs2_holder *gh)
1125{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001126 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127}
1128
1129/**
1130 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1131 * @gh: the glock holder
1132 *
1133 */
1134
1135void gfs2_glock_dq(struct gfs2_holder *gh)
1136{
1137 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001138 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001139 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001140 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001142 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001144 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146 list_del_init(&gh->gh_list);
Bob Peterson7508abc2015-12-18 11:54:55 -06001147 clear_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001148 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001149 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001150 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001151 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 glops->go_unlock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001153 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001154 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2c2007-11-02 08:39:34 +00001155 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001156 if (list_empty(&gl->gl_holders) &&
1157 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1158 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1159 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160 }
Bob Petersone7ccaf52015-06-12 13:15:54 -05001161 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl) &&
1162 (glops->go_flags & GLOF_LRU))
Bob Peterson4abb6ad2012-08-09 12:48:43 -05001163 gfs2_glock_add_to_lru(gl);
1164
Steven Whitehouse63997772009-06-12 08:49:20 +01001165 trace_gfs2_glock_queue(gh, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001166 if (unlikely(!fast_path)) {
1167 gl->gl_lockref.count++;
1168 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1169 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1170 gl->gl_name.ln_type == LM_TYPE_INODE)
1171 delay = gl->gl_hold_time;
1172 __gfs2_glock_queue_work(gl, delay);
1173 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001174 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175}
1176
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001177void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1178{
1179 struct gfs2_glock *gl = gh->gh_gl;
1180 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001181 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001182 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001183}
1184
David Teiglandb3b94fa2006-01-16 16:50:04 +00001185/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1187 * @gh: the holder structure
1188 *
1189 */
1190
1191void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1192{
1193 gfs2_glock_dq(gh);
1194 gfs2_holder_uninit(gh);
1195}
1196
1197/**
1198 * gfs2_glock_nq_num - acquire a glock based on lock number
1199 * @sdp: the filesystem
1200 * @number: the lock number
1201 * @glops: the glock operations for the type of glock
1202 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001203 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 * @gh: the struct gfs2_holder
1205 *
1206 * Returns: errno
1207 */
1208
Steven Whitehousecd915492006-09-04 12:49:07 -04001209int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001210 const struct gfs2_glock_operations *glops,
Bob Petersonb58bf402015-07-24 09:45:43 -05001211 unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001212{
1213 struct gfs2_glock *gl;
1214 int error;
1215
1216 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1217 if (!error) {
1218 error = gfs2_glock_nq_init(gl, state, flags, gh);
1219 gfs2_glock_put(gl);
1220 }
1221
1222 return error;
1223}
1224
1225/**
1226 * glock_compare - Compare two struct gfs2_glock structures for sorting
1227 * @arg_a: the first structure
1228 * @arg_b: the second structure
1229 *
1230 */
1231
1232static int glock_compare(const void *arg_a, const void *arg_b)
1233{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001234 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1235 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1236 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1237 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238
1239 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001240 return 1;
1241 if (a->ln_number < b->ln_number)
1242 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001243 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 -04001244 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001245}
1246
1247/**
1248 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1249 * @num_gh: the number of structures
1250 * @ghs: an array of struct gfs2_holder structures
1251 *
1252 * Returns: 0 on success (all glocks acquired),
1253 * errno on failure (no glocks acquired)
1254 */
1255
1256static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1257 struct gfs2_holder **p)
1258{
1259 unsigned int x;
1260 int error = 0;
1261
1262 for (x = 0; x < num_gh; x++)
1263 p[x] = &ghs[x];
1264
1265 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1266
1267 for (x = 0; x < num_gh; x++) {
1268 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1269
1270 error = gfs2_glock_nq(p[x]);
1271 if (error) {
1272 while (x--)
1273 gfs2_glock_dq(p[x]);
1274 break;
1275 }
1276 }
1277
1278 return error;
1279}
1280
1281/**
1282 * gfs2_glock_nq_m - acquire multiple glocks
1283 * @num_gh: the number of structures
1284 * @ghs: an array of struct gfs2_holder structures
1285 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286 *
1287 * Returns: 0 on success (all glocks acquired),
1288 * errno on failure (no glocks acquired)
1289 */
1290
1291int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1292{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001293 struct gfs2_holder *tmp[4];
1294 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295 int error = 0;
1296
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001297 switch(num_gh) {
1298 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001299 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001300 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1302 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001303 default:
1304 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001305 break;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001306 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1307 if (!pph)
1308 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 }
1310
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001311 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001313 if (pph != tmp)
1314 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315
1316 return error;
1317}
1318
1319/**
1320 * gfs2_glock_dq_m - release multiple glocks
1321 * @num_gh: the number of structures
1322 * @ghs: an array of struct gfs2_holder structures
1323 *
1324 */
1325
1326void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1327{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001328 while (num_gh--)
1329 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330}
1331
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001332void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001333{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001334 unsigned long delay = 0;
1335 unsigned long holdtime;
1336 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001338 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001339 holdtime = gl->gl_tchange + gl->gl_hold_time;
1340 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1341 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001342 if (time_before(now, holdtime))
1343 delay = holdtime - now;
1344 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001345 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001346 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001347
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001348 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001349 handle_callback(gl, state, delay, true);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001350 __gfs2_glock_queue_work(gl, delay);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001351 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352}
1353
1354/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001355 * gfs2_should_freeze - Figure out if glock should be frozen
1356 * @gl: The glock in question
1357 *
1358 * Glocks are not frozen if (a) the result of the dlm operation is
1359 * an error, (b) the locking operation was an unlock operation or
1360 * (c) if there is a "noexp" flagged request anywhere in the queue
1361 *
1362 * Returns: 1 if freezing should occur, 0 otherwise
1363 */
1364
1365static int gfs2_should_freeze(const struct gfs2_glock *gl)
1366{
1367 const struct gfs2_holder *gh;
1368
1369 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1370 return 0;
1371 if (gl->gl_target == LM_ST_UNLOCKED)
1372 return 0;
1373
1374 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1375 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1376 continue;
1377 if (LM_FLAG_NOEXP & gh->gh_flags)
1378 return 0;
1379 }
1380
1381 return 1;
1382}
1383
1384/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001385 * gfs2_glock_complete - Callback used by locking
1386 * @gl: Pointer to the glock
1387 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001389 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
Steven Whitehouse47a25382010-11-30 15:49:31 +00001390 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 */
1392
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001393void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394{
Bob Peterson15562c42015-03-16 11:52:05 -05001395 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001396
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001397 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001398 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001399
David Teiglande0c2a9a2012-01-09 17:18:05 -05001400 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001401 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001402 set_bit(GLF_FROZEN, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001403 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001405 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001407
Steven Whitehousee66cf162013-10-15 15:18:08 +01001408 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001409 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001410 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001411 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001412}
1413
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001414static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1415{
1416 struct gfs2_glock *gla, *glb;
1417
1418 gla = list_entry(a, struct gfs2_glock, gl_lru);
1419 glb = list_entry(b, struct gfs2_glock, gl_lru);
1420
1421 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1422 return 1;
1423 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1424 return -1;
1425
1426 return 0;
1427}
1428
1429/**
1430 * gfs2_dispose_glock_lru - Demote a list of glocks
1431 * @list: The list to dispose of
1432 *
1433 * Disposing of glocks may involve disk accesses, so that here we sort
1434 * the glocks by number (i.e. disk location of the inodes) so that if
1435 * there are any such accesses, they'll be sent in order (mostly).
1436 *
1437 * Must be called under the lru_lock, but may drop and retake this
1438 * lock. While the lru_lock is dropped, entries may vanish from the
1439 * list, but no new entries will appear on the list (since it is
1440 * private)
1441 */
1442
1443static void gfs2_dispose_glock_lru(struct list_head *list)
1444__releases(&lru_lock)
1445__acquires(&lru_lock)
1446{
1447 struct gfs2_glock *gl;
1448
1449 list_sort(NULL, list, glock_cmp);
1450
1451 while(!list_empty(list)) {
1452 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1453 list_del_init(&gl->gl_lru);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001454 if (!spin_trylock(&gl->gl_lockref.lock)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001455add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001456 list_add(&gl->gl_lru, &lru_list);
1457 atomic_inc(&lru_count);
1458 continue;
1459 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001460 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001461 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001462 goto add_back_to_lru;
1463 }
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001464 clear_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001465 gl->gl_lockref.count++;
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001466 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001467 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001468 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001469 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001470 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001471 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001472 }
1473}
1474
Steven Whitehouse2a005852012-12-14 12:28:30 +00001475/**
1476 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1477 * @nr: The number of entries to scan
1478 *
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001479 * This function selects the entries on the LRU which are able to
1480 * be demoted, and then kicks off the process by calling
1481 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001482 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001483
Dave Chinner1ab6c492013-08-28 10:18:09 +10001484static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485{
1486 struct gfs2_glock *gl;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001487 LIST_HEAD(skipped);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001488 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001489 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001491 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001492 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001493 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001494
1495 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001496 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001497 list_move(&gl->gl_lru, &dispose);
1498 atomic_dec(&lru_count);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001499 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001500 continue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001501 }
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001502
1503 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001504 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001505 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001506 if (!list_empty(&dispose))
1507 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001508 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001509
1510 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001511}
1512
Dave Chinner1ab6c492013-08-28 10:18:09 +10001513static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1514 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001515{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001516 if (!(sc->gfp_mask & __GFP_FS))
1517 return SHRINK_STOP;
1518 return gfs2_scan_glock_lru(sc->nr_to_scan);
1519}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001520
Dave Chinner1ab6c492013-08-28 10:18:09 +10001521static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1522 struct shrink_control *sc)
1523{
Glauber Costa55f841c2013-08-28 10:17:53 +10001524 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001525}
1526
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001527static struct shrinker glock_shrinker = {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001528 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001529 .count_objects = gfs2_glock_shrink_count,
1530 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001531};
1532
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533/**
1534 * examine_bucket - Call a function for glock in a hash bucket
1535 * @examiner: the function
1536 * @sdp: the filesystem
1537 * @bucket: the bucket
1538 *
Herbert Xu98687f42017-02-11 19:26:45 +08001539 * Note that the function can be called multiple times on the same
1540 * object. So the user must ensure that the function can cope with
1541 * that.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001542 */
1543
Bob Peterson88ffbf32015-03-16 11:02:46 -05001544static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001545{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001546 struct gfs2_glock *gl;
Herbert Xu98687f42017-02-11 19:26:45 +08001547 struct rhashtable_iter iter;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548
Herbert Xu98687f42017-02-11 19:26:45 +08001549 rhashtable_walk_enter(&gl_hash_table, &iter);
1550
1551 do {
1552 gl = ERR_PTR(rhashtable_walk_start(&iter));
Bob Peterson27c3b412017-08-18 09:15:13 -05001553 if (IS_ERR(gl))
1554 goto walk_stop;
Herbert Xu98687f42017-02-11 19:26:45 +08001555
1556 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl))
Bob Peterson27c3b412017-08-18 09:15:13 -05001557 if (gl->gl_name.ln_sbd == sdp &&
Bob Peterson88ffbf32015-03-16 11:02:46 -05001558 lockref_get_not_dead(&gl->gl_lockref))
1559 examiner(gl);
Herbert Xu98687f42017-02-11 19:26:45 +08001560
Bob Peterson27c3b412017-08-18 09:15:13 -05001561walk_stop:
Herbert Xu98687f42017-02-11 19:26:45 +08001562 rhashtable_walk_stop(&iter);
1563 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1564
1565 rhashtable_walk_exit(&iter);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001566}
1567
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001568/**
1569 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1570 * @gl: The glock to thaw
1571 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001572 */
1573
1574static void thaw_glock(struct gfs2_glock *gl)
1575{
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001576 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001577 gfs2_glock_put(gl);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001578 return;
Steven Whitehouse7286b312013-08-20 09:35:09 +01001579 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001580 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1581 gfs2_glock_queue_work(gl, 0);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001582}
1583
David Teiglandb3b94fa2006-01-16 16:50:04 +00001584/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001585 * clear_glock - look at a glock and see if we can free it from glock cache
1586 * @gl: the glock to look at
1587 *
1588 */
1589
1590static void clear_glock(struct gfs2_glock *gl)
1591{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001592 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001593
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001594 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousec741c452010-09-29 14:20:52 +01001595 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001596 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001597 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001598 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001599}
1600
1601/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001602 * gfs2_glock_thaw - Thaw any frozen glocks
1603 * @sdp: The super block
1604 *
1605 */
1606
1607void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1608{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001609 glock_hash_walk(thaw_glock, sdp);
1610}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001611
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001612static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001613{
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001614 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001615 gfs2_dump_glock(seq, gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001616 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001617}
1618
1619static void dump_glock_func(struct gfs2_glock *gl)
1620{
1621 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001622}
1623
1624/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001625 * gfs2_gl_hash_clear - Empty out the glock hash table
1626 * @sdp: the filesystem
1627 * @wait: wait until it's all gone
1628 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001629 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001630 */
1631
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001632void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001633{
David Teiglandfb6791d2012-11-13 10:58:56 -05001634 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001635 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001636 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001637 flush_workqueue(glock_workqueue);
Bob Peterson2aba1b52015-05-19 09:11:23 -05001638 wait_event_timeout(sdp->sd_glock_wait,
1639 atomic_read(&sdp->sd_glock_disposal) == 0,
1640 HZ * 600);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001641 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001642}
1643
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001644void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1645{
1646 struct gfs2_glock *gl = ip->i_gl;
1647 int ret;
1648
1649 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001650 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001651
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001652 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001653 clear_bit(GLF_LOCK, &gl->gl_flags);
1654 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001655 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001656}
1657
Steven Whitehouse6802e342008-05-21 17:03:22 +01001658static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001659{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001660 switch(state) {
1661 case LM_ST_UNLOCKED:
1662 return "UN";
1663 case LM_ST_SHARED:
1664 return "SH";
1665 case LM_ST_DEFERRED:
1666 return "DF";
1667 case LM_ST_EXCLUSIVE:
1668 return "EX";
1669 }
1670 return "??";
1671}
Robert Peterson04b933f2007-03-23 17:05:15 -05001672
Bob Petersonb58bf402015-07-24 09:45:43 -05001673static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
Steven Whitehouse6802e342008-05-21 17:03:22 +01001674{
1675 char *p = buf;
1676 if (flags & LM_FLAG_TRY)
1677 *p++ = 't';
1678 if (flags & LM_FLAG_TRY_1CB)
1679 *p++ = 'T';
1680 if (flags & LM_FLAG_NOEXP)
1681 *p++ = 'e';
1682 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001683 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001684 if (flags & LM_FLAG_PRIORITY)
1685 *p++ = 'p';
1686 if (flags & GL_ASYNC)
1687 *p++ = 'a';
1688 if (flags & GL_EXACT)
1689 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001690 if (flags & GL_NOCACHE)
1691 *p++ = 'c';
1692 if (test_bit(HIF_HOLDER, &iflags))
1693 *p++ = 'H';
1694 if (test_bit(HIF_WAIT, &iflags))
1695 *p++ = 'W';
1696 if (test_bit(HIF_FIRST, &iflags))
1697 *p++ = 'F';
1698 *p = 0;
1699 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001700}
1701
David Teiglandb3b94fa2006-01-16 16:50:04 +00001702/**
1703 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001704 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001705 * @gh: the glock holder
1706 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001707 */
1708
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001709static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001710{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001711 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001712 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001713
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001714 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001715 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001716 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001717 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1718 state2str(gh->gh_state),
1719 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1720 gh->gh_error,
1721 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1722 gh_owner ? gh_owner->comm : "(ended)",
1723 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001724 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001725}
1726
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001727static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001729 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001730 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001731
Steven Whitehouse6802e342008-05-21 17:03:22 +01001732 if (test_bit(GLF_LOCK, gflags))
1733 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001734 if (test_bit(GLF_DEMOTE, gflags))
1735 *p++ = 'D';
1736 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1737 *p++ = 'd';
1738 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1739 *p++ = 'p';
1740 if (test_bit(GLF_DIRTY, gflags))
1741 *p++ = 'y';
1742 if (test_bit(GLF_LFLUSH, gflags))
1743 *p++ = 'f';
1744 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1745 *p++ = 'i';
1746 if (test_bit(GLF_REPLY_PENDING, gflags))
1747 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001748 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001749 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001750 if (test_bit(GLF_FROZEN, gflags))
1751 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001752 if (test_bit(GLF_QUEUED, gflags))
1753 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001754 if (test_bit(GLF_LRU, gflags))
1755 *p++ = 'L';
1756 if (gl->gl_object)
1757 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001758 if (test_bit(GLF_BLOCKING, gflags))
1759 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001760 *p = 0;
1761 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001762}
1763
1764/**
Steven Whitehouse8eae1ca2012-10-15 10:57:02 +01001765 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001766 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001768 *
1769 * The file format is as follows:
1770 * One line per object, capital letters are used to indicate objects
1771 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1772 * other objects are indented by a single space and follow the glock to
1773 * which they are related. Fields are indicated by lower case letters
1774 * followed by a colon and the field value, except for strings which are in
1775 * [] so that its possible to see if they are composed of spaces for
1776 * example. The field's are n = number (id of the object), f = flags,
1777 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001778 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001779 */
1780
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001781void gfs2_dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001783 const struct gfs2_glock_operations *glops = gl->gl_ops;
1784 unsigned long long dtime;
1785 const struct gfs2_holder *gh;
1786 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001787
Steven Whitehouse6802e342008-05-21 17:03:22 +01001788 dtime = jiffies - gl->gl_demote_time;
1789 dtime *= 1000000/HZ; /* demote time in uSec */
1790 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1791 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001792 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 +01001793 state2str(gl->gl_state),
1794 gl->gl_name.ln_type,
1795 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001796 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001797 state2str(gl->gl_target),
1798 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001799 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001800 atomic_read(&gl->gl_revokes),
Steven Whitehousee66cf162013-10-15 15:18:08 +01001801 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001802
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001803 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1804 dump_holder(seq, gh);
1805
Steven Whitehouse6802e342008-05-21 17:03:22 +01001806 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001807 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001808}
1809
Steven Whitehousea2457692012-01-20 10:38:36 +00001810static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1811{
1812 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001813
Ben Hutchings4d207132015-08-27 12:51:45 -05001814 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 +00001815 gl->gl_name.ln_type,
1816 (unsigned long long)gl->gl_name.ln_number,
Ben Hutchings4d207132015-08-27 12:51:45 -05001817 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1818 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1819 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1820 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1821 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1822 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1823 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1824 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001825 return 0;
1826}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001827
Steven Whitehousea2457692012-01-20 10:38:36 +00001828static const char *gfs2_gltype[] = {
1829 "type",
1830 "reserved",
1831 "nondisk",
1832 "inode",
1833 "rgrp",
1834 "meta",
1835 "iopen",
1836 "flock",
1837 "plock",
1838 "quota",
1839 "journal",
1840};
1841
1842static const char *gfs2_stype[] = {
1843 [GFS2_LKS_SRTT] = "srtt",
1844 [GFS2_LKS_SRTTVAR] = "srttvar",
1845 [GFS2_LKS_SRTTB] = "srttb",
1846 [GFS2_LKS_SRTTVARB] = "srttvarb",
1847 [GFS2_LKS_SIRT] = "sirt",
1848 [GFS2_LKS_SIRTVAR] = "sirtvar",
1849 [GFS2_LKS_DCOUNT] = "dlm",
1850 [GFS2_LKS_QCOUNT] = "queue",
1851};
1852
1853#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1854
1855static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1856{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001857 struct gfs2_sbd *sdp = seq->private;
1858 loff_t pos = *(loff_t *)iter_ptr;
1859 unsigned index = pos >> 3;
1860 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001861 int i;
1862
1863 if (index == 0 && subindex != 0)
1864 return 0;
1865
1866 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1867 (index == 0) ? "cpu": gfs2_stype[subindex]);
1868
1869 for_each_possible_cpu(i) {
1870 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
Andreas Gruenbacher8f7e0a82015-08-27 13:02:54 -05001871
1872 if (index == 0)
1873 seq_printf(seq, " %15u", i);
1874 else
1875 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1876 lkstats[index - 1].stats[subindex]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001877 }
1878 seq_putc(seq, '\n');
1879 return 0;
1880}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001881
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001882int __init gfs2_glock_init(void)
1883{
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001884 int i, ret;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001885
1886 ret = rhashtable_init(&gl_hash_table, &ht_parms);
1887 if (ret < 0)
1888 return ret;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001889
Steven Whitehoused2115772010-11-03 19:58:53 +00001890 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001891 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001892 if (!glock_workqueue) {
1893 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001894 return -ENOMEM;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001895 }
Steven Whitehoused2115772010-11-03 19:58:53 +00001896 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001897 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001898 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001899 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001900 destroy_workqueue(glock_workqueue);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001901 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001902 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001903 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001904
Chao Yue0d735c2016-09-21 12:09:40 -05001905 ret = register_shrinker(&glock_shrinker);
1906 if (ret) {
1907 destroy_workqueue(gfs2_delete_workqueue);
1908 destroy_workqueue(glock_workqueue);
1909 rhashtable_destroy(&gl_hash_table);
1910 return ret;
1911 }
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001912
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001913 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
1914 init_waitqueue_head(glock_wait_table + i);
1915
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001916 return 0;
1917}
1918
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001919void gfs2_glock_exit(void)
1920{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001921 unregister_shrinker(&glock_shrinker);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001922 rhashtable_destroy(&gl_hash_table);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001923 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001924 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001925}
1926
Bob Peterson88ffbf32015-03-16 11:02:46 -05001927static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001928{
Dan Carpenter14d37562016-12-14 08:02:03 -06001929 while ((gi->gl = rhashtable_walk_next(&gi->hti))) {
Bob Peterson88ffbf32015-03-16 11:02:46 -05001930 if (IS_ERR(gi->gl)) {
1931 if (PTR_ERR(gi->gl) == -EAGAIN)
1932 continue;
1933 gi->gl = NULL;
Dan Carpenter14d37562016-12-14 08:02:03 -06001934 return;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001935 }
Dan Carpenter14d37562016-12-14 08:02:03 -06001936 /* Skip entries for other sb and dead entries */
1937 if (gi->sdp == gi->gl->gl_name.ln_sbd &&
1938 !__lockref_is_dead(&gi->gl->gl_lockref))
1939 return;
1940 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001941}
1942
Steven Whitehouse6802e342008-05-21 17:03:22 +01001943static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Bob Peterson27c3b412017-08-18 09:15:13 -05001944 __acquires(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00001945{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001946 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001947 loff_t n = *pos;
1948
Andreas Gruenbacher10201652017-09-19 07:15:35 -05001949 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
1950 if (rhashtable_walk_start(&gi->hti) != 0)
Bob Peterson88ffbf32015-03-16 11:02:46 -05001951 return NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00001952
Steven Whitehouse6802e342008-05-21 17:03:22 +01001953 do {
Bob Peterson88ffbf32015-03-16 11:02:46 -05001954 gfs2_glock_iter_next(gi);
1955 } while (gi->gl && n--);
Robert Peterson7c52b162007-03-16 10:26:37 +00001956
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001957 gi->last_pos = *pos;
Andreas Gruenbacher10201652017-09-19 07:15:35 -05001958
Steven Whitehouse6802e342008-05-21 17:03:22 +01001959 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001960}
1961
Steven Whitehouse6802e342008-05-21 17:03:22 +01001962static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001963 loff_t *pos)
1964{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001965 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001966
1967 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001968 gi->last_pos = *pos;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001969 gfs2_glock_iter_next(gi);
Andreas Gruenbacher10201652017-09-19 07:15:35 -05001970
Steven Whitehouse6802e342008-05-21 17:03:22 +01001971 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001972}
1973
Steven Whitehouse6802e342008-05-21 17:03:22 +01001974static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Bob Peterson27c3b412017-08-18 09:15:13 -05001975 __releases(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00001976{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001977 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001978
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001979 gi->gl = NULL;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001980 rhashtable_walk_stop(&gi->hti);
Andreas Gruenbacher10201652017-09-19 07:15:35 -05001981 rhashtable_walk_exit(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00001982}
1983
Steven Whitehouse6802e342008-05-21 17:03:22 +01001984static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00001985{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001986 dump_glock(seq, iter_ptr);
1987 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00001988}
1989
Steven Whitehousea2457692012-01-20 10:38:36 +00001990static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
1991{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001992 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00001993 if (*pos >= GFS2_NR_SBSTATS)
1994 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001995 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00001996}
1997
1998static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
1999 loff_t *pos)
2000{
Steven Whitehousea2457692012-01-20 10:38:36 +00002001 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002002 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00002003 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002004 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002005}
2006
2007static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2008{
2009 preempt_enable();
2010}
2011
Denis Cheng4ef29002007-07-31 18:31:11 +08002012static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002013 .start = gfs2_glock_seq_start,
2014 .next = gfs2_glock_seq_next,
2015 .stop = gfs2_glock_seq_stop,
2016 .show = gfs2_glock_seq_show,
2017};
2018
Steven Whitehousea2457692012-01-20 10:38:36 +00002019static const struct seq_operations gfs2_glstats_seq_ops = {
2020 .start = gfs2_glock_seq_start,
2021 .next = gfs2_glock_seq_next,
2022 .stop = gfs2_glock_seq_stop,
2023 .show = gfs2_glstats_seq_show,
2024};
2025
2026static const struct seq_operations gfs2_sbstats_seq_ops = {
2027 .start = gfs2_sbstats_seq_start,
2028 .next = gfs2_sbstats_seq_next,
2029 .stop = gfs2_sbstats_seq_stop,
2030 .show = gfs2_sbstats_seq_show,
2031};
2032
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002033#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2034
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002035static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2036 const struct seq_operations *ops)
Robert Peterson7c52b162007-03-16 10:26:37 +00002037{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002038 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
Steven Whitehouse6802e342008-05-21 17:03:22 +01002039 if (ret == 0) {
2040 struct seq_file *seq = file->private_data;
2041 struct gfs2_glock_iter *gi = seq->private;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002042
Steven Whitehouse6802e342008-05-21 17:03:22 +01002043 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002044 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01002045 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002046 seq->size = GFS2_SEQ_GOODSIZE;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002047 gi->gl = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01002048 }
2049 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00002050}
2051
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002052static int gfs2_glocks_open(struct inode *inode, struct file *file)
2053{
2054 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2055}
2056
Bob Peterson88ffbf32015-03-16 11:02:46 -05002057static int gfs2_glocks_release(struct inode *inode, struct file *file)
2058{
2059 struct seq_file *seq = file->private_data;
2060 struct gfs2_glock_iter *gi = seq->private;
2061
2062 gi->gl = NULL;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002063 return seq_release_private(inode, file);
2064}
2065
Steven Whitehousea2457692012-01-20 10:38:36 +00002066static int gfs2_glstats_open(struct inode *inode, struct file *file)
2067{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002068 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002069}
2070
2071static int gfs2_sbstats_open(struct inode *inode, struct file *file)
2072{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002073 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002074 if (ret == 0) {
2075 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002076 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00002077 }
2078 return ret;
2079}
2080
2081static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002082 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00002083 .open = gfs2_glocks_open,
2084 .read = seq_read,
2085 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002086 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002087};
2088
2089static const struct file_operations gfs2_glstats_fops = {
2090 .owner = THIS_MODULE,
2091 .open = gfs2_glstats_open,
2092 .read = seq_read,
2093 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002094 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002095};
2096
2097static const struct file_operations gfs2_sbstats_fops = {
2098 .owner = THIS_MODULE,
2099 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00002100 .read = seq_read,
2101 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002102 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00002103};
2104
2105int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2106{
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002107 struct dentry *dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002108
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002109 dent = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2110 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002111 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002112 sdp->debugfs_dir = dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002113
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002114 dent = debugfs_create_file("glocks",
2115 S_IFREG | S_IRUGO,
2116 sdp->debugfs_dir, sdp,
2117 &gfs2_glocks_fops);
2118 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002119 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002120 sdp->debugfs_dentry_glocks = dent;
2121
2122 dent = debugfs_create_file("glstats",
2123 S_IFREG | S_IRUGO,
2124 sdp->debugfs_dir, sdp,
2125 &gfs2_glstats_fops);
2126 if (IS_ERR_OR_NULL(dent))
2127 goto fail;
2128 sdp->debugfs_dentry_glstats = dent;
2129
2130 dent = debugfs_create_file("sbstats",
2131 S_IFREG | S_IRUGO,
2132 sdp->debugfs_dir, sdp,
2133 &gfs2_sbstats_fops);
2134 if (IS_ERR_OR_NULL(dent))
2135 goto fail;
2136 sdp->debugfs_dentry_sbstats = dent;
Robert Peterson7c52b162007-03-16 10:26:37 +00002137
2138 return 0;
Steven Whitehousea2457692012-01-20 10:38:36 +00002139fail:
2140 gfs2_delete_debugfs_file(sdp);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002141 return dent ? PTR_ERR(dent) : -ENOMEM;
Robert Peterson7c52b162007-03-16 10:26:37 +00002142}
2143
2144void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2145{
Steven Whitehousea2457692012-01-20 10:38:36 +00002146 if (sdp->debugfs_dir) {
Robert Peterson5f882092007-04-18 11:41:11 -05002147 if (sdp->debugfs_dentry_glocks) {
2148 debugfs_remove(sdp->debugfs_dentry_glocks);
2149 sdp->debugfs_dentry_glocks = NULL;
2150 }
Steven Whitehousea2457692012-01-20 10:38:36 +00002151 if (sdp->debugfs_dentry_glstats) {
2152 debugfs_remove(sdp->debugfs_dentry_glstats);
2153 sdp->debugfs_dentry_glstats = NULL;
2154 }
2155 if (sdp->debugfs_dentry_sbstats) {
2156 debugfs_remove(sdp->debugfs_dentry_sbstats);
2157 sdp->debugfs_dentry_sbstats = NULL;
2158 }
Robert Peterson5f882092007-04-18 11:41:11 -05002159 debugfs_remove(sdp->debugfs_dir);
2160 sdp->debugfs_dir = NULL;
2161 }
Robert Peterson7c52b162007-03-16 10:26:37 +00002162}
2163
2164int gfs2_register_debugfs(void)
2165{
2166 gfs2_root = debugfs_create_dir("gfs2", NULL);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002167 if (IS_ERR(gfs2_root))
2168 return PTR_ERR(gfs2_root);
Robert Peterson7c52b162007-03-16 10:26:37 +00002169 return gfs2_root ? 0 : -ENOMEM;
2170}
2171
2172void gfs2_unregister_debugfs(void)
2173{
2174 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002175 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002176}