tunnels: fix netns vs proto registration ordering
[safe/jmp/linux-2.6] / fs / ocfs2 / localalloc.c
index f2f384d..ac10f83 100644 (file)
@@ -35,6 +35,7 @@
 #include "ocfs2.h"
 
 #include "alloc.h"
+#include "blockcheck.h"
 #include "dlmglue.h"
 #include "inode.h"
 #include "journal.h"
@@ -47,8 +48,6 @@
 
 #define OCFS2_LOCAL_ALLOC(dinode)      (&((dinode)->id2.i_lab))
 
-static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
-
 static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
 
 static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
@@ -58,57 +57,83 @@ static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
 static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
 
 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
-                                   struct ocfs2_journal_handle *handle,
+                                   handle_t *handle,
                                    struct ocfs2_dinode *alloc,
                                    struct inode *main_bm_inode,
                                    struct buffer_head *main_bm_bh);
 
 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
-                                               struct ocfs2_journal_handle *handle,
                                                struct ocfs2_alloc_context **ac,
                                                struct inode **bitmap_inode,
                                                struct buffer_head **bitmap_bh);
 
 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
-                                       struct ocfs2_journal_handle *handle,
+                                       handle_t *handle,
                                        struct ocfs2_alloc_context *ac);
 
 static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
                                          struct inode *local_alloc_inode);
 
-/*
- * Determine how large our local alloc window should be, in bits.
- *
- * These values (and the behavior in ocfs2_alloc_should_use_local) have
- * been chosen so that most allocations, including new block groups go
- * through local alloc.
- */
-static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
+static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
 {
-       BUG_ON(osb->s_clustersize_bits < 12);
+       return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
+               osb->local_alloc_state == OCFS2_LA_ENABLED);
+}
+
+void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
+                                     unsigned int num_clusters)
+{
+       spin_lock(&osb->osb_lock);
+       if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
+           osb->local_alloc_state == OCFS2_LA_THROTTLED)
+               if (num_clusters >= osb->local_alloc_default_bits) {
+                       cancel_delayed_work(&osb->la_enable_wq);
+                       osb->local_alloc_state = OCFS2_LA_ENABLED;
+               }
+       spin_unlock(&osb->osb_lock);
+}
 
-       return 2048 >> (osb->s_clustersize_bits - 12);
+void ocfs2_la_enable_worker(struct work_struct *work)
+{
+       struct ocfs2_super *osb =
+               container_of(work, struct ocfs2_super,
+                            la_enable_wq.work);
+       spin_lock(&osb->osb_lock);
+       osb->local_alloc_state = OCFS2_LA_ENABLED;
+       spin_unlock(&osb->osb_lock);
 }
 
 /*
  * Tell us whether a given allocation should use the local alloc
  * file. Otherwise, it has to go to the main bitmap.
+ *
+ * This function does semi-dirty reads of local alloc size and state!
+ * This is ok however, as the values are re-checked once under mutex.
  */
 int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
 {
-       int la_bits = ocfs2_local_alloc_window_bits(osb);
+       int ret = 0;
+       int la_bits;
 
-       if (osb->local_alloc_state != OCFS2_LA_ENABLED)
-               return 0;
+       spin_lock(&osb->osb_lock);
+       la_bits = osb->local_alloc_bits;
+
+       if (!ocfs2_la_state_enabled(osb))
+               goto bail;
 
        /* la_bits should be at least twice the size (in clusters) of
         * a new block group. We want to be sure block group
         * allocations go through the local alloc, so allow an
         * allocation to take up to half the bitmap. */
        if (bits > (la_bits / 2))
-               return 0;
+               goto bail;
 
-       return 1;
+       ret = 1;
+bail:
+       mlog(0, "state=%d, bits=%llu, la_bits=%d, ret=%d\n",
+            osb->local_alloc_state, (unsigned long long)bits, la_bits, ret);
+       spin_unlock(&osb->osb_lock);
+       return ret;
 }
 
 int ocfs2_load_local_alloc(struct ocfs2_super *osb)
@@ -122,6 +147,18 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
 
        mlog_entry_void();
 
+       if (osb->local_alloc_bits == 0)
+               goto bail;
+
+       if (osb->local_alloc_bits >= osb->bitmap_cpg) {
+               mlog(ML_NOTICE, "Requested local alloc window %d is larger "
+                    "than max possible %u. Using defaults.\n",
+                    osb->local_alloc_bits, (osb->bitmap_cpg - 1));
+               osb->local_alloc_bits =
+                       ocfs2_megabytes_to_clusters(osb->sb,
+                                                   OCFS2_DEFAULT_LOCAL_ALLOC_SIZE);
+       }
+
        /* read the alloc off disk */
        inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
                                            osb->slot_num);
