Josef Bacik 8a3df8
diff --git a/btrfsck.c b/btrfsck.c
Josef Bacik 8a3df8
index 40c90f8..73f1836 100644
Josef Bacik 8a3df8
--- a/btrfsck.c
Josef Bacik 8a3df8
+++ b/btrfsck.c
Josef Bacik 8a3df8
@@ -36,7 +36,7 @@ static u64 total_fs_tree_bytes = 0;
Josef Bacik 8a3df8
 static u64 btree_space_waste = 0;
Josef Bacik 8a3df8
 static u64 data_bytes_allocated = 0;
Josef Bacik 8a3df8
 static u64 data_bytes_referenced = 0;
Josef Bacik 8a3df8
-int found_old_backref = 0;
Josef Bacik 8a3df8
+static int found_old_backref = 0;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 struct extent_backref {
Josef Bacik 8a3df8
 	struct list_head list;
Josef Bacik 8a3df8
@@ -100,7 +100,11 @@ struct inode_backref {
Josef Bacik 8a3df8
 #define REF_ERR_DUP_INODE_REF		(1 << 5)
Josef Bacik 8a3df8
 #define REF_ERR_INDEX_UNMATCH		(1 << 6)
Josef Bacik 8a3df8
 #define REF_ERR_FILETYPE_UNMATCH	(1 << 7)
Josef Bacik 8a3df8
-#define REF_ERR_NAME_TOO_LONG		(1 << 8)
Josef Bacik 8a3df8
+#define REF_ERR_NAME_TOO_LONG		(1 << 8) // 100
Josef Bacik 8a3df8
+#define REF_ERR_NO_ROOT_REF		(1 << 9)
Josef Bacik 8a3df8
+#define REF_ERR_NO_ROOT_BACKREF		(1 << 10)
Josef Bacik 8a3df8
+#define REF_ERR_DUP_ROOT_REF		(1 << 11)
Josef Bacik 8a3df8
+#define REF_ERR_DUP_ROOT_BACKREF	(1 << 12)
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 struct inode_record {
Josef Bacik 8a3df8
 	struct list_head backrefs;
Josef Bacik 8a3df8
@@ -144,6 +148,29 @@ struct inode_record {
Josef Bacik 8a3df8
 #define I_ERR_SOME_CSUM_MISSING		(1 << 12)
Josef Bacik 8a3df8
 #define I_ERR_LINK_COUNT_WRONG		(1 << 13)
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+struct root_backref {
Josef Bacik 8a3df8
+	struct list_head list;
Josef Bacik 8a3df8
+	unsigned int found_dir_item:1;
Josef Bacik 8a3df8
+	unsigned int found_dir_index:1;
Josef Bacik 8a3df8
+	unsigned int found_back_ref:1;
Josef Bacik 8a3df8
+	unsigned int found_forward_ref:1;
Josef Bacik 8a3df8
+	unsigned int reachable:1;
Josef Bacik 8a3df8
+	int errors;
Josef Bacik 8a3df8
+	u64 ref_root;
Josef Bacik 8a3df8
+	u64 dir;
Josef Bacik 8a3df8
+	u64 index;
Josef Bacik 8a3df8
+	u16 namelen;
Josef Bacik 8a3df8
+	char name[0];
Josef Bacik 8a3df8
+};
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+struct root_record {
Josef Bacik 8a3df8
+	struct list_head backrefs;
Josef Bacik 8a3df8
+	struct cache_extent cache;
Josef Bacik 8a3df8
+	unsigned int found_root_item:1;
Josef Bacik 8a3df8
+	u64 objectid;
Josef Bacik 8a3df8
+	u32 found_ref;
Josef Bacik 8a3df8
+};
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 struct ptr_node {
Josef Bacik 8a3df8
 	struct cache_extent cache;
Josef Bacik 8a3df8
 	void *data;
Josef Bacik 8a3df8
@@ -151,6 +178,7 @@ struct ptr_node {
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 struct shared_node {
Josef Bacik 8a3df8
 	struct cache_extent cache;
Josef Bacik 8a3df8
+	struct cache_tree root_cache;
Josef Bacik 8a3df8
 	struct cache_tree inode_cache;
Josef Bacik 8a3df8
 	struct inode_record *current;
Josef Bacik 8a3df8
 	u32 refs;
Josef Bacik 8a3df8
@@ -258,6 +286,14 @@ static void free_inode_rec(struct inode_record *rec)
Josef Bacik 8a3df8
 	free(rec);
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+static int can_free_inode_rec(struct inode_record *rec)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	if (!rec->errors && rec->checked && rec->found_inode_item &&
Josef Bacik 8a3df8
+	    rec->nlink == rec->found_link && list_empty(&rec->backrefs))
Josef Bacik 8a3df8
+		return 1;
Josef Bacik 8a3df8
+	return 0;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
Josef Bacik 8a3df8
 				 struct inode_record *rec)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
@@ -309,8 +345,7 @@ static void maybe_free_inode_rec(struct cache_tree *inode_cache,
Josef Bacik 8a3df8
 	}
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	BUG_ON(rec->refs != 1);
Josef Bacik 8a3df8
-	if (!rec->errors && rec->nlink == rec->found_link &&
Josef Bacik 8a3df8
-	    list_empty(&rec->backrefs)) {
Josef Bacik 8a3df8
+	if (can_free_inode_rec(rec)) {
Josef Bacik 8a3df8
 		cache = find_cache_extent(inode_cache, rec->ino, 1);
Josef Bacik 8a3df8
 		node = container_of(cache, struct ptr_node, cache);
Josef Bacik 8a3df8
 		BUG_ON(node->data != rec);
Josef Bacik 8a3df8
@@ -338,14 +373,12 @@ static int check_orphan_item(struct btrfs_root *root, u64 ino)
Josef Bacik 8a3df8
 	return ret;
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
-static int process_inode_item(struct btrfs_root *root,
Josef Bacik 8a3df8
-			      struct extent_buffer *eb,
Josef Bacik 8a3df8
+static int process_inode_item(struct extent_buffer *eb,
Josef Bacik 8a3df8
 			      int slot, struct btrfs_key *key,
Josef Bacik 8a3df8
 			      struct shared_node *active_node)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
 	struct inode_record *rec;
Josef Bacik 8a3df8
 	struct btrfs_inode_item *item;
Josef Bacik 8a3df8
-	int ret;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	rec = active_node->current;
Josef Bacik 8a3df8
 	BUG_ON(rec->ino != key->objectid || rec->refs > 1);
Josef Bacik 8a3df8
@@ -361,11 +394,8 @@ static int process_inode_item(struct btrfs_root *root,
Josef Bacik 8a3df8
 	if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
Josef Bacik 8a3df8
 		rec->nodatasum = 1;
Josef Bacik 8a3df8
 	rec->found_inode_item = 1;
Josef Bacik 8a3df8
-	if (rec->nlink == 0) {
Josef Bacik 8a3df8
-		ret = check_orphan_item(root, rec->ino);
Josef Bacik 8a3df8
-		if (ret == -ENOENT)
Josef Bacik 8a3df8
-			rec->errors |= I_ERR_NO_ORPHAN_ITEM;
Josef Bacik 8a3df8
-	}
Josef Bacik 8a3df8
+	if (rec->nlink == 0)
Josef Bacik 8a3df8
+		rec->errors |= I_ERR_NO_ORPHAN_ITEM;
Josef Bacik 8a3df8
 	maybe_free_inode_rec(&active_node->inode_cache, rec);
Josef Bacik 8a3df8
 	return 0;
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
@@ -443,10 +473,9 @@ static int add_inode_backref(struct cache_tree *inode_cache,
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
Josef Bacik 8a3df8
-			    struct shared_node *dst_node)
Josef Bacik 8a3df8
+			    struct cache_tree *dst_cache)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
 	struct inode_backref *backref;
Josef Bacik 8a3df8
-	struct cache_tree *dst_cache = &dst_node->inode_cache;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	dst->merging = 1;
Josef Bacik 8a3df8
 	list_for_each_entry(backref, &src->backrefs, list) {
Josef Bacik 8a3df8
@@ -510,14 +539,8 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
Josef Bacik 8a3df8
 			dst->errors |= I_ERR_DUP_INODE_ITEM;
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
 	}
Josef Bacik 8a3df8
-
Josef Bacik 8a3df8
-	if (src->checked) {
Josef Bacik 8a3df8
-		dst->checked = 1;
Josef Bacik 8a3df8
-		if (dst_node->current == dst)
Josef Bacik 8a3df8
-			dst_node->current = NULL;
Josef Bacik 8a3df8
-	}
Josef Bacik 8a3df8
 	dst->merging = 0;
Josef Bacik 8a3df8
-	maybe_free_inode_rec(dst_cache, dst);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 	return 0;
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
@@ -537,8 +560,9 @@ static int splice_shared_node(struct shared_node *src_node,
Josef Bacik 8a3df8
 	if (src_node->current)
Josef Bacik 8a3df8
 		current_ino = src_node->current->ino;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
-	src = &src_node->inode_cache;
Josef Bacik 8a3df8
-	dst = &dst_node->inode_cache;
Josef Bacik 8a3df8
+	src = &src_node->root_cache;
Josef Bacik 8a3df8
+	dst = &dst_node->root_cache;
Josef Bacik 8a3df8
+again:
Josef Bacik 8a3df8
 	cache = find_first_cache_extent(src, 0);
Josef Bacik 8a3df8
 	while (cache) {
Josef Bacik 8a3df8
 		node = container_of(cache, struct ptr_node, cache);
Josef Bacik 8a3df8
@@ -557,14 +581,28 @@ static int splice_shared_node(struct shared_node *src_node,
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
 		ret = insert_existing_cache_extent(dst, &ins->cache);
Josef Bacik 8a3df8
 		if (ret == -EEXIST) {
Josef Bacik 8a3df8
+			WARN_ON(src == &src_node->root_cache);
Josef Bacik 8a3df8
 			conflict = get_inode_rec(dst, rec->ino, 1);
Josef Bacik 8a3df8
-			merge_inode_recs(rec, conflict, dst_node);
Josef Bacik 8a3df8
+			merge_inode_recs(rec, conflict, dst);
Josef Bacik 8a3df8
+			if (rec->checked) {
Josef Bacik 8a3df8
+				conflict->checked = 1;
Josef Bacik 8a3df8
+				if (dst_node->current == conflict)
Josef Bacik 8a3df8
+					dst_node->current = NULL;
Josef Bacik 8a3df8
+			}
Josef Bacik 8a3df8
+			maybe_free_inode_rec(dst, conflict);
Josef Bacik 8a3df8
 			free_inode_rec(rec);
Josef Bacik 8a3df8
 			free(ins);
Josef Bacik 8a3df8
 		} else {
Josef Bacik 8a3df8
 			BUG_ON(ret);
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
 	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	if (src == &src_node->root_cache) {
Josef Bacik 8a3df8
+		src = &src_node->inode_cache;
Josef Bacik 8a3df8
+		dst = &dst_node->inode_cache;
Josef Bacik 8a3df8
+		goto again;
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 	if (current_ino > 0 && (!dst_node->current ||
Josef Bacik 8a3df8
 	    current_ino > dst_node->current->ino)) {
Josef Bacik 8a3df8
 		if (dst_node->current) {
Josef Bacik 8a3df8
@@ -616,6 +654,7 @@ static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
Josef Bacik 8a3df8
 	node = calloc(1, sizeof(*node));
Josef Bacik 8a3df8
 	node->cache.start = bytenr;
Josef Bacik 8a3df8
 	node->cache.size = 1;
Josef Bacik 8a3df8
+	cache_tree_init(&node->root_cache);
Josef Bacik 8a3df8
 	cache_tree_init(&node->inode_cache);
Josef Bacik 8a3df8
 	node->refs = refs;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
@@ -646,6 +685,7 @@ static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
Josef Bacik 8a3df8
 	if (wc->root_level == wc->active_node &&
Josef Bacik 8a3df8
 	    btrfs_root_refs(&root->root_item) == 0) {
Josef Bacik 8a3df8
 		if (--node->refs == 0) {
Josef Bacik 8a3df8
+			free_inode_recs(&node->root_cache);
Josef Bacik 8a3df8
 			free_inode_recs(&node->inode_cache);
Josef Bacik 8a3df8
 			remove_cache_extent(&wc->shared, &node->cache);
Josef Bacik 8a3df8
 			free(node);
Josef Bacik 8a3df8
@@ -708,10 +748,12 @@ static int process_dir_item(struct extent_buffer *eb,
Josef Bacik 8a3df8
 	int filetype;
Josef Bacik 8a3df8
 	struct btrfs_dir_item *di;
Josef Bacik 8a3df8
 	struct inode_record *rec;
Josef Bacik 8a3df8
+	struct cache_tree *root_cache;
Josef Bacik 8a3df8
 	struct cache_tree *inode_cache;
Josef Bacik 8a3df8
 	struct btrfs_key location;
Josef Bacik 8a3df8
 	char namebuf[BTRFS_NAME_LEN];
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+	root_cache = &active_node->root_cache;
Josef Bacik 8a3df8
 	inode_cache = &active_node->inode_cache;
Josef Bacik 8a3df8
 	rec = active_node->current;
Josef Bacik 8a3df8
 	rec->found_dir_item = 1;
Josef Bacik 8a3df8
@@ -740,7 +782,9 @@ static int process_dir_item(struct extent_buffer *eb,
Josef Bacik 8a3df8
 					  key->objectid, key->offset, namebuf,
Josef Bacik 8a3df8
 					  len, filetype, key->type, error);
Josef Bacik 8a3df8
 		} else if (location.type == BTRFS_ROOT_ITEM_KEY) {
Josef Bacik 8a3df8
-			/* fixme: check root back & forward references */
Josef Bacik 8a3df8
+			add_inode_backref(root_cache, location.objectid,
Josef Bacik 8a3df8
+					  key->objectid, key->offset, namebuf,
Josef Bacik 8a3df8
+					  len, filetype, key->type, error);
Josef Bacik 8a3df8
 		} else {
Josef Bacik 8a3df8
 			fprintf(stderr, "warning line %d\n", __LINE__);
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
@@ -977,8 +1021,7 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
Josef Bacik 8a3df8
 			ret = process_inode_ref(eb, i, &key, active_node);
Josef Bacik 8a3df8
 			break;
Josef Bacik 8a3df8
 		case BTRFS_INODE_ITEM_KEY:
Josef Bacik 8a3df8
-			ret = process_inode_item(root, eb, i, &key,
Josef Bacik 8a3df8
-						 active_node);
Josef Bacik 8a3df8
+			ret = process_inode_item(eb, i, &key, active_node);
Josef Bacik 8a3df8
 			break;
Josef Bacik 8a3df8
 		case BTRFS_EXTENT_DATA_KEY:
Josef Bacik 8a3df8
 			ret = process_file_extent(root, eb, i, &key,
Josef Bacik 8a3df8
@@ -1176,13 +1219,23 @@ static int check_inode_recs(struct btrfs_root *root,
Josef Bacik 8a3df8
 		node = container_of(cache, struct ptr_node, cache);
Josef Bacik 8a3df8
 		rec = node->data;
Josef Bacik 8a3df8
 		remove_cache_extent(inode_cache, &node->cache);
Josef Bacik 8a3df8
+		free(node);
Josef Bacik 8a3df8
 		if (rec->ino == root_dirid ||
Josef Bacik 8a3df8
 		    rec->ino == BTRFS_ORPHAN_OBJECTID) {
Josef Bacik 8a3df8
-			free(node);
Josef Bacik 8a3df8
 			free_inode_rec(rec);
Josef Bacik 8a3df8
 			continue;
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+		if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
Josef Bacik 8a3df8
+			ret = check_orphan_item(root, rec->ino);
Josef Bacik 8a3df8
+			if (ret == 0)
Josef Bacik 8a3df8
+				rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
Josef Bacik 8a3df8
+			if (can_free_inode_rec(rec)) {
Josef Bacik 8a3df8
+				free_inode_rec(rec);
Josef Bacik 8a3df8
+				continue;
Josef Bacik 8a3df8
+			}
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 		error++;
Josef Bacik 8a3df8
 		if (!rec->found_inode_item)
Josef Bacik 8a3df8
 			rec->errors |= I_ERR_NO_INODE_ITEM;
Josef Bacik 8a3df8
@@ -1205,13 +1258,314 @@ static int check_inode_recs(struct btrfs_root *root,
Josef Bacik 8a3df8
 				backref->namelen, backref->name,
Josef Bacik 8a3df8
 				backref->filetype, backref->errors);
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
-		free(node);
Josef Bacik 8a3df8
 		free_inode_rec(rec);
Josef Bacik 8a3df8
 	}
Josef Bacik 8a3df8
 	return (error > 0) ? -1 : 0;
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+static struct root_record *get_root_rec(struct cache_tree *root_cache,
Josef Bacik 8a3df8
+					u64 objectid)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	struct cache_extent *cache;
Josef Bacik 8a3df8
+	struct root_record *rec = NULL;
Josef Bacik 8a3df8
+	int ret;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	cache = find_cache_extent(root_cache, objectid, 1);
Josef Bacik 8a3df8
+	if (cache) {
Josef Bacik 8a3df8
+		rec = container_of(cache, struct root_record, cache);
Josef Bacik 8a3df8
+	} else {
Josef Bacik 8a3df8
+		rec = calloc(1, sizeof(*rec));
Josef Bacik 8a3df8
+		rec->objectid = objectid;
Josef Bacik 8a3df8
+		INIT_LIST_HEAD(&rec->backrefs);
Josef Bacik 8a3df8
+		rec->cache.start = objectid;
Josef Bacik 8a3df8
+		rec->cache.size = 1;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		ret = insert_existing_cache_extent(root_cache, &rec->cache);
Josef Bacik 8a3df8
+		BUG_ON(ret);
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+	return rec;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+static struct root_backref *get_root_backref(struct root_record *rec,
Josef Bacik 8a3df8
+					     u64 ref_root, u64 dir, u64 index,
Josef Bacik 8a3df8
+					     const char *name, int namelen)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	struct root_backref *backref;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	list_for_each_entry(backref, &rec->backrefs, list) {
Josef Bacik 8a3df8
+		if (backref->ref_root != ref_root || backref->dir != dir ||
Josef Bacik 8a3df8
+		    backref->namelen != namelen)
Josef Bacik 8a3df8
+			continue;
Josef Bacik 8a3df8
+		if (memcmp(name, backref->name, namelen))
Josef Bacik 8a3df8
+			continue;
Josef Bacik 8a3df8
+		return backref;
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	backref = malloc(sizeof(*backref) + namelen + 1);
Josef Bacik 8a3df8
+	memset(backref, 0, sizeof(*backref));
Josef Bacik 8a3df8
+	backref->ref_root = ref_root;
Josef Bacik 8a3df8
+	backref->dir = dir;
Josef Bacik 8a3df8
+	backref->index = index;
Josef Bacik 8a3df8
+	backref->namelen = namelen;
Josef Bacik 8a3df8
+	memcpy(backref->name, name, namelen);
Josef Bacik 8a3df8
+	backref->name[namelen] = '\0';
Josef Bacik 8a3df8
+	list_add_tail(&backref->list, &rec->backrefs);
Josef Bacik 8a3df8
+	return backref;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+static void free_root_recs(struct cache_tree *root_cache)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	struct cache_extent *cache;
Josef Bacik 8a3df8
+	struct root_record *rec;
Josef Bacik 8a3df8
+	struct root_backref *backref;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	while (1) {
Josef Bacik 8a3df8
+		cache = find_first_cache_extent(root_cache, 0);
Josef Bacik 8a3df8
+		if (!cache)
Josef Bacik 8a3df8
+			break;
Josef Bacik 8a3df8
+		rec = container_of(cache, struct root_record, cache);
Josef Bacik 8a3df8
+		remove_cache_extent(root_cache, &rec->cache);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		while (!list_empty(&rec->backrefs)) {
Josef Bacik 8a3df8
+			backref = list_entry(rec->backrefs.next,
Josef Bacik 8a3df8
+					     struct root_backref, list);
Josef Bacik 8a3df8
+			list_del(&backref->list);
Josef Bacik 8a3df8
+			free(backref);
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+		kfree(rec);
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+static int add_root_backref(struct cache_tree *root_cache,
Josef Bacik 8a3df8
+			    u64 root_id, u64 ref_root, u64 dir, u64 index,
Josef Bacik 8a3df8
+			    const char *name, int namelen,
Josef Bacik 8a3df8
+			    int item_type, int errors)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	struct root_record *rec;
Josef Bacik 8a3df8
+	struct root_backref *backref;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	rec = get_root_rec(root_cache, root_id);
Josef Bacik 8a3df8
+	backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	backref->errors |= errors;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	if (item_type != BTRFS_DIR_ITEM_KEY) {
Josef Bacik 8a3df8
+		if (backref->found_dir_index || backref->found_back_ref ||
Josef Bacik 8a3df8
+		    backref->found_forward_ref) {
Josef Bacik 8a3df8
+			if (backref->index != index)
Josef Bacik 8a3df8
+				backref->errors |= REF_ERR_INDEX_UNMATCH;
Josef Bacik 8a3df8
+		} else {
Josef Bacik 8a3df8
+			backref->index = index;
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	if (item_type == BTRFS_DIR_ITEM_KEY) {
Josef Bacik 8a3df8
+		backref->found_dir_item = 1;
Josef Bacik 8a3df8
+		backref->reachable = 1;
Josef Bacik 8a3df8
+		rec->found_ref++;
Josef Bacik 8a3df8
+	} else if (item_type == BTRFS_DIR_INDEX_KEY) {
Josef Bacik 8a3df8
+		backref->found_dir_index = 1;
Josef Bacik 8a3df8
+	} else if (item_type == BTRFS_ROOT_REF_KEY) {
Josef Bacik 8a3df8
+		if (backref->found_forward_ref)
Josef Bacik 8a3df8
+			backref->errors |= REF_ERR_DUP_ROOT_REF;
Josef Bacik 8a3df8
+		backref->found_forward_ref = 1;
Josef Bacik 8a3df8
+	} else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
Josef Bacik 8a3df8
+		if (backref->found_back_ref)
Josef Bacik 8a3df8
+			backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
Josef Bacik 8a3df8
+		backref->found_back_ref = 1;
Josef Bacik 8a3df8
+	} else {
Josef Bacik 8a3df8
+		BUG_ON(1);
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	return 0;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+static int merge_root_recs(struct btrfs_root *root,
Josef Bacik 8a3df8
+			   struct cache_tree *src_cache,
Josef Bacik 8a3df8
+			   struct cache_tree *dst_cache)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	struct cache_extent *cache;
Josef Bacik 8a3df8
+	struct ptr_node *node;
Josef Bacik 8a3df8
+	struct inode_record *rec;
Josef Bacik 8a3df8
+	struct inode_backref *backref;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacik 8a3df8
+		free_inode_recs(src_cache);
Josef Bacik 8a3df8
+		return 0;
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	while (1) {
Josef Bacik 8a3df8
+		cache = find_first_cache_extent(src_cache, 0);
Josef Bacik 8a3df8
+		if (!cache)
Josef Bacik 8a3df8
+			break;
Josef Bacik 8a3df8
+		node = container_of(cache, struct ptr_node, cache);
Josef Bacik 8a3df8
+		rec = node->data;
Josef Bacik 8a3df8
+		remove_cache_extent(src_cache, &node->cache);
Josef Bacik 8a3df8
+		free(node);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		list_for_each_entry(backref, &rec->backrefs, list) {
Josef Bacik 8a3df8
+			BUG_ON(backref->found_inode_ref);
Josef Bacik 8a3df8
+			if (backref->found_dir_item)
Josef Bacik 8a3df8
+				add_root_backref(dst_cache, rec->ino,
Josef Bacik 8a3df8
+					root->root_key.objectid, backref->dir,
Josef Bacik 8a3df8
+					backref->index, backref->name,
Josef Bacik 8a3df8
+					backref->namelen, BTRFS_DIR_ITEM_KEY,
Josef Bacik 8a3df8
+					backref->errors);
Josef Bacik 8a3df8
+			if (backref->found_dir_index)
Josef Bacik 8a3df8
+				add_root_backref(dst_cache, rec->ino,
Josef Bacik 8a3df8
+					root->root_key.objectid, backref->dir,
Josef Bacik 8a3df8
+					backref->index, backref->name,
Josef Bacik 8a3df8
+					backref->namelen, BTRFS_DIR_INDEX_KEY,
Josef Bacik 8a3df8
+					backref->errors);
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+		free_inode_rec(rec);
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+	return 0;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+static int check_root_refs(struct btrfs_root *root,
Josef Bacik 8a3df8
+			   struct cache_tree *root_cache)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	struct root_record *rec;
Josef Bacik 8a3df8
+	struct root_record *ref_root;
Josef Bacik 8a3df8
+	struct root_backref *backref;
Josef Bacik 8a3df8
+	struct cache_extent *cache;
Josef Bacik 8a3df8
+	int loop = 1;
Josef Bacik 8a3df8
+	int ret;
Josef Bacik 8a3df8
+	int error;
Josef Bacik 8a3df8
+	int errors = 0;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
Josef Bacik 8a3df8
+	rec->found_ref = 1;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	/* fixme: this can not detect circular references */
Josef Bacik 8a3df8
+	while (loop) {
Josef Bacik 8a3df8
+		loop = 0;
Josef Bacik 8a3df8
+		cache = find_first_cache_extent(root_cache, 0);
Josef Bacik 8a3df8
+		while (1) {
Josef Bacik 8a3df8
+			if (!cache)
Josef Bacik 8a3df8
+				break;
Josef Bacik 8a3df8
+			rec = container_of(cache, struct root_record, cache);
Josef Bacik 8a3df8
+			cache = next_cache_extent(cache);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+			if (rec->found_ref == 0)
Josef Bacik 8a3df8
+				continue;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+			list_for_each_entry(backref, &rec->backrefs, list) {
Josef Bacik 8a3df8
+				if (!backref->reachable)
Josef Bacik 8a3df8
+					continue;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+				ref_root = get_root_rec(root_cache,
Josef Bacik 8a3df8
+							backref->ref_root);
Josef Bacik 8a3df8
+				if (ref_root->found_ref > 0)
Josef Bacik 8a3df8
+					continue;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+				backref->reachable = 0;
Josef Bacik 8a3df8
+				rec->found_ref--;
Josef Bacik 8a3df8
+				if (rec->found_ref == 0)
Josef Bacik 8a3df8
+					loop = 1;
Josef Bacik 8a3df8
+			}
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	cache = find_first_cache_extent(root_cache, 0);
Josef Bacik 8a3df8
+	while (1) {
Josef Bacik 8a3df8
+		if (!cache)
Josef Bacik 8a3df8
+			break;
Josef Bacik 8a3df8
+		rec = container_of(cache, struct root_record, cache);
Josef Bacik 8a3df8
+		cache = next_cache_extent(cache);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		if (rec->found_ref == 0 &&
Josef Bacik 8a3df8
+		    rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
Josef Bacik 8a3df8
+		    rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
Josef Bacik 8a3df8
+			ret = check_orphan_item(root->fs_info->tree_root,
Josef Bacik 8a3df8
+						rec->objectid);
Josef Bacik 8a3df8
+			if (ret == 0)
Josef Bacik 8a3df8
+				continue;
Josef Bacik 8a3df8
+			errors++;
Josef Bacik 8a3df8
+			fprintf(stderr, "fs tree %llu not referenced\n",
Josef Bacik 8a3df8
+				(unsigned long long)rec->objectid);
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		error = 0;
Josef Bacik 8a3df8
+		if (rec->found_ref > 0 && !rec->found_root_item)
Josef Bacik 8a3df8
+			error = 1;
Josef Bacik 8a3df8
+		list_for_each_entry(backref, &rec->backrefs, list) {
Josef Bacik 8a3df8
+			if (!backref->found_dir_item)
Josef Bacik 8a3df8
+				backref->errors |= REF_ERR_NO_DIR_ITEM;
Josef Bacik 8a3df8
+			if (!backref->found_dir_index)
Josef Bacik 8a3df8
+				backref->errors |= REF_ERR_NO_DIR_INDEX;
Josef Bacik 8a3df8
+			if (!backref->found_back_ref)
Josef Bacik 8a3df8
+				backref->errors |= REF_ERR_NO_ROOT_BACKREF;
Josef Bacik 8a3df8
+			if (!backref->found_forward_ref)
Josef Bacik 8a3df8
+				backref->errors |= REF_ERR_NO_ROOT_REF;
Josef Bacik 8a3df8
+			if (backref->reachable && backref->errors)
Josef Bacik 8a3df8
+				error = 1;
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+		if (!error)
Josef Bacik 8a3df8
+			continue;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		errors++;
Josef Bacik 8a3df8
+		fprintf(stderr, "fs tree %llu refs %u %s\n",
Josef Bacik 8a3df8
+			(unsigned long long)rec->objectid, rec->found_ref,
Josef Bacik 8a3df8
+			 rec->found_root_item ? "" : "not found");
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+		list_for_each_entry(backref, &rec->backrefs, list) {
Josef Bacik 8a3df8
+			if (!backref->reachable)
Josef Bacik 8a3df8
+				continue;
Josef Bacik 8a3df8
+			if (!backref->errors && rec->found_root_item)
Josef Bacik 8a3df8
+				continue;
Josef Bacik 8a3df8
+			fprintf(stderr, "\tunresolved ref root %llu dir %llu"
Josef Bacik 8a3df8
+				" index %llu namelen %u name %s error %x\n",
Josef Bacik 8a3df8
+				(unsigned long long)backref->ref_root,
Josef Bacik 8a3df8
+				(unsigned long long)backref->dir,
Josef Bacik 8a3df8
+				(unsigned long long)backref->index,
Josef Bacik 8a3df8
+				backref->namelen, backref->name,
Josef Bacik 8a3df8
+				backref->errors);
Josef Bacik 8a3df8
+		}
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+	return errors > 0 ? 1 : 0;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+static int process_root_ref(struct extent_buffer *eb, int slot,
Josef Bacik 8a3df8
+			    struct btrfs_key *key,
Josef Bacik 8a3df8
+			    struct cache_tree *root_cache)
Josef Bacik 8a3df8
+{
Josef Bacik 8a3df8
+	u64 dirid;
Josef Bacik 8a3df8
+	u64 index;
Josef Bacik 8a3df8
+	u32 len;
Josef Bacik 8a3df8
+	u32 name_len;
Josef Bacik 8a3df8
+	struct btrfs_root_ref *ref;
Josef Bacik 8a3df8
+	char namebuf[BTRFS_NAME_LEN];
Josef Bacik 8a3df8
+	int error;
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	dirid = btrfs_root_ref_dirid(eb, ref);
Josef Bacik 8a3df8
+	index = btrfs_root_ref_sequence(eb, ref);
Josef Bacik 8a3df8
+	name_len = btrfs_root_ref_name_len(eb, ref);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	if (name_len <= BTRFS_NAME_LEN) {
Josef Bacik 8a3df8
+		len = name_len;
Josef Bacik 8a3df8
+		error = 0;
Josef Bacik 8a3df8
+	} else {
Josef Bacik 8a3df8
+		len = BTRFS_NAME_LEN;
Josef Bacik 8a3df8
+		error = REF_ERR_NAME_TOO_LONG;
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+	read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+	if (key->type == BTRFS_ROOT_REF_KEY) {
Josef Bacik 8a3df8
+		add_root_backref(root_cache, key->offset, key->objectid, dirid,
Josef Bacik 8a3df8
+				 index, namebuf, len, key->type, error);
Josef Bacik 8a3df8
+	} else {
Josef Bacik 8a3df8
+		add_root_backref(root_cache, key->objectid, key->offset, dirid,
Josef Bacik 8a3df8
+				 index, namebuf, len, key->type, error);
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+	return 0;
Josef Bacik 8a3df8
+}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 static int check_fs_root(struct btrfs_root *root,
Josef Bacik 8a3df8
+			 struct cache_tree *root_cache,
Josef Bacik 8a3df8
 			 struct walk_control *wc)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
 	int ret = 0;
Josef Bacik 8a3df8
@@ -1219,10 +1573,18 @@ static int check_fs_root(struct btrfs_root *root,
Josef Bacik 8a3df8
 	int level;
Josef Bacik 8a3df8
 	struct btrfs_path path;
Josef Bacik 8a3df8
 	struct shared_node root_node;
Josef Bacik 8a3df8
+	struct root_record *rec;
Josef Bacik 8a3df8
 	struct btrfs_root_item *root_item = &root->root_item;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+	if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacik 8a3df8
+		rec = get_root_rec(root_cache, root->root_key.objectid);
Josef Bacik 8a3df8
+		if (btrfs_root_refs(root_item) > 0)
Josef Bacik 8a3df8
+			rec->found_root_item = 1;
Josef Bacik 8a3df8
+	}
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 	btrfs_init_path(&path);
Josef Bacik 8a3df8
 	memset(&root_node, 0, sizeof(root_node));
Josef Bacik 8a3df8
+	cache_tree_init(&root_node.root_cache);
Josef Bacik 8a3df8
 	cache_tree_init(&root_node.inode_cache);
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	level = btrfs_header_level(root->node);
Josef Bacik 8a3df8
@@ -1266,6 +1628,8 @@ static int check_fs_root(struct btrfs_root *root,
Josef Bacik 8a3df8
 	}
Josef Bacik 8a3df8
 	btrfs_release_path(root, &path);
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+	merge_root_recs(root, &root_node.root_cache, root_cache);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 	if (root_node.current) {
Josef Bacik 8a3df8
 		root_node.current->checked = 1;
Josef Bacik 8a3df8
 		maybe_free_inode_rec(&root_node.inode_cache,
Josef Bacik 8a3df8
@@ -1280,13 +1644,15 @@ static int fs_root_objectid(u64 objectid)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
 	if (objectid == BTRFS_FS_TREE_OBJECTID ||
Josef Bacik 8a3df8
 	    objectid == BTRFS_TREE_RELOC_OBJECTID ||
Josef Bacik 8a3df8
+	    objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
Josef Bacik 8a3df8
 	    (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
Josef Bacik 8a3df8
-	     objectid < BTRFS_LAST_FREE_OBJECTID))
Josef Bacik 8a3df8
+	     objectid <= BTRFS_LAST_FREE_OBJECTID))
Josef Bacik 8a3df8
 		return 1;
Josef Bacik 8a3df8
 	return 0;
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
-static int check_fs_roots(struct btrfs_root *root)
Josef Bacik 8a3df8
+static int check_fs_roots(struct btrfs_root *root,
Josef Bacik 8a3df8
+			  struct cache_tree *root_cache)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
 	struct btrfs_path path;
Josef Bacik 8a3df8
 	struct btrfs_key key;
Josef Bacik 8a3df8
@@ -1319,10 +1685,14 @@ static int check_fs_roots(struct btrfs_root *root)
Josef Bacik 8a3df8
 		    fs_root_objectid(key.objectid)) {
Josef Bacik 8a3df8
 			tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
Josef Bacik 8a3df8
 							       &key);
Josef Bacik 8a3df8
-			ret = check_fs_root(tmp_root, &wc);
Josef Bacik 8a3df8
+			ret = check_fs_root(tmp_root, root_cache, &wc);
Josef Bacik 8a3df8
 			if (ret)
Josef Bacik 8a3df8
 				err = 1;
Josef Bacik 8a3df8
 			btrfs_free_fs_root(root->fs_info, tmp_root);
Josef Bacik 8a3df8
+		} else if (key.type == BTRFS_ROOT_REF_KEY ||
Josef Bacik 8a3df8
+			   key.type == BTRFS_ROOT_BACKREF_KEY) {
Josef Bacik 8a3df8
+			process_root_ref(leaf, path.slots[0], &key,
Josef Bacik 8a3df8
+					 root_cache);
Josef Bacik 8a3df8
 		}
Josef Bacik 8a3df8
 		path.slots[0]++;
Josef Bacik 8a3df8
 	}
Josef Bacik 8a3df8
@@ -1895,7 +2265,6 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
Josef Bacik 8a3df8
 	return 0;
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
-
Josef Bacik 8a3df8
 static int add_pending(struct cache_tree *pending,
Josef Bacik 8a3df8
 		       struct cache_tree *seen, u64 bytenr, u32 size)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
@@ -2443,6 +2812,7 @@ static void print_usage(void)
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 int main(int ac, char **av)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
+	struct cache_tree root_cache;
Josef Bacik 8a3df8
 	struct btrfs_root *root;
Josef Bacik 8a3df8
 	int ret;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
@@ -2450,6 +2820,7 @@ int main(int ac, char **av)
Josef Bacik 8a3df8
 		print_usage();
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	radix_tree_init();
Josef Bacik 8a3df8
+	cache_tree_init(&root_cache);
Josef Bacik 8a3df8
 	root = open_ctree(av[1], 0, 0);
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	if (root == NULL)
Josef Bacik 8a3df8
@@ -2458,10 +2829,15 @@ int main(int ac, char **av)
Josef Bacik 8a3df8
 	ret = check_extents(root);
Josef Bacik 8a3df8
 	if (ret)
Josef Bacik 8a3df8
 		goto out;
Josef Bacik 8a3df8
-	ret = check_fs_roots(root);
Josef Bacik 8a3df8
+	ret = check_fs_roots(root, &root_cache);
Josef Bacik 8a3df8
+	if (ret)
Josef Bacik 8a3df8
+		goto out;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
+	ret = check_root_refs(root, &root_cache);
Josef Bacik 8a3df8
 out:
Josef Bacik 8a3df8
+	free_root_recs(&root_cache);
Josef Bacik 8a3df8
 	close_ctree(root);
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
 	if (found_old_backref) {
Josef Bacik 8a3df8
 		/*
Josef Bacik 8a3df8
 		 * there was a disk format change when mixed
Josef Bacik 8a3df8
diff --git a/btrfsctl.c b/btrfsctl.c
Josef Bacik 8a3df8
index b323818..66c4e89 100644
Josef Bacik 8a3df8
--- a/btrfsctl.c
Josef Bacik 8a3df8
+++ b/btrfsctl.c
Josef Bacik 8a3df8
@@ -46,7 +46,7 @@ static inline int ioctl(int fd, int define, void *arg) { return 0; }
Josef Bacik 8a3df8
 static void print_usage(void)
Josef Bacik 8a3df8
 {
Josef Bacik 8a3df8
 	printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n");
Josef Bacik 8a3df8
-	printf("                [-r size] [-A device] [-a] [-c]\n");
Josef Bacik 8a3df8
+	printf("                [-r size] [-A device] [-a] [-c] [-D dir .]\n");
Josef Bacik 8a3df8
 	printf("\t-d filename: defragments one file\n");
Josef Bacik 8a3df8
 	printf("\t-d directory: defragments the entire Btree\n");
Josef Bacik 8a3df8
 	printf("\t-s snap_name dir: creates a new snapshot of dir\n");
Josef Bacik 8a3df8
@@ -55,6 +55,7 @@ static void print_usage(void)
Josef Bacik 8a3df8
 	printf("\t-A device: scans the device file for a Btrfs filesystem\n");
Josef Bacik 8a3df8
 	printf("\t-a: scans all devices for Btrfs filesystems\n");
Josef Bacik 8a3df8
 	printf("\t-c: forces a single FS sync\n");
Josef Bacik 8a3df8
+	printf("\t-D: delete snapshot\n");
Josef Bacik 8a3df8
 	printf("%s\n", BTRFS_BUILD_VERSION);
Josef Bacik 8a3df8
 	exit(1);
Josef Bacik 8a3df8
 }
Josef Bacik 8a3df8
@@ -158,6 +159,18 @@ int main(int ac, char **av)
Josef Bacik 8a3df8
 				print_usage();
Josef Bacik 8a3df8
 			}
Josef Bacik 8a3df8
 			command = BTRFS_IOC_DEFRAG;
Josef Bacik 8a3df8
+		} else if (strcmp(av[i], "-D") == 0) {
Josef Bacik 8a3df8
+			if (i >= ac - 1) {
Josef Bacik 8a3df8
+				fprintf(stderr, "-D requires an arg\n");
Josef Bacik 8a3df8
+				print_usage();
Josef Bacik 8a3df8
+			}
Josef Bacik 8a3df8
+			command = BTRFS_IOC_SNAP_DESTROY;
Josef Bacik 8a3df8
+			name = av[i + 1];
Josef Bacik 8a3df8
+			len = strlen(name);
Josef Bacik 8a3df8
+			if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
Josef Bacik 8a3df8
+				fprintf(stderr, "-D size too long\n");
Josef Bacik 8a3df8
+				exit(1);
Josef Bacik 8a3df8
+			}
Josef Bacik 8a3df8
 		} else if (strcmp(av[i], "-A") == 0) {
Josef Bacik 8a3df8
 			if (i >= ac - 1) {
Josef Bacik 8a3df8
 				fprintf(stderr, "-A requires an arg\n");
Josef Bacik 8a3df8
diff --git a/convert.c b/convert.c
Josef Bacik 8a3df8
index d2c9efa..d037c98 100644
Josef Bacik 8a3df8
--- a/convert.c
Josef Bacik 8a3df8
+++ b/convert.c
Josef Bacik 8a3df8
@@ -370,7 +370,6 @@ static int record_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik 8a3df8
 	struct btrfs_extent_item *ei;
Josef Bacik 8a3df8
 	u32 blocksize = root->sectorsize;
Josef Bacik 8a3df8
 	u64 nbytes;
Josef Bacik 8a3df8
-	u64 bytes_used;
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	if (disk_bytenr == 0) {
Josef Bacik 8a3df8
 		ret = btrfs_insert_file_extent(trans, root, objectid,
Josef Bacik 8a3df8
@@ -432,9 +431,6 @@ static int record_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik 8a3df8
 	nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
Josef Bacik 8a3df8
 	btrfs_set_stack_inode_nbytes(inode, nbytes);
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
-	bytes_used = btrfs_root_used(&root->root_item);
Josef Bacik 8a3df8
-	btrfs_set_root_used(&root->root_item, bytes_used + num_bytes);
Josef Bacik 8a3df8
-
Josef Bacik 8a3df8
 	btrfs_release_path(root, &path);
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 	ins_key.objectid = disk_bytenr;
Josef Bacik 8a3df8
@@ -454,9 +450,6 @@ static int record_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
 		btrfs_mark_buffer_dirty(leaf);
Josef Bacik 8a3df8
 
Josef Bacik 8a3df8
-		bytes_used = btrfs_super_bytes_used(&info->super_copy);
Josef Bacik 8a3df8
-		btrfs_set_super_bytes_used(&info->super_copy, bytes_used +
Josef Bacik 8a3df8
-					   num_bytes);
Josef Bacik 8a3df8
 		ret = btrfs_update_block_group(trans, root, disk_bytenr,
Josef Bacik 8a3df8
 					       num_bytes, 1, 0);
Josef Bacik 8a3df8
 		if (ret)
Josef Bacik 8a3df8
diff --git a/ioctl.h b/ioctl.h
Josef Bacik 8a3df8
index a084f33..4410ac0 100644
Josef Bacik 8a3df8
--- a/ioctl.h
Josef Bacik 8a3df8
+++ b/ioctl.h
Josef Bacik 8a3df8
@@ -56,4 +56,7 @@ struct btrfs_ioctl_vol_args {
Josef Bacik 8a3df8
 /* 13 is for CLONE_RANGE */
Josef Bacik 8a3df8
 #define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, \
Josef Bacik 8a3df8
 				   struct btrfs_ioctl_vol_args)
Josef Bacik 8a3df8
+
Josef Bacik 8a3df8
+#define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
Josef Bacik 8a3df8
+				   struct btrfs_ioctl_vol_args)
Josef Bacik 8a3df8
 #endif