blob: b92740edc4169998772d8251010deac232f8405e [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 */
Bob Petersone54c78a2018-10-03 08:47:36 -0500497 fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
498 gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100499 GLOCK_BUG_ON(gl, 1);
500 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500501 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100502 return;
503 }
504
505 /* Fast path - we got what we asked for */
506 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
507 gfs2_demote_wake(gl);
508 if (state != LM_ST_UNLOCKED) {
509 if (glops->go_xmote_bh) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500510 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100511 rv = glops->go_xmote_bh(gl, gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500512 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100513 if (rv) {
514 do_error(gl, rv);
515 goto out;
516 }
517 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000518 rv = do_promote(gl);
519 if (rv == 2)
520 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100521 }
522out:
523 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000524out_locked:
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500525 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500526}
527
Steven Whitehouse6802e342008-05-21 17:03:22 +0100528/**
529 * do_xmote - Calls the DLM to change the state of a lock
530 * @gl: The lock state
531 * @gh: The holder (only for promotes)
532 * @target: The target lock state
533 *
534 */
535
536static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500537__releases(&gl->gl_lockref.lock)
538__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100539{
540 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500541 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Bob Petersonb58bf402015-07-24 09:45:43 -0500542 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100543 int ret;
544
Bob Petersoned175452017-05-05 09:43:02 -0500545 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) &&
546 target != LM_ST_UNLOCKED)
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500547 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100548 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
549 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000550 GLOCK_BUG_ON(gl, gl->gl_state == target);
551 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100552 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
553 glops->go_inval) {
554 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
555 do_error(gl, 0); /* Fail queued try locks */
556 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000557 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000558 set_bit(GLF_BLOCKING, &gl->gl_flags);
559 if ((gl->gl_req == LM_ST_UNLOCKED) ||
560 (gl->gl_state == LM_ST_EXCLUSIVE) ||
561 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
562 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500563 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson06dfc302012-10-24 14:41:05 -0400564 if (glops->go_sync)
565 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100566 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
567 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
568 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
569
570 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000571 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
572 /* lock_dlm */
573 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
Benjamin Marzinski3e11e5302016-03-23 14:29:59 -0400574 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
575 target == LM_ST_UNLOCKED &&
576 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
577 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500578 gfs2_glock_queue_work(gl, 0);
Benjamin Marzinski3e11e5302016-03-23 14:29:59 -0400579 }
580 else if (ret) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500581 fs_err(sdp, "lm_lock ret %d\n", ret);
Bob Petersoned175452017-05-05 09:43:02 -0500582 GLOCK_BUG_ON(gl, !test_bit(SDF_SHUTDOWN,
583 &sdp->sd_flags));
David Teiglanddba2d702012-11-14 13:46:53 -0500584 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000585 } else { /* lock_nolock */
586 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500587 gfs2_glock_queue_work(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100588 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000589
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500590 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100591}
592
593/**
594 * find_first_holder - find the first "holder" gh
595 * @gl: the glock
596 */
597
598static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
599{
600 struct gfs2_holder *gh;
601
602 if (!list_empty(&gl->gl_holders)) {
603 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
604 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
605 return gh;
606 }
607 return NULL;
608}
609
610/**
611 * run_queue - do all outstanding tasks related to a glock
612 * @gl: The glock in question
613 * @nonblock: True if we must not block in run_queue
614 *
615 */
616
617static void run_queue(struct gfs2_glock *gl, const int nonblock)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500618__releases(&gl->gl_lockref.lock)
619__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100620{
621 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000622 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100623
624 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
625 return;
626
627 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
628
629 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
630 gl->gl_demote_state != gl->gl_state) {
631 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000632 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100633 if (nonblock)
634 goto out_sched;
635 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100636 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100637 gl->gl_target = gl->gl_demote_state;
638 } else {
639 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
640 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000641 ret = do_promote(gl);
642 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000643 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000644 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100645 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100646 gh = find_first_waiter(gl);
647 gl->gl_target = gh->gh_state;
648 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
649 do_error(gl, 0); /* Fail queued try locks */
650 }
651 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100652out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100653 return;
654
655out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100656 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100657 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100658 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500659 __gfs2_glock_queue_work(gl, 0);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100660 return;
661
Steven Whitehoused8348de2009-02-05 10:12:38 +0000662out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100663 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100664 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100665 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100666}
667
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500668static void delete_work_func(struct work_struct *work)
669{
670 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500671 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500672 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000673 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500674
Bob Petersona4923862015-12-07 16:24:27 -0600675 /* If someone's using this glock to create a new dinode, the block must
676 have been freed by another node, then re-used, in which case our
677 iopen callback is too late after the fact. Ignore it. */
678 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
679 goto out;
680
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500681 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000682 if (inode && !IS_ERR(inode)) {
683 d_prune_aliases(inode);
684 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500685 }
Bob Petersona4923862015-12-07 16:24:27 -0600686out:
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500687 gfs2_glock_put(gl);
688}
689
Steven Whitehouse6802e342008-05-21 17:03:22 +0100690static void glock_work_func(struct work_struct *work)
691{
692 unsigned long delay = 0;
693 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500694 unsigned int drop_refs = 1;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100695
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000696 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100697 finish_xmote(gl, gl->gl_reply);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500698 drop_refs++;
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000699 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500700 spin_lock(&gl->gl_lockref.lock);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400701 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100702 gl->gl_state != LM_ST_UNLOCKED &&
703 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100704 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400705
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400706 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100707 if (time_before(now, holdtime))
708 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400709
710 if (!delay) {
711 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
712 set_bit(GLF_DEMOTE, &gl->gl_flags);
713 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100714 }
715 run_queue(gl, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500716 if (delay) {
717 /* Keep one glock reference for the work we requeue. */
718 drop_refs--;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400719 if (gl->gl_name.ln_type != LM_TYPE_INODE)
720 delay = 0;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500721 __gfs2_glock_queue_work(gl, delay);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400722 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500723
724 /*
725 * Drop the remaining glock references manually here. (Mind that
726 * __gfs2_glock_queue_work depends on the lockref spinlock begin held
727 * here as well.)
728 */
729 gl->gl_lockref.count -= drop_refs;
730 if (!gl->gl_lockref.count) {
731 __gfs2_glock_put(gl);
732 return;
733 }
734 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100735}
736
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500737static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
738 struct gfs2_glock *new)
739{
740 struct wait_glock_queue wait;
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500741 wait_queue_head_t *wq = glock_waitqueue(name);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500742 struct gfs2_glock *gl;
743
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500744 wait.name = name;
745 init_wait(&wait.wait);
746 wait.wait.func = glock_wake_function;
747
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500748again:
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500749 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500750 rcu_read_lock();
751 if (new) {
752 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
753 &new->gl_node, ht_parms);
754 if (IS_ERR(gl))
755 goto out;
756 } else {
757 gl = rhashtable_lookup_fast(&gl_hash_table,
758 name, ht_parms);
759 }
760 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
761 rcu_read_unlock();
762 schedule();
763 goto again;
764 }
765out:
766 rcu_read_unlock();
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500767 finish_wait(wq, &wait.wait);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500768 return gl;
769}
770
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771/**
772 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
773 * @sdp: The GFS2 superblock
774 * @number: the lock number
775 * @glops: The glock_operations to use
776 * @create: If 0, don't create the glock if it doesn't exist
777 * @glp: the glock is returned here
778 *
779 * This does not lock a glock, just finds/creates structures for one.
780 *
781 * Returns: errno
782 */
783
Steven Whitehousecd915492006-09-04 12:49:07 -0400784int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400785 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 struct gfs2_glock **glp)
787{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000788 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500789 struct lm_lockname name = { .ln_number = number,
790 .ln_type = glops->go_type,
791 .ln_sbd = sdp };
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100792 struct gfs2_glock *gl, *tmp;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000793 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000794 struct kmem_cache *cachep;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100795 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500797 gl = find_insert_glock(&name, NULL);
798 if (gl) {
799 *glp = gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800 return 0;
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500801 }
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000802 if (!create)
803 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804
Steven Whitehouse009d8512009-12-08 12:12:13 +0000805 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000806 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000807 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000808 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100809 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000810 if (!gl)
811 return -ENOMEM;
812
David Teiglanddba2d702012-11-14 13:46:53 -0500813 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500814
815 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100816 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500817 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500818 kmem_cache_free(cachep, gl);
819 return -ENOMEM;
820 }
David Teiglanddba2d702012-11-14 13:46:53 -0500821 }
822
Steven Whitehouse8f052282010-01-29 15:21:27 +0000823 atomic_inc(&sdp->sd_glock_disposal);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500824 gl->gl_node.next = NULL;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400825 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100827 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100829 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500830 gl->gl_demote_state = LM_ST_EXCLUSIVE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 gl->gl_ops = glops;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100832 gl->gl_dstamp = 0;
Steven Whitehousea2457692012-01-20 10:38:36 +0000833 preempt_disable();
834 /* We use the global stats to estimate the initial per-glock stats */
835 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
836 preempt_enable();
837 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
838 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500839 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400840 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400841 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500842 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500843 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844
Steven Whitehouse009d8512009-12-08 12:12:13 +0000845 mapping = gfs2_glock2aspace(gl);
846 if (mapping) {
847 mapping->a_ops = &gfs2_meta_aops;
848 mapping->host = s->s_bdev->bd_inode;
849 mapping->flags = 0;
850 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800851 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000852 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 }
854
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500855 tmp = find_insert_glock(&name, gl);
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100856 if (!tmp) {
Bob Peterson88ffbf32015-03-16 11:02:46 -0500857 *glp = gl;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100858 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 }
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100860 if (IS_ERR(tmp)) {
861 ret = PTR_ERR(tmp);
862 goto out_free;
863 }
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500864 *glp = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100866out_free:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500867 kfree(gl->gl_lksb.sb_lvbptr);
868 kmem_cache_free(cachep, gl);
869 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100871out:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500872 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873}
874
875/**
876 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
877 * @gl: the glock
878 * @state: the state we're requesting
879 * @flags: the modifier flags
880 * @gh: the holder structure
881 *
882 */
883
Bob Petersonb58bf402015-07-24 09:45:43 -0500884void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 struct gfs2_holder *gh)
886{
887 INIT_LIST_HEAD(&gh->gh_list);
888 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200889 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800890 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 gh->gh_state = state;
892 gh->gh_flags = flags;
893 gh->gh_error = 0;
894 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 gfs2_glock_hold(gl);
896}
897
898/**
899 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
900 * @state: the state we're requesting
901 * @flags: the modifier flags
902 * @gh: the holder structure
903 *
904 * Don't mess with the glock.
905 *
906 */
907
Bob Petersonb58bf402015-07-24 09:45:43 -0500908void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909{
910 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400911 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000912 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200913 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100914 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400915 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916}
917
918/**
919 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
920 * @gh: the holder structure
921 *
922 */
923
924void gfs2_holder_uninit(struct gfs2_holder *gh)
925{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800926 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927 gfs2_glock_put(gh->gh_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500928 gfs2_holder_mark_uninitialized(gh);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500929 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930}
931
Steven Whitehousefe64d512009-05-19 10:01:18 +0100932/**
Bob Peterson07a79042012-08-09 12:48:44 -0500933 * gfs2_glock_wait - wait on a glock acquisition
934 * @gh: the glock holder
935 *
936 * Returns: 0 on success
937 */
938
939int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000940{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400941 unsigned long time1 = jiffies;
942
Steven Whitehousefee852e2007-01-17 15:33:23 +0000943 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000944 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400945 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
946 /* Lengthen the minimum hold time. */
947 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
948 GL_GLOCK_HOLD_INCR,
949 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500950 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100951}
952
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000954 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955 * @gl: the glock
956 * @state: the state the caller wants us to change to
957 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000958 * There are only two requests that we are going to see in actual
959 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 */
961
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500962static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100963 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500965 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
966
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500967 set_bit(bit, &gl->gl_flags);
968 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000969 gl->gl_demote_state = state;
970 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100971 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
972 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100973 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500975 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100976 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100977 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978}
979
Steven Whitehouse6802e342008-05-21 17:03:22 +0100980void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000981{
Joe Perches5e690692010-11-09 16:35:20 -0800982 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000983 va_list args;
984
985 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800986
Steven Whitehouse6802e342008-05-21 17:03:22 +0100987 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100988 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100989 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800990 vaf.fmt = fmt;
991 vaf.va = &args;
992
Joe Perchesd77d1b52014-03-06 12:10:45 -0800993 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100994 }
Joe Perches5e690692010-11-09 16:35:20 -0800995
Robert Peterson7c52b162007-03-16 10:26:37 +0000996 va_end(args);
997}
998
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1001 * @gh: the holder structure to add
1002 *
Steven Whitehouse6802e342008-05-21 17:03:22 +01001003 * Eventually we should move the recursive locking trap to a
1004 * debugging option or something like that. This is the fast
1005 * path and needs to have the minimum number of distractions.
1006 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007 */
1008
Steven Whitehouse6802e342008-05-21 17:03:22 +01001009static inline void add_to_queue(struct gfs2_holder *gh)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001010__releases(&gl->gl_lockref.lock)
1011__acquires(&gl->gl_lockref.lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012{
1013 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001014 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001015 struct list_head *insert_pt = NULL;
1016 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001017 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001019 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +00001020 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1021 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -04001022
Steven Whitehouse6802e342008-05-21 17:03:22 +01001023 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1024 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -05001025 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001026 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1027 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 }
1029
Steven Whitehouse6802e342008-05-21 17:03:22 +01001030 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1031 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
1032 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
1033 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001034 if (try_futile &&
1035 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001036fail:
1037 gh->gh_error = GLR_TRYFAILED;
1038 gfs2_holder_wake(gh);
1039 return;
1040 }
1041 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1042 continue;
1043 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
1044 insert_pt = &gh2->gh_list;
1045 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001046 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +00001047 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +00001048 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1049 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001050 if (likely(insert_pt == NULL)) {
1051 list_add_tail(&gh->gh_list, &gl->gl_holders);
1052 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
1053 goto do_cancel;
1054 return;
1055 }
1056 list_add_tail(&gh->gh_list, insert_pt);
1057do_cancel:
1058 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
1059 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001060 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +01001061 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001062 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001063 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001064 }
1065 return;
1066
1067trap_recursive:
Bob Petersone54c78a2018-10-03 08:47:36 -05001068 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1069 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1070 fs_err(sdp, "lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001071 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Bob Petersone54c78a2018-10-03 08:47:36 -05001072 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1073 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1074 fs_err(sdp, "lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001075 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +01001076 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001077 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001078}
1079
1080/**
1081 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1082 * @gh: the holder structure
1083 *
1084 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1085 *
1086 * Returns: 0, GLR_TRYFAILED, or errno on failure
1087 */
1088
1089int gfs2_glock_nq(struct gfs2_holder *gh)
1090{
1091 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001092 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 int error = 0;
1094
Steven Whitehouse6802e342008-05-21 17:03:22 +01001095 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097
Steven Whitehousef42ab082011-04-14 16:50:31 +01001098 if (test_bit(GLF_LRU, &gl->gl_flags))
1099 gfs2_glock_remove_from_lru(gl);
1100
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001101 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -04001103 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1104 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001105 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -04001106 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001107 __gfs2_glock_queue_work(gl, 0);
Bob Peterson01b172b2014-03-12 10:32:20 -04001108 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001109 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001110 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111
Steven Whitehouse6802e342008-05-21 17:03:22 +01001112 if (!(gh->gh_flags & GL_ASYNC))
1113 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115 return error;
1116}
1117
1118/**
1119 * gfs2_glock_poll - poll to see if an async request has been completed
1120 * @gh: the holder
1121 *
1122 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1123 */
1124
1125int gfs2_glock_poll(struct gfs2_holder *gh)
1126{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001127 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128}
1129
1130/**
1131 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1132 * @gh: the glock holder
1133 *
1134 */
1135
1136void gfs2_glock_dq(struct gfs2_holder *gh)
1137{
1138 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001139 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001140 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001141 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001143 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001145 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 list_del_init(&gh->gh_list);
Bob Peterson7508abc2015-12-18 11:54:55 -06001148 clear_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001149 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001150 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001151 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001152 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153 glops->go_unlock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001154 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001155 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001156 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001157 if (list_empty(&gl->gl_holders) &&
1158 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1159 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1160 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001161 }
Bob Petersone7ccaf52015-06-12 13:15:54 -05001162 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl) &&
1163 (glops->go_flags & GLOF_LRU))
Bob Peterson4abb6ad92012-08-09 12:48:43 -05001164 gfs2_glock_add_to_lru(gl);
1165
Steven Whitehouse63997772009-06-12 08:49:20 +01001166 trace_gfs2_glock_queue(gh, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001167 if (unlikely(!fast_path)) {
1168 gl->gl_lockref.count++;
1169 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1170 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1171 gl->gl_name.ln_type == LM_TYPE_INODE)
1172 delay = gl->gl_hold_time;
1173 __gfs2_glock_queue_work(gl, delay);
1174 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001175 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001176}
1177
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001178void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1179{
1180 struct gfs2_glock *gl = gh->gh_gl;
1181 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001182 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001183 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001184}
1185
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001187 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1188 * @gh: the holder structure
1189 *
1190 */
1191
1192void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1193{
1194 gfs2_glock_dq(gh);
1195 gfs2_holder_uninit(gh);
1196}
1197
1198/**
1199 * gfs2_glock_nq_num - acquire a glock based on lock number
1200 * @sdp: the filesystem
1201 * @number: the lock number
1202 * @glops: the glock operations for the type of glock
1203 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001204 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001205 * @gh: the struct gfs2_holder
1206 *
1207 * Returns: errno
1208 */
1209
Steven Whitehousecd915492006-09-04 12:49:07 -04001210int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001211 const struct gfs2_glock_operations *glops,
Bob Petersonb58bf402015-07-24 09:45:43 -05001212 unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213{
1214 struct gfs2_glock *gl;
1215 int error;
1216
1217 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1218 if (!error) {
1219 error = gfs2_glock_nq_init(gl, state, flags, gh);
1220 gfs2_glock_put(gl);
1221 }
1222
1223 return error;
1224}
1225
1226/**
1227 * glock_compare - Compare two struct gfs2_glock structures for sorting
1228 * @arg_a: the first structure
1229 * @arg_b: the second structure
1230 *
1231 */
1232
1233static int glock_compare(const void *arg_a, const void *arg_b)
1234{
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001235 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1236 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1237 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1238 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001239
1240 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a92006-09-09 17:07:05 -04001241 return 1;
1242 if (a->ln_number < b->ln_number)
1243 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001244 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 -04001245 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246}
1247
1248/**
1249 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1250 * @num_gh: the number of structures
1251 * @ghs: an array of struct gfs2_holder structures
1252 *
1253 * Returns: 0 on success (all glocks acquired),
1254 * errno on failure (no glocks acquired)
1255 */
1256
1257static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1258 struct gfs2_holder **p)
1259{
1260 unsigned int x;
1261 int error = 0;
1262
1263 for (x = 0; x < num_gh; x++)
1264 p[x] = &ghs[x];
1265
1266 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1267
1268 for (x = 0; x < num_gh; x++) {
1269 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1270
1271 error = gfs2_glock_nq(p[x]);
1272 if (error) {
1273 while (x--)
1274 gfs2_glock_dq(p[x]);
1275 break;
1276 }
1277 }
1278
1279 return error;
1280}
1281
1282/**
1283 * gfs2_glock_nq_m - acquire multiple glocks
1284 * @num_gh: the number of structures
1285 * @ghs: an array of struct gfs2_holder structures
1286 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001287 *
1288 * Returns: 0 on success (all glocks acquired),
1289 * errno on failure (no glocks acquired)
1290 */
1291
1292int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1293{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001294 struct gfs2_holder *tmp[4];
1295 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001296 int error = 0;
1297
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001298 switch(num_gh) {
1299 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001301 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001302 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1303 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001304 default:
1305 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306 break;
Kees Cook6da2ec52018-06-12 13:55:00 -07001307 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1308 GFP_NOFS);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001309 if (!pph)
1310 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001311 }
1312
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001313 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001315 if (pph != tmp)
1316 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317
1318 return error;
1319}
1320
1321/**
1322 * gfs2_glock_dq_m - release multiple glocks
1323 * @num_gh: the number of structures
1324 * @ghs: an array of struct gfs2_holder structures
1325 *
1326 */
1327
1328void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1329{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001330 while (num_gh--)
1331 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332}
1333
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001334void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001335{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001336 unsigned long delay = 0;
1337 unsigned long holdtime;
1338 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001340 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001341 holdtime = gl->gl_tchange + gl->gl_hold_time;
1342 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1343 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001344 if (time_before(now, holdtime))
1345 delay = holdtime - now;
1346 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001347 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001348 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001349
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001350 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001351 handle_callback(gl, state, delay, true);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001352 __gfs2_glock_queue_work(gl, delay);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001353 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354}
1355
1356/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001357 * gfs2_should_freeze - Figure out if glock should be frozen
1358 * @gl: The glock in question
1359 *
1360 * Glocks are not frozen if (a) the result of the dlm operation is
1361 * an error, (b) the locking operation was an unlock operation or
1362 * (c) if there is a "noexp" flagged request anywhere in the queue
1363 *
1364 * Returns: 1 if freezing should occur, 0 otherwise
1365 */
1366
1367static int gfs2_should_freeze(const struct gfs2_glock *gl)
1368{
1369 const struct gfs2_holder *gh;
1370
1371 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1372 return 0;
1373 if (gl->gl_target == LM_ST_UNLOCKED)
1374 return 0;
1375
1376 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1377 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1378 continue;
1379 if (LM_FLAG_NOEXP & gh->gh_flags)
1380 return 0;
1381 }
1382
1383 return 1;
1384}
1385
1386/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001387 * gfs2_glock_complete - Callback used by locking
1388 * @gl: Pointer to the glock
1389 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001390 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001391 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
Steven Whitehouse47a25382010-11-30 15:49:31 +00001392 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001393 */
1394
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001395void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396{
Bob Peterson15562c42015-03-16 11:52:05 -05001397 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001398
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001399 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001400 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001401
David Teiglande0c2a9a2012-01-09 17:18:05 -05001402 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001403 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001404 set_bit(GLF_FROZEN, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001405 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001407 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001409
Steven Whitehousee66cf162013-10-15 15:18:08 +01001410 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001411 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001412 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001413 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001414}
1415
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001416static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1417{
1418 struct gfs2_glock *gla, *glb;
1419
1420 gla = list_entry(a, struct gfs2_glock, gl_lru);
1421 glb = list_entry(b, struct gfs2_glock, gl_lru);
1422
1423 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1424 return 1;
1425 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1426 return -1;
1427
1428 return 0;
1429}
1430
1431/**
1432 * gfs2_dispose_glock_lru - Demote a list of glocks
1433 * @list: The list to dispose of
1434 *
1435 * Disposing of glocks may involve disk accesses, so that here we sort
1436 * the glocks by number (i.e. disk location of the inodes) so that if
1437 * there are any such accesses, they'll be sent in order (mostly).
1438 *
1439 * Must be called under the lru_lock, but may drop and retake this
1440 * lock. While the lru_lock is dropped, entries may vanish from the
1441 * list, but no new entries will appear on the list (since it is
1442 * private)
1443 */
1444
1445static void gfs2_dispose_glock_lru(struct list_head *list)
1446__releases(&lru_lock)
1447__acquires(&lru_lock)
1448{
1449 struct gfs2_glock *gl;
1450
1451 list_sort(NULL, list, glock_cmp);
1452
1453 while(!list_empty(list)) {
1454 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1455 list_del_init(&gl->gl_lru);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001456 if (!spin_trylock(&gl->gl_lockref.lock)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001457add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001458 list_add(&gl->gl_lru, &lru_list);
1459 atomic_inc(&lru_count);
1460 continue;
1461 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001462 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001463 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001464 goto add_back_to_lru;
1465 }
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001466 clear_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001467 gl->gl_lockref.count++;
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001468 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001469 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001470 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001471 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001472 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001473 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001474 }
1475}
1476
Steven Whitehouse2a005852012-12-14 12:28:30 +00001477/**
1478 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1479 * @nr: The number of entries to scan
1480 *
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001481 * This function selects the entries on the LRU which are able to
1482 * be demoted, and then kicks off the process by calling
1483 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001484 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485
Dave Chinner1ab6c492013-08-28 10:18:09 +10001486static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487{
1488 struct gfs2_glock *gl;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001489 LIST_HEAD(skipped);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001490 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001491 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001493 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001494 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001495 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001496
1497 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001498 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001499 list_move(&gl->gl_lru, &dispose);
1500 atomic_dec(&lru_count);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001501 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001502 continue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001503 }
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001504
1505 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001506 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001507 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001508 if (!list_empty(&dispose))
1509 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001510 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001511
1512 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001513}
1514
Dave Chinner1ab6c492013-08-28 10:18:09 +10001515static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1516 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001517{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001518 if (!(sc->gfp_mask & __GFP_FS))
1519 return SHRINK_STOP;
1520 return gfs2_scan_glock_lru(sc->nr_to_scan);
1521}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001522
Dave Chinner1ab6c492013-08-28 10:18:09 +10001523static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1524 struct shrink_control *sc)
1525{
Glauber Costa55f841c2013-08-28 10:17:53 +10001526 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001527}
1528
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001529static struct shrinker glock_shrinker = {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001530 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001531 .count_objects = gfs2_glock_shrink_count,
1532 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001533};
1534
David Teiglandb3b94fa2006-01-16 16:50:04 +00001535/**
1536 * examine_bucket - Call a function for glock in a hash bucket
1537 * @examiner: the function
1538 * @sdp: the filesystem
1539 * @bucket: the bucket
1540 *
Herbert Xu98687f42017-02-11 19:26:45 +08001541 * Note that the function can be called multiple times on the same
1542 * object. So the user must ensure that the function can cope with
1543 * that.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001544 */
1545
Bob Peterson88ffbf32015-03-16 11:02:46 -05001546static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001548 struct gfs2_glock *gl;
Herbert Xu98687f42017-02-11 19:26:45 +08001549 struct rhashtable_iter iter;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001550
Herbert Xu98687f42017-02-11 19:26:45 +08001551 rhashtable_walk_enter(&gl_hash_table, &iter);
1552
1553 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08001554 rhashtable_walk_start(&iter);
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
1561 rhashtable_walk_stop(&iter);
1562 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1563
1564 rhashtable_walk_exit(&iter);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001565}
1566
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001567/**
1568 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1569 * @gl: The glock to thaw
1570 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001571 */
1572
1573static void thaw_glock(struct gfs2_glock *gl)
1574{
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001575 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001576 gfs2_glock_put(gl);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001577 return;
Steven Whitehouse7286b312013-08-20 09:35:09 +01001578 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001579 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1580 gfs2_glock_queue_work(gl, 0);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001581}
1582
David Teiglandb3b94fa2006-01-16 16:50:04 +00001583/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001584 * clear_glock - look at a glock and see if we can free it from glock cache
1585 * @gl: the glock to look at
1586 *
1587 */
1588
1589static void clear_glock(struct gfs2_glock *gl)
1590{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001591 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001592
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001593 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousec741c452010-09-29 14:20:52 +01001594 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001595 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001596 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001597 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001598}
1599
1600/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001601 * gfs2_glock_thaw - Thaw any frozen glocks
1602 * @sdp: The super block
1603 *
1604 */
1605
1606void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1607{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001608 glock_hash_walk(thaw_glock, sdp);
1609}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001610
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001611static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001612{
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001613 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001614 gfs2_dump_glock(seq, gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001615 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001616}
1617
1618static void dump_glock_func(struct gfs2_glock *gl)
1619{
1620 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001621}
1622
1623/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001624 * gfs2_gl_hash_clear - Empty out the glock hash table
1625 * @sdp: the filesystem
1626 * @wait: wait until it's all gone
1627 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001628 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001629 */
1630
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001631void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001632{
David Teiglandfb6791d2012-11-13 10:58:56 -05001633 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001634 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001635 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001636 flush_workqueue(glock_workqueue);
Bob Peterson2aba1b52015-05-19 09:11:23 -05001637 wait_event_timeout(sdp->sd_glock_wait,
1638 atomic_read(&sdp->sd_glock_disposal) == 0,
1639 HZ * 600);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001640 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001641}
1642
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001643void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1644{
1645 struct gfs2_glock *gl = ip->i_gl;
1646 int ret;
1647
1648 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001649 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001650
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001651 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001652 clear_bit(GLF_LOCK, &gl->gl_flags);
1653 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001654 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001655}
1656
Steven Whitehouse6802e342008-05-21 17:03:22 +01001657static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001658{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001659 switch(state) {
1660 case LM_ST_UNLOCKED:
1661 return "UN";
1662 case LM_ST_SHARED:
1663 return "SH";
1664 case LM_ST_DEFERRED:
1665 return "DF";
1666 case LM_ST_EXCLUSIVE:
1667 return "EX";
1668 }
1669 return "??";
1670}
Robert Peterson04b933f2007-03-23 17:05:15 -05001671
Bob Petersonb58bf402015-07-24 09:45:43 -05001672static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
Steven Whitehouse6802e342008-05-21 17:03:22 +01001673{
1674 char *p = buf;
1675 if (flags & LM_FLAG_TRY)
1676 *p++ = 't';
1677 if (flags & LM_FLAG_TRY_1CB)
1678 *p++ = 'T';
1679 if (flags & LM_FLAG_NOEXP)
1680 *p++ = 'e';
1681 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001682 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001683 if (flags & LM_FLAG_PRIORITY)
1684 *p++ = 'p';
1685 if (flags & GL_ASYNC)
1686 *p++ = 'a';
1687 if (flags & GL_EXACT)
1688 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001689 if (flags & GL_NOCACHE)
1690 *p++ = 'c';
1691 if (test_bit(HIF_HOLDER, &iflags))
1692 *p++ = 'H';
1693 if (test_bit(HIF_WAIT, &iflags))
1694 *p++ = 'W';
1695 if (test_bit(HIF_FIRST, &iflags))
1696 *p++ = 'F';
1697 *p = 0;
1698 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001699}
1700
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701/**
1702 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001703 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001704 * @gh: the glock holder
1705 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001706 */
1707
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001708static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001710 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001711 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001712
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001713 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001714 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001715 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001716 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1717 state2str(gh->gh_state),
1718 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1719 gh->gh_error,
1720 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1721 gh_owner ? gh_owner->comm : "(ended)",
1722 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001723 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001724}
1725
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001726static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001727{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001728 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001729 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001730
Steven Whitehouse6802e342008-05-21 17:03:22 +01001731 if (test_bit(GLF_LOCK, gflags))
1732 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001733 if (test_bit(GLF_DEMOTE, gflags))
1734 *p++ = 'D';
1735 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1736 *p++ = 'd';
1737 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1738 *p++ = 'p';
1739 if (test_bit(GLF_DIRTY, gflags))
1740 *p++ = 'y';
1741 if (test_bit(GLF_LFLUSH, gflags))
1742 *p++ = 'f';
1743 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1744 *p++ = 'i';
1745 if (test_bit(GLF_REPLY_PENDING, gflags))
1746 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001747 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001748 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001749 if (test_bit(GLF_FROZEN, gflags))
1750 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001751 if (test_bit(GLF_QUEUED, gflags))
1752 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001753 if (test_bit(GLF_LRU, gflags))
1754 *p++ = 'L';
1755 if (gl->gl_object)
1756 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001757 if (test_bit(GLF_BLOCKING, gflags))
1758 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001759 *p = 0;
1760 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001761}
1762
1763/**
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +01001764 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001765 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001766 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001767 *
1768 * The file format is as follows:
1769 * One line per object, capital letters are used to indicate objects
1770 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1771 * other objects are indented by a single space and follow the glock to
1772 * which they are related. Fields are indicated by lower case letters
1773 * followed by a colon and the field value, except for strings which are in
1774 * [] so that its possible to see if they are composed of spaces for
1775 * example. The field's are n = number (id of the object), f = flags,
1776 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001778 */
1779
Bob Peterson27a2660f2018-04-18 12:05:01 -07001780void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001781{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001782 const struct gfs2_glock_operations *glops = gl->gl_ops;
1783 unsigned long long dtime;
1784 const struct gfs2_holder *gh;
1785 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001786
Steven Whitehouse6802e342008-05-21 17:03:22 +01001787 dtime = jiffies - gl->gl_demote_time;
1788 dtime *= 1000000/HZ; /* demote time in uSec */
1789 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1790 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001791 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 +01001792 state2str(gl->gl_state),
1793 gl->gl_name.ln_type,
1794 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001795 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001796 state2str(gl->gl_target),
1797 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001798 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001799 atomic_read(&gl->gl_revokes),
Steven Whitehousee66cf162013-10-15 15:18:08 +01001800 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001801
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001802 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1803 dump_holder(seq, gh);
1804
Steven Whitehouse6802e342008-05-21 17:03:22 +01001805 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001806 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001807}
1808
Steven Whitehousea2457692012-01-20 10:38:36 +00001809static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1810{
1811 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001812
Ben Hutchings4d207132015-08-27 12:51:45 -05001813 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 +00001814 gl->gl_name.ln_type,
1815 (unsigned long long)gl->gl_name.ln_number,
Ben Hutchings4d207132015-08-27 12:51:45 -05001816 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1817 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1818 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1819 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1820 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1821 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1822 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1823 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001824 return 0;
1825}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001826
Steven Whitehousea2457692012-01-20 10:38:36 +00001827static const char *gfs2_gltype[] = {
1828 "type",
1829 "reserved",
1830 "nondisk",
1831 "inode",
1832 "rgrp",
1833 "meta",
1834 "iopen",
1835 "flock",
1836 "plock",
1837 "quota",
1838 "journal",
1839};
1840
1841static const char *gfs2_stype[] = {
1842 [GFS2_LKS_SRTT] = "srtt",
1843 [GFS2_LKS_SRTTVAR] = "srttvar",
1844 [GFS2_LKS_SRTTB] = "srttb",
1845 [GFS2_LKS_SRTTVARB] = "srttvarb",
1846 [GFS2_LKS_SIRT] = "sirt",
1847 [GFS2_LKS_SIRTVAR] = "sirtvar",
1848 [GFS2_LKS_DCOUNT] = "dlm",
1849 [GFS2_LKS_QCOUNT] = "queue",
1850};
1851
1852#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1853
1854static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1855{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001856 struct gfs2_sbd *sdp = seq->private;
1857 loff_t pos = *(loff_t *)iter_ptr;
1858 unsigned index = pos >> 3;
1859 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001860 int i;
1861
1862 if (index == 0 && subindex != 0)
1863 return 0;
1864
1865 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1866 (index == 0) ? "cpu": gfs2_stype[subindex]);
1867
1868 for_each_possible_cpu(i) {
1869 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
Andreas Gruenbacher8f7e0a82015-08-27 13:02:54 -05001870
1871 if (index == 0)
1872 seq_printf(seq, " %15u", i);
1873 else
1874 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1875 lkstats[index - 1].stats[subindex]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001876 }
1877 seq_putc(seq, '\n');
1878 return 0;
1879}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001880
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001881int __init gfs2_glock_init(void)
1882{
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001883 int i, ret;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001884
1885 ret = rhashtable_init(&gl_hash_table, &ht_parms);
1886 if (ret < 0)
1887 return ret;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001888
Steven Whitehoused2115772010-11-03 19:58:53 +00001889 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001890 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001891 if (!glock_workqueue) {
1892 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001893 return -ENOMEM;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001894 }
Steven Whitehoused2115772010-11-03 19:58:53 +00001895 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001896 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001897 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001898 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001899 destroy_workqueue(glock_workqueue);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001900 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001901 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001902 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001903
Chao Yue0d735c2016-09-21 12:09:40 -05001904 ret = register_shrinker(&glock_shrinker);
1905 if (ret) {
1906 destroy_workqueue(gfs2_delete_workqueue);
1907 destroy_workqueue(glock_workqueue);
1908 rhashtable_destroy(&gl_hash_table);
1909 return ret;
1910 }
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001911
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001912 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
1913 init_waitqueue_head(glock_wait_table + i);
1914
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001915 return 0;
1916}
1917
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001918void gfs2_glock_exit(void)
1919{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001920 unregister_shrinker(&glock_shrinker);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001921 rhashtable_destroy(&gl_hash_table);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001922 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001923 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001924}
1925
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001926static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001927{
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001928 struct gfs2_glock *gl = gi->gl;
1929
1930 if (gl) {
1931 if (n == 0)
1932 return;
1933 if (!lockref_put_not_zero(&gl->gl_lockref))
1934 gfs2_glock_queue_put(gl);
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001935 }
1936 for (;;) {
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001937 gl = rhashtable_walk_next(&gi->hti);
1938 if (IS_ERR_OR_NULL(gl)) {
1939 if (gl == ERR_PTR(-EAGAIN)) {
1940 n = 1;
1941 continue;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001942 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001943 gl = NULL;
1944 break;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001945 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001946 if (gl->gl_name.ln_sbd != gi->sdp)
1947 continue;
1948 if (n <= 1) {
1949 if (!lockref_get_not_dead(&gl->gl_lockref))
1950 continue;
1951 break;
1952 } else {
1953 if (__lockref_is_dead(&gl->gl_lockref))
1954 continue;
1955 n--;
1956 }
Dan Carpenter14d37562016-12-14 08:02:03 -06001957 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001958 gi->gl = gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001959}
1960
Steven Whitehouse6802e342008-05-21 17:03:22 +01001961static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Bob Peterson27c3b412017-08-18 09:15:13 -05001962 __acquires(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00001963{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001964 struct gfs2_glock_iter *gi = seq->private;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001965 loff_t n;
Robert Peterson7c52b162007-03-16 10:26:37 +00001966
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001967 /*
1968 * We can either stay where we are, skip to the next hash table
1969 * entry, or start from the beginning.
1970 */
1971 if (*pos < gi->last_pos) {
1972 rhashtable_walk_exit(&gi->hti);
1973 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
1974 n = *pos + 1;
1975 } else {
1976 n = *pos - gi->last_pos;
1977 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001978
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001979 rhashtable_walk_start(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00001980
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001981 gfs2_glock_iter_next(gi, n);
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001982 gi->last_pos = *pos;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001983 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001984}
1985
Steven Whitehouse6802e342008-05-21 17:03:22 +01001986static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001987 loff_t *pos)
1988{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001989 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001990
1991 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001992 gi->last_pos = *pos;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001993 gfs2_glock_iter_next(gi, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001994 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001995}
1996
Steven Whitehouse6802e342008-05-21 17:03:22 +01001997static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Bob Peterson27c3b412017-08-18 09:15:13 -05001998 __releases(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00001999{
Steven Whitehouse6802e342008-05-21 17:03:22 +01002000 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00002001
Bob Peterson88ffbf32015-03-16 11:02:46 -05002002 rhashtable_walk_stop(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00002003}
2004
Steven Whitehouse6802e342008-05-21 17:03:22 +01002005static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00002006{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00002007 dump_glock(seq, iter_ptr);
2008 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00002009}
2010
Steven Whitehousea2457692012-01-20 10:38:36 +00002011static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2012{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002013 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00002014 if (*pos >= GFS2_NR_SBSTATS)
2015 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002016 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002017}
2018
2019static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2020 loff_t *pos)
2021{
Steven Whitehousea2457692012-01-20 10:38:36 +00002022 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002023 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00002024 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002025 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002026}
2027
2028static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2029{
2030 preempt_enable();
2031}
2032
Denis Cheng4ef29002007-07-31 18:31:11 +08002033static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002034 .start = gfs2_glock_seq_start,
2035 .next = gfs2_glock_seq_next,
2036 .stop = gfs2_glock_seq_stop,
2037 .show = gfs2_glock_seq_show,
2038};
2039
Steven Whitehousea2457692012-01-20 10:38:36 +00002040static const struct seq_operations gfs2_glstats_seq_ops = {
2041 .start = gfs2_glock_seq_start,
2042 .next = gfs2_glock_seq_next,
2043 .stop = gfs2_glock_seq_stop,
2044 .show = gfs2_glstats_seq_show,
2045};
2046
2047static const struct seq_operations gfs2_sbstats_seq_ops = {
2048 .start = gfs2_sbstats_seq_start,
2049 .next = gfs2_sbstats_seq_next,
2050 .stop = gfs2_sbstats_seq_stop,
2051 .show = gfs2_sbstats_seq_show,
2052};
2053
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002054#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2055
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002056static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2057 const struct seq_operations *ops)
Robert Peterson7c52b162007-03-16 10:26:37 +00002058{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002059 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
Steven Whitehouse6802e342008-05-21 17:03:22 +01002060 if (ret == 0) {
2061 struct seq_file *seq = file->private_data;
2062 struct gfs2_glock_iter *gi = seq->private;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002063
Steven Whitehouse6802e342008-05-21 17:03:22 +01002064 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002065 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01002066 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002067 seq->size = GFS2_SEQ_GOODSIZE;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002068 /*
2069 * Initially, we are "before" the first hash table entry; the
2070 * first call to rhashtable_walk_next gets us the first entry.
2071 */
2072 gi->last_pos = -1;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002073 gi->gl = NULL;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002074 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
Steven Whitehouse6802e342008-05-21 17:03:22 +01002075 }
2076 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00002077}
2078
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002079static int gfs2_glocks_open(struct inode *inode, struct file *file)
2080{
2081 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2082}
2083
Bob Peterson88ffbf32015-03-16 11:02:46 -05002084static int gfs2_glocks_release(struct inode *inode, struct file *file)
2085{
2086 struct seq_file *seq = file->private_data;
2087 struct gfs2_glock_iter *gi = seq->private;
2088
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02002089 if (gi->gl)
2090 gfs2_glock_put(gi->gl);
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002091 rhashtable_walk_exit(&gi->hti);
Bob Peterson88ffbf32015-03-16 11:02:46 -05002092 return seq_release_private(inode, file);
2093}
2094
Steven Whitehousea2457692012-01-20 10:38:36 +00002095static int gfs2_glstats_open(struct inode *inode, struct file *file)
2096{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002097 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002098}
2099
2100static int gfs2_sbstats_open(struct inode *inode, struct file *file)
2101{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002102 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002103 if (ret == 0) {
2104 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002105 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00002106 }
2107 return ret;
2108}
2109
2110static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002111 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00002112 .open = gfs2_glocks_open,
2113 .read = seq_read,
2114 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002115 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002116};
2117
2118static const struct file_operations gfs2_glstats_fops = {
2119 .owner = THIS_MODULE,
2120 .open = gfs2_glstats_open,
2121 .read = seq_read,
2122 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002123 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002124};
2125
2126static const struct file_operations gfs2_sbstats_fops = {
2127 .owner = THIS_MODULE,
2128 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00002129 .read = seq_read,
2130 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002131 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00002132};
2133
2134int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2135{
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002136 struct dentry *dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002137
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002138 dent = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2139 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002140 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002141 sdp->debugfs_dir = dent;
Steven Whitehousea2457692012-01-20 10:38:36 +00002142
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002143 dent = debugfs_create_file("glocks",
2144 S_IFREG | S_IRUGO,
2145 sdp->debugfs_dir, sdp,
2146 &gfs2_glocks_fops);
2147 if (IS_ERR_OR_NULL(dent))
Steven Whitehousea2457692012-01-20 10:38:36 +00002148 goto fail;
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002149 sdp->debugfs_dentry_glocks = dent;
2150
2151 dent = debugfs_create_file("glstats",
2152 S_IFREG | S_IRUGO,
2153 sdp->debugfs_dir, sdp,
2154 &gfs2_glstats_fops);
2155 if (IS_ERR_OR_NULL(dent))
2156 goto fail;
2157 sdp->debugfs_dentry_glstats = dent;
2158
2159 dent = debugfs_create_file("sbstats",
2160 S_IFREG | S_IRUGO,
2161 sdp->debugfs_dir, sdp,
2162 &gfs2_sbstats_fops);
2163 if (IS_ERR_OR_NULL(dent))
2164 goto fail;
2165 sdp->debugfs_dentry_sbstats = dent;
Robert Peterson7c52b162007-03-16 10:26:37 +00002166
2167 return 0;
Steven Whitehousea2457692012-01-20 10:38:36 +00002168fail:
2169 gfs2_delete_debugfs_file(sdp);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002170 return dent ? PTR_ERR(dent) : -ENOMEM;
Robert Peterson7c52b162007-03-16 10:26:37 +00002171}
2172
2173void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2174{
Steven Whitehousea2457692012-01-20 10:38:36 +00002175 if (sdp->debugfs_dir) {
Robert Peterson5f882092007-04-18 11:41:11 -05002176 if (sdp->debugfs_dentry_glocks) {
2177 debugfs_remove(sdp->debugfs_dentry_glocks);
2178 sdp->debugfs_dentry_glocks = NULL;
2179 }
Steven Whitehousea2457692012-01-20 10:38:36 +00002180 if (sdp->debugfs_dentry_glstats) {
2181 debugfs_remove(sdp->debugfs_dentry_glstats);
2182 sdp->debugfs_dentry_glstats = NULL;
2183 }
2184 if (sdp->debugfs_dentry_sbstats) {
2185 debugfs_remove(sdp->debugfs_dentry_sbstats);
2186 sdp->debugfs_dentry_sbstats = NULL;
2187 }
Robert Peterson5f882092007-04-18 11:41:11 -05002188 debugfs_remove(sdp->debugfs_dir);
2189 sdp->debugfs_dir = NULL;
2190 }
Robert Peterson7c52b162007-03-16 10:26:37 +00002191}
2192
2193int gfs2_register_debugfs(void)
2194{
2195 gfs2_root = debugfs_create_dir("gfs2", NULL);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002196 if (IS_ERR(gfs2_root))
2197 return PTR_ERR(gfs2_root);
Robert Peterson7c52b162007-03-16 10:26:37 +00002198 return gfs2_root ? 0 : -ENOMEM;
2199}
2200
2201void gfs2_unregister_debugfs(void)
2202{
2203 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002204 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002205}