[GFS2] Reorder writeback for glock sync
[linux-block.git] / fs / gfs2 / glock.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/delay.h>
16 #include <linux/sort.h>
17 #include <linux/jhash.h>
18 #include <linux/kallsyms.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/list.h>
21 #include <linux/lm_interface.h>
22 #include <linux/wait.h>
23 #include <linux/module.h>
24 #include <linux/rwsem.h>
25 #include <asm/uaccess.h>
26 #include <linux/seq_file.h>
27 #include <linux/debugfs.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30 #include <linux/workqueue.h>
31 #include <linux/jiffies.h>
32
33 #include "gfs2.h"
34 #include "incore.h"
35 #include "glock.h"
36 #include "glops.h"
37 #include "inode.h"
38 #include "lm.h"
39 #include "lops.h"
40 #include "meta_io.h"
41 #include "quota.h"
42 #include "super.h"
43 #include "util.h"
44
45 struct gfs2_gl_hash_bucket {
46         struct hlist_head hb_list;
47 };
48
49 struct glock_iter {
50         int hash;                     /* hash bucket index         */
51         struct gfs2_sbd *sdp;         /* incore superblock         */
52         struct gfs2_glock *gl;        /* current glock struct      */
53         struct seq_file *seq;         /* sequence file for debugfs */
54         char string[512];             /* scratch space             */
55 };
56
57 typedef void (*glock_examiner) (struct gfs2_glock * gl);
58
59 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp);
60 static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl);
61 static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh);
62 static void gfs2_glock_drop_th(struct gfs2_glock *gl);
63 static void run_queue(struct gfs2_glock *gl);
64
65 static DECLARE_RWSEM(gfs2_umount_flush_sem);
66 static struct dentry *gfs2_root;
67 static struct task_struct *scand_process;
68 static unsigned int scand_secs = 5;
69 static struct workqueue_struct *glock_workqueue;
70
71 #define GFS2_GL_HASH_SHIFT      15
72 #define GFS2_GL_HASH_SIZE       (1 << GFS2_GL_HASH_SHIFT)
73 #define GFS2_GL_HASH_MASK       (GFS2_GL_HASH_SIZE - 1)
74
75 static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE];
76 static struct dentry *gfs2_root;
77
78 /*
79  * Despite what you might think, the numbers below are not arbitrary :-)
80  * They are taken from the ipv4 routing hash code, which is well tested
81  * and thus should be nearly optimal. Later on we might tweek the numbers
82  * but for now this should be fine.
83  *
84  * The reason for putting the locks in a separate array from the list heads
85  * is that we can have fewer locks than list heads and save memory. We use
86  * the same hash function for both, but with a different hash mask.
87  */
88 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \
89         defined(CONFIG_PROVE_LOCKING)
90
91 #ifdef CONFIG_LOCKDEP
92 # define GL_HASH_LOCK_SZ        256
93 #else
94 # if NR_CPUS >= 32
95 #  define GL_HASH_LOCK_SZ       4096
96 # elif NR_CPUS >= 16
97 #  define GL_HASH_LOCK_SZ       2048
98 # elif NR_CPUS >= 8
99 #  define GL_HASH_LOCK_SZ       1024
100 # elif NR_CPUS >= 4
101 #  define GL_HASH_LOCK_SZ       512
102 # else
103 #  define GL_HASH_LOCK_SZ       256
104 # endif
105 #endif
106
107 /* We never want more locks than chains */
108 #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ
109 # undef GL_HASH_LOCK_SZ
110 # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE
111 #endif
112
113 static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ];
114
115 static inline rwlock_t *gl_lock_addr(unsigned int x)
116 {
117         return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)];
118 }
119 #else /* not SMP, so no spinlocks required */
120 static inline rwlock_t *gl_lock_addr(unsigned int x)
121 {
122         return NULL;
123 }
124 #endif
125
126 /**
127  * relaxed_state_ok - is a requested lock compatible with the current lock mode?
128  * @actual: the current state of the lock
129  * @requested: the lock state that was requested by the caller
130  * @flags: the modifier flags passed in by the caller
131  *
132  * Returns: 1 if the locks are compatible, 0 otherwise
133  */
134
135 static inline int relaxed_state_ok(unsigned int actual, unsigned requested,
136                                    int flags)
137 {
138         if (actual == requested)
139                 return 1;
140
141         if (flags & GL_EXACT)
142                 return 0;
143
144         if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED)
145                 return 1;
146
147         if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY))
148                 return 1;
149
150         return 0;
151 }
152
153 /**
154  * gl_hash() - Turn glock number into hash bucket number
155  * @lock: The glock number
156  *
157  * Returns: The number of the corresponding hash bucket
158  */
159
160 static unsigned int gl_hash(const struct gfs2_sbd *sdp,
161                             const struct lm_lockname *name)
162 {
163         unsigned int h;
164
165         h = jhash(&name->ln_number, sizeof(u64), 0);
166         h = jhash(&name->ln_type, sizeof(unsigned int), h);
167         h = jhash(&sdp, sizeof(struct gfs2_sbd *), h);
168         h &= GFS2_GL_HASH_MASK;
169
170         return h;
171 }
172
173 /**
174  * glock_free() - Perform a few checks and then release struct gfs2_glock
175  * @gl: The glock to release
176  *
177  * Also calls lock module to release its internal structure for this glock.
178  *
179  */
180
181 static void glock_free(struct gfs2_glock *gl)
182 {
183         struct gfs2_sbd *sdp = gl->gl_sbd;
184         struct inode *aspace = gl->gl_aspace;
185
186         gfs2_lm_put_lock(sdp, gl->gl_lock);
187
188         if (aspace)
189                 gfs2_aspace_put(aspace);
190
191         kmem_cache_free(gfs2_glock_cachep, gl);
192 }
193
194 /**
195  * gfs2_glock_hold() - increment reference count on glock
196  * @gl: The glock to hold
197  *
198  */
199
200 void gfs2_glock_hold(struct gfs2_glock *gl)
201 {
202         atomic_inc(&gl->gl_ref);
203 }
204
205 /**
206  * gfs2_glock_put() - Decrement reference count on glock
207  * @gl: The glock to put
208  *
209  */
210
211 int gfs2_glock_put(struct gfs2_glock *gl)
212 {
213         int rv = 0;
214         struct gfs2_sbd *sdp = gl->gl_sbd;
215
216         write_lock(gl_lock_addr(gl->gl_hash));
217         if (atomic_dec_and_test(&gl->gl_ref)) {
218                 hlist_del(&gl->gl_list);
219                 write_unlock(gl_lock_addr(gl->gl_hash));
220                 BUG_ON(spin_is_locked(&gl->gl_spin));
221                 gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED);
222                 gfs2_assert(sdp, list_empty(&gl->gl_reclaim));
223                 gfs2_assert(sdp, list_empty(&gl->gl_holders));
224                 gfs2_assert(sdp, list_empty(&gl->gl_waiters1));
225                 gfs2_assert(sdp, list_empty(&gl->gl_waiters3));
226                 glock_free(gl);
227                 rv = 1;
228                 goto out;
229         }
230         write_unlock(gl_lock_addr(gl->gl_hash));
231 out:
232         return rv;
233 }
234
235 /**
236  * search_bucket() - Find struct gfs2_glock by lock number
237  * @bucket: the bucket to search
238  * @name: The lock name
239  *
240  * Returns: NULL, or the struct gfs2_glock with the requested number
241  */
242
243 static struct gfs2_glock *search_bucket(unsigned int hash,
244                                         const struct gfs2_sbd *sdp,
245                                         const struct lm_lockname *name)
246 {
247         struct gfs2_glock *gl;
248         struct hlist_node *h;
249
250         hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) {
251                 if (!lm_name_equal(&gl->gl_name, name))
252                         continue;
253                 if (gl->gl_sbd != sdp)
254                         continue;
255
256                 atomic_inc(&gl->gl_ref);
257
258                 return gl;
259         }
260
261         return NULL;
262 }
263
264 /**
265  * gfs2_glock_find() - Find glock by lock number
266  * @sdp: The GFS2 superblock
267  * @name: The lock name
268  *
269  * Returns: NULL, or the struct gfs2_glock with the requested number
270  */
271
272 static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
273                                           const struct lm_lockname *name)
274 {
275         unsigned int hash = gl_hash(sdp, name);
276         struct gfs2_glock *gl;
277
278         read_lock(gl_lock_addr(hash));
279         gl = search_bucket(hash, sdp, name);
280         read_unlock(gl_lock_addr(hash));
281
282         return gl;
283 }
284
285 static void glock_work_func(struct work_struct *work)
286 {
287         struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work);
288
289         spin_lock(&gl->gl_spin);
290         if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags))
291                 set_bit(GLF_DEMOTE, &gl->gl_flags);
292         run_queue(gl);
293         spin_unlock(&gl->gl_spin);
294         gfs2_glock_put(gl);
295 }
296
297 /**
298  * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
299  * @sdp: The GFS2 superblock
300  * @number: the lock number
301  * @glops: The glock_operations to use
302  * @create: If 0, don't create the glock if it doesn't exist
303  * @glp: the glock is returned here
304  *
305  * This does not lock a glock, just finds/creates structures for one.
306  *
307  * Returns: errno
308  */
309
310 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
311                    const struct gfs2_glock_operations *glops, int create,
312                    struct gfs2_glock **glp)
313 {
314         struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type };
315         struct gfs2_glock *gl, *tmp;
316         unsigned int hash = gl_hash(sdp, &name);
317         int error;
318
319         read_lock(gl_lock_addr(hash));
320         gl = search_bucket(hash, sdp, &name);
321         read_unlock(gl_lock_addr(hash));
322
323         if (gl || !create) {
324                 *glp = gl;
325                 return 0;
326         }
327
328         gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
329         if (!gl)
330                 return -ENOMEM;
331
332         gl->gl_flags = 0;
333         gl->gl_name = name;
334         atomic_set(&gl->gl_ref, 1);
335         gl->gl_state = LM_ST_UNLOCKED;
336         gl->gl_demote_state = LM_ST_EXCLUSIVE;
337         gl->gl_hash = hash;
338         gl->gl_owner_pid = 0;
339         gl->gl_ip = 0;
340         gl->gl_ops = glops;
341         gl->gl_req_gh = NULL;
342         gl->gl_req_bh = NULL;
343         gl->gl_vn = 0;
344         gl->gl_stamp = jiffies;
345         gl->gl_tchange = jiffies;
346         gl->gl_object = NULL;
347         gl->gl_sbd = sdp;
348         gl->gl_aspace = NULL;
349         lops_init_le(&gl->gl_le, &gfs2_glock_lops);
350         INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
351
352         /* If this glock protects actual on-disk data or metadata blocks,
353            create a VFS inode to manage the pages/buffers holding them. */
354         if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) {
355                 gl->gl_aspace = gfs2_aspace_get(sdp);
356                 if (!gl->gl_aspace) {
357                         error = -ENOMEM;
358                         goto fail;
359                 }
360         }
361
362         error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
363         if (error)
364                 goto fail_aspace;
365
366         write_lock(gl_lock_addr(hash));
367         tmp = search_bucket(hash, sdp, &name);
368         if (tmp) {
369                 write_unlock(gl_lock_addr(hash));
370                 glock_free(gl);
371                 gl = tmp;
372         } else {
373                 hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list);
374                 write_unlock(gl_lock_addr(hash));
375         }
376
377         *glp = gl;
378
379         return 0;
380
381 fail_aspace:
382         if (gl->gl_aspace)
383                 gfs2_aspace_put(gl->gl_aspace);
384 fail:
385         kmem_cache_free(gfs2_glock_cachep, gl);
386         return error;
387 }
388
389 /**
390  * gfs2_holder_init - initialize a struct gfs2_holder in the default way
391  * @gl: the glock
392  * @state: the state we're requesting
393  * @flags: the modifier flags
394  * @gh: the holder structure
395  *
396  */
397
398 void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, unsigned flags,
399                       struct gfs2_holder *gh)
400 {
401         INIT_LIST_HEAD(&gh->gh_list);
402         gh->gh_gl = gl;
403         gh->gh_ip = (unsigned long)__builtin_return_address(0);
404         gh->gh_owner_pid = current->pid;
405         gh->gh_state = state;
406         gh->gh_flags = flags;
407         gh->gh_error = 0;
408         gh->gh_iflags = 0;
409         gfs2_glock_hold(gl);
410 }
411
412 /**
413  * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it
414  * @state: the state we're requesting
415  * @flags: the modifier flags
416  * @gh: the holder structure
417  *
418  * Don't mess with the glock.
419  *
420  */
421
422 void gfs2_holder_reinit(unsigned int state, unsigned flags, struct gfs2_holder *gh)
423 {
424         gh->gh_state = state;
425         gh->gh_flags = flags;
426         gh->gh_iflags = 0;
427         gh->gh_ip = (unsigned long)__builtin_return_address(0);
428 }
429
430 /**
431  * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference)
432  * @gh: the holder structure
433  *
434  */
435
436 void gfs2_holder_uninit(struct gfs2_holder *gh)
437 {
438         gfs2_glock_put(gh->gh_gl);
439         gh->gh_gl = NULL;
440         gh->gh_ip = 0;
441 }
442
443 static void gfs2_holder_wake(struct gfs2_holder *gh)
444 {
445         clear_bit(HIF_WAIT, &gh->gh_iflags);
446         smp_mb__after_clear_bit();
447         wake_up_bit(&gh->gh_iflags, HIF_WAIT);
448 }
449
450 static int just_schedule(void *word)
451 {
452         schedule();
453         return 0;
454 }
455
456 static void wait_on_holder(struct gfs2_holder *gh)
457 {
458         might_sleep();
459         wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
460 }
461
462 static void gfs2_demote_wake(struct gfs2_glock *gl)
463 {
464         BUG_ON(!spin_is_locked(&gl->gl_spin));
465         gl->gl_demote_state = LM_ST_EXCLUSIVE;
466         clear_bit(GLF_DEMOTE, &gl->gl_flags);
467         smp_mb__after_clear_bit();
468         wake_up_bit(&gl->gl_flags, GLF_DEMOTE);
469 }
470
471 static void wait_on_demote(struct gfs2_glock *gl)
472 {
473         might_sleep();
474         wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
475 }
476
477 /**
478  * rq_mutex - process a mutex request in the queue
479  * @gh: the glock holder
480  *
481  * Returns: 1 if the queue is blocked
482  */
483
484 static int rq_mutex(struct gfs2_holder *gh)
485 {
486         struct gfs2_glock *gl = gh->gh_gl;
487
488         list_del_init(&gh->gh_list);
489         /*  gh->gh_error never examined.  */
490         set_bit(GLF_LOCK, &gl->gl_flags);
491         clear_bit(HIF_WAIT, &gh->gh_iflags);
492         smp_mb();
493         wake_up_bit(&gh->gh_iflags, HIF_WAIT);
494
495         return 1;
496 }
497
498 /**
499  * rq_promote - process a promote request in the queue
500  * @gh: the glock holder
501  *
502  * Acquire a new inter-node lock, or change a lock state to more restrictive.
503  *
504  * Returns: 1 if the queue is blocked
505  */
506
507 static int rq_promote(struct gfs2_holder *gh)
508 {
509         struct gfs2_glock *gl = gh->gh_gl;
510
511         if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
512                 if (list_empty(&gl->gl_holders)) {
513                         gl->gl_req_gh = gh;
514                         set_bit(GLF_LOCK, &gl->gl_flags);
515                         spin_unlock(&gl->gl_spin);
516                         gfs2_glock_xmote_th(gh->gh_gl, gh);
517                         spin_lock(&gl->gl_spin);
518                 }
519                 return 1;
520         }
521
522         if (list_empty(&gl->gl_holders)) {
523                 set_bit(HIF_FIRST, &gh->gh_iflags);
524                 set_bit(GLF_LOCK, &gl->gl_flags);
525         } else {
526                 struct gfs2_holder *next_gh;
527                 if (gh->gh_state == LM_ST_EXCLUSIVE)
528                         return 1;
529                 next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder,
530                                      gh_list);
531                 if (next_gh->gh_state == LM_ST_EXCLUSIVE)
532                          return 1;
533         }
534
535         list_move_tail(&gh->gh_list, &gl->gl_holders);
536         gh->gh_error = 0;
537         set_bit(HIF_HOLDER, &gh->gh_iflags);
538
539         gfs2_holder_wake(gh);
540
541         return 0;
542 }
543
544 /**
545  * rq_demote - process a demote request in the queue
546  * @gh: the glock holder
547  *
548  * Returns: 1 if the queue is blocked
549  */
550
551 static int rq_demote(struct gfs2_glock *gl)
552 {
553         if (!list_empty(&gl->gl_holders))
554                 return 1;
555
556         if (gl->gl_state == gl->gl_demote_state ||
557             gl->gl_state == LM_ST_UNLOCKED) {
558                 gfs2_demote_wake(gl);
559                 return 0;
560         }
561
562         set_bit(GLF_LOCK, &gl->gl_flags);
563         set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
564
565         if (gl->gl_demote_state == LM_ST_UNLOCKED ||
566             gl->gl_state != LM_ST_EXCLUSIVE) {
567                 spin_unlock(&gl->gl_spin);
568                 gfs2_glock_drop_th(gl);
569         } else {
570                 spin_unlock(&gl->gl_spin);
571                 gfs2_glock_xmote_th(gl, NULL);
572         }
573
574         spin_lock(&gl->gl_spin);
575         clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
576
577         return 0;
578 }
579
580 /**
581  * run_queue - process holder structures on a glock
582  * @gl: the glock
583  *
584  */
585 static void run_queue(struct gfs2_glock *gl)
586 {
587         struct gfs2_holder *gh;
588         int blocked = 1;
589
590         for (;;) {
591                 if (test_bit(GLF_LOCK, &gl->gl_flags))
592                         break;
593
594                 if (!list_empty(&gl->gl_waiters1)) {
595                         gh = list_entry(gl->gl_waiters1.next,
596                                         struct gfs2_holder, gh_list);
597
598                         if (test_bit(HIF_MUTEX, &gh->gh_iflags))
599                                 blocked = rq_mutex(gh);
600                         else
601                                 gfs2_assert_warn(gl->gl_sbd, 0);
602
603                 } else if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
604                         blocked = rq_demote(gl);
605                         if (gl->gl_waiters2 && !blocked) {
606                                 set_bit(GLF_DEMOTE, &gl->gl_flags);
607                                 gl->gl_demote_state = LM_ST_UNLOCKED;
608                         }
609                         gl->gl_waiters2 = 0;
610                 } else if (!list_empty(&gl->gl_waiters3)) {
611                         gh = list_entry(gl->gl_waiters3.next,
612                                         struct gfs2_holder, gh_list);
613
614                         if (test_bit(HIF_PROMOTE, &gh->gh_iflags))
615                                 blocked = rq_promote(gh);
616                         else
617                                 gfs2_assert_warn(gl->gl_sbd, 0);
618
619                 } else
620                         break;
621
622                 if (blocked)
623                         break;
624         }
625 }
626
627 /**
628  * gfs2_glmutex_lock - acquire a local lock on a glock
629  * @gl: the glock
630  *
631  * Gives caller exclusive access to manipulate a glock structure.
632  */
633
634 static void gfs2_glmutex_lock(struct gfs2_glock *gl)
635 {
636         struct gfs2_holder gh;
637
638         gfs2_holder_init(gl, 0, 0, &gh);
639         set_bit(HIF_MUTEX, &gh.gh_iflags);
640         if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags))
641                 BUG();
642
643         spin_lock(&gl->gl_spin);
644         if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
645                 list_add_tail(&gh.gh_list, &gl->gl_waiters1);
646         } else {
647                 gl->gl_owner_pid = current->pid;
648                 gl->gl_ip = (unsigned long)__builtin_return_address(0);
649                 clear_bit(HIF_WAIT, &gh.gh_iflags);
650                 smp_mb();
651                 wake_up_bit(&gh.gh_iflags, HIF_WAIT);
652         }
653         spin_unlock(&gl->gl_spin);
654
655         wait_on_holder(&gh);
656         gfs2_holder_uninit(&gh);
657 }
658
659 /**
660  * gfs2_glmutex_trylock - try to acquire a local lock on a glock
661  * @gl: the glock
662  *
663  * Returns: 1 if the glock is acquired
664  */
665
666 static int gfs2_glmutex_trylock(struct gfs2_glock *gl)
667 {
668         int acquired = 1;
669
670         spin_lock(&gl->gl_spin);
671         if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
672                 acquired = 0;
673         } else {
674                 gl->gl_owner_pid = current->pid;
675                 gl->gl_ip = (unsigned long)__builtin_return_address(0);
676         }
677         spin_unlock(&gl->gl_spin);
678
679         return acquired;
680 }
681
682 /**
683  * gfs2_glmutex_unlock - release a local lock on a glock
684  * @gl: the glock
685  *
686  */
687
688 static void gfs2_glmutex_unlock(struct gfs2_glock *gl)
689 {
690         spin_lock(&gl->gl_spin);
691         clear_bit(GLF_LOCK, &gl->gl_flags);
692         gl->gl_owner_pid = 0;
693         gl->gl_ip = 0;
694         run_queue(gl);
695         BUG_ON(!spin_is_locked(&gl->gl_spin));
696         spin_unlock(&gl->gl_spin);
697 }
698
699 /**
700  * handle_callback - process a demote request
701  * @gl: the glock
702  * @state: the state the caller wants us to change to
703  *
704  * There are only two requests that we are going to see in actual
705  * practise: LM_ST_SHARED and LM_ST_UNLOCKED
706  */
707
708 static void handle_callback(struct gfs2_glock *gl, unsigned int state,
709                             int remote, unsigned long delay)
710 {
711         int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
712
713         spin_lock(&gl->gl_spin);
714         set_bit(bit, &gl->gl_flags);
715         if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
716                 gl->gl_demote_state = state;
717                 gl->gl_demote_time = jiffies;
718                 if (remote && gl->gl_ops->go_type == LM_TYPE_IOPEN &&
719                     gl->gl_object) {
720                         gfs2_glock_schedule_for_reclaim(gl);
721                         spin_unlock(&gl->gl_spin);
722                         return;
723                 }
724         } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
725                         gl->gl_demote_state != state) {
726                 if (test_bit(GLF_DEMOTE_IN_PROGRESS,  &gl->gl_flags)) 
727                         gl->gl_waiters2 = 1;
728                 else 
729                         gl->gl_demote_state = LM_ST_UNLOCKED;
730         }
731         spin_unlock(&gl->gl_spin);
732 }
733
734 /**
735  * state_change - record that the glock is now in a different state
736  * @gl: the glock
737  * @new_state the new state
738  *
739  */
740
741 static void state_change(struct gfs2_glock *gl, unsigned int new_state)
742 {
743         int held1, held2;
744
745         held1 = (gl->gl_state != LM_ST_UNLOCKED);
746         held2 = (new_state != LM_ST_UNLOCKED);
747
748         if (held1 != held2) {
749                 if (held2)
750                         gfs2_glock_hold(gl);
751                 else
752                         gfs2_glock_put(gl);
753         }
754
755         gl->gl_state = new_state;
756         gl->gl_tchange = jiffies;
757 }
758
759 /**
760  * xmote_bh - Called after the lock module is done acquiring a lock
761  * @gl: The glock in question
762  * @ret: the int returned from the lock module
763  *
764  */
765
766 static void xmote_bh(struct gfs2_glock *gl, unsigned int ret)
767 {
768         struct gfs2_sbd *sdp = gl->gl_sbd;
769         const struct gfs2_glock_operations *glops = gl->gl_ops;
770         struct gfs2_holder *gh = gl->gl_req_gh;
771         int prev_state = gl->gl_state;
772         int op_done = 1;
773
774         gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
775         gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
776         gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC));
777
778         state_change(gl, ret & LM_OUT_ST_MASK);
779
780         if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) {
781                 if (glops->go_inval)
782                         glops->go_inval(gl, DIO_METADATA);
783         } else if (gl->gl_state == LM_ST_DEFERRED) {
784                 /* We might not want to do this here.
785                    Look at moving to the inode glops. */
786                 if (glops->go_inval)
787                         glops->go_inval(gl, 0);
788         }
789
790         /*  Deal with each possible exit condition  */
791
792         if (!gh) {
793                 gl->gl_stamp = jiffies;
794                 if (ret & LM_OUT_CANCELED) {
795                         op_done = 0;
796                 } else {
797                         spin_lock(&gl->gl_spin);
798                         if (gl->gl_state != gl->gl_demote_state) {
799                                 gl->gl_req_bh = NULL;
800                                 spin_unlock(&gl->gl_spin);
801                                 gfs2_glock_drop_th(gl);
802                                 gfs2_glock_put(gl);
803                                 return;
804                         }
805                         gfs2_demote_wake(gl);
806                         spin_unlock(&gl->gl_spin);
807                 }
808         } else {
809                 spin_lock(&gl->gl_spin);
810                 list_del_init(&gh->gh_list);
811                 gh->gh_error = -EIO;
812                 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) 
813                         goto out;
814                 gh->gh_error = GLR_CANCELED;
815                 if (ret & LM_OUT_CANCELED) 
816                         goto out;
817                 if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) {
818                         list_add_tail(&gh->gh_list, &gl->gl_holders);
819                         gh->gh_error = 0;
820                         set_bit(HIF_HOLDER, &gh->gh_iflags);
821                         set_bit(HIF_FIRST, &gh->gh_iflags);
822                         op_done = 0;
823                         goto out;
824                 }
825                 gh->gh_error = GLR_TRYFAILED;
826                 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))
827                         goto out;
828                 gh->gh_error = -EINVAL;
829                 if (gfs2_assert_withdraw(sdp, 0) == -1)
830                         fs_err(sdp, "ret = 0x%.8X\n", ret);
831 out:
832                 spin_unlock(&gl->gl_spin);
833         }
834
835         if (glops->go_xmote_bh)
836                 glops->go_xmote_bh(gl);
837
838         if (op_done) {
839                 spin_lock(&gl->gl_spin);
840                 gl->gl_req_gh = NULL;
841                 gl->gl_req_bh = NULL;
842                 clear_bit(GLF_LOCK, &gl->gl_flags);
843                 spin_unlock(&gl->gl_spin);
844         }
845
846         gfs2_glock_put(gl);
847
848         if (gh)
849                 gfs2_holder_wake(gh);
850 }
851
852 /**
853  * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock
854  * @gl: The glock in question
855  * @state: the requested state
856  * @flags: modifier flags to the lock call
857  *
858  */
859
860 static void gfs2_glock_xmote_th(struct gfs2_glock *gl, struct gfs2_holder *gh)
861 {
862         struct gfs2_sbd *sdp = gl->gl_sbd;
863         int flags = gh ? gh->gh_flags : 0;
864         unsigned state = gh ? gh->gh_state : gl->gl_demote_state;
865         const struct gfs2_glock_operations *glops = gl->gl_ops;
866         int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB |
867                                  LM_FLAG_NOEXP | LM_FLAG_ANY |
868                                  LM_FLAG_PRIORITY);
869         unsigned int lck_ret;
870
871         if (glops->go_xmote_th)
872                 glops->go_xmote_th(gl);
873
874         gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
875         gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
876         gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED);
877         gfs2_assert_warn(sdp, state != gl->gl_state);
878
879         gfs2_glock_hold(gl);
880         gl->gl_req_bh = xmote_bh;
881
882         lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags);
883
884         if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR)))
885                 return;
886
887         if (lck_ret & LM_OUT_ASYNC)
888                 gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC);
889         else
890                 xmote_bh(gl, lck_ret);
891 }
892
893 /**
894  * drop_bh - Called after a lock module unlock completes
895  * @gl: the glock
896  * @ret: the return status
897  *
898  * Doesn't wake up the process waiting on the struct gfs2_holder (if any)
899  * Doesn't drop the reference on the glock the top half took out
900  *
901  */
902
903 static void drop_bh(struct gfs2_glock *gl, unsigned int ret)
904 {
905         struct gfs2_sbd *sdp = gl->gl_sbd;
906         const struct gfs2_glock_operations *glops = gl->gl_ops;
907         struct gfs2_holder *gh = gl->gl_req_gh;
908
909         gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
910         gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
911         gfs2_assert_warn(sdp, !ret);
912
913         state_change(gl, LM_ST_UNLOCKED);
914
915         if (glops->go_inval)
916                 glops->go_inval(gl, DIO_METADATA);
917
918         if (gh) {
919                 spin_lock(&gl->gl_spin);
920                 list_del_init(&gh->gh_list);
921                 gh->gh_error = 0;
922                 spin_unlock(&gl->gl_spin);
923         }
924
925         spin_lock(&gl->gl_spin);
926         gfs2_demote_wake(gl);
927         gl->gl_req_gh = NULL;
928         gl->gl_req_bh = NULL;
929         clear_bit(GLF_LOCK, &gl->gl_flags);
930         spin_unlock(&gl->gl_spin);
931
932         gfs2_glock_put(gl);
933
934         if (gh)
935                 gfs2_holder_wake(gh);
936 }
937
938 /**
939  * gfs2_glock_drop_th - call into the lock module to unlock a lock
940  * @gl: the glock
941  *
942  */
943
944 static void gfs2_glock_drop_th(struct gfs2_glock *gl)
945 {
946         struct gfs2_sbd *sdp = gl->gl_sbd;
947         const struct gfs2_glock_operations *glops = gl->gl_ops;
948         unsigned int ret;
949
950         if (glops->go_xmote_th)
951                 glops->go_xmote_th(gl);
952
953         gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
954         gfs2_assert_warn(sdp, list_empty(&gl->gl_holders));
955         gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED);
956
957         gfs2_glock_hold(gl);
958         gl->gl_req_bh = drop_bh;
959
960         ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state);
961
962         if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR)))
963                 return;
964
965         if (!ret)
966                 drop_bh(gl, ret);
967         else
968                 gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC);
969 }
970
971 /**
972  * do_cancels - cancel requests for locks stuck waiting on an expire flag
973  * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock
974  *
975  * Don't cancel GL_NOCANCEL requests.
976  */
977
978 static void do_cancels(struct gfs2_holder *gh)
979 {
980         struct gfs2_glock *gl = gh->gh_gl;
981
982         spin_lock(&gl->gl_spin);
983
984         while (gl->gl_req_gh != gh &&
985                !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
986                !list_empty(&gh->gh_list)) {
987                 if (gl->gl_req_bh && !(gl->gl_req_gh &&
988                                      (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) {
989                         spin_unlock(&gl->gl_spin);
990                         gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock);
991                         msleep(100);
992                         spin_lock(&gl->gl_spin);
993                 } else {
994                         spin_unlock(&gl->gl_spin);
995                         msleep(100);
996                         spin_lock(&gl->gl_spin);
997                 }
998         }
999
1000         spin_unlock(&gl->gl_spin);
1001 }
1002
1003 /**
1004  * glock_wait_internal - wait on a glock acquisition
1005  * @gh: the glock holder
1006  *
1007  * Returns: 0 on success
1008  */
1009
1010 static int glock_wait_internal(struct gfs2_holder *gh)
1011 {
1012         struct gfs2_glock *gl = gh->gh_gl;
1013         struct gfs2_sbd *sdp = gl->gl_sbd;
1014         const struct gfs2_glock_operations *glops = gl->gl_ops;
1015
1016         if (test_bit(HIF_ABORTED, &gh->gh_iflags))
1017                 return -EIO;
1018
1019         if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) {
1020                 spin_lock(&gl->gl_spin);
1021                 if (gl->gl_req_gh != gh &&
1022                     !test_bit(HIF_HOLDER, &gh->gh_iflags) &&
1023                     !list_empty(&gh->gh_list)) {
1024                         list_del_init(&gh->gh_list);
1025                         gh->gh_error = GLR_TRYFAILED;
1026                         run_queue(gl);
1027                         spin_unlock(&gl->gl_spin);
1028                         return gh->gh_error;
1029                 }
1030                 spin_unlock(&gl->gl_spin);
1031         }
1032
1033         if (gh->gh_flags & LM_FLAG_PRIORITY)
1034                 do_cancels(gh);
1035
1036         wait_on_holder(gh);
1037         if (gh->gh_error)
1038                 return gh->gh_error;
1039
1040         gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags));
1041         gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state,
1042                                                    gh->gh_flags));
1043
1044         if (test_bit(HIF_FIRST, &gh->gh_iflags)) {
1045                 gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags));
1046
1047                 if (glops->go_lock) {
1048                         gh->gh_error = glops->go_lock(gh);
1049                         if (gh->gh_error) {
1050                                 spin_lock(&gl->gl_spin);
1051                                 list_del_init(&gh->gh_list);
1052                                 spin_unlock(&gl->gl_spin);
1053                         }
1054                 }
1055
1056                 spin_lock(&gl->gl_spin);
1057                 gl->gl_req_gh = NULL;
1058                 gl->gl_req_bh = NULL;
1059                 clear_bit(GLF_LOCK, &gl->gl_flags);
1060                 run_queue(gl);
1061                 spin_unlock(&gl->gl_spin);
1062         }
1063
1064         return gh->gh_error;
1065 }
1066
1067 static inline struct gfs2_holder *
1068 find_holder_by_owner(struct list_head *head, pid_t pid)
1069 {
1070         struct gfs2_holder *gh;
1071
1072         list_for_each_entry(gh, head, gh_list) {
1073                 if (gh->gh_owner_pid == pid)
1074                         return gh;
1075         }
1076
1077         return NULL;
1078 }
1079
1080 static void print_dbg(struct glock_iter *gi, const char *fmt, ...)
1081 {
1082         va_list args;
1083
1084         va_start(args, fmt);
1085         if (gi) {
1086                 vsprintf(gi->string, fmt, args);
1087                 seq_printf(gi->seq, gi->string);
1088         }
1089         else
1090                 vprintk(fmt, args);
1091         va_end(args);
1092 }
1093
1094 /**
1095  * add_to_queue - Add a holder to the wait queue (but look for recursion)
1096  * @gh: the holder structure to add
1097  *
1098  */
1099
1100 static void add_to_queue(struct gfs2_holder *gh)
1101 {
1102         struct gfs2_glock *gl = gh->gh_gl;
1103         struct gfs2_holder *existing;
1104
1105         BUG_ON(!gh->gh_owner_pid);
1106         if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags))
1107                 BUG();
1108
1109         if (!(gh->gh_flags & GL_FLOCK)) {
1110                 existing = find_holder_by_owner(&gl->gl_holders, 
1111                                                 gh->gh_owner_pid);
1112                 if (existing) {
1113                         print_symbol(KERN_WARNING "original: %s\n", 
1114                                      existing->gh_ip);
1115                         printk(KERN_INFO "pid : %d\n", existing->gh_owner_pid);
1116                         printk(KERN_INFO "lock type : %d lock state : %d\n",
1117                                existing->gh_gl->gl_name.ln_type, 
1118                                existing->gh_gl->gl_state);
1119                         print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1120                         printk(KERN_INFO "pid : %d\n", gh->gh_owner_pid);
1121                         printk(KERN_INFO "lock type : %d lock state : %d\n",
1122                                gl->gl_name.ln_type, gl->gl_state);
1123                         BUG();
1124                 }
1125                 
1126                 existing = find_holder_by_owner(&gl->gl_waiters3, 
1127                                                 gh->gh_owner_pid);
1128                 if (existing) {
1129                         print_symbol(KERN_WARNING "original: %s\n", 
1130                                      existing->gh_ip);
1131                         print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip);
1132                         BUG();
1133                 }
1134         }
1135
1136         if (gh->gh_flags & LM_FLAG_PRIORITY)
1137                 list_add(&gh->gh_list, &gl->gl_waiters3);
1138         else
1139                 list_add_tail(&gh->gh_list, &gl->gl_waiters3);
1140 }
1141
1142 /**
1143  * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock)
1144  * @gh: the holder structure
1145  *
1146  * if (gh->gh_flags & GL_ASYNC), this never returns an error
1147  *
1148  * Returns: 0, GLR_TRYFAILED, or errno on failure
1149  */
1150
1151 int gfs2_glock_nq(struct gfs2_holder *gh)
1152 {
1153         struct gfs2_glock *gl = gh->gh_gl;
1154         struct gfs2_sbd *sdp = gl->gl_sbd;
1155         int error = 0;
1156
1157 restart:
1158         if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1159                 set_bit(HIF_ABORTED, &gh->gh_iflags);
1160                 return -EIO;
1161         }
1162
1163         set_bit(HIF_PROMOTE, &gh->gh_iflags);
1164
1165         spin_lock(&gl->gl_spin);
1166         add_to_queue(gh);
1167         run_queue(gl);
1168         spin_unlock(&gl->gl_spin);
1169
1170         if (!(gh->gh_flags & GL_ASYNC)) {
1171                 error = glock_wait_internal(gh);
1172                 if (error == GLR_CANCELED) {
1173                         msleep(100);
1174                         goto restart;
1175                 }
1176         }
1177
1178         return error;
1179 }
1180
1181 /**
1182  * gfs2_glock_poll - poll to see if an async request has been completed
1183  * @gh: the holder
1184  *
1185  * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on
1186  */
1187
1188 int gfs2_glock_poll(struct gfs2_holder *gh)
1189 {
1190         struct gfs2_glock *gl = gh->gh_gl;
1191         int ready = 0;
1192
1193         spin_lock(&gl->gl_spin);
1194
1195         if (test_bit(HIF_HOLDER, &gh->gh_iflags))
1196                 ready = 1;
1197         else if (list_empty(&gh->gh_list)) {
1198                 if (gh->gh_error == GLR_CANCELED) {
1199                         spin_unlock(&gl->gl_spin);
1200                         msleep(100);
1201                         if (gfs2_glock_nq(gh))
1202                                 return 1;
1203                         return 0;
1204                 } else
1205                         ready = 1;
1206         }
1207
1208         spin_unlock(&gl->gl_spin);
1209
1210         return ready;
1211 }
1212
1213 /**
1214  * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC
1215  * @gh: the holder structure
1216  *
1217  * Returns: 0, GLR_TRYFAILED, or errno on failure
1218  */
1219
1220 int gfs2_glock_wait(struct gfs2_holder *gh)
1221 {
1222         int error;
1223
1224         error = glock_wait_internal(gh);
1225         if (error == GLR_CANCELED) {
1226                 msleep(100);
1227                 gh->gh_flags &= ~GL_ASYNC;
1228                 error = gfs2_glock_nq(gh);
1229         }
1230
1231         return error;
1232 }
1233
1234 /**
1235  * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock)
1236  * @gh: the glock holder
1237  *
1238  */
1239
1240 void gfs2_glock_dq(struct gfs2_holder *gh)
1241 {
1242         struct gfs2_glock *gl = gh->gh_gl;
1243         const struct gfs2_glock_operations *glops = gl->gl_ops;
1244         unsigned delay = 0;
1245
1246         if (gh->gh_flags & GL_NOCACHE)
1247                 handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
1248
1249         gfs2_glmutex_lock(gl);
1250
1251         spin_lock(&gl->gl_spin);
1252         list_del_init(&gh->gh_list);
1253
1254         if (list_empty(&gl->gl_holders)) {
1255                 if (glops->go_unlock) {
1256                         spin_unlock(&gl->gl_spin);
1257                         glops->go_unlock(gh);
1258                         spin_lock(&gl->gl_spin);
1259                 }
1260                 gl->gl_stamp = jiffies;
1261         }
1262
1263         clear_bit(GLF_LOCK, &gl->gl_flags);
1264         spin_unlock(&gl->gl_spin);
1265
1266         gfs2_glock_hold(gl);
1267         if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
1268             !test_bit(GLF_DEMOTE, &gl->gl_flags))
1269                 delay = gl->gl_ops->go_min_hold_time;
1270         if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1271                 gfs2_glock_put(gl);
1272 }
1273
1274 void gfs2_glock_dq_wait(struct gfs2_holder *gh)
1275 {
1276         struct gfs2_glock *gl = gh->gh_gl;
1277         gfs2_glock_dq(gh);
1278         wait_on_demote(gl);
1279 }
1280
1281 /**
1282  * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it
1283  * @gh: the holder structure
1284  *
1285  */
1286
1287 void gfs2_glock_dq_uninit(struct gfs2_holder *gh)
1288 {
1289         gfs2_glock_dq(gh);
1290         gfs2_holder_uninit(gh);
1291 }
1292
1293 /**
1294  * gfs2_glock_nq_num - acquire a glock based on lock number
1295  * @sdp: the filesystem
1296  * @number: the lock number
1297  * @glops: the glock operations for the type of glock
1298  * @state: the state to acquire the glock in
1299  * @flags: modifier flags for the aquisition
1300  * @gh: the struct gfs2_holder
1301  *
1302  * Returns: errno
1303  */
1304
1305 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
1306                       const struct gfs2_glock_operations *glops,
1307                       unsigned int state, int flags, struct gfs2_holder *gh)
1308 {
1309         struct gfs2_glock *gl;
1310         int error;
1311
1312         error = gfs2_glock_get(sdp, number, glops, CREATE, &gl);
1313         if (!error) {
1314                 error = gfs2_glock_nq_init(gl, state, flags, gh);
1315                 gfs2_glock_put(gl);
1316         }
1317
1318         return error;
1319 }
1320
1321 /**
1322  * glock_compare - Compare two struct gfs2_glock structures for sorting
1323  * @arg_a: the first structure
1324  * @arg_b: the second structure
1325  *
1326  */
1327
1328 static int glock_compare(const void *arg_a, const void *arg_b)
1329 {
1330         const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1331         const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1332         const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1333         const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1334
1335         if (a->ln_number > b->ln_number)
1336                 return 1;
1337         if (a->ln_number < b->ln_number)
1338                 return -1;
1339         BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type);
1340         return 0;
1341 }
1342
1343 /**
1344  * nq_m_sync - synchonously acquire more than one glock in deadlock free order
1345  * @num_gh: the number of structures
1346  * @ghs: an array of struct gfs2_holder structures
1347  *
1348  * Returns: 0 on success (all glocks acquired),
1349  *          errno on failure (no glocks acquired)
1350  */
1351
1352 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs,
1353                      struct gfs2_holder **p)
1354 {
1355         unsigned int x;
1356         int error = 0;
1357
1358         for (x = 0; x < num_gh; x++)
1359                 p[x] = &ghs[x];
1360
1361         sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL);
1362
1363         for (x = 0; x < num_gh; x++) {
1364                 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1365
1366                 error = gfs2_glock_nq(p[x]);
1367                 if (error) {
1368                         while (x--)
1369                                 gfs2_glock_dq(p[x]);
1370                         break;
1371                 }
1372         }
1373
1374         return error;
1375 }
1376
1377 /**
1378  * gfs2_glock_nq_m - acquire multiple glocks
1379  * @num_gh: the number of structures
1380  * @ghs: an array of struct gfs2_holder structures
1381  *
1382  *
1383  * Returns: 0 on success (all glocks acquired),
1384  *          errno on failure (no glocks acquired)
1385  */
1386
1387 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1388 {
1389         struct gfs2_holder *tmp[4];
1390         struct gfs2_holder **pph = tmp;
1391         int error = 0;
1392
1393         switch(num_gh) {
1394         case 0:
1395                 return 0;
1396         case 1:
1397                 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1398                 return gfs2_glock_nq(ghs);
1399         default:
1400                 if (num_gh <= 4)
1401                         break;
1402                 pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS);
1403                 if (!pph)
1404                         return -ENOMEM;
1405         }
1406
1407         error = nq_m_sync(num_gh, ghs, pph);
1408
1409         if (pph != tmp)
1410                 kfree(pph);
1411
1412         return error;
1413 }
1414
1415 /**
1416  * gfs2_glock_dq_m - release multiple glocks
1417  * @num_gh: the number of structures
1418  * @ghs: an array of struct gfs2_holder structures
1419  *
1420  */
1421
1422 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs)
1423 {
1424         unsigned int x;
1425
1426         for (x = 0; x < num_gh; x++)
1427                 gfs2_glock_dq(&ghs[x]);
1428 }
1429
1430 /**
1431  * gfs2_glock_dq_uninit_m - release multiple glocks
1432  * @num_gh: the number of structures
1433  * @ghs: an array of struct gfs2_holder structures
1434  *
1435  */
1436
1437 void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
1438 {
1439         unsigned int x;
1440
1441         for (x = 0; x < num_gh; x++)
1442                 gfs2_glock_dq_uninit(&ghs[x]);
1443 }
1444
1445 /**
1446  * gfs2_lvb_hold - attach a LVB from a glock
1447  * @gl: The glock in question
1448  *
1449  */
1450
1451 int gfs2_lvb_hold(struct gfs2_glock *gl)
1452 {
1453         int error;
1454
1455         gfs2_glmutex_lock(gl);
1456
1457         if (!atomic_read(&gl->gl_lvb_count)) {
1458                 error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
1459                 if (error) {
1460                         gfs2_glmutex_unlock(gl);
1461                         return error;
1462                 }
1463                 gfs2_glock_hold(gl);
1464         }
1465         atomic_inc(&gl->gl_lvb_count);
1466
1467         gfs2_glmutex_unlock(gl);
1468
1469         return 0;
1470 }
1471
1472 /**
1473  * gfs2_lvb_unhold - detach a LVB from a glock
1474  * @gl: The glock in question
1475  *
1476  */
1477
1478 void gfs2_lvb_unhold(struct gfs2_glock *gl)
1479 {
1480         gfs2_glock_hold(gl);
1481         gfs2_glmutex_lock(gl);
1482
1483         gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
1484         if (atomic_dec_and_test(&gl->gl_lvb_count)) {
1485                 gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb);
1486                 gl->gl_lvb = NULL;
1487                 gfs2_glock_put(gl);
1488         }
1489
1490         gfs2_glmutex_unlock(gl);
1491         gfs2_glock_put(gl);
1492 }
1493
1494 static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
1495                         unsigned int state)
1496 {
1497         struct gfs2_glock *gl;
1498         unsigned long delay = 0;
1499         unsigned long holdtime;
1500         unsigned long now = jiffies;
1501
1502         gl = gfs2_glock_find(sdp, name);
1503         if (!gl)
1504                 return;
1505
1506         holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
1507         if (time_before(now, holdtime))
1508                 delay = holdtime - now;
1509
1510         handle_callback(gl, state, 1, delay);
1511         if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
1512                 gfs2_glock_put(gl);
1513 }
1514
1515 /**
1516  * gfs2_glock_cb - Callback used by locking module
1517  * @sdp: Pointer to the superblock
1518  * @type: Type of callback
1519  * @data: Type dependent data pointer
1520  *
1521  * Called by the locking module when it wants to tell us something.
1522  * Either we need to drop a lock, one of our ASYNC requests completed, or
1523  * a journal from another client needs to be recovered.
1524  */
1525
1526 void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
1527 {
1528         struct gfs2_sbd *sdp = cb_data;
1529
1530         switch (type) {
1531         case LM_CB_NEED_E:
1532                 blocking_cb(sdp, data, LM_ST_UNLOCKED);
1533                 return;
1534
1535         case LM_CB_NEED_D:
1536                 blocking_cb(sdp, data, LM_ST_DEFERRED);
1537                 return;
1538
1539         case LM_CB_NEED_S:
1540                 blocking_cb(sdp, data, LM_ST_SHARED);
1541                 return;
1542
1543         case LM_CB_ASYNC: {
1544                 struct lm_async_cb *async = data;
1545                 struct gfs2_glock *gl;
1546
1547                 down_read(&gfs2_umount_flush_sem);
1548                 gl = gfs2_glock_find(sdp, &async->lc_name);
1549                 if (gfs2_assert_warn(sdp, gl))
1550                         return;
1551                 if (!gfs2_assert_warn(sdp, gl->gl_req_bh))
1552                         gl->gl_req_bh(gl, async->lc_ret);
1553                 if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
1554                         gfs2_glock_put(gl);
1555                 up_read(&gfs2_umount_flush_sem);
1556                 return;
1557         }
1558
1559         case LM_CB_NEED_RECOVERY:
1560                 gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
1561                 if (sdp->sd_recoverd_process)
1562                         wake_up_process(sdp->sd_recoverd_process);
1563                 return;
1564
1565         case LM_CB_DROPLOCKS:
1566                 gfs2_gl_hash_clear(sdp, NO_WAIT);
1567                 gfs2_quota_scan(sdp);
1568                 return;
1569
1570         default:
1571                 gfs2_assert_warn(sdp, 0);
1572                 return;
1573         }
1574 }
1575
1576 /**
1577  * demote_ok - Check to see if it's ok to unlock a glock
1578  * @gl: the glock
1579  *
1580  * Returns: 1 if it's ok
1581  */
1582
1583 static int demote_ok(struct gfs2_glock *gl)
1584 {
1585         const struct gfs2_glock_operations *glops = gl->gl_ops;
1586         int demote = 1;
1587
1588         if (test_bit(GLF_STICKY, &gl->gl_flags))
1589                 demote = 0;
1590         else if (glops->go_demote_ok)
1591                 demote = glops->go_demote_ok(gl);
1592
1593         return demote;
1594 }
1595
1596 /**
1597  * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
1598  * @gl: the glock
1599  *
1600  */
1601
1602 void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
1603 {
1604         struct gfs2_sbd *sdp = gl->gl_sbd;
1605
1606         spin_lock(&sdp->sd_reclaim_lock);
1607         if (list_empty(&gl->gl_reclaim)) {
1608                 gfs2_glock_hold(gl);
1609                 list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
1610                 atomic_inc(&sdp->sd_reclaim_count);
1611         }
1612         spin_unlock(&sdp->sd_reclaim_lock);
1613
1614         wake_up(&sdp->sd_reclaim_wq);
1615 }
1616
1617 /**
1618  * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
1619  * @sdp: the filesystem
1620  *
1621  * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
1622  * different glock and we notice that there are a lot of glocks in the
1623  * reclaim list.
1624  *
1625  */
1626
1627 void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
1628 {
1629         struct gfs2_glock *gl;
1630
1631         spin_lock(&sdp->sd_reclaim_lock);
1632         if (list_empty(&sdp->sd_reclaim_list)) {
1633                 spin_unlock(&sdp->sd_reclaim_lock);
1634                 return;
1635         }
1636         gl = list_entry(sdp->sd_reclaim_list.next,
1637                         struct gfs2_glock, gl_reclaim);
1638         list_del_init(&gl->gl_reclaim);
1639         spin_unlock(&sdp->sd_reclaim_lock);
1640
1641         atomic_dec(&sdp->sd_reclaim_count);
1642         atomic_inc(&sdp->sd_reclaimed);
1643
1644         if (gfs2_glmutex_trylock(gl)) {
1645                 if (list_empty(&gl->gl_holders) &&
1646                     gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1647                         handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
1648                 gfs2_glmutex_unlock(gl);
1649         }
1650
1651         gfs2_glock_put(gl);
1652 }
1653
1654 /**
1655  * examine_bucket - Call a function for glock in a hash bucket
1656  * @examiner: the function
1657  * @sdp: the filesystem
1658  * @bucket: the bucket
1659  *
1660  * Returns: 1 if the bucket has entries
1661  */
1662
1663 static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp,
1664                           unsigned int hash)
1665 {
1666         struct gfs2_glock *gl, *prev = NULL;
1667         int has_entries = 0;
1668         struct hlist_head *head = &gl_hash_table[hash].hb_list;
1669
1670         read_lock(gl_lock_addr(hash));
1671         /* Can't use hlist_for_each_entry - don't want prefetch here */
1672         if (hlist_empty(head))
1673                 goto out;
1674         gl = list_entry(head->first, struct gfs2_glock, gl_list);
1675         while(1) {
1676                 if (!sdp || gl->gl_sbd == sdp) {
1677                         gfs2_glock_hold(gl);
1678                         read_unlock(gl_lock_addr(hash));
1679                         if (prev)
1680                                 gfs2_glock_put(prev);
1681                         prev = gl;
1682                         examiner(gl);
1683                         has_entries = 1;
1684                         read_lock(gl_lock_addr(hash));
1685                 }
1686                 if (gl->gl_list.next == NULL)
1687                         break;
1688                 gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list);
1689         }
1690 out:
1691         read_unlock(gl_lock_addr(hash));
1692         if (prev)
1693                 gfs2_glock_put(prev);
1694         cond_resched();
1695         return has_entries;
1696 }
1697
1698 /**
1699  * scan_glock - look at a glock and see if we can reclaim it
1700  * @gl: the glock to look at
1701  *
1702  */
1703
1704 static void scan_glock(struct gfs2_glock *gl)
1705 {
1706         if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
1707                 return;
1708
1709         if (gfs2_glmutex_trylock(gl)) {
1710                 if (list_empty(&gl->gl_holders) &&
1711                     gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
1712                         goto out_schedule;
1713                 gfs2_glmutex_unlock(gl);
1714         }
1715         return;
1716
1717 out_schedule:
1718         gfs2_glmutex_unlock(gl);
1719         gfs2_glock_schedule_for_reclaim(gl);
1720 }
1721
1722 /**
1723  * clear_glock - look at a glock and see if we can free it from glock cache
1724  * @gl: the glock to look at
1725  *
1726  */
1727
1728 static void clear_glock(struct gfs2_glock *gl)
1729 {
1730         struct gfs2_sbd *sdp = gl->gl_sbd;
1731         int released;
1732
1733         spin_lock(&sdp->sd_reclaim_lock);
1734         if (!list_empty(&gl->gl_reclaim)) {
1735                 list_del_init(&gl->gl_reclaim);
1736                 atomic_dec(&sdp->sd_reclaim_count);
1737                 spin_unlock(&sdp->sd_reclaim_lock);
1738                 released = gfs2_glock_put(gl);
1739                 gfs2_assert(sdp, !released);
1740         } else {
1741                 spin_unlock(&sdp->sd_reclaim_lock);
1742         }
1743
1744         if (gfs2_glmutex_trylock(gl)) {
1745                 if (list_empty(&gl->gl_holders) &&
1746                     gl->gl_state != LM_ST_UNLOCKED)
1747                         handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
1748                 gfs2_glmutex_unlock(gl);
1749         }
1750 }
1751
1752 /**
1753  * gfs2_gl_hash_clear - Empty out the glock hash table
1754  * @sdp: the filesystem
1755  * @wait: wait until it's all gone
1756  *
1757  * Called when unmounting the filesystem, or when inter-node lock manager
1758  * requests DROPLOCKS because it is running out of capacity.
1759  */
1760
1761 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait)
1762 {
1763         unsigned long t;
1764         unsigned int x;
1765         int cont;
1766
1767         t = jiffies;
1768
1769         for (;;) {
1770                 cont = 0;
1771                 for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1772                         if (examine_bucket(clear_glock, sdp, x))
1773                                 cont = 1;
1774                 }
1775
1776                 if (!wait || !cont)
1777                         break;
1778
1779                 if (time_after_eq(jiffies,
1780                                   t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
1781                         fs_warn(sdp, "Unmount seems to be stalled. "
1782                                      "Dumping lock state...\n");
1783                         gfs2_dump_lockstate(sdp);
1784                         t = jiffies;
1785                 }
1786
1787                 down_write(&gfs2_umount_flush_sem);
1788                 invalidate_inodes(sdp->sd_vfs);
1789                 up_write(&gfs2_umount_flush_sem);
1790                 msleep(10);
1791         }
1792 }
1793
1794 /*
1795  *  Diagnostic routines to help debug distributed deadlock
1796  */
1797
1798 static void gfs2_print_symbol(struct glock_iter *gi, const char *fmt,
1799                               unsigned long address)
1800 {
1801         char buffer[KSYM_SYMBOL_LEN];
1802
1803         sprint_symbol(buffer, address);
1804         print_dbg(gi, fmt, buffer);
1805 }
1806
1807 /**
1808  * dump_holder - print information about a glock holder
1809  * @str: a string naming the type of holder
1810  * @gh: the glock holder
1811  *
1812  * Returns: 0 on success, -ENOBUFS when we run out of space
1813  */
1814
1815 static int dump_holder(struct glock_iter *gi, char *str,
1816                        struct gfs2_holder *gh)
1817 {
1818         unsigned int x;
1819         struct task_struct *gh_owner;
1820
1821         print_dbg(gi, "  %s\n", str);
1822         if (gh->gh_owner_pid) {
1823                 print_dbg(gi, "    owner = %ld ", (long)gh->gh_owner_pid);
1824                 gh_owner = find_task_by_pid(gh->gh_owner_pid);
1825                 if (gh_owner)
1826                         print_dbg(gi, "(%s)\n", gh_owner->comm);
1827                 else
1828                         print_dbg(gi, "(ended)\n");
1829         } else
1830                 print_dbg(gi, "    owner = -1\n");
1831         print_dbg(gi, "    gh_state = %u\n", gh->gh_state);
1832         print_dbg(gi, "    gh_flags =");
1833         for (x = 0; x < 32; x++)
1834                 if (gh->gh_flags & (1 << x))
1835                         print_dbg(gi, " %u", x);
1836         print_dbg(gi, " \n");
1837         print_dbg(gi, "    error = %d\n", gh->gh_error);
1838         print_dbg(gi, "    gh_iflags =");
1839         for (x = 0; x < 32; x++)
1840                 if (test_bit(x, &gh->gh_iflags))
1841                         print_dbg(gi, " %u", x);
1842         print_dbg(gi, " \n");
1843         gfs2_print_symbol(gi, "    initialized at: %s\n", gh->gh_ip);
1844
1845         return 0;
1846 }
1847
1848 /**
1849  * dump_inode - print information about an inode
1850  * @ip: the inode
1851  *
1852  * Returns: 0 on success, -ENOBUFS when we run out of space
1853  */
1854
1855 static int dump_inode(struct glock_iter *gi, struct gfs2_inode *ip)
1856 {
1857         unsigned int x;
1858
1859         print_dbg(gi, "  Inode:\n");
1860         print_dbg(gi, "    num = %llu/%llu\n",
1861                   (unsigned long long)ip->i_no_formal_ino,
1862                   (unsigned long long)ip->i_no_addr);
1863         print_dbg(gi, "    type = %u\n", IF2DT(ip->i_inode.i_mode));
1864         print_dbg(gi, "    i_flags =");
1865         for (x = 0; x < 32; x++)
1866                 if (test_bit(x, &ip->i_flags))
1867                         print_dbg(gi, " %u", x);
1868         print_dbg(gi, " \n");
1869         return 0;
1870 }
1871
1872 /**
1873  * dump_glock - print information about a glock
1874  * @gl: the glock
1875  * @count: where we are in the buffer
1876  *
1877  * Returns: 0 on success, -ENOBUFS when we run out of space
1878  */
1879
1880 static int dump_glock(struct glock_iter *gi, struct gfs2_glock *gl)
1881 {
1882         struct gfs2_holder *gh;
1883         unsigned int x;
1884         int error = -ENOBUFS;
1885         struct task_struct *gl_owner;
1886
1887         spin_lock(&gl->gl_spin);
1888
1889         print_dbg(gi, "Glock 0x%p (%u, 0x%llx)\n", gl, gl->gl_name.ln_type,
1890                    (unsigned long long)gl->gl_name.ln_number);
1891         print_dbg(gi, "  gl_flags =");
1892         for (x = 0; x < 32; x++) {
1893                 if (test_bit(x, &gl->gl_flags))
1894                         print_dbg(gi, " %u", x);
1895         }
1896         if (!test_bit(GLF_LOCK, &gl->gl_flags))
1897                 print_dbg(gi, " (unlocked)");
1898         print_dbg(gi, " \n");
1899         print_dbg(gi, "  gl_ref = %d\n", atomic_read(&gl->gl_ref));
1900         print_dbg(gi, "  gl_state = %u\n", gl->gl_state);
1901         if (gl->gl_owner_pid) {
1902                 gl_owner = find_task_by_pid(gl->gl_owner_pid);
1903                 if (gl_owner)
1904                         print_dbg(gi, "  gl_owner = pid %d (%s)\n",
1905                                   gl->gl_owner_pid, gl_owner->comm);
1906                 else
1907                         print_dbg(gi, "  gl_owner = %d (ended)\n",
1908                                   gl->gl_owner_pid);
1909         } else
1910                 print_dbg(gi, "  gl_owner = -1\n");
1911         print_dbg(gi, "  gl_ip = %lu\n", gl->gl_ip);
1912         print_dbg(gi, "  req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no");
1913         print_dbg(gi, "  req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no");
1914         print_dbg(gi, "  lvb_count = %d\n", atomic_read(&gl->gl_lvb_count));
1915         print_dbg(gi, "  object = %s\n", (gl->gl_object) ? "yes" : "no");
1916         print_dbg(gi, "  le = %s\n",
1917                    (list_empty(&gl->gl_le.le_list)) ? "no" : "yes");
1918         print_dbg(gi, "  reclaim = %s\n",
1919                    (list_empty(&gl->gl_reclaim)) ? "no" : "yes");
1920         if (gl->gl_aspace)
1921                 print_dbg(gi, "  aspace = 0x%p nrpages = %lu\n", gl->gl_aspace,
1922                            gl->gl_aspace->i_mapping->nrpages);
1923         else
1924                 print_dbg(gi, "  aspace = no\n");
1925         print_dbg(gi, "  ail = %d\n", atomic_read(&gl->gl_ail_count));
1926         if (gl->gl_req_gh) {
1927                 error = dump_holder(gi, "Request", gl->gl_req_gh);
1928                 if (error)
1929                         goto out;
1930         }
1931         list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1932                 error = dump_holder(gi, "Holder", gh);
1933                 if (error)
1934                         goto out;
1935         }
1936         list_for_each_entry(gh, &gl->gl_waiters1, gh_list) {
1937                 error = dump_holder(gi, "Waiter1", gh);
1938                 if (error)
1939                         goto out;
1940         }
1941         list_for_each_entry(gh, &gl->gl_waiters3, gh_list) {
1942                 error = dump_holder(gi, "Waiter3", gh);
1943                 if (error)
1944                         goto out;
1945         }
1946         if (test_bit(GLF_DEMOTE, &gl->gl_flags)) {
1947                 print_dbg(gi, "  Demotion req to state %u (%llu uS ago)\n",
1948                           gl->gl_demote_state, (unsigned long long)
1949                           (jiffies - gl->gl_demote_time)*(1000000/HZ));
1950         }
1951         if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) {
1952                 if (!test_bit(GLF_LOCK, &gl->gl_flags) &&
1953                         list_empty(&gl->gl_holders)) {
1954                         error = dump_inode(gi, gl->gl_object);
1955                         if (error)
1956                                 goto out;
1957                 } else {
1958                         error = -ENOBUFS;
1959                         print_dbg(gi, "  Inode: busy\n");
1960                 }
1961         }
1962
1963         error = 0;
1964
1965 out:
1966         spin_unlock(&gl->gl_spin);
1967         return error;
1968 }
1969
1970 /**
1971  * gfs2_dump_lockstate - print out the current lockstate
1972  * @sdp: the filesystem
1973  * @ub: the buffer to copy the information into
1974  *
1975  * If @ub is NULL, dump the lockstate to the console.
1976  *
1977  */
1978
1979 static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
1980 {
1981         struct gfs2_glock *gl;
1982         struct hlist_node *h;
1983         unsigned int x;
1984         int error = 0;
1985
1986         for (x = 0; x < GFS2_GL_HASH_SIZE; x++) {
1987
1988                 read_lock(gl_lock_addr(x));
1989
1990                 hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) {
1991                         if (gl->gl_sbd != sdp)
1992                                 continue;
1993
1994                         error = dump_glock(NULL, gl);
1995                         if (error)
1996                                 break;
1997                 }
1998
1999                 read_unlock(gl_lock_addr(x));
2000
2001                 if (error)
2002                         break;
2003         }
2004
2005
2006         return error;
2007 }
2008
2009 /**
2010  * gfs2_scand - Look for cached glocks and inodes to toss from memory
2011  * @sdp: Pointer to GFS2 superblock
2012  *
2013  * One of these daemons runs, finding candidates to add to sd_reclaim_list.
2014  * See gfs2_glockd()
2015  */
2016
2017 static int gfs2_scand(void *data)
2018 {
2019         unsigned x;
2020         unsigned delay;
2021
2022         while (!kthread_should_stop()) {
2023                 for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
2024                         examine_bucket(scan_glock, NULL, x);
2025                 if (freezing(current))
2026                         refrigerator();
2027                 delay = scand_secs;
2028                 if (delay < 1)
2029                         delay = 1;
2030                 schedule_timeout_interruptible(delay * HZ);
2031         }
2032
2033         return 0;
2034 }
2035
2036
2037
2038 int __init gfs2_glock_init(void)
2039 {
2040         unsigned i;
2041         for(i = 0; i < GFS2_GL_HASH_SIZE; i++) {
2042                 INIT_HLIST_HEAD(&gl_hash_table[i].hb_list);
2043         }
2044 #ifdef GL_HASH_LOCK_SZ
2045         for(i = 0; i < GL_HASH_LOCK_SZ; i++) {
2046                 rwlock_init(&gl_hash_locks[i]);
2047         }
2048 #endif
2049
2050         scand_process = kthread_run(gfs2_scand, NULL, "gfs2_scand");
2051         if (IS_ERR(scand_process))
2052                 return PTR_ERR(scand_process);
2053
2054         glock_workqueue = create_workqueue("glock_workqueue");
2055         if (IS_ERR(glock_workqueue)) {
2056                 kthread_stop(scand_process);
2057                 return PTR_ERR(glock_workqueue);
2058         }
2059
2060         return 0;
2061 }
2062
2063 void gfs2_glock_exit(void)
2064 {
2065         destroy_workqueue(glock_workqueue);
2066         kthread_stop(scand_process);
2067 }
2068
2069 module_param(scand_secs, uint, S_IRUGO|S_IWUSR);
2070 MODULE_PARM_DESC(scand_secs, "The number of seconds between scand runs");
2071
2072 static int gfs2_glock_iter_next(struct glock_iter *gi)
2073 {
2074         struct gfs2_glock *gl;
2075
2076 restart:
2077         read_lock(gl_lock_addr(gi->hash));
2078         gl = gi->gl;
2079         if (gl) {
2080                 gi->gl = hlist_entry(gl->gl_list.next,
2081                                      struct gfs2_glock, gl_list);
2082                 if (gi->gl)
2083                         gfs2_glock_hold(gi->gl);
2084         }
2085         read_unlock(gl_lock_addr(gi->hash));
2086         if (gl)
2087                 gfs2_glock_put(gl);
2088         if (gl && gi->gl == NULL)
2089                 gi->hash++;
2090         while(gi->gl == NULL) {
2091                 if (gi->hash >= GFS2_GL_HASH_SIZE)
2092                         return 1;
2093                 read_lock(gl_lock_addr(gi->hash));
2094                 gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
2095                                      struct gfs2_glock, gl_list);
2096                 if (gi->gl)
2097                         gfs2_glock_hold(gi->gl);
2098                 read_unlock(gl_lock_addr(gi->hash));
2099                 gi->hash++;
2100         }
2101
2102         if (gi->sdp != gi->gl->gl_sbd)
2103                 goto restart;
2104
2105         return 0;
2106 }
2107
2108 static void gfs2_glock_iter_free(struct glock_iter *gi)
2109 {
2110         if (gi->gl)
2111                 gfs2_glock_put(gi->gl);
2112         kfree(gi);
2113 }
2114
2115 static struct glock_iter *gfs2_glock_iter_init(struct gfs2_sbd *sdp)
2116 {
2117         struct glock_iter *gi;
2118
2119         gi = kmalloc(sizeof (*gi), GFP_KERNEL);
2120         if (!gi)
2121                 return NULL;
2122
2123         gi->sdp = sdp;
2124         gi->hash = 0;
2125         gi->seq = NULL;
2126         gi->gl = NULL;
2127         memset(gi->string, 0, sizeof(gi->string));
2128
2129         if (gfs2_glock_iter_next(gi)) {
2130                 gfs2_glock_iter_free(gi);
2131                 return NULL;
2132         }
2133
2134         return gi;
2135 }
2136
2137 static void *gfs2_glock_seq_start(struct seq_file *file, loff_t *pos)
2138 {
2139         struct glock_iter *gi;
2140         loff_t n = *pos;
2141
2142         gi = gfs2_glock_iter_init(file->private);
2143         if (!gi)
2144                 return NULL;
2145
2146         while(n--) {
2147                 if (gfs2_glock_iter_next(gi)) {
2148                         gfs2_glock_iter_free(gi);
2149                         return NULL;
2150                 }
2151         }
2152
2153         return gi;
2154 }
2155
2156 static void *gfs2_glock_seq_next(struct seq_file *file, void *iter_ptr,
2157                                  loff_t *pos)
2158 {
2159         struct glock_iter *gi = iter_ptr;
2160
2161         (*pos)++;
2162
2163         if (gfs2_glock_iter_next(gi)) {
2164                 gfs2_glock_iter_free(gi);
2165                 return NULL;
2166         }
2167
2168         return gi;
2169 }
2170
2171 static void gfs2_glock_seq_stop(struct seq_file *file, void *iter_ptr)
2172 {
2173         struct glock_iter *gi = iter_ptr;
2174         if (gi)
2175                 gfs2_glock_iter_free(gi);
2176 }
2177
2178 static int gfs2_glock_seq_show(struct seq_file *file, void *iter_ptr)
2179 {
2180         struct glock_iter *gi = iter_ptr;
2181
2182         gi->seq = file;
2183         dump_glock(gi, gi->gl);
2184
2185         return 0;
2186 }
2187
2188 static const struct seq_operations gfs2_glock_seq_ops = {
2189         .start = gfs2_glock_seq_start,
2190         .next  = gfs2_glock_seq_next,
2191         .stop  = gfs2_glock_seq_stop,
2192         .show  = gfs2_glock_seq_show,
2193 };
2194
2195 static int gfs2_debugfs_open(struct inode *inode, struct file *file)
2196 {
2197         struct seq_file *seq;
2198         int ret;
2199
2200         ret = seq_open(file, &gfs2_glock_seq_ops);
2201         if (ret)
2202                 return ret;
2203
2204         seq = file->private_data;
2205         seq->private = inode->i_private;
2206
2207         return 0;
2208 }
2209
2210 static const struct file_operations gfs2_debug_fops = {
2211         .owner   = THIS_MODULE,
2212         .open    = gfs2_debugfs_open,
2213         .read    = seq_read,
2214         .llseek  = seq_lseek,
2215         .release = seq_release
2216 };
2217
2218 int gfs2_create_debugfs_file(struct gfs2_sbd *sdp)
2219 {
2220         sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root);
2221         if (!sdp->debugfs_dir)
2222                 return -ENOMEM;
2223         sdp->debugfs_dentry_glocks = debugfs_create_file("glocks",
2224                                                          S_IFREG | S_IRUGO,
2225                                                          sdp->debugfs_dir, sdp,
2226                                                          &gfs2_debug_fops);
2227         if (!sdp->debugfs_dentry_glocks)
2228                 return -ENOMEM;
2229
2230         return 0;
2231 }
2232
2233 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp)
2234 {
2235         if (sdp && sdp->debugfs_dir) {
2236                 if (sdp->debugfs_dentry_glocks) {
2237                         debugfs_remove(sdp->debugfs_dentry_glocks);
2238                         sdp->debugfs_dentry_glocks = NULL;
2239                 }
2240                 debugfs_remove(sdp->debugfs_dir);
2241                 sdp->debugfs_dir = NULL;
2242         }
2243 }
2244
2245 int gfs2_register_debugfs(void)
2246 {
2247         gfs2_root = debugfs_create_dir("gfs2", NULL);
2248         return gfs2_root ? 0 : -ENOMEM;
2249 }
2250
2251 void gfs2_unregister_debugfs(void)
2252 {
2253         debugfs_remove(gfs2_root);
2254         gfs2_root = NULL;
2255 }