@@ -131,8 +168,8 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
                goto bail;
        }
 
-       status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
-                                 &alloc_bh, 0, inode);
+       status = ocfs2_read_inode_block_full(inode, &alloc_bh,
+                                            OCFS2_BH_IGNORE_CACHE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -177,11 +214,12 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
 
 bail:
        if (status < 0)
-               if (alloc_bh)
-                       brelse(alloc_bh);
+               brelse(alloc_bh);
        if (inode)
                iput(inode);
 
+       mlog(0, "Local alloc window bits = %d\n", osb->local_alloc_bits);
+
        mlog_exit(status);
        return status;
 }
@@ -196,7 +234,7 @@ bail:
 void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
 {
        int status;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle;
        struct inode *local_alloc_inode = NULL;
        struct buffer_head *bh = NULL;
        struct buffer_head *main_bm_bh = NULL;
@@ -206,8 +244,11 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
 
        mlog_entry_void();
 
+       cancel_delayed_work(&osb->la_enable_wq);
+       flush_workqueue(ocfs2_wq);
+
        if (osb->local_alloc_state == OCFS2_LA_UNUSED)
-               goto bail;
+               goto out;
 
        local_alloc_inode =
                ocfs2_get_system_file_inode(osb,
@@ -216,57 +257,51 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
        if (!local_alloc_inode) {
                status = -ENOENT;
                mlog_errno(status);
-               goto bail;
+               goto out;
        }
 
        osb->local_alloc_state = OCFS2_LA_DISABLED;
 
-       handle = ocfs2_alloc_handle(osb);
-       if (!handle) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
        main_bm_inode = ocfs2_get_system_file_inode(osb,
                                                    GLOBAL_BITMAP_SYSTEM_INODE,
                                                    OCFS2_INVALID_SLOT);
        if (!main_bm_inode) {
                status = -EINVAL;
                mlog_errno(status);
-               goto bail;
+               goto out;
        }
 
-       ocfs2_handle_add_inode(handle, main_bm_inode);
-       status = ocfs2_meta_lock(main_bm_inode, handle, &main_bm_bh, 1);
+       mutex_lock(&main_bm_inode->i_mutex);
+
+       status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
        if (status < 0) {
                mlog_errno(status);
-               goto bail;
+               goto out_mutex;
        }
 
        /* WINDOW_MOVE_CREDITS is a bit heavy... */
-       handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
        if (IS_ERR(handle)) {
                mlog_errno(PTR_ERR(handle));
                handle = NULL;
-               goto bail;
+               goto out_unlock;
        }
 
        bh = osb->local_alloc_bh;
        alloc = (struct ocfs2_dinode *) bh->b_data;
 
-       alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
+       alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
        if (!alloc_copy) {
                status = -ENOMEM;
-               goto bail;
+               goto out_commit;
        }
        memcpy(alloc_copy, alloc, bh->b_size);
 
-       status = ocfs2_journal_access(handle, local_alloc_inode, bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
+                                        bh, OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
-               goto bail;
+               goto out_commit;
        }
 
        ocfs2_clear_local_alloc(alloc);
@@ -274,7 +309,7 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
        status = ocfs2_journal_dirty(handle, bh);
        if (status < 0) {
                mlog_errno(status);
-               goto bail;
+               goto out_commit;
        }
 
        brelse(bh);
@@ -286,16 +321,19 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
        if (status < 0)
                mlog_errno(status);
 
-bail:
-       if (handle)
-               ocfs2_commit_trans(handle);
+out_commit:
+       ocfs2_commit_trans(osb, handle);
 
-       if (main_bm_bh)
-               brelse(main_bm_bh);
+out_unlock:
+       brelse(main_bm_bh);
 
-       if (main_bm_inode)
-               iput(main_bm_inode);
+       ocfs2_inode_unlock(main_bm_inode, 1);
 
+out_mutex:
+       mutex_unlock(&main_bm_inode->i_mutex);
+       iput(main_bm_inode);
+
+out:
        if (local_alloc_inode)
                iput(local_alloc_inode);
 
@@ -336,8 +374,8 @@ int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
 
        mutex_lock(&inode->i_mutex);
 
-       status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
-                                 &alloc_bh, 0, inode);
+       status = ocfs2_read_inode_block_full(inode, &alloc_bh,
+                                            OCFS2_BH_IGNORE_CACHE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -353,7 +391,8 @@ int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
        alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
        ocfs2_clear_local_alloc(alloc);
 
-       status = ocfs2_write_block(osb, alloc_bh, inode);
+       ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
+       status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
        if (status < 0)
                mlog_errno(status);
 
@@ -363,8 +402,7 @@ bail:
                *alloc_copy = NULL;
        }
 
-       if (alloc_bh)
-               brelse(alloc_bh);
+       brelse(alloc_bh);
 
        if (inode) {
                mutex_unlock(&inode->i_mutex);
@@ -385,74 +423,112 @@ int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
                                        struct ocfs2_dinode *alloc)
 {
        int status;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle;
        struct buffer_head *main_bm_bh = NULL;
-       struct inode *main_bm_inode = NULL;
+       struct inode *main_bm_inode;
 
        mlog_entry_void();
 
-       handle = ocfs2_alloc_handle(osb);
-       if (!handle) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
        main_bm_inode = ocfs2_get_system_file_inode(osb,
                                                    GLOBAL_BITMAP_SYSTEM_INODE,
                                                    OCFS2_INVALID_SLOT);
        if (!main_bm_inode) {
                status = -EINVAL;
                mlog_errno(status);
-               goto bail;
+               goto out;
        }
 
-       ocfs2_handle_add_inode(handle, main_bm_inode);
-       status = ocfs2_meta_lock(main_bm_inode, handle, &main_bm_bh, 1);
+       mutex_lock(&main_bm_inode->i_mutex);
+
+       status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
        if (status < 0) {
                mlog_errno(status);
-               goto bail;
+               goto out_mutex;
        }
 
-       handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
                mlog_errno(status);
-               goto bail;
+               goto out_unlock;
        }
 
        /* we want the bitmap change to be recorded on disk asap */
-       handle->k_handle->h_sync = 1;
+       handle->h_sync = 1;
 
        status = ocfs2_sync_local_to_main(osb, handle, alloc,
                                          main_bm_inode, main_bm_bh);
        if (status < 0)
                mlog_errno(status);
 
-bail:
-       if (handle)
-               ocfs2_commit_trans(handle);
+       ocfs2_commit_trans(osb, handle);
 
-       if (main_bm_bh)
-               brelse(main_bm_bh);
+out_unlock:
+       ocfs2_inode_unlock(main_bm_inode, 1);
 
-       if (main_bm_inode)
-               iput(main_bm_inode);
+out_mutex:
+       mutex_unlock(&main_bm_inode->i_mutex);
 
+       brelse(main_bm_bh);
+
+       iput(main_bm_inode);
+
+out:
+       if (!status)
+               ocfs2_init_inode_steal_slot(osb);
        mlog_exit(status);
        return status;
 }
 
