Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-mballoc3-core.patch
index fa7db0b..2f5ab81 100644 (file)
@@ -1,8 +1,8 @@
-Index: linux-2.6.18-92.1.6/include/linux/ext3_fs.h
+Index: linux-2.6.22.19/include/linux/ext3_fs.h
 ===================================================================
---- linux-2.6.18-92.1.6.orig/include/linux/ext3_fs.h
-+++ linux-2.6.18-92.1.6/include/linux/ext3_fs.h
-@@ -53,6 +53,31 @@
+--- linux-2.6.22.19.orig/include/linux/ext3_fs.h
++++ linux-2.6.22.19/include/linux/ext3_fs.h
+@@ -54,6 +54,31 @@
  #define ext3_debug(f, a...)   do {} while (0)
  #endif
  
@@ -34,7 +34,7 @@ Index: linux-2.6.18-92.1.6/include/linux/ext3_fs.h
  /*
   * Special inodes numbers
   */
-@@ -398,6 +423,14 @@ struct ext3_inode {
+@@ -412,6 +437,14 @@ struct ext3_inode {
  #define ext3_find_first_zero_bit      ext2_find_first_zero_bit
  #define ext3_find_next_zero_bit               ext2_find_next_zero_bit
  
@@ -49,7 +49,7 @@ Index: linux-2.6.18-92.1.6/include/linux/ext3_fs.h
  /*
   * Maximal mount counts between two filesystem checks
   */
-@@ -799,6 +832,20 @@ extern unsigned long ext3_count_dirs (st
+@@ -813,6 +846,20 @@ extern unsigned long ext3_count_dirs (st
  extern void ext3_check_inodes_bitmap (struct super_block *);
  extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
  
@@ -70,7 +70,7 @@ Index: linux-2.6.18-92.1.6/include/linux/ext3_fs.h
  
  /* inode.c */
  int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
-@@ -843,6 +890,10 @@ extern int ext3_group_extend(struct supe
+@@ -859,6 +906,10 @@ extern int ext3_group_extend(struct supe
                                ext3_fsblk_t n_blocks_count);
  
  /* super.c */
@@ -81,10 +81,10 @@ Index: linux-2.6.18-92.1.6/include/linux/ext3_fs.h
  extern void ext3_error (struct super_block *, const char *, const char *, ...)
        __attribute__ ((format (printf, 3, 4)));
  extern void __ext3_std_error (struct super_block *, const char *, int);
-Index: linux-2.6.18-92.1.6/include/linux/ext3_fs_sb.h
+Index: linux-2.6.22.19/include/linux/ext3_fs_sb.h
 ===================================================================
---- linux-2.6.18-92.1.6.orig/include/linux/ext3_fs_sb.h
-+++ linux-2.6.18-92.1.6/include/linux/ext3_fs_sb.h
+--- linux-2.6.22.19.orig/include/linux/ext3_fs_sb.h
++++ linux-2.6.22.19/include/linux/ext3_fs_sb.h
 @@ -88,6 +88,68 @@ struct ext3_sb_info {
        unsigned long s_ext_blocks;
        unsigned long s_ext_extents;
@@ -154,11 +154,11 @@ Index: linux-2.6.18-92.1.6/include/linux/ext3_fs_sb.h
 +                               [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
 +
  #endif        /* _LINUX_EXT3_FS_SB */
-Index: linux-2.6.18-92.1.6/fs/ext3/super.c
+Index: linux-2.6.22.19/fs/ext3/super.c
 ===================================================================
---- linux-2.6.18-92.1.6.orig/fs/ext3/super.c
-+++ linux-2.6.18-92.1.6/fs/ext3/super.c
-@@ -391,6 +391,7 @@ static void ext3_put_super (struct super
+--- linux-2.6.22.19.orig/fs/ext3/super.c
++++ linux-2.6.22.19/fs/ext3/super.c
+@@ -392,6 +392,7 @@ static void ext3_put_super (struct super
        struct ext3_super_block *es = sbi->s_es;
        int i;
  
@@ -166,16 +166,18 @@ Index: linux-2.6.18-92.1.6/fs/ext3/super.c
        ext3_ext_release(sb);
        ext3_xattr_put_super(sb);
        journal_destroy(sbi->s_journal);
-@@ -433,6 +434,8 @@ static void ext3_put_super (struct super
-               invalidate_bdev(sbi->journal_bdev, 0);
+@@ -434,6 +435,10 @@ static void ext3_put_super (struct super
+               invalidate_bdev(sbi->journal_bdev);
                ext3_blkdev_remove(sbi);
        }
-+      remove_proc_entry(sb->s_id, proc_root_ext3);
-+      sbi->s_dev_proc = NULL;
++      if (sbi->s_dev_proc) {
++              remove_proc_entry(sbi->s_dev_proc->name, proc_root_ext3);
++              sbi->s_dev_proc = NULL;
++      }
        sb->s_fs_info = NULL;
        kfree(sbi);
        return;
-@@ -458,6 +461,8 @@ static struct inode *ext3_alloc_inode(st
+@@ -459,6 +464,8 @@ static struct inode *ext3_alloc_inode(st
        ei->vfs_inode.i_version = 1;
  
        memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
@@ -184,30 +186,49 @@ Index: linux-2.6.18-92.1.6/fs/ext3/super.c
        return &ei->vfs_inode;
  }
  
-@@ -1454,6 +1459,13 @@ static int ext3_fill_super (struct super
+@@ -1434,6 +1441,7 @@ static int ext3_fill_super (struct super
+       unsigned long journal_devnum = 0;
+       unsigned long def_mount_opts;
+       struct inode *root;
++      char *devname;
+       int blocksize;
+       int hblock;
+       int db_count;
+@@ -1448,6 +1456,22 @@ static int ext3_fill_super (struct super
        sbi->s_mount_opt = 0;
        sbi->s_resuid = EXT3_DEF_RESUID;
        sbi->s_resgid = EXT3_DEF_RESGID;
-+      sbi->s_dev_proc = proc_mkdir(sb->s_id, proc_root_ext3);
-+      if (sbi->s_dev_proc == NULL) {
-+              printk(KERN_ERR "EXT3-fs: Unable to create %s\n", sb->s_id);
-+              sb->s_fs_info = NULL;
-+              kfree(sbi);
-+              return -ENOMEM;
++      devname = kstrdup(sb->s_id, GFP_KERNEL);
++      if (devname) {
++              char *p = devname;
++              while ((p = strchr(p, '/')))
++                      *p = '!';
++              sbi->s_dev_proc = proc_mkdir(devname, proc_root_ext3);
++              if (sbi->s_dev_proc == NULL)
++                      printk(KERN_WARNING "EXT3-fs warning: unable to create "
++                                          "procfs entry for %s(%s)\n",
++                                          sb->s_id, devname);
++              kfree(devname);
++      } else {
++              printk(KERN_WARNING "EXT3-fs warning: cannot allocate memory "
++                                  "to create procfs entry for %s\n",
++                                  sb->s_id);
 +      }
  
        unlock_kernel();
  
-@@ -1857,6 +1869,8 @@ failed_mount:
+@@ -1857,6 +1881,10 @@ failed_mount:
        ext3_blkdev_remove(sbi);
        brelse(bh);
  out_fail:
-+      remove_proc_entry(sb->s_id, proc_root_ext3);
-+      sbi->s_dev_proc = NULL;
++      if (sbi->s_dev_proc) {
++              remove_proc_entry(sbi->s_dev_proc->name, proc_root_ext3);
++              sbi->s_dev_proc = NULL;
++      }
        sb->s_fs_info = NULL;
        kfree(sbi);
        lock_kernel();
-@@ -2782,9 +2796,46 @@ static struct file_system_type ext3_fs_t
+@@ -2787,9 +2815,46 @@ static struct file_system_type ext3_fs_t
        .fs_flags       = FS_REQUIRES_DEV,
  };
  
@@ -255,7 +276,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/super.c
        if (err)
                return err;
        err = init_inodecache();
-@@ -2806,6 +2857,7 @@ static void __exit exit_ext3_fs(void)
+@@ -2811,6 +2876,7 @@ static void __exit exit_ext3_fs(void)
        unregister_filesystem(&ext3_fs_type);
        destroy_inodecache();
        exit_ext3_xattr();
@@ -263,14 +284,14 @@ Index: linux-2.6.18-92.1.6/fs/ext3/super.c
  }
  
  int ext3_map_inode_page(struct inode *inode, struct page *page,
-Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
+Index: linux-2.6.22.19/fs/ext3/mballoc.c
 ===================================================================
 --- /dev/null
-+++ linux-2.6.18-92.1.6/fs/ext3/mballoc.c
-@@ -0,0 +1,4391 @@
++++ linux-2.6.22.19/fs/ext3/mballoc.c
+@@ -0,0 +1,4483 @@
 +/*
-+ * Copyright 2008 Sun Microsystems, Inc.
-+ * Written by Alex Tomas <alex@clusterfs.com>
++ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
++ * Written by Alex Zhuravlev <alex.zhuravlev@sun.com>
 + *
 + * This program is free software; you can redistribute it and/or modify
 + * it under the terms of the GNU General Public License version 2 as
@@ -597,6 +618,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      unsigned short  bb_free;
 +      unsigned short  bb_fragments;
 +      struct          list_head bb_prealloc_list;
++      unsigned long   bb_prealloc_nr;
 +#ifdef DOUBLE_CHECK
 +      void            *bb_bitmap;
 +#endif
@@ -700,7 +722,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      __u16 tail;     /* what tail broke some buddy */
 +      __u16 buddy;    /* buddy the tail ^^^ broke */
 +      __u16 flags;
-+      __u8 cr:3;      /* which phase the result extent was found at */
++      __u8 cr:8;      /* which phase the result extent was found at */
 +      __u8 op:4;
 +      __u8 merged:1;
 +};
@@ -733,7 +755,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +void ext3_mb_release_blocks(struct super_block *, int);
 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
 +void ext3_mb_free_committed_blocks(struct super_block *);
-+void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group);
++int ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group);
 +void ext3_mb_free_consumed_preallocations(struct ext3_allocation_context *ac);
 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
 +                                      sector_t block, int count);
@@ -1126,7 +1148,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      }
 +}
 +
-+static void
++static int
 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
 +                      int group)
 +{
@@ -1158,9 +1180,14 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      grp->bb_fragments = fragments;
 +
 +      if (free != grp->bb_free) {
-+              printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
-+                      group, free, grp->bb_free);
-+              grp->bb_free = free;
++              struct ext3_group_desc *gdp;
++              gdp = ext3_get_group_desc (sb, group, NULL);
++              ext3_error(sb, __FUNCTION__,
++                      "group %u: %u blocks in bitmap, %u in bb, "
++                      "%u in gd, %lu pa's\n", group, free, grp->bb_free,
++                      le16_to_cpu(gdp->bg_free_blocks_count),
++                      grp->bb_prealloc_nr);
++              return -EIO;
 +      }
 +
 +      clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
@@ -1170,6 +1197,8 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      EXT3_SB(sb)->s_mb_buddies_generated++;
 +      EXT3_SB(sb)->s_mb_generation_time += period;
 +      spin_unlock(&EXT3_SB(sb)->s_bal_lock);
++
++      return 0;
 +}
 +
 +static int ext3_mb_init_cache(struct page *page, char *incore)
@@ -1247,8 +1276,9 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              if (!buffer_uptodate(bh[i]))
 +                      goto out;
 +
++      err = 0;
 +      first_block = page->index * blocks_per_page;
-+      for (i = 0; i < blocks_per_page; i++) {
++      for (i = 0; i < blocks_per_page && err == 0; i++) {
 +              int group;
 +
 +              group = (first_block + i) >> 1;
@@ -1267,7 +1297,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
 +                      memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
 +                             sizeof(unsigned short)*(sb->s_blocksize_bits+2));
-+                      ext3_mb_generate_buddy(sb, data, incore, group);
++                      err = ext3_mb_generate_buddy(sb, data, incore, group);
 +                      incore = NULL;
 +              } else {
 +                      /* this is block of bitmap */
@@ -1280,13 +1310,14 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      memcpy(data, bitmap, blocksize);
 +
 +                      /* mark all preallocated blocks used in in-core bitmap */
-+                      ext3_mb_generate_from_pa(sb, data, group);
++                      err = ext3_mb_generate_from_pa(sb, data, group);
 +                      ext3_unlock_group(sb, group);
 +
 +                      incore = data;
 +              }
 +      }
-+      SetPageUptodate(page);
++      if (likely(err == 0))
++              SetPageUptodate(page);
 +
 +out:
 +      if (bh) {
@@ -1425,7 +1456,10 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      cur += 32;
 +                      continue;
 +              }
-+              mb_clear_bit_atomic(lock, cur, bm);
++              if (lock)
++                      mb_clear_bit_atomic(lock, cur, bm);
++              else
++                      mb_clear_bit(cur, bm);
 +              cur++;
 +      }
 +}
@@ -1443,7 +1477,10 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      cur += 32;
 +                      continue;
 +              }
-+              mb_set_bit_atomic(lock, cur, bm);
++              if (lock)
++                      mb_set_bit_atomic(lock, cur, bm);
++              else
++                      mb_set_bit(cur, bm);
 +              cur++;
 +      }
 +}
