block: use blkdev_issue_discard in blk_ioctl_discard
[safe/jmp/linux-2.6] / fs / gfs2 / glock.c
index 8d5450f..8b674b1 100644 (file)
@@ -10,7 +10,6 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/completion.h>
 #include <linux/buffer_head.h>
 #include <linux/delay.h>
 #include <linux/sort.h>
@@ -18,7 +17,6 @@
 #include <linux/kallsyms.h>
 #include <linux/gfs2_ondisk.h>
 #include <linux/list.h>
-#include <linux/lm_interface.h>
 #include <linux/wait.h>
 #include <linux/module.h>
 #include <linux/rwsem.h>
@@ -40,6 +38,9 @@
 #include "quota.h"
 #include "super.h"
 #include "util.h"
+#include "bmap.h"
+#define CREATE_TRACE_POINTS
+#include "trace_gfs2.h"
 
 struct gfs2_gl_hash_bucket {
         struct hlist_head hb_list;
@@ -61,9 +62,11 @@ static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int
 
 static DECLARE_RWSEM(gfs2_umount_flush_sem);
 static struct dentry *gfs2_root;
-static struct task_struct *scand_process;
-static unsigned int scand_secs = 5;
 static struct workqueue_struct *glock_workqueue;
+struct workqueue_struct *gfs2_delete_workqueue;
+static LIST_HEAD(lru_list);
+static atomic_t lru_count = ATOMIC_INIT(0);
+static DEFINE_SPINLOCK(lru_lock);
 
 #define GFS2_GL_HASH_SHIFT      15
 #define GFS2_GL_HASH_SIZE       (1 << GFS2_GL_HASH_SHIFT)
@@ -153,13 +156,10 @@ static void glock_free(struct gfs2_glock *gl)
        struct gfs2_sbd *sdp = gl->gl_sbd;
        struct inode *aspace = gl->gl_aspace;
 
-       if (sdp->sd_lockstruct.ls_ops->lm_put_lock)
-               sdp->sd_lockstruct.ls_ops->lm_put_lock(gl->gl_lock);
-
        if (aspace)
                gfs2_aspace_put(aspace);
-
-       kmem_cache_free(gfs2_glock_cachep, gl);
+       trace_gfs2_glock_put(gl);
+       sdp->sd_lockstruct.ls_ops->lm_put_lock(gfs2_glock_cachep, gl);
 }
 
 /**
@@ -168,12 +168,69 @@ static void glock_free(struct gfs2_glock *gl)
  *
  */
 
-static void gfs2_glock_hold(struct gfs2_glock *gl)
+void gfs2_glock_hold(struct gfs2_glock *gl)
 {
+       GLOCK_BUG_ON(gl, atomic_read(&gl->gl_ref) == 0);
        atomic_inc(&gl->gl_ref);
 }
 
 /**
+ * demote_ok - Check to see if it's ok to unlock a glock
+ * @gl: the glock
+ *
+ * Returns: 1 if it's ok
+ */
+
+static int demote_ok(const struct gfs2_glock *gl)
+{
+       const struct gfs2_glock_operations *glops = gl->gl_ops;
+
+       if (gl->gl_state == LM_ST_UNLOCKED)
+               return 0;
+       if (!list_empty(&gl->gl_holders))
+               return 0;
+       if (glops->go_demote_ok)
+               return glops->go_demote_ok(gl);
+       return 1;
+}
+
+/**
+ * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
+ * @gl: the glock
+ *
+ */
+
+static void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
+{
+       int may_reclaim;
+       may_reclaim = (demote_ok(gl) &&
+                      (atomic_read(&gl->gl_ref) == 1 ||
+                       (gl->gl_name.ln_type == LM_TYPE_INODE &&
+                        atomic_read(&gl->gl_ref) <= 2)));
+       spin_lock(&lru_lock);
+       if (list_empty(&gl->gl_lru) && may_reclaim) {
+               list_add_tail(&gl->gl_lru, &lru_list);
+               atomic_inc(&lru_count);
+       }
+       spin_unlock(&lru_lock);
+}
+
+/**
+ * gfs2_glock_put_nolock() - Decrement reference count on glock
+ * @gl: The glock to put
+ *
+ * This function should only be used if the caller has its own reference
+ * to the glock, in addition to the one it is dropping.
+ */
+
+void gfs2_glock_put_nolock(struct gfs2_glock *gl)
+{
+       if (atomic_dec_and_test(&gl->gl_ref))
+               GLOCK_BUG_ON(gl, 1);
+       gfs2_glock_schedule_for_reclaim(gl);
+}
+
+/**
  * gfs2_glock_put() - Decrement reference count on glock
  * @gl: The glock to put
  *
@@ -187,13 +244,20 @@ int gfs2_glock_put(struct gfs2_glock *gl)
        if (atomic_dec_and_test(&gl->gl_ref)) {
                hlist_del(&gl->gl_list);
                write_unlock(gl_lock_addr(gl->gl_hash));
-               GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_UNLOCKED);
-               GLOCK_BUG_ON(gl, !list_empty(&gl->gl_reclaim));
+               spin_lock(&lru_lock);
+               if (!list_empty(&gl->gl_lru)) {
+                       list_del_init(&gl->gl_lru);
+                       atomic_dec(&lru_count);
+               }
+               spin_unlock(&lru_lock);
                GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders));
                glock_free(gl);
                rv = 1;
                goto out;
        }
+       spin_lock(&gl->gl_spin);
+       gfs2_glock_schedule_for_reclaim(gl);
+       spin_unlock(&gl->gl_spin);
        write_unlock(gl_lock_addr(gl->gl_hash));
 out:
        return rv;
@@ -229,27 +293,6 @@ static struct gfs2_glock *search_bucket(unsigned int hash,
 }
 
 /**
- * gfs2_glock_find() - Find glock by lock number
- * @sdp: The GFS2 superblock
- * @name: The lock name
- *
- * Returns: NULL, or the struct gfs2_glock with the requested number
- */
-
-static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp,
-                                         const struct lm_lockname *name)
-{
-       unsigned int hash = gl_hash(sdp, name);
-       struct gfs2_glock *gl;
-
-       read_lock(gl_lock_addr(hash));
-       gl = search_bucket(hash, sdp, name);
-       read_unlock(gl_lock_addr(hash));
-
-       return gl;
-}
-
-/**
  * may_grant - check if its ok to grant a new lock
  * @gl: The glock
  * @gh: The lock request which we wish to grant
@@ -267,8 +310,12 @@ static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holde
                return 1;
        if (gh->gh_flags & GL_EXACT)
                return 0;
-       if (gh->gh_state == LM_ST_SHARED && gl->gl_state == LM_ST_EXCLUSIVE)
-               return 1;
+       if (gl->gl_state == LM_ST_EXCLUSIVE) {
+               if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED)
+                       return 1;
+               if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED)
+                       return 1;
+       }
        if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY))
                return 1;
        return 0;
@@ -285,10 +332,13 @@ static void gfs2_holder_wake(struct gfs2_holder *gh)
  * do_promote - promote as many requests as possible on the current queue
  * @gl: The glock
  * 
- * Returns: true if there is a blocked holder at the head of the list
+ * Returns: 1 if there is a blocked holder at the head of the list, or 2
+ *          if a type specific operation is underway.
  */
 
 static int do_promote(struct gfs2_glock *gl)
