blob: 15c605cfcfc8521510aa3acb44b865ea3199ea0a [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
Bob Peterson73118ca2019-04-05 04:41:38 +0100143 BUG_ON(test_bit(GLF_REVOKES, &gl->gl_flags));
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500144 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
145 smp_mb();
146 wake_up_glock(gl);
Andreas Gruenbacher961ae1d2017-07-07 13:22:05 -0500147 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000148 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
149 wake_up(&sdp->sd_glock_wait);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150}
151
152/**
153 * gfs2_glock_hold() - increment reference count on glock
154 * @gl: The glock to hold
155 *
156 */
157
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -0500158void gfs2_glock_hold(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159{
Steven Whitehousee66cf162013-10-15 15:18:08 +0100160 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
161 lockref_get(&gl->gl_lockref);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162}
163
164/**
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500165 * demote_ok - Check to see if it's ok to unlock a glock
166 * @gl: the glock
167 *
168 * Returns: 1 if it's ok
169 */
170
171static int demote_ok(const struct gfs2_glock *gl)
172{
173 const struct gfs2_glock_operations *glops = gl->gl_ops;
174
175 if (gl->gl_state == LM_ST_UNLOCKED)
176 return 0;
Steven Whitehousef42ab082011-04-14 16:50:31 +0100177 if (!list_empty(&gl->gl_holders))
Benjamin Marzinski8ff22a62009-07-10 18:04:24 -0500178 return 0;
179 if (glops->go_demote_ok)
180 return glops->go_demote_ok(gl);
181 return 1;
182}
183
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000184
Steven Whitehouse29687a22011-03-30 16:33:25 +0100185void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
186{
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000187 if (!(gl->gl_ops->go_flags & GLOF_LRU))
188 return;
189
Steven Whitehouse29687a22011-03-30 16:33:25 +0100190 spin_lock(&lru_lock);
191
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000192 list_del(&gl->gl_lru);
Steven Whitehouse29687a22011-03-30 16:33:25 +0100193 list_add_tail(&gl->gl_lru, &lru_list);
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000194
195 if (!test_bit(GLF_LRU, &gl->gl_flags)) {
196 set_bit(GLF_LRU, &gl->gl_flags);
197 atomic_inc(&lru_count);
198 }
199
Steven Whitehouse29687a22011-03-30 16:33:25 +0100200 spin_unlock(&lru_lock);
201}
202
Bob Peterson8f6cb402015-01-05 13:25:10 -0500203static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
Steven Whitehousef42ab082011-04-14 16:50:31 +0100204{
Bob Peterson645ebd42017-07-26 10:57:35 -0500205 if (!(gl->gl_ops->go_flags & GLOF_LRU))
206 return;
207
Bob Peterson8f6cb402015-01-05 13:25:10 -0500208 spin_lock(&lru_lock);
Ross Lagerwall7881ef32019-03-27 17:09:17 +0000209 if (test_bit(GLF_LRU, &gl->gl_flags)) {
Steven Whitehousef42ab082011-04-14 16:50:31 +0100210 list_del_init(&gl->gl_lru);
211 atomic_dec(&lru_count);
212 clear_bit(GLF_LRU, &gl->gl_flags);
213 }
214 spin_unlock(&lru_lock);
215}
216
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500217/*
218 * Enqueue the glock on the work queue. Passes one glock reference on to the
219 * work queue.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 */
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500221static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
222 if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) {
223 /*
224 * We are holding the lockref spinlock, and the work was still
225 * queued above. The queued work (glock_work_func) takes that
226 * spinlock before dropping its glock reference(s), so it
227 * cannot have dropped them in the meantime.
228 */
229 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
230 gl->gl_lockref.count--;
231 }
232}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500234static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
235 spin_lock(&gl->gl_lockref.lock);
236 __gfs2_glock_queue_work(gl, delay);
237 spin_unlock(&gl->gl_lockref.lock);
238}
239
240static void __gfs2_glock_put(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241{
Bob Peterson15562c42015-03-16 11:52:05 -0500242 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000243 struct address_space *mapping = gfs2_glock2aspace(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244
Steven Whitehousee66cf162013-10-15 15:18:08 +0100245 lockref_mark_dead(&gl->gl_lockref);
246
Bob Peterson8f6cb402015-01-05 13:25:10 -0500247 gfs2_glock_remove_from_lru(gl);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100248 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousee66cf162013-10-15 15:18:08 +0100249 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
250 GLOCK_BUG_ON(gl, mapping && mapping->nrpages);
251 trace_gfs2_glock_put(gl);
252 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253}
254
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -0500255/*
256 * Cause the glock to be put in work queue context.
257 */
258void gfs2_glock_queue_put(struct gfs2_glock *gl)
259{
260 gfs2_glock_queue_work(gl, 0);
261}
262
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263/**
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500264 * gfs2_glock_put() - Decrement reference count on glock
265 * @gl: The glock to put
266 *
267 */
268
269void gfs2_glock_put(struct gfs2_glock *gl)
270{
271 if (lockref_put_or_lock(&gl->gl_lockref))
272 return;
273
274 __gfs2_glock_put(gl);
275}
276
277/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100278 * may_grant - check if its ok to grant a new lock
279 * @gl: The glock
280 * @gh: The lock request which we wish to grant
281 *
282 * Returns: true if its ok to grant the lock
283 */
284
285static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh)
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500286{
Steven Whitehouse6802e342008-05-21 17:03:22 +0100287 const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list);
288 if ((gh->gh_state == LM_ST_EXCLUSIVE ||
289 gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head)
290 return 0;
291 if (gl->gl_state == gh->gh_state)
292 return 1;
293 if (gh->gh_flags & GL_EXACT)
294 return 0;
Steven Whitehouse209806a2008-07-07 10:07:28 +0100295 if (gl->gl_state == LM_ST_EXCLUSIVE) {
296 if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
297 return 1;
298 if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
299 return 1;
300 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100301 if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
302 return 1;
303 return 0;
304}
305
306static void gfs2_holder_wake(struct gfs2_holder *gh)
307{
308 clear_bit(HIF_WAIT, &gh->gh_iflags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100309 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100310 wake_up_bit(&gh->gh_iflags, HIF_WAIT);
311}
312
313/**
Steven Whitehoused5341a92010-07-23 14:05:51 +0100314 * do_error - Something unexpected has happened during a lock request
315 *
316 */
317
Denys Vlasenkoa527b382016-04-12 12:39:12 -0400318static void do_error(struct gfs2_glock *gl, const int ret)
Steven Whitehoused5341a92010-07-23 14:05:51 +0100319{
320 struct gfs2_holder *gh, *tmp;
321
322 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
323 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
324 continue;
325 if (ret & LM_OUT_ERROR)
326 gh->gh_error = -EIO;
327 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
328 gh->gh_error = GLR_TRYFAILED;
329 else
330 continue;
331 list_del_init(&gh->gh_list);
332 trace_gfs2_glock_queue(gh, 0);
333 gfs2_holder_wake(gh);
334 }
335}
336
337/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100338 * do_promote - promote as many requests as possible on the current queue
339 * @gl: The glock
340 *
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000341 * Returns: 1 if there is a blocked holder at the head of the list, or 2
342 * if a type specific operation is underway.
Steven Whitehouse6802e342008-05-21 17:03:22 +0100343 */
344
345static int do_promote(struct gfs2_glock *gl)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500346__releases(&gl->gl_lockref.lock)
347__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100348{
349 const struct gfs2_glock_operations *glops = gl->gl_ops;
350 struct gfs2_holder *gh, *tmp;
351 int ret;
352
353restart:
354 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
355 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
356 continue;
357 if (may_grant(gl, gh)) {
358 if (gh->gh_list.prev == &gl->gl_holders &&
359 glops->go_lock) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500360 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100361 /* FIXME: eliminate this eventually */
362 ret = glops->go_lock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500363 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100364 if (ret) {
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000365 if (ret == 1)
366 return 2;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100367 gh->gh_error = ret;
368 list_del_init(&gh->gh_list);
Steven Whitehouse63997772009-06-12 08:49:20 +0100369 trace_gfs2_glock_queue(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100370 gfs2_holder_wake(gh);
371 goto restart;
372 }
373 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100374 trace_gfs2_promote(gh, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100375 gfs2_holder_wake(gh);
376 goto restart;
377 }
378 set_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse63997772009-06-12 08:49:20 +0100379 trace_gfs2_promote(gh, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100380 gfs2_holder_wake(gh);
381 continue;
382 }
383 if (gh->gh_list.prev == &gl->gl_holders)
384 return 1;
Steven Whitehoused5341a92010-07-23 14:05:51 +0100385 do_error(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100386 break;
387 }
388 return 0;
389}
390
391/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100392 * find_first_waiter - find the first gh that's waiting for the glock
393 * @gl: the glock
394 */
395
396static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
397{
398 struct gfs2_holder *gh;
399
400 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
401 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
402 return gh;
403 }
404 return NULL;
405}
406
407/**
408 * state_change - record that the glock is now in a different state
409 * @gl: the glock
410 * @new_state the new state
411 *
412 */
413
414static void state_change(struct gfs2_glock *gl, unsigned int new_state)
415{
416 int held1, held2;
417
418 held1 = (gl->gl_state != LM_ST_UNLOCKED);
419 held2 = (new_state != LM_ST_UNLOCKED);
420
421 if (held1 != held2) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100422 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
Steven Whitehouse6802e342008-05-21 17:03:22 +0100423 if (held2)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100424 gl->gl_lockref.count++;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100425 else
Steven Whitehousee66cf162013-10-15 15:18:08 +0100426 gl->gl_lockref.count--;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100427 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +0100428 if (held1 && held2 && list_empty(&gl->gl_holders))
429 clear_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100430
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400431 if (new_state != gl->gl_target)
432 /* shorten our minimum hold time */
433 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
434 GL_GLOCK_MIN_HOLD);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100435 gl->gl_state = new_state;
436 gl->gl_tchange = jiffies;
437}
438
439static void gfs2_demote_wake(struct gfs2_glock *gl)
440{
441 gl->gl_demote_state = LM_ST_EXCLUSIVE;
442 clear_bit(GLF_DEMOTE, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100443 smp_mb__after_atomic();
Steven Whitehouse6802e342008-05-21 17:03:22 +0100444 wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
445}
446
447/**
448 * finish_xmote - The DLM has replied to one of our lock requests
449 * @gl: The glock
450 * @ret: The status from the DLM
451 *
452 */
453
454static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
455{
456 const struct gfs2_glock_operations *glops = gl->gl_ops;
457 struct gfs2_holder *gh;
458 unsigned state = ret & LM_OUT_ST_MASK;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000459 int rv;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500460
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500461 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse63997772009-06-12 08:49:20 +0100462 trace_gfs2_glock_state_change(gl, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100463 state_change(gl, state);
464 gh = find_first_waiter(gl);
465
466 /* Demote to UN request arrived during demote to SH or DF */
467 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
468 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
469 gl->gl_target = LM_ST_UNLOCKED;
470
471 /* Check for state != intended state */
472 if (unlikely(state != gl->gl_target)) {
473 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
474 /* move to back of queue and try next entry */
475 if (ret & LM_OUT_CANCELED) {
476 if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0)
477 list_move_tail(&gh->gh_list, &gl->gl_holders);
478 gh = find_first_waiter(gl);
479 gl->gl_target = gh->gh_state;
480 goto retry;
481 }
482 /* Some error or failed "try lock" - report it */
483 if ((ret & LM_OUT_ERROR) ||
484 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
485 gl->gl_target = gl->gl_state;
486 do_error(gl, ret);
487 goto out;
488 }
489 }
490 switch(state) {
491 /* Unlocked due to conversion deadlock, try again */
492 case LM_ST_UNLOCKED:
493retry:
494 do_xmote(gl, gh, gl->gl_target);
495 break;
496 /* Conversion fails, unlock and try again */
497 case LM_ST_SHARED:
498 case LM_ST_DEFERRED:
499 do_xmote(gl, gh, LM_ST_UNLOCKED);
500 break;
501 default: /* Everything else */
Bob Petersone54c78a2018-10-03 08:47:36 -0500502 fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
503 gl->gl_target, state);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100504 GLOCK_BUG_ON(gl, 1);
505 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500506 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100507 return;
508 }
509
510 /* Fast path - we got what we asked for */
511 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
512 gfs2_demote_wake(gl);
513 if (state != LM_ST_UNLOCKED) {
514 if (glops->go_xmote_bh) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500515 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100516 rv = glops->go_xmote_bh(gl, gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500517 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100518 if (rv) {
519 do_error(gl, rv);
520 goto out;
521 }
522 }
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000523 rv = do_promote(gl);
524 if (rv == 2)
525 goto out_locked;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100526 }
527out:
528 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000529out_locked:
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500530 spin_unlock(&gl->gl_lockref.lock);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500531}
532
Steven Whitehouse6802e342008-05-21 17:03:22 +0100533/**
534 * do_xmote - Calls the DLM to change the state of a lock
535 * @gl: The lock state
536 * @gh: The holder (only for promotes)
537 * @target: The target lock state
538 *
539 */
540
541static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500542__releases(&gl->gl_lockref.lock)
543__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100544{
545 const struct gfs2_glock_operations *glops = gl->gl_ops;
Bob Peterson15562c42015-03-16 11:52:05 -0500546 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Bob Petersonb58bf402015-07-24 09:45:43 -0500547 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100548 int ret;
549
Bob Petersoned175452017-05-05 09:43:02 -0500550 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) &&
551 target != LM_ST_UNLOCKED)
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500552 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100553 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP |
554 LM_FLAG_PRIORITY);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000555 GLOCK_BUG_ON(gl, gl->gl_state == target);
556 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100557 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
558 glops->go_inval) {
559 set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
560 do_error(gl, 0); /* Fail queued try locks */
561 }
Steven Whitehouse47a25382010-11-30 15:49:31 +0000562 gl->gl_req = target;
Steven Whitehousea2457692012-01-20 10:38:36 +0000563 set_bit(GLF_BLOCKING, &gl->gl_flags);
564 if ((gl->gl_req == LM_ST_UNLOCKED) ||
565 (gl->gl_state == LM_ST_EXCLUSIVE) ||
566 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
567 clear_bit(GLF_BLOCKING, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500568 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson06dfc302012-10-24 14:41:05 -0400569 if (glops->go_sync)
570 glops->go_sync(gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100571 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
572 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
573 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
574
575 gfs2_glock_hold(gl);
Steven Whitehouse921169c2010-11-29 12:50:38 +0000576 if (sdp->sd_lockstruct.ls_ops->lm_lock) {
577 /* lock_dlm */
578 ret = sdp->sd_lockstruct.ls_ops->lm_lock(gl, target, lck_flags);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400579 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
580 target == LM_ST_UNLOCKED &&
581 test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags)) {
582 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500583 gfs2_glock_queue_work(gl, 0);
Benjamin Marzinski3e11e532016-03-23 14:29:59 -0400584 }
585 else if (ret) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500586 fs_err(sdp, "lm_lock ret %d\n", ret);
Bob Petersoned175452017-05-05 09:43:02 -0500587 GLOCK_BUG_ON(gl, !test_bit(SDF_SHUTDOWN,
588 &sdp->sd_flags));
David Teiglanddba2d702012-11-14 13:46:53 -0500589 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000590 } else { /* lock_nolock */
591 finish_xmote(gl, target);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500592 gfs2_glock_queue_work(gl, 0);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100593 }
Steven Whitehouse921169c2010-11-29 12:50:38 +0000594
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500595 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100596}
597
598/**
599 * find_first_holder - find the first "holder" gh
600 * @gl: the glock
601 */
602
603static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
604{
605 struct gfs2_holder *gh;
606
607 if (!list_empty(&gl->gl_holders)) {
608 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
609 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
610 return gh;
611 }
612 return NULL;
613}
614
615/**
616 * run_queue - do all outstanding tasks related to a glock
617 * @gl: The glock in question
618 * @nonblock: True if we must not block in run_queue
619 *
620 */
621
622static void run_queue(struct gfs2_glock *gl, const int nonblock)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500623__releases(&gl->gl_lockref.lock)
624__acquires(&gl->gl_lockref.lock)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100625{
626 struct gfs2_holder *gh = NULL;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000627 int ret;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100628
629 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
630 return;
631
632 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
633
634 if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
635 gl->gl_demote_state != gl->gl_state) {
636 if (find_first_holder(gl))
Steven Whitehoused8348de2009-02-05 10:12:38 +0000637 goto out_unlock;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100638 if (nonblock)
639 goto out_sched;
640 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100641 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100642 gl->gl_target = gl->gl_demote_state;
643 } else {
644 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
645 gfs2_demote_wake(gl);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000646 ret = do_promote(gl);
647 if (ret == 0)
Steven Whitehoused8348de2009-02-05 10:12:38 +0000648 goto out_unlock;
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000649 if (ret == 2)
Steven Whitehousea228df62009-04-07 14:01:34 +0100650 goto out;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100651 gh = find_first_waiter(gl);
652 gl->gl_target = gh->gh_state;
653 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
654 do_error(gl, 0); /* Fail queued try locks */
655 }
656 do_xmote(gl, gh, gl->gl_target);
Steven Whitehousea228df62009-04-07 14:01:34 +0100657out:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100658 return;
659
660out_sched:
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100661 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100662 smp_mb__after_atomic();
Steven Whitehousee66cf162013-10-15 15:18:08 +0100663 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500664 __gfs2_glock_queue_work(gl, 0);
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100665 return;
666
Steven Whitehoused8348de2009-02-05 10:12:38 +0000667out_unlock:
Steven Whitehouse6802e342008-05-21 17:03:22 +0100668 clear_bit(GLF_LOCK, &gl->gl_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100669 smp_mb__after_atomic();
Steven Whitehouse7e71c552009-09-22 10:56:16 +0100670 return;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100671}
672
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500673static void delete_work_func(struct work_struct *work)
674{
675 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
Bob Peterson15562c42015-03-16 11:52:05 -0500676 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500677 struct inode *inode;
Steven Whitehouse044b9412010-11-03 20:01:07 +0000678 u64 no_addr = gl->gl_name.ln_number;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500679
Bob Petersona4923862015-12-07 16:24:27 -0600680 /* If someone's using this glock to create a new dinode, the block must
681 have been freed by another node, then re-used, in which case our
682 iopen callback is too late after the fact. Ignore it. */
683 if (test_bit(GLF_INODE_CREATING, &gl->gl_flags))
684 goto out;
685
Andreas Gruenbacherec5ec662016-06-14 12:23:59 -0500686 inode = gfs2_lookup_by_inum(sdp, no_addr, NULL, GFS2_BLKST_UNLINKED);
Steven Whitehouse044b9412010-11-03 20:01:07 +0000687 if (inode && !IS_ERR(inode)) {
688 d_prune_aliases(inode);
689 iput(inode);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500690 }
Bob Petersona4923862015-12-07 16:24:27 -0600691out:
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500692 gfs2_glock_put(gl);
693}
694
Steven Whitehouse6802e342008-05-21 17:03:22 +0100695static void glock_work_func(struct work_struct *work)
696{
697 unsigned long delay = 0;
698 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500699 unsigned int drop_refs = 1;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100700
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000701 if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100702 finish_xmote(gl, gl->gl_reply);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500703 drop_refs++;
Steven Whitehouse26bb7502009-11-27 10:31:11 +0000704 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500705 spin_lock(&gl->gl_lockref.lock);
Bob Petersonf90e5b52011-05-24 10:44:42 -0400706 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
Steven Whitehouse265d529c2008-07-07 10:02:36 +0100707 gl->gl_state != LM_ST_UNLOCKED &&
708 gl->gl_demote_state != LM_ST_EXCLUSIVE) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100709 unsigned long holdtime, now = jiffies;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400710
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400711 holdtime = gl->gl_tchange + gl->gl_hold_time;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100712 if (time_before(now, holdtime))
713 delay = holdtime - now;
Bob Petersonf90e5b52011-05-24 10:44:42 -0400714
715 if (!delay) {
716 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
717 set_bit(GLF_DEMOTE, &gl->gl_flags);
718 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100719 }
720 run_queue(gl, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500721 if (delay) {
722 /* Keep one glock reference for the work we requeue. */
723 drop_refs--;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400724 if (gl->gl_name.ln_type != LM_TYPE_INODE)
725 delay = 0;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500726 __gfs2_glock_queue_work(gl, delay);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400727 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -0500728
729 /*
730 * Drop the remaining glock references manually here. (Mind that
731 * __gfs2_glock_queue_work depends on the lockref spinlock begin held
732 * here as well.)
733 */
734 gl->gl_lockref.count -= drop_refs;
735 if (!gl->gl_lockref.count) {
736 __gfs2_glock_put(gl);
737 return;
738 }
739 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100740}
741
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500742static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
743 struct gfs2_glock *new)
744{
745 struct wait_glock_queue wait;
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500746 wait_queue_head_t *wq = glock_waitqueue(name);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500747 struct gfs2_glock *gl;
748
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500749 wait.name = name;
750 init_wait(&wait.wait);
751 wait.wait.func = glock_wake_function;
752
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500753again:
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500754 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500755 rcu_read_lock();
756 if (new) {
757 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
758 &new->gl_node, ht_parms);
759 if (IS_ERR(gl))
760 goto out;
761 } else {
762 gl = rhashtable_lookup_fast(&gl_hash_table,
763 name, ht_parms);
764 }
765 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
766 rcu_read_unlock();
767 schedule();
768 goto again;
769 }
770out:
771 rcu_read_unlock();
Andreas Gruenbachera91323e2017-08-04 07:40:45 -0500772 finish_wait(wq, &wait.wait);
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500773 return gl;
774}
775
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776/**
777 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
778 * @sdp: The GFS2 superblock
779 * @number: the lock number
780 * @glops: The glock_operations to use
781 * @create: If 0, don't create the glock if it doesn't exist
782 * @glp: the glock is returned here
783 *
784 * This does not lock a glock, just finds/creates structures for one.
785 *
786 * Returns: errno
787 */
788
Steven Whitehousecd915492006-09-04 12:49:07 -0400789int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400790 const struct gfs2_glock_operations *glops, int create,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 struct gfs2_glock **glp)
792{
Steven Whitehouse009d8512009-12-08 12:12:13 +0000793 struct super_block *s = sdp->sd_vfs;
Bob Peterson15562c42015-03-16 11:52:05 -0500794 struct lm_lockname name = { .ln_number = number,
795 .ln_type = glops->go_type,
796 .ln_sbd = sdp };
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100797 struct gfs2_glock *gl, *tmp;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000798 struct address_space *mapping;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000799 struct kmem_cache *cachep;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100800 int ret = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500802 gl = find_insert_glock(&name, NULL);
803 if (gl) {
804 *glp = gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 return 0;
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500806 }
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000807 if (!create)
808 return -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809
Steven Whitehouse009d8512009-12-08 12:12:13 +0000810 if (glops->go_flags & GLOF_ASPACE)
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000811 cachep = gfs2_glock_aspace_cachep;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000812 else
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000813 cachep = gfs2_glock_cachep;
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100814 gl = kmem_cache_alloc(cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 if (!gl)
816 return -ENOMEM;
817
David Teiglanddba2d702012-11-14 13:46:53 -0500818 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
David Teiglanddba2d702012-11-14 13:46:53 -0500819
820 if (glops->go_flags & GLOF_LVB) {
Steven Whitehousefe0bbd22014-06-23 14:50:20 +0100821 gl->gl_lksb.sb_lvbptr = kzalloc(GFS2_MIN_LVB_SIZE, GFP_NOFS);
David Teigland4e2f8842012-11-14 13:47:37 -0500822 if (!gl->gl_lksb.sb_lvbptr) {
David Teiglanddba2d702012-11-14 13:46:53 -0500823 kmem_cache_free(cachep, gl);
824 return -ENOMEM;
825 }
David Teiglanddba2d702012-11-14 13:46:53 -0500826 }
827
Steven Whitehouse8f052282010-01-29 15:21:27 +0000828 atomic_inc(&sdp->sd_glock_disposal);
Bob Peterson88ffbf32015-03-16 11:02:46 -0500829 gl->gl_node.next = NULL;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400830 gl->gl_flags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 gl->gl_name = name;
Steven Whitehousee66cf162013-10-15 15:18:08 +0100832 gl->gl_lockref.count = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833 gl->gl_state = LM_ST_UNLOCKED;
Steven Whitehouse6802e342008-05-21 17:03:22 +0100834 gl->gl_target = LM_ST_UNLOCKED;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500835 gl->gl_demote_state = LM_ST_EXCLUSIVE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 gl->gl_ops = glops;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100837 gl->gl_dstamp = 0;
Steven Whitehousea2457692012-01-20 10:38:36 +0000838 preempt_disable();
839 /* We use the global stats to estimate the initial per-glock stats */
840 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
841 preempt_enable();
842 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
843 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500844 gl->gl_tchange = jiffies;
Steven Whitehouseec45d9f2006-08-30 10:36:52 -0400845 gl->gl_object = NULL;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400846 gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500847 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500848 INIT_WORK(&gl->gl_delete, delete_work_func);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849
Steven Whitehouse009d8512009-12-08 12:12:13 +0000850 mapping = gfs2_glock2aspace(gl);
851 if (mapping) {
852 mapping->a_ops = &gfs2_meta_aops;
853 mapping->host = s->s_bdev->bd_inode;
854 mapping->flags = 0;
855 mapping_set_gfp_mask(mapping, GFP_NOFS);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800856 mapping->private_data = NULL;
Steven Whitehouse009d8512009-12-08 12:12:13 +0000857 mapping->writeback_index = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 }
859
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500860 tmp = find_insert_glock(&name, gl);
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100861 if (!tmp) {
Bob Peterson88ffbf32015-03-16 11:02:46 -0500862 *glp = gl;
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100863 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 }
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100865 if (IS_ERR(tmp)) {
866 ret = PTR_ERR(tmp);
867 goto out_free;
868 }
Andreas Gruenbacher05154802017-08-01 11:18:26 -0500869 *glp = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100871out_free:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500872 kfree(gl->gl_lksb.sb_lvbptr);
873 kmem_cache_free(cachep, gl);
874 atomic_dec(&sdp->sd_glock_disposal);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875
Andreas Gruenbacher0a52aba2017-02-21 23:19:10 +0100876out:
Bob Peterson88ffbf32015-03-16 11:02:46 -0500877 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878}
879
880/**
881 * gfs2_holder_init - initialize a struct gfs2_holder in the default way
882 * @gl: the glock
883 * @state: the state we're requesting
884 * @flags: the modifier flags
885 * @gh: the holder structure
886 *
887 */
888
Bob Petersonb58bf402015-07-24 09:45:43 -0500889void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890 struct gfs2_holder *gh)
891{
892 INIT_LIST_HEAD(&gh->gh_list);
893 gh->gh_gl = gl;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200894 gh->gh_ip = _RET_IP_;
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800895 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 gh->gh_state = state;
897 gh->gh_flags = flags;
898 gh->gh_error = 0;
899 gh->gh_iflags = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 gfs2_glock_hold(gl);
901}
902
903/**
904 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
905 * @state: the state we're requesting
906 * @flags: the modifier flags
907 * @gh: the holder structure
908 *
909 * Don't mess with the glock.
910 *
911 */
912
Bob Petersonb58bf402015-07-24 09:45:43 -0500913void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914{
915 gh->gh_state = state;
Steven Whitehouse579b78a2006-04-26 14:58:26 -0400916 gh->gh_flags = flags;
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000917 gh->gh_iflags = 0;
Fabian Frederickd29c0af2014-10-03 20:15:36 +0200918 gh->gh_ip = _RET_IP_;
Markus Elfring30badc92014-11-18 11:31:23 +0100919 put_pid(gh->gh_owner_pid);
Bob Peterson1a0eae82010-04-14 11:58:16 -0400920 gh->gh_owner_pid = get_pid(task_pid(current));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921}
922
923/**
924 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
925 * @gh: the holder structure
926 *
927 */
928
929void gfs2_holder_uninit(struct gfs2_holder *gh)
930{
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -0800931 put_pid(gh->gh_owner_pid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 gfs2_glock_put(gh->gh_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500933 gfs2_holder_mark_uninitialized(gh);
Steven Whitehoused0dc80d2006-03-29 14:36:49 -0500934 gh->gh_ip = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935}
936
Steven Whitehousefe64d512009-05-19 10:01:18 +0100937/**
Bob Peterson07a79042012-08-09 12:48:44 -0500938 * gfs2_glock_wait - wait on a glock acquisition
939 * @gh: the glock holder
940 *
941 * Returns: 0 on success
942 */
943
944int gfs2_glock_wait(struct gfs2_holder *gh)
Steven Whitehousefee852e2007-01-17 15:33:23 +0000945{
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400946 unsigned long time1 = jiffies;
947
Steven Whitehousefee852e2007-01-17 15:33:23 +0000948 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000949 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -0400950 if (time_after(jiffies, time1 + HZ)) /* have we waited > a second? */
951 /* Lengthen the minimum hold time. */
952 gh->gh_gl->gl_hold_time = min(gh->gh_gl->gl_hold_time +
953 GL_GLOCK_HOLD_INCR,
954 GL_GLOCK_MAX_HOLD);
Bob Peterson07a79042012-08-09 12:48:44 -0500955 return gh->gh_error;
Abhijith Dasd93cfa92007-06-11 08:22:32 +0100956}
957
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958/**
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000959 * handle_callback - process a demote request
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 * @gl: the glock
961 * @state: the state the caller wants us to change to
962 *
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000963 * There are only two requests that we are going to see in actual
964 * practise: LM_ST_SHARED and LM_ST_UNLOCKED
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 */
966
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500967static void handle_callback(struct gfs2_glock *gl, unsigned int state,
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100968 unsigned long delay, bool remote)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500970 int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
971
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -0500972 set_bit(bit, &gl->gl_flags);
973 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
Steven Whitehouse3b8249f2007-03-16 09:40:31 +0000974 gl->gl_demote_state = state;
975 gl->gl_demote_time = jiffies;
Josef Whiter26caee52007-07-23 10:02:40 +0100976 } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
977 gl->gl_demote_state != state) {
Steven Whitehouse6802e342008-05-21 17:03:22 +0100978 gl->gl_demote_state = LM_ST_UNLOCKED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 }
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500980 if (gl->gl_ops->go_callback)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100981 gl->gl_ops->go_callback(gl, remote);
Steven Whitehouse7bd8b2e2013-04-10 10:32:05 +0100982 trace_gfs2_demote_rq(gl, remote);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983}
984
Steven Whitehouse6802e342008-05-21 17:03:22 +0100985void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
Robert Peterson7c52b162007-03-16 10:26:37 +0000986{
Joe Perches5e690692010-11-09 16:35:20 -0800987 struct va_format vaf;
Robert Peterson7c52b162007-03-16 10:26:37 +0000988 va_list args;
989
990 va_start(args, fmt);
Joe Perches5e690692010-11-09 16:35:20 -0800991
Steven Whitehouse6802e342008-05-21 17:03:22 +0100992 if (seq) {
Steven Whitehouse1bb49302012-06-11 13:26:50 +0100993 seq_vprintf(seq, fmt, args);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100994 } else {
Joe Perches5e690692010-11-09 16:35:20 -0800995 vaf.fmt = fmt;
996 vaf.va = &args;
997
Joe Perchesd77d1b52014-03-06 12:10:45 -0800998 pr_err("%pV", &vaf);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100999 }
Joe Perches5e690692010-11-09 16:35:20 -08001000
Robert Peterson7c52b162007-03-16 10:26:37 +00001001 va_end(args);
1002}
1003
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 * add_to_queue - Add a holder to the wait queue (but look for recursion)
1006 * @gh: the holder structure to add
1007 *
Steven Whitehouse6802e342008-05-21 17:03:22 +01001008 * Eventually we should move the recursive locking trap to a
1009 * debugging option or something like that. This is the fast
1010 * path and needs to have the minimum number of distractions.
1011 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 */
1013
Steven Whitehouse6802e342008-05-21 17:03:22 +01001014static inline void add_to_queue(struct gfs2_holder *gh)
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001015__releases(&gl->gl_lockref.lock)
1016__acquires(&gl->gl_lockref.lock)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017{
1018 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001019 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001020 struct list_head *insert_pt = NULL;
1021 struct gfs2_holder *gh2;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001022 int try_futile = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001024 BUG_ON(gh->gh_owner_pid == NULL);
Steven Whitehousefee852e2007-01-17 15:33:23 +00001025 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1026 BUG();
Steven Whitehouse190562b2006-04-20 16:57:23 -04001027
Steven Whitehouse6802e342008-05-21 17:03:22 +01001028 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1029 if (test_bit(GLF_LOCK, &gl->gl_flags))
Bob Petersone5dc76b2012-08-09 12:48:46 -05001030 try_futile = !may_grant(gl, gh);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001031 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1032 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 }
1034
Steven Whitehouse6802e342008-05-21 17:03:22 +01001035 list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1036 if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid &&
1037 (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK)))
1038 goto trap_recursive;
Bob Petersone5dc76b2012-08-09 12:48:46 -05001039 if (try_futile &&
1040 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001041fail:
1042 gh->gh_error = GLR_TRYFAILED;
1043 gfs2_holder_wake(gh);
1044 return;
1045 }
1046 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1047 continue;
1048 if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt))
1049 insert_pt = &gh2->gh_list;
1050 }
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001051 set_bit(GLF_QUEUED, &gl->gl_flags);
Steven Whitehouseedae38a2011-01-31 09:38:12 +00001052 trace_gfs2_glock_queue(gh, 1);
Steven Whitehousea2457692012-01-20 10:38:36 +00001053 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1054 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001055 if (likely(insert_pt == NULL)) {
1056 list_add_tail(&gh->gh_list, &gl->gl_holders);
1057 if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY))
1058 goto do_cancel;
1059 return;
1060 }
1061 list_add_tail(&gh->gh_list, insert_pt);
1062do_cancel:
1063 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
1064 if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001065 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse048bca22008-05-23 14:46:04 +01001066 if (sdp->sd_lockstruct.ls_ops->lm_cancel)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001067 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001068 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001069 }
1070 return;
1071
1072trap_recursive:
Bob Petersone54c78a2018-10-03 08:47:36 -05001073 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1074 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1075 fs_err(sdp, "lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001076 gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
Bob Petersone54c78a2018-10-03 08:47:36 -05001077 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1078 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1079 fs_err(sdp, "lock type: %d req lock state : %d\n",
Steven Whitehouse6802e342008-05-21 17:03:22 +01001080 gh->gh_gl->gl_name.ln_type, gh->gh_state);
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +01001081 gfs2_dump_glock(NULL, gl);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001082 BUG();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001083}
1084
1085/**
1086 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1087 * @gh: the holder structure
1088 *
1089 * if (gh->gh_flags & GL_ASYNC), this never returns an error
1090 *
1091 * Returns: 0, GLR_TRYFAILED, or errno on failure
1092 */
1093
1094int gfs2_glock_nq(struct gfs2_holder *gh)
1095{
1096 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -05001097 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001098 int error = 0;
1099
Steven Whitehouse6802e342008-05-21 17:03:22 +01001100 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102
Steven Whitehousef42ab082011-04-14 16:50:31 +01001103 if (test_bit(GLF_LRU, &gl->gl_flags))
1104 gfs2_glock_remove_from_lru(gl);
1105
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001106 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107 add_to_queue(gh);
Bob Peterson01b172b2014-03-12 10:32:20 -04001108 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1109 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001110 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Bob Peterson01b172b2014-03-12 10:32:20 -04001111 gl->gl_lockref.count++;
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001112 __gfs2_glock_queue_work(gl, 0);
Bob Peterson01b172b2014-03-12 10:32:20 -04001113 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001114 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001115 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116
Steven Whitehouse6802e342008-05-21 17:03:22 +01001117 if (!(gh->gh_flags & GL_ASYNC))
1118 error = gfs2_glock_wait(gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 return error;
1121}
1122
1123/**
1124 * gfs2_glock_poll - poll to see if an async request has been completed
1125 * @gh: the holder
1126 *
1127 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1128 */
1129
1130int gfs2_glock_poll(struct gfs2_holder *gh)
1131{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001132 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001133}
1134
1135/**
1136 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1137 * @gh: the glock holder
1138 *
1139 */
1140
1141void gfs2_glock_dq(struct gfs2_holder *gh)
1142{
1143 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001144 const struct gfs2_glock_operations *glops = gl->gl_ops;
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001145 unsigned delay = 0;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001146 int fast_path = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001148 spin_lock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 if (gh->gh_flags & GL_NOCACHE)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001150 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 list_del_init(&gh->gh_list);
Bob Peterson7508abc2015-12-18 11:54:55 -06001153 clear_bit(HIF_HOLDER, &gh->gh_iflags);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001154 if (find_first_holder(gl) == NULL) {
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001155 if (glops->go_unlock) {
Steven Whitehouse6802e342008-05-21 17:03:22 +01001156 GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001157 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001158 glops->go_unlock(gh);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001159 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001160 clear_bit(GLF_LOCK, &gl->gl_flags);
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +00001161 }
Steven Whitehouse6802e342008-05-21 17:03:22 +01001162 if (list_empty(&gl->gl_holders) &&
1163 !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1164 !test_bit(GLF_DEMOTE, &gl->gl_flags))
1165 fast_path = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 }
Ross Lagerwall7881ef32019-03-27 17:09:17 +00001167 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl))
Bob Peterson4abb6ad92012-08-09 12:48:43 -05001168 gfs2_glock_add_to_lru(gl);
1169
Steven Whitehouse63997772009-06-12 08:49:20 +01001170 trace_gfs2_glock_queue(gh, 0);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001171 if (unlikely(!fast_path)) {
1172 gl->gl_lockref.count++;
1173 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1174 !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1175 gl->gl_name.ln_type == LM_TYPE_INODE)
1176 delay = gl->gl_hold_time;
1177 __gfs2_glock_queue_work(gl, delay);
1178 }
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001179 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180}
1181
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001182void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1183{
1184 struct gfs2_glock *gl = gh->gh_gl;
1185 gfs2_glock_dq(gh);
Bob Peterson81e1d452012-08-09 12:48:45 -05001186 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001187 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
Abhijith Dasd93cfa92007-06-11 08:22:32 +01001188}
1189
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001191 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1192 * @gh: the holder structure
1193 *
1194 */
1195
1196void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1197{
1198 gfs2_glock_dq(gh);
1199 gfs2_holder_uninit(gh);
1200}
1201
1202/**
1203 * gfs2_glock_nq_num - acquire a glock based on lock number
1204 * @sdp: the filesystem
1205 * @number: the lock number
1206 * @glops: the glock operations for the type of glock
1207 * @state: the state to acquire the glock in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001208 * @flags: modifier flags for the acquisition
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 * @gh: the struct gfs2_holder
1210 *
1211 * Returns: errno
1212 */
1213
Steven Whitehousecd915492006-09-04 12:49:07 -04001214int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
Steven Whitehouse8fb4b532006-08-30 09:30:00 -04001215 const struct gfs2_glock_operations *glops,
Bob Petersonb58bf402015-07-24 09:45:43 -05001216 unsigned int state, u16 flags, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217{
1218 struct gfs2_glock *gl;
1219 int error;
1220
1221 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1222 if (!error) {
1223 error = gfs2_glock_nq_init(gl, state, flags, gh);
1224 gfs2_glock_put(gl);
1225 }
1226
1227 return error;
1228}
1229
1230/**
1231 * glock_compare - Compare two struct gfs2_glock structures for sorting
1232 * @arg_a: the first structure
1233 * @arg_b: the second structure
1234 *
1235 */
1236
1237static int glock_compare(const void *arg_a, const void *arg_b)
1238{
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001239 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1240 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1241 const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1242 const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243
1244 if (a->ln_number > b->ln_number)
Steven Whitehousea5e08a9e2006-09-09 17:07:05 -04001245 return 1;
1246 if (a->ln_number < b->ln_number)
1247 return -1;
Steven Whitehouse1c0f4872007-01-22 12:10:39 -05001248 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 -04001249 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001250}
1251
1252/**
1253 * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1254 * @num_gh: the number of structures
1255 * @ghs: an array of struct gfs2_holder structures
1256 *
1257 * Returns: 0 on success (all glocks acquired),
1258 * errno on failure (no glocks acquired)
1259 */
1260
1261static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1262 struct gfs2_holder **p)
1263{
1264 unsigned int x;
1265 int error = 0;
1266
1267 for (x = 0; x < num_gh; x++)
1268 p[x] = &ghs[x];
1269
1270 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1271
1272 for (x = 0; x < num_gh; x++) {
1273 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1274
1275 error = gfs2_glock_nq(p[x]);
1276 if (error) {
1277 while (x--)
1278 gfs2_glock_dq(p[x]);
1279 break;
1280 }
1281 }
1282
1283 return error;
1284}
1285
1286/**
1287 * gfs2_glock_nq_m - acquire multiple glocks
1288 * @num_gh: the number of structures
1289 * @ghs: an array of struct gfs2_holder structures
1290 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291 *
1292 * Returns: 0 on success (all glocks acquired),
1293 * errno on failure (no glocks acquired)
1294 */
1295
1296int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1297{
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001298 struct gfs2_holder *tmp[4];
1299 struct gfs2_holder **pph = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 int error = 0;
1301
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001302 switch(num_gh) {
1303 case 0:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304 return 0;
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001305 case 1:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1307 return gfs2_glock_nq(ghs);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001308 default:
1309 if (num_gh <= 4)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 break;
Kees Cook6da2ec52018-06-12 13:55:00 -07001311 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1312 GFP_NOFS);
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001313 if (!pph)
1314 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315 }
1316
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001317 error = nq_m_sync(num_gh, ghs, pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318
Steven Whitehouseeaf5bd32007-06-19 15:38:17 +01001319 if (pph != tmp)
1320 kfree(pph);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321
1322 return error;
1323}
1324
1325/**
1326 * gfs2_glock_dq_m - release multiple glocks
1327 * @num_gh: the number of structures
1328 * @ghs: an array of struct gfs2_holder structures
1329 *
1330 */
1331
1332void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1333{
Bob Petersonfa1bbde2011-03-10 11:41:57 -05001334 while (num_gh--)
1335 gfs2_glock_dq(&ghs[num_gh]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001336}
1337
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001338void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
Steven Whitehouseda755fd2008-01-30 15:34:04 +00001339{
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001340 unsigned long delay = 0;
1341 unsigned long holdtime;
1342 unsigned long now = jiffies;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001344 gfs2_glock_hold(gl);
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001345 holdtime = gl->gl_tchange + gl->gl_hold_time;
1346 if (test_bit(GLF_QUEUED, &gl->gl_flags) &&
1347 gl->gl_name.ln_type == LM_TYPE_INODE) {
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001348 if (time_before(now, holdtime))
1349 delay = holdtime - now;
1350 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001351 delay = gl->gl_hold_time;
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001352 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001354 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001355 handle_callback(gl, state, delay, true);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001356 __gfs2_glock_queue_work(gl, delay);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001357 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001358}
1359
1360/**
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001361 * gfs2_should_freeze - Figure out if glock should be frozen
1362 * @gl: The glock in question
1363 *
1364 * Glocks are not frozen if (a) the result of the dlm operation is
1365 * an error, (b) the locking operation was an unlock operation or
1366 * (c) if there is a "noexp" flagged request anywhere in the queue
1367 *
1368 * Returns: 1 if freezing should occur, 0 otherwise
1369 */
1370
1371static int gfs2_should_freeze(const struct gfs2_glock *gl)
1372{
1373 const struct gfs2_holder *gh;
1374
1375 if (gl->gl_reply & ~LM_OUT_ST_MASK)
1376 return 0;
1377 if (gl->gl_target == LM_ST_UNLOCKED)
1378 return 0;
1379
1380 list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1381 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1382 continue;
1383 if (LM_FLAG_NOEXP & gh->gh_flags)
1384 return 0;
1385 }
1386
1387 return 1;
1388}
1389
1390/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001391 * gfs2_glock_complete - Callback used by locking
1392 * @gl: Pointer to the glock
1393 * @ret: The return value from the dlm
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001395 * The gl_reply field is under the gl_lockref.lock lock so that it is ok
Steven Whitehouse47a25382010-11-30 15:49:31 +00001396 * to use a bitfield shared with other glock state fields.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 */
1398
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001399void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400{
Bob Peterson15562c42015-03-16 11:52:05 -05001401 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001402
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001403 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001404 gl->gl_reply = ret;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001405
David Teiglande0c2a9a2012-01-09 17:18:05 -05001406 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001407 if (gfs2_should_freeze(gl)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001408 set_bit(GLF_FROZEN, &gl->gl_flags);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001409 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410 return;
Steven Whitehouse0809f6e2010-08-02 10:15:17 +01001411 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001412 }
Steven Whitehouse47a25382010-11-30 15:49:31 +00001413
Steven Whitehousee66cf162013-10-15 15:18:08 +01001414 gl->gl_lockref.count++;
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001415 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001416 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001417 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418}
1419
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001420static int glock_cmp(void *priv, struct list_head *a, struct list_head *b)
1421{
1422 struct gfs2_glock *gla, *glb;
1423
1424 gla = list_entry(a, struct gfs2_glock, gl_lru);
1425 glb = list_entry(b, struct gfs2_glock, gl_lru);
1426
1427 if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1428 return 1;
1429 if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1430 return -1;
1431
1432 return 0;
1433}
1434
1435/**
1436 * gfs2_dispose_glock_lru - Demote a list of glocks
1437 * @list: The list to dispose of
1438 *
1439 * Disposing of glocks may involve disk accesses, so that here we sort
1440 * the glocks by number (i.e. disk location of the inodes) so that if
1441 * there are any such accesses, they'll be sent in order (mostly).
1442 *
1443 * Must be called under the lru_lock, but may drop and retake this
1444 * lock. While the lru_lock is dropped, entries may vanish from the
1445 * list, but no new entries will appear on the list (since it is
1446 * private)
1447 */
1448
1449static void gfs2_dispose_glock_lru(struct list_head *list)
1450__releases(&lru_lock)
1451__acquires(&lru_lock)
1452{
1453 struct gfs2_glock *gl;
1454
1455 list_sort(NULL, list, glock_cmp);
1456
1457 while(!list_empty(list)) {
1458 gl = list_entry(list->next, struct gfs2_glock, gl_lru);
1459 list_del_init(&gl->gl_lru);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001460 if (!spin_trylock(&gl->gl_lockref.lock)) {
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001461add_back_to_lru:
Steven Whitehousee66cf162013-10-15 15:18:08 +01001462 list_add(&gl->gl_lru, &lru_list);
Ross Lagerwall7881ef32019-03-27 17:09:17 +00001463 set_bit(GLF_LRU, &gl->gl_flags);
Steven Whitehousee66cf162013-10-15 15:18:08 +01001464 atomic_inc(&lru_count);
1465 continue;
1466 }
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001467 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001468 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001469 goto add_back_to_lru;
1470 }
Steven Whitehousee66cf162013-10-15 15:18:08 +01001471 gl->gl_lockref.count++;
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001472 if (demote_ok(gl))
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001473 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001474 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags));
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001475 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001476 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001477 cond_resched_lock(&lru_lock);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001478 }
1479}
1480
Steven Whitehouse2a005852012-12-14 12:28:30 +00001481/**
1482 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
1483 * @nr: The number of entries to scan
1484 *
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001485 * This function selects the entries on the LRU which are able to
1486 * be demoted, and then kicks off the process by calling
1487 * gfs2_dispose_glock_lru() above.
Steven Whitehouse2a005852012-12-14 12:28:30 +00001488 */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489
Dave Chinner1ab6c492013-08-28 10:18:09 +10001490static long gfs2_scan_glock_lru(int nr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001491{
1492 struct gfs2_glock *gl;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001493 LIST_HEAD(skipped);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001494 LIST_HEAD(dispose);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001495 long freed = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001496
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001497 spin_lock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001498 while ((nr-- >= 0) && !list_empty(&lru_list)) {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001499 gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001500
1501 /* Test for being demotable */
Steven Whitehouse94a09a32014-06-23 14:43:32 +01001502 if (!test_bit(GLF_LOCK, &gl->gl_flags)) {
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001503 list_move(&gl->gl_lru, &dispose);
1504 atomic_dec(&lru_count);
Ross Lagerwall7881ef32019-03-27 17:09:17 +00001505 clear_bit(GLF_LRU, &gl->gl_flags);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001506 freed++;
Steven Whitehouse2163b1e2009-06-25 16:30:26 +01001507 continue;
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001508 }
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001509
1510 list_move(&gl->gl_lru, &skipped);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001511 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001512 list_splice(&skipped, &lru_list);
Steven Whitehouse4506a5192013-02-01 20:36:03 +00001513 if (!list_empty(&dispose))
1514 gfs2_dispose_glock_lru(&dispose);
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001515 spin_unlock(&lru_lock);
Dave Chinner1ab6c492013-08-28 10:18:09 +10001516
1517 return freed;
Steven Whitehouse2a005852012-12-14 12:28:30 +00001518}
1519
Dave Chinner1ab6c492013-08-28 10:18:09 +10001520static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
1521 struct shrink_control *sc)
Steven Whitehouse2a005852012-12-14 12:28:30 +00001522{
Dave Chinner1ab6c492013-08-28 10:18:09 +10001523 if (!(sc->gfp_mask & __GFP_FS))
1524 return SHRINK_STOP;
1525 return gfs2_scan_glock_lru(sc->nr_to_scan);
1526}
Steven Whitehouse2a005852012-12-14 12:28:30 +00001527
Dave Chinner1ab6c492013-08-28 10:18:09 +10001528static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
1529 struct shrink_control *sc)
1530{
Glauber Costa55f841c2013-08-28 10:17:53 +10001531 return vfs_pressure_ratio(atomic_read(&lru_count));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001532}
1533
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001534static struct shrinker glock_shrinker = {
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001535 .seeks = DEFAULT_SEEKS,
Dave Chinner1ab6c492013-08-28 10:18:09 +10001536 .count_objects = gfs2_glock_shrink_count,
1537 .scan_objects = gfs2_glock_shrink_scan,
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001538};
1539
David Teiglandb3b94fa2006-01-16 16:50:04 +00001540/**
1541 * examine_bucket - Call a function for glock in a hash bucket
1542 * @examiner: the function
1543 * @sdp: the filesystem
1544 * @bucket: the bucket
1545 *
Herbert Xu98687f42017-02-11 19:26:45 +08001546 * Note that the function can be called multiple times on the same
1547 * object. So the user must ensure that the function can cope with
1548 * that.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001549 */
1550
Bob Peterson88ffbf32015-03-16 11:02:46 -05001551static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001553 struct gfs2_glock *gl;
Herbert Xu98687f42017-02-11 19:26:45 +08001554 struct rhashtable_iter iter;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001555
Herbert Xu98687f42017-02-11 19:26:45 +08001556 rhashtable_walk_enter(&gl_hash_table, &iter);
1557
1558 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08001559 rhashtable_walk_start(&iter);
Herbert Xu98687f42017-02-11 19:26:45 +08001560
1561 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl))
Bob Peterson27c3b412017-08-18 09:15:13 -05001562 if (gl->gl_name.ln_sbd == sdp &&
Bob Peterson88ffbf32015-03-16 11:02:46 -05001563 lockref_get_not_dead(&gl->gl_lockref))
1564 examiner(gl);
Herbert Xu98687f42017-02-11 19:26:45 +08001565
1566 rhashtable_walk_stop(&iter);
1567 } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
1568
1569 rhashtable_walk_exit(&iter);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001570}
1571
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001572/**
1573 * thaw_glock - thaw out a glock which has an unprocessed reply waiting
1574 * @gl: The glock to thaw
1575 *
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001576 */
1577
1578static void thaw_glock(struct gfs2_glock *gl)
1579{
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001580 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) {
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001581 gfs2_glock_put(gl);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001582 return;
Steven Whitehouse7286b312013-08-20 09:35:09 +01001583 }
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001584 set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
1585 gfs2_glock_queue_work(gl, 0);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001586}
1587
David Teiglandb3b94fa2006-01-16 16:50:04 +00001588/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001589 * clear_glock - look at a glock and see if we can free it from glock cache
1590 * @gl: the glock to look at
1591 *
1592 */
1593
1594static void clear_glock(struct gfs2_glock *gl)
1595{
Steven Whitehousef42ab082011-04-14 16:50:31 +01001596 gfs2_glock_remove_from_lru(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001597
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001598 spin_lock(&gl->gl_lockref.lock);
Steven Whitehousec741c452010-09-29 14:20:52 +01001599 if (gl->gl_state != LM_ST_UNLOCKED)
Steven Whitehouse81ffbf62013-04-10 10:26:55 +01001600 handle_callback(gl, LM_ST_UNLOCKED, 0, false);
Andreas Gruenbacher6b0c7442017-06-30 08:10:01 -05001601 __gfs2_glock_queue_work(gl, 0);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001602 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001603}
1604
1605/**
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001606 * gfs2_glock_thaw - Thaw any frozen glocks
1607 * @sdp: The super block
1608 *
1609 */
1610
1611void gfs2_glock_thaw(struct gfs2_sbd *sdp)
1612{
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001613 glock_hash_walk(thaw_glock, sdp);
1614}
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001615
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001616static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001617{
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001618 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001619 gfs2_dump_glock(seq, gl);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001620 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001621}
1622
1623static void dump_glock_func(struct gfs2_glock *gl)
1624{
1625 dump_glock(NULL, gl);
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001626}
1627
1628/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001629 * gfs2_gl_hash_clear - Empty out the glock hash table
1630 * @sdp: the filesystem
1631 * @wait: wait until it's all gone
1632 *
Steven Whitehouse1bdad602008-06-03 14:09:53 +01001633 * Called when unmounting the filesystem.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001634 */
1635
Steven Whitehousefefc03b2008-12-19 15:32:06 +00001636void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001637{
David Teiglandfb6791d2012-11-13 10:58:56 -05001638 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
Bob Peterson222cb532013-04-25 12:49:17 -04001639 flush_workqueue(glock_workqueue);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001640 glock_hash_walk(clear_glock, sdp);
Steven Whitehouse8f052282010-01-29 15:21:27 +00001641 flush_workqueue(glock_workqueue);
Bob Peterson2aba1b52015-05-19 09:11:23 -05001642 wait_event_timeout(sdp->sd_glock_wait,
1643 atomic_read(&sdp->sd_glock_disposal) == 0,
1644 HZ * 600);
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001645 glock_hash_walk(dump_glock_func, sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001646}
1647
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001648void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
1649{
1650 struct gfs2_glock *gl = ip->i_gl;
1651 int ret;
1652
1653 ret = gfs2_truncatei_resume(ip);
Bob Peterson15562c42015-03-16 11:52:05 -05001654 gfs2_assert_withdraw(gl->gl_name.ln_sbd, ret == 0);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001655
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001656 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001657 clear_bit(GLF_LOCK, &gl->gl_flags);
1658 run_queue(gl, 1);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -05001659 spin_unlock(&gl->gl_lockref.lock);
Steven Whitehouse813e0c42008-11-18 13:38:48 +00001660}
1661
Steven Whitehouse6802e342008-05-21 17:03:22 +01001662static const char *state2str(unsigned state)
Robert Peterson04b933f2007-03-23 17:05:15 -05001663{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001664 switch(state) {
1665 case LM_ST_UNLOCKED:
1666 return "UN";
1667 case LM_ST_SHARED:
1668 return "SH";
1669 case LM_ST_DEFERRED:
1670 return "DF";
1671 case LM_ST_EXCLUSIVE:
1672 return "EX";
1673 }
1674 return "??";
1675}
Robert Peterson04b933f2007-03-23 17:05:15 -05001676
Bob Petersonb58bf402015-07-24 09:45:43 -05001677static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
Steven Whitehouse6802e342008-05-21 17:03:22 +01001678{
1679 char *p = buf;
1680 if (flags & LM_FLAG_TRY)
1681 *p++ = 't';
1682 if (flags & LM_FLAG_TRY_1CB)
1683 *p++ = 'T';
1684 if (flags & LM_FLAG_NOEXP)
1685 *p++ = 'e';
1686 if (flags & LM_FLAG_ANY)
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001687 *p++ = 'A';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001688 if (flags & LM_FLAG_PRIORITY)
1689 *p++ = 'p';
1690 if (flags & GL_ASYNC)
1691 *p++ = 'a';
1692 if (flags & GL_EXACT)
1693 *p++ = 'E';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001694 if (flags & GL_NOCACHE)
1695 *p++ = 'c';
1696 if (test_bit(HIF_HOLDER, &iflags))
1697 *p++ = 'H';
1698 if (test_bit(HIF_WAIT, &iflags))
1699 *p++ = 'W';
1700 if (test_bit(HIF_FIRST, &iflags))
1701 *p++ = 'F';
1702 *p = 0;
1703 return buf;
Robert Peterson04b933f2007-03-23 17:05:15 -05001704}
1705
David Teiglandb3b94fa2006-01-16 16:50:04 +00001706/**
1707 * dump_holder - print information about a glock holder
Steven Whitehouse6802e342008-05-21 17:03:22 +01001708 * @seq: the seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709 * @gh: the glock holder
1710 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001711 */
1712
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001713static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001714{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001715 struct task_struct *gh_owner = NULL;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001716 char flags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001717
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001718 rcu_read_lock();
Steven Whitehouse6802e342008-05-21 17:03:22 +01001719 if (gh->gh_owner_pid)
Pavel Emelyanovb1e058d2008-02-07 00:13:19 -08001720 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
Joe Perchescc181522010-11-05 16:12:36 -07001721 gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
1722 state2str(gh->gh_state),
1723 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
1724 gh->gh_error,
1725 gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1,
1726 gh_owner ? gh_owner->comm : "(ended)",
1727 (void *)gh->gh_ip);
Tetsuo Handa0b3a2c92014-01-02 19:52:20 +09001728 rcu_read_unlock();
David Teiglandb3b94fa2006-01-16 16:50:04 +00001729}
1730
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001731static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001732{
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001733 const unsigned long *gflags = &gl->gl_flags;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001734 char *p = buf;
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001735
Steven Whitehouse6802e342008-05-21 17:03:22 +01001736 if (test_bit(GLF_LOCK, gflags))
1737 *p++ = 'l';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001738 if (test_bit(GLF_DEMOTE, gflags))
1739 *p++ = 'D';
1740 if (test_bit(GLF_PENDING_DEMOTE, gflags))
1741 *p++ = 'd';
1742 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
1743 *p++ = 'p';
1744 if (test_bit(GLF_DIRTY, gflags))
1745 *p++ = 'y';
1746 if (test_bit(GLF_LFLUSH, gflags))
1747 *p++ = 'f';
1748 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
1749 *p++ = 'i';
1750 if (test_bit(GLF_REPLY_PENDING, gflags))
1751 *p++ = 'r';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001752 if (test_bit(GLF_INITIAL, gflags))
Steven Whitehoused8348de2009-02-05 10:12:38 +00001753 *p++ = 'I';
Steven Whitehousef057f6c2009-01-12 10:43:39 +00001754 if (test_bit(GLF_FROZEN, gflags))
1755 *p++ = 'F';
Steven Whitehouse7b5e3d52010-09-03 09:39:20 +01001756 if (test_bit(GLF_QUEUED, gflags))
1757 *p++ = 'q';
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001758 if (test_bit(GLF_LRU, gflags))
1759 *p++ = 'L';
1760 if (gl->gl_object)
1761 *p++ = 'o';
Steven Whitehousea2457692012-01-20 10:38:36 +00001762 if (test_bit(GLF_BLOCKING, gflags))
1763 *p++ = 'b';
Steven Whitehouse6802e342008-05-21 17:03:22 +01001764 *p = 0;
1765 return buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001766}
1767
1768/**
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +01001769 * gfs2_dump_glock - print information about a glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001770 * @seq: The seq_file struct
David Teiglandb3b94fa2006-01-16 16:50:04 +00001771 * @gl: the glock
Steven Whitehouse6802e342008-05-21 17:03:22 +01001772 *
1773 * The file format is as follows:
1774 * One line per object, capital letters are used to indicate objects
1775 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
1776 * other objects are indented by a single space and follow the glock to
1777 * which they are related. Fields are indicated by lower case letters
1778 * followed by a colon and the field value, except for strings which are in
1779 * [] so that its possible to see if they are composed of spaces for
1780 * example. The field's are n = number (id of the object), f = flags,
1781 * t = type, s = state, r = refcount, e = error, p = pid.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001783 */
1784
Bob Peterson27a2660f2018-04-18 12:05:01 -07001785void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001786{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001787 const struct gfs2_glock_operations *glops = gl->gl_ops;
1788 unsigned long long dtime;
1789 const struct gfs2_holder *gh;
1790 char gflags_buf[32];
David Teiglandb3b94fa2006-01-16 16:50:04 +00001791
Steven Whitehouse6802e342008-05-21 17:03:22 +01001792 dtime = jiffies - gl->gl_demote_time;
1793 dtime *= 1000000/HZ; /* demote time in uSec */
1794 if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
1795 dtime = 0;
Bob Peterson7cf8dcd2011-06-15 11:41:48 -04001796 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 +01001797 state2str(gl->gl_state),
1798 gl->gl_name.ln_type,
1799 (unsigned long long)gl->gl_name.ln_number,
Steven Whitehouse627c10b2011-04-14 14:09:52 +01001800 gflags2str(gflags_buf, gl),
Steven Whitehouse6802e342008-05-21 17:03:22 +01001801 state2str(gl->gl_target),
1802 state2str(gl->gl_demote_state), dtime,
Steven Whitehouse6802e342008-05-21 17:03:22 +01001803 atomic_read(&gl->gl_ail_count),
Bob Peterson73118ca2019-04-05 04:41:38 +01001804 test_bit(GLF_REVOKES, &gl->gl_flags) ? 1 : 0,
Steven Whitehousee66cf162013-10-15 15:18:08 +01001805 (int)gl->gl_lockref.count, gl->gl_hold_time);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001806
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001807 list_for_each_entry(gh, &gl->gl_holders, gh_list)
1808 dump_holder(seq, gh);
1809
Steven Whitehouse6802e342008-05-21 17:03:22 +01001810 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
Steven Whitehouseac3beb62014-01-16 10:31:13 +00001811 glops->go_dump(seq, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001812}
1813
Steven Whitehousea2457692012-01-20 10:38:36 +00001814static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
1815{
1816 struct gfs2_glock *gl = iter_ptr;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001817
Ben Hutchings4d207132015-08-27 12:51:45 -05001818 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 +00001819 gl->gl_name.ln_type,
1820 (unsigned long long)gl->gl_name.ln_number,
Ben Hutchings4d207132015-08-27 12:51:45 -05001821 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
1822 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
1823 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
1824 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
1825 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
1826 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
1827 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
1828 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001829 return 0;
1830}
David Teiglandb3b94fa2006-01-16 16:50:04 +00001831
Steven Whitehousea2457692012-01-20 10:38:36 +00001832static const char *gfs2_gltype[] = {
1833 "type",
1834 "reserved",
1835 "nondisk",
1836 "inode",
1837 "rgrp",
1838 "meta",
1839 "iopen",
1840 "flock",
1841 "plock",
1842 "quota",
1843 "journal",
1844};
1845
1846static const char *gfs2_stype[] = {
1847 [GFS2_LKS_SRTT] = "srtt",
1848 [GFS2_LKS_SRTTVAR] = "srttvar",
1849 [GFS2_LKS_SRTTB] = "srttb",
1850 [GFS2_LKS_SRTTVARB] = "srttvarb",
1851 [GFS2_LKS_SIRT] = "sirt",
1852 [GFS2_LKS_SIRTVAR] = "sirtvar",
1853 [GFS2_LKS_DCOUNT] = "dlm",
1854 [GFS2_LKS_QCOUNT] = "queue",
1855};
1856
1857#define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
1858
1859static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
1860{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05001861 struct gfs2_sbd *sdp = seq->private;
1862 loff_t pos = *(loff_t *)iter_ptr;
1863 unsigned index = pos >> 3;
1864 unsigned subindex = pos & 0x07;
Steven Whitehousea2457692012-01-20 10:38:36 +00001865 int i;
1866
1867 if (index == 0 && subindex != 0)
1868 return 0;
1869
1870 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
1871 (index == 0) ? "cpu": gfs2_stype[subindex]);
1872
1873 for_each_possible_cpu(i) {
1874 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
Andreas Gruenbacher8f7e0a82015-08-27 13:02:54 -05001875
1876 if (index == 0)
1877 seq_printf(seq, " %15u", i);
1878 else
1879 seq_printf(seq, " %15llu", (unsigned long long)lkstats->
1880 lkstats[index - 1].stats[subindex]);
Steven Whitehousea2457692012-01-20 10:38:36 +00001881 }
1882 seq_putc(seq, '\n');
1883 return 0;
1884}
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001885
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001886int __init gfs2_glock_init(void)
1887{
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001888 int i, ret;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001889
1890 ret = rhashtable_init(&gl_hash_table, &ht_parms);
1891 if (ret < 0)
1892 return ret;
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001893
Steven Whitehoused2115772010-11-03 19:58:53 +00001894 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM |
Tejun Heo58a69cb2011-02-16 09:25:31 +01001895 WQ_HIGHPRI | WQ_FREEZABLE, 0);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001896 if (!glock_workqueue) {
1897 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001898 return -ENOMEM;
Bob Peterson88ffbf32015-03-16 11:02:46 -05001899 }
Steven Whitehoused2115772010-11-03 19:58:53 +00001900 gfs2_delete_workqueue = alloc_workqueue("delete_workqueue",
Tejun Heo58a69cb2011-02-16 09:25:31 +01001901 WQ_MEM_RECLAIM | WQ_FREEZABLE,
Steven Whitehoused2115772010-11-03 19:58:53 +00001902 0);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001903 if (!gfs2_delete_workqueue) {
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001904 destroy_workqueue(glock_workqueue);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001905 rhashtable_destroy(&gl_hash_table);
Dan Carpenterdfc46162013-08-15 10:54:43 +03001906 return -ENOMEM;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001907 }
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001908
Chao Yue0d735c2016-09-21 12:09:40 -05001909 ret = register_shrinker(&glock_shrinker);
1910 if (ret) {
1911 destroy_workqueue(gfs2_delete_workqueue);
1912 destroy_workqueue(glock_workqueue);
1913 rhashtable_destroy(&gl_hash_table);
1914 return ret;
1915 }
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001916
Andreas Gruenbacher05154802017-08-01 11:18:26 -05001917 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
1918 init_waitqueue_head(glock_wait_table + i);
1919
Steven Whitehouse85d1da62006-09-07 14:40:21 -04001920 return 0;
1921}
1922
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001923void gfs2_glock_exit(void)
1924{
Steven Whitehouse97cc10252008-11-20 13:39:47 +00001925 unregister_shrinker(&glock_shrinker);
Bob Peterson88ffbf32015-03-16 11:02:46 -05001926 rhashtable_destroy(&gl_hash_table);
Benjamin Marzinskic4f68a12007-08-23 13:19:05 -05001927 destroy_workqueue(glock_workqueue);
Benjamin Marzinskib94a1702009-07-23 18:52:34 -05001928 destroy_workqueue(gfs2_delete_workqueue);
Steven Whitehouse8fbbfd22007-08-01 13:57:10 +01001929}
1930
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001931static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001932{
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001933 struct gfs2_glock *gl = gi->gl;
1934
1935 if (gl) {
1936 if (n == 0)
1937 return;
1938 if (!lockref_put_not_zero(&gl->gl_lockref))
1939 gfs2_glock_queue_put(gl);
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001940 }
1941 for (;;) {
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001942 gl = rhashtable_walk_next(&gi->hti);
1943 if (IS_ERR_OR_NULL(gl)) {
1944 if (gl == ERR_PTR(-EAGAIN)) {
1945 n = 1;
1946 continue;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001947 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001948 gl = NULL;
1949 break;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00001950 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001951 if (gl->gl_name.ln_sbd != gi->sdp)
1952 continue;
1953 if (n <= 1) {
1954 if (!lockref_get_not_dead(&gl->gl_lockref))
1955 continue;
1956 break;
1957 } else {
1958 if (__lockref_is_dead(&gl->gl_lockref))
1959 continue;
1960 n--;
1961 }
Dan Carpenter14d37562016-12-14 08:02:03 -06001962 }
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02001963 gi->gl = gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001964}
1965
Steven Whitehouse6802e342008-05-21 17:03:22 +01001966static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
Bob Peterson27c3b412017-08-18 09:15:13 -05001967 __acquires(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00001968{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001969 struct gfs2_glock_iter *gi = seq->private;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001970 loff_t n;
Robert Peterson7c52b162007-03-16 10:26:37 +00001971
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001972 /*
1973 * We can either stay where we are, skip to the next hash table
1974 * entry, or start from the beginning.
1975 */
1976 if (*pos < gi->last_pos) {
1977 rhashtable_walk_exit(&gi->hti);
1978 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
1979 n = *pos + 1;
1980 } else {
1981 n = *pos - gi->last_pos;
1982 }
Robert Peterson7c52b162007-03-16 10:26:37 +00001983
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001984 rhashtable_walk_start(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00001985
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001986 gfs2_glock_iter_next(gi, n);
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001987 gi->last_pos = *pos;
Steven Whitehouse6802e342008-05-21 17:03:22 +01001988 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00001989}
1990
Steven Whitehouse6802e342008-05-21 17:03:22 +01001991static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
Robert Peterson7c52b162007-03-16 10:26:37 +00001992 loff_t *pos)
1993{
Steven Whitehouse6802e342008-05-21 17:03:22 +01001994 struct gfs2_glock_iter *gi = seq->private;
Robert Peterson7c52b162007-03-16 10:26:37 +00001995
1996 (*pos)++;
Steven Whitehouseba1ddcb2012-06-08 11:16:22 +01001997 gi->last_pos = *pos;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01001998 gfs2_glock_iter_next(gi, 1);
Steven Whitehouse6802e342008-05-21 17:03:22 +01001999 return gi->gl;
Robert Peterson7c52b162007-03-16 10:26:37 +00002000}
2001
Steven Whitehouse6802e342008-05-21 17:03:22 +01002002static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
Bob Peterson27c3b412017-08-18 09:15:13 -05002003 __releases(RCU)
Robert Peterson7c52b162007-03-16 10:26:37 +00002004{
Steven Whitehouse6802e342008-05-21 17:03:22 +01002005 struct gfs2_glock_iter *gi = seq->private;
Steven Whitehousebc015cb2011-01-19 09:30:01 +00002006
Bob Peterson88ffbf32015-03-16 11:02:46 -05002007 rhashtable_walk_stop(&gi->hti);
Robert Peterson7c52b162007-03-16 10:26:37 +00002008}
2009
Steven Whitehouse6802e342008-05-21 17:03:22 +01002010static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
Robert Peterson7c52b162007-03-16 10:26:37 +00002011{
Steven Whitehouseac3beb62014-01-16 10:31:13 +00002012 dump_glock(seq, iter_ptr);
2013 return 0;
Robert Peterson7c52b162007-03-16 10:26:37 +00002014}
2015
Steven Whitehousea2457692012-01-20 10:38:36 +00002016static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2017{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002018 preempt_disable();
Steven Whitehousea2457692012-01-20 10:38:36 +00002019 if (*pos >= GFS2_NR_SBSTATS)
2020 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002021 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002022}
2023
2024static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2025 loff_t *pos)
2026{
Steven Whitehousea2457692012-01-20 10:38:36 +00002027 (*pos)++;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002028 if (*pos >= GFS2_NR_SBSTATS)
Steven Whitehousea2457692012-01-20 10:38:36 +00002029 return NULL;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002030 return pos;
Steven Whitehousea2457692012-01-20 10:38:36 +00002031}
2032
2033static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2034{
2035 preempt_enable();
2036}
2037
Denis Cheng4ef29002007-07-31 18:31:11 +08002038static const struct seq_operations gfs2_glock_seq_ops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002039 .start = gfs2_glock_seq_start,
2040 .next = gfs2_glock_seq_next,
2041 .stop = gfs2_glock_seq_stop,
2042 .show = gfs2_glock_seq_show,
2043};
2044
Steven Whitehousea2457692012-01-20 10:38:36 +00002045static const struct seq_operations gfs2_glstats_seq_ops = {
2046 .start = gfs2_glock_seq_start,
2047 .next = gfs2_glock_seq_next,
2048 .stop = gfs2_glock_seq_stop,
2049 .show = gfs2_glstats_seq_show,
2050};
2051
2052static const struct seq_operations gfs2_sbstats_seq_ops = {
2053 .start = gfs2_sbstats_seq_start,
2054 .next = gfs2_sbstats_seq_next,
2055 .stop = gfs2_sbstats_seq_stop,
2056 .show = gfs2_sbstats_seq_show,
2057};
2058
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002059#define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2060
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002061static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2062 const struct seq_operations *ops)
Robert Peterson7c52b162007-03-16 10:26:37 +00002063{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002064 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
Steven Whitehouse6802e342008-05-21 17:03:22 +01002065 if (ret == 0) {
2066 struct seq_file *seq = file->private_data;
2067 struct gfs2_glock_iter *gi = seq->private;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002068
Steven Whitehouse6802e342008-05-21 17:03:22 +01002069 gi->sdp = inode->i_private;
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002070 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
Steven Whitehousedf5d2f52012-06-07 13:30:16 +01002071 if (seq->buf)
Steven Whitehouse0fe2f1e2012-06-11 13:49:47 +01002072 seq->size = GFS2_SEQ_GOODSIZE;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002073 /*
2074 * Initially, we are "before" the first hash table entry; the
2075 * first call to rhashtable_walk_next gets us the first entry.
2076 */
2077 gi->last_pos = -1;
Bob Peterson88ffbf32015-03-16 11:02:46 -05002078 gi->gl = NULL;
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002079 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
Steven Whitehouse6802e342008-05-21 17:03:22 +01002080 }
2081 return ret;
Robert Peterson7c52b162007-03-16 10:26:37 +00002082}
2083
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002084static int gfs2_glocks_open(struct inode *inode, struct file *file)
2085{
2086 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2087}
2088
Bob Peterson88ffbf32015-03-16 11:02:46 -05002089static int gfs2_glocks_release(struct inode *inode, struct file *file)
2090{
2091 struct seq_file *seq = file->private_data;
2092 struct gfs2_glock_iter *gi = seq->private;
2093
Andreas Gruenbacher3fd5d3a2018-03-28 12:05:35 +02002094 if (gi->gl)
2095 gfs2_glock_put(gi->gl);
Andreas Gruenbacher7ac07fd2018-01-08 22:35:43 +01002096 rhashtable_walk_exit(&gi->hti);
Bob Peterson88ffbf32015-03-16 11:02:46 -05002097 return seq_release_private(inode, file);
2098}
2099
Steven Whitehousea2457692012-01-20 10:38:36 +00002100static int gfs2_glstats_open(struct inode *inode, struct file *file)
2101{
Andreas Gruenbacher92ecd732017-03-09 09:48:05 -05002102 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002103}
2104
2105static int gfs2_sbstats_open(struct inode *inode, struct file *file)
2106{
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002107 int ret = seq_open(file, &gfs2_sbstats_seq_ops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002108 if (ret == 0) {
2109 struct seq_file *seq = file->private_data;
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002110 seq->private = inode->i_private; /* sdp */
Steven Whitehousea2457692012-01-20 10:38:36 +00002111 }
2112 return ret;
2113}
2114
2115static const struct file_operations gfs2_glocks_fops = {
Robert Peterson7c52b162007-03-16 10:26:37 +00002116 .owner = THIS_MODULE,
Steven Whitehousea2457692012-01-20 10:38:36 +00002117 .open = gfs2_glocks_open,
2118 .read = seq_read,
2119 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002120 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002121};
2122
2123static const struct file_operations gfs2_glstats_fops = {
2124 .owner = THIS_MODULE,
2125 .open = gfs2_glstats_open,
2126 .read = seq_read,
2127 .llseek = seq_lseek,
Bob Peterson88ffbf32015-03-16 11:02:46 -05002128 .release = gfs2_glocks_release,
Steven Whitehousea2457692012-01-20 10:38:36 +00002129};
2130
2131static const struct file_operations gfs2_sbstats_fops = {
2132 .owner = THIS_MODULE,
2133 .open = gfs2_sbstats_open,
Robert Peterson7c52b162007-03-16 10:26:37 +00002134 .read = seq_read,
2135 .llseek = seq_lseek,
Andreas Gruenbacher81648d02015-08-27 11:43:00 -05002136 .release = seq_release,
Robert Peterson7c52b162007-03-16 10:26:37 +00002137};
2138
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002139void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
Robert Peterson7c52b162007-03-16 10:26:37 +00002140{
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002141 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
Steven Whitehousea2457692012-01-20 10:38:36 +00002142
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002143 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2144 &gfs2_glocks_fops);
Steven Whitehousea2457692012-01-20 10:38:36 +00002145
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002146 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2147 &gfs2_glstats_fops);
Chengyu Song7b4ddfa2015-03-24 09:37:53 -05002148
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002149 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2150 &gfs2_sbstats_fops);
Robert Peterson7c52b162007-03-16 10:26:37 +00002151}
2152
2153void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2154{
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002155 debugfs_remove_recursive(sdp->debugfs_dir);
2156 sdp->debugfs_dir = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002157}
2158
Greg Kroah-Hartman2abbf9a2019-01-22 16:21:51 +01002159void gfs2_register_debugfs(void)
Robert Peterson7c52b162007-03-16 10:26:37 +00002160{
2161 gfs2_root = debugfs_create_dir("gfs2", NULL);
Robert Peterson7c52b162007-03-16 10:26:37 +00002162}
2163
2164void gfs2_unregister_debugfs(void)
2165{
2166 debugfs_remove(gfs2_root);
Robert Peterson5f882092007-04-18 11:41:11 -05002167 gfs2_root = NULL;
Robert Peterson7c52b162007-03-16 10:26:37 +00002168}