@@ -1597,6 +1634,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      BUG_ON(start + len > (e3b->bd_sb->s_blocksize << 3));
 +      BUG_ON(e3b->bd_group != ex->fe_group);
 +      BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
++      spin_lock(sb_bgl_lock(EXT3_SB(e3b->bd_sb), ex->fe_group));
 +      mb_check_buddy(e3b);
 +      mb_mark_used_double(e3b, start, len);
 +
@@ -1650,9 +1688,9 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              e3b->bd_info->bb_counters[ord]++;
 +      }
 +
-+      mb_set_bits(sb_bgl_lock(EXT3_SB(e3b->bd_sb), ex->fe_group),
-+                  EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
++      mb_set_bits(NULL, EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
 +      mb_check_buddy(e3b);
++      spin_unlock(sb_bgl_lock(EXT3_SB(e3b->bd_sb), ex->fe_group));
 +
 +      return ret;
 +}
@@ -2264,6 +2302,8 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      hs->result.fe_start, hs->result.fe_len);
 +              seq_printf(seq, "%-5u %-8u %-23s free\n",
 +                              hs->pid, hs->ino, buf2);
++      } else {
++              seq_printf(seq, "unknown op %d\n", hs->op);
 +      }
 +      return 0;
 +}
@@ -2389,8 +2429,9 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
 +{
 +      struct super_block *sb = seq->private;
++      struct ext3_group_desc *gdp;
 +      long group = (long) v;
-+      int i, err;
++      int i, err, free = 0;
 +      struct ext3_buddy e3b;
 +      struct sg {
 +              struct ext3_group_info info;
@@ -2399,10 +2440,10 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +
 +      group--;
 +      if (group == 0)
-+              seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
++              seq_printf(seq, "#%-5s: %-5s %-5s %-5s %-5s %-5s "
 +                              "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
 +                                "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
-+                         "group", "free", "frags", "first",
++                         "group", "free", "ingd", "frags", "first", "pa",
 +                         "2^0", "2^1", "2^2", "2^3", "2^4", "2^5","2^6",
 +                         "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
 +
@@ -2413,13 +2454,20 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              seq_printf(seq, "#%-5lu: I/O error\n", group);
 +              return 0;
 +      }
++
++      gdp = ext3_get_group_desc(sb, group, NULL);
++      if (gdp != NULL)
++              free = le16_to_cpu(gdp->bg_free_blocks_count);
++
 +      ext3_lock_group(sb, group);
 +      memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
 +      ext3_unlock_group(sb, group);
 +      ext3_mb_release_desc(&e3b);
 +
-+      seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
-+                      sg.info.bb_fragments, sg.info.bb_first_free);
++      seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [", group,
++                      sg.info.bb_free, free,
++                      sg.info.bb_fragments, sg.info.bb_first_free,
++                      sg.info.bb_prealloc_nr);
 +      for (i = 0; i <= 13; i++)
 +              seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
 +                              sg.info.bb_counters[i] : 0);