+/* Check to see if the local alloc window is within ac->ac_max_block */
+static int ocfs2_local_alloc_in_range(struct inode *inode,
+                                     struct ocfs2_alloc_context *ac,
+                                     u32 bits_wanted)
+{
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct ocfs2_dinode *alloc;
+       struct ocfs2_local_alloc *la;
+       int start;
+       u64 block_off;
+
+       if (!ac->ac_max_block)
+               return 1;
+
+       alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
+       la = OCFS2_LOCAL_ALLOC(alloc);
+
+       start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
+       if (start == -1) {
+               mlog_errno(-ENOSPC);
+               return 0;
+       }
+
+       /*
+        * Converting (bm_off + start + bits_wanted) to blocks gives us
+        * the blkno just past our actual allocation.  This is perfect
+        * to compare with ac_max_block.
+        */
+       block_off = ocfs2_clusters_to_blocks(inode->i_sb,
+                                            le32_to_cpu(la->la_bm_off) +
+                                            start + bits_wanted);
+       mlog(0, "Checking %llu against %llu\n",
+            (unsigned long long)block_off,
+            (unsigned long long)ac->ac_max_block);
+       if (block_off > ac->ac_max_block)
+               return 0;
+
+       return 1;
+}
+
 /*
- * make sure we've got at least bitswanted contiguous bits in the
+ * make sure we've got at least bits_wanted contiguous bits in the
  * local alloc. You lose them when you drop i_mutex.
  *
  * We will add ourselves to the transaction passed in, but may start
  * our own in order to shift windows.
  */
 int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
