]> Git Repo - linux.git/blob - fs/gfs2/glock.c
gfs2: Prevent inode creation race
[linux.git] / fs / gfs2 / glock.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
5  */
6
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/buffer_head.h>
13 #include <linux/delay.h>
14 #include <linux/sort.h>
15 #include <linux/hash.h>
16 #include <linux/jhash.h>
17 #include <linux/kallsyms.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/list.h>
20 #include <linux/wait.h>
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/seq_file.h>
24 #include <linux/debugfs.h>
25 #include <linux/kthread.h>
26 #include <linux/freezer.h>
27 #include <linux/workqueue.h>
28 #include <linux/jiffies.h>
29 #include <linux/rcupdate.h>
30 #include <linux/rculist_bl.h>
31 #include <linux/bit_spinlock.h>
32 #include <linux/percpu.h>
33 #include <linux/list_sort.h>
34 #include <linux/lockref.h>
35 #include <linux/rhashtable.h>
36 #include <linux/pid_namespace.h>
37 #include <linux/fdtable.h>
38 #include <linux/file.h>
39 #include <linux/random.h>
40
41 #include "gfs2.h"
42 #include "incore.h"
43 #include "glock.h"
44 #include "glops.h"
45 #include "inode.h"
46 #include "lops.h"
47 #include "meta_io.h"
48 #include "quota.h"
49 #include "super.h"
50 #include "util.h"
51 #include "bmap.h"
52 #define CREATE_TRACE_POINTS
53 #include "trace_gfs2.h"
54
55 struct gfs2_glock_iter {
56         struct gfs2_sbd *sdp;           /* incore superblock           */
57         struct rhashtable_iter hti;     /* rhashtable iterator         */
58         struct gfs2_glock *gl;          /* current glock struct        */
59         loff_t last_pos;                /* last position               */
60 };
61
62 typedef void (*glock_examiner) (struct gfs2_glock * gl);
63
64 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target);
65 static void request_demote(struct gfs2_glock *gl, unsigned int state,
66                            unsigned long delay, bool remote);
67
68 static struct dentry *gfs2_root;
69 static LIST_HEAD(lru_list);
70 static atomic_t lru_count = ATOMIC_INIT(0);
71 static DEFINE_SPINLOCK(lru_lock);
72
73 #define GFS2_GL_HASH_SHIFT      15
74 #define GFS2_GL_HASH_SIZE       BIT(GFS2_GL_HASH_SHIFT)
75
76 static const struct rhashtable_params ht_parms = {
77         .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4,
78         .key_len = offsetofend(struct lm_lockname, ln_type),
79         .key_offset = offsetof(struct gfs2_glock, gl_name),
80         .head_offset = offsetof(struct gfs2_glock, gl_node),
81 };
82
83 static struct rhashtable gl_hash_table;
84
85 #define GLOCK_WAIT_TABLE_BITS 12
86 #define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS)
87 static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned;
88
89 struct wait_glock_queue {
90         struct lm_lockname *name;
91         wait_queue_entry_t wait;
92 };
93
94 static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode,
95                                int sync, void *key)
96 {
97         struct wait_glock_queue *wait_glock =
98                 container_of(wait, struct wait_glock_queue, wait);
99         struct lm_lockname *wait_name = wait_glock->name;
100         struct lm_lockname *wake_name = key;
101
102         if (wake_name->ln_sbd != wait_name->ln_sbd ||
103             wake_name->ln_number != wait_name->ln_number ||
104             wake_name->ln_type != wait_name->ln_type)
105                 return 0;
106         return autoremove_wake_function(wait, mode, sync, key);
107 }
108
109 static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name)
110 {
111         u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0);
112
113         return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS);
114 }
115
116 /**
117  * wake_up_glock  -  Wake up waiters on a glock
118  * @gl: the glock
119  */
120 static void wake_up_glock(struct gfs2_glock *gl)
121 {
122         wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
123
124         if (waitqueue_active(wq))
125                 __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
126 }
127
128 static void gfs2_glock_dealloc(struct rcu_head *rcu)
129 {
130         struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu);
131
132         kfree(gl->gl_lksb.sb_lvbptr);
133         if (gl->gl_ops->go_flags & GLOF_ASPACE) {
134                 struct gfs2_glock_aspace *gla =
135                         container_of(gl, struct gfs2_glock_aspace, glock);
136                 kmem_cache_free(gfs2_glock_aspace_cachep, gla);
137         } else
138                 kmem_cache_free(gfs2_glock_cachep, gl);
139 }
140
141 /**
142  * glock_blocked_by_withdraw - determine if we can still use a glock
143  * @gl: the glock
144  *
145  * We need to allow some glocks to be enqueued, dequeued, promoted, and demoted
146  * when we're withdrawn. For example, to maintain metadata integrity, we should
147  * disallow the use of inode and rgrp glocks when withdrawn. Other glocks like
148  * the iopen or freeze glock may be safely used because none of their
149  * metadata goes through the journal. So in general, we should disallow all
150  * glocks that are journaled, and allow all the others. One exception is:
151  * we need to allow our active journal to be promoted and demoted so others
152  * may recover it and we can reacquire it when they're done.
153  */
154 static bool glock_blocked_by_withdraw(struct gfs2_glock *gl)
155 {
156         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
157
158         if (!gfs2_withdrawing_or_withdrawn(sdp))
159                 return false;
160         if (gl->gl_ops->go_flags & GLOF_NONDISK)
161                 return false;
162         if (!sdp->sd_jdesc ||
163             gl->gl_name.ln_number == sdp->sd_jdesc->jd_no_addr)
164                 return false;
165         return true;
166 }
167
168 static void __gfs2_glock_free(struct gfs2_glock *gl)
169 {
170         rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms);
171         smp_mb();
172         wake_up_glock(gl);
173         call_rcu(&gl->gl_rcu, gfs2_glock_dealloc);
174 }
175
176 void gfs2_glock_free(struct gfs2_glock *gl) {
177         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
178
179         __gfs2_glock_free(gl);
180         if (atomic_dec_and_test(&sdp->sd_glock_disposal))
181                 wake_up(&sdp->sd_kill_wait);
182 }
183
184 void gfs2_glock_free_later(struct gfs2_glock *gl) {
185         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
186
187         spin_lock(&lru_lock);
188         list_add(&gl->gl_lru, &sdp->sd_dead_glocks);
189         spin_unlock(&lru_lock);
190         if (atomic_dec_and_test(&sdp->sd_glock_disposal))
191                 wake_up(&sdp->sd_kill_wait);
192 }
193
194 static void gfs2_free_dead_glocks(struct gfs2_sbd *sdp)
195 {
196         struct list_head *list = &sdp->sd_dead_glocks;
197
198         while(!list_empty(list)) {
199                 struct gfs2_glock *gl;
200
201                 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
202                 list_del_init(&gl->gl_lru);
203                 __gfs2_glock_free(gl);
204         }
205 }
206
207 /**
208  * gfs2_glock_hold() - increment reference count on glock
209  * @gl: The glock to hold
210  *
211  */
212
213 struct gfs2_glock *gfs2_glock_hold(struct gfs2_glock *gl)
214 {
215         GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref));
216         lockref_get(&gl->gl_lockref);
217         return gl;
218 }
219
220 static void gfs2_glock_add_to_lru(struct gfs2_glock *gl)
221 {
222         spin_lock(&lru_lock);
223         list_move_tail(&gl->gl_lru, &lru_list);
224
225         if (!test_bit(GLF_LRU, &gl->gl_flags)) {
226                 set_bit(GLF_LRU, &gl->gl_flags);
227                 atomic_inc(&lru_count);
228         }
229
230         spin_unlock(&lru_lock);
231 }
232
233 static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl)
234 {
235         spin_lock(&lru_lock);
236         if (test_bit(GLF_LRU, &gl->gl_flags)) {
237                 list_del_init(&gl->gl_lru);
238                 atomic_dec(&lru_count);
239                 clear_bit(GLF_LRU, &gl->gl_flags);
240         }
241         spin_unlock(&lru_lock);
242 }
243
244 /*
245  * Enqueue the glock on the work queue.  Passes one glock reference on to the
246  * work queue.
247  */
248 static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) {
249         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
250
251         if (!queue_delayed_work(sdp->sd_glock_wq, &gl->gl_work, delay)) {
252                 /*
253                  * We are holding the lockref spinlock, and the work was still
254                  * queued above.  The queued work (glock_work_func) takes that
255                  * spinlock before dropping its glock reference(s), so it
256                  * cannot have dropped them in the meantime.
257                  */
258                 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2);
259                 gl->gl_lockref.count--;
260         }
261 }
262
263 static void __gfs2_glock_put(struct gfs2_glock *gl)
264 {
265         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
266         struct address_space *mapping = gfs2_glock2aspace(gl);
267
268         lockref_mark_dead(&gl->gl_lockref);
269         spin_unlock(&gl->gl_lockref.lock);
270         gfs2_glock_remove_from_lru(gl);
271         GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
272         if (mapping) {
273                 truncate_inode_pages_final(mapping);
274                 if (!gfs2_withdrawing_or_withdrawn(sdp))
275                         GLOCK_BUG_ON(gl, !mapping_empty(mapping));
276         }
277         trace_gfs2_glock_put(gl);
278         sdp->sd_lockstruct.ls_ops->lm_put_lock(gl);
279 }
280
281 static bool __gfs2_glock_put_or_lock(struct gfs2_glock *gl)
282 {
283         if (lockref_put_or_lock(&gl->gl_lockref))
284                 return true;
285         GLOCK_BUG_ON(gl, gl->gl_lockref.count != 1);
286         if (gl->gl_state != LM_ST_UNLOCKED) {
287                 gl->gl_lockref.count--;
288                 gfs2_glock_add_to_lru(gl);
289                 spin_unlock(&gl->gl_lockref.lock);
290                 return true;
291         }
292         return false;
293 }
294
295 /**
296  * gfs2_glock_put() - Decrement reference count on glock
297  * @gl: The glock to put
298  *
299  */
300
301 void gfs2_glock_put(struct gfs2_glock *gl)
302 {
303         if (__gfs2_glock_put_or_lock(gl))
304                 return;
305
306         __gfs2_glock_put(gl);
307 }
308
309 /*
310  * gfs2_glock_put_async - Decrement reference count without sleeping
311  * @gl: The glock to put
312  *
313  * Decrement the reference count on glock immediately unless it is the last
314  * reference.  Defer putting the last reference to work queue context.
315  */
316 void gfs2_glock_put_async(struct gfs2_glock *gl)
317 {
318         if (__gfs2_glock_put_or_lock(gl))
319                 return;
320
321         gfs2_glock_queue_work(gl, 0);
322         spin_unlock(&gl->gl_lockref.lock);
323 }
324
325 /**
326  * may_grant - check if it's ok to grant a new lock
327  * @gl: The glock
328  * @current_gh: One of the current holders of @gl
329  * @gh: The lock request which we wish to grant
330  *
331  * With our current compatibility rules, if a glock has one or more active
332  * holders (HIF_HOLDER flag set), any of those holders can be passed in as
333  * @current_gh; they are all the same as far as compatibility with the new @gh
334  * goes.
335  *
336  * Returns true if it's ok to grant the lock.
337  */
338
339 static inline bool may_grant(struct gfs2_glock *gl,
340                              struct gfs2_holder *current_gh,
341                              struct gfs2_holder *gh)
342 {
343         if (current_gh) {
344                 GLOCK_BUG_ON(gl, !test_bit(HIF_HOLDER, &current_gh->gh_iflags));
345
346                 switch(current_gh->gh_state) {
347                 case LM_ST_EXCLUSIVE:
348                         /*
349                          * Here we make a special exception to grant holders
350                          * who agree to share the EX lock with other holders
351                          * who also have the bit set. If the original holder
352                          * has the LM_FLAG_NODE_SCOPE bit set, we grant more
353                          * holders with the bit set.
354                          */
355                         return gh->gh_state == LM_ST_EXCLUSIVE &&
356                                (current_gh->gh_flags & LM_FLAG_NODE_SCOPE) &&
357                                (gh->gh_flags & LM_FLAG_NODE_SCOPE);
358
359                 case LM_ST_SHARED:
360                 case LM_ST_DEFERRED:
361                         return gh->gh_state == current_gh->gh_state;
362
363                 default:
364                         return false;
365                 }
366         }
367
368         if (gl->gl_state == gh->gh_state)
369                 return true;
370         if (gh->gh_flags & GL_EXACT)
371                 return false;
372         if (gl->gl_state == LM_ST_EXCLUSIVE) {
373                 return gh->gh_state == LM_ST_SHARED ||
374                        gh->gh_state == LM_ST_DEFERRED;
375         }
376         if (gh->gh_flags & LM_FLAG_ANY)
377                 return gl->gl_state != LM_ST_UNLOCKED;
378         return false;
379 }
380
381 static void gfs2_holder_wake(struct gfs2_holder *gh)
382 {
383         clear_bit(HIF_WAIT, &gh->gh_iflags);
384         smp_mb__after_atomic();
385         wake_up_bit(&gh->gh_iflags, HIF_WAIT);
386         if (gh->gh_flags & GL_ASYNC) {
387                 struct gfs2_sbd *sdp = gh->gh_gl->gl_name.ln_sbd;
388
389                 wake_up(&sdp->sd_async_glock_wait);
390         }
391 }
392
393 /**
394  * do_error - Something unexpected has happened during a lock request
395  * @gl: The glock
396  * @ret: The status from the DLM
397  */
398
399 static void do_error(struct gfs2_glock *gl, const int ret)
400 {
401         struct gfs2_holder *gh, *tmp;
402
403         list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) {
404                 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
405                         continue;
406                 if (ret & LM_OUT_ERROR)
407                         gh->gh_error = -EIO;
408                 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
409                         gh->gh_error = GLR_TRYFAILED;
410                 else
411                         continue;
412                 list_del_init(&gh->gh_list);
413                 trace_gfs2_glock_queue(gh, 0);
414                 gfs2_holder_wake(gh);
415         }
416 }
417
418 /**
419  * find_first_holder - find the first "holder" gh
420  * @gl: the glock
421  */
422
423 static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
424 {
425         struct gfs2_holder *gh;
426
427         if (!list_empty(&gl->gl_holders)) {
428                 gh = list_first_entry(&gl->gl_holders, struct gfs2_holder,
429                                       gh_list);
430                 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
431                         return gh;
432         }
433         return NULL;
434 }
435
436 /*
437  * gfs2_instantiate - Call the glops instantiate function
438  * @gh: The glock holder
439  *
440  * Returns: 0 if instantiate was successful, or error.
441  */
442 int gfs2_instantiate(struct gfs2_holder *gh)
443 {
444         struct gfs2_glock *gl = gh->gh_gl;
445         const struct gfs2_glock_operations *glops = gl->gl_ops;
446         int ret;
447
448 again:
449         if (!test_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags))
450                 goto done;
451
452         /*
453          * Since we unlock the lockref lock, we set a flag to indicate
454          * instantiate is in progress.
455          */
456         if (test_and_set_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags)) {
457                 wait_on_bit(&gl->gl_flags, GLF_INSTANTIATE_IN_PROG,
458                             TASK_UNINTERRUPTIBLE);
459                 /*
460                  * Here we just waited for a different instantiate to finish.
461                  * But that may not have been successful, as when a process
462                  * locks an inode glock _before_ it has an actual inode to
463                  * instantiate into. So we check again. This process might
464                  * have an inode to instantiate, so might be successful.
465                  */
466                 goto again;
467         }
468
469         ret = glops->go_instantiate(gl);
470         if (!ret)
471                 clear_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags);
472         clear_and_wake_up_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags);
473         if (ret)
474                 return ret;
475
476 done:
477         if (glops->go_held)
478                 return glops->go_held(gh);
479         return 0;
480 }
481
482 /**
483  * do_promote - promote as many requests as possible on the current queue
484  * @gl: The glock
485  * 
486  * Returns true on success (i.e., progress was made or there are no waiters).
487  */
488
489 static bool do_promote(struct gfs2_glock *gl)
490 {
491         struct gfs2_holder *gh, *current_gh;
492
493         current_gh = find_first_holder(gl);
494         list_for_each_entry(gh, &gl->gl_holders, gh_list) {
495                 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
496                         continue;
497                 if (!may_grant(gl, current_gh, gh)) {
498                         /*
499                          * If we get here, it means we may not grant this
500                          * holder for some reason. If this holder is at the
501                          * head of the list, it means we have a blocked holder
502                          * at the head, so return false.
503                          */
504                         if (list_is_first(&gh->gh_list, &gl->gl_holders))
505                                 return false;
506                         do_error(gl, 0);
507                         break;
508                 }
509                 set_bit(HIF_HOLDER, &gh->gh_iflags);
510                 trace_gfs2_promote(gh);
511                 gfs2_holder_wake(gh);
512                 if (!current_gh)
513                         current_gh = gh;
514         }
515         return true;
516 }
517
518 /**
519  * find_first_waiter - find the first gh that's waiting for the glock
520  * @gl: the glock
521  */
522
523 static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl)
524 {
525         struct gfs2_holder *gh;
526
527         list_for_each_entry(gh, &gl->gl_holders, gh_list) {
528                 if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
529                         return gh;
530         }
531         return NULL;
532 }
533
534 /**
535  * find_last_waiter - find the last gh that's waiting for the glock
536  * @gl: the glock
537  *
538  * This also is a fast way of finding out if there are any waiters.
539  */
540
541 static inline struct gfs2_holder *find_last_waiter(const struct gfs2_glock *gl)
542 {
543         struct gfs2_holder *gh;
544
545         if (list_empty(&gl->gl_holders))
546                 return NULL;
547         gh = list_last_entry(&gl->gl_holders, struct gfs2_holder, gh_list);
548         return test_bit(HIF_HOLDER, &gh->gh_iflags) ? NULL : gh;
549 }
550
551 /**
552  * state_change - record that the glock is now in a different state
553  * @gl: the glock
554  * @new_state: the new state
555  */
556
557 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
558 {
559         if (new_state != gl->gl_target)
560                 /* shorten our minimum hold time */
561                 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR,
562                                        GL_GLOCK_MIN_HOLD);
563         gl->gl_state = new_state;
564         gl->gl_tchange = jiffies;
565 }
566
567 static void gfs2_set_demote(int nr, struct gfs2_glock *gl)
568 {
569         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
570
571         set_bit(nr, &gl->gl_flags);
572         smp_mb();
573         wake_up(&sdp->sd_async_glock_wait);
574 }
575
576 static void gfs2_demote_wake(struct gfs2_glock *gl)
577 {
578         gl->gl_demote_state = LM_ST_EXCLUSIVE;
579         clear_bit(GLF_DEMOTE, &gl->gl_flags);
580         smp_mb__after_atomic();
581         wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
582 }
583
584 /**
585  * finish_xmote - The DLM has replied to one of our lock requests
586  * @gl: The glock
587  * @ret: The status from the DLM
588  *
589  */
590
591 static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
592 {
593         const struct gfs2_glock_operations *glops = gl->gl_ops;
594         struct gfs2_holder *gh;
595         unsigned state = ret & LM_OUT_ST_MASK;
596
597         trace_gfs2_glock_state_change(gl, state);
598         state_change(gl, state);
599         gh = find_first_waiter(gl);
600
601         /* Demote to UN request arrived during demote to SH or DF */
602         if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
603             state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED)
604                 gl->gl_target = LM_ST_UNLOCKED;
605
606         /* Check for state != intended state */
607         if (unlikely(state != gl->gl_target)) {
608                 if (gh && (ret & LM_OUT_CANCELED))
609                         gfs2_holder_wake(gh);
610                 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
611                         /* move to back of queue and try next entry */
612                         if (ret & LM_OUT_CANCELED) {
613                                 list_move_tail(&gh->gh_list, &gl->gl_holders);
614                                 gh = find_first_waiter(gl);
615                                 gl->gl_target = gh->gh_state;
616                                 if (do_promote(gl))
617                                         goto out;
618                                 goto retry;
619                         }
620                         /* Some error or failed "try lock" - report it */
621                         if ((ret & LM_OUT_ERROR) ||
622                             (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
623                                 gl->gl_target = gl->gl_state;
624                                 do_error(gl, ret);
625                                 goto out;
626                         }
627                 }
628                 switch(state) {
629                 /* Unlocked due to conversion deadlock, try again */
630                 case LM_ST_UNLOCKED:
631 retry:
632                         do_xmote(gl, gh, gl->gl_target);
633                         break;
634                 /* Conversion fails, unlock and try again */
635                 case LM_ST_SHARED:
636                 case LM_ST_DEFERRED:
637                         do_xmote(gl, gh, LM_ST_UNLOCKED);
638                         break;
639                 default: /* Everything else */
640                         fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n",
641                                gl->gl_target, state);
642                         GLOCK_BUG_ON(gl, 1);
643                 }
644                 return;
645         }
646
647         /* Fast path - we got what we asked for */
648         if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags))
649                 gfs2_demote_wake(gl);
650         if (state != LM_ST_UNLOCKED) {
651                 if (glops->go_xmote_bh) {
652                         int rv;
653
654                         spin_unlock(&gl->gl_lockref.lock);
655                         rv = glops->go_xmote_bh(gl);
656                         spin_lock(&gl->gl_lockref.lock);
657                         if (rv) {
658                                 do_error(gl, rv);
659                                 goto out;
660                         }
661                 }
662                 do_promote(gl);
663         }
664 out:
665         clear_bit(GLF_LOCK, &gl->gl_flags);
666 }
667
668 static bool is_system_glock(struct gfs2_glock *gl)
669 {
670         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
671         struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
672
673         if (gl == m_ip->i_gl)
674                 return true;
675         return false;
676 }
677
678 /**
679  * do_xmote - Calls the DLM to change the state of a lock
680  * @gl: The lock state
681  * @gh: The holder (only for promotes)
682  * @target: The target lock state
683  *
684  */
685
686 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh,
687                                          unsigned int target)
688 __releases(&gl->gl_lockref.lock)
689 __acquires(&gl->gl_lockref.lock)
690 {
691         const struct gfs2_glock_operations *glops = gl->gl_ops;
692         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
693         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
694         unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0);
695         int ret;
696
697         if (target != LM_ST_UNLOCKED && glock_blocked_by_withdraw(gl) &&
698             gh && !(gh->gh_flags & LM_FLAG_NOEXP))
699                 goto skip_inval;
700
701         lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP);
702         GLOCK_BUG_ON(gl, gl->gl_state == target);
703         GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target);
704         if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) &&
705             glops->go_inval) {
706                 /*
707                  * If another process is already doing the invalidate, let that
708                  * finish first.  The glock state machine will get back to this
709                  * holder again later.
710                  */
711                 if (test_and_set_bit(GLF_INVALIDATE_IN_PROGRESS,
712                                      &gl->gl_flags))
713                         return;
714                 do_error(gl, 0); /* Fail queued try locks */
715         }
716         gl->gl_req = target;
717         set_bit(GLF_BLOCKING, &gl->gl_flags);
718         if ((gl->gl_req == LM_ST_UNLOCKED) ||
719             (gl->gl_state == LM_ST_EXCLUSIVE) ||
720             (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB)))
721                 clear_bit(GLF_BLOCKING, &gl->gl_flags);
722         if (!glops->go_inval && !glops->go_sync)
723                 goto skip_inval;
724
725         spin_unlock(&gl->gl_lockref.lock);
726         if (glops->go_sync) {
727                 ret = glops->go_sync(gl);
728                 /* If we had a problem syncing (due to io errors or whatever,
729                  * we should not invalidate the metadata or tell dlm to
730                  * release the glock to other nodes.
731                  */
732                 if (ret) {
733                         if (cmpxchg(&sdp->sd_log_error, 0, ret)) {
734                                 fs_err(sdp, "Error %d syncing glock \n", ret);
735                                 gfs2_dump_glock(NULL, gl, true);
736                         }
737                         spin_lock(&gl->gl_lockref.lock);
738                         goto skip_inval;
739                 }
740         }
741         if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) {
742                 /*
743                  * The call to go_sync should have cleared out the ail list.
744                  * If there are still items, we have a problem. We ought to
745                  * withdraw, but we can't because the withdraw code also uses
746                  * glocks. Warn about the error, dump the glock, then fall
747                  * through and wait for logd to do the withdraw for us.
748                  */
749                 if ((atomic_read(&gl->gl_ail_count) != 0) &&
750                     (!cmpxchg(&sdp->sd_log_error, 0, -EIO))) {
751                         gfs2_glock_assert_warn(gl,
752                                                !atomic_read(&gl->gl_ail_count));
753                         gfs2_dump_glock(NULL, gl, true);
754                 }
755                 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA);
756                 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
757         }
758         spin_lock(&gl->gl_lockref.lock);
759
760 skip_inval:
761         gl->gl_lockref.count++;
762         /*
763          * Check for an error encountered since we called go_sync and go_inval.
764          * If so, we can't withdraw from the glock code because the withdraw
765          * code itself uses glocks (see function signal_our_withdraw) to
766          * change the mount to read-only. Most importantly, we must not call
767          * dlm to unlock the glock until the journal is in a known good state
768          * (after journal replay) otherwise other nodes may use the object
769          * (rgrp or dinode) and then later, journal replay will corrupt the
770          * file system. The best we can do here is wait for the logd daemon
771          * to see sd_log_error and withdraw, and in the meantime, requeue the
772          * work for later.
773          *
774          * We make a special exception for some system glocks, such as the
775          * system statfs inode glock, which needs to be granted before the
776          * gfs2_quotad daemon can exit, and that exit needs to finish before
777          * we can unmount the withdrawn file system.
778          *
779          * However, if we're just unlocking the lock (say, for unmount, when
780          * gfs2_gl_hash_clear calls clear_glock) and recovery is complete
781          * then it's okay to tell dlm to unlock it.
782          */
783         if (unlikely(sdp->sd_log_error) && !gfs2_withdrawing_or_withdrawn(sdp))
784                 gfs2_withdraw_delayed(sdp);
785         if (glock_blocked_by_withdraw(gl) &&
786             (target != LM_ST_UNLOCKED ||
787              test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags))) {
788                 if (!is_system_glock(gl)) {
789                         request_demote(gl, LM_ST_UNLOCKED, 0, false);
790                         /*
791                          * Ordinarily, we would call dlm and its callback would call
792                          * finish_xmote, which would call state_change() to the new state.
793                          * Since we withdrew, we won't call dlm, so call state_change
794                          * manually, but to the UNLOCKED state we desire.
795                          */
796                         state_change(gl, LM_ST_UNLOCKED);
797                         /*
798                          * We skip telling dlm to do the locking, so we won't get a
799                          * reply that would otherwise clear GLF_LOCK. So we clear it here.
800                          */
801                         clear_bit(GLF_LOCK, &gl->gl_flags);
802                         clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
803                         gfs2_glock_queue_work(gl, GL_GLOCK_DFT_HOLD);
804                         return;
805                 } else {
806                         clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags);
807                 }
808         }
809
810         if (ls->ls_ops->lm_lock) {
811                 spin_unlock(&gl->gl_lockref.lock);
812                 ret = ls->ls_ops->lm_lock(gl, target, lck_flags);
813                 spin_lock(&gl->gl_lockref.lock);
814
815                 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED &&
816                     target == LM_ST_UNLOCKED &&
817                     test_bit(DFL_UNMOUNT, &ls->ls_recover_flags)) {
818                         /*
819                          * The lockspace has been released and the lock has
820                          * been unlocked implicitly.
821                          */
822                 } else if (ret) {
823                         fs_err(sdp, "lm_lock ret %d\n", ret);
824                         target = gl->gl_state | LM_OUT_ERROR;
825                 } else {
826                         /* The operation will be completed asynchronously. */
827                         return;
828                 }
829         }
830
831         /* Complete the operation now. */
832         finish_xmote(gl, target);
833         gfs2_glock_queue_work(gl, 0);
834 }
835
836 /**
837  * run_queue - do all outstanding tasks related to a glock
838  * @gl: The glock in question
839  * @nonblock: True if we must not block in run_queue
840  *
841  */
842
843 static void run_queue(struct gfs2_glock *gl, const int nonblock)
844 __releases(&gl->gl_lockref.lock)
845 __acquires(&gl->gl_lockref.lock)
846 {
847         struct gfs2_holder *gh = NULL;
848
849         if (test_bit(GLF_LOCK, &gl->gl_flags))
850                 return;
851         set_bit(GLF_LOCK, &gl->gl_flags);
852
853         GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags));
854
855         if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
856             gl->gl_demote_state != gl->gl_state) {
857                 if (find_first_holder(gl))
858                         goto out_unlock;
859                 if (nonblock)
860                         goto out_sched;
861                 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
862                 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
863                 gl->gl_target = gl->gl_demote_state;
864         } else {
865                 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
866                         gfs2_demote_wake(gl);
867                 if (do_promote(gl))
868                         goto out_unlock;
869                 gh = find_first_waiter(gl);
870                 gl->gl_target = gh->gh_state;
871                 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
872                         do_error(gl, 0); /* Fail queued try locks */
873         }
874         do_xmote(gl, gh, gl->gl_target);
875         return;
876
877 out_sched:
878         clear_bit(GLF_LOCK, &gl->gl_flags);
879         smp_mb__after_atomic();
880         gl->gl_lockref.count++;
881         gfs2_glock_queue_work(gl, 0);
882         return;
883
884 out_unlock:
885         clear_bit(GLF_LOCK, &gl->gl_flags);
886         smp_mb__after_atomic();
887 }
888
889 /**
890  * glock_set_object - set the gl_object field of a glock
891  * @gl: the glock
892  * @object: the object
893  */
894 void glock_set_object(struct gfs2_glock *gl, void *object)
895 {
896         void *prev_object;
897
898         spin_lock(&gl->gl_lockref.lock);
899         prev_object = gl->gl_object;
900         gl->gl_object = object;
901         spin_unlock(&gl->gl_lockref.lock);
902         if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == NULL)) {
903                 pr_warn("glock=%u/%llx\n",
904                         gl->gl_name.ln_type,
905                         (unsigned long long)gl->gl_name.ln_number);
906                 gfs2_dump_glock(NULL, gl, true);
907         }
908 }
909
910 /**
911  * glock_clear_object - clear the gl_object field of a glock
912  * @gl: the glock
913  * @object: object the glock currently points at
914  */
915 void glock_clear_object(struct gfs2_glock *gl, void *object)
916 {
917         void *prev_object;
918
919         spin_lock(&gl->gl_lockref.lock);
920         prev_object = gl->gl_object;
921         gl->gl_object = NULL;
922         spin_unlock(&gl->gl_lockref.lock);
923         if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == object)) {
924                 pr_warn("glock=%u/%llx\n",
925                         gl->gl_name.ln_type,
926                         (unsigned long long)gl->gl_name.ln_number);
927                 gfs2_dump_glock(NULL, gl, true);
928         }
929 }
930
931 void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation)
932 {
933         struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
934
935         if (ri->ri_magic == 0)
936                 ri->ri_magic = cpu_to_be32(GFS2_MAGIC);
937         if (ri->ri_magic == cpu_to_be32(GFS2_MAGIC))
938                 ri->ri_generation_deleted = cpu_to_be64(generation);
939 }
940
941 bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation)
942 {
943         struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr;
944
945         if (ri->ri_magic != cpu_to_be32(GFS2_MAGIC))
946                 return false;
947         return generation <= be64_to_cpu(ri->ri_generation_deleted);
948 }
949
950 static void gfs2_glock_poke(struct gfs2_glock *gl)
951 {
952         int flags = LM_FLAG_TRY_1CB | LM_FLAG_ANY | GL_SKIP;
953         struct gfs2_holder gh;
954         int error;
955
956         __gfs2_holder_init(gl, LM_ST_SHARED, flags, &gh, _RET_IP_);
957         error = gfs2_glock_nq(&gh);
958         if (!error)
959                 gfs2_glock_dq(&gh);
960         gfs2_holder_uninit(&gh);
961 }
962
963 static void gfs2_try_evict(struct gfs2_glock *gl)
964 {
965         struct gfs2_inode *ip;
966
967         /*
968          * If there is contention on the iopen glock and we have an inode, try
969          * to grab and release the inode so that it can be evicted.  The
970          * GIF_DEFER_DELETE flag indicates to gfs2_evict_inode() that the inode
971          * should not be deleted locally.  This will allow the remote node to
972          * go ahead and delete the inode without us having to do it, which will
973          * avoid rgrp glock thrashing.
974          *
975          * The remote node is likely still holding the corresponding inode
976          * glock, so it will run before we get to verify that the delete has
977          * happened below.  (Verification is triggered by the call to
978          * gfs2_queue_verify_delete() in gfs2_evict_inode().)
979          */
980         spin_lock(&gl->gl_lockref.lock);
981         ip = gl->gl_object;
982         if (ip && !igrab(&ip->i_inode))
983                 ip = NULL;
984         spin_unlock(&gl->gl_lockref.lock);
985         if (ip) {
986                 wait_on_inode(&ip->i_inode);
987                 if (is_bad_inode(&ip->i_inode)) {
988                         iput(&ip->i_inode);
989                         ip = NULL;
990                 }
991         }
992         if (ip) {
993                 set_bit(GIF_DEFER_DELETE, &ip->i_flags);
994                 d_prune_aliases(&ip->i_inode);
995                 iput(&ip->i_inode);
996
997                 /* If the inode was evicted, gl->gl_object will now be NULL. */
998                 spin_lock(&gl->gl_lockref.lock);
999                 ip = gl->gl_object;
1000                 if (ip) {
1001                         clear_bit(GIF_DEFER_DELETE, &ip->i_flags);
1002                         if (!igrab(&ip->i_inode))
1003                                 ip = NULL;
1004                 }
1005                 spin_unlock(&gl->gl_lockref.lock);
1006                 if (ip) {
1007                         gfs2_glock_poke(ip->i_gl);
1008                         iput(&ip->i_inode);
1009                 }
1010         }
1011 }
1012
1013 bool gfs2_queue_try_to_evict(struct gfs2_glock *gl)
1014 {
1015         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1016
1017         if (test_and_set_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
1018                 return false;
1019         return !mod_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, 0);
1020 }
1021
1022 bool gfs2_queue_verify_delete(struct gfs2_glock *gl, bool later)
1023 {
1024         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1025         unsigned long delay;
1026
1027         if (test_and_set_bit(GLF_VERIFY_DELETE, &gl->gl_flags))
1028                 return false;
1029         delay = later ? HZ + get_random_long() % (HZ * 9) : 0;
1030         return queue_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, delay);
1031 }
1032
1033 static void delete_work_func(struct work_struct *work)
1034 {
1035         struct delayed_work *dwork = to_delayed_work(work);
1036         struct gfs2_glock *gl = container_of(dwork, struct gfs2_glock, gl_delete);
1037         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1038         bool verify_delete = test_and_clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
1039
1040         if (test_and_clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags))
1041                 gfs2_try_evict(gl);
1042
1043         if (verify_delete) {
1044                 u64 no_addr = gl->gl_name.ln_number;
1045                 struct inode *inode;
1046
1047                 inode = gfs2_lookup_by_inum(sdp, no_addr, gl->gl_no_formal_ino,
1048                                             GFS2_BLKST_UNLINKED);
1049                 if (IS_ERR(inode)) {
1050                         if (PTR_ERR(inode) == -EAGAIN &&
1051                             !test_bit(SDF_KILL, &sdp->sd_flags) &&
1052                             gfs2_queue_verify_delete(gl, true))
1053                                 return;
1054                 } else {
1055                         d_prune_aliases(inode);
1056                         iput(inode);
1057                 }
1058         }
1059
1060         gfs2_glock_put(gl);
1061 }
1062
1063 static void glock_work_func(struct work_struct *work)
1064 {
1065         unsigned long delay = 0;
1066         struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
1067         unsigned int drop_refs = 1;
1068
1069         spin_lock(&gl->gl_lockref.lock);
1070         if (test_bit(GLF_HAVE_REPLY, &gl->gl_flags)) {
1071                 clear_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1072                 finish_xmote(gl, gl->gl_reply);
1073                 drop_refs++;
1074         }
1075         if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1076             gl->gl_state != LM_ST_UNLOCKED &&
1077             gl->gl_demote_state != LM_ST_EXCLUSIVE) {
1078                 if (gl->gl_name.ln_type == LM_TYPE_INODE) {
1079                         unsigned long holdtime, now = jiffies;
1080
1081                         holdtime = gl->gl_tchange + gl->gl_hold_time;
1082                         if (time_before(now, holdtime))
1083                                 delay = holdtime - now;
1084                 }
1085
1086                 if (!delay) {
1087                         clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
1088                         gfs2_set_demote(GLF_DEMOTE, gl);
1089                 }
1090         }
1091         run_queue(gl, 0);
1092         if (delay) {
1093                 /* Keep one glock reference for the work we requeue. */
1094                 drop_refs--;
1095                 gfs2_glock_queue_work(gl, delay);
1096         }
1097
1098         /* Drop the remaining glock references manually. */
1099         GLOCK_BUG_ON(gl, gl->gl_lockref.count < drop_refs);
1100         gl->gl_lockref.count -= drop_refs;
1101         if (!gl->gl_lockref.count) {
1102                 if (gl->gl_state == LM_ST_UNLOCKED) {
1103                         __gfs2_glock_put(gl);
1104                         return;
1105                 }
1106                 gfs2_glock_add_to_lru(gl);
1107         }
1108         spin_unlock(&gl->gl_lockref.lock);
1109 }
1110
1111 static struct gfs2_glock *find_insert_glock(struct lm_lockname *name,
1112                                             struct gfs2_glock *new)
1113 {
1114         struct wait_glock_queue wait;
1115         wait_queue_head_t *wq = glock_waitqueue(name);
1116         struct gfs2_glock *gl;
1117
1118         wait.name = name;
1119         init_wait(&wait.wait);
1120         wait.wait.func = glock_wake_function;
1121
1122 again:
1123         prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1124         rcu_read_lock();
1125         if (new) {
1126                 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table,
1127                         &new->gl_node, ht_parms);
1128                 if (IS_ERR(gl))
1129                         goto out;
1130         } else {
1131                 gl = rhashtable_lookup_fast(&gl_hash_table,
1132                         name, ht_parms);
1133         }
1134         if (gl && !lockref_get_not_dead(&gl->gl_lockref)) {
1135                 rcu_read_unlock();
1136                 schedule();
1137                 goto again;
1138         }
1139 out:
1140         rcu_read_unlock();
1141         finish_wait(wq, &wait.wait);
1142         if (gl)
1143                 gfs2_glock_remove_from_lru(gl);
1144         return gl;
1145 }
1146
1147 /**
1148  * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
1149  * @sdp: The GFS2 superblock
1150  * @number: the lock number
1151  * @glops: The glock_operations to use
1152  * @create: If 0, don't create the glock if it doesn't exist
1153  * @glp: the glock is returned here
1154  *
1155  * This does not lock a glock, just finds/creates structures for one.
1156  *
1157  * Returns: errno
1158  */
1159
1160 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
1161                    const struct gfs2_glock_operations *glops, int create,
1162                    struct gfs2_glock **glp)
1163 {
1164         struct super_block *s = sdp->sd_vfs;
1165         struct lm_lockname name = { .ln_number = number,
1166                                     .ln_type = glops->go_type,
1167                                     .ln_sbd = sdp };
1168         struct gfs2_glock *gl, *tmp;
1169         struct address_space *mapping;
1170
1171         gl = find_insert_glock(&name, NULL);
1172         if (gl)
1173                 goto found;
1174         if (!create)
1175                 return -ENOENT;
1176
1177         if (glops->go_flags & GLOF_ASPACE) {
1178                 struct gfs2_glock_aspace *gla =
1179                         kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_NOFS);
1180                 if (!gla)
1181                         return -ENOMEM;
1182                 gl = &gla->glock;
1183         } else {
1184                 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_NOFS);
1185                 if (!gl)
1186                         return -ENOMEM;
1187         }
1188         memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
1189         gl->gl_ops = glops;
1190
1191         if (glops->go_flags & GLOF_LVB) {
1192                 gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
1193                 if (!gl->gl_lksb.sb_lvbptr) {
1194                         gfs2_glock_dealloc(&gl->gl_rcu);
1195                         return -ENOMEM;
1196                 }
1197         }
1198
1199         atomic_inc(&sdp->sd_glock_disposal);
1200         gl->gl_node.next = NULL;
1201         gl->gl_flags = BIT(GLF_INITIAL);
1202         if (glops->go_instantiate)
1203                 gl->gl_flags |= BIT(GLF_INSTANTIATE_NEEDED);
1204         gl->gl_name = name;
1205         lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass);
1206         gl->gl_lockref.count = 1;
1207         gl->gl_state = LM_ST_UNLOCKED;
1208         gl->gl_target = LM_ST_UNLOCKED;
1209         gl->gl_demote_state = LM_ST_EXCLUSIVE;
1210         gl->gl_dstamp = 0;
1211         preempt_disable();
1212         /* We use the global stats to estimate the initial per-glock stats */
1213         gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type];
1214         preempt_enable();
1215         gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0;
1216         gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0;
1217         gl->gl_tchange = jiffies;
1218         gl->gl_object = NULL;
1219         gl->gl_hold_time = GL_GLOCK_DFT_HOLD;
1220         INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
1221         if (gl->gl_name.ln_type == LM_TYPE_IOPEN)
1222                 INIT_DELAYED_WORK(&gl->gl_delete, delete_work_func);
1223
1224         mapping = gfs2_glock2aspace(gl);
1225         if (mapping) {
1226                 mapping->a_ops = &gfs2_meta_aops;
1227                 mapping->host = s->s_bdev->bd_mapping->host;
1228                 mapping->flags = 0;
1229                 mapping_set_gfp_mask(mapping, GFP_NOFS);
1230                 mapping->i_private_data = NULL;
1231                 mapping->writeback_index = 0;
1232         }
1233
1234         tmp = find_insert_glock(&name, gl);
1235         if (tmp) {
1236                 gfs2_glock_dealloc(&gl->gl_rcu);
1237                 if (atomic_dec_and_test(&sdp->sd_glock_disposal))
1238                         wake_up(&sdp->sd_kill_wait);
1239
1240                 if (IS_ERR(tmp))
1241                         return PTR_ERR(tmp);
1242                 gl = tmp;
1243         }
1244
1245 found:
1246         *glp = gl;
1247         return 0;
1248 }
1249
1250 /**
1251  * __gfs2_holder_init - initialize a struct gfs2_holder in the default way
1252  * @gl: the glock
1253  * @state: the state we're requesting
1254  * @flags: the modifier flags
1255  * @gh: the holder structure
1256  *
1257  */
1258
1259 void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
1260                         struct gfs2_holder *gh, unsigned long ip)
1261 {
1262         INIT_LIST_HEAD(&gh->gh_list);
1263         gh->gh_gl = gfs2_glock_hold(gl);
1264         gh->gh_ip = ip;
1265         gh->gh_owner_pid = get_pid(task_pid(current));
1266         gh->gh_state = state;
1267         gh->gh_flags = flags;
1268         gh->gh_iflags = 0;
1269 }
1270
1271 /**
1272  * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
1273  * @state: the state we're requesting
1274  * @flags: the modifier flags
1275  * @gh: the holder structure
1276  *
1277  * Don't mess with the glock.
1278  *
1279  */
1280
1281 void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh)
1282 {
1283         gh->gh_state = state;
1284         gh->gh_flags = flags;
1285         gh->gh_iflags = 0;
1286         gh->gh_ip = _RET_IP_;
1287         put_pid(gh->gh_owner_pid);
1288         gh->gh_owner_pid = get_pid(task_pid(current));
1289 }
1290
1291 /**
1292  * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
1293  * @gh: the holder structure
1294  *
1295  */
1296
1297 void gfs2_holder_uninit(struct gfs2_holder *gh)
1298 {
1299         put_pid(gh->gh_owner_pid);
1300         gfs2_glock_put(gh->gh_gl);
1301         gfs2_holder_mark_uninitialized(gh);
1302         gh->gh_ip = 0;
1303 }
1304
1305 static void gfs2_glock_update_hold_time(struct gfs2_glock *gl,
1306                                         unsigned long start_time)
1307 {
1308         /* Have we waited longer that a second? */
1309         if (time_after(jiffies, start_time + HZ)) {
1310                 /* Lengthen the minimum hold time. */
1311                 gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR,
1312                                        GL_GLOCK_MAX_HOLD);
1313         }
1314 }
1315
1316 /**
1317  * gfs2_glock_holder_ready - holder is ready and its error code can be collected
1318  * @gh: the glock holder
1319  *
1320  * Called when a glock holder no longer needs to be waited for because it is
1321  * now either held (HIF_HOLDER set; gh_error == 0), or acquiring the lock has
1322  * failed (gh_error != 0).
1323  */
1324
1325 int gfs2_glock_holder_ready(struct gfs2_holder *gh)
1326 {
1327         if (gh->gh_error || (gh->gh_flags & GL_SKIP))
1328                 return gh->gh_error;
1329         gh->gh_error = gfs2_instantiate(gh);
1330         if (gh->gh_error)
1331                 gfs2_glock_dq(gh);
1332         return gh->gh_error;
1333 }
1334
1335 /**
1336  * gfs2_glock_wait - wait on a glock acquisition
1337  * @gh: the glock holder
1338  *
1339  * Returns: 0 on success
1340  */
1341
1342 int gfs2_glock_wait(struct gfs2_holder *gh)
1343 {
1344         unsigned long start_time = jiffies;
1345
1346         might_sleep();
1347         wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1348         gfs2_glock_update_hold_time(gh->gh_gl, start_time);
1349         return gfs2_glock_holder_ready(gh);
1350 }
1351
1352 static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs)
1353 {
1354         int i;
1355
1356         for (i = 0; i < num_gh; i++)
1357                 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags))
1358                         return 1;
1359         return 0;
1360 }
1361
1362 /**
1363  * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions
1364  * @num_gh: the number of holders in the array
1365  * @ghs: the glock holder array
1366  *
1367  * Returns: 0 on success, meaning all glocks have been granted and are held.
1368  *          -ESTALE if the request timed out, meaning all glocks were released,
1369  *          and the caller should retry the operation.
1370  */
1371
1372 int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs)
1373 {
1374         struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd;
1375         int i, ret = 0, timeout = 0;
1376         unsigned long start_time = jiffies;
1377
1378         might_sleep();
1379         /*
1380          * Total up the (minimum hold time * 2) of all glocks and use that to
1381          * determine the max amount of time we should wait.
1382          */
1383         for (i = 0; i < num_gh; i++)
1384                 timeout += ghs[i].gh_gl->gl_hold_time << 1;
1385
1386         if (!wait_event_timeout(sdp->sd_async_glock_wait,
1387                                 !glocks_pending(num_gh, ghs), timeout)) {
1388                 ret = -ESTALE; /* request timed out. */
1389                 goto out;
1390         }
1391
1392         for (i = 0; i < num_gh; i++) {
1393                 struct gfs2_holder *gh = &ghs[i];
1394                 int ret2;
1395
1396                 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1397                         gfs2_glock_update_hold_time(gh->gh_gl,
1398                                                     start_time);
1399                 }
1400                 ret2 = gfs2_glock_holder_ready(gh);
1401                 if (!ret)
1402                         ret = ret2;
1403         }
1404
1405 out:
1406         if (ret) {
1407                 for (i = 0; i < num_gh; i++) {
1408                         struct gfs2_holder *gh = &ghs[i];
1409
1410                         gfs2_glock_dq(gh);
1411                 }
1412         }
1413         return ret;
1414 }
1415
1416 /**
1417  * request_demote - process a demote request
1418  * @gl: the glock
1419  * @state: the state the caller wants us to change to
1420  * @delay: zero to demote immediately; otherwise pending demote
1421  * @remote: true if this came from a different cluster node
1422  *
1423  * There are only two requests that we are going to see in actual
1424  * practise: LM_ST_SHARED and LM_ST_UNLOCKED
1425  */
1426
1427 static void request_demote(struct gfs2_glock *gl, unsigned int state,
1428                            unsigned long delay, bool remote)
1429 {
1430         gfs2_set_demote(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, gl);
1431         if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
1432                 gl->gl_demote_state = state;
1433                 gl->gl_demote_time = jiffies;
1434         } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
1435                         gl->gl_demote_state != state) {
1436                 gl->gl_demote_state = LM_ST_UNLOCKED;
1437         }
1438         if (gl->gl_ops->go_callback)
1439                 gl->gl_ops->go_callback(gl, remote);
1440         trace_gfs2_demote_rq(gl, remote);
1441 }
1442
1443 void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
1444 {
1445         struct va_format vaf;
1446         va_list args;
1447
1448         va_start(args, fmt);
1449
1450         if (seq) {
1451                 seq_vprintf(seq, fmt, args);
1452         } else {
1453                 vaf.fmt = fmt;
1454                 vaf.va = &args;
1455
1456                 pr_err("%pV", &vaf);
1457         }
1458
1459         va_end(args);
1460 }
1461
1462 static inline bool pid_is_meaningful(const struct gfs2_holder *gh)
1463 {
1464         if (!(gh->gh_flags & GL_NOPID))
1465                 return true;
1466         if (gh->gh_state == LM_ST_UNLOCKED)
1467                 return true;
1468         return false;
1469 }
1470
1471 /**
1472  * add_to_queue - Add a holder to the wait queue (but look for recursion)
1473  * @gh: the holder structure to add
1474  *
1475  * Eventually we should move the recursive locking trap to a
1476  * debugging option or something like that. This is the fast
1477  * path and needs to have the minimum number of distractions.
1478  * 
1479  */
1480
1481 static inline void add_to_queue(struct gfs2_holder *gh)
1482 __releases(&gl->gl_lockref.lock)
1483 __acquires(&gl->gl_lockref.lock)
1484 {
1485         struct gfs2_glock *gl = gh->gh_gl;
1486         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1487         struct list_head *insert_pt = NULL;
1488         struct gfs2_holder *gh2;
1489         int try_futile = 0;
1490
1491         GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL);
1492         if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1493                 GLOCK_BUG_ON(gl, true);
1494
1495         if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1496                 if (test_bit(GLF_LOCK, &gl->gl_flags)) {
1497                         struct gfs2_holder *current_gh;
1498
1499                         current_gh = find_first_holder(gl);
1500                         try_futile = !may_grant(gl, current_gh, gh);
1501                 }
1502                 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags))
1503                         goto fail;
1504         }
1505
1506         list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1507                 if (likely(gh2->gh_owner_pid != gh->gh_owner_pid))
1508                         continue;
1509                 if (gh->gh_gl->gl_ops->go_type == LM_TYPE_FLOCK)
1510                         continue;
1511                 if (!pid_is_meaningful(gh2))
1512                         continue;
1513                 goto trap_recursive;
1514         }
1515         list_for_each_entry(gh2, &gl->gl_holders, gh_list) {
1516                 if (try_futile &&
1517                     !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) {
1518 fail:
1519                         gh->gh_error = GLR_TRYFAILED;
1520                         gfs2_holder_wake(gh);
1521                         return;
1522                 }
1523                 if (test_bit(HIF_HOLDER, &gh2->gh_iflags))
1524                         continue;
1525         }
1526         trace_gfs2_glock_queue(gh, 1);
1527         gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT);
1528         gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT);
1529         if (likely(insert_pt == NULL)) {
1530                 list_add_tail(&gh->gh_list, &gl->gl_holders);
1531                 return;
1532         }
1533         list_add_tail(&gh->gh_list, insert_pt);
1534         spin_unlock(&gl->gl_lockref.lock);
1535         if (sdp->sd_lockstruct.ls_ops->lm_cancel)
1536                 sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
1537         spin_lock(&gl->gl_lockref.lock);
1538         return;
1539
1540 trap_recursive:
1541         fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip);
1542         fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid));
1543         fs_err(sdp, "lock type: %d req lock state : %d\n",
1544                gh2->gh_gl->gl_name.ln_type, gh2->gh_state);
1545         fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip);
1546         fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid));
1547         fs_err(sdp, "lock type: %d req lock state : %d\n",
1548                gh->gh_gl->gl_name.ln_type, gh->gh_state);
1549         gfs2_dump_glock(NULL, gl, true);
1550         BUG();
1551 }
1552
1553 /**
1554  * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1555  * @gh: the holder structure
1556  *
1557  * if (gh->gh_flags & GL_ASYNC), this never returns an error
1558  *
1559  * Returns: 0, GLR_TRYFAILED, or errno on failure
1560  */
1561
1562 int gfs2_glock_nq(struct gfs2_holder *gh)
1563 {
1564         struct gfs2_glock *gl = gh->gh_gl;
1565         int error;
1566
1567         if (glock_blocked_by_withdraw(gl) && !(gh->gh_flags & LM_FLAG_NOEXP))
1568                 return -EIO;
1569
1570         if (gh->gh_flags & GL_NOBLOCK) {
1571                 struct gfs2_holder *current_gh;
1572
1573                 error = -ECHILD;
1574                 spin_lock(&gl->gl_lockref.lock);
1575                 if (find_last_waiter(gl))
1576                         goto unlock;
1577                 current_gh = find_first_holder(gl);
1578                 if (!may_grant(gl, current_gh, gh))
1579                         goto unlock;
1580                 set_bit(HIF_HOLDER, &gh->gh_iflags);
1581                 list_add_tail(&gh->gh_list, &gl->gl_holders);
1582                 trace_gfs2_promote(gh);
1583                 error = 0;
1584 unlock:
1585                 spin_unlock(&gl->gl_lockref.lock);
1586                 return error;
1587         }
1588
1589         gh->gh_error = 0;
1590         spin_lock(&gl->gl_lockref.lock);
1591         add_to_queue(gh);
1592         if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
1593                      test_and_clear_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags))) {
1594                 set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1595                 gl->gl_lockref.count++;
1596                 gfs2_glock_queue_work(gl, 0);
1597         }
1598         run_queue(gl, 1);
1599         spin_unlock(&gl->gl_lockref.lock);
1600
1601         error = 0;
1602         if (!(gh->gh_flags & GL_ASYNC))
1603                 error = gfs2_glock_wait(gh);
1604
1605         return error;
1606 }
1607
1608 /**
1609  * gfs2_glock_poll - poll to see if an async request has been completed
1610  * @gh: the holder
1611  *
1612  * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1613  */
1614
1615 int gfs2_glock_poll(struct gfs2_holder *gh)
1616 {
1617         return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1;
1618 }
1619
1620 static void __gfs2_glock_dq(struct gfs2_holder *gh)
1621 {
1622         struct gfs2_glock *gl = gh->gh_gl;
1623         unsigned delay = 0;
1624         int fast_path = 0;
1625
1626         /*
1627          * This holder should not be cached, so mark it for demote.
1628          * Note: this should be done before the glock_needs_demote
1629          * check below.
1630          */
1631         if (gh->gh_flags & GL_NOCACHE)
1632                 request_demote(gl, LM_ST_UNLOCKED, 0, false);
1633
1634         list_del_init(&gh->gh_list);
1635         clear_bit(HIF_HOLDER, &gh->gh_iflags);
1636         trace_gfs2_glock_queue(gh, 0);
1637
1638         /*
1639          * If there hasn't been a demote request we are done.
1640          * (Let the remaining holders, if any, keep holding it.)
1641          */
1642         if (!glock_needs_demote(gl)) {
1643                 if (list_empty(&gl->gl_holders))
1644                         fast_path = 1;
1645         }
1646
1647         if (unlikely(!fast_path)) {
1648                 gl->gl_lockref.count++;
1649                 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1650                     !test_bit(GLF_DEMOTE, &gl->gl_flags) &&
1651                     gl->gl_name.ln_type == LM_TYPE_INODE)
1652                         delay = gl->gl_hold_time;
1653                 gfs2_glock_queue_work(gl, delay);
1654         }
1655 }
1656
1657 /**
1658  * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1659  * @gh: the glock holder
1660  *
1661  */
1662 void gfs2_glock_dq(struct gfs2_holder *gh)
1663 {
1664         struct gfs2_glock *gl = gh->gh_gl;
1665         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1666
1667         spin_lock(&gl->gl_lockref.lock);
1668         if (!gfs2_holder_queued(gh)) {
1669                 /*
1670                  * May have already been dequeued because the locking request
1671                  * was GL_ASYNC and it has failed in the meantime.
1672                  */
1673                 goto out;
1674         }
1675
1676         if (list_is_first(&gh->gh_list, &gl->gl_holders) &&
1677             !test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1678                 spin_unlock(&gl->gl_lockref.lock);
1679                 gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl);
1680                 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1681                 spin_lock(&gl->gl_lockref.lock);
1682         }
1683
1684         /*
1685          * If we're in the process of file system withdraw, we cannot just
1686          * dequeue any glocks until our journal is recovered, lest we introduce
1687          * file system corruption. We need two exceptions to this rule: We need
1688          * to allow unlocking of nondisk glocks and the glock for our own
1689          * journal that needs recovery.
1690          */
1691         if (test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags) &&
1692             glock_blocked_by_withdraw(gl) &&
1693             gh->gh_gl != sdp->sd_jinode_gl) {
1694                 sdp->sd_glock_dqs_held++;
1695                 spin_unlock(&gl->gl_lockref.lock);
1696                 might_sleep();
1697                 wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY,
1698                             TASK_UNINTERRUPTIBLE);
1699                 spin_lock(&gl->gl_lockref.lock);
1700         }
1701
1702         __gfs2_glock_dq(gh);
1703 out:
1704         spin_unlock(&gl->gl_lockref.lock);
1705 }
1706
1707 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1708 {
1709         struct gfs2_glock *gl = gh->gh_gl;
1710         gfs2_glock_dq(gh);
1711         might_sleep();
1712         wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE);
1713 }
1714
1715 /**
1716  * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1717  * @gh: the holder structure
1718  *
1719  */
1720
1721 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1722 {
1723         gfs2_glock_dq(gh);
1724         gfs2_holder_uninit(gh);
1725 }
1726
1727 /**
1728  * gfs2_glock_nq_num - acquire a glock based on lock number
1729  * @sdp: the filesystem
1730  * @number: the lock number
1731  * @glops: the glock operations for the type of glock
1732  * @state: the state to acquire the glock in
1733  * @flags: modifier flags for the acquisition
1734  * @gh: the struct gfs2_holder
1735  *
1736  * Returns: errno
1737  */
1738
1739 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1740                       const struct gfs2_glock_operations *glops,
1741                       unsigned int state, u16 flags, struct gfs2_holder *gh)
1742 {
1743         struct gfs2_glock *gl;
1744         int error;
1745
1746         error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1747         if (!error) {
1748                 error = gfs2_glock_nq_init(gl, state, flags, gh);
1749                 gfs2_glock_put(gl);
1750         }
1751
1752         return error;
1753 }
1754
1755 /**
1756  * glock_compare - Compare two struct gfs2_glock structures for sorting
1757  * @arg_a: the first structure
1758  * @arg_b: the second structure
1759  *
1760  */
1761
1762 static int glock_compare(const void *arg_a, const void *arg_b)
1763 {
1764         const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1765         const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1766         const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1767         const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1768
1769         if (a->ln_number > b->ln_number)
1770                 return 1;
1771         if (a->ln_number < b->ln_number)
1772                 return -1;
1773         BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1774         return 0;
1775 }
1776
1777 /**
1778  * nq_m_sync - synchronously acquire more than one glock in deadlock free order
1779  * @num_gh: the number of structures
1780  * @ghs: an array of struct gfs2_holder structures
1781  * @p: placeholder for the holder structure to pass back
1782  *
1783  * Returns: 0 on success (all glocks acquired),
1784  *          errno on failure (no glocks acquired)
1785  */
1786
1787 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1788                      struct gfs2_holder **p)
1789 {
1790         unsigned int x;
1791         int error = 0;
1792
1793         for (x = 0; x < num_gh; x++)
1794                 p[x] = &ghs[x];
1795
1796         sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1797
1798         for (x = 0; x < num_gh; x++) {
1799                 error = gfs2_glock_nq(p[x]);
1800                 if (error) {
1801                         while (x--)
1802                                 gfs2_glock_dq(p[x]);
1803                         break;
1804                 }
1805         }
1806
1807         return error;
1808 }
1809
1810 /**
1811  * gfs2_glock_nq_m - acquire multiple glocks
1812  * @num_gh: the number of structures
1813  * @ghs: an array of struct gfs2_holder structures
1814  *
1815  * Returns: 0 on success (all glocks acquired),
1816  *          errno on failure (no glocks acquired)
1817  */
1818
1819 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1820 {
1821         struct gfs2_holder *tmp[4];
1822         struct gfs2_holder **pph = tmp;
1823         int error = 0;
1824
1825         switch(num_gh) {
1826         case 0:
1827                 return 0;
1828         case 1:
1829                 return gfs2_glock_nq(ghs);
1830         default:
1831                 if (num_gh <= 4)
1832                         break;
1833                 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *),
1834                                     GFP_NOFS);
1835                 if (!pph)
1836                         return -ENOMEM;
1837         }
1838
1839         error = nq_m_sync(num_gh, ghs, pph);
1840
1841         if (pph != tmp)
1842                 kfree(pph);
1843
1844         return error;
1845 }
1846
1847 /**
1848  * gfs2_glock_dq_m - release multiple glocks
1849  * @num_gh: the number of structures
1850  * @ghs: an array of struct gfs2_holder structures
1851  *
1852  */
1853
1854 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1855 {
1856         while (num_gh--)
1857                 gfs2_glock_dq(&ghs[num_gh]);
1858 }
1859
1860 void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
1861 {
1862         unsigned long delay = 0;
1863
1864         gfs2_glock_hold(gl);
1865         spin_lock(&gl->gl_lockref.lock);
1866         if (!list_empty(&gl->gl_holders) &&
1867             gl->gl_name.ln_type == LM_TYPE_INODE) {
1868                 unsigned long now = jiffies;
1869                 unsigned long holdtime;
1870
1871                 holdtime = gl->gl_tchange + gl->gl_hold_time;
1872
1873                 if (time_before(now, holdtime))
1874                         delay = holdtime - now;
1875                 if (test_bit(GLF_HAVE_REPLY, &gl->gl_flags))
1876                         delay = gl->gl_hold_time;
1877         }
1878         request_demote(gl, state, delay, true);
1879         gfs2_glock_queue_work(gl, delay);
1880         spin_unlock(&gl->gl_lockref.lock);
1881 }
1882
1883 /**
1884  * gfs2_should_freeze - Figure out if glock should be frozen
1885  * @gl: The glock in question
1886  *
1887  * Glocks are not frozen if (a) the result of the dlm operation is
1888  * an error, (b) the locking operation was an unlock operation or
1889  * (c) if there is a "noexp" flagged request anywhere in the queue
1890  *
1891  * Returns: 1 if freezing should occur, 0 otherwise
1892  */
1893
1894 static int gfs2_should_freeze(const struct gfs2_glock *gl)
1895 {
1896         const struct gfs2_holder *gh;
1897
1898         if (gl->gl_reply & ~LM_OUT_ST_MASK)
1899                 return 0;
1900         if (gl->gl_target == LM_ST_UNLOCKED)
1901                 return 0;
1902
1903         list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1904                 if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1905                         continue;
1906                 if (LM_FLAG_NOEXP & gh->gh_flags)
1907                         return 0;
1908         }
1909
1910         return 1;
1911 }
1912
1913 /**
1914  * gfs2_glock_complete - Callback used by locking
1915  * @gl: Pointer to the glock
1916  * @ret: The return value from the dlm
1917  *
1918  * The gl_reply field is under the gl_lockref.lock lock so that it is ok
1919  * to use a bitfield shared with other glock state fields.
1920  */
1921
1922 void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
1923 {
1924         struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct;
1925
1926         spin_lock(&gl->gl_lockref.lock);
1927         gl->gl_reply = ret;
1928
1929         if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) {
1930                 if (gfs2_should_freeze(gl)) {
1931                         set_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags);
1932                         spin_unlock(&gl->gl_lockref.lock);
1933                         return;
1934                 }
1935         }
1936
1937         gl->gl_lockref.count++;
1938         set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
1939         gfs2_glock_queue_work(gl, 0);
1940         spin_unlock(&gl->gl_lockref.lock);
1941 }
1942
1943 static int glock_cmp(void *priv, const struct list_head *a,
1944                      const struct list_head *b)
1945 {
1946         struct gfs2_glock *gla, *glb;
1947
1948         gla = list_entry(a, struct gfs2_glock, gl_lru);
1949         glb = list_entry(b, struct gfs2_glock, gl_lru);
1950
1951         if (gla->gl_name.ln_number > glb->gl_name.ln_number)
1952                 return 1;
1953         if (gla->gl_name.ln_number < glb->gl_name.ln_number)
1954                 return -1;
1955
1956         return 0;
1957 }
1958
1959 static bool can_free_glock(struct gfs2_glock *gl)
1960 {
1961         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
1962
1963         return !test_bit(GLF_LOCK, &gl->gl_flags) &&
1964                !gl->gl_lockref.count &&
1965                (!test_bit(GLF_LFLUSH, &gl->gl_flags) ||
1966                 test_bit(SDF_KILL, &sdp->sd_flags));
1967 }
1968
1969 /**
1970  * gfs2_dispose_glock_lru - Demote a list of glocks
1971  * @list: The list to dispose of
1972  *
1973  * Disposing of glocks may involve disk accesses, so that here we sort
1974  * the glocks by number (i.e. disk location of the inodes) so that if
1975  * there are any such accesses, they'll be sent in order (mostly).
1976  *
1977  * Must be called under the lru_lock, but may drop and retake this
1978  * lock. While the lru_lock is dropped, entries may vanish from the
1979  * list, but no new entries will appear on the list (since it is
1980  * private)
1981  */
1982
1983 static unsigned long gfs2_dispose_glock_lru(struct list_head *list)
1984 __releases(&lru_lock)
1985 __acquires(&lru_lock)
1986 {
1987         struct gfs2_glock *gl;
1988         unsigned long freed = 0;
1989
1990         list_sort(NULL, list, glock_cmp);
1991
1992         while(!list_empty(list)) {
1993                 gl = list_first_entry(list, struct gfs2_glock, gl_lru);
1994                 if (!spin_trylock(&gl->gl_lockref.lock)) {
1995 add_back_to_lru:
1996                         list_move(&gl->gl_lru, &lru_list);
1997                         continue;
1998                 }
1999                 if (!can_free_glock(gl)) {
2000                         spin_unlock(&gl->gl_lockref.lock);
2001                         goto add_back_to_lru;
2002                 }
2003                 list_del_init(&gl->gl_lru);
2004                 atomic_dec(&lru_count);
2005                 clear_bit(GLF_LRU, &gl->gl_flags);
2006                 freed++;
2007                 gl->gl_lockref.count++;
2008                 if (gl->gl_state != LM_ST_UNLOCKED)
2009                         request_demote(gl, LM_ST_UNLOCKED, 0, false);
2010                 gfs2_glock_queue_work(gl, 0);
2011                 spin_unlock(&gl->gl_lockref.lock);
2012                 cond_resched_lock(&lru_lock);
2013         }
2014         return freed;
2015 }
2016
2017 /**
2018  * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote
2019  * @nr: The number of entries to scan
2020  *
2021  * This function selects the entries on the LRU which are able to
2022  * be demoted, and then kicks off the process by calling
2023  * gfs2_dispose_glock_lru() above.
2024  */
2025
2026 static unsigned long gfs2_scan_glock_lru(unsigned long nr)
2027 {
2028         struct gfs2_glock *gl, *next;
2029         LIST_HEAD(dispose);
2030         unsigned long freed = 0;
2031
2032         spin_lock(&lru_lock);
2033         list_for_each_entry_safe(gl, next, &lru_list, gl_lru) {
2034                 if (!nr--)
2035                         break;
2036                 if (can_free_glock(gl))
2037                         list_move(&gl->gl_lru, &dispose);
2038         }
2039         if (!list_empty(&dispose))
2040                 freed = gfs2_dispose_glock_lru(&dispose);
2041         spin_unlock(&lru_lock);
2042
2043         return freed;
2044 }
2045
2046 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink,
2047                                             struct shrink_control *sc)
2048 {
2049         if (!(sc->gfp_mask & __GFP_FS))
2050                 return SHRINK_STOP;
2051         return gfs2_scan_glock_lru(sc->nr_to_scan);
2052 }
2053
2054 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink,
2055                                              struct shrink_control *sc)
2056 {
2057         return vfs_pressure_ratio(atomic_read(&lru_count));
2058 }
2059
2060 static struct shrinker *glock_shrinker;
2061
2062 /**
2063  * glock_hash_walk - Call a function for glock in a hash bucket
2064  * @examiner: the function
2065  * @sdp: the filesystem
2066  *
2067  * Note that the function can be called multiple times on the same
2068  * object.  So the user must ensure that the function can cope with
2069  * that.
2070  */
2071
2072 static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp)
2073 {
2074         struct gfs2_glock *gl;
2075         struct rhashtable_iter iter;
2076
2077         rhashtable_walk_enter(&gl_hash_table, &iter);
2078
2079         do {
2080                 rhashtable_walk_start(&iter);
2081
2082                 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) {
2083                         if (gl->gl_name.ln_sbd == sdp)
2084                                 examiner(gl);
2085                 }
2086
2087                 rhashtable_walk_stop(&iter);
2088         } while (cond_resched(), gl == ERR_PTR(-EAGAIN));
2089
2090         rhashtable_walk_exit(&iter);
2091 }
2092
2093 void gfs2_cancel_delete_work(struct gfs2_glock *gl)
2094 {
2095         clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags);
2096         clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags);
2097         if (cancel_delayed_work(&gl->gl_delete))
2098                 gfs2_glock_put(gl);
2099 }
2100
2101 static void flush_delete_work(struct gfs2_glock *gl)
2102 {
2103         if (gl->gl_name.ln_type == LM_TYPE_IOPEN) {
2104                 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2105
2106                 if (cancel_delayed_work(&gl->gl_delete)) {
2107                         queue_delayed_work(sdp->sd_delete_wq,
2108                                            &gl->gl_delete, 0);
2109                 }
2110         }
2111 }
2112
2113 void gfs2_flush_delete_work(struct gfs2_sbd *sdp)
2114 {
2115         glock_hash_walk(flush_delete_work, sdp);
2116         flush_workqueue(sdp->sd_delete_wq);
2117 }
2118
2119 /**
2120  * thaw_glock - thaw out a glock which has an unprocessed reply waiting
2121  * @gl: The glock to thaw
2122  *
2123  */
2124
2125 static void thaw_glock(struct gfs2_glock *gl)
2126 {
2127         if (!test_and_clear_bit(GLF_HAVE_FROZEN_REPLY, &gl->gl_flags))
2128                 return;
2129         if (!lockref_get_not_dead(&gl->gl_lockref))
2130                 return;
2131
2132         gfs2_glock_remove_from_lru(gl);
2133         spin_lock(&gl->gl_lockref.lock);
2134         set_bit(GLF_HAVE_REPLY, &gl->gl_flags);
2135         gfs2_glock_queue_work(gl, 0);
2136         spin_unlock(&gl->gl_lockref.lock);
2137 }
2138
2139 /**
2140  * clear_glock - look at a glock and see if we can free it from glock cache
2141  * @gl: the glock to look at
2142  *
2143  */
2144
2145 static void clear_glock(struct gfs2_glock *gl)
2146 {
2147         gfs2_glock_remove_from_lru(gl);
2148
2149         spin_lock(&gl->gl_lockref.lock);
2150         if (!__lockref_is_dead(&gl->gl_lockref)) {
2151                 gl->gl_lockref.count++;
2152                 if (gl->gl_state != LM_ST_UNLOCKED)
2153                         request_demote(gl, LM_ST_UNLOCKED, 0, false);
2154                 gfs2_glock_queue_work(gl, 0);
2155         }
2156         spin_unlock(&gl->gl_lockref.lock);
2157 }
2158
2159 /**
2160  * gfs2_glock_thaw - Thaw any frozen glocks
2161  * @sdp: The super block
2162  *
2163  */
2164
2165 void gfs2_glock_thaw(struct gfs2_sbd *sdp)
2166 {
2167         glock_hash_walk(thaw_glock, sdp);
2168 }
2169
2170 static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2171 {
2172         spin_lock(&gl->gl_lockref.lock);
2173         gfs2_dump_glock(seq, gl, fsid);
2174         spin_unlock(&gl->gl_lockref.lock);
2175 }
2176
2177 static void dump_glock_func(struct gfs2_glock *gl)
2178 {
2179         dump_glock(NULL, gl, true);
2180 }
2181
2182 static void withdraw_dq(struct gfs2_glock *gl)
2183 {
2184         spin_lock(&gl->gl_lockref.lock);
2185         if (!__lockref_is_dead(&gl->gl_lockref) &&
2186             glock_blocked_by_withdraw(gl))
2187                 do_error(gl, LM_OUT_ERROR); /* remove pending waiters */
2188         spin_unlock(&gl->gl_lockref.lock);
2189 }
2190
2191 void gfs2_gl_dq_holders(struct gfs2_sbd *sdp)
2192 {
2193         glock_hash_walk(withdraw_dq, sdp);
2194 }
2195
2196 /**
2197  * gfs2_gl_hash_clear - Empty out the glock hash table
2198  * @sdp: the filesystem
2199  *
2200  * Called when unmounting the filesystem.
2201  */
2202
2203 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
2204 {
2205         unsigned long start = jiffies;
2206         bool timed_out = false;
2207
2208         set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
2209         flush_workqueue(sdp->sd_glock_wq);
2210         glock_hash_walk(clear_glock, sdp);
2211         flush_workqueue(sdp->sd_glock_wq);
2212
2213         while (!timed_out) {
2214                 wait_event_timeout(sdp->sd_kill_wait,
2215                                    !atomic_read(&sdp->sd_glock_disposal),
2216                                    HZ * 60);
2217                 if (!atomic_read(&sdp->sd_glock_disposal))
2218                         break;
2219                 timed_out = time_after(jiffies, start + (HZ * 600));
2220                 fs_warn(sdp, "%u glocks left after %u seconds%s\n",
2221                         atomic_read(&sdp->sd_glock_disposal),
2222                         jiffies_to_msecs(jiffies - start) / 1000,
2223                         timed_out ? ":" : "; still waiting");
2224         }
2225         gfs2_lm_unmount(sdp);
2226         gfs2_free_dead_glocks(sdp);
2227         glock_hash_walk(dump_glock_func, sdp);
2228         destroy_workqueue(sdp->sd_glock_wq);
2229         sdp->sd_glock_wq = NULL;
2230 }
2231
2232 static const char *state2str(unsigned state)
2233 {
2234         switch(state) {
2235         case LM_ST_UNLOCKED:
2236                 return "UN";
2237         case LM_ST_SHARED:
2238                 return "SH";
2239         case LM_ST_DEFERRED:
2240                 return "DF";
2241         case LM_ST_EXCLUSIVE:
2242                 return "EX";
2243         }
2244         return "??";
2245 }
2246
2247 static const char *hflags2str(char *buf, u16 flags, unsigned long iflags)
2248 {
2249         char *p = buf;
2250         if (flags & LM_FLAG_TRY)
2251                 *p++ = 't';
2252         if (flags & LM_FLAG_TRY_1CB)
2253                 *p++ = 'T';
2254         if (flags & LM_FLAG_NOEXP)
2255                 *p++ = 'e';
2256         if (flags & LM_FLAG_ANY)
2257                 *p++ = 'A';
2258         if (flags & LM_FLAG_NODE_SCOPE)
2259                 *p++ = 'n';
2260         if (flags & GL_ASYNC)
2261                 *p++ = 'a';
2262         if (flags & GL_EXACT)
2263                 *p++ = 'E';
2264         if (flags & GL_NOCACHE)
2265                 *p++ = 'c';
2266         if (test_bit(HIF_HOLDER, &iflags))
2267                 *p++ = 'H';
2268         if (test_bit(HIF_WAIT, &iflags))
2269                 *p++ = 'W';
2270         if (flags & GL_SKIP)
2271                 *p++ = 's';
2272         *p = 0;
2273         return buf;
2274 }
2275
2276 /**
2277  * dump_holder - print information about a glock holder
2278  * @seq: the seq_file struct
2279  * @gh: the glock holder
2280  * @fs_id_buf: pointer to file system id (if requested)
2281  *
2282  */
2283
2284 static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh,
2285                         const char *fs_id_buf)
2286 {
2287         const char *comm = "(none)";
2288         pid_t owner_pid = 0;
2289         char flags_buf[32];
2290
2291         rcu_read_lock();
2292         if (pid_is_meaningful(gh)) {
2293                 struct task_struct *gh_owner;
2294
2295                 comm = "(ended)";
2296                 owner_pid = pid_nr(gh->gh_owner_pid);
2297                 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID);
2298                 if (gh_owner)
2299                         comm = gh_owner->comm;
2300         }
2301         gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n",
2302                        fs_id_buf, state2str(gh->gh_state),
2303                        hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags),
2304                        gh->gh_error, (long)owner_pid, comm, (void *)gh->gh_ip);
2305         rcu_read_unlock();
2306 }
2307
2308 static const char *gflags2str(char *buf, const struct gfs2_glock *gl)
2309 {
2310         const unsigned long *gflags = &gl->gl_flags;
2311         char *p = buf;
2312
2313         if (test_bit(GLF_LOCK, gflags))
2314                 *p++ = 'l';
2315         if (test_bit(GLF_DEMOTE, gflags))
2316                 *p++ = 'D';
2317         if (test_bit(GLF_PENDING_DEMOTE, gflags))
2318                 *p++ = 'd';
2319         if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags))
2320                 *p++ = 'p';
2321         if (test_bit(GLF_DIRTY, gflags))
2322                 *p++ = 'y';
2323         if (test_bit(GLF_LFLUSH, gflags))
2324                 *p++ = 'f';
2325         if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags))
2326                 *p++ = 'i';
2327         if (test_bit(GLF_HAVE_REPLY, gflags))
2328                 *p++ = 'r';
2329         if (test_bit(GLF_INITIAL, gflags))
2330                 *p++ = 'a';
2331         if (test_bit(GLF_HAVE_FROZEN_REPLY, gflags))
2332                 *p++ = 'F';
2333         if (!list_empty(&gl->gl_holders))
2334                 *p++ = 'q';
2335         if (test_bit(GLF_LRU, gflags))
2336                 *p++ = 'L';
2337         if (gl->gl_object)
2338                 *p++ = 'o';
2339         if (test_bit(GLF_BLOCKING, gflags))
2340                 *p++ = 'b';
2341         if (test_bit(GLF_UNLOCKED, gflags))
2342                 *p++ = 'x';
2343         if (test_bit(GLF_INSTANTIATE_NEEDED, gflags))
2344                 *p++ = 'n';
2345         if (test_bit(GLF_INSTANTIATE_IN_PROG, gflags))
2346                 *p++ = 'N';
2347         if (test_bit(GLF_TRY_TO_EVICT, gflags))
2348                 *p++ = 'e';
2349         if (test_bit(GLF_VERIFY_DELETE, gflags))
2350                 *p++ = 'E';
2351         *p = 0;
2352         return buf;
2353 }
2354
2355 /**
2356  * gfs2_dump_glock - print information about a glock
2357  * @seq: The seq_file struct
2358  * @gl: the glock
2359  * @fsid: If true, also dump the file system id
2360  *
2361  * The file format is as follows:
2362  * One line per object, capital letters are used to indicate objects
2363  * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented,
2364  * other objects are indented by a single space and follow the glock to
2365  * which they are related. Fields are indicated by lower case letters
2366  * followed by a colon and the field value, except for strings which are in
2367  * [] so that its possible to see if they are composed of spaces for
2368  * example. The field's are n = number (id of the object), f = flags,
2369  * t = type, s = state, r = refcount, e = error, p = pid.
2370  *
2371  */
2372
2373 void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid)
2374 {
2375         const struct gfs2_glock_operations *glops = gl->gl_ops;
2376         unsigned long long dtime;
2377         const struct gfs2_holder *gh;
2378         char gflags_buf[32];
2379         struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
2380         char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
2381         unsigned long nrpages = 0;
2382
2383         if (gl->gl_ops->go_flags & GLOF_ASPACE) {
2384                 struct address_space *mapping = gfs2_glock2aspace(gl);
2385
2386                 nrpages = mapping->nrpages;
2387         }
2388         memset(fs_id_buf, 0, sizeof(fs_id_buf));
2389         if (fsid && sdp) /* safety precaution */
2390                 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
2391         dtime = jiffies - gl->gl_demote_time;
2392         dtime *= 1000000/HZ; /* demote time in uSec */
2393         if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
2394                 dtime = 0;
2395         gfs2_print_dbg(seq, "%sG:  s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d "
2396                        "v:%d r:%d m:%ld p:%lu\n",
2397                        fs_id_buf, state2str(gl->gl_state),
2398                        gl->gl_name.ln_type,
2399                        (unsigned long long)gl->gl_name.ln_number,
2400                        gflags2str(gflags_buf, gl),
2401                        state2str(gl->gl_target),
2402                        state2str(gl->gl_demote_state), dtime,
2403                        atomic_read(&gl->gl_ail_count),
2404                        atomic_read(&gl->gl_revokes),
2405                        (int)gl->gl_lockref.count, gl->gl_hold_time, nrpages);
2406
2407         list_for_each_entry(gh, &gl->gl_holders, gh_list)
2408                 dump_holder(seq, gh, fs_id_buf);
2409
2410         if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump)
2411                 glops->go_dump(seq, gl, fs_id_buf);
2412 }
2413
2414 static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr)
2415 {
2416         struct gfs2_glock *gl = iter_ptr;
2417
2418         seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n",
2419                    gl->gl_name.ln_type,
2420                    (unsigned long long)gl->gl_name.ln_number,
2421                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT],
2422                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR],
2423                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB],
2424                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB],
2425                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT],
2426                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR],
2427                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT],
2428                    (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]);
2429         return 0;
2430 }
2431
2432 static const char *gfs2_gltype[] = {
2433         "type",
2434         "reserved",
2435         "nondisk",
2436         "inode",
2437         "rgrp",
2438         "meta",
2439         "iopen",
2440         "flock",
2441         "plock",
2442         "quota",
2443         "journal",
2444 };
2445
2446 static const char *gfs2_stype[] = {
2447         [GFS2_LKS_SRTT]         = "srtt",
2448         [GFS2_LKS_SRTTVAR]      = "srttvar",
2449         [GFS2_LKS_SRTTB]        = "srttb",
2450         [GFS2_LKS_SRTTVARB]     = "srttvarb",
2451         [GFS2_LKS_SIRT]         = "sirt",
2452         [GFS2_LKS_SIRTVAR]      = "sirtvar",
2453         [GFS2_LKS_DCOUNT]       = "dlm",
2454         [GFS2_LKS_QCOUNT]       = "queue",
2455 };
2456
2457 #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype))
2458
2459 static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr)
2460 {
2461         struct gfs2_sbd *sdp = seq->private;
2462         loff_t pos = *(loff_t *)iter_ptr;
2463         unsigned index = pos >> 3;
2464         unsigned subindex = pos & 0x07;
2465         int i;
2466
2467         if (index == 0 && subindex != 0)
2468                 return 0;
2469
2470         seq_printf(seq, "%-10s %8s:", gfs2_gltype[index],
2471                    (index == 0) ? "cpu": gfs2_stype[subindex]);
2472
2473         for_each_possible_cpu(i) {
2474                 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i);
2475
2476                 if (index == 0)
2477                         seq_printf(seq, " %15u", i);
2478                 else
2479                         seq_printf(seq, " %15llu", (unsigned long long)lkstats->
2480                                    lkstats[index - 1].stats[subindex]);
2481         }
2482         seq_putc(seq, '\n');
2483         return 0;
2484 }
2485
2486 int __init gfs2_glock_init(void)
2487 {
2488         int i, ret;
2489
2490         ret = rhashtable_init(&gl_hash_table, &ht_parms);
2491         if (ret < 0)
2492                 return ret;
2493
2494         glock_shrinker = shrinker_alloc(0, "gfs2-glock");
2495         if (!glock_shrinker) {
2496                 rhashtable_destroy(&gl_hash_table);
2497                 return -ENOMEM;
2498         }
2499
2500         glock_shrinker->count_objects = gfs2_glock_shrink_count;
2501         glock_shrinker->scan_objects = gfs2_glock_shrink_scan;
2502
2503         shrinker_register(glock_shrinker);
2504
2505         for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++)
2506                 init_waitqueue_head(glock_wait_table + i);
2507
2508         return 0;
2509 }
2510
2511 void gfs2_glock_exit(void)
2512 {
2513         shrinker_free(glock_shrinker);
2514         rhashtable_destroy(&gl_hash_table);
2515 }
2516
2517 static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n)
2518 {
2519         struct gfs2_glock *gl = gi->gl;
2520
2521         if (gl) {
2522                 if (n == 0)
2523                         return;
2524                 gfs2_glock_put_async(gl);
2525         }
2526         for (;;) {
2527                 gl = rhashtable_walk_next(&gi->hti);
2528                 if (IS_ERR_OR_NULL(gl)) {
2529                         if (gl == ERR_PTR(-EAGAIN)) {
2530                                 n = 1;
2531                                 continue;
2532                         }
2533                         gl = NULL;
2534                         break;
2535                 }
2536                 if (gl->gl_name.ln_sbd != gi->sdp)
2537                         continue;
2538                 if (n <= 1) {
2539                         if (!lockref_get_not_dead(&gl->gl_lockref))
2540                                 continue;
2541                         break;
2542                 } else {
2543                         if (__lockref_is_dead(&gl->gl_lockref))
2544                                 continue;
2545                         n--;
2546                 }
2547         }
2548         gi->gl = gl;
2549 }
2550
2551 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos)
2552         __acquires(RCU)
2553 {
2554         struct gfs2_glock_iter *gi = seq->private;
2555         loff_t n;
2556
2557         /*
2558          * We can either stay where we are, skip to the next hash table
2559          * entry, or start from the beginning.
2560          */
2561         if (*pos < gi->last_pos) {
2562                 rhashtable_walk_exit(&gi->hti);
2563                 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2564                 n = *pos + 1;
2565         } else {
2566                 n = *pos - gi->last_pos;
2567         }
2568
2569         rhashtable_walk_start(&gi->hti);
2570
2571         gfs2_glock_iter_next(gi, n);
2572         gi->last_pos = *pos;
2573         return gi->gl;
2574 }
2575
2576 static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr,
2577                                  loff_t *pos)
2578 {
2579         struct gfs2_glock_iter *gi = seq->private;
2580
2581         (*pos)++;
2582         gi->last_pos = *pos;
2583         gfs2_glock_iter_next(gi, 1);
2584         return gi->gl;
2585 }
2586
2587 static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr)
2588         __releases(RCU)
2589 {
2590         struct gfs2_glock_iter *gi = seq->private;
2591
2592         rhashtable_walk_stop(&gi->hti);
2593 }
2594
2595 static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr)
2596 {
2597         dump_glock(seq, iter_ptr, false);
2598         return 0;
2599 }
2600
2601 static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos)
2602 {
2603         preempt_disable();
2604         if (*pos >= GFS2_NR_SBSTATS)
2605                 return NULL;
2606         return pos;
2607 }
2608
2609 static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr,
2610                                    loff_t *pos)
2611 {
2612         (*pos)++;
2613         if (*pos >= GFS2_NR_SBSTATS)
2614                 return NULL;
2615         return pos;
2616 }
2617
2618 static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr)
2619 {
2620         preempt_enable();
2621 }
2622
2623 static const struct seq_operations gfs2_glock_seq_ops = {
2624         .start = gfs2_glock_seq_start,
2625         .next  = gfs2_glock_seq_next,
2626         .stop  = gfs2_glock_seq_stop,
2627         .show  = gfs2_glock_seq_show,
2628 };
2629
2630 static const struct seq_operations gfs2_glstats_seq_ops = {
2631         .start = gfs2_glock_seq_start,
2632         .next  = gfs2_glock_seq_next,
2633         .stop  = gfs2_glock_seq_stop,
2634         .show  = gfs2_glstats_seq_show,
2635 };
2636
2637 static const struct seq_operations gfs2_sbstats_sops = {
2638         .start = gfs2_sbstats_seq_start,
2639         .next  = gfs2_sbstats_seq_next,
2640         .stop  = gfs2_sbstats_seq_stop,
2641         .show  = gfs2_sbstats_seq_show,
2642 };
2643
2644 #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL)
2645
2646 static int __gfs2_glocks_open(struct inode *inode, struct file *file,
2647                               const struct seq_operations *ops)
2648 {
2649         int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter));
2650         if (ret == 0) {
2651                 struct seq_file *seq = file->private_data;
2652                 struct gfs2_glock_iter *gi = seq->private;
2653
2654                 gi->sdp = inode->i_private;
2655                 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN);
2656                 if (seq->buf)
2657                         seq->size = GFS2_SEQ_GOODSIZE;
2658                 /*
2659                  * Initially, we are "before" the first hash table entry; the
2660                  * first call to rhashtable_walk_next gets us the first entry.
2661                  */
2662                 gi->last_pos = -1;
2663                 gi->gl = NULL;
2664                 rhashtable_walk_enter(&gl_hash_table, &gi->hti);
2665         }
2666         return ret;
2667 }
2668
2669 static int gfs2_glocks_open(struct inode *inode, struct file *file)
2670 {
2671         return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops);
2672 }
2673
2674 static int gfs2_glocks_release(struct inode *inode, struct file *file)
2675 {
2676         struct seq_file *seq = file->private_data;
2677         struct gfs2_glock_iter *gi = seq->private;
2678
2679         if (gi->gl)
2680                 gfs2_glock_put(gi->gl);
2681         rhashtable_walk_exit(&gi->hti);
2682         return seq_release_private(inode, file);
2683 }
2684
2685 static int gfs2_glstats_open(struct inode *inode, struct file *file)
2686 {
2687         return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops);
2688 }
2689
2690 static const struct file_operations gfs2_glocks_fops = {
2691         .owner   = THIS_MODULE,
2692         .open    = gfs2_glocks_open,
2693         .read    = seq_read,
2694         .llseek  = seq_lseek,
2695         .release = gfs2_glocks_release,
2696 };
2697
2698 static const struct file_operations gfs2_glstats_fops = {
2699         .owner   = THIS_MODULE,
2700         .open    = gfs2_glstats_open,
2701         .read    = seq_read,
2702         .llseek  = seq_lseek,
2703         .release = gfs2_glocks_release,
2704 };
2705
2706 struct gfs2_glockfd_iter {
2707         struct super_block *sb;
2708         unsigned int tgid;
2709         struct task_struct *task;
2710         unsigned int fd;
2711         struct file *file;
2712 };
2713
2714 static struct task_struct *gfs2_glockfd_next_task(struct gfs2_glockfd_iter *i)
2715 {
2716         struct pid_namespace *ns = task_active_pid_ns(current);
2717         struct pid *pid;
2718
2719         if (i->task)
2720                 put_task_struct(i->task);
2721
2722         rcu_read_lock();
2723 retry:
2724         i->task = NULL;
2725         pid = find_ge_pid(i->tgid, ns);
2726         if (pid) {
2727                 i->tgid = pid_nr_ns(pid, ns);
2728                 i->task = pid_task(pid, PIDTYPE_TGID);
2729                 if (!i->task) {
2730                         i->tgid++;
2731                         goto retry;
2732                 }
2733                 get_task_struct(i->task);
2734         }
2735         rcu_read_unlock();
2736         return i->task;
2737 }
2738
2739 static struct file *gfs2_glockfd_next_file(struct gfs2_glockfd_iter *i)
2740 {
2741         if (i->file) {
2742                 fput(i->file);
2743                 i->file = NULL;
2744         }
2745
2746         rcu_read_lock();
2747         for(;; i->fd++) {
2748                 struct inode *inode;
2749
2750                 i->file = task_lookup_next_fdget_rcu(i->task, &i->fd);
2751                 if (!i->file) {
2752                         i->fd = 0;
2753                         break;
2754                 }
2755
2756                 inode = file_inode(i->file);
2757                 if (inode->i_sb == i->sb)
2758                         break;
2759
2760                 rcu_read_unlock();
2761                 fput(i->file);
2762                 rcu_read_lock();
2763         }
2764         rcu_read_unlock();
2765         return i->file;
2766 }
2767
2768 static void *gfs2_glockfd_seq_start(struct seq_file *seq, loff_t *pos)
2769 {
2770         struct gfs2_glockfd_iter *i = seq->private;
2771
2772         if (*pos)
2773                 return NULL;
2774         while (gfs2_glockfd_next_task(i)) {
2775                 if (gfs2_glockfd_next_file(i))
2776                         return i;
2777                 i->tgid++;
2778         }
2779         return NULL;
2780 }
2781
2782 static void *gfs2_glockfd_seq_next(struct seq_file *seq, void *iter_ptr,
2783                                    loff_t *pos)
2784 {
2785         struct gfs2_glockfd_iter *i = seq->private;
2786
2787         (*pos)++;
2788         i->fd++;
2789         do {
2790                 if (gfs2_glockfd_next_file(i))
2791                         return i;
2792                 i->tgid++;
2793         } while (gfs2_glockfd_next_task(i));
2794         return NULL;
2795 }
2796
2797 static void gfs2_glockfd_seq_stop(struct seq_file *seq, void *iter_ptr)
2798 {
2799         struct gfs2_glockfd_iter *i = seq->private;
2800
2801         if (i->file)
2802                 fput(i->file);
2803         if (i->task)
2804                 put_task_struct(i->task);
2805 }
2806
2807 static void gfs2_glockfd_seq_show_flock(struct seq_file *seq,
2808                                         struct gfs2_glockfd_iter *i)
2809 {
2810         struct gfs2_file *fp = i->file->private_data;
2811         struct gfs2_holder *fl_gh = &fp->f_fl_gh;
2812         struct lm_lockname gl_name = { .ln_type = LM_TYPE_RESERVED };
2813
2814         if (!READ_ONCE(fl_gh->gh_gl))
2815                 return;
2816
2817         spin_lock(&i->file->f_lock);
2818         if (gfs2_holder_initialized(fl_gh))
2819                 gl_name = fl_gh->gh_gl->gl_name;
2820         spin_unlock(&i->file->f_lock);
2821
2822         if (gl_name.ln_type != LM_TYPE_RESERVED) {
2823                 seq_printf(seq, "%d %u %u/%llx\n",
2824                            i->tgid, i->fd, gl_name.ln_type,
2825                            (unsigned long long)gl_name.ln_number);
2826         }
2827 }
2828
2829 static int gfs2_glockfd_seq_show(struct seq_file *seq, void *iter_ptr)
2830 {
2831         struct gfs2_glockfd_iter *i = seq->private;
2832         struct inode *inode = file_inode(i->file);
2833         struct gfs2_glock *gl;
2834
2835         inode_lock_shared(inode);
2836         gl = GFS2_I(inode)->i_iopen_gh.gh_gl;
2837         if (gl) {
2838                 seq_printf(seq, "%d %u %u/%llx\n",
2839                            i->tgid, i->fd, gl->gl_name.ln_type,
2840                            (unsigned long long)gl->gl_name.ln_number);
2841         }
2842         gfs2_glockfd_seq_show_flock(seq, i);
2843         inode_unlock_shared(inode);
2844         return 0;
2845 }
2846
2847 static const struct seq_operations gfs2_glockfd_seq_ops = {
2848         .start = gfs2_glockfd_seq_start,
2849         .next  = gfs2_glockfd_seq_next,
2850         .stop  = gfs2_glockfd_seq_stop,
2851         .show  = gfs2_glockfd_seq_show,
2852 };
2853
2854 static int gfs2_glockfd_open(struct inode *inode, struct file *file)
2855 {
2856         struct gfs2_glockfd_iter *i;
2857         struct gfs2_sbd *sdp = inode->i_private;
2858
2859         i = __seq_open_private(file, &gfs2_glockfd_seq_ops,
2860                                sizeof(struct gfs2_glockfd_iter));
2861         if (!i)
2862                 return -ENOMEM;
2863         i->sb = sdp->sd_vfs;
2864         return 0;
2865 }
2866
2867 static const struct file_operations gfs2_glockfd_fops = {
2868         .owner   = THIS_MODULE,
2869         .open    = gfs2_glockfd_open,
2870         .read    = seq_read,
2871         .llseek  = seq_lseek,
2872         .release = seq_release_private,
2873 };
2874
2875 DEFINE_SEQ_ATTRIBUTE(gfs2_sbstats);
2876
2877 void gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2878 {
2879         sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2880
2881         debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2882                             &gfs2_glocks_fops);
2883
2884         debugfs_create_file("glockfd", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2885                             &gfs2_glockfd_fops);
2886
2887         debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2888                             &gfs2_glstats_fops);
2889
2890         debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp,
2891                             &gfs2_sbstats_fops);
2892 }
2893
2894 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2895 {
2896         debugfs_remove_recursive(sdp->debugfs_dir);
2897         sdp->debugfs_dir = NULL;
2898 }
2899
2900 void gfs2_register_debugfs(void)
2901 {
2902         gfs2_root = debugfs_create_dir("gfs2", NULL);
2903 }
2904
2905 void gfs2_unregister_debugfs(void)
2906 {
2907         debugfs_remove(gfs2_root);
2908         gfs2_root = NULL;
2909 }
This page took 0.207916 seconds and 4 git commands to generate.