@@ -2525,6 +2573,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      h.tail = ac->ac_tail;
 +      h.buddy = ac->ac_buddy;
 +      h.merged = 0;
++      h.cr = ac->ac_criteria;
 +      if (ac->ac_op == EXT3_MB_HISTORY_ALLOC) {
 +              if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
 +                              ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
@@ -3202,6 +3251,8 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              ext3_error(sb, __FUNCTION__,
 +                         "Allocating block in system zone - block = %lu",
 +                         (unsigned long) block);
++      ext3_lock_group(sb, ac->ac_b_ex.fe_group);
++      spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
 +#ifdef AGGRESSIVE_CHECK
 +      {
 +              int i;
@@ -3211,15 +3262,15 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              }
 +      }
 +#endif
-+      mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
++      mb_set_bits(NULL, bitmap_bh->b_data,
 +                  ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
 +
-+      spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
 +      gdp->bg_free_blocks_count =
 +              cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
 +                              - ac->ac_b_ex.fe_len);
 +      spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
 +      percpu_counter_mod(&sbi->s_freeblocks_counter, - ac->ac_b_ex.fe_len);
++      ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
 +
 +      err = ext3_journal_dirty_metadata(handle, bitmap_bh);
 +      if (err)
@@ -3561,17 +3612,62 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +}
 +
 +/*
++ * check free blocks in bitmap match free block in group descriptor
++ * do this before taking preallocated blocks into account to be able
++ * to detect on-disk corruptions
++ */
++int ext3_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
++                              struct ext3_group_desc *gdp, int group)
++{
++      unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
++      unsigned short i, first, free = 0;
++
++      spin_lock(sb_bgl_lock(EXT3_SB(sb), group));
++      i = mb_find_next_zero_bit(bitmap, max, 0);
++
++      while (i < max) {
++              first = i;
++              i = ext2_find_next_le_bit(bitmap, max, i);
++              if (i > max)
++                      i = max;
++              free += i - first;
++              if (i < max)
++                      i = mb_find_next_zero_bit(bitmap, max, i);
++      }
++
++      if (free != le16_to_cpu(gdp->bg_free_blocks_count)) {
++              spin_unlock(sb_bgl_lock(EXT3_SB(sb), group));
++              ext3_error(sb, __FUNCTION__, "on-disk bitmap for group %d"
++                      "corrupted: %u blocks free in bitmap, %u - in gd\n",
++                      group, free, le16_to_cpu(gdp->bg_free_blocks_count));
++              return -EIO;
++      }
++      spin_unlock(sb_bgl_lock(EXT3_SB(sb), group));
++      return 0;
++}
++
++/*
 + * the function goes through all preallocation in this group and marks them
 + * used in in-core bitmap. buddy must be generated from this bitmap
 + */
