Btrfs: Add the ability to find and remove dead roots after a crash.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 865a284..d7615e1 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -326,8 +326,8 @@
 	return 0;
 }
 
-struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
-				      struct btrfs_key *location)
+struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
+					       struct btrfs_key *location)
 {
 	struct btrfs_root *root;
 	struct btrfs_root *tree_root = fs_info->tree_root;
@@ -336,11 +336,7 @@
 	u64 highest_inode;
 	int ret = 0;
 
-	root = radix_tree_lookup(&fs_info->fs_roots_radix,
-				 (unsigned long)location->objectid);
-	if (root)
-		return root;
-	root = kmalloc(sizeof(*root), GFP_NOFS);
+	root = kzalloc(sizeof(*root), GFP_NOFS);
 	if (!root)
 		return ERR_PTR(-ENOMEM);
 	if (location->offset == (u64)-1) {
@@ -383,6 +379,28 @@
 	BUG_ON(!root->node);
 insert:
 	root->ref_cows = 1;
+	ret = btrfs_find_highest_inode(root, &highest_inode);
+	if (ret == 0) {
+		root->highest_inode = highest_inode;
+		root->last_inode_alloc = highest_inode;
+	}
+	return root;
+}
+
+struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
+				      struct btrfs_key *location)
+{
+	struct btrfs_root *root;
+	int ret;
+
+	root = radix_tree_lookup(&fs_info->fs_roots_radix,
+				 (unsigned long)location->objectid);
+	if (root)
+		return root;
+
+	root = btrfs_read_fs_root_no_radix(fs_info, location);
+	if (IS_ERR(root))
+		return root;
 	ret = radix_tree_insert(&fs_info->fs_roots_radix,
 				(unsigned long)root->root_key.objectid,
 				root);
@@ -391,11 +409,6 @@
 		kfree(root);
 		return ERR_PTR(ret);
 	}
-	ret = btrfs_find_highest_inode(root, &highest_inode);
-	if (ret == 0) {
-		root->highest_inode = highest_inode;
-		root->last_inode_alloc = highest_inode;
-	}
 	return root;
 }
 
@@ -489,6 +502,9 @@
 	btrfs_read_block_groups(extent_root);
 
 	fs_info->generation = btrfs_super_generation(disk_super) + 1;
+	ret = btrfs_find_dead_roots(tree_root);
+	if (ret)
+		goto fail_tree_root;
 	mutex_unlock(&fs_info->fs_mutex);
 	return tree_root;
 
@@ -538,7 +554,7 @@
 	return 0;
 }
 
-static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
+int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
 {
 	radix_tree_delete(&fs_info->fs_roots_radix,
 			  (unsigned long)root->root_key.objectid);
@@ -565,7 +581,7 @@
 		if (!ret)
 			break;
 		for (i = 0; i < ret; i++)
-			free_fs_root(fs_info, gang[i]);
+			btrfs_free_fs_root(fs_info, gang[i]);
 	}
 	return 0;
 }