+__releases(&gl->gl_spin)
+__acquires(&gl->gl_spin)
 {
        const struct gfs2_glock_operations *glops = gl->gl_ops;
        struct gfs2_holder *gh, *tmp;
@@ -306,16 +356,21 @@ restart:
                                ret = glops->go_lock(gh);
                                spin_lock(&gl->gl_spin);
                                if (ret) {
+                                       if (ret == 1)
+                                               return 2;
                                        gh->gh_error = ret;
                                        list_del_init(&gh->gh_list);
+                                       trace_gfs2_glock_queue(gh, 0);
                                        gfs2_holder_wake(gh);
                                        goto restart;
                                }
                                set_bit(HIF_HOLDER, &gh->gh_iflags);
+                               trace_gfs2_promote(gh, 1);
                                gfs2_holder_wake(gh);
                                goto restart;
                        }
                        set_bit(HIF_HOLDER, &gh->gh_iflags);
+                       trace_gfs2_promote(gh, 0);
                        gfs2_holder_wake(gh);
                        continue;
                }
@@ -345,6 +400,7 @@ static inline void do_error(struct gfs2_glock *gl, const int ret)
                else
                        continue;
                list_del_init(&gh->gh_list);
+               trace_gfs2_glock_queue(gh, 0);
                gfs2_holder_wake(gh);
        }
 }
@@ -383,7 +439,7 @@ static void state_change(struct gfs2_glock *gl, unsigned int new_state)
                if (held2)
                        gfs2_glock_hold(gl);
                else
-                       gfs2_glock_put(gl);
+                       gfs2_glock_put_nolock(gl);
        }
 
        gl->gl_state = new_state;
@@ -410,8 +466,10 @@ static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
        const struct gfs2_glock_operations *glops = gl->gl_ops;
        struct gfs2_holder *gh;
        unsigned state = ret & LM_OUT_ST_MASK;
+       int rv;
 
        spin_lock(&gl->gl_spin);
+       trace_gfs2_glock_state_change(gl, state);
        state_change(gl, state);
        gh = find_first_waiter(gl);
 
@@ -464,7 +522,6 @@ retry:
                gfs2_demote_wake(gl);
        if (state != LM_ST_UNLOCKED) {
                if (glops->go_xmote_bh) {
-                       int rv;
                        spin_unlock(&gl->gl_spin);
                        rv = glops->go_xmote_bh(gl, gh);
                        if (rv == -EAGAIN)
@@ -475,16 +532,19 @@ retry:
                                goto out;
                        }
                }
-               do_promote(gl);
+               rv = do_promote(gl);
+               if (rv == 2)
+                       goto out_locked;
        }
 out:
        clear_bit(GLF_LOCK, &gl->gl_flags);
+out_locked:
        spin_unlock(&gl->gl_spin);
        gfs2_glock_put(gl);
 }
 
 static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