-+void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group)
++int ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group)
 +{
 +      struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
 +      struct ext3_prealloc_space *pa;
++      struct ext3_group_desc *gdp;
 +      struct list_head *cur;
 +      unsigned long groupnr;
 +      unsigned long start;
-+      int preallocated = 0, count = 0, len;
++      int preallocated = 0, count = 0, len, skip = 0, err;
++
++      gdp = ext3_get_group_desc (sb, group, NULL);
++      if (gdp == NULL)
++              return -EIO;
++
++      /* before applying preallocations, check bitmap consistency */
++      err = ext3_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
++      if (err)
++              return err;
 +
 +      /* all form of preallocation discards first load group,
 +       * so the only competing code is preallocation use.
@@ -3587,14 +3683,23 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              ext3_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &start);
 +              len = pa->pa_len;
 +              spin_unlock(&pa->pa_lock);
-+              if (unlikely(len == 0))
++              if (unlikely(len == 0)) {
++                      skip++;
 +                      continue;
++              }
 +              BUG_ON(groupnr != group && len != 0);
 +              mb_set_bits(sb_bgl_lock(EXT3_SB(sb), group), bitmap, start,len);
 +              preallocated += len;
 +              count++;
 +      }
++      if (count + skip != grp->bb_prealloc_nr) {
++              ext3_error(sb, __FUNCTION__, "lost preallocations: "
++                      "count %d, bb_prealloc_nr %lu, skip %d\n",
++                      count, grp->bb_prealloc_nr, skip);
++              return -EIO;
++      }
 +      mb_debug("prellocated %u for group %u\n", preallocated, group);
++      return 0;
 +}
 +
 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,5)
@@ -3654,6 +3759,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +       */
 +      ext3_lock_group(sb, grp);
 +      list_del_rcu(&pa->pa_group_list);
++      EXT3_GROUP_INFO(sb, grp)->bb_prealloc_nr--;
 +      ext3_unlock_group(sb, grp);
 +
 +      spin_lock(pa->pa_obj_lock);
@@ -3738,6 +3844,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +
 +      ext3_lock_group(sb, ac->ac_b_ex.fe_group);
 +      list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
++      grp->bb_prealloc_nr++;
 +      ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
 +
 +      spin_lock(pa->pa_obj_lock);
@@ -3795,6 +3902,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +
 +      ext3_lock_group(sb, ac->ac_b_ex.fe_group);
 +      list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
++      grp->bb_prealloc_nr++;
 +      ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
 +
 +      spin_lock(pa->pa_obj_lock);
@@ -3842,6 +3950,7 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      ac.ac_sb = sb;
 +      ac.ac_inode = pa->pa_inode;
 +      ac.ac_op = EXT3_MB_HISTORY_DISCARD;
++      ac.ac_o_ex.fe_len = 1;
 +
 +      while (bit < end) {
 +              bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
@@ -3937,7 +4046,10 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      }
 +
 +      err = ext3_mb_load_buddy(sb, group, &e3b);
-+      BUG_ON(err != 0); /* error handling here */
++      if (err) {
++              brelse(bitmap_bh);
++              return err;
++      }
 +
 +      if (needed == 0)
 +              needed = EXT3_BLOCKS_PER_GROUP(sb) + 1;
@@ -3968,6 +4080,8 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +
 +              spin_unlock(&pa->pa_lock);
 +
++              BUG_ON(grp->bb_prealloc_nr == 0);
++              grp->bb_prealloc_nr--;
 +              list_del_rcu(&pa->pa_group_list);
 +              list_add(&pa->u.pa_tmp_list, &list);
 +      }