-                                  struct ocfs2_journal_handle *passed_handle,
                                   u32 bits_wanted,
                                   struct ocfs2_alloc_context *ac)
 {
@@ -463,9 +539,7 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
 
        mlog_entry_void();
 
-       BUG_ON(!passed_handle);
        BUG_ON(!ac);
-       BUG_ON(passed_handle->k_handle);
 
        local_alloc_inode =
                ocfs2_get_system_file_inode(osb,
@@ -476,21 +550,25 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
                mlog_errno(status);
                goto bail;
        }
-       ocfs2_handle_add_inode(passed_handle, local_alloc_inode);
 
-       if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
-               status = -ENOSPC;
-               goto bail;
-       }
+       mutex_lock(&local_alloc_inode->i_mutex);
 
-       if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
-               mlog(0, "Asking for more than my max window size!\n");
+       /*
+        * We must double check state and allocator bits because
+        * another process may have changed them while holding i_mutex.
+        */
+       spin_lock(&osb->osb_lock);
+       if (!ocfs2_la_state_enabled(osb) ||
+           (bits_wanted > osb->local_alloc_bits)) {
+               spin_unlock(&osb->osb_lock);
                status = -ENOSPC;
                goto bail;
        }
+       spin_unlock(&osb->osb_lock);
 
        alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
 
+#ifdef CONFIG_OCFS2_DEBUG_FS
        if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
            ocfs2_local_alloc_count_bits(alloc)) {
                ocfs2_error(osb->sb, "local alloc inode %llu says it has "
@@ -501,6 +579,7 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
                status = -EIO;
                goto bail;
        }
+#endif
 
        free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
                le32_to_cpu(alloc->id1.bitmap1.i_used);
@@ -513,31 +592,67 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
                                mlog_errno(status);
                        goto bail;
                }
+
+               /*
+                * Under certain conditions, the window slide code
+                * might have reduced the number of bits available or
+                * disabled the the local alloc entirely. Re-check
+                * here and return -ENOSPC if necessary.
+                */
+               status = -ENOSPC;
+               if (!ocfs2_la_state_enabled(osb))
+                       goto bail;
+
+               free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
+                       le32_to_cpu(alloc->id1.bitmap1.i_used);
+               if (bits_wanted > free_bits)
+                       goto bail;
+       }
+
+       if (ac->ac_max_block)
+               mlog(0, "Calling in_range for max block %llu\n",
+                    (unsigned long long)ac->ac_max_block);
+
+       if (!ocfs2_local_alloc_in_range(local_alloc_inode, ac,
+                                       bits_wanted)) {
+               /*
+                * The window is outside ac->ac_max_block.
+                * This errno tells the caller to keep localalloc enabled
+                * but to get the allocation from the main bitmap.
+                */
+               status = -EFBIG;
+               goto bail;
        }
 
-       ac->ac_inode = igrab(local_alloc_inode);
+       ac->ac_inode = local_alloc_inode;
+       /* We should never use localalloc from another slot */
+       ac->ac_alloc_slot = osb->slot_num;
+       ac->ac_which = OCFS2_AC_USE_LOCAL;
        get_bh(osb->local_alloc_bh);
        ac->ac_bh = osb->local_alloc_bh;
-       ac->ac_which = OCFS2_AC_USE_LOCAL;
        status = 0;
 bail:
-       if (local_alloc_inode)
+       if (status < 0 && local_alloc_inode) {
+               mutex_unlock(&local_alloc_inode->i_mutex);
                iput(local_alloc_inode);
+       }
+
+       mlog(0, "bits=%d, slot=%d, ret=%d\n", bits_wanted, osb->slot_num,
+            status);
 
        mlog_exit(status);
        return status;
 }
 
 int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
-                                struct ocfs2_journal_handle *handle,
+                                handle_t *handle,
                                 struct ocfs2_alloc_context *ac,