-                                unsigned int cur_state, unsigned int req_state,
+                                unsigned int req_state,
                                 unsigned int flags)
 {
        int ret = LM_OUT_ERROR;
@@ -493,7 +553,7 @@ static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
                return req_state == LM_ST_UNLOCKED ? 0 : req_state;
 
        if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
-               ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock, cur_state,
+               ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock,
                                                         req_state, flags);
        return ret;
 }
@@ -507,6 +567,8 @@ static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock,
  */
 
 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target)
+__releases(&gl->gl_spin)
+__acquires(&gl->gl_spin)
 {
        const struct gfs2_glock_operations *glops = gl->gl_ops;
        struct gfs2_sbd *sdp = gl->gl_sbd;
@@ -534,7 +596,7 @@ static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int
            gl->gl_state == LM_ST_DEFERRED) &&
            !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)))
                lck_flags |= LM_FLAG_TRY_1CB;
-       ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, target, lck_flags);
+       ret = gfs2_lm_lock(sdp, gl, target, lck_flags);
 
        if (!(ret & LM_OUT_ASYNC)) {
                finish_xmote(gl, ret);
@@ -572,8 +634,11 @@ static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl)
  */
 
 static void run_queue(struct gfs2_glock *gl, const int nonblock)
+__releases(&gl->gl_spin)
+__acquires(&gl->gl_spin)
 {
        struct gfs2_holder *gh = NULL;
+       int ret;
 
        if (test_and_set_bit(GLF_LOCK, &gl->gl_flags))
                return;
@@ -583,15 +648,19 @@ static void run_queue(struct gfs2_glock *gl, const int nonblock)
        if (test_bit(GLF_DEMOTE, &gl->gl_flags) &&
            gl->gl_demote_state != gl->gl_state) {
                if (find_first_holder(gl))
-                       goto out;
+                       goto out_unlock;
                if (nonblock)
                        goto out_sched;
                set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags);
+               GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE);
                gl->gl_target = gl->gl_demote_state;
        } else {
                if (test_bit(GLF_DEMOTE, &gl->gl_flags))
                        gfs2_demote_wake(gl);
-               if (do_promote(gl) == 0)
+               ret = do_promote(gl);
+               if (ret == 0)
+                       goto out_unlock;
+               if (ret == 2)
                        goto out;
                gh = find_first_waiter(gl);
                gl->gl_target = gh->gh_state;
@@ -599,14 +668,39 @@ static void run_queue(struct gfs2_glock *gl, const int nonblock)
                        do_error(gl, 0); /* Fail queued try locks */
        }
        do_xmote(gl, gh, gl->gl_target);
+out:
        return;
 
 out_sched:
        gfs2_glock_hold(gl);
        if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
-               gfs2_glock_put(gl);
-out:
+               gfs2_glock_put_nolock(gl);
+out_unlock:
        clear_bit(GLF_LOCK, &gl->gl_flags);
+       goto out;
+}
+
+static void delete_work_func(struct work_struct *work)
+{
+       struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_delete);
+       struct gfs2_sbd *sdp = gl->gl_sbd;
+       struct gfs2_inode *ip = NULL;
+       struct inode *inode;
+       u64 no_addr = 0;
+
+       spin_lock(&gl->gl_spin);
+       ip = (struct gfs2_inode *)gl->gl_object;
+       if (ip)
+               no_addr = ip->i_no_addr;
+       spin_unlock(&gl->gl_spin);
+       if (ip) {
+               inode = gfs2_ilookup(sdp->sd_vfs, no_addr);
+               if (inode) {
+                       d_prune_aliases(inode);
+                       iput(inode);
+               }
+       }
+       gfs2_glock_put(gl);
 }
 
 static void glock_work_func(struct work_struct *work)
@@ -616,8 +710,11 @@ static void glock_work_func(struct work_struct *work)
 
        if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags))
                finish_xmote(gl, gl->gl_reply);
+       down_read(&gfs2_umount_flush_sem);
        spin_lock(&gl->gl_spin);
-       if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags)) {
+       if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
+           gl->gl_state != LM_ST_UNLOCKED &&
+           gl->gl_demote_state != LM_ST_EXCLUSIVE) {
                unsigned long holdtime, now = jiffies;
                holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
                if (time_before(now, holdtime))
@@ -626,23 +723,12 @@ static void glock_work_func(struct work_struct *work)
        }
        run_queue(gl, 0);
        spin_unlock(&gl->gl_spin);
+       up_read(&gfs2_umount_flush_sem);
        if (!delay ||
            queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
                gfs2_glock_put(gl);
 }
 