@@ -4084,11 +4198,14 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +              ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
 +
 +              err = ext3_mb_load_buddy(sb, group, &e3b);
-+              BUG_ON(err != 0); /* error handling here */
++              if (err)
++                      return;
 +
 +              bitmap_bh = read_block_bitmap(sb, group);
 +
 +              ext3_lock_group(sb, group);
++              BUG_ON(e3b.bd_info->bb_prealloc_nr == 0);
++              e3b.bd_info->bb_prealloc_nr--;
 +              list_del_rcu(&pa->pa_group_list);
 +
 +              /* can be NULL due to IO error, at worst
@@ -4461,7 +4578,6 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      BUG_ON(e3b->bd_bitmap_page == NULL);
 +      BUG_ON(e3b->bd_buddy_page == NULL);
 +
-+      ext3_lock_group(sb, group);
 +      for (i = 0; i < count; i++) {
 +              md = db->bb_md_cur;
 +              if (md && db->bb_tid != handle->h_transaction->t_tid) {
@@ -4506,7 +4622,6 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      db->bb_md_cur = NULL;
 +              }
 +      }
-+      ext3_unlock_group(sb, group);
 +      return 0;
 +}
 +
@@ -4599,6 +4714,8 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +      if (err)
 +              goto error_return;
 +
++      ext3_lock_group(sb, block_group);
++      spin_lock(sb_bgl_lock(sbi, block_group));
 +#ifdef AGGRESSIVE_CHECK
 +      {
 +              int i;
@@ -4606,35 +4723,31 @@ Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
 +                      BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
 +      }
 +#endif
-+      mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data, bit,
-+                      count);
-+
-+      /* We dirtied the bitmap block */
-+      BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
-+      err = ext3_journal_dirty_metadata(handle, bitmap_bh);
-+
-+      ac.ac_b_ex.fe_group = block_group;
-+      ac.ac_b_ex.fe_start = bit;
-+      ac.ac_b_ex.fe_len = count;
-+      ext3_mb_store_history(&ac);
++      mb_clear_bits(NULL, bitmap_bh->b_data, bit, count);
++      gdp->bg_free_blocks_count =
++              cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
++      spin_unlock(sb_bgl_lock(sbi, block_group));
++      percpu_counter_mod(&sbi->s_freeblocks_counter, count);
 +
 +      if (metadata) {
 +              /* blocks being freed are metadata. these blocks shouldn't
 +               * be used until this transaction is committed */
 +              ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
 +      } else {
-+              ext3_lock_group(sb, block_group);
 +              err = mb_free_blocks(inode, &e3b, bit, count);
 +              ext3_mb_return_to_preallocation(inode, &e3b, block, count);
-+              ext3_unlock_group(sb, block_group);
 +              BUG_ON(err != 0);
 +      }
++      ext3_unlock_group(sb, block_group);
 +
-+      spin_lock(sb_bgl_lock(sbi, block_group));
-+      gdp->bg_free_blocks_count =
-+              cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
-+      spin_unlock(sb_bgl_lock(sbi, block_group));
-+      percpu_counter_mod(&sbi->s_freeblocks_counter, count);
++      ac.ac_b_ex.fe_group = block_group;
++      ac.ac_b_ex.fe_start = bit;
++      ac.ac_b_ex.fe_len = count;
++      ext3_mb_store_history(&ac);
++
++      /* We dirtied the bitmap block */
++      BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
++      err = ext3_journal_dirty_metadata(handle, bitmap_bh);
 +
 +      ext3_mb_release_desc(&e3b);
 +