#include "daemon.h"
 #include "glock.h"
 #include "log.h"
-#include "quota.h"
 #include "recovery.h"
 #include "super.h"
 #include "util.h"
        return 0;
 }
 
-/**
- * gfs2_quotad - Write cached quota changes into the quota file
- * @sdp: Pointer to GFS2 superblock
- *
- */
-
-int gfs2_quotad(void *data)
-{
-       struct gfs2_sbd *sdp = data;
-       unsigned long t;
-       int error;
-
-       while (!kthread_should_stop()) {
-               /* Update the master statfs file */
-
-               t = sdp->sd_statfs_sync_time +
-                   gfs2_tune_get(sdp, gt_statfs_quantum) * HZ;
-
-               if (time_after_eq(jiffies, t)) {
-                       error = gfs2_statfs_sync(sdp);
-                       if (error &&
-                           error != -EROFS &&
-                           !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
-                               fs_err(sdp, "quotad: (1) error=%d\n", error);
-                       sdp->sd_statfs_sync_time = jiffies;
-               }
-
-               /* Update quota file */
-
-               t = sdp->sd_quota_sync_time +
-                   gfs2_tune_get(sdp, gt_quota_quantum) * HZ;
-
-               if (time_after_eq(jiffies, t)) {
-                       error = gfs2_quota_sync(sdp);
-                       if (error &&
-                           error != -EROFS &&
-                           !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
-                               fs_err(sdp, "quotad: (2) error=%d\n", error);
-                       sdp->sd_quota_sync_time = jiffies;
-               }
-
-               gfs2_quota_scan(sdp);
-
-               t = gfs2_tune_get(sdp, gt_quotad_secs) * HZ;
-               if (freezing(current))
-                       refrigerator();
-               schedule_timeout_interruptible(t);
-       }
-
-       return 0;
-}
-
 
 
        unsigned int gt_recoverd_secs;
        unsigned int gt_logd_secs;
-       unsigned int gt_quotad_secs;
 
        unsigned int gt_quota_simul_sync; /* Max quotavals to sync at once */
        unsigned int gt_quota_warn_period; /* Secs between quota warn msgs */
        spinlock_t sd_statfs_spin;
        struct gfs2_statfs_change_host sd_statfs_master;
        struct gfs2_statfs_change_host sd_statfs_local;
-       unsigned long sd_statfs_sync_time;
 
        /* Resource group stuff */
 
        atomic_t sd_quota_count;
        spinlock_t sd_quota_spin;
        struct mutex sd_quota_mutex;
+       wait_queue_head_t sd_quota_wait;
 
        unsigned int sd_quota_slots;
        unsigned int sd_quota_chunks;
        unsigned char **sd_quota_bitmap;
 
        u64 sd_quota_sync_gen;
-       unsigned long sd_quota_sync_time;
 
        /* Log stuff */
 
 
        if (!gfs2_rgrpd_cachep)
                goto fail;
 
+       gfs2_quotad_cachep = kmem_cache_create("gfs2_quotad",
+                                              sizeof(struct gfs2_quota_data),
+                                              0, 0, NULL);
+       if (!gfs2_quotad_cachep)
+               goto fail;
+
        error = register_filesystem(&gfs2_fs_type);
        if (error)
                goto fail;
 fail:
        gfs2_glock_exit();
 
+       if (gfs2_quotad_cachep)
+               kmem_cache_destroy(gfs2_quotad_cachep);
+
        if (gfs2_rgrpd_cachep)
                kmem_cache_destroy(gfs2_rgrpd_cachep);
 
        unregister_filesystem(&gfs2_fs_type);
        unregister_filesystem(&gfs2meta_fs_type);
 
+       kmem_cache_destroy(gfs2_quotad_cachep);
        kmem_cache_destroy(gfs2_rgrpd_cachep);
        kmem_cache_destroy(gfs2_bufdata_cachep);
        kmem_cache_destroy(gfs2_inode_cachep);
 
        gt->gt_log_flush_secs = 60;
        gt->gt_recoverd_secs = 60;
        gt->gt_logd_secs = 1;
-       gt->gt_quotad_secs = 5;
        gt->gt_quota_simul_sync = 64;
        gt->gt_quota_warn_period = 10;
        gt->gt_quota_scale_num = 1;
        INIT_LIST_HEAD(&sdp->sd_quota_list);
        spin_lock_init(&sdp->sd_quota_spin);
        mutex_init(&sdp->sd_quota_mutex);
+       init_waitqueue_head(&sdp->sd_quota_wait);
 
        spin_lock_init(&sdp->sd_log_lock);
 
        }
        sdp->sd_logd_process = p;
 
-       sdp->sd_statfs_sync_time = jiffies;
-       sdp->sd_quota_sync_time = jiffies;
-
        p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
        error = IS_ERR(p);
        if (error) {
 
 #include <linux/bio.h>
 #include <linux/gfs2_ondisk.h>
 #include <linux/lm_interface.h>
+#include <linux/kthread.h>
+#include <linux/freezer.h>
 
 #include "gfs2.h"
 #include "incore.h"
        struct gfs2_quota_data *qd;
        int error;
 
-       qd = kzalloc(sizeof(struct gfs2_quota_data), GFP_NOFS);
+       qd = kmem_cache_zalloc(gfs2_quotad_cachep, GFP_NOFS);
        if (!qd)
                return -ENOMEM;
 
        return 0;
 
 fail:
-       kfree(qd);
+       kmem_cache_free(gfs2_quotad_cachep, qd);
        return error;
 }
 
                if (qd || !create) {
                        if (new_qd) {
                                gfs2_lvb_unhold(new_qd->qd_gl);
-                               kfree(new_qd);
+                               kmem_cache_free(gfs2_quotad_cachep, new_qd);
                        }
                        *qdp = qd;
                        return 0;
        return error;
 }
 
-void gfs2_quota_scan(struct gfs2_sbd *sdp)
+static void gfs2_quota_scan(struct gfs2_sbd *sdp)
 {
        struct gfs2_quota_data *qd, *safe;
        LIST_HEAD(dead);
                gfs2_assert_warn(sdp, !qd->qd_bh_count);
 
                gfs2_lvb_unhold(qd->qd_gl);
-               kfree(qd);
+               kmem_cache_free(gfs2_quotad_cachep, qd);
        }
 }
 
                gfs2_assert_warn(sdp, !qd->qd_bh_count);
 
                gfs2_lvb_unhold(qd->qd_gl);