-static int gfs2_lm_get_lock(struct gfs2_sbd *sdp, struct lm_lockname *name,
-                    void **lockp)
-{
-       int error = -EIO;
-       if (!sdp->sd_lockstruct.ls_ops->lm_get_lock)
-               return 0;
-       if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
-               error = sdp->sd_lockstruct.ls_ops->lm_get_lock(
-                               sdp->sd_lockstruct.ls_lockspace, name, lockp);
-       return error;
-}
-
 /**
  * gfs2_glock_get() - Get a glock, or create one if one doesn't exist
  * @sdp: The GFS2 superblock
@@ -669,10 +755,11 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
        gl = search_bucket(hash, sdp, &name);
        read_unlock(gl_lock_addr(hash));
 
-       if (gl || !create) {
-               *glp = gl;
+       *glp = gl;
+       if (gl)
                return 0;
-       }
+       if (!create)
+               return -ENOENT;
 
        gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL);
        if (!gl)
@@ -686,12 +773,15 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
        gl->gl_demote_state = LM_ST_EXCLUSIVE;
        gl->gl_hash = hash;
        gl->gl_ops = glops;
-       gl->gl_stamp = jiffies;
+       snprintf(gl->gl_strname, GDLM_STRNAME_BYTES, "%8x%16llx", name.ln_type, (unsigned long long)number);
+       memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb));
+       gl->gl_lksb.sb_lvbptr = gl->gl_lvb;
        gl->gl_tchange = jiffies;
        gl->gl_object = NULL;
        gl->gl_sbd = sdp;
        gl->gl_aspace = NULL;
        INIT_DELAYED_WORK(&gl->gl_work, glock_work_func);
+       INIT_WORK(&gl->gl_delete, delete_work_func);
 
        /* If this glock protects actual on-disk data or metadata blocks,
           create a VFS inode to manage the pages/buffers holding them. */
@@ -703,10 +793,6 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
                }
        }
 
-       error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock);
-       if (error)
-               goto fail_aspace;
-
        write_lock(gl_lock_addr(hash));
        tmp = search_bucket(hash, sdp, &name);
        if (tmp) {
@@ -722,9 +808,6 @@ int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
 
        return 0;
 
-fail_aspace:
-       if (gl->gl_aspace)
-               gfs2_aspace_put(gl->gl_aspace);
 fail:
        kmem_cache_free(gfs2_glock_cachep, gl);
        return error;
@@ -785,22 +868,37 @@ void gfs2_holder_uninit(struct gfs2_holder *gh)
        gh->gh_ip = 0;
 }
 
-static int just_schedule(void *word)
+/**
+ * gfs2_glock_holder_wait
+ * @word: unused
+ *
+ * This function and gfs2_glock_demote_wait both show up in the WCHAN
+ * field. Thus I've separated these otherwise identical functions in
+ * order to be more informative to the user.
+ */
+
+static int gfs2_glock_holder_wait(void *word)
 {
         schedule();
         return 0;
 }
 
+static int gfs2_glock_demote_wait(void *word)
+{
+       schedule();
+       return 0;
+}
+
 static void wait_on_holder(struct gfs2_holder *gh)
 {
        might_sleep();
-       wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE);
+       wait_on_bit(&gh->gh_iflags, HIF_WAIT, gfs2_glock_holder_wait, TASK_UNINTERRUPTIBLE);
 }
 
 static void wait_on_demote(struct gfs2_glock *gl)
 {
        might_sleep();
-       wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE);
+       wait_on_bit(&gl->gl_flags, GLF_DEMOTE, gfs2_glock_demote_wait, TASK_UNINTERRUPTIBLE);
 }
 
 /**
@@ -813,7 +911,7 @@ static void wait_on_demote(struct gfs2_glock *gl)
  */
 
 static void handle_callback(struct gfs2_glock *gl, unsigned int state,
-                           int remote, unsigned long delay)
+                           unsigned long delay)
 {
        int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE;
 
@@ -821,13 +919,13 @@ static void handle_callback(struct gfs2_glock *gl, unsigned int state,
        if (gl->gl_demote_state == LM_ST_EXCLUSIVE) {
                gl->gl_demote_state = state;
                gl->gl_demote_time = jiffies;
-               if (remote && gl->gl_ops->go_type == LM_TYPE_IOPEN &&
-                   gl->gl_object)
-                       gfs2_glock_schedule_for_reclaim(gl);
        } else if (gl->gl_demote_state != LM_ST_UNLOCKED &&
                        gl->gl_demote_state != state) {
                gl->gl_demote_state = LM_ST_UNLOCKED;
        }
+       if (gl->gl_ops->go_callback)
+               gl->gl_ops->go_callback(gl);
+       trace_gfs2_demote_rq(gl);
 }
 
 /**
@@ -870,6 +968,8 @@ void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...)
  */
 
 static inline void add_to_queue(struct gfs2_holder *gh)
+__releases(&gl->gl_spin)
+__acquires(&gl->gl_spin)
 {
        struct gfs2_glock *gl = gh->gh_gl;
        struct gfs2_sbd *sdp = gl->gl_sbd;
@@ -911,13 +1011,14 @@ fail:
                        goto do_cancel;
                return;
        }
+       trace_gfs2_glock_queue(gh, 1);
        list_add_tail(&gh->gh_list, insert_pt);
 do_cancel:
        gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
        if (!(gh->gh_flags & LM_FLAG_PRIORITY)) {
                spin_unlock(&gl->gl_spin);
                if (sdp->sd_lockstruct.ls_ops->lm_cancel)
-                       sdp->sd_lockstruct.ls_ops->lm_cancel(gl->gl_lock);
+                       sdp->sd_lockstruct.ls_ops->lm_cancel(gl);
                spin_lock(&gl->gl_spin);
        }
        return;
@@ -991,7 +1092,7 @@ void gfs2_glock_dq(struct gfs2_holder *gh)
 
        spin_lock(&gl->gl_spin);
        if (gh->gh_flags & GL_NOCACHE)
-               handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
+               handle_callback(gl, LM_ST_UNLOCKED, 0);
 
        list_del_init(&gh->gh_list);
        if (find_first_holder(gl) == NULL) {
@@ -1002,12 +1103,12 @@ void gfs2_glock_dq(struct gfs2_holder *gh)
                        spin_lock(&gl->gl_spin);
                        clear_bit(GLF_LOCK, &gl->gl_flags);
                }
-               gl->gl_stamp = jiffies;
                if (list_empty(&gl->gl_holders) &&
                    !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) &&
                    !test_bit(GLF_DEMOTE, &gl->gl_flags))
                        fast_path = 1;
        }