-                                u32 min_bits,
+                                u32 bits_wanted,
                                 u32 *bit_off,
                                 u32 *num_bits)
 {
        int status, start;
        struct inode *local_alloc_inode;
-       u32 bits_wanted;
        void *bitmap;
        struct ocfs2_dinode *alloc;
        struct ocfs2_local_alloc *la;
@@ -545,7 +660,6 @@ int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
        mlog_entry_void();
        BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
 
-       bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
        local_alloc_inode = ac->ac_inode;
        alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
        la = OCFS2_LOCAL_ALLOC(alloc);
@@ -564,9 +678,10 @@ int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
         * delete bits from it! */
        *num_bits = bits_wanted;
 
-       status = ocfs2_journal_access(handle, local_alloc_inode,
-                                     osb->local_alloc_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle,
+                                        INODE_CACHE(local_alloc_inode),
+                                        osb->local_alloc_bh,
+                                        OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -575,8 +690,7 @@ int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
        while(bits_wanted--)
                ocfs2_set_bit(start++, bitmap);
 
-       alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
-                               le32_to_cpu(alloc->id1.bitmap1.i_used));
+       le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
 
        status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
        if (status < 0) {
@@ -707,7 +821,7 @@ static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  * passed is used for caching.
  */
 static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
-                                   struct ocfs2_journal_handle *handle,
+                                   handle_t *handle,
                                    struct ocfs2_dinode *alloc,
                                    struct inode *main_bm_inode,
                                    struct buffer_head *main_bm_bh)
@@ -719,9 +833,8 @@ static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
        void *bitmap;
        struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
 
-       mlog_entry("total = %u, COUNT = %u, used = %u\n",
+       mlog_entry("total = %u, used = %u\n",
                   le32_to_cpu(alloc->id1.bitmap1.i_total),
-                  ocfs2_local_alloc_count_bits(alloc),
                   le32_to_cpu(alloc->id1.bitmap1.i_used));
 
        if (!alloc->id1.bitmap1.i_total) {
@@ -777,28 +890,114 @@ bail:
        return status;
 }
 
+enum ocfs2_la_event {
+       OCFS2_LA_EVENT_SLIDE,           /* Normal window slide. */
+       OCFS2_LA_EVENT_FRAGMENTED,      /* The global bitmap has
+                                        * enough bits theoretically
+                                        * free, but a contiguous
+                                        * allocation could not be
+                                        * found. */
+       OCFS2_LA_EVENT_ENOSPC,          /* Global bitmap doesn't have
+                                        * enough bits free to satisfy
+                                        * our request. */
+};
+#define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
+/*
+ * Given an event, calculate the size of our next local alloc window.
+ *
+ * This should always be called under i_mutex of the local alloc inode
+ * so that local alloc disabling doesn't race with processes trying to
+ * use the allocator.
+ *
+ * Returns the state which the local alloc was left in. This value can
+ * be ignored by some paths.
+ */
+static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
+                                 enum ocfs2_la_event event)
+{
+       unsigned int bits;
+       int state;
+
+       spin_lock(&osb->osb_lock);
+       if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
+               WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
+               goto out_unlock;
+       }
+
+       /*
+        * ENOSPC and fragmentation are treated similarly for now.
+        */
+       if (event == OCFS2_LA_EVENT_ENOSPC ||
+           event == OCFS2_LA_EVENT_FRAGMENTED) {
+               /*
+                * We ran out of contiguous space in the primary
+                * bitmap. Drastically reduce the number of bits used
+                * by local alloc until we have to disable it.
+                */
+               bits = osb->local_alloc_bits >> 1;
+               if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
+                       /*
+                        * By setting state to THROTTLED, we'll keep
+                        * the number of local alloc bits used down
+                        * until an event occurs which would give us
+                        * reason to assume the bitmap situation might
+                        * have changed.
+                        */
+                       osb->local_alloc_state = OCFS2_LA_THROTTLED;
+                       osb->local_alloc_bits = bits;
+               } else {
+                       osb->local_alloc_state = OCFS2_LA_DISABLED;
+               }
+               queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
+                                  OCFS2_LA_ENABLE_INTERVAL);
+               goto out_unlock;
+       }
+
+       /*
+        * Don't increase the size of the local alloc window until we
+        * know we might be able to fulfill the request. Otherwise, we
+        * risk bouncing around the global bitmap during periods of
+        * low space.
+        */
+       if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
+               osb->local_alloc_bits = osb->local_alloc_default_bits;
+
+out_unlock:
+       state = osb->local_alloc_state;
+       spin_unlock(&osb->osb_lock);
+
+       return state;
+}
+
 static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
