fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / ocfs2 / localalloc.c
index 149b351..4dedd97 100644 (file)
@@ -58,19 +58,18 @@ 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,
@@ -143,8 +142,8 @@ int ocfs2_load_local_alloc(struct ocfs2_super *osb)
 
        if (!(le32_to_cpu(alloc->i_flags) &
            (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
-               mlog(ML_ERROR, "Invalid local alloc inode, %"MLFu64"\n",
-                    OCFS2_I(inode)->ip_blkno);
+               mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
+                    (unsigned long long)OCFS2_I(inode)->ip_blkno);
                status = -EINVAL;
                goto bail;
        }
@@ -196,7 +195,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;
@@ -207,7 +206,7 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
        mlog_entry_void();
 
        if (osb->local_alloc_state == OCFS2_LA_UNUSED)
-               goto bail;
+               goto out;
 
        local_alloc_inode =
                ocfs2_get_system_file_inode(osb,
@@ -216,40 +215,34 @@ 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_meta_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;
@@ -258,7 +251,7 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
        alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
        if (!alloc_copy) {
                status = -ENOMEM;
-               goto bail;
+               goto out_commit;
        }
        memcpy(alloc_copy, alloc, bh->b_size);
 
@@ -266,7 +259,7 @@ void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
-               goto bail;
+               goto out_commit;
        }
 
        ocfs2_clear_local_alloc(alloc);
@@ -274,7 +267,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 +279,20 @@ 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);
 
+out_unlock:
        if (main_bm_bh)
                brelse(main_bm_bh);
 
-       if (main_bm_inode)
-               iput(main_bm_inode);
+       ocfs2_meta_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);
 
@@ -385,61 +382,59 @@ 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_meta_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 */
-       ocfs2_handle_set_sync(handle, 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);
+
+out_unlock:
+       ocfs2_meta_unlock(main_bm_inode, 1);
+
+out_mutex:
+       mutex_unlock(&main_bm_inode->i_mutex);
 
        if (main_bm_bh)
                brelse(main_bm_bh);
 
-       if (main_bm_inode)
-               iput(main_bm_inode);
+       iput(main_bm_inode);
 
+out:
        mlog_exit(status);
        return status;
 }
@@ -452,7 +447,6 @@ bail:
  * 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 +457,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->flags & OCFS2_HANDLE_STARTED);
 
        local_alloc_inode =
                ocfs2_get_system_file_inode(osb,
@@ -476,7 +468,11 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
                mlog_errno(status);
                goto bail;
        }
-       ocfs2_handle_add_inode(passed_handle, local_alloc_inode);
+
+       mutex_lock(&local_alloc_inode->i_mutex);
+
+       ac->ac_inode = local_alloc_inode;
+       ac->ac_which = OCFS2_AC_USE_LOCAL;
 
        if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
                status = -ENOSPC;
@@ -493,9 +489,9 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
 
        if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
            ocfs2_local_alloc_count_bits(alloc)) {
-               ocfs2_error(osb->sb, "local alloc inode %"MLFu64" says it has "
+               ocfs2_error(osb->sb, "local alloc inode %llu says it has "
                            "%u free bits, but a count shows %u",
-                           le64_to_cpu(alloc->i_blkno),
+                           (unsigned long long)le64_to_cpu(alloc->i_blkno),
                            le32_to_cpu(alloc->id1.bitmap1.i_used),
                            ocfs2_local_alloc_count_bits(alloc));
                status = -EIO;
@@ -515,21 +511,17 @@ int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
                }
        }
 
-       ac->ac_inode = igrab(local_alloc_inode);
        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)
-               iput(local_alloc_inode);
 
        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 *bit_off,
@@ -707,7 +699,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)
@@ -753,10 +745,11 @@ static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
                                ocfs2_clusters_to_blocks(osb->sb,
                                                         start - count);
 
-                       mlog(0, "freeing %u bits starting at local "
-                            "alloc bit %u (la_start_blk = %"MLFu64", "
-                            "blkno = %"MLFu64")\n", count, start - count,
-                            la_start_blk, blkno);
+                       mlog(0, "freeing %u bits starting at local alloc bit "
+                            "%u (la_start_blk = %llu, blkno = %llu)\n",
+                            count, start - count,
+                            (unsigned long long)la_start_blk,
+                            (unsigned long long)blkno);
 
                        status = ocfs2_free_clusters(handle, main_bm_inode,
                                                     main_bm_bh, blkno, count);
@@ -777,21 +770,19 @@ bail:
 }
 
 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);
 
        status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
@@ -820,7 +811,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,6 +830,12 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
 
        mlog(0, "Allocating %u clusters for a new window.\n",
             ocfs2_local_alloc_window_bits(osb));
+
+       /* Instruct the allocation code to try the most recently used
+        * cluster group. We'll re-record the group used this pass
+        * below. */
+       ac->ac_last_group = osb->la_last_gd;
+
        /* 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. */
@@ -851,6 +848,8 @@ static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
                goto bail;
        }
 
+       osb->la_last_gd = ac->ac_last_group;
+
        la->la_bm_off = cpu_to_le32(cluster_off);
        alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
        /* just in case... In the future when we find space ourselves,
@@ -879,23 +878,15 @@ 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;
-       }
-
        /* This will lock the main bitmap for us. */
        status = ocfs2_local_alloc_reserve_for_window(osb,
-                                                     handle,
                                                      &ac,
                                                      &main_bm_inode,
                                                      &main_bm_bh);
@@ -905,7 +896,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;
@@ -963,7 +954,7 @@ 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);