Btrfs: Enable delalloc accounting

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 6647b90..ad72e21 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -861,6 +861,10 @@
 	write_ctree_super(NULL, root);
 	mutex_unlock(&fs_info->fs_mutex);
 
+	if (fs_info->delalloc_bytes) {
+		printk("btrfs: at unmount delalloc count %Lu\n",
+		       fs_info->delalloc_bytes);
+	}
 	if (fs_info->extent_root->node)
 		free_extent_buffer(fs_info->extent_root->node);
 
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 6999b81..624aabc 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -262,7 +262,7 @@
 {
 	if (tree->ops && tree->ops->set_bit_hook) {
 		tree->ops->set_bit_hook(tree->mapping->host, state->start,
-					state->end, bits);
+					state->end, state->state, bits);
 	}
 }
 
@@ -272,7 +272,7 @@
 {
 	if (tree->ops && tree->ops->set_bit_hook) {
 		tree->ops->clear_bit_hook(tree->mapping->host, state->start,
-					  state->end, bits);
+					  state->end, state->state, bits);
 	}
 }
 
@@ -298,10 +298,10 @@
 	}
 	if (bits & EXTENT_DIRTY)
 		tree->dirty_bytes += end - start + 1;
+	set_state_cb(tree, state, bits);
 	state->state |= bits;
 	state->start = start;
 	state->end = end;
-	set_state_cb(tree, state, bits);
 	node = tree_insert(&tree->state, end, &state->rb_node);
 	if (node) {
 		struct extent_state *found;
@@ -369,8 +369,8 @@
 		WARN_ON(range > tree->dirty_bytes);
 		tree->dirty_bytes -= range;
 	}
-	state->state &= ~bits;
 	clear_state_cb(tree, state, bits);
+	state->state &= ~bits;
 	if (wake)
 		wake_up(&state->wq);
 	if (delete || state->state == 0) {
@@ -574,8 +574,8 @@
 		u64 range = state->end - state->start + 1;
 		tree->dirty_bytes += range;
 	}
-	state->state |= bits;
 	set_state_cb(tree, state, bits);
+	state->state |= bits;
 }
 
 /*
@@ -997,8 +997,8 @@
 			free_extent_state(state);
 			goto search_again;
 		}
-		state->state |= EXTENT_LOCKED;
 		set_state_cb(tree, state, EXTENT_LOCKED);
+		state->state |= EXTENT_LOCKED;
 		if (!found)
 			*start = state->start;
 		found++;
@@ -1497,8 +1497,8 @@
 			} else {
 				state = NULL;
 			}
-			clear->state |= EXTENT_UPTODATE;
 			set_state_cb(tree, clear, EXTENT_UPTODATE);
+			clear->state |= EXTENT_UPTODATE;
 			clear_state_bit(tree, clear, EXTENT_LOCKED,
 					1, 0);
 			if (cur == start)
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index a96c5a1..6fd5e2c 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -34,9 +34,9 @@
 	void (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
 				      struct extent_state *state);
 	int (*set_bit_hook)(struct inode *inode, u64 start, u64 end,
-			    unsigned long bits);
+			    unsigned long old, unsigned long bits);
 	int (*clear_bit_hook)(struct inode *inode, u64 start, u64 end,
-			    unsigned long bits);
+			    unsigned long old, unsigned long bits);
 };
 
 struct extent_io_tree {
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index bcf3b35..f13b1db 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -260,9 +260,9 @@
 }
 
 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
-		       unsigned long bits)
+		       unsigned long old, unsigned long bits)
 {
-	if ((bits & EXTENT_DELALLOC)) {
+	if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
 		struct btrfs_root *root = BTRFS_I(inode)->root;
 		spin_lock(&root->fs_info->delalloc_lock);
 		root->fs_info->delalloc_bytes += end - start + 1;
@@ -272,12 +272,18 @@
 }
 
 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
-			 unsigned long bits)
+			 unsigned long old, unsigned long bits)
 {
-	if ((bits & EXTENT_DELALLOC)) {
+	if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
 		struct btrfs_root *root = BTRFS_I(inode)->root;
 		spin_lock(&root->fs_info->delalloc_lock);
-		root->fs_info->delalloc_bytes -= end - start + 1;
+		if (end - start + 1 > root->fs_info->delalloc_bytes) {
+			printk("warning: delalloc account %Lu %Lu\n",
+			       end - start + 1, root->fs_info->delalloc_bytes);
+			root->fs_info->delalloc_bytes = 0;
+		} else {
+			root->fs_info->delalloc_bytes -= end - start + 1;
+		}
 		spin_unlock(&root->fs_info->delalloc_lock);
 	}
 	return 0;
@@ -3002,6 +3008,8 @@
 	.writepage_io_hook = btrfs_writepage_io_hook,
 	.readpage_io_hook = btrfs_readpage_io_hook,
 	.readpage_end_io_hook = btrfs_readpage_end_io_hook,
+	.set_bit_hook = btrfs_set_bit_hook,
+	.clear_bit_hook = btrfs_clear_bit_hook,
 };
 
 static struct address_space_operations btrfs_aops = {