-                                               struct ocfs2_journal_handle *handle,
                                                struct ocfs2_alloc_context **ac,
                                                struct inode **bitmap_inode,
                                                struct buffer_head **bitmap_bh)
 {
        int status;
 
-       *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
+       *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
        if (!(*ac)) {
                status = -ENOMEM;
                mlog_errno(status);
                goto bail;
        }
 
-       (*ac)->ac_handle = handle;
-       (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
+retry_enospc:
+       (*ac)->ac_bits_wanted = osb->local_alloc_bits;
 
        status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
+       if (status == -ENOSPC) {
+               if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
+                   OCFS2_LA_DISABLED)
+                       goto bail;
+
+               ocfs2_free_ac_resource(*ac);
+               memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
+               goto retry_enospc;
+       }
        if (status < 0) {
-               if (status != -ENOSPC)
-                       mlog_errno(status);
+               mlog_errno(status);
                goto bail;
        }
 
@@ -821,7 +1020,7 @@ bail:
  * pass it the bitmap lock in lock_bh if you have it.
  */
 static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
-                                       struct ocfs2_journal_handle *handle,
+                                       handle_t *handle,
                                        struct ocfs2_alloc_context *ac)
 {
        int status = 0;
@@ -839,7 +1038,7 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
                     "one\n");
 
        mlog(0, "Allocating %u clusters for a new window.\n",
-            ocfs2_local_alloc_window_bits(osb));
+            osb->local_alloc_bits);
 
        /* Instruct the allocation code to try the most recently used
         * cluster group. We'll re-record the group used this pass
@@ -849,9 +1048,36 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
        /* we used the generic suballoc reserve function, but we set
         * everything up nicely, so there's no reason why we can't use
         * the more specific cluster api to claim bits. */
-       status = ocfs2_claim_clusters(osb, handle, ac,
-                                     ocfs2_local_alloc_window_bits(osb),
+       status = ocfs2_claim_clusters(osb, handle, ac, osb->local_alloc_bits,
                                      &cluster_off, &cluster_count);
+       if (status == -ENOSPC) {
+retry_enospc:
+               /*
+                * Note: We could also try syncing the journal here to
+                * allow use of any free bits which the current
+                * transaction can't give us access to. --Mark
+                */
+               if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
+                   OCFS2_LA_DISABLED)
+                       goto bail;
+
+               status = ocfs2_claim_clusters(osb, handle, ac,
+                                             osb->local_alloc_bits,
+                                             &cluster_off,
+                                             &cluster_count);
+               if (status == -ENOSPC)
+                       goto retry_enospc;
+               /*
+                * We only shrunk the *minimum* number of in our
+                * request - it's entirely possible that the allocator
+                * might give us more than we asked for.
+                */
+               if (status == 0) {
+                       spin_lock(&osb->osb_lock);
+                       osb->local_alloc_bits = cluster_count;
+                       spin_unlock(&osb->osb_lock);
+               }
+       }
        if (status < 0) {
                if (status != -ENOSPC)
                        mlog_errno(status);
@@ -888,23 +1114,17 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
        int status = 0;
        struct buffer_head *main_bm_bh = NULL;
        struct inode *main_bm_inode = NULL;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle = NULL;
        struct ocfs2_dinode *alloc;
        struct ocfs2_dinode *alloc_copy = NULL;
        struct ocfs2_alloc_context *ac = NULL;
 
        mlog_entry_void();
 
-       handle = ocfs2_alloc_handle(osb);
-       if (!handle) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
+       ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
 
        /* This will lock the main bitmap for us. */
        status = ocfs2_local_alloc_reserve_for_window(osb,
-                                                     handle,
                                                      &ac,
                                                      &main_bm_inode,
                                                      &main_bm_bh);
@@ -914,7 +1134,7 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
                goto bail;
        }
 
-       handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
@@ -929,7 +1149,7 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
         * local alloc shutdown won't try to double free main bitmap
         * bits. Make a copy so the sync function knows which bits to
         * free. */
-       alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
+       alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
        if (!alloc_copy) {
                status = -ENOMEM;
                mlog_errno(status);
@@ -937,9 +1157,10 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
        }
        memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
 
-       status = ocfs2_journal_access(handle, local_alloc_inode,
-                                     osb->local_alloc_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle,
+                                        INODE_CACHE(local_alloc_inode),
+                                        osb->local_alloc_bh,
+                                        OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -972,10 +1193,9 @@ static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
        status = 0;
 bail:
        if (handle)
-               ocfs2_commit_trans(handle);
+               ocfs2_commit_trans(osb, handle);
 
-       if (main_bm_bh)
-               brelse(main_bm_bh);
+       brelse(main_bm_bh);
 
        if (main_bm_inode)
                iput(main_bm_inode);