+       trace_gfs2_glock_queue(gh, 0);
        spin_unlock(&gl->gl_spin);
        if (likely(fast_path))
                return;
@@ -1191,218 +1292,111 @@ void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs)
                gfs2_glock_dq_uninit(&ghs[x]);
 }
 
-static int gfs2_lm_hold_lvb(struct gfs2_sbd *sdp, void *lock, char **lvbp)
-{
-       int error = -EIO;
-       if (!sdp->sd_lockstruct.ls_ops->lm_hold_lvb)
-               return 0;
-       if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
-               error = sdp->sd_lockstruct.ls_ops->lm_hold_lvb(lock, lvbp);
-       return error;
-}
-
-/**
- * gfs2_lvb_hold - attach a LVB from a glock
- * @gl: The glock in question
- *
- */
-
-int gfs2_lvb_hold(struct gfs2_glock *gl)
-{
-       int error;
-
-       if (!atomic_read(&gl->gl_lvb_count)) {
-               error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb);
-               if (error) 
-                       return error;
-               gfs2_glock_hold(gl);
-       }
-       atomic_inc(&gl->gl_lvb_count);
-
-       return 0;
-}
-
-/**
- * gfs2_lvb_unhold - detach a LVB from a glock
- * @gl: The glock in question
- *
- */
-
-void gfs2_lvb_unhold(struct gfs2_glock *gl)
-{
-       struct gfs2_sbd *sdp = gl->gl_sbd;
-
-       gfs2_glock_hold(gl);
-       gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0);
-       if (atomic_dec_and_test(&gl->gl_lvb_count)) {
-               if (sdp->sd_lockstruct.ls_ops->lm_unhold_lvb)
-                       sdp->sd_lockstruct.ls_ops->lm_unhold_lvb(gl->gl_lock, gl->gl_lvb);
-               gl->gl_lvb = NULL;
-               gfs2_glock_put(gl);
-       }
-       gfs2_glock_put(gl);
-}
-
-static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name,
-                       unsigned int state)
+void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state)
 {
-       struct gfs2_glock *gl;
        unsigned long delay = 0;
        unsigned long holdtime;
        unsigned long now = jiffies;
 
-       gl = gfs2_glock_find(sdp, name);
-       if (!gl)
-               return;
-
+       gfs2_glock_hold(gl);
        holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time;
        if (time_before(now, holdtime))
                delay = holdtime - now;
+       if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags))
+               delay = gl->gl_ops->go_min_hold_time;
 
        spin_lock(&gl->gl_spin);
-       handle_callback(gl, state, 1, delay);
+       handle_callback(gl, state, delay);
        spin_unlock(&gl->gl_spin);
        if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0)
                gfs2_glock_put(gl);
 }
 
 /**
- * gfs2_glock_cb - Callback used by locking module
- * @sdp: Pointer to the superblock
- * @type: Type of callback
- * @data: Type dependent data pointer
+ * gfs2_glock_complete - Callback used by locking
+ * @gl: Pointer to the glock
+ * @ret: The return value from the dlm
  *
- * Called by the locking module when it wants to tell us something.
- * Either we need to drop a lock, one of our ASYNC requests completed, or
- * a journal from another client needs to be recovered.
  */
 
-void gfs2_glock_cb(void *cb_data, unsigned int type, void *data)
+void gfs2_glock_complete(struct gfs2_glock *gl, int ret)
 {
-       struct gfs2_sbd *sdp = cb_data;
-
-       switch (type) {
-       case LM_CB_NEED_E:
-               blocking_cb(sdp, data, LM_ST_UNLOCKED);
-               return;
-
-       case LM_CB_NEED_D:
-               blocking_cb(sdp, data, LM_ST_DEFERRED);
-               return;
-
-       case LM_CB_NEED_S:
-               blocking_cb(sdp, data, LM_ST_SHARED);
-               return;
-
-       case LM_CB_ASYNC: {
-               struct lm_async_cb *async = data;
-               struct gfs2_glock *gl;
-
-               down_read(&gfs2_umount_flush_sem);
-               gl = gfs2_glock_find(sdp, &async->lc_name);
-               if (gfs2_assert_warn(sdp, gl))
+       struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
+       gl->gl_reply = ret;
+       if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))) {
+               struct gfs2_holder *gh;
+               spin_lock(&gl->gl_spin);
+               gh = find_first_waiter(gl);
+               if ((!(gh && (gh->gh_flags & LM_FLAG_NOEXP)) &&
+                    (gl->gl_target != LM_ST_UNLOCKED)) ||
+                   ((ret & ~LM_OUT_ST_MASK) != 0))
+                       set_bit(GLF_FROZEN, &gl->gl_flags);
+               spin_unlock(&gl->gl_spin);
+               if (test_bit(GLF_FROZEN, &gl->gl_flags))
                        return;
-               gl->gl_reply = async->lc_ret;
-               set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
-               if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
-                       gfs2_glock_put(gl);
-               up_read(&gfs2_umount_flush_sem);
-               return;
        }
-
-       case LM_CB_NEED_RECOVERY:
-               gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data);
-               if (sdp->sd_recoverd_process)
-                       wake_up_process(sdp->sd_recoverd_process);
-               return;
-
-       default:
-               gfs2_assert_warn(sdp, 0);
-               return;
-       }
-}
-
-/**
- * demote_ok - Check to see if it's ok to unlock a glock
- * @gl: the glock
- *
- * Returns: 1 if it's ok
- */
-
-static int demote_ok(struct gfs2_glock *gl)
-{
-       const struct gfs2_glock_operations *glops = gl->gl_ops;
-       int demote = 1;
-
-       if (test_bit(GLF_STICKY, &gl->gl_flags))
-               demote = 0;
-       else if (glops->go_demote_ok)
-               demote = glops->go_demote_ok(gl);
-
-       return demote;
+       set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
+       gfs2_glock_hold(gl);
+       if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
+               gfs2_glock_put(gl);
 }
 
-/**
- * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list
- * @gl: the glock
- *
- */
 
-void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl)
+static int gfs2_shrink_glock_memory(int nr, gfp_t gfp_mask)
 {
-       struct gfs2_sbd *sdp = gl->gl_sbd;
-
-       spin_lock(&sdp->sd_reclaim_lock);
-       if (list_empty(&gl->gl_reclaim)) {
-               gfs2_glock_hold(gl);
-               list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list);
-               atomic_inc(&sdp->sd_reclaim_count);
-               spin_unlock(&sdp->sd_reclaim_lock);
-               wake_up(&sdp->sd_reclaim_wq);
-       } else
-               spin_unlock(&sdp->sd_reclaim_lock);
-}
+       struct gfs2_glock *gl;
+       int may_demote;
+       int nr_skipped = 0;
+       LIST_HEAD(skipped);
 
-/**
- * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list
- * @sdp: the filesystem
- *
- * Called from gfs2_glockd() glock reclaim daemon, or when promoting a
- * different glock and we notice that there are a lot of glocks in the
- * reclaim list.
- *
- */
+       if (nr == 0)
+               goto out;
 
-void gfs2_reclaim_glock(struct gfs2_sbd *sdp)
-{
-       struct gfs2_glock *gl;
-       int done_callback = 0;
+       if (!(gfp_mask & __GFP_FS))
+               return -1;
 
-       spin_lock(&sdp->sd_reclaim_lock);
-       if (list_empty(&sdp->sd_reclaim_list)) {
-               spin_unlock(&sdp->sd_reclaim_lock);
-               return;
-       }
-       gl = list_entry(sdp->sd_reclaim_list.next,
-                       struct gfs2_glock, gl_reclaim);
-       list_del_init(&gl->gl_reclaim);
-       spin_unlock(&sdp->sd_reclaim_lock);
+       spin_lock(&lru_lock);
+       while(nr && !list_empty(&lru_list)) {
+               gl = list_entry(lru_list.next, struct gfs2_glock, gl_lru);
+               list_del_init(&gl->gl_lru);
+               atomic_dec(&lru_count);
 
-       atomic_dec(&sdp->sd_reclaim_count);
-       atomic_inc(&sdp->sd_reclaimed);
+               /* Check if glock is about to be freed */
+               if (atomic_read(&gl->gl_ref) == 0)
+                       continue;
 
-       spin_lock(&gl->gl_spin);
-       if (find_first_holder(gl) == NULL &&
-           gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl)) {
-               handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
-               done_callback = 1;
+               /* Test for being demotable */
+               if (!test_and_set_bit(GLF_LOCK, &gl->gl_flags)) {
+                       gfs2_glock_hold(gl);
+                       spin_unlock(&lru_lock);
+                       spin_lock(&gl->gl_spin);
+                       may_demote = demote_ok(gl);
+                       if (may_demote) {
+                               handle_callback(gl, LM_ST_UNLOCKED, 0);
+                               nr--;
+                       }
+                       if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
+                               gfs2_glock_put_nolock(gl);
+                       spin_unlock(&gl->gl_spin);
+                       clear_bit(GLF_LOCK, &gl->gl_flags);
+                       spin_lock(&lru_lock);
+                       continue;
+               }
+               nr_skipped++;
+               list_add(&gl->gl_lru, &skipped);
        }
-       spin_unlock(&gl->gl_spin);
-       if (!done_callback ||
-           queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
-               gfs2_glock_put(gl);
+       list_splice(&skipped, &lru_list);
+       atomic_add(nr_skipped, &lru_count);
+       spin_unlock(&lru_lock);
+out:
+       return (atomic_read(&lru_count) / 100) * sysctl_vfs_cache_pressure;
 }
 
+static struct shrinker glock_shrinker = {
+       .shrink = gfs2_shrink_glock_memory,
+       .seeks = DEFAULT_SEEKS,
+};
+
 /**
  * examine_bucket - Call a function for glock in a hash bucket
  * @examiner: the function
@@ -1447,24 +1441,23 @@ out:
        return has_entries;
 }
 
+
 /**
- * scan_glock - look at a glock and see if we can reclaim it
- * @gl: the glock to look at
+ * thaw_glock - thaw out a glock which has an unprocessed reply waiting
+ * @gl: The glock to thaw
  *
+ * N.B. When we freeze a glock, we leave a ref to the glock outstanding,
+ * so this has to result in the ref count being dropped by one.
  */
 
-static void scan_glock(struct gfs2_glock *gl)
+static void thaw_glock(struct gfs2_glock *gl)
 {
-       if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object)
-               return;
-       if (test_bit(GLF_LOCK, &gl->gl_flags))
+       if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))
                return;
