blob: e4f6d39500bcc710e3958208475f4791f4275ea5 [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{
Andreas Gruenbacher605b0482019-03-06 15:41:57 +0100110 u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500111
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{
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000186 if (!(gl->gl_ops->go_flags & GLOF_LRU))
187 return;
188
Steven Whitehouse29687a22011-03-30 16:33:25 +0100189 spin_lock(&lru_lock);
190
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000191 list_del(&gl->gl_lru);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100192 list_add_tail(&gl->gl_lru, &lru_list);
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000193
194 if (!test_bit(GLF_LRU, &gl->gl_flags)) {
195 set_bit(GLF_LRU, &gl->gl_flags);
196 atomic_inc(&lru_count);
197 }
198
Steven Whitehouse29687a22011-03-30 16:33:25 +0100199 spin_unlock(&lru_lock);
200}
201
Bob Peterson8f6cb402015-01-05 13:25:10 -0500202static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
Steven Whitehousef42ab082011-04-14 16:50:31 +0100203{
Bob Peterson645ebd42017-07-26 10:57:35 -0500204 if (!(gl->gl_ops->go_flags & GLOF_LRU))
205 return;
206
Bob Peterson8f6cb402015-01-05 13:25:10 -0500207 spin_lock(&lru_lock);
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000208 if (test_bit(GLF_LRU, &gl->gl_flags)) {
Steven Whitehousef42ab082011-04-14 16:50:31 +0100209 list_del_init(&gl->gl_lru);
210 atomic_dec(&lru_count);
211 clear_bit(GLF_LRU, &gl->gl_flags);
212 }
213 spin_unlock(&lru_lock);
214}
215
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500216/*
217 * Enqueue the glock on the work queue. Passes one glock reference on to the
218 * work queue.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 */
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500220static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
221 if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) {
222 /*
223 * We are holding the lockref spinlock, and the work was still
224 * queued above. The queued work (glock_work_func) takes that
225 * spinlock before dropping its glock reference(s), so it
226 * cannot have dropped them in the meantime.
227 */
228 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
229 gl->gl_lockref.count--;
230 }
231}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500233static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
234 spin_lock(&gl->gl_lockref.lock);
235 __gfs2_glock_queue_work(gl, delay);
236 spin_unlock(&gl->gl_lockref.lock);
237}
238
239static void __gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240{
Bob Peterson15562c42015-03-16 11:52:05 -0500241 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000242 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243
Steven Whitehousee66cf162013-10-15 15:18:08 +0100244 lockref_mark_dead(&gl->gl_lockref);
245
Bob Peterson8f6cb402015-01-05 13:25:10 -0500246 gfs2_glock_remove_from_lru(gl);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100247 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100248 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
249 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
250 trace_gfs2_glock_put(gl);
251 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252}
253
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -0500254/*
255 * Cause the glock to be put in work queue context.
256 */
257void gfs2_glock_queue_put(struct gfs2_glock *gl)
258{
259 gfs2_glock_queue_work(gl, 0);
260}
261
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262/**
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500263 * gfs2_glock_put() - Decrement reference count on glock
264 * @gl: The glock to put
265 *
266 */
267
268void gfs2_glock_put(struct gfs2_glock *gl)
269{
270 if (lockref_put_or_lock(&gl->gl_lockref))
271 return;
272
273 __gfs2_glock_put(gl);
274}
275
276/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100277 * may_grant - check if its ok to grant a new lock
278 * @gl: The glock
279 * @gh: The lock request which we wish to grant
280 *
281 * Returns: true if its ok to grant the lock
282 */
283
284static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500285{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100286 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
287 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
288 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
289 return 0;
290 if (gl->gl_state == gh->gh_state)
291 return 1;
292 if (gh->gh_flags & GL_EXACT)
293 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100294 if (gl->gl_state == LM_ST_EXCLUSIVE) {
295 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
296 return 1;
297 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
298 return 1;
299 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100300 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
301 return 1;
302 return 0;
303}
304
305static void gfs2_holder_wake(struct gfs2_holder *gh)
306{
307 clear_bit(HIF_WAIT, &gh->gh_iflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100308 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100309 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
310}
311
312/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100313 * do_error - Something unexpected has happened during a lock request
314 *
315 */
316
Denys Vlasenkoa527b382016-04-12 12:39:12 -0400317static void do_error(struct gfs2_glock *gl, const int ret)
Steven Whitehoused5341a92010-07-23 14:05:51 +0100318{
319 struct gfs2_holder *gh, *tmp;
320
321 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
322 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
323 continue;
324 if (ret & LM_OUT_ERROR)
325 gh->gh_error = -EIO;
326 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
327 gh->gh_error = GLR_TRYFAILED;
328 else
329 continue;
330 list_del_init(&gh->gh_list);
331 trace_gfs2_glock_queue(gh, 0);
332 gfs2_holder_wake(gh);
333 }
334}
335
336/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100337 * do_promote - promote as many requests as possible on the current queue
338 * @gl: The glock
339 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000340 * Returns: 1 if there is a blocked holder at the head of the list, or 2
341 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100342 */
343
344static int do_promote(struct gfs2_glock *gl)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500345__releases(&gl->gl_lockref.lock)
346__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100347{
348 const struct gfs2_glock_operations *glops = gl->gl_ops;
349 struct gfs2_holder *gh, *tmp;
350 int ret;
351
352restart:
353 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
354 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
355 continue;
356 if (may_grant(gl, gh)) {
357 if (gh->gh_list.prev == &gl->gl_holders &&
358 glops->go_lock) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500359 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100360 /* FIXME: eliminate this eventually */
361 ret = glops->go_lock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500362 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100363 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000364 if (ret == 1)
365 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100366 gh->gh_error = ret;
367 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100368 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100369 gfs2_holder_wake(gh);
370 goto restart;
371 }
372 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100373 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100374 gfs2_holder_wake(gh);
375 goto restart;
376 }
377 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100378 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100379 gfs2_holder_wake(gh);
380 continue;
381 }
382 if (gh->gh_list.prev == &gl->gl_holders)
383 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100384 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100385 break;
386 }
387 return 0;
388}
389
390/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100391 * find_first_waiter - find the first gh that's waiting for the glock
392 * @gl: the glock
393 */
394
395static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
396{
397 struct gfs2_holder *gh;
398
399 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
400 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
401 return gh;
402 }
403 return NULL;
404}
405
406/**
407 * state_change - record that the glock is now in a different state
408 * @gl: the glock
409 * @new_state the new state
410 *
411 */
412
413static void state_change(struct gfs2_glock *gl, unsigned int new_state)
414{
415 int held1, held2;
416
417 held1 = (gl->gl_state != LM_ST_UNLOCKED);
418 held2 = (new_state != LM_ST_UNLOCKED);
419
420 if (held1 != held2) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100421 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100422 if (held2)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100423 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100424 else
Steven Whitehousee66cf162013-10-15 15:18:08 +0100425 gl->gl_lockref.count--;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100426 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100427 if (held1 && held2 && list_empty(&gl->gl_holders))
428 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100429
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400430 if (new_state != gl->gl_target)
431 /* shorten our minimum hold time */
432 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
433 GL_GLOCK_MIN_HOLD);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100434 gl->gl_state = new_state;
435 gl->gl_tchange = jiffies;
436}
437
438static void gfs2_demote_wake(struct gfs2_glock *gl)
439{
440 gl->gl_demote_state = LM_ST_EXCLUSIVE;
441 clear_bit(GLF_DEMOTE, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100442 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100443 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
444}
445
446/**
447 * finish_xmote - The DLM has replied to one of our lock requests
448 * @gl: The glock
449 * @ret: The status from the DLM
450 *
451 */
452
453static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
454{
455 const struct gfs2_glock_operations *glops = gl->gl_ops;
456 struct gfs2_holder *gh;
457 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000458 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500459
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500460 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse63997772009-06-12 08:49:20 +0100461 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100462 state_change(gl, state);
463 gh = find_first_waiter(gl);
464
465 /* Demote to UN request arrived during demote to SH or DF */
466 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
467 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
468 gl->gl_target = LM_ST_UNLOCKED;
469
470 /* Check for state != intended state */
471 if (unlikely(state != gl->gl_target)) {
472 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
473 /* move to back of queue and try next entry */
474 if (ret & LM_OUT_CANCELED) {
475 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
476 list_move_tail(&gh->gh_list, &gl->gl_holders);
477 gh = find_first_waiter(gl);
478 gl->gl_target = gh->gh_state;
479 goto retry;
480 }
481 /* Some error or failed "try lock" - report it */
482 if ((ret & LM_OUT_ERROR) ||
483 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
484 gl->gl_target = gl->gl_state;
485 do_error(gl, ret);
486 goto out;
487 }
488 }
489 switch(state) {
490 /* Unlocked due to conversion deadlock, try again */
491 case LM_ST_UNLOCKED:
492retry:
493 do_xmote(gl, gh, gl->gl_target);
494 break;
495 /* Conversion fails, unlock and try again */
496 case LM_ST_SHARED:
497 case LM_ST_DEFERRED:
498 do_xmote(gl, gh, LM_ST_UNLOCKED);
499 break;
500 default: /* Everything else */
Bob Petersone54c78a2018-10-03 08:47:36 -0500501 fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
502 gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100503 GLOCK_BUG_ON(gl, 1);
504 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500505 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100506 return;
507 }
508
509 /* Fast path - we got what we asked for */
510 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
511 gfs2_demote_wake(gl);
512 if (state != LM_ST_UNLOCKED) {
513 if (glops->go_xmote_bh) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500514 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100515 rv = glops->go_xmote_bh(gl, gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500516 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100517 if (rv) {
518 do_error(gl, rv);
519 goto out;
520 }
521 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000522 rv = do_promote(gl);
523 if (rv == 2)
524 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100525 }
526out:
527 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000528out_locked:
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500529 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500530}
531
Steven Whitehouse6802e342008-05-21 17:03:22 +0100532/**
533 * do_xmote - Calls the DLM to change the state of a lock
534 * @gl: The lock state
535 * @gh: The holder (only for promotes)
536 * @target: The target lock state
537 *
538 */
539
540static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500541__releases(&gl->gl_lockref.lock)
542__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100543{
544 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500545 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Bob Petersonb58bf402015-07-24 09:45:43 -0500546 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100547 int ret;
548
Bob Petersoned175452017-05-05 09:43:02 -0500549 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) &&
550 target != LM_ST_UNLOCKED)
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500551 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100552 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
553 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000554 GLOCK_BUG_ON(gl, gl->gl_state == target);
555 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100556 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
557 glops->go_inval) {
558 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
559 do_error(gl, 0); /* Fail queued try locks */
560 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000561 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000562 set_bit(GLF_BLOCKING, &gl->gl_flags);
563 if ((gl->gl_req == LM_ST_UNLOCKED) ||
564 (gl->gl_state == LM_ST_EXCLUSIVE) ||
565 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
566 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500567 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson06dfc302012-10-24 14:41:05 -0400568 if (glops->go_sync)
569 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100570 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
571 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
572 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
573
574 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000575 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
576 /* lock_dlm */
577 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400578 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
579 target == LM_ST_UNLOCKED &&
580 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
581 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500582 gfs2_glock_queue_work(gl, 0);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400583 }
584 else if (ret) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500585 fs_err(sdp, "lm_lock ret %d\n", ret);
Bob Petersoned175452017-05-05 09:43:02 -0500586 GLOCK_BUG_ON(gl, !test_bit(SDF_SHUTDOWN,
587 &sdp->sd_flags));
David Teiglanddba2d702012-11-14 13:46:53 -0500588 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000589 } else { /* lock_nolock */
590 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500591 gfs2_glock_queue_work(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100592 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000593
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500594 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100595}
596
597/**
598 * find_first_holder - find the first "holder" gh
599 * @gl: the glock
600 */
601
602static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
603{
604 struct gfs2_holder *gh;
605
606 if (!list_empty(&gl->gl_holders)) {
607 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
608 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
609 return gh;
610 }
611 return NULL;
612}
613
614/**
615 * run_queue - do all outstanding tasks related to a glock
616 * @gl: The glock in question
617 * @nonblock: True if we must not block in run_queue
618 *
619 */
620
621static void run_queue(struct gfs2_glock *gl, const int nonblock)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500622__releases(&gl->gl_lockref.lock)
623__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100624{
625 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000626 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100627
628 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
629 return;
630
631 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
632
633 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
634 gl->gl_demote_state != gl->gl_state) {
635 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000636 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100637 if (nonblock)
638 goto out_sched;
639 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100640 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100641 gl->gl_target = gl->gl_demote_state;
642 } else {
643 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
644 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000645 ret = do_promote(gl);
646 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000647 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000648 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100649 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100650 gh = find_first_waiter(gl);
651 gl->gl_target = gh->gh_state;
652 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
653 do_error(gl, 0); /* Fail queued try locks */
654 }
655 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100656out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100657 return;
658
659out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100660 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100661 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100662 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500663 __gfs2_glock_queue_work(gl, 0);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100664 return;
665
Steven Whitehoused8348de2009-02-05 10:12:38 +0000666out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100667 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100668 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100669 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100670}
671
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500672static void delete_work_func(struct work_struct *work)
673{
674 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500675 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500676 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000677 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500678
Bob Petersona4923862015-12-07 16:24:27 -0600679 /* If someone's using this glock to create a new dinode, the block must
680 have been freed by another node, then re-used, in which case our
681 iopen callback is too late after the fact. Ignore it. */
682 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
683 goto out;
684
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500685 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000686 if (inode && !IS_ERR(inode)) {
687 d_prune_aliases(inode);
688 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500689 }
Bob Petersona4923862015-12-07 16:24:27 -0600690out:
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500691 gfs2_glock_put(gl);
692}
693
Steven Whitehouse6802e342008-05-21 17:03:22 +0100694static void glock_work_func(struct work_struct *work)
695{
696 unsigned long delay = 0;
697 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500698 unsigned int drop_refs = 1;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100699
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000700 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100701 finish_xmote(gl, gl->gl_reply);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500702 drop_refs++;
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000703 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500704 spin_lock(&gl->gl_lockref.lock);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400705 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100706 gl->gl_state != LM_ST_UNLOCKED &&
707 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100708 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400709
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400710 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100711 if (time_before(now, holdtime))
712 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400713
714 if (!delay) {
715 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
716 set_bit(GLF_DEMOTE, &gl->gl_flags);
717 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100718 }
719 run_queue(gl, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500720 if (delay) {
721 /* Keep one glock reference for the work we requeue. */
722 drop_refs--;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400723 if (gl->gl_name.ln_type != LM_TYPE_INODE)
724 delay = 0;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500725 __gfs2_glock_queue_work(gl, delay);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400726 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500727
728 /*
729 * Drop the remaining glock references manually here. (Mind that
730 * __gfs2_glock_queue_work depends on the lockref spinlock begin held
731 * here as well.)
732 */
733 gl->gl_lockref.count -= drop_refs;
734 if (!gl->gl_lockref.count) {
735 __gfs2_glock_put(gl);
736 return;
737 }
738 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100739}
740
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500741static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
742 struct gfs2_glock *new)
743{
744 struct wait_glock_queue wait;
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500745 wait_queue_head_t *wq = glock_waitqueue(name);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500746 struct gfs2_glock *gl;
747
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500748 wait.name = name;
749 init_wait(&wait.wait);
750 wait.wait.func = glock_wake_function;
751
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500752again:
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500753 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500754 rcu_read_lock();
755 if (new) {
756 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
757 &new->gl_node, ht_parms);
758 if (IS_ERR(gl))
759 goto out;
760 } else {
761 gl = rhashtable_lookup_fast(&gl_hash_table,
762 name, ht_parms);
763 }
764 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
765 rcu_read_unlock();
766 schedule();
767 goto again;
768 }
769out:
770 rcu_read_unlock();
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500771 finish_wait(wq, &wait.wait);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500772 return gl;
773}
774
David Teiglandb3b94fa2006-01-16 16:50:04 +0000775/**
776 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
777 * @sdp: The GFS2 superblock
778 * @number: the lock number
779 * @glops: The glock_operations to use
780 * @create: If 0, don't create the glock if it doesn't exist
781 * @glp: the glock is returned here
782 *
783 * This does not lock a glock, just finds/creates structures for one.
784 *
785 * Returns: errno
786 */
787
Steven Whitehousecd915492006-09-04 12:49:07 -0400788int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400789 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 struct gfs2_glock **glp)
791{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000792 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500793 struct lm_lockname name = { .ln_number = number,
794 .ln_type = glops->go_type,
795 .ln_sbd = sdp };
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100796 struct gfs2_glock *gl, *tmp;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000797 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000798 struct kmem_cache *cachep;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100799 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500801 gl = find_insert_glock(&name, NULL);
802 if (gl) {
803 *glp = gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000804 return 0;
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500805 }
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000806 if (!create)
807 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808
Steven Whitehouse009d8512009-12-08 12:12:13 +0000809 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000810 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000811 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000812 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100813 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814 if (!gl)
815 return -ENOMEM;
816
David Teiglanddba2d702012-11-14 13:46:53 -0500817 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500818
819 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100820 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500821 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500822 kmem_cache_free(cachep, gl);
823 return -ENOMEM;
824 }
David Teiglanddba2d702012-11-14 13:46:53 -0500825 }
826
Steven Whitehouse8f052282010-01-29 15:21:27 +0000827 atomic_inc(&sdp->sd_glock_disposal);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500828 gl->gl_node.next = NULL;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400829 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100831 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100833 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500834 gl->gl_demote_state = LM_ST_EXCLUSIVE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835 gl->gl_ops = glops;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100836 gl->gl_dstamp = 0;
Steven Whitehousea2457692012-01-20 10:38:36 +0000837 preempt_disable();
838 /* We use the global stats to estimate the initial per-glock stats */
839 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
840 preempt_enable();
841 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
842 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500843 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400844 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400845 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500846 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500847 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848
Steven Whitehouse009d8512009-12-08 12:12:13 +0000849 mapping = gfs2_glock2aspace(gl);
850 if (mapping) {
851 mapping->a_ops = &gfs2_meta_aops;
852 mapping->host = s->s_bdev->bd_inode;
853 mapping->flags = 0;
854 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800855 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000856 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 }
858
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500859 tmp = find_insert_glock(&name, gl);
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100860 if (!tmp) {
Bob Peterson88ffbf32015-03-16 11:02:46 -0500861 *glp = gl;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100862 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000863 }
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100864 if (IS_ERR(tmp)) {
865 ret = PTR_ERR(tmp);
866 goto out_free;
867 }
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500868 *glp = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100870out_free:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500871 kfree(gl->gl_lksb.sb_lvbptr);
872 kmem_cache_free(cachep, gl);
873 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100875out:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500876 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877}
878
879/**
880 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
881 * @gl: the glock
882 * @state: the state we're requesting
883 * @flags: the modifier flags
884 * @gh: the holder structure
885 *
886 */
887
Bob Petersonb58bf402015-07-24 09:45:43 -0500888void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889 struct gfs2_holder *gh)
890{
891 INIT_LIST_HEAD(&gh->gh_list);
892 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200893 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800894 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 gh->gh_state = state;
896 gh->gh_flags = flags;
897 gh->gh_error = 0;
898 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899 gfs2_glock_hold(gl);
900}
901
902/**
903 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
904 * @state: the state we're requesting
905 * @flags: the modifier flags
906 * @gh: the holder structure
907 *
908 * Don't mess with the glock.
909 *
910 */
911
Bob Petersonb58bf402015-07-24 09:45:43 -0500912void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913{
914 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400915 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000916 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200917 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100918 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400919 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920}
921
922/**
923 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
924 * @gh: the holder structure
925 *
926 */
927
928void gfs2_holder_uninit(struct gfs2_holder *gh)
929{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800930 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 gfs2_glock_put(gh->gh_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500932 gfs2_holder_mark_uninitialized(gh);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500933 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934}
935
Steven Whitehousefe64d512009-05-19 10:01:18 +0100936/**
Bob Peterson07a79042012-08-09 12:48:44 -0500937 * gfs2_glock_wait - wait on a glock acquisition
938 * @gh: the glock holder
939 *
940 * Returns: 0 on success
941 */
942
943int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000944{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400945 unsigned long time1 = jiffies;
946
Steven Whitehousefee852e2007-01-17 15:33:23 +0000947 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000948 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400949 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
950 /* Lengthen the minimum hold time. */
951 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
952 GL_GLOCK_HOLD_INCR,
953 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500954 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100955}
956
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000958 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 * @gl: the glock
960 * @state: the state the caller wants us to change to
961 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000962 * There are only two requests that we are going to see in actual
963 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964 */
965
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500966static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100967 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000968{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500969 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
970
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500971 set_bit(bit, &gl->gl_flags);
972 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000973 gl->gl_demote_state = state;
974 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100975 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
976 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100977 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500979 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100980 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100981 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982}
983
Steven Whitehouse6802e342008-05-21 17:03:22 +0100984void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000985{
Joe Perches5e690692010-11-09 16:35:20 -0800986 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000987 va_list args;
988
989 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800990
Steven Whitehouse6802e342008-05-21 17:03:22 +0100991 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100992 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100993 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800994 vaf.fmt = fmt;
995 vaf.va = &args;
996
Joe Perchesd77d1b52014-03-06 12:10:45 -0800997 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100998 }
Joe Perches5e690692010-11-09 16:35:20 -0800999
Robert Peterson7c52b162007-03-16 10:26:37 +00001000 va_end(args);
1001}
1002
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1005 * @gh: the holder structure to add
1006 *
Steven Whitehouse6802e342008-05-21 17:03:22 +01001007 * Eventually we should move the recursive locking trap to a
1008 * debugging option or something like that. This is the fast
1009 * path and needs to have the minimum number of distractions.
1010 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011 */
1012
Steven Whitehouse6802e342008-05-21 17:03:22 +01001013static inline void add_to_queue(struct gfs2_holder *gh)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001014__releases(&gl->gl_lockref.lock)
1015__acquires(&gl->gl_lockref.lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016{
1017 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001018 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001019 struct list_head *insert_pt = NULL;
1020 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001021 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001023 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +00001024 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1025 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -04001026
Steven Whitehouse6802e342008-05-21 17:03:22 +01001027 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1028 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -05001029 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001030 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1031 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 }
1033
Steven Whitehouse6802e342008-05-21 17:03:22 +01001034 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1035 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
1036 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
1037 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001038 if (try_futile &&
1039 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001040fail:
1041 gh->gh_error = GLR_TRYFAILED;
1042 gfs2_holder_wake(gh);
1043 return;
1044 }
1045 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1046 continue;
1047 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
1048 insert_pt = &gh2->gh_list;
1049 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001050 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +00001051 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +00001052 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1053 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001054 if (likely(insert_pt == NULL)) {
1055 list_add_tail(&gh->gh_list, &gl->gl_holders);
1056 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
1057 goto do_cancel;
1058 return;
1059 }
1060 list_add_tail(&gh->gh_list, insert_pt);
1061do_cancel:
1062 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
1063 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001064 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +01001065 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001066 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001067 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001068 }
1069 return;
1070
1071trap_recursive:
Bob Petersone54c78a2018-10-03 08:47:36 -05001072 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1073 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1074 fs_err(sdp, "lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001075 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Bob Petersone54c78a2018-10-03 08:47:36 -05001076 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1077 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1078 fs_err(sdp, "lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001079 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +01001080 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001081 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082}
1083
1084/**
1085 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1086 * @gh: the holder structure
1087 *
1088 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1089 *
1090 * Returns: 0, GLR_TRYFAILED, or errno on failure
1091 */
1092
1093int gfs2_glock_nq(struct gfs2_holder *gh)
1094{
1095 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001096 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 int error = 0;
1098
Steven Whitehouse6802e342008-05-21 17:03:22 +01001099 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101
Steven Whitehousef42ab082011-04-14 16:50:31 +01001102 if (test_bit(GLF_LRU, &gl->gl_flags))
1103 gfs2_glock_remove_from_lru(gl);
1104
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001105 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -04001107 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1108 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001109 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -04001110 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001111 __gfs2_glock_queue_work(gl, 0);
Bob Peterson01b172b2014-03-12 10:32:20 -04001112 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001113 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001114 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115
Steven Whitehouse6802e342008-05-21 17:03:22 +01001116 if (!(gh->gh_flags & GL_ASYNC))
1117 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 return error;
1120}
1121
1122/**
1123 * gfs2_glock_poll - poll to see if an async request has been completed
1124 * @gh: the holder
1125 *
1126 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1127 */
1128
1129int gfs2_glock_poll(struct gfs2_holder *gh)
1130{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001131 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001132}
1133
1134/**
1135 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1136 * @gh: the glock holder
1137 *
1138 */
1139
1140void gfs2_glock_dq(struct gfs2_holder *gh)
1141{
1142 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001143 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001144 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001145 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001146
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001147 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001148 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001149 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001150
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151 list_del_init(&gh->gh_list);
Bob Peterson7508abc2015-12-18 11:54:55 -06001152 clear_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001153 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001154 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001155 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001156 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 glops->go_unlock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001158 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001159 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001160 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001161 if (list_empty(&gl->gl_holders) &&
1162 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1163 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1164 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 }
Ross Lagerwall7881ef32019-03-27 17:09:17 +00001166 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
Bob Peterson4abb6ad92012-08-09 12:48:43 -05001167 gfs2_glock_add_to_lru(gl);
1168
Steven Whitehouse63997772009-06-12 08:49:20 +01001169 trace_gfs2_glock_queue(gh, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001170 if (unlikely(!fast_path)) {
1171 gl->gl_lockref.count++;
1172 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1173 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1174 gl->gl_name.ln_type == LM_TYPE_INODE)
1175 delay = gl->gl_hold_time;
1176 __gfs2_glock_queue_work(gl, delay);
1177 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001178 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001179}
1180
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001181void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1182{
1183 struct gfs2_glock *gl = gh->gh_gl;
1184 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001185 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001186 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001187}
1188
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1191 * @gh: the holder structure
1192 *
1193 */
1194
1195void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1196{
1197 gfs2_glock_dq(gh);
1198 gfs2_holder_uninit(gh);
1199}
1200
1201/**
1202 * gfs2_glock_nq_num - acquire a glock based on lock number
1203 * @sdp: the filesystem
1204 * @number: the lock number
1205 * @glops: the glock operations for the type of glock
1206 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001207 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208 * @gh: the struct gfs2_holder
1209 *
1210 * Returns: errno
1211 */
1212
Steven Whitehousecd915492006-09-04 12:49:07 -04001213int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001214 const struct gfs2_glock_operations *glops,
Bob Petersonb58bf402015-07-24 09:45:43 -05001215 unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216{
1217 struct gfs2_glock *gl;
1218 int error;
1219
1220 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1221 if (!error) {
1222 error = gfs2_glock_nq_init(gl, state, flags, gh);
1223 gfs2_glock_put(gl);
1224 }
1225
1226 return error;
1227}
1228
1229/**
1230 * glock_compare - Compare two struct gfs2_glock structures for sorting
1231 * @arg_a: the first structure
1232 * @arg_b: the second structure
1233 *
1234 */
1235
1236static int glock_compare(const void *arg_a, const void *arg_b)
1237{
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001238 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1239 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1240 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1241 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001242
1243 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001244 return 1;
1245 if (a->ln_number < b->ln_number)
1246 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001247 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001248 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249}
1250
1251/**
1252 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1253 * @num_gh: the number of structures
1254 * @ghs: an array of struct gfs2_holder structures
1255 *
1256 * Returns: 0 on success (all glocks acquired),
1257 * errno on failure (no glocks acquired)
1258 */
1259
1260static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1261 struct gfs2_holder **p)
1262{
1263 unsigned int x;
1264 int error = 0;
1265
1266 for (x = 0; x < num_gh; x++)
1267 p[x] = &ghs[x];
1268
1269 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1270
1271 for (x = 0; x < num_gh; x++) {
1272 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1273
1274 error = gfs2_glock_nq(p[x]);
1275 if (error) {
1276 while (x--)
1277 gfs2_glock_dq(p[x]);
1278 break;
1279 }
1280 }
1281
1282 return error;
1283}
1284
1285/**
1286 * gfs2_glock_nq_m - acquire multiple glocks
1287 * @num_gh: the number of structures
1288 * @ghs: an array of struct gfs2_holder structures
1289 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290 *
1291 * Returns: 0 on success (all glocks acquired),
1292 * errno on failure (no glocks acquired)
1293 */
1294
1295int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1296{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001297 struct gfs2_holder *tmp[4];
1298 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001299 int error = 0;
1300
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001301 switch(num_gh) {
1302 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001304 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001305 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1306 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001307 default:
1308 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 break;
Kees Cook6da2ec52018-06-12 13:55:00 -07001310 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1311 GFP_NOFS);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001312 if (!pph)
1313 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314 }
1315
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001316 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001318 if (pph != tmp)
1319 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320
1321 return error;
1322}
1323
1324/**
1325 * gfs2_glock_dq_m - release multiple glocks
1326 * @num_gh: the number of structures
1327 * @ghs: an array of struct gfs2_holder structures
1328 *
1329 */
1330
1331void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1332{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001333 while (num_gh--)
1334 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335}
1336
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001337void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001338{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001339 unsigned long delay = 0;
1340 unsigned long holdtime;
1341 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001342
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001343 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001344 holdtime = gl->gl_tchange + gl->gl_hold_time;
1345 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1346 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001347 if (time_before(now, holdtime))
1348 delay = holdtime - now;
1349 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001350 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001351 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001353 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001354 handle_callback(gl, state, delay, true);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001355 __gfs2_glock_queue_work(gl, delay);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001356 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357}
1358
1359/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001360 * gfs2_should_freeze - Figure out if glock should be frozen
1361 * @gl: The glock in question
1362 *
1363 * Glocks are not frozen if (a) the result of the dlm operation is
1364 * an error, (b) the locking operation was an unlock operation or
1365 * (c) if there is a "noexp" flagged request anywhere in the queue
1366 *
1367 * Returns: 1 if freezing should occur, 0 otherwise
1368 */
1369
1370static int gfs2_should_freeze(const struct gfs2_glock *gl)
1371{
1372 const struct gfs2_holder *gh;
1373
1374 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1375 return 0;
1376 if (gl->gl_target == LM_ST_UNLOCKED)
1377 return 0;
1378
1379 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1380 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1381 continue;
1382 if (LM_FLAG_NOEXP & gh->gh_flags)
1383 return 0;
1384 }
1385
1386 return 1;
1387}
1388
1389/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001390 * gfs2_glock_complete - Callback used by locking
1391 * @gl: Pointer to the glock
1392 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001393 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001394 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
Steven Whitehouse47a25382010-11-30 15:49:31 +00001395 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396 */
1397
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001398void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001399{
Bob Peterson15562c42015-03-16 11:52:05 -05001400 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001401
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001402 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001403 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001404
David Teiglande0c2a9a2012-01-09 17:18:05 -05001405 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001406 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001407 set_bit(GLF_FROZEN, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001408 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001409 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001410 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001411 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001412
Steven Whitehousee66cf162013-10-15 15:18:08 +01001413 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001414 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001415 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001416 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001417}
1418
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001419static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1420{
1421 struct gfs2_glock *gla, *glb;
1422
1423 gla = list_entry(a, struct gfs2_glock, gl_lru);
1424 glb = list_entry(b, struct gfs2_glock, gl_lru);
1425
1426 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1427 return 1;
1428 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1429 return -1;
1430
1431 return 0;
1432}
1433
1434/**
1435 * gfs2_dispose_glock_lru - Demote a list of glocks
1436 * @list: The list to dispose of
1437 *
1438 * Disposing of glocks may involve disk accesses, so that here we sort
1439 * the glocks by number (i.e. disk location of the inodes) so that if
1440 * there are any such accesses, they'll be sent in order (mostly).
1441 *
1442 * Must be called under the lru_lock, but may drop and retake this
1443 * lock. While the lru_lock is dropped, entries may vanish from the
1444 * list, but no new entries will appear on the list (since it is
1445 * private)
1446 */
1447
1448static void gfs2_dispose_glock_lru(struct list_head *list)
1449__releases(&lru_lock)
1450__acquires(&lru_lock)
1451{
1452 struct gfs2_glock *gl;
1453
1454 list_sort(NULL, list, glock_cmp);
1455
1456 while(!list_empty(list)) {
1457 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1458 list_del_init(&gl->gl_lru);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001459 if (!spin_trylock(&gl->gl_lockref.lock)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001460add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001461 list_add(&gl->gl_lru, &lru_list);
Ross Lagerwall7881ef32019-03-27 17:09:17 +00001462 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001463 atomic_inc(&lru_count);
1464 continue;
1465 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001466 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001467 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001468 goto add_back_to_lru;
1469 }
Steven Whitehousee66cf162013-10-15 15:18:08 +01001470 gl->gl_lockref.count++;
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001471 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001472 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001473 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001474 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001475 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001476 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001477 }
1478}
1479
Steven Whitehouse2a005852012-12-14 12:28:30 +00001480/**
1481 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1482 * @nr: The number of entries to scan
1483 *
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001484 * This function selects the entries on the LRU which are able to
1485 * be demoted, and then kicks off the process by calling
1486 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001487 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001488
Dave Chinner1ab6c492013-08-28 10:18:09 +10001489static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490{
1491 struct gfs2_glock *gl;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001492 LIST_HEAD(skipped);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001493 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001494 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001496 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001497 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001498 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001499
1500 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001501 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001502 list_move(&gl->gl_lru, &dispose);
1503 atomic_dec(&lru_count);
Ross Lagerwall7881ef32019-03-27 17:09:17 +00001504 clear_bit(GLF_LRU, &gl->gl_flags);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001505 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001506 continue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001507 }
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001508
1509 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001510 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001511 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001512 if (!list_empty(&dispose))
1513 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001514 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001515
1516 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001517}
1518
Dave Chinner1ab6c492013-08-28 10:18:09 +10001519static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1520 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001521{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001522 if (!(sc->gfp_mask & __GFP_FS))
1523 return SHRINK_STOP;
1524 return gfs2_scan_glock_lru(sc->nr_to_scan);
1525}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001526
Dave Chinner1ab6c492013-08-28 10:18:09 +10001527static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1528 struct shrink_control *sc)
1529{
Glauber Costa55f841c2013-08-28 10:17:53 +10001530 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531}
1532
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001533static struct shrinker glock_shrinker = {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001534 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001535 .count_objects = gfs2_glock_shrink_count,
1536 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001537};
1538
David Teiglandb3b94fa2006-01-16 16:50:04 +00001539/**
1540 * examine_bucket - Call a function for glock in a hash bucket
1541 * @examiner: the function
1542 * @sdp: the filesystem
1543 * @bucket: the bucket
1544 *
Herbert Xu98687f42017-02-11 19:26:45 +08001545 * Note that the function can be called multiple times on the same
1546 * object. So the user must ensure that the function can cope with
1547 * that.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548 */
1549
Bob Peterson88ffbf32015-03-16 11:02:46 -05001550static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001551{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001552 struct gfs2_glock *gl;
Herbert Xu98687f42017-02-11 19:26:45 +08001553 struct rhashtable_iter iter;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001554
Herbert Xu98687f42017-02-11 19:26:45 +08001555 rhashtable_walk_enter(&gl_hash_table, &iter);
1556
1557 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08001558 rhashtable_walk_start(&iter);
Herbert Xu98687f42017-02-11 19:26:45 +08001559
1560 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl))
Bob Peterson27c3b412017-08-18 09:15:13 -05001561 if (gl->gl_name.ln_sbd == sdp &&
Bob Peterson88ffbf32015-03-16 11:02:46 -05001562 lockref_get_not_dead(&gl->gl_lockref))
1563 examiner(gl);
Herbert Xu98687f42017-02-11 19:26:45 +08001564
1565 rhashtable_walk_stop(&iter);
1566 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1567
1568 rhashtable_walk_exit(&iter);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001569}
1570
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001571/**
1572 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1573 * @gl: The glock to thaw
1574 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001575 */
1576
1577static void thaw_glock(struct gfs2_glock *gl)
1578{
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001579 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001580 gfs2_glock_put(gl);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001581 return;
Steven Whitehouse7286b312013-08-20 09:35:09 +01001582 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001583 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1584 gfs2_glock_queue_work(gl, 0);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001585}
1586
David Teiglandb3b94fa2006-01-16 16:50:04 +00001587/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001588 * clear_glock - look at a glock and see if we can free it from glock cache
1589 * @gl: the glock to look at
1590 *
1591 */
1592
1593static void clear_glock(struct gfs2_glock *gl)
1594{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001595 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001596
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001597 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousec741c452010-09-29 14:20:52 +01001598 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001599 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001600 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001601 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001602}
1603
1604/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001605 * gfs2_glock_thaw - Thaw any frozen glocks
1606 * @sdp: The super block
1607 *
1608 */
1609
1610void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1611{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001612 glock_hash_walk(thaw_glock, sdp);
1613}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001614
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001615static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001616{
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001617 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001618 gfs2_dump_glock(seq, gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001619 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001620}
1621
1622static void dump_glock_func(struct gfs2_glock *gl)
1623{
1624 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001625}
1626
1627/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001628 * gfs2_gl_hash_clear - Empty out the glock hash table
1629 * @sdp: the filesystem
1630 * @wait: wait until it's all gone
1631 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001632 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001633 */
1634
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001635void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001636{
David Teiglandfb6791d2012-11-13 10:58:56 -05001637 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001638 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001639 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001640 flush_workqueue(glock_workqueue);
Bob Peterson2aba1b52015-05-19 09:11:23 -05001641 wait_event_timeout(sdp->sd_glock_wait,
1642 atomic_read(&sdp->sd_glock_disposal) == 0,
1643 HZ * 600);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001644 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001645}
1646
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001647void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1648{
1649 struct gfs2_glock *gl = ip->i_gl;
1650 int ret;
1651
1652 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001653 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001654
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001655 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001656 clear_bit(GLF_LOCK, &gl->gl_flags);
1657 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001658 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001659}
1660
Steven Whitehouse6802e342008-05-21 17:03:22 +01001661static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001662{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001663 switch(state) {
1664 case LM_ST_UNLOCKED:
1665 return "UN";
1666 case LM_ST_SHARED:
1667 return "SH";
1668 case LM_ST_DEFERRED:
1669 return "DF";
1670 case LM_ST_EXCLUSIVE:
1671 return "EX";
1672 }
1673 return "??";
1674}
Robert Peterson04b933f2007-03-23 17:05:15 -05001675
Bob Petersonb58bf402015-07-24 09:45:43 -05001676static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
Steven Whitehouse6802e342008-05-21 17:03:22 +01001677{
1678 char *p = buf;
1679 if (flags & LM_FLAG_TRY)
1680 *p++ = 't';
1681 if (flags & LM_FLAG_TRY_1CB)
1682 *p++ = 'T';
1683 if (flags & LM_FLAG_NOEXP)
1684 *p++ = 'e';
1685 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001686 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001687 if (flags & LM_FLAG_PRIORITY)
1688 *p++ = 'p';
1689 if (flags & GL_ASYNC)
1690 *p++ = 'a';
1691 if (flags & GL_EXACT)
1692 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001693 if (flags & GL_NOCACHE)
1694 *p++ = 'c';
1695 if (test_bit(HIF_HOLDER, &iflags))
1696 *p++ = 'H';
1697 if (test_bit(HIF_WAIT, &iflags))
1698 *p++ = 'W';
1699 if (test_bit(HIF_FIRST, &iflags))
1700 *p++ = 'F';
1701 *p = 0;
1702 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001703}
1704
David Teiglandb3b94fa2006-01-16 16:50:04 +00001705/**
1706 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001707 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001708 * @gh: the glock holder
1709 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001710 */
1711
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001712static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001713{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001714 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001715 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001716
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001717 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001718 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001719 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001720 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1721 state2str(gh->gh_state),
1722 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1723 gh->gh_error,
1724 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1725 gh_owner ? gh_owner->comm : "(ended)",
1726 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001727 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728}
1729
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001730static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001732 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001733 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001734
Steven Whitehouse6802e342008-05-21 17:03:22 +01001735 if (test_bit(GLF_LOCK, gflags))
1736 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001737 if (test_bit(GLF_DEMOTE, gflags))
1738 *p++ = 'D';
1739 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1740 *p++ = 'd';
1741 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1742 *p++ = 'p';
1743 if (test_bit(GLF_DIRTY, gflags))
1744 *p++ = 'y';
1745 if (test_bit(GLF_LFLUSH, gflags))
1746 *p++ = 'f';
1747 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1748 *p++ = 'i';
1749 if (test_bit(GLF_REPLY_PENDING, gflags))
1750 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001751 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001752 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001753 if (test_bit(GLF_FROZEN, gflags))
1754 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001755 if (test_bit(GLF_QUEUED, gflags))
1756 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001757 if (test_bit(GLF_LRU, gflags))
1758 *p++ = 'L';
1759 if (gl->gl_object)
1760 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001761 if (test_bit(GLF_BLOCKING, gflags))
1762 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001763 *p = 0;
1764 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001765}
1766
1767/**
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +01001768 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001769 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001770 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001771 *
1772 * The file format is as follows:
1773 * One line per object, capital letters are used to indicate objects
1774 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1775 * other objects are indented by a single space and follow the glock to
1776 * which they are related. Fields are indicated by lower case letters
1777 * followed by a colon and the field value, except for strings which are in
1778 * [] so that its possible to see if they are composed of spaces for
1779 * example. The field's are n = number (id of the object), f = flags,
1780 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001781 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782 */
1783
Bob Peterson27a2660f2018-04-18 12:05:01 -07001784void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001785{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001786 const struct gfs2_glock_operations *glops = gl->gl_ops;
1787 unsigned long long dtime;
1788 const struct gfs2_holder *gh;
1789 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001790
Steven Whitehouse6802e342008-05-21 17:03:22 +01001791 dtime = jiffies - gl->gl_demote_time;
1792 dtime *= 1000000/HZ; /* demote time in uSec */
1793 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1794 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001795 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 +01001796 state2str(gl->gl_state),
1797 gl->gl_name.ln_type,
1798 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001799 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001800 state2str(gl->gl_target),
1801 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001802 atomic_read(&gl->gl_ail_count),
Steven Whitehousef42ab082011-04-14 16:50:31 +01001803 atomic_read(&gl->gl_revokes),
Steven Whitehousee66cf162013-10-15 15:18:08 +01001804 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001805
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001806 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1807 dump_holder(seq, gh);
1808
Steven Whitehouse6802e342008-05-21 17:03:22 +01001809 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001810 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001811}
1812
Steven Whitehousea2457692012-01-20 10:38:36 +00001813static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1814{
1815 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001816
Ben Hutchings4d207132015-08-27 12:51:45 -05001817 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 +00001818 gl->gl_name.ln_type,
1819 (unsigned long long)gl->gl_name.ln_number,
Ben Hutchings4d207132015-08-27 12:51:45 -05001820 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1821 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1822 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1823 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1824 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1825 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1826 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1827 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001828 return 0;
1829}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830
Steven Whitehousea2457692012-01-20 10:38:36 +00001831static const char *gfs2_gltype[] = {
1832 "type",
1833 "reserved",
1834 "nondisk",
1835 "inode",
1836 "rgrp",
1837 "meta",
1838 "iopen",
1839 "flock",
1840 "plock",
1841 "quota",
1842 "journal",
1843};
1844
1845static const char *gfs2_stype[] = {
1846 [GFS2_LKS_SRTT] = "srtt",
1847 [GFS2_LKS_SRTTVAR] = "srttvar",
1848 [GFS2_LKS_SRTTB] = "srttb",
1849 [GFS2_LKS_SRTTVARB] = "srttvarb",
1850 [GFS2_LKS_SIRT] = "sirt",
1851 [GFS2_LKS_SIRTVAR] = "sirtvar",
1852 [GFS2_LKS_DCOUNT] = "dlm",
1853 [GFS2_LKS_QCOUNT] = "queue",
1854};
1855
1856#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1857
1858static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1859{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001860 struct gfs2_sbd *sdp = seq->private;
1861 loff_t pos = *(loff_t *)iter_ptr;
1862 unsigned index = pos >> 3;
1863 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001864 int i;
1865
1866 if (index == 0 && subindex != 0)
1867 return 0;
1868
1869 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1870 (index == 0) ? "cpu": gfs2_stype[subindex]);
1871
1872 for_each_possible_cpu(i) {
1873 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
Andreas Gruenbacher8f7e0a82015-08-27 13:02:54 -05001874
1875 if (index == 0)
1876 seq_printf(seq, " %15u", i);
1877 else
1878 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1879 lkstats[index - 1].stats[subindex]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001880 }
1881 seq_putc(seq, '\n');
1882 return 0;
1883}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001884
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001885int __init gfs2_glock_init(void)
1886{
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001887 int i, ret;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001888
1889 ret = rhashtable_init(&gl_hash_table, &ht_parms);
1890 if (ret < 0)
1891 return ret;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001892
Steven Whitehoused2115772010-11-03 19:58:53 +00001893 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001894 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001895 if (!glock_workqueue) {
1896 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001897 return -ENOMEM;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001898 }
Steven Whitehoused2115772010-11-03 19:58:53 +00001899 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001900 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001901 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001902 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001903 destroy_workqueue(glock_workqueue);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001904 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001905 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001906 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001907
Chao Yue0d735c2016-09-21 12:09:40 -05001908 ret = register_shrinker(&glock_shrinker);
1909 if (ret) {
1910 destroy_workqueue(gfs2_delete_workqueue);
1911 destroy_workqueue(glock_workqueue);
1912 rhashtable_destroy(&gl_hash_table);
1913 return ret;
1914 }
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001915
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001916 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
1917 init_waitqueue_head(glock_wait_table + i);
1918
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001919 return 0;
1920}
1921
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001922void gfs2_glock_exit(void)
1923{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001924 unregister_shrinker(&glock_shrinker);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001925 rhashtable_destroy(&gl_hash_table);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001926 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001927 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001928}
1929
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001930static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001931{
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001932 struct gfs2_glock *gl = gi->gl;
1933
1934 if (gl) {
1935 if (n == 0)
1936 return;
1937 if (!lockref_put_not_zero(&gl->gl_lockref))
1938 gfs2_glock_queue_put(gl);
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001939 }
1940 for (;;) {
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001941 gl = rhashtable_walk_next(&gi->hti);
1942 if (IS_ERR_OR_NULL(gl)) {
1943 if (gl == ERR_PTR(-EAGAIN)) {
1944 n = 1;
1945 continue;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001946 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001947 gl = NULL;
1948 break;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001949 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001950 if (gl->gl_name.ln_sbd != gi->sdp)
1951 continue;
1952 if (n <= 1) {
1953 if (!lockref_get_not_dead(&gl->gl_lockref))
1954 continue;
1955 break;
1956 } else {
1957 if (__lockref_is_dead(&gl->gl_lockref))
1958 continue;
1959 n--;
1960 }
Dan Carpenter14d37562016-12-14 08:02:03 -06001961 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001962 gi->gl = gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001963}
1964
Steven Whitehouse6802e342008-05-21 17:03:22 +01001965static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Bob Peterson27c3b412017-08-18 09:15:13 -05001966 __acquires(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00001967{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001968 struct gfs2_glock_iter *gi = seq->private;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001969 loff_t n;
Robert Peterson7c52b162007-03-16 10:26:37 +00001970
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001971 /*
1972 * We can either stay where we are, skip to the next hash table
1973 * entry, or start from the beginning.
1974 */
1975 if (*pos < gi->last_pos) {
1976 rhashtable_walk_exit(&gi->hti);
1977 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
1978 n = *pos + 1;
1979 } else {
1980 n = *pos - gi->last_pos;
1981 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001982
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001983 rhashtable_walk_start(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00001984
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001985 gfs2_glock_iter_next(gi, n);
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001986 gi->last_pos = *pos;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001987 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001988}
1989
Steven Whitehouse6802e342008-05-21 17:03:22 +01001990static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001991 loff_t *pos)
1992{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001993 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001994
1995 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001996 gi->last_pos = *pos;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001997 gfs2_glock_iter_next(gi, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001998 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001999}
2000
Steven Whitehouse6802e342008-05-21 17:03:22 +01002001static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Bob Peterson27c3b412017-08-18 09:15:13 -05002002 __releases(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00002003{
Steven Whitehouse6802e342008-05-21 17:03:22 +01002004 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00002005
Bob Peterson88ffbf32015-03-16 11:02:46 -05002006 rhashtable_walk_stop(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00002007}
2008
Steven Whitehouse6802e342008-05-21 17:03:22 +01002009static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00002010{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00002011 dump_glock(seq, iter_ptr);
2012 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00002013}
2014
Steven Whitehousea2457692012-01-20 10:38:36 +00002015static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2016{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002017 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00002018 if (*pos >= GFS2_NR_SBSTATS)
2019 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002020 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002021}
2022
2023static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2024 loff_t *pos)
2025{
Steven Whitehousea2457692012-01-20 10:38:36 +00002026 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002027 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00002028 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002029 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002030}
2031
2032static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2033{
2034 preempt_enable();
2035}
2036
Denis Cheng4ef29002007-07-31 18:31:11 +08002037static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002038 .start = gfs2_glock_seq_start,
2039 .next = gfs2_glock_seq_next,
2040 .stop = gfs2_glock_seq_stop,
2041 .show = gfs2_glock_seq_show,
2042};
2043
Steven Whitehousea2457692012-01-20 10:38:36 +00002044static const struct seq_operations gfs2_glstats_seq_ops = {
2045 .start = gfs2_glock_seq_start,
2046 .next = gfs2_glock_seq_next,
2047 .stop = gfs2_glock_seq_stop,
2048 .show = gfs2_glstats_seq_show,
2049};
2050
2051static const struct seq_operations gfs2_sbstats_seq_ops = {
2052 .start = gfs2_sbstats_seq_start,
2053 .next = gfs2_sbstats_seq_next,
2054 .stop = gfs2_sbstats_seq_stop,
2055 .show = gfs2_sbstats_seq_show,
2056};
2057
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002058#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2059
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002060static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2061 const struct seq_operations *ops)
Robert Peterson7c52b162007-03-16 10:26:37 +00002062{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002063 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
Steven Whitehouse6802e342008-05-21 17:03:22 +01002064 if (ret == 0) {
2065 struct seq_file *seq = file->private_data;
2066 struct gfs2_glock_iter *gi = seq->private;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002067
Steven Whitehouse6802e342008-05-21 17:03:22 +01002068 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002069 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01002070 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002071 seq->size = GFS2_SEQ_GOODSIZE;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002072 /*
2073 * Initially, we are "before" the first hash table entry; the
2074 * first call to rhashtable_walk_next gets us the first entry.
2075 */
2076 gi->last_pos = -1;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002077 gi->gl = NULL;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002078 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
Steven Whitehouse6802e342008-05-21 17:03:22 +01002079 }
2080 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00002081}
2082
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002083static int gfs2_glocks_open(struct inode *inode, struct file *file)
2084{
2085 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2086}
2087
Bob Peterson88ffbf32015-03-16 11:02:46 -05002088static int gfs2_glocks_release(struct inode *inode, struct file *file)
2089{
2090 struct seq_file *seq = file->private_data;
2091 struct gfs2_glock_iter *gi = seq->private;
2092
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02002093 if (gi->gl)
2094 gfs2_glock_put(gi->gl);
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002095 rhashtable_walk_exit(&gi->hti);
Bob Peterson88ffbf32015-03-16 11:02:46 -05002096 return seq_release_private(inode, file);
2097}
2098
Steven Whitehousea2457692012-01-20 10:38:36 +00002099static int gfs2_glstats_open(struct inode *inode, struct file *file)
2100{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002101 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002102}
2103
2104static int gfs2_sbstats_open(struct inode *inode, struct file *file)
2105{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002106 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002107 if (ret == 0) {
2108 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002109 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00002110 }
2111 return ret;
2112}
2113
2114static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002115 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00002116 .open = gfs2_glocks_open,
2117 .read = seq_read,
2118 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002119 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002120};
2121
2122static const struct file_operations gfs2_glstats_fops = {
2123 .owner = THIS_MODULE,
2124 .open = gfs2_glstats_open,
2125 .read = seq_read,
2126 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002127 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002128};
2129
2130static const struct file_operations gfs2_sbstats_fops = {
2131 .owner = THIS_MODULE,
2132 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00002133 .read = seq_read,
2134 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002135 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00002136};
2137
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002138void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
Robert Peterson7c52b162007-03-16 10:26:37 +00002139{
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002140 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
Steven Whitehousea2457692012-01-20 10:38:36 +00002141
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002142 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2143 &gfs2_glocks_fops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002144
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002145 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2146 &gfs2_glstats_fops);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002147
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002148 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2149 &gfs2_sbstats_fops);
Robert Peterson7c52b162007-03-16 10:26:37 +00002150}
2151
2152void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2153{
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002154 debugfs_remove_recursive(sdp->debugfs_dir);
2155 sdp->debugfs_dir = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002156}
2157
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002158void gfs2_register_debugfs(void)
Robert Peterson7c52b162007-03-16 10:26:37 +00002159{
2160 gfs2_root = debugfs_create_dir("gfs2", NULL);
Robert Peterson7c52b162007-03-16 10:26:37 +00002161}
2162
2163void gfs2_unregister_debugfs(void)
2164{
2165 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002166 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002167}