--- /tmp/a	2011-07-01 11:57:40.024152318 -0400
+++ /tmp/b	2011-07-01 11:58:27.723355014 -0400
@@ -1,6 +1,6 @@
-commit cd0f2eddea7acb90cbc533457401ad6a650d4d45
+commit 37fcc83f6fea64d6e90cd21eb1df0708a8287d86
 Author: Aditya Kali <adityakali@google.com>
-Date:   Thu May 26 16:06:08 2011 -0700
+Date:   Thu Jun 16 15:39:06 2011 -0700
 
     ext4: fix confusing blocks/cluster naming and usage for bigalloc
     
@@ -12,13 +12,15 @@
     incorrect usage of some variables and functions.
     
     Tested: Built and booted the kernel
-    Change-Id: I855dfc503c1fa0b9aeee8d50e9f130a9aa16a6b7
+    
+    Google-Bug-Id:
+    Change-Id: Ifceba524627250334a1727cba4b02c699e5dfd7a
 
 diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
-index 6967211..1c11630 100644
+index 6967211..60603b0 100644
 --- a/fs/ext4/balloc.c
 +++ b/fs/ext4/balloc.c
-@@ -527,55 +527,58 @@ error_return:
+@@ -527,55 +527,56 @@ error_return:
  }
  
  /**
@@ -40,28 +42,24 @@
 +	s64 free_clusters, dirty_clusters, root_clusters;
  	struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
 -	struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;
--
++	struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
+ 
 -	free_blocks  = EXT4_C2B(sbi, percpu_counter_read_positive(fcc));
 -	dirty_blocks = percpu_counter_read_positive(dbc);
 -	root_blocks = ext4_r_blocks_count(sbi->s_es);
--
++	free_clusters  = percpu_counter_read_positive(fcc);
++	dirty_clusters = percpu_counter_read_positive(dcc);
++	root_clusters = EXT4_B2C(sbi, ext4_r_blocks_count(sbi->s_es));
+ 
 -	if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
 -						EXT4_FREEBLOCKS_WATERMARK) {
 -		free_blocks  = EXT4_C2B(sbi, percpu_counter_sum_positive(fcc));
 -		dirty_blocks = percpu_counter_sum_positive(dbc);
 -		if (dirty_blocks < 0) {
-+	struct percpu_counter *dbc = &sbi->s_dirtyclusters_counter;
-+
-+	free_clusters  = percpu_counter_read_positive(fcc);
-+	dirty_clusters = percpu_counter_read_positive(dbc);
-+	root_clusters = EXT4_B2C(sbi, ext4_r_blocks_count(sbi->s_es));
-+
-+	/* TODO: EXT4_FREEBLOCKS_WATERMARK value should be considered as
-+	 * clusters ? */
 +	if (free_clusters - (nclusters + root_clusters + dirty_clusters) <
-+			EXT4_FREEBLOCKS_WATERMARK) {
++			EXT4_FREECLUSTERS_WATERMARK) {
 +		free_clusters  = percpu_counter_sum_positive(fcc);
-+		dirty_clusters = percpu_counter_sum_positive(dbc);
++		dirty_clusters = percpu_counter_sum_positive(dcc);
 +		if (dirty_clusters < 0) {
  			printk(KERN_CRIT "Dirty block accounting "
  					"went wrong %lld\n",
@@ -102,7 +100,7 @@
  		return 0;
  	} else
  		return -ENOSPC;
-@@ -595,7 +598,7 @@ int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
+@@ -595,7 +596,7 @@ int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
   */
  int ext4_should_retry_alloc(struct super_block *sb, int *retries)
  {
@@ -111,7 +109,7 @@
  	    (*retries)++ > 3 ||
  	    !EXT4_SB(sb)->s_journal)
  		return 0;
-@@ -611,7 +614,7 @@ int ext4_should_retry_alloc(struct super_block *sb, int *retries)
+@@ -611,7 +612,7 @@ int ext4_should_retry_alloc(struct super_block *sb, int *retries)
   * @handle:             handle to this transaction
   * @inode:              file inode
   * @goal:               given target block(filesystem wide)
@@ -120,8 +118,18 @@
   * @errp:               error code
   *
   * Return 1st allocated block number on success, *count stores total account
+@@ -640,7 +641,8 @@ ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
+ 		spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
+ 		EXT4_I(inode)->i_allocated_meta_blocks += ar.len;
+ 		spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
+-		dquot_alloc_block_nofail(inode, ar.len);
++		dquot_alloc_block_nofail(inode,
++				EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
+ 	}
+ 	return ret;
+ }
 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
-index d90182f..4851780 100644
+index e993e8a..c6f9a89 100644
 --- a/fs/ext4/ext4.h
 +++ b/fs/ext4/ext4.h
 @@ -207,16 +207,15 @@ typedef struct ext4_io_end {
@@ -136,7 +144,7 @@
  #endif
  #define	EXT4_ADDR_PER_BLOCK(s)		(EXT4_BLOCK_SIZE(s) / sizeof(__u32))
 +#define EXT4_CLUSTER_SIZE(s)		(EXT4_BLOCK_SIZE(s) << \
-+					 EXT4_SB(s)->s_cluster_ratio)
++					 (EXT4_SB(s)->s_cluster_bits))
  #ifdef __KERNEL__
  # define EXT4_BLOCK_SIZE_BITS(s)	((s)->s_blocksize_bits)
 -# define EXT4_CLUSTER_SIZE_BITS(s)	(EXT4_SB(s)->s_clustersize_bits)