-
-       spin_lock(&gl->gl_spin);
-       if (find_first_holder(gl) == NULL &&
-           gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl))
-               gfs2_glock_schedule_for_reclaim(gl);
-       spin_unlock(&gl->gl_spin);
+       set_bit(GLF_REPLY_PENDING, &gl->gl_flags);
+       gfs2_glock_hold(gl);
+       if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
+               gfs2_glock_put(gl);
 }
 
 /**
@@ -1475,23 +1468,16 @@ static void scan_glock(struct gfs2_glock *gl)
 
 static void clear_glock(struct gfs2_glock *gl)
 {
-       struct gfs2_sbd *sdp = gl->gl_sbd;
-       int released;
-
-       spin_lock(&sdp->sd_reclaim_lock);
-       if (!list_empty(&gl->gl_reclaim)) {
-               list_del_init(&gl->gl_reclaim);
-               atomic_dec(&sdp->sd_reclaim_count);
-               spin_unlock(&sdp->sd_reclaim_lock);
-               released = gfs2_glock_put(gl);
-               gfs2_assert(sdp, !released);
-       } else {
-               spin_unlock(&sdp->sd_reclaim_lock);
+       spin_lock(&lru_lock);
+       if (!list_empty(&gl->gl_lru)) {
+               list_del_init(&gl->gl_lru);
+               atomic_dec(&lru_count);
        }
+       spin_unlock(&lru_lock);
 
        spin_lock(&gl->gl_spin);
        if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED)
-               handle_callback(gl, LM_ST_UNLOCKED, 0, 0);
+               handle_callback(gl, LM_ST_UNLOCKED, 0);
        spin_unlock(&gl->gl_spin);
        gfs2_glock_hold(gl);
        if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0)
@@ -1499,6 +1485,20 @@ static void clear_glock(struct gfs2_glock *gl)
 }
 
 /**
+ * gfs2_glock_thaw - Thaw any frozen glocks
+ * @sdp: The super block
+ *
+ */
+
+void gfs2_glock_thaw(struct gfs2_sbd *sdp)
+{
+       unsigned x;
+
+       for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
+               examine_bucket(thaw_glock, sdp, x);
+}
+
+/**
  * gfs2_gl_hash_clear - Empty out the glock hash table
  * @sdp: the filesystem
  * @wait: wait until it's all gone
@@ -1539,6 +1539,20 @@ void gfs2_gl_hash_clear(struct gfs2_sbd *sdp)
        }
 }
 
+void gfs2_glock_finish_truncate(struct gfs2_inode *ip)
+{
+       struct gfs2_glock *gl = ip->i_gl;
+       int ret;
+
+       ret = gfs2_truncatei_resume(ip);
+       gfs2_assert_withdraw(gl->gl_sbd, ret == 0);
+
+       spin_lock(&gl->gl_spin);
+       clear_bit(GLF_LOCK, &gl->gl_flags);
+       run_queue(gl, 1);
+       spin_unlock(&gl->gl_spin);
+}
+
 static const char *state2str(unsigned state)
 {
        switch(state) {
@@ -1564,15 +1578,13 @@ static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags)
        if (flags & LM_FLAG_NOEXP)
                *p++ = 'e';
        if (flags & LM_FLAG_ANY)
-               *p++ = 'a';
+               *p++ = 'A';
        if (flags & LM_FLAG_PRIORITY)
                *p++ = 'p';
        if (flags & GL_ASYNC)
                *p++ = 'a';
        if (flags & GL_EXACT)
                *p++ = 'E';
-       if (flags & GL_ATIME)
-               *p++ = 'a';
        if (flags & GL_NOCACHE)
                *p++ = 'c';
        if (test_bit(HIF_HOLDER, &iflags))
@@ -1616,8 +1628,6 @@ static const char *gflags2str(char *buf, const unsigned long *gflags)
        char *p = buf;
        if (test_bit(GLF_LOCK, gflags))
                *p++ = 'l';
-       if (test_bit(GLF_STICKY, gflags))
-               *p++ = 's';
        if (test_bit(GLF_DEMOTE, gflags))
                *p++ = 'D';
        if (test_bit(GLF_PENDING_DEMOTE, gflags))
@@ -1632,6 +1642,10 @@ static const char *gflags2str(char *buf, const unsigned long *gflags)
                *p++ = 'i';
        if (test_bit(GLF_REPLY_PENDING, gflags))
                *p++ = 'r';
+       if (test_bit(GLF_INITIAL, gflags))
+               *p++ = 'I';
+       if (test_bit(GLF_FROZEN, gflags))
+               *p++ = 'F';
        *p = 0;
        return buf;
 }
@@ -1666,14 +1680,13 @@ static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl)
        dtime *= 1000000/HZ; /* demote time in uSec */
        if (!test_bit(GLF_DEMOTE, &gl->gl_flags))
                dtime = 0;
-       gfs2_print_dbg(seq, "G:  s:%s n:%u/%llu f:%s t:%s d:%s/%llu l:%d a:%d r:%d\n",
+       gfs2_print_dbg(seq, "G:  s:%s n:%u/%llu f:%s t:%s d:%s/%llu a:%d r:%d\n",
                  state2str(gl->gl_state),
                  gl->gl_name.ln_type,
                  (unsigned long long)gl->gl_name.ln_number,
                  gflags2str(gflags_buf, &gl->gl_flags),
                  state2str(gl->gl_target),
                  state2str(gl->gl_demote_state), dtime,
-                 atomic_read(&gl->gl_lvb_count),
                  atomic_read(&gl->gl_ail_count),
                  atomic_read(&gl->gl_ref));
 
@@ -1736,34 +1749,6 @@ static int gfs2_dump_lockstate(struct gfs2_sbd *sdp)
        return error;
 }
 
-/**
- * gfs2_scand - Look for cached glocks and inodes to toss from memory
- * @sdp: Pointer to GFS2 superblock
- *
- * One of these daemons runs, finding candidates to add to sd_reclaim_list.
- * See gfs2_glockd()
- */
-
-static int gfs2_scand(void *data)
-{
-       unsigned x;
-       unsigned delay;
-
-       while (!kthread_should_stop()) {
-               for (x = 0; x < GFS2_GL_HASH_SIZE; x++)
-                       examine_bucket(scan_glock, NULL, x);
-               if (freezing(current))
-                       refrigerator();
-               delay = scand_secs;
-               if (delay < 1)
-                       delay = 1;
-               schedule_timeout_interruptible(delay * HZ);
-       }
-
-       return 0;
-}
-
-
 
 int __init gfs2_glock_init(void)
 {
@@ -1777,28 +1762,27 @@ int __init gfs2_glock_init(void)
        }
 #endif
 
-       scand_process = kthread_run(gfs2_scand, NULL, "gfs2_scand");
-       if (IS_ERR(scand_process))
-               return PTR_ERR(scand_process);
-
        glock_workqueue = create_workqueue("glock_workqueue");
-       if (IS_ERR(glock_workqueue)) {
-               kthread_stop(scand_process);
+       if (IS_ERR(glock_workqueue))
                return PTR_ERR(glock_workqueue);
+       gfs2_delete_workqueue = create_workqueue("delete_workqueue");
+       if (IS_ERR(gfs2_delete_workqueue)) {
+               destroy_workqueue(glock_workqueue);
+               return PTR_ERR(gfs2_delete_workqueue);
        }
 
+       register_shrinker(&glock_shrinker);
+
        return 0;
 }
 
 void gfs2_glock_exit(void)
 {
+       unregister_shrinker(&glock_shrinker);
        destroy_workqueue(glock_workqueue);
-       kthread_stop(scand_process);
+       destroy_workqueue(gfs2_delete_workqueue);
 }
 
-module_param(scand_secs, uint, S_IRUGO|S_IWUSR);
-MODULE_PARM_DESC(scand_secs, "The number of seconds between scand runs");
-
 static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi)
 {
        struct gfs2_glock *gl;
@@ -1809,15 +1793,17 @@ restart:
        if (gl) {
                gi->gl = hlist_entry(gl->gl_list.next,
                                     struct gfs2_glock, gl_list);
-               if (gi->gl)
-                       gfs2_glock_hold(gi->gl);
+       } else {
+               gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first,
+                                    struct gfs2_glock, gl_list);
        }
+       if (gi->gl)
+               gfs2_glock_hold(gi->gl);
        read_unlock(gl_lock_addr(gi->hash));
        if (gl)
                gfs2_glock_put(gl);
-       if (gl && gi->gl == NULL)
-               gi->hash++;
        while (gi->gl == NULL) {
+               gi->hash++;
                if (gi->hash >= GFS2_GL_HASH_SIZE)
                        return 1;
                read_lock(gl_lock_addr(gi->hash));
@@ -1826,7 +1812,6 @@ restart:
                if (gi->gl)
                        gfs2_glock_hold(gi->gl);
                read_unlock(gl_lock_addr(gi->hash));
-               gi->hash++;
        }
 
        if (gi->sdp != gi->gl->gl_sbd)