-               kfree(qd);
+               kmem_cache_free(gfs2_quotad_cachep, qd);
 
                spin_lock(&sdp->sd_quota_spin);
        }
        }
 }
 
+static void quotad_error(struct gfs2_sbd *sdp, const char *msg, int error)
+{
+       if (error == 0 || error == -EROFS)
+               return;
+       if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
+               fs_err(sdp, "gfs2_quotad: %s error %d\n", msg, error);
+}
+
+static void quotad_check_timeo(struct gfs2_sbd *sdp, const char *msg,
+                              int (*fxn)(struct gfs2_sbd *sdp),
+                              unsigned long t, unsigned long *timeo,
+                              unsigned int *new_timeo)
+{
+       if (t >= *timeo) {
+               int error = fxn(sdp);
+               quotad_error(sdp, msg, error);
+               *timeo = gfs2_tune_get_i(&sdp->sd_tune, new_timeo) * HZ;
+       } else {
+               *timeo -= t;
+       }
+}
+
+/**
+ * gfs2_quotad - Write cached quota changes into the quota file
+ * @sdp: Pointer to GFS2 superblock
+ *
+ */
+
+int gfs2_quotad(void *data)
+{
+       struct gfs2_sbd *sdp = data;
+       struct gfs2_tune *tune = &sdp->sd_tune;
+       unsigned long statfs_timeo = 0;
+       unsigned long quotad_timeo = 0;
+       unsigned long t = 0;
+       DEFINE_WAIT(wait);
+
+       while (!kthread_should_stop()) {
+
+               /* Update the master statfs file */
+               quotad_check_timeo(sdp, "statfs", gfs2_statfs_sync, t,
+                                  &statfs_timeo, &tune->gt_statfs_quantum);
+
+               /* Update quota file */
+               quotad_check_timeo(sdp, "sync", gfs2_quota_sync, t,
+                                  "ad_timeo, &tune->gt_quota_quantum);
+
+               /* FIXME: This should be turned into a shrinker */
+               gfs2_quota_scan(sdp);
+
+               if (freezing(current))
+                       refrigerator();
+               t = min(quotad_timeo, statfs_timeo);
+
+               prepare_to_wait(&sdp->sd_quota_wait, &wait, TASK_UNINTERRUPTIBLE);
+               t -= schedule_timeout(t);
+               finish_wait(&sdp->sd_quota_wait, &wait);
+       }
+
+       return 0;
+}
+
 
 int gfs2_quota_refresh(struct gfs2_sbd *sdp, int user, u32 id);
 
 int gfs2_quota_init(struct gfs2_sbd *sdp);
-void gfs2_quota_scan(struct gfs2_sbd *sdp);
 void gfs2_quota_cleanup(struct gfs2_sbd *sdp);
 
 static inline int gfs2_quota_lock_check(struct gfs2_inode *ip)
 
 TUNE_ATTR(statfs_quantum, 1);
 TUNE_ATTR_DAEMON(recoverd_secs, recoverd_process);
 TUNE_ATTR_DAEMON(logd_secs, logd_process);
-TUNE_ATTR_DAEMON(quotad_secs, quotad_process);
 TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
 
 static struct attribute *tune_attrs[] = {
        &tune_attr_statfs_quantum.attr,
        &tune_attr_recoverd_secs.attr,
        &tune_attr_logd_secs.attr,
-       &tune_attr_quotad_secs.attr,
        &tune_attr_quota_scale.attr,
        &tune_attr_new_files_jdata.attr,
        NULL,
 
 struct kmem_cache *gfs2_inode_cachep __read_mostly;
 struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
 struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
+struct kmem_cache *gfs2_quotad_cachep __read_mostly;
 
 void gfs2_assert_i(struct gfs2_sbd *sdp)
 {
 
 extern struct kmem_cache *gfs2_inode_cachep;
 extern struct kmem_cache *gfs2_bufdata_cachep;
 extern struct kmem_cache *gfs2_rgrpd_cachep;
+extern struct kmem_cache *gfs2_quotad_cachep;
 
 static inline unsigned int gfs2_tune_get_i(struct gfs2_tune *gt,
                                           unsigned int *p)