@@ -152,7 +160,7 @@
  	unsigned int i_reserved_data_blocks;
  	unsigned int i_reserved_meta_blocks;
  	unsigned int i_allocated_meta_blocks;
-@@ -1112,7 +1112,7 @@ struct ext4_sb_info {
+@@ -1116,7 +1116,7 @@ struct ext4_sb_info {
  	struct percpu_counter s_freeclusters_counter;
  	struct percpu_counter s_freeinodes_counter;
  	struct percpu_counter s_dirs_counter;
@@ -161,7 +169,7 @@
  	struct blockgroup_lock *s_blockgroup_lock;
  	struct proc_dir_entry *s_proc;
  	struct kobject s_kobj;
-@@ -1616,8 +1616,8 @@ extern unsigned long ext4_bg_num_gdb(struct super_block *sb,
+@@ -1623,8 +1623,8 @@ extern unsigned long ext4_bg_num_gdb(struct super_block *sb,
  			ext4_group_t group);
  extern ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
  			ext4_fsblk_t goal, unsigned long *count, int *errp);
@@ -172,22 +180,26 @@
  extern void ext4_add_groupblocks(handle_t *handle, struct super_block *sb,
  				ext4_fsblk_t block, unsigned long count);
  extern ext4_fsblk_t ext4_count_free_clusters(struct super_block *);
-diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
-index 9838260..bd06deb 100644
---- a/fs/ext4/extents.c
-+++ b/fs/ext4/extents.c
-@@ -299,7 +299,8 @@ int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
- 	struct ext4_inode_info *ei = EXT4_I(inode);
- 	int idxs, num = 0;
+@@ -1927,13 +1927,13 @@ do {								\
+ } while (0)
  
--	idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
-+	idxs = ((EXT4_CLUSTER_SIZE(inode->i_sb) -
-+			sizeof(struct ext4_extent_header))
- 		/ sizeof(struct ext4_extent_idx));
+ #ifdef CONFIG_SMP
+-/* Each CPU can accumulate percpu_counter_batch blocks in their local
+- * counters. So we need to make sure we have free blocks more
++/* Each CPU can accumulate percpu_counter_batch values in their local
++ * counters. So we need to make sure we have free clusters more
+  * than percpu_counter_batch  * nr_cpu_ids. Also add a window of 4 times.
+  */
+-#define EXT4_FREEBLOCKS_WATERMARK (4 * (percpu_counter_batch * nr_cpu_ids))
++#define EXT4_FREECLUSTERS_WATERMARK (4 * (percpu_counter_batch * nr_cpu_ids))
+ #else
+-#define EXT4_FREEBLOCKS_WATERMARK 0
++#define EXT4_FREECLUSTERS_WATERMARK 0
+ #endif
  
- 	/*
+ static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
-index 4300ccd..ea51135 100644
+index b71c0d8..bbb1aae 100644
 --- a/fs/ext4/inode.c
 +++ b/fs/ext4/inode.c
 @@ -1107,7 +1107,7 @@ void ext4_da_update_reserve_space(struct inode *inode,
@@ -208,7 +220,24 @@
  				   ei->i_reserved_meta_blocks);
  		ei->i_reserved_meta_blocks = 0;
  		ei->i_da_metadata_calc_len = 0;
-@@ -1854,7 +1854,7 @@ static int ext4_journalled_write_end(struct file *file,
+@@ -1126,14 +1126,14 @@ void ext4_da_update_reserve_space(struct inode *inode,
+ 
+ 	/* Update quota subsystem for data blocks */
+ 	if (quota_claim)
+-		dquot_claim_block(inode, used);
++		dquot_claim_block(inode, EXT4_C2B(sbi, used));
+ 	else {
+ 		/*
+ 		 * We did fallocate with an offset that is already delayed
+ 		 * allocated. So on delayed allocated writeback we should
+ 		 * not re-claim the quota for fallocated blocks.
+ 		 */
+-		dquot_release_reservation_block(inode, used);
++		dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
+ 	}
+ 
+ 	/*
+@@ -1854,14 +1854,14 @@ static int ext4_journalled_write_end(struct file *file,
  }
  
  /*
@@ -217,18 +246,49 @@
   */
  static int ext4_da_reserve_space(struct inode *inode, sector_t lblock)
  {
-@@ -1887,7 +1887,7 @@ repeat:
+ 	int retries = 0;
+ 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
+ 	struct ext4_inode_info *ei = EXT4_I(inode);
+-	unsigned long md_needed;
++	unsigned int md_needed;
+ 	int ret;
+ 
+ 	/*
+@@ -1871,7 +1871,8 @@ static int ext4_da_reserve_space(struct inode *inode, sector_t lblock)
+ 	 */
+ repeat:
+ 	spin_lock(&ei->i_block_reservation_lock);
+-	md_needed = ext4_calc_metadata_amount(inode, lblock);
++	md_needed = EXT4_NUM_B2C(sbi,
++			ext4_calc_metadata_amount(inode, lblock));
+ 	trace_ext4_da_reserve_space(inode, md_needed);
+ 	spin_unlock(&ei->i_block_reservation_lock);
+ 
+@@ -1880,15 +1881,15 @@ repeat:
+ 	 * us from metadata over-estimation, though we may go over by
+ 	 * a small amount in the end.  Here we just reserve for data.
+ 	 */
+-	ret = dquot_reserve_block(inode, 1);
++	ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
+ 	if (ret)
+ 		return ret;
+ 	/*
  	 * We do still charge estimated metadata to the sb though;
  	 * we cannot afford to run out of free blocks.
  	 */
 -	if (ext4_claim_free_blocks(sbi, md_needed + 1)) {
+-		dquot_release_reservation_block(inode, 1);
 +	if (ext4_claim_free_clusters(sbi, md_needed + 1)) {
- 		dquot_release_reservation_block(inode, 1);
++		dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
  		if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  			yield();
-@@ -1936,14 +1936,14 @@ static void ext4_da_release_space(struct inode *inode, int to_free)
+ 			goto repeat;
+@@ -1935,19 +1936,21 @@ static void ext4_da_release_space(struct inode *inode, int to_free)
+ 		 * We can release all of the reserved metadata blocks
  		 * only when we have written all of the delayed
  		 * allocation blocks.
++		 * Note that in case of bigalloc, i_reserved_meta_blocks,
++		 * i_reserved_data_blocks, etc. refer to number of clusters.
  		 */
 -		percpu_counter_sub(&sbi->s_dirtyblocks_counter,
 +		percpu_counter_sub(&sbi->s_dirtyclusters_counter,
@@ -243,7 +303,12 @@
  
  	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  
-@@ -2172,7 +2172,8 @@ static void ext4_print_free_blocks(struct inode *inode)
+-	dquot_release_reservation_block(inode, to_free);
++	dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
+ }
+ 
+ static void ext4_da_page_release_reservation(struct page *page,
+@@ -2172,7 +2175,8 @@ static void ext4_print_free_blocks(struct inode *inode)
  	       (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
  		percpu_counter_sum(&sbi->s_freeclusters_counter)));
  	printk(KERN_CRIT "dirty_blocks=%lld\n",
@@ -253,7 +318,7 @@
  	printk(KERN_CRIT "Block reservation details\n");
  	printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
  	       EXT4_I(inode)->i_reserved_data_blocks);
-@@ -3143,7 +3144,8 @@ static int ext4_nonda_switch(struct super_block *sb)
+@@ -3143,9 +3147,10 @@ static int ext4_nonda_switch(struct super_block *sb)
  	 */
  	free_blocks  = EXT4_C2B(sbi,
  		percpu_counter_read_positive(&sbi->s_freeclusters_counter));
@@ -261,13 +326,16 @@
 +	dirty_blocks = EXT4_C2B(sbi,
 +		percpu_counter_read_positive(&sbi->s_dirtyclusters_counter));
  	if (2 * free_blocks < 3 * dirty_blocks ||
- 		free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
+-		free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
++		free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
  		/*
+ 		 * free block count is less than 150% of dirty blocks
+ 		 * or free blocks is less than watermark
 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
-index 4fba39c..f2f5c51 100644
+index 497a6aa..9723fec 100644
 --- a/fs/ext4/mballoc.c
 +++ b/fs/ext4/mballoc.c
-@@ -2725,7 +2725,7 @@ void exit_ext4_mballoc(void)
+@@ -2731,7 +2731,7 @@ void exit_ext4_mballoc(void)
   */
  static noinline_for_stack int
  ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
@@ -276,7 +344,7 @@
  {
  	struct buffer_head *bitmap_bh = NULL;
  	struct ext4_group_desc *gdp;
-@@ -2810,7 +2810,8 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
+@@ -2816,7 +2816,8 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  	 */
  	if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  		/* release all the reserved blocks if non delalloc */
@@ -286,7 +354,7 @@
  
  	if (sbi->s_log_groups_per_flex) {
  		ext4_group_t flex_group = ext4_flex_group(sbi,
-@@ -4287,7 +4288,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
+@@ -4293,7 +4294,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  	struct super_block *sb;
  	ext4_fsblk_t block = 0;
  	unsigned int inquota = 0;
@@ -295,7 +363,7 @@
  
  	sb = ar->inode->i_sb;
  	sbi = EXT4_SB(sb);
-@@ -4306,7 +4307,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
+@@ -4312,7 +4313,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  		 * there is enough free blocks to do block allocation
  		 * and verify allocation doesn't exceed the quota limits.
  		 */
@@ -304,7 +372,7 @@
  			/* let others to free the space */
  			yield();
  			ar->len = ar->len >> 1;
-@@ -4315,7 +4316,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
+@@ -4321,7 +4322,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  			*errp = -ENOSPC;
  			return 0;
  		}
@@ -313,7 +381,7 @@
  		while (ar->len && dquot_alloc_block(ar->inode,
  						    EXT4_C2B(sbi, ar->len))) {
  			ar->flags |= EXT4_MB_HINT_NOPREALLOC;
-@@ -4357,8 +4358,8 @@ repeat:
+@@ -4363,8 +4364,8 @@ repeat:
  			ext4_mb_new_preallocation(ac);
  	}
  	if (likely(ac->ac_status == AC_STATUS_FOUND)) {
@@ -324,7 +392,7 @@
  			/*
  			 * drop the reference that we took
  			 * in ext4_mb_use_best_found
-@@ -4399,8 +4400,8 @@ out3:
+@@ -4405,8 +4406,8 @@ out3:
  	if (!ar->len) {
  		if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
  			/* release all the reserved blocks if non delalloc */
@@ -336,7 +404,7 @@
  
  	trace_ext4_allocate_blocks(ar, (unsigned long long)block);
 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
-index d286f6b..d15d09e 100644
+index 49da16d..6adcab5 100644
 --- a/fs/ext4/super.c
 +++ b/fs/ext4/super.c
 @@ -742,7 +742,7 @@ static void ext4_put_super(struct super_block *sb)
@@ -348,7 +416,7 @@
  	brelse(sbi->s_sbh);
  #ifdef CONFIG_QUOTA
  	for (i = 0; i < MAXQUOTAS; i++)
-@@ -2333,7 +2333,8 @@ static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
+@@ -2334,7 +2334,8 @@ static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
  					      char *buf)
  {
  	return snprintf(buf, PAGE_SIZE, "%llu\n",
@@ -358,7 +426,7 @@
  }
  
  static ssize_t session_write_kbytes_show(struct ext4_attr *a,
-@@ -3116,7 +3117,7 @@ no_journal:
+@@ -3126,7 +3127,7 @@ no_journal:
  		err = percpu_counter_init(&sbi->s_dirs_counter,
  					  ext4_count_dirs(sb));
  	if (!err)
@@ -367,7 +435,7 @@
  	if (err) {
  		ext4_msg(sb, KERN_ERR, "insufficient memory");
  		goto failed_mount_wq;
-@@ -3270,7 +3271,7 @@ failed_mount_wq:
+@@ -3280,7 +3281,7 @@ failed_mount_wq:
  	percpu_counter_destroy(&sbi->s_freeclusters_counter);
  	percpu_counter_destroy(&sbi->s_freeinodes_counter);
  	percpu_counter_destroy(&sbi->s_dirs_counter);
@@ -376,7 +444,7 @@
  failed_mount3:
  	if (sbi->s_flex_groups) {
  		if (is_vmalloc_addr(sbi->s_flex_groups))
-@@ -4026,8 +4027,8 @@ static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
+@@ -4036,8 +4037,8 @@ static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  	buf->f_blocks = (ext4_blocks_count(es) -
  			 EXT4_C2B(sbi, sbi->s_overhead_last));
  	buf->f_bfree = EXT4_C2B(sbi,
