borisb / rpms / btrfs-progs

Forked from rpms/btrfs-progs 2 years ago
Clone
Josef Bacik 0b8f9c
diff --git a/Makefile b/Makefile
Josef Bacik 0b8f9c
index 8097b5a..525676e 100644
Josef Bacik 0b8f9c
--- a/Makefile
Josef Bacik 0b8f9c
+++ b/Makefile
Josef Bacik 0b8f9c
@@ -4,7 +4,7 @@ CFLAGS = -g -Werror -Os
Josef Bacik 0b8f9c
 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
Josef Bacik 0b8f9c
 	  root-tree.o dir-item.o file-item.o inode-item.o \
Josef Bacik 0b8f9c
 	  inode-map.o crc32c.o rbtree.o extent-cache.o extent_io.o \
Josef Bacik 0b8f9c
-	  volumes.o utils.o
Josef Bacik 0b8f9c
+	  volumes.o utils.o btrfs-list.o
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 #
Josef Bacik 0b8f9c
 CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \
Josef Bacik 0b8f9c
@@ -16,7 +16,9 @@ prefix ?= /usr/local
Josef Bacik 0b8f9c
 bindir = $(prefix)/bin
Josef Bacik 0b8f9c
 LIBS=-luuid
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
-progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck
Josef Bacik 0b8f9c
+progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck \
Josef Bacik 0b8f9c
+	btrfs \
Josef Bacik 0b8f9c
+	btrfs-map-logical
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 # make C=1 to enable sparse
Josef Bacik 0b8f9c
 ifdef C
Josef Bacik 0b8f9c
@@ -35,6 +37,10 @@ all: version $(progs) manpages
Josef Bacik 0b8f9c
 version:
Josef Bacik 0b8f9c
 	bash version.sh
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
+btrfs: $(objects) btrfs.o btrfs_cmds.o
Josef Bacik 0b8f9c
+	gcc $(CFLAGS) -o btrfs btrfs.o btrfs_cmds.o \
Josef Bacik 0b8f9c
+		$(objects) $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
 btrfsctl: $(objects) btrfsctl.o
Josef Bacik 0b8f9c
 	gcc $(CFLAGS) -o btrfsctl btrfsctl.o $(objects) $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
@@ -56,6 +62,9 @@ btrfs-debug-tree: $(objects) debug-tree.o
Josef Bacik 0b8f9c
 btrfstune: $(objects) btrfstune.o
Josef Bacik 0b8f9c
 	gcc $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
+btrfs-map-logical: $(objects) btrfs-map-logical.o
Josef Bacik 0b8f9c
+	gcc $(CFLAGS) -o btrfs-map-logical $(objects) btrfs-map-logical.o $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
 btrfs-image: $(objects) btrfs-image.o
Josef Bacik 0b8f9c
 	gcc $(CFLAGS) -o btrfs-image $(objects) btrfs-image.o -lpthread -lz $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
@@ -68,6 +77,9 @@ quick-test: $(objects) quick-test.o
Josef Bacik 0b8f9c
 convert: $(objects) convert.o
Josef Bacik 0b8f9c
 	gcc $(CFLAGS) -o btrfs-convert $(objects) convert.o -lext2fs $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
+ioctl-test: $(objects) ioctl-test.o
Josef Bacik 0b8f9c
+	gcc $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
 manpages:
Josef Bacik 0b8f9c
 	cd man; make
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
diff --git a/btrfs-defrag.c b/btrfs-defrag.c
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..8f1525a
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/btrfs-defrag.c
Josef Bacik 0b8f9c
@@ -0,0 +1,39 @@
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * Copyright (C) 2010 Oracle.  All rights reserved.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is free software; you can redistribute it and/or
Josef Bacik 0b8f9c
+ * modify it under the terms of the GNU General Public
Josef Bacik 0b8f9c
+ * License v2 as published by the Free Software Foundation.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is distributed in the hope that it will be useful,
Josef Bacik 0b8f9c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Josef Bacik 0b8f9c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Josef Bacik 0b8f9c
+ * General Public License for more details.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * You should have received a copy of the GNU General Public
Josef Bacik 0b8f9c
+ * License along with this program; if not, write to the
Josef Bacik 0b8f9c
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Josef Bacik 0b8f9c
+ * Boston, MA 021110-1307, USA.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#ifndef __CHECKER__
Josef Bacik 0b8f9c
+#include <sys/ioctl.h>
Josef Bacik 0b8f9c
+#include <sys/mount.h>
Josef Bacik 0b8f9c
+#include "ioctl.h"
Josef Bacik 0b8f9c
+#endif
Josef Bacik 0b8f9c
+#include <stdio.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
+#include <sys/types.h>
Josef Bacik 0b8f9c
+#include <sys/stat.h>
Josef Bacik 0b8f9c
+#include <fcntl.h>
Josef Bacik 0b8f9c
+#include <ctype.h>
Josef Bacik 0b8f9c
+#include <unistd.h>
Josef Bacik 0b8f9c
+#include <dirent.h>
Josef Bacik 0b8f9c
+#include <libgen.h>
Josef Bacik 0b8f9c
+#include <getopt.h>
Josef Bacik 0b8f9c
+#include "kerncompat.h"
Josef Bacik 0b8f9c
+#include "ctree.h"
Josef Bacik 0b8f9c
+#include "transaction.h"
Josef Bacik 0b8f9c
+#include "utils.h"
Josef Bacik 0b8f9c
+#include "version.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
diff --git a/btrfs-list.c b/btrfs-list.c
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..7741705
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/btrfs-list.c
Josef Bacik 0b8f9c
@@ -0,0 +1,825 @@
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * Copyright (C) 2010 Oracle.  All rights reserved.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is free software; you can redistribute it and/or
Josef Bacik 0b8f9c
+ * modify it under the terms of the GNU General Public
Josef Bacik 0b8f9c
+ * License v2 as published by the Free Software Foundation.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is distributed in the hope that it will be useful,
Josef Bacik 0b8f9c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Josef Bacik 0b8f9c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Josef Bacik 0b8f9c
+ * General Public License for more details.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * You should have received a copy of the GNU General Public
Josef Bacik 0b8f9c
+ * License along with this program; if not, write to the
Josef Bacik 0b8f9c
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Josef Bacik 0b8f9c
+ * Boston, MA 021110-1307, USA.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#ifndef __CHECKER__
Josef Bacik 0b8f9c
+#include <sys/ioctl.h>
Josef Bacik 0b8f9c
+#include <sys/mount.h>
Josef Bacik 0b8f9c
+#include "ioctl.h"
Josef Bacik 0b8f9c
+#endif
Josef Bacik 0b8f9c
+#include <stdio.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
+#include <sys/types.h>
Josef Bacik 0b8f9c
+#include <sys/stat.h>
Josef Bacik 0b8f9c
+#include <fcntl.h>
Josef Bacik 0b8f9c
+#include <unistd.h>
Josef Bacik 0b8f9c
+#include <dirent.h>
Josef Bacik 0b8f9c
+#include <libgen.h>
Josef Bacik 0b8f9c
+#include "kerncompat.h"
Josef Bacik 0b8f9c
+#include "ctree.h"
Josef Bacik 0b8f9c
+#include "transaction.h"
Josef Bacik 0b8f9c
+#include "utils.h"
Josef Bacik 0b8f9c
+#include "version.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/* we store all the roots we find in an rbtree so that we can
Josef Bacik 0b8f9c
+ * search for them later.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+struct root_lookup {
Josef Bacik 0b8f9c
+	struct rb_root root;
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * one of these for each root we find.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+struct root_info {
Josef Bacik 0b8f9c
+	struct rb_node rb_node;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* this root's id */
Josef Bacik 0b8f9c
+	u64 root_id;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* the id of the root that references this one */
Josef Bacik 0b8f9c
+	u64 ref_tree;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* the dir id we're in from ref_tree */
Josef Bacik 0b8f9c
+	u64 dir_id;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* path from the subvol we live in to this root, including the
Josef Bacik 0b8f9c
+	 * root's name.  This is null until we do the extra lookup ioctl.
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	char *path;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* the name of this root in the directory it lives in */
Josef Bacik 0b8f9c
+	char name[];
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static void root_lookup_init(struct root_lookup *tree)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	tree->root.rb_node = NULL;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	if (entry->root_id > root_id)
Josef Bacik 0b8f9c
+		return 1;
Josef Bacik 0b8f9c
+	if (entry->root_id < root_id)
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+	if (entry->ref_tree > ref_tree)
Josef Bacik 0b8f9c
+		return 1;
Josef Bacik 0b8f9c
+	if (entry->ref_tree < ref_tree)
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * insert a new root into the tree.  returns the existing root entry
Josef Bacik 0b8f9c
+ * if one is already there.  Both root_id and ref_tree are used
Josef Bacik 0b8f9c
+ * as the key
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static struct rb_node *tree_insert(struct rb_root *root, u64 root_id,
Josef Bacik 0b8f9c
+				   u64 ref_tree, struct rb_node *node)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct rb_node ** p = &root->rb_node;
Josef Bacik 0b8f9c
+	struct rb_node * parent = NULL;
Josef Bacik 0b8f9c
+	struct root_info *entry;
Josef Bacik 0b8f9c
+	int comp;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	while(*p) {
Josef Bacik 0b8f9c
+		parent = *p;
Josef Bacik 0b8f9c
+		entry = rb_entry(parent, struct root_info, rb_node);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		comp = comp_entry(entry, root_id, ref_tree);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if (comp < 0)
Josef Bacik 0b8f9c
+			p = &(*p)->rb_left;
Josef Bacik 0b8f9c
+		else if (comp > 0)
Josef Bacik 0b8f9c
+			p = &(*p)->rb_right;
Josef Bacik 0b8f9c
+		else
Josef Bacik 0b8f9c
+			return parent;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	entry = rb_entry(parent, struct root_info, rb_node);
Josef Bacik 0b8f9c
+	rb_link_node(node, parent, p);
Josef Bacik 0b8f9c
+	rb_insert_color(node, root);
Josef Bacik 0b8f9c
+	return NULL;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * find a given root id in the tree.  We return the smallest one,
Josef Bacik 0b8f9c
+ * rb_next can be used to move forward looking for more if required
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static struct root_info *tree_search(struct rb_root *root, u64 root_id)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct rb_node * n = root->rb_node;
Josef Bacik 0b8f9c
+	struct root_info *entry;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	while(n) {
Josef Bacik 0b8f9c
+		entry = rb_entry(n, struct root_info, rb_node);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if (entry->root_id < root_id)
Josef Bacik 0b8f9c
+			n = n->rb_left;
Josef Bacik 0b8f9c
+		else if (entry->root_id > root_id)
Josef Bacik 0b8f9c
+			n = n->rb_right;
Josef Bacik 0b8f9c
+		else {
Josef Bacik 0b8f9c
+			struct root_info *prev;
Josef Bacik 0b8f9c
+			struct rb_node *prev_n;
Josef Bacik 0b8f9c
+			while (1) {
Josef Bacik 0b8f9c
+				prev_n = rb_prev(n);
Josef Bacik 0b8f9c
+				if (!prev_n)
Josef Bacik 0b8f9c
+					break;
Josef Bacik 0b8f9c
+				prev = rb_entry(prev_n, struct root_info,
Josef Bacik 0b8f9c
+						      rb_node);
Josef Bacik 0b8f9c
+				if (prev->root_id != root_id)
Josef Bacik 0b8f9c
+					break;
Josef Bacik 0b8f9c
+				entry = prev;
Josef Bacik 0b8f9c
+				n = prev_n;
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+			return entry;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return NULL;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * this allocates a new root in the lookup tree.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * root_id should be the object id of the root
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * ref_tree is the objectid of the referring root.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * dir_id is the directory in ref_tree where this root_id can be found.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * name is the name of root_id in that directory
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * name_len is the length of name
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static int add_root(struct root_lookup *root_lookup,
Josef Bacik 0b8f9c
+		    u64 root_id, u64 ref_tree, u64 dir_id, char *name,
Josef Bacik 0b8f9c
+		    int name_len)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct root_info *ri;
Josef Bacik 0b8f9c
+	struct rb_node *ret;
Josef Bacik 0b8f9c
+	ri = malloc(sizeof(*ri) + name_len + 1);
Josef Bacik 0b8f9c
+	if (!ri) {
Josef Bacik 0b8f9c
+		printf("memory allocation failed\n");
Josef Bacik 0b8f9c
+		exit(1);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	memset(ri, 0, sizeof(*ri) + name_len + 1);
Josef Bacik 0b8f9c
+	ri->path = NULL;
Josef Bacik 0b8f9c
+	ri->dir_id = dir_id;
Josef Bacik 0b8f9c
+	ri->root_id = root_id;
Josef Bacik 0b8f9c
+	ri->ref_tree = ref_tree;
Josef Bacik 0b8f9c
+	strncpy(ri->name, name, name_len);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = tree_insert(&root_lookup->root, root_id, ref_tree, &ri->rb_node);
Josef Bacik 0b8f9c
+	if (ret) {
Josef Bacik 0b8f9c
+		printf("failed to insert tree %llu\n", (unsigned long long)root_id);
Josef Bacik 0b8f9c
+		exit(1);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * for a given root_info, search through the root_lookup tree to construct
Josef Bacik 0b8f9c
+ * the full path name to it.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This can't be called until all the root_info->path fields are filled
Josef Bacik 0b8f9c
+ * in by lookup_ino_path
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static int resolve_root(struct root_lookup *rl, struct root_info *ri)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	u64 top_id;
Josef Bacik 0b8f9c
+	char *full_path = NULL;
Josef Bacik 0b8f9c
+	int len = 0;
Josef Bacik 0b8f9c
+	struct root_info *found;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * we go backwards from the root_info object and add pathnames
Josef Bacik 0b8f9c
+	 * from parent directories as we go.
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	found = ri;
Josef Bacik 0b8f9c
+	while (1) {
Josef Bacik 0b8f9c
+		char *tmp;
Josef Bacik 0b8f9c
+		u64 next;
Josef Bacik 0b8f9c
+		int add_len = strlen(found->path);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/* room for / and for null */
Josef Bacik 0b8f9c
+		tmp = malloc(add_len + 2 + len);
Josef Bacik 0b8f9c
+		if (full_path) {
Josef Bacik 0b8f9c
+			memcpy(tmp + add_len + 1, full_path, len);
Josef Bacik 0b8f9c
+			tmp[add_len] = '/';
Josef Bacik 0b8f9c
+			memcpy(tmp, found->path, add_len);
Josef Bacik 0b8f9c
+			tmp [add_len + len + 1] = '\0';
Josef Bacik 0b8f9c
+			free(full_path);
Josef Bacik 0b8f9c
+			full_path = tmp;
Josef Bacik 0b8f9c
+			len += add_len + 1;
Josef Bacik 0b8f9c
+		} else {
Josef Bacik 0b8f9c
+			full_path = strdup(found->path);
Josef Bacik 0b8f9c
+			len = add_len;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		next = found->ref_tree;
Josef Bacik 0b8f9c
+		/* if the ref_tree refers to ourselves, we're at the top */
Josef Bacik 0b8f9c
+		if (next == found->root_id) {
Josef Bacik 0b8f9c
+			top_id = next;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/*
Josef Bacik 0b8f9c
+		 * if the ref_tree wasn't in our tree of roots, we're
Josef Bacik 0b8f9c
+		 * at the top
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		found = tree_search(&rl->root, next);
Josef Bacik 0b8f9c
+		if (!found) {
Josef Bacik 0b8f9c
+			top_id = next;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	printf("ID %llu top level %llu path %s\n", ri->root_id, top_id,
Josef Bacik 0b8f9c
+	       full_path);
Josef Bacik 0b8f9c
+	free(full_path);
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * for a single root_info, ask the kernel to give us a path name
Josef Bacik 0b8f9c
+ * inside it's ref_root for the dir_id where it lives.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This fills in root_info->path with the path to the directory and and
Josef Bacik 0b8f9c
+ * appends this root's name.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static int lookup_ino_path(int fd, struct root_info *ri)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_ino_lookup_args args;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (ri->path)
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+	args.treeid = ri->ref_tree;
Josef Bacik 0b8f9c
+	args.objectid = ri->dir_id;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
Josef Bacik 0b8f9c
+	if (ret) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: Failed to lookup path for root %llu\n",
Josef Bacik 0b8f9c
+			(unsigned long long)ri->ref_tree);
Josef Bacik 0b8f9c
+		return ret;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (args.name[0]) {
Josef Bacik 0b8f9c
+		/*
Josef Bacik 0b8f9c
+		 * we're in a subdirectory of ref_tree, the kernel ioctl
Josef Bacik 0b8f9c
+		 * puts a / in there for us
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
Josef Bacik 0b8f9c
+		if (!ri->path) {
Josef Bacik 0b8f9c
+			perror("malloc failed");
Josef Bacik 0b8f9c
+			exit(1);
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		strcpy(ri->path, args.name);
Josef Bacik 0b8f9c
+		strcat(ri->path, ri->name);
Josef Bacik 0b8f9c
+	} else {
Josef Bacik 0b8f9c
+		/* we're at the root of ref_tree */
Josef Bacik 0b8f9c
+		ri->path = strdup(ri->name);
Josef Bacik 0b8f9c
+		if (!ri->path) {
Josef Bacik 0b8f9c
+			perror("strdup failed");
Josef Bacik 0b8f9c
+			exit(1);
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/* finding the generation for a given path is a two step process.
Josef Bacik 0b8f9c
+ * First we use the inode loookup routine to find out the root id
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * Then we use the tree search ioctl to scan all the root items for a
Josef Bacik 0b8f9c
+ * given root id and spit out the latest generation we can find
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static u64 find_root_gen(int fd)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_ino_lookup_args ino_args;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_args args;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_key *sk = &args.key;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_header *sh;
Josef Bacik 0b8f9c
+	unsigned long off = 0;
Josef Bacik 0b8f9c
+	u64 max_found = 0;
Josef Bacik 0b8f9c
+	int i;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&ino_args, 0, sizeof(ino_args));
Josef Bacik 0b8f9c
+	ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* this ioctl fills in ino_args->treeid */
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
Josef Bacik 0b8f9c
+	if (ret) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
Josef Bacik 0b8f9c
+			(unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sk->tree_id = 1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * there may be more than one ROOT_ITEM key if there are
Josef Bacik 0b8f9c
+	 * snapshots pending deletion, we have to loop through
Josef Bacik 0b8f9c
+	 * them.
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	sk->min_objectid = ino_args.treeid;
Josef Bacik 0b8f9c
+	sk->max_objectid = ino_args.treeid;
Josef Bacik 0b8f9c
+	sk->max_type = BTRFS_ROOT_ITEM_KEY;
Josef Bacik 0b8f9c
+	sk->min_type = BTRFS_ROOT_ITEM_KEY;
Josef Bacik 0b8f9c
+	sk->max_offset = (u64)-1;
Josef Bacik 0b8f9c
+	sk->max_transid = (u64)-1;
Josef Bacik 0b8f9c
+	sk->nr_items = 4096;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	while (1) {
Josef Bacik 0b8f9c
+		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
Josef Bacik 0b8f9c
+		if (ret < 0) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: can't perform the search\n");
Josef Bacik 0b8f9c
+			return 0;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		/* the ioctl returns the number of item it found in nr_items */
Josef Bacik 0b8f9c
+		if (sk->nr_items == 0)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		off = 0;
Josef Bacik 0b8f9c
+		for (i = 0; i < sk->nr_items; i++) {
Josef Bacik 0b8f9c
+			struct btrfs_root_item *item;
Josef Bacik 0b8f9c
+			sh = (struct btrfs_ioctl_search_header *)(args.buf +
Josef Bacik 0b8f9c
+								  off);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			off += sizeof(*sh);
Josef Bacik 0b8f9c
+			item = (struct btrfs_root_item *)(args.buf + off);
Josef Bacik 0b8f9c
+			off += sh->len;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			sk->min_objectid = sh->objectid;
Josef Bacik 0b8f9c
+			sk->min_type = sh->type;
Josef Bacik 0b8f9c
+			sk->min_offset = sh->offset;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			if (sh->objectid > ino_args.treeid)
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			if (sh->objectid == ino_args.treeid &&
Josef Bacik 0b8f9c
+			    sh->type == BTRFS_ROOT_ITEM_KEY) {
Josef Bacik 0b8f9c
+				max_found = max(max_found,
Josef Bacik 0b8f9c
+						btrfs_root_generation(item));
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		if (sk->min_offset < (u64)-1)
Josef Bacik 0b8f9c
+			sk->min_offset++;
Josef Bacik 0b8f9c
+		else
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return max_found;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/* pass in a directory id and this will return
Josef Bacik 0b8f9c
+ * the full path of the parent directory inside its
Josef Bacik 0b8f9c
+ * subvolume root.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * It may return NULL if it is in the root, or an ERR_PTR if things
Josef Bacik 0b8f9c
+ * go badly.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static char *__ino_resolve(int fd, u64 dirid)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_ino_lookup_args args;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	char *full;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+	args.objectid = dirid;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
Josef Bacik 0b8f9c
+	if (ret) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
Josef Bacik 0b8f9c
+			(unsigned long long)dirid);
Josef Bacik 0b8f9c
+		return ERR_PTR(ret);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (args.name[0]) {
Josef Bacik 0b8f9c
+		/*
Josef Bacik 0b8f9c
+		 * we're in a subdirectory of ref_tree, the kernel ioctl
Josef Bacik 0b8f9c
+		 * puts a / in there for us
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		full = strdup(args.name);
Josef Bacik 0b8f9c
+		if (!full) {
Josef Bacik 0b8f9c
+			perror("malloc failed");
Josef Bacik 0b8f9c
+			return ERR_PTR(-ENOMEM);
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	} else {
Josef Bacik 0b8f9c
+		/* we're at the root of ref_tree */
Josef Bacik 0b8f9c
+		full = NULL;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return full;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * simple string builder, returning a new string with both
Josef Bacik 0b8f9c
+ * dirid and name
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+char *build_name(char *dirid, char *name)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	char *full;
Josef Bacik 0b8f9c
+	if (!dirid)
Josef Bacik 0b8f9c
+		return strdup(name);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	full = malloc(strlen(dirid) + strlen(name) + 1);
Josef Bacik 0b8f9c
+	if (!full)
Josef Bacik 0b8f9c
+		return NULL;
Josef Bacik 0b8f9c
+	strcpy(full, dirid);
Josef Bacik 0b8f9c
+	strcat(full, name);
Josef Bacik 0b8f9c
+	return full;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * given an inode number, this returns the full path name inside the subvolume
Josef Bacik 0b8f9c
+ * to that file/directory.  cache_dirid and cache_name are used to
Josef Bacik 0b8f9c
+ * cache the results so we can avoid tree searches if a later call goes
Josef Bacik 0b8f9c
+ * to the same directory or file name
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	u64 dirid;
Josef Bacik 0b8f9c
+	char *dirname;
Josef Bacik 0b8f9c
+	char *name;
Josef Bacik 0b8f9c
+	char *full;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_args args;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_key *sk = &args.key;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_header *sh;
Josef Bacik 0b8f9c
+	unsigned long off = 0;
Josef Bacik 0b8f9c
+	int namelen;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sk->tree_id = 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * step one, we search for the inode back ref.  We just use the first
Josef Bacik 0b8f9c
+	 * one
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	sk->min_objectid = ino;
Josef Bacik 0b8f9c
+	sk->max_objectid = ino;
Josef Bacik 0b8f9c
+	sk->max_type = BTRFS_INODE_REF_KEY;
Josef Bacik 0b8f9c
+	sk->max_offset = (u64)-1;
Josef Bacik 0b8f9c
+	sk->min_type = BTRFS_INODE_REF_KEY;
Josef Bacik 0b8f9c
+	sk->max_transid = (u64)-1;
Josef Bacik 0b8f9c
+	sk->nr_items = 1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
Josef Bacik 0b8f9c
+	if (ret < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't perform the search\n");
Josef Bacik 0b8f9c
+		return NULL;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	/* the ioctl returns the number of item it found in nr_items */
Josef Bacik 0b8f9c
+	if (sk->nr_items == 0)
Josef Bacik 0b8f9c
+		return NULL;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	off = 0;
Josef Bacik 0b8f9c
+	sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (sh->type == BTRFS_INODE_REF_KEY) {
Josef Bacik 0b8f9c
+		struct btrfs_inode_ref *ref;
Josef Bacik 0b8f9c
+		dirid = sh->offset;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		ref = (struct btrfs_inode_ref *)(sh + 1);
Josef Bacik 0b8f9c
+		namelen = btrfs_stack_inode_ref_name_len(ref);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		name = (char *)(ref + 1);
Josef Bacik 0b8f9c
+		name = strndup(name, namelen);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/* use our cached value */
Josef Bacik 0b8f9c
+		if (dirid == *cache_dirid && *cache_name) {
Josef Bacik 0b8f9c
+			dirname = *cache_name;
Josef Bacik 0b8f9c
+			goto build;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	} else {
Josef Bacik 0b8f9c
+		return NULL;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * the inode backref gives us the file name and the parent directory id.
Josef Bacik 0b8f9c
+	 * From here we use __ino_resolve to get the path to the parent
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	dirname = __ino_resolve(fd, dirid);
Josef Bacik 0b8f9c
+build:
Josef Bacik 0b8f9c
+	full = build_name(dirname, name);
Josef Bacik 0b8f9c
+	if (*cache_name && dirname != *cache_name)
Josef Bacik 0b8f9c
+		free(*cache_name);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	*cache_name = dirname;
Josef Bacik 0b8f9c
+	*cache_dirid = dirid;
Josef Bacik 0b8f9c
+	free(name);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return full;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int list_subvols(int fd)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct root_lookup root_lookup;
Josef Bacik 0b8f9c
+	struct rb_node *n;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_args args;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_key *sk = &args.key;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_header *sh;
Josef Bacik 0b8f9c
+	struct btrfs_root_ref *ref;
Josef Bacik 0b8f9c
+	unsigned long off = 0;
Josef Bacik 0b8f9c
+	int name_len;
Josef Bacik 0b8f9c
+	char *name;
Josef Bacik 0b8f9c
+	u64 dir_id;
Josef Bacik 0b8f9c
+	int i;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	root_lookup_init(&root_lookup);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* search in the tree of tree roots */
Josef Bacik 0b8f9c
+	sk->tree_id = 1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * set the min and max to backref keys.  The search will
Josef Bacik 0b8f9c
+	 * only send back this type of key now.
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	sk->max_type = BTRFS_ROOT_BACKREF_KEY;
Josef Bacik 0b8f9c
+	sk->min_type = BTRFS_ROOT_BACKREF_KEY;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * set all the other params to the max, we'll take any objectid
Josef Bacik 0b8f9c
+	 * and any trans
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	sk->max_objectid = (u64)-1;
Josef Bacik 0b8f9c
+	sk->max_offset = (u64)-1;
Josef Bacik 0b8f9c
+	sk->max_transid = (u64)-1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* just a big number, doesn't matter much */
Josef Bacik 0b8f9c
+	sk->nr_items = 4096;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	while(1) {
Josef Bacik 0b8f9c
+		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
Josef Bacik 0b8f9c
+		if (ret < 0) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: can't perform the search\n");
Josef Bacik 0b8f9c
+			return ret;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		/* the ioctl returns the number of item it found in nr_items */
Josef Bacik 0b8f9c
+		if (sk->nr_items == 0)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		off = 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/*
Josef Bacik 0b8f9c
+		 * for each item, pull the key out of the header and then
Josef Bacik 0b8f9c
+		 * read the root_ref item it contains
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		for (i = 0; i < sk->nr_items; i++) {
Josef Bacik 0b8f9c
+			sh = (struct btrfs_ioctl_search_header *)(args.buf +
Josef Bacik 0b8f9c
+								  off);
Josef Bacik 0b8f9c
+			off += sizeof(*sh);
Josef Bacik 0b8f9c
+			if (sh->type == BTRFS_ROOT_BACKREF_KEY) {
Josef Bacik 0b8f9c
+				ref = (struct btrfs_root_ref *)(args.buf + off);
Josef Bacik 0b8f9c
+				name_len = btrfs_stack_root_ref_name_len(ref);
Josef Bacik 0b8f9c
+				name = (char *)(ref + 1);
Josef Bacik 0b8f9c
+				dir_id = btrfs_stack_root_ref_dirid(ref);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+				add_root(&root_lookup, sh->objectid, sh->offset,
Josef Bacik 0b8f9c
+					 dir_id, name, name_len);
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			off += sh->len;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			/*
Josef Bacik 0b8f9c
+			 * record the mins in sk so we can make sure the
Josef Bacik 0b8f9c
+			 * next search doesn't repeat this root
Josef Bacik 0b8f9c
+			 */
Josef Bacik 0b8f9c
+			sk->min_objectid = sh->objectid;
Josef Bacik 0b8f9c
+			sk->min_type = sh->type;
Josef Bacik 0b8f9c
+			sk->min_offset = sh->offset;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		sk->nr_items = 4096;
Josef Bacik 0b8f9c
+		/* this iteration is done, step forward one root for the next
Josef Bacik 0b8f9c
+		 * ioctl
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		if (sk->min_objectid < (u64)-1) {
Josef Bacik 0b8f9c
+			sk->min_objectid++;
Josef Bacik 0b8f9c
+			sk->min_type = BTRFS_ROOT_BACKREF_KEY;
Josef Bacik 0b8f9c
+			sk->min_offset = 0;
Josef Bacik 0b8f9c
+		} else
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * now we have an rbtree full of root_info objects, but we need to fill
Josef Bacik 0b8f9c
+	 * in their path names within the subvol that is referencing each one.
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	n = rb_first(&root_lookup.root);
Josef Bacik 0b8f9c
+	while (n) {
Josef Bacik 0b8f9c
+		struct root_info *entry;
Josef Bacik 0b8f9c
+		int ret;
Josef Bacik 0b8f9c
+		entry = rb_entry(n, struct root_info, rb_node);
Josef Bacik 0b8f9c
+		ret = lookup_ino_path(fd, entry);
Josef Bacik 0b8f9c
+		if(ret < 0)
Josef Bacik 0b8f9c
+			return ret;
Josef Bacik 0b8f9c
+		n = rb_next(n);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* now that we have all the subvol-relative paths filled in,
Josef Bacik 0b8f9c
+	 * we have to string the subvols together so that we can get
Josef Bacik 0b8f9c
+	 * a path all the way back to the FS root
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	n = rb_last(&root_lookup.root);
Josef Bacik 0b8f9c
+	while (n) {
Josef Bacik 0b8f9c
+		struct root_info *entry;
Josef Bacik 0b8f9c
+		entry = rb_entry(n, struct root_info, rb_node);
Josef Bacik 0b8f9c
+		resolve_root(&root_lookup, entry);
Josef Bacik 0b8f9c
+		n = rb_prev(n);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return ret;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
Josef Bacik 0b8f9c
+			    struct btrfs_file_extent_item *item,
Josef Bacik 0b8f9c
+			    u64 found_gen, u64 *cache_dirid,
Josef Bacik 0b8f9c
+			    char **cache_dir_name, u64 *cache_ino,
Josef Bacik 0b8f9c
+			    char **cache_full_name)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	u64 len;
Josef Bacik 0b8f9c
+	u64 disk_start;
Josef Bacik 0b8f9c
+	u64 disk_offset;
Josef Bacik 0b8f9c
+	u8 type;
Josef Bacik 0b8f9c
+	int compressed = 0;
Josef Bacik 0b8f9c
+	int flags = 0;
Josef Bacik 0b8f9c
+	char *name = NULL;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (sh->objectid == *cache_ino) {
Josef Bacik 0b8f9c
+		name = *cache_full_name;
Josef Bacik 0b8f9c
+	} else if (*cache_full_name) {
Josef Bacik 0b8f9c
+		free(*cache_full_name);
Josef Bacik 0b8f9c
+		*cache_full_name = NULL;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (!name) {
Josef Bacik 0b8f9c
+		name = ino_resolve(fd, sh->objectid, cache_dirid,
Josef Bacik 0b8f9c
+				   cache_dir_name);
Josef Bacik 0b8f9c
+		*cache_full_name = name;
Josef Bacik 0b8f9c
+		*cache_ino = sh->objectid;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (!name)
Josef Bacik 0b8f9c
+		return -EIO;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	type = btrfs_stack_file_extent_type(item);
Josef Bacik 0b8f9c
+	compressed = btrfs_stack_file_extent_compression(item);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik 0b8f9c
+	    type == BTRFS_FILE_EXTENT_PREALLOC) {
Josef Bacik 0b8f9c
+		disk_start = btrfs_stack_file_extent_disk_bytenr(item);
Josef Bacik 0b8f9c
+		disk_offset = btrfs_stack_file_extent_offset(item);
Josef Bacik 0b8f9c
+		len = btrfs_stack_file_extent_num_bytes(item);
Josef Bacik 0b8f9c
+	} else if (type == BTRFS_FILE_EXTENT_INLINE) {
Josef Bacik 0b8f9c
+		disk_start = 0;
Josef Bacik 0b8f9c
+		disk_offset = 0;
Josef Bacik 0b8f9c
+		len = btrfs_stack_file_extent_ram_bytes(item);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	printf("inode %llu file offset %llu len %llu disk start %llu "
Josef Bacik 0b8f9c
+	       "offset %llu gen %llu flags ",
Josef Bacik 0b8f9c
+	       (unsigned long long)sh->objectid,
Josef Bacik 0b8f9c
+	       (unsigned long long)sh->offset,
Josef Bacik 0b8f9c
+	       (unsigned long long)len,
Josef Bacik 0b8f9c
+	       (unsigned long long)disk_start,
Josef Bacik 0b8f9c
+	       (unsigned long long)disk_offset,
Josef Bacik 0b8f9c
+	       (unsigned long long)found_gen);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (compressed) {
Josef Bacik 0b8f9c
+		printf("COMPRESS");
Josef Bacik 0b8f9c
+		flags++;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (type == BTRFS_FILE_EXTENT_PREALLOC) {
Josef Bacik 0b8f9c
+		printf("%sPREALLOC", flags ? "|" : "");
Josef Bacik 0b8f9c
+		flags++;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (type == BTRFS_FILE_EXTENT_INLINE) {
Josef Bacik 0b8f9c
+		printf("%sINLINE", flags ? "|" : "");
Josef Bacik 0b8f9c
+		flags++;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (!flags)
Josef Bacik 0b8f9c
+		printf("NONE");
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf(" %s\n", name);
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_args args;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_key *sk = &args.key;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_header *sh;
Josef Bacik 0b8f9c
+	struct btrfs_file_extent_item *item;
Josef Bacik 0b8f9c
+	unsigned long off = 0;
Josef Bacik 0b8f9c
+	u64 found_gen;
Josef Bacik 0b8f9c
+	u64 max_found = 0;
Josef Bacik 0b8f9c
+	int i;
Josef Bacik 0b8f9c
+	u64 cache_dirid = 0;
Josef Bacik 0b8f9c
+	u64 cache_ino = 0;
Josef Bacik 0b8f9c
+	char *cache_dir_name = NULL;
Josef Bacik 0b8f9c
+	char *cache_full_name = NULL;
Josef Bacik 0b8f9c
+	struct btrfs_file_extent_item backup;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&backup, 0, sizeof(backup));
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sk->tree_id = root_id;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * set all the other params to the max, we'll take any objectid
Josef Bacik 0b8f9c
+	 * and any trans
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	sk->max_objectid = (u64)-1;
Josef Bacik 0b8f9c
+	sk->max_offset = (u64)-1;
Josef Bacik 0b8f9c
+	sk->max_transid = (u64)-1;
Josef Bacik 0b8f9c
+	sk->max_type = BTRFS_EXTENT_DATA_KEY;
Josef Bacik 0b8f9c
+	sk->min_transid = oldest_gen;
Josef Bacik 0b8f9c
+	/* just a big number, doesn't matter much */
Josef Bacik 0b8f9c
+	sk->nr_items = 4096;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	max_found = find_root_gen(fd);
Josef Bacik 0b8f9c
+	while(1) {
Josef Bacik 0b8f9c
+		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
Josef Bacik 0b8f9c
+		if (ret < 0) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: can't perform the search\n");
Josef Bacik 0b8f9c
+			return ret;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		/* the ioctl returns the number of item it found in nr_items */
Josef Bacik 0b8f9c
+		if (sk->nr_items == 0)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		off = 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/*
Josef Bacik 0b8f9c
+		 * for each item, pull the key out of the header and then
Josef Bacik 0b8f9c
+		 * read the root_ref item it contains
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		for (i = 0; i < sk->nr_items; i++) {
Josef Bacik 0b8f9c
+			sh = (struct btrfs_ioctl_search_header *)(args.buf +
Josef Bacik 0b8f9c
+								  off);
Josef Bacik 0b8f9c
+			off += sizeof(*sh);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			/*
Josef Bacik 0b8f9c
+			 * just in case the item was too big, pass something other
Josef Bacik 0b8f9c
+			 * than garbage
Josef Bacik 0b8f9c
+			 */
Josef Bacik 0b8f9c
+			if (sh->len == 0)
Josef Bacik 0b8f9c
+				item = &backu;;
Josef Bacik 0b8f9c
+			else
Josef Bacik 0b8f9c
+				item = (struct btrfs_file_extent_item *)(args.buf +
Josef Bacik 0b8f9c
+								 off);
Josef Bacik 0b8f9c
+			found_gen = btrfs_stack_file_extent_generation(item);
Josef Bacik 0b8f9c
+			if (sh->type == BTRFS_EXTENT_DATA_KEY &&
Josef Bacik 0b8f9c
+			    found_gen >= oldest_gen) {
Josef Bacik 0b8f9c
+				print_one_extent(fd, sh, item, found_gen,
Josef Bacik 0b8f9c
+						 &cache_dirid, &cache_dir_name,
Josef Bacik 0b8f9c
+						 &cache_ino, &cache_full_name);
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+			off += sh->len;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			/*
Josef Bacik 0b8f9c
+			 * record the mins in sk so we can make sure the
Josef Bacik 0b8f9c
+			 * next search doesn't repeat this root
Josef Bacik 0b8f9c
+			 */
Josef Bacik 0b8f9c
+			sk->min_objectid = sh->objectid;
Josef Bacik 0b8f9c
+			sk->min_offset = sh->offset;
Josef Bacik 0b8f9c
+			sk->min_type = sh->type;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		sk->nr_items = 4096;
Josef Bacik 0b8f9c
+		if (sk->min_offset < (u64)-1)
Josef Bacik 0b8f9c
+			sk->min_offset++;
Josef Bacik 0b8f9c
+		else if (sk->min_objectid < (u64)-1) {
Josef Bacik 0b8f9c
+			sk->min_objectid++;
Josef Bacik 0b8f9c
+			sk->min_offset = 0;
Josef Bacik 0b8f9c
+			sk->min_type = 0;
Josef Bacik 0b8f9c
+		} else
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	free(cache_dir_name);
Josef Bacik 0b8f9c
+	free(cache_full_name);
Josef Bacik 0b8f9c
+	printf("transid marker was %llu\n", (unsigned long long)max_found);
Josef Bacik 0b8f9c
+	return ret;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..a109c6a
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/btrfs-map-logical.c
Josef Bacik 0b8f9c
@@ -0,0 +1,221 @@
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * Copyright (C) 2009 Oracle.  All rights reserved.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is free software; you can redistribute it and/or
Josef Bacik 0b8f9c
+ * modify it under the terms of the GNU General Public
Josef Bacik 0b8f9c
+ * License v2 as published by the Free Software Foundation.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is distributed in the hope that it will be useful,
Josef Bacik 0b8f9c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Josef Bacik 0b8f9c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Josef Bacik 0b8f9c
+ * General Public License for more details.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * You should have received a copy of the GNU General Public
Josef Bacik 0b8f9c
+ * License along with this program; if not, write to the
Josef Bacik 0b8f9c
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Josef Bacik 0b8f9c
+ * Boston, MA 021110-1307, USA.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#define _XOPEN_SOURCE 500
Josef Bacik 0b8f9c
+#define _GNU_SOURCE 1
Josef Bacik 0b8f9c
+#include <stdio.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
+#include <fcntl.h>
Josef Bacik 0b8f9c
+#include <unistd.h>
Josef Bacik 0b8f9c
+#include <getopt.h>
Josef Bacik 0b8f9c
+#include "kerncompat.h"
Josef Bacik 0b8f9c
+#include "ctree.h"
Josef Bacik 0b8f9c
+#include "volumes.h"
Josef Bacik 0b8f9c
+#include "disk-io.h"
Josef Bacik 0b8f9c
+#include "print-tree.h"
Josef Bacik 0b8f9c
+#include "transaction.h"
Josef Bacik 0b8f9c
+#include "list.h"
Josef Bacik 0b8f9c
+#include "version.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/* we write the mirror info to stdout unless they are dumping the data
Josef Bacik 0b8f9c
+ * to stdout
Josef Bacik 0b8f9c
+ * */
Josef Bacik 0b8f9c
+static FILE *info_file;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
Josef Bacik 0b8f9c
+				     u32 blocksize, int copy)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	int dev_nr;
Josef Bacik 0b8f9c
+	struct extent_buffer *eb;
Josef Bacik 0b8f9c
+	u64 length;
Josef Bacik 0b8f9c
+	struct btrfs_multi_bio *multi = NULL;
Josef Bacik 0b8f9c
+	struct btrfs_device *device;
Josef Bacik 0b8f9c
+	int num_copies;
Josef Bacik 0b8f9c
+	int mirror_num = 1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
Josef Bacik 0b8f9c
+	if (!eb)
Josef Bacik 0b8f9c
+		return NULL;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	dev_nr = 0;
Josef Bacik 0b8f9c
+	length = blocksize;
Josef Bacik 0b8f9c
+	while (1) {
Josef Bacik 0b8f9c
+		ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
Josef Bacik 0b8f9c
+				      eb->start, &length, &multi, mirror_num);
Josef Bacik 0b8f9c
+		BUG_ON(ret);
Josef Bacik 0b8f9c
+		device = multi->stripes[0].dev;
Josef Bacik 0b8f9c
+		eb->fd = device->fd;
Josef Bacik 0b8f9c
+		device->total_ios++;
Josef Bacik 0b8f9c
+		eb->dev_bytenr = multi->stripes[0].physical;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		fprintf(info_file, "mirror %d logical %Lu physical %Lu "
Josef Bacik 0b8f9c
+			"device %s\n", mirror_num, bytenr, eb->dev_bytenr,
Josef Bacik 0b8f9c
+			device->name);
Josef Bacik 0b8f9c
+		kfree(multi);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if (!copy || mirror_num == copy)
Josef Bacik 0b8f9c
+			ret = read_extent_from_disk(eb);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
Josef Bacik 0b8f9c
+					      eb->start, eb->len);
Josef Bacik 0b8f9c
+		if (num_copies == 1)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		mirror_num++;
Josef Bacik 0b8f9c
+		if (mirror_num > num_copies)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return eb;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static void print_usage(void)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n");
Josef Bacik 0b8f9c
+	fprintf(stderr, "\t-l Logical extent to map\n");
Josef Bacik 0b8f9c
+	fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
Josef Bacik 0b8f9c
+	fprintf(stderr, "\t-o Output file to hold the extent\n");
Josef Bacik 0b8f9c
+	fprintf(stderr, "\t-s Number of bytes to read\n");
Josef Bacik 0b8f9c
+	exit(1);
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static struct option long_options[] = {
Josef Bacik 0b8f9c
+	/* { "byte-count", 1, NULL, 'b' }, */
Josef Bacik 0b8f9c
+	{ "logical", 1, NULL, 'l' },
Josef Bacik 0b8f9c
+	{ "copy", 1, NULL, 'c' },
Josef Bacik 0b8f9c
+	{ "output", 1, NULL, 'c' },
Josef Bacik 0b8f9c
+	{ "bytes", 1, NULL, 'b' },
Josef Bacik 0b8f9c
+	{ 0, 0, 0, 0}
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int main(int ac, char **av)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct cache_tree root_cache;
Josef Bacik 0b8f9c
+	struct btrfs_root *root;
Josef Bacik 0b8f9c
+	struct extent_buffer *eb;
Josef Bacik 0b8f9c
+	char *dev;
Josef Bacik 0b8f9c
+	char *output_file = NULL;
Josef Bacik 0b8f9c
+	u64 logical = 0;
Josef Bacik 0b8f9c
+	int ret = 0;
Josef Bacik 0b8f9c
+	int option_index = 0;
Josef Bacik 0b8f9c
+	int copy = 0;
Josef Bacik 0b8f9c
+	u64 bytes = 0;
Josef Bacik 0b8f9c
+	int out_fd = 0;
Josef Bacik 0b8f9c
+	int err;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	while(1) {
Josef Bacik 0b8f9c
+		int c;
Josef Bacik 0b8f9c
+		c = getopt_long(ac, av, "l:c:o:b:", long_options,
Josef Bacik 0b8f9c
+				&option_index);
Josef Bacik 0b8f9c
+		if (c < 0)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		switch(c) {
Josef Bacik 0b8f9c
+			case 'l':
Josef Bacik 0b8f9c
+				logical = atoll(optarg);
Josef Bacik 0b8f9c
+				if (logical == 0) {
Josef Bacik 0b8f9c
+					fprintf(stderr,
Josef Bacik 0b8f9c
+						"invalid extent number\n");
Josef Bacik 0b8f9c
+					print_usage();
Josef Bacik 0b8f9c
+				}
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+			case 'c':
Josef Bacik 0b8f9c
+				copy = atoi(optarg);
Josef Bacik 0b8f9c
+				if (copy == 0) {
Josef Bacik 0b8f9c
+					fprintf(stderr,
Josef Bacik 0b8f9c
+						"invalid copy number\n");
Josef Bacik 0b8f9c
+					print_usage();
Josef Bacik 0b8f9c
+				}
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+			case 'b':
Josef Bacik 0b8f9c
+				bytes = atoll(optarg);
Josef Bacik 0b8f9c
+				if (bytes == 0) {
Josef Bacik 0b8f9c
+					fprintf(stderr,
Josef Bacik 0b8f9c
+						"invalid byte count\n");
Josef Bacik 0b8f9c
+					print_usage();
Josef Bacik 0b8f9c
+				}
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+			case 'o':
Josef Bacik 0b8f9c
+				output_file = strdup(optarg);
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+			default:
Josef Bacik 0b8f9c
+				print_usage();
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	ac = ac - optind;
Josef Bacik 0b8f9c
+	if (ac == 0)
Josef Bacik 0b8f9c
+		print_usage();
Josef Bacik 0b8f9c
+	if (logical == 0)
Josef Bacik 0b8f9c
+		print_usage();
Josef Bacik 0b8f9c
+	if (copy < 0)
Josef Bacik 0b8f9c
+		print_usage();
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	dev = av[optind];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	radix_tree_init();
Josef Bacik 0b8f9c
+	cache_tree_init(&root_cache);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	root = open_ctree(dev, 0, 0);
Josef Bacik 0b8f9c
+	if (!root) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "Open ctree failed\n");
Josef Bacik 0b8f9c
+		exit(1);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (output_file) {
Josef Bacik 0b8f9c
+		if (strcmp(output_file, "-") == 0) {
Josef Bacik 0b8f9c
+			out_fd = 1;
Josef Bacik 0b8f9c
+			info_file = stderr;
Josef Bacik 0b8f9c
+		} else {
Josef Bacik 0b8f9c
+			out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
Josef Bacik 0b8f9c
+			if (out_fd < 0)
Josef Bacik 0b8f9c
+				goto close;
Josef Bacik 0b8f9c
+			err = ftruncate(out_fd, 0);
Josef Bacik 0b8f9c
+			if (err) {
Josef Bacik 0b8f9c
+				close(out_fd);
Josef Bacik 0b8f9c
+				goto close;
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+			info_file = stdout;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (bytes == 0)
Josef Bacik 0b8f9c
+		bytes = root->sectorsize;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
Josef Bacik 0b8f9c
+	bytes *= root->sectorsize;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	while (bytes > 0) {
Josef Bacik 0b8f9c
+		eb = debug_read_block(root, logical, root->sectorsize, copy);
Josef Bacik 0b8f9c
+		if (eb && output_file) {
Josef Bacik 0b8f9c
+			err = write(out_fd, eb->data, eb->len);
Josef Bacik 0b8f9c
+			if (err < 0 || err != eb->len) {
Josef Bacik 0b8f9c
+				fprintf(stderr, "output file write failed\n");
Josef Bacik 0b8f9c
+				goto out_close_fd;
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		free_extent_buffer(eb);
Josef Bacik 0b8f9c
+		logical += root->sectorsize;
Josef Bacik 0b8f9c
+		bytes -= root->sectorsize;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+out_close_fd:
Josef Bacik 0b8f9c
+	if (output_file && out_fd != 1)
Josef Bacik 0b8f9c
+		close(out_fd);
Josef Bacik 0b8f9c
+close:
Josef Bacik 0b8f9c
+	close_ctree(root);
Josef Bacik 0b8f9c
+	return ret;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
diff --git a/btrfs.c b/btrfs.c
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..ab5e57f
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/btrfs.c
Josef Bacik 0b8f9c
@@ -0,0 +1,387 @@
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * This program is free software; you can redistribute it and/or
Josef Bacik 0b8f9c
+ * modify it under the terms of the GNU General Public
Josef Bacik 0b8f9c
+ * License v2 as published by the Free Software Foundation.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is distributed in the hope that it will be useful,
Josef Bacik 0b8f9c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Josef Bacik 0b8f9c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Josef Bacik 0b8f9c
+ * General Public License for more details.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * You should have received a copy of the GNU General Public
Josef Bacik 0b8f9c
+ * License along with this program; if not, write to the
Josef Bacik 0b8f9c
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Josef Bacik 0b8f9c
+ * Boston, MA 021110-1307, USA.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#include <stdio.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
+#include <string.h>
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#include "kerncompat.h"
Josef Bacik 0b8f9c
+#include "btrfs_cmds.h"
Josef Bacik 0b8f9c
+#include "version.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+typedef int (*CommandFunction)(int argc, char **argv);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+struct Command {
Josef Bacik 0b8f9c
+	CommandFunction	func;	/* function which implements the command */
Josef Bacik 0b8f9c
+	int	nargs;		/* if == 999, any number of arguments
Josef Bacik 0b8f9c
+				   if >= 0, number of arguments,
Josef Bacik 0b8f9c
+				   if < 0, _minimum_ number of arguments */
Josef Bacik 0b8f9c
+	char	*verb;		/* verb */
Josef Bacik 0b8f9c
+	char	*help;		/* help lines; form the 2nd onward they are
Josef Bacik 0b8f9c
+				   indented */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* the following fields are run-time filled by the program */
Josef Bacik 0b8f9c
+	char	**cmds;		/* array of subcommands */
Josef Bacik 0b8f9c
+	int	ncmds;		/* number of subcommand */
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static struct Command commands[] = {
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+		avoid short commands different for the case only
Josef Bacik 0b8f9c
+	*/
Josef Bacik 0b8f9c
+	{ do_clone, 2,
Josef Bacik 0b8f9c
+	  "subvolume snapshot", "<source> [<dest>/]<name>\n"
Josef Bacik 0b8f9c
+		"Create a writable snapshot of the subvolume <source> with\n"
Josef Bacik 0b8f9c
+		"the name <name> in the <dest> directory."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_delete_subvolume, 1,
Josef Bacik 0b8f9c
+	  "subvolume delete", "<subvolume>\n"
Josef Bacik 0b8f9c
+		"Delete the subvolume <subvolume>."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_create_subvol, 1,
Josef Bacik 0b8f9c
+	  "subvolume create", "[<dest>/]<name>\n"
Josef Bacik 0b8f9c
+		"Create a subvolume in <dest> (or the current directory if\n"
Josef Bacik 0b8f9c
+		"not passed)."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_subvol_list, 1, "subvolume list", "<path>\n"
Josef Bacik 0b8f9c
+		"List the snapshot/subvolume of a filesystem."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_find_newer, 2, "subvolume find-new", "<path> <last_gen>\n"
Josef Bacik 0b8f9c
+		"List the recently modified files in a filesystem."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_defrag, -1,
Josef Bacik 0b8f9c
+	  "filesystem defragment", "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
Josef Bacik 0b8f9c
+		"Defragment a file or a directory."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_set_default_subvol, 2,
Josef Bacik 0b8f9c
+	  "subvolume set-default", "<id> <path>\n"
Josef Bacik 0b8f9c
+		"Set the subvolume of the filesystem <path> which will be mounted\n"
Josef Bacik 0b8f9c
+		"as default."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_fssync, 1,
Josef Bacik 0b8f9c
+	  "filesystem sync", "<path>\n"
Josef Bacik 0b8f9c
+		"Force a sync on the filesystem <path>."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_resize, 2,
Josef Bacik 0b8f9c
+	  "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
Josef Bacik 0b8f9c
+		"Resize the file system. If 'max' is passed, the filesystem\n"
Josef Bacik 0b8f9c
+		"will occupe all available space on the device."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_show_filesystem, 999,
Josef Bacik 0b8f9c
+	  "filesystem show", "[<uuid>|<label>]\n"
Josef Bacik 0b8f9c
+		"Show the info of a btrfs filesystem. If no <uuid> or <label>\n"
Josef Bacik 0b8f9c
+		"is passed, info of all the btrfs filesystem are shown."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_df_filesystem, 1,
Josef Bacik 0b8f9c
+	  "filesystem df", "<path>\n"
Josef Bacik 0b8f9c
+		"Show space usage information for a mount point\n."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_balance, 1,
Josef Bacik 0b8f9c
+	  "filesystem balance", "<path>\n"
Josef Bacik 0b8f9c
+		"Balance the chunks across the device."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_scan,
Josef Bacik 0b8f9c
+	  999, "device scan", "[<device> [<device>..]\n"
Josef Bacik 0b8f9c
+		"Scan all device for or the passed device for a btrfs\n"
Josef Bacik 0b8f9c
+		"filesystem."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_add_volume, -2,
Josef Bacik 0b8f9c
+	  "device add", "<dev> [<dev>..] <path>\n"
Josef Bacik 0b8f9c
+		"Add a device to a filesystem."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	{ do_remove_volume, -2,
Josef Bacik 0b8f9c
+	  "device delete", "<dev> [<dev>..] <path>\n"
Josef Bacik 0b8f9c
+		"Remove a device from a filesystem."
Josef Bacik 0b8f9c
+	},
Josef Bacik 0b8f9c
+	/* coming soon
Josef Bacik 0b8f9c
+	{ 2, "filesystem label", "<label> <path>\n"
Josef Bacik 0b8f9c
+		"Set the label of a filesystem"
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	*/
Josef Bacik 0b8f9c
+	{ 0, 0 , 0 }
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static char *get_prgname(char *programname)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	char	*np;
Josef Bacik 0b8f9c
+	np = strrchr(programname,'/');
Josef Bacik 0b8f9c
+	if(!np)
Josef Bacik 0b8f9c
+		np = programname;
Josef Bacik 0b8f9c
+	else
Josef Bacik 0b8f9c
+		np++;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return np;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static void print_help(char *programname, struct Command *cmd)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	char	*pc;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("\t%s %s ", programname, cmd->verb );
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for(pc = cmd->help; *pc; pc++){
Josef Bacik 0b8f9c
+		putchar(*pc);
Josef Bacik 0b8f9c
+		if(*pc == '\n')
Josef Bacik 0b8f9c
+			printf("\t\t");
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	putchar('\n');
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static void help(char *np)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct Command *cp;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("Usage:\n");
Josef Bacik 0b8f9c
+	for( cp = commands; cp->verb; cp++ )
Josef Bacik 0b8f9c
+		print_help(np, cp);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
Josef Bacik 0b8f9c
+	printf("\n%s\n", BTRFS_BUILD_VERSION);
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static int split_command(char *cmd, char ***commands)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int	c, l;
Josef Bacik 0b8f9c
+	char	*p, *s;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
Josef Bacik 0b8f9c
+		if ( *p && *p != ' ' )
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/* c + 2 so that we have room for the null */
Josef Bacik 0b8f9c
+		(*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
Josef Bacik 0b8f9c
+		(*commands)[c] = strndup(s, l);
Josef Bacik 0b8f9c
+		c++;
Josef Bacik 0b8f9c
+		l = 0;
Josef Bacik 0b8f9c
+		s = p+1;
Josef Bacik 0b8f9c
+		if( !*p ) break;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	(*commands)[c] = 0;
Josef Bacik 0b8f9c
+	return c;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+	This function checks if the passed command is ambiguous
Josef Bacik 0b8f9c
+*/
Josef Bacik 0b8f9c
+static int check_ambiguity(struct Command *cmd, char **argv){
Josef Bacik 0b8f9c
+	int		i;
Josef Bacik 0b8f9c
+	struct Command	*cp;
Josef Bacik 0b8f9c
+	/* check for ambiguity */
Josef Bacik 0b8f9c
+	for( i = 0 ; i < cmd->ncmds ; i++ ){
Josef Bacik 0b8f9c
+		int match;
Josef Bacik 0b8f9c
+		for( match = 0, cp = commands; cp->verb; cp++ ){
Josef Bacik 0b8f9c
+			int	j, skip;
Josef Bacik 0b8f9c
+			char	*s1, *s2;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			if( cp->ncmds < i )
Josef Bacik 0b8f9c
+				continue;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			for( skip = 0, j = 0 ; j < i ; j++ )
Josef Bacik 0b8f9c
+				if( strcmp(cmd->cmds[j], cp->cmds[j])){
Josef Bacik 0b8f9c
+					skip=1;
Josef Bacik 0b8f9c
+					break;
Josef Bacik 0b8f9c
+				}
Josef Bacik 0b8f9c
+			if(skip)
Josef Bacik 0b8f9c
+				continue;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			if( !strcmp(cmd->cmds[i], cp->cmds[i]))
Josef Bacik 0b8f9c
+				continue;
Josef Bacik 0b8f9c
+			for(s2 = cp->cmds[i], s1 = argv[i+1];
Josef Bacik 0b8f9c
+				*s1 == *s2 && *s1; s1++, s2++ ) ;
Josef Bacik 0b8f9c
+			if( !*s1 )
Josef Bacik 0b8f9c
+				match++;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		if(match){
Josef Bacik 0b8f9c
+			int j;
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: in command '");
Josef Bacik 0b8f9c
+			for( j = 0 ; j <= i ; j++ )
Josef Bacik 0b8f9c
+				fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
Josef Bacik 0b8f9c
+			fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
Josef Bacik 0b8f9c
+			return -2;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * This function, compacts the program name and the command in the first
Josef Bacik 0b8f9c
+ * element of the '*av' array
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	char	**ret;
Josef Bacik 0b8f9c
+	int	i;
Josef Bacik 0b8f9c
+	char	*newname;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = (char **)malloc(sizeof(char*)*(*ac+1));
Josef Bacik 0b8f9c
+	newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
Josef Bacik 0b8f9c
+	if( !ret || !newname ){
Josef Bacik 0b8f9c
+		free(ret);
Josef Bacik 0b8f9c
+		free(newname);
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret[0] = newname;
Josef Bacik 0b8f9c
+	for(i=0; i < *ac ; i++ )
Josef Bacik 0b8f9c
+		ret[i+1] = (*av)[i];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	strcpy(newname, prgname);
Josef Bacik 0b8f9c
+	strcat(newname, " ");
Josef Bacik 0b8f9c
+	strcat(newname, cmd->verb);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	(*ac)++;
Josef Bacik 0b8f9c
+	*av = ret;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	This function perform the following jobs:
Josef Bacik 0b8f9c
+	- show the help if '--help' or 'help' or '-h' are passed
Josef Bacik 0b8f9c
+	- verify that a command is not ambiguous, otherwise show which
Josef Bacik 0b8f9c
+	  part of the command is ambiguous
Josef Bacik 0b8f9c
+	- if after a (even partial) command there is '--help' show the help
Josef Bacik 0b8f9c
+	  for all the matching commands
Josef Bacik 0b8f9c
+	- if the command doesn't' match show an error
Josef Bacik 0b8f9c
+	- finally, if a command match, they return which command is matched and
Josef Bacik 0b8f9c
+	  the arguments
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	The function return 0 in case of help is requested; <0 in case
Josef Bacik 0b8f9c
+	of uncorrect command; >0 in case of matching commands
Josef Bacik 0b8f9c
+	argc, argv are the arg-counter and arg-vector (input)
Josef Bacik 0b8f9c
+	*nargs_ is the number of the arguments after the command (output)
Josef Bacik 0b8f9c
+	**cmd_  is the invoked command (output)
Josef Bacik 0b8f9c
+	***args_ are the arguments after the command
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+*/
Josef Bacik 0b8f9c
+static int parse_args(int argc, char **argv,
Josef Bacik 0b8f9c
+		      CommandFunction *func_,
Josef Bacik 0b8f9c
+		      int *nargs_, char **cmd_, char ***args_ )
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct Command	*cp;
Josef Bacik 0b8f9c
+	struct Command	*matchcmd=0;
Josef Bacik 0b8f9c
+	char		*prgname = get_prgname(argv[0]);
Josef Bacik 0b8f9c
+	int		i=0, helprequested=0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if( argc < 2 || !strcmp(argv[1], "help") ||
Josef Bacik 0b8f9c
+		!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
Josef Bacik 0b8f9c
+		help(prgname);
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for( cp = commands; cp->verb; cp++ )
Josef Bacik 0b8f9c
+		if( !cp->ncmds)
Josef Bacik 0b8f9c
+			cp->ncmds = split_command(cp->verb, &(cp->cmds));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for( cp = commands; cp->verb; cp++ ){
Josef Bacik 0b8f9c
+		int     match;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if( argc-1 < cp->ncmds )
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
Josef Bacik 0b8f9c
+			char	*s1, *s2;
Josef Bacik 0b8f9c
+			s1 = cp->cmds[i];
Josef Bacik 0b8f9c
+			s2 = argv[i+1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+			for(s2 = cp->cmds[i], s1 = argv[i+1];
Josef Bacik 0b8f9c
+				*s1 == *s2 && *s1;
Josef Bacik 0b8f9c
+				s1++, s2++ ) ;
Josef Bacik 0b8f9c
+			if( *s1 ){
Josef Bacik 0b8f9c
+				match=0;
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		/* If you understand why this code works ...
Josef Bacik 0b8f9c
+			you are a genious !! */
Josef Bacik 0b8f9c
+		if(argc>i+1 && !strcmp(argv[i+1],"--help")){
Josef Bacik 0b8f9c
+			if(!helprequested)
Josef Bacik 0b8f9c
+				printf("Usage:\n");
Josef Bacik 0b8f9c
+			print_help(prgname, cp);
Josef Bacik 0b8f9c
+			helprequested=1;
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if(!match)
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		matchcmd = cp;
Josef Bacik 0b8f9c
+		*nargs_  = argc-matchcmd->ncmds-1;
Josef Bacik 0b8f9c
+		*cmd_ = matchcmd->verb;
Josef Bacik 0b8f9c
+		*args_ = argv+matchcmd->ncmds+1;
Josef Bacik 0b8f9c
+		*func_ = cp->func;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		break;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(helprequested){
Josef Bacik 0b8f9c
+		printf("\n%s\n", BTRFS_BUILD_VERSION);
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(!matchcmd){
Josef Bacik 0b8f9c
+		fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
Josef Bacik 0b8f9c
+		help(prgname);
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(check_ambiguity(matchcmd, argv))
Josef Bacik 0b8f9c
+		return -2;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* check the number of argument */
Josef Bacik 0b8f9c
+	if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
Josef Bacik 0b8f9c
+			matchcmd->verb, -matchcmd->nargs);
Josef Bacik 0b8f9c
+			return -2;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
Josef Bacik 0b8f9c
+			matchcmd->verb, matchcmd->nargs);
Josef Bacik 0b8f9c
+			return -2;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	
Josef Bacik 0b8f9c
+        if (prepare_args( nargs_, args_, prgname, matchcmd )){
Josef Bacik 0b8f9c
+                fprintf(stderr, "ERROR: not enough memory\\n");
Josef Bacik 0b8f9c
+		return -20;
Josef Bacik 0b8f9c
+        }
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 1;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+int main(int ac, char **av )
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	char		*cmd=0, **args=0;
Josef Bacik 0b8f9c
+	int		nargs=0, r;
Josef Bacik 0b8f9c
+	CommandFunction func=0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	r = parse_args(ac, av, &func, &nargs, &cmd, &args);
Josef Bacik 0b8f9c
+	if( r <= 0 ){
Josef Bacik 0b8f9c
+		/* error or no command to parse*/
Josef Bacik 0b8f9c
+		exit(-r);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	exit(func(nargs, args));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
diff --git a/btrfs_cmds.c b/btrfs_cmds.c
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..8031c58
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/btrfs_cmds.c
Josef Bacik 0b8f9c
@@ -0,0 +1,924 @@
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * This program is free software; you can redistribute it and/or
Josef Bacik 0b8f9c
+ * modify it under the terms of the GNU General Public
Josef Bacik 0b8f9c
+ * License v2 as published by the Free Software Foundation.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is distributed in the hope that it will be useful,
Josef Bacik 0b8f9c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Josef Bacik 0b8f9c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Josef Bacik 0b8f9c
+ * General Public License for more details.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * You should have received a copy of the GNU General Public
Josef Bacik 0b8f9c
+ * License along with this program; if not, write to the
Josef Bacik 0b8f9c
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Josef Bacik 0b8f9c
+ * Boston, MA 021110-1307, USA.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#include <stdio.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
+#include <string.h>
Josef Bacik 0b8f9c
+#include <sys/ioctl.h>
Josef Bacik 0b8f9c
+#include <sys/types.h>
Josef Bacik 0b8f9c
+#include <dirent.h>
Josef Bacik 0b8f9c
+#include <sys/stat.h>
Josef Bacik 0b8f9c
+#include <unistd.h>
Josef Bacik 0b8f9c
+#include <fcntl.h>
Josef Bacik 0b8f9c
+#include <libgen.h>
Josef Bacik 0b8f9c
+#include <limits.h>
Josef Bacik 0b8f9c
+#include <uuid/uuid.h>
Josef Bacik 0b8f9c
+#include <ctype.h>
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#undef ULONG_MAX
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#include "kerncompat.h"
Josef Bacik 0b8f9c
+#include "ctree.h"
Josef Bacik 0b8f9c
+#include "transaction.h"
Josef Bacik 0b8f9c
+#include "utils.h"
Josef Bacik 0b8f9c
+#include "version.h"
Josef Bacik 0b8f9c
+#include "ioctl.h"
Josef Bacik 0b8f9c
+#include "volumes.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#include "btrfs_cmds.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#ifdef __CHECKER__
Josef Bacik 0b8f9c
+#define BLKGETSIZE64 0
Josef Bacik 0b8f9c
+#define BTRFS_IOC_SNAP_CREATE 0
Josef Bacik 0b8f9c
+#define BTRFS_VOL_NAME_MAX 255
Josef Bacik 0b8f9c
+struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
Josef Bacik 0b8f9c
+static inline int ioctl(int fd, int define, void *arg) { return 0; }
Josef Bacik 0b8f9c
+#endif
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * test if path is a subvolume:
Josef Bacik 0b8f9c
+ * this function return
Josef Bacik 0b8f9c
+ * 0-> path exists but it is not a subvolume
Josef Bacik 0b8f9c
+ * 1-> path exists and it is  a subvolume
Josef Bacik 0b8f9c
+ * -1 -> path is unaccessible
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static int test_issubvolume(char *path)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	struct stat	st;
Josef Bacik 0b8f9c
+	int		res;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	res = stat(path, &st);
Josef Bacik 0b8f9c
+	if(res < 0 )
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return (st.st_ino == 256) && S_ISDIR(st.st_mode);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * test if path is a directory
Josef Bacik 0b8f9c
+ * this function return
Josef Bacik 0b8f9c
+ * 0-> path exists but it is not a directory
Josef Bacik 0b8f9c
+ * 1-> path exists and it is  a directory
Josef Bacik 0b8f9c
+ * -1 -> path is unaccessible
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+static int test_isdir(char *path)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct stat	st;
Josef Bacik 0b8f9c
+	int		res;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	res = stat(path, &st);
Josef Bacik 0b8f9c
+	if(res < 0 )
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return S_ISDIR(st.st_mode);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static int open_file_or_dir(const char *fname)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	struct stat st;
Josef Bacik 0b8f9c
+	DIR *dirstream;
Josef Bacik 0b8f9c
+	int fd;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = stat(fname, &st);
Josef Bacik 0b8f9c
+	if (ret < 0) {
Josef Bacik 0b8f9c
+		return -1;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (S_ISDIR(st.st_mode)) {
Josef Bacik 0b8f9c
+		dirstream = opendir(fname);
Josef Bacik 0b8f9c
+		if (!dirstream) {
Josef Bacik 0b8f9c
+			return -2;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		fd = dirfd(dirstream);
Josef Bacik 0b8f9c
+	} else {
Josef Bacik 0b8f9c
+		fd = open(fname, O_RDWR);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		return -3;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return fd;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static u64 parse_size(char *s)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int len = strlen(s);
Josef Bacik 0b8f9c
+	char c;
Josef Bacik 0b8f9c
+	u64 mult = 1;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if (!isdigit(s[len - 1])) {
Josef Bacik 0b8f9c
+		c = tolower(s[len - 1]);
Josef Bacik 0b8f9c
+		switch (c) {
Josef Bacik 0b8f9c
+		case 'g':
Josef Bacik 0b8f9c
+			mult *= 1024;
Josef Bacik 0b8f9c
+		case 'm':
Josef Bacik 0b8f9c
+			mult *= 1024;
Josef Bacik 0b8f9c
+		case 'k':
Josef Bacik 0b8f9c
+			mult *= 1024;
Josef Bacik 0b8f9c
+		case 'b':
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		default:
Josef Bacik 0b8f9c
+			fprintf(stderr, "Unknown size descriptor %c\n", c);
Josef Bacik 0b8f9c
+			exit(1);
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		s[len - 1] = '\0';
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return atoll(s) * mult;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_defrag(int ac, char **av)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int fd;
Josef Bacik 0b8f9c
+	int compress = 0;
Josef Bacik 0b8f9c
+	int flush = 0;
Josef Bacik 0b8f9c
+	u64 start = 0;
Josef Bacik 0b8f9c
+	u64 len = (u64)-1;
Josef Bacik 0b8f9c
+	u32 thresh = 0;
Josef Bacik 0b8f9c
+	int i;
Josef Bacik 0b8f9c
+	int errors = 0;
Josef Bacik 0b8f9c
+	int ret = 0;
Josef Bacik 0b8f9c
+	int verbose = 0;
Josef Bacik 0b8f9c
+	int fancy_ioctl = 0;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_defrag_range_args range;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	optind = 1;
Josef Bacik 0b8f9c
+	while(1) {
Josef Bacik 0b8f9c
+		int c = getopt(ac, av, "vcfs:l:t:");
Josef Bacik 0b8f9c
+		if (c < 0)
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		switch(c) {
Josef Bacik 0b8f9c
+		case 'c':
Josef Bacik 0b8f9c
+			compress = 1;
Josef Bacik 0b8f9c
+			fancy_ioctl = 1;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		case 'f':
Josef Bacik 0b8f9c
+			flush = 1;
Josef Bacik 0b8f9c
+			fancy_ioctl = 1;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		case 'v':
Josef Bacik 0b8f9c
+			verbose = 1;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		case 's':
Josef Bacik 0b8f9c
+			start = parse_size(optarg);
Josef Bacik 0b8f9c
+			fancy_ioctl = 1;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		case 'l':
Josef Bacik 0b8f9c
+			len = parse_size(optarg);
Josef Bacik 0b8f9c
+			fancy_ioctl = 1;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		case 't':
Josef Bacik 0b8f9c
+			thresh = parse_size(optarg);
Josef Bacik 0b8f9c
+			fancy_ioctl = 1;
Josef Bacik 0b8f9c
+			break;
Josef Bacik 0b8f9c
+		default:
Josef Bacik 0b8f9c
+			fprintf(stderr, "Invalid arguments for defragment\n");
Josef Bacik 0b8f9c
+			free(av);
Josef Bacik 0b8f9c
+			return 1;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (ac - optind == 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "Invalid arguments for defragment\n");
Josef Bacik 0b8f9c
+		free(av);
Josef Bacik 0b8f9c
+		return 1;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&range, 0, sizeof(range));
Josef Bacik 0b8f9c
+	range.start = start;
Josef Bacik 0b8f9c
+	range.len = len;
Josef Bacik 0b8f9c
+	range.extent_thresh = thresh;
Josef Bacik 0b8f9c
+	if (compress)
Josef Bacik 0b8f9c
+		range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
Josef Bacik 0b8f9c
+	if (flush)
Josef Bacik 0b8f9c
+		range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for (i = optind; i < ac; i++) {
Josef Bacik 0b8f9c
+		if (verbose)
Josef Bacik 0b8f9c
+			printf("%s\n", av[i]);
Josef Bacik 0b8f9c
+		fd = open_file_or_dir(av[i]);
Josef Bacik 0b8f9c
+		if (fd < 0) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "failed to open %s\n", av[i]);
Josef Bacik 0b8f9c
+			perror("open:");
Josef Bacik 0b8f9c
+			errors++;
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		if (!fancy_ioctl) {
Josef Bacik 0b8f9c
+			ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
Josef Bacik 0b8f9c
+		} else {
Josef Bacik 0b8f9c
+			ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
Josef Bacik 0b8f9c
+			if (ret && errno == ENOTTY) {
Josef Bacik 0b8f9c
+				fprintf(stderr, "defrag range ioctl not "
Josef Bacik 0b8f9c
+					"supported in this kernel, please try "
Josef Bacik 0b8f9c
+					"without any options.\n");
Josef Bacik 0b8f9c
+				errors++;
Josef Bacik 0b8f9c
+				break;
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		if (ret) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ioctl failed on %s ret %d errno %d\n",
Josef Bacik 0b8f9c
+				av[i], ret, errno);
Josef Bacik 0b8f9c
+			errors++;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		close(fd);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (verbose)
Josef Bacik 0b8f9c
+		printf("%s\n", BTRFS_BUILD_VERSION);
Josef Bacik 0b8f9c
+	if (errors) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "total %d failures\n", errors);
Josef Bacik 0b8f9c
+		exit(1);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	free(av);
Josef Bacik 0b8f9c
+	return errors + 20;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_find_newer(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int fd;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	char *subvol;
Josef Bacik 0b8f9c
+	u64 last_gen;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	subvol = argv[1];
Josef Bacik 0b8f9c
+	last_gen = atoll(argv[2]);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = test_issubvolume(subvol);
Josef Bacik 0b8f9c
+	if (ret < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (!ret) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
Josef Bacik 0b8f9c
+		return 13;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(subvol);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	ret = find_updated_files(fd, 0, last_gen);
Josef Bacik 0b8f9c
+	if (ret)
Josef Bacik 0b8f9c
+		return 19;
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_subvol_list(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int fd;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	char *subvol;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	subvol = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = test_issubvolume(subvol);
Josef Bacik 0b8f9c
+	if (ret < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (!ret) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
Josef Bacik 0b8f9c
+		return 13;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(subvol);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	ret = list_subvols(fd);
Josef Bacik 0b8f9c
+	if (ret)
Josef Bacik 0b8f9c
+		return 19;
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_clone(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	char	*subvol, *dst;
Josef Bacik 0b8f9c
+	int	res, fd, fddst, len;
Josef Bacik 0b8f9c
+	char	*newname;
Josef Bacik 0b8f9c
+	char	*dstdir;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	subvol = argv[1];
Josef Bacik 0b8f9c
+	dst = argv[2];
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_vol_args	args;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	res = test_issubvolume(subvol);
Josef Bacik 0b8f9c
+	if(res<0){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if(!res){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
Josef Bacik 0b8f9c
+		return 13;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	res = test_isdir(dst);
Josef Bacik 0b8f9c
+	if(res == 0 ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(res>0){
Josef Bacik 0b8f9c
+		newname = strdup(subvol);
Josef Bacik 0b8f9c
+		newname = basename(newname);
Josef Bacik 0b8f9c
+		dstdir = dst;
Josef Bacik 0b8f9c
+	}else{
Josef Bacik 0b8f9c
+		newname = strdup(dst);
Josef Bacik 0b8f9c
+		newname = basename(newname);
Josef Bacik 0b8f9c
+		dstdir = strdup(dst);
Josef Bacik 0b8f9c
+		dstdir = dirname(dstdir);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if( !strcmp(newname,".") || !strcmp(newname,"..") ||
Josef Bacik 0b8f9c
+	     strchr(newname, '/') ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
Josef Bacik 0b8f9c
+			newname);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	len = strlen(newname);
Josef Bacik 0b8f9c
+	if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
Josef Bacik 0b8f9c
+			newname);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fddst = open_file_or_dir(dstdir);
Josef Bacik 0b8f9c
+	if (fddst < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(subvol);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		close(fddst);
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("Create a snapshot of '%s' in '%s/%s'\n",
Josef Bacik 0b8f9c
+	       subvol, dstdir, newname);
Josef Bacik 0b8f9c
+	args.fd = fd;
Josef Bacik 0b8f9c
+	strcpy(args.name, newname);
Josef Bacik 0b8f9c
+	res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	close(fd);
Josef Bacik 0b8f9c
+	close(fddst);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(res < 0 ){
Josef Bacik 0b8f9c
+		fprintf( stderr, "ERROR: cannot snapshot '%s'\n",subvol);
Josef Bacik 0b8f9c
+		return 11;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_delete_subvolume(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int	res, fd, len;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_vol_args	args;
Josef Bacik 0b8f9c
+	char	*dname, *vname, *cpath;
Josef Bacik 0b8f9c
+	char	*path = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	res = test_issubvolume(path);
Josef Bacik 0b8f9c
+	if(res<0){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: error accessing '%s'\n", path);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if(!res){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
Josef Bacik 0b8f9c
+		return 13;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	cpath = realpath(path, 0);
Josef Bacik 0b8f9c
+	dname = strdup(cpath);
Josef Bacik 0b8f9c
+	dname = dirname(dname);
Josef Bacik 0b8f9c
+	vname = strdup(cpath);
Josef Bacik 0b8f9c
+	vname = basename(vname);
Josef Bacik 0b8f9c
+	free(cpath);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if( !strcmp(vname,".") || !strcmp(vname,"..") ||
Josef Bacik 0b8f9c
+	     strchr(vname, '/') ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
Josef Bacik 0b8f9c
+			vname);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	len = strlen(vname);
Josef Bacik 0b8f9c
+	if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
Josef Bacik 0b8f9c
+			vname);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(dname);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		close(fd);
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("Delete subvolume '%s/%s'\n", dname, vname);
Josef Bacik 0b8f9c
+	strcpy(args.name, vname);
Josef Bacik 0b8f9c
+	res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	close(fd);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(res < 0 ){
Josef Bacik 0b8f9c
+		fprintf( stderr, "ERROR: cannot delete '%s/%s'\n",dname, vname);
Josef Bacik 0b8f9c
+		return 11;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_create_subvol(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int	res, fddst, len;
Josef Bacik 0b8f9c
+	char	*newname;
Josef Bacik 0b8f9c
+	char	*dstdir;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_vol_args	args;
Josef Bacik 0b8f9c
+	char	*dst = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	res = test_isdir(dst);
Josef Bacik 0b8f9c
+	if(res >= 0 ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: '%s' exists\n", dst);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	newname = strdup(dst);
Josef Bacik 0b8f9c
+	newname = basename(newname);
Josef Bacik 0b8f9c
+	dstdir = strdup(dst);
Josef Bacik 0b8f9c
+	dstdir = dirname(dstdir);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if( !strcmp(newname,".") || !strcmp(newname,"..") ||
Josef Bacik 0b8f9c
+	     strchr(newname, '/') ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
Josef Bacik 0b8f9c
+			newname);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	len = strlen(newname);
Josef Bacik 0b8f9c
+	if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
Josef Bacik 0b8f9c
+			newname);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fddst = open_file_or_dir(dstdir);
Josef Bacik 0b8f9c
+	if (fddst < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("Create subvolume '%s/%s'\n", dstdir, newname);
Josef Bacik 0b8f9c
+	strcpy(args.name, newname);
Josef Bacik 0b8f9c
+	res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	close(fddst);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	if(res < 0 ){
Josef Bacik 0b8f9c
+		fprintf( stderr, "ERROR: cannot create subvolume\n");
Josef Bacik 0b8f9c
+		return 11;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_fssync(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int fd, res;
Josef Bacik 0b8f9c
+	char	*path = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(path);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("FSSync '%s'\n", path);
Josef Bacik 0b8f9c
+	res = ioctl(fd, BTRFS_IOC_SYNC);
Josef Bacik 0b8f9c
+	close(fd);
Josef Bacik 0b8f9c
+	if( res < 0 ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: unable to fs-syncing '%s'\n", path);
Josef Bacik 0b8f9c
+		return 16;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_scan(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int	i, fd;
Josef Bacik 0b8f9c
+	if(argc<=1){
Josef Bacik 0b8f9c
+		int ret;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		printf("Scanning for Btrfs filesystems\n");
Josef Bacik 0b8f9c
+		ret = btrfs_scan_one_dir("/dev", 1);
Josef Bacik 0b8f9c
+		if (ret){
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: error %d while scanning\n", ret);
Josef Bacik 0b8f9c
+			return 18;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open("/dev/btrfs-control", O_RDWR);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		perror("failed to open /dev/btrfs-control");
Josef Bacik 0b8f9c
+		return 10;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for( i = 1 ; i < argc ; i++ ){
Josef Bacik 0b8f9c
+		struct btrfs_ioctl_vol_args args;
Josef Bacik 0b8f9c
+		int ret;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		strcpy(args.name, argv[i]);
Josef Bacik 0b8f9c
+		/*
Josef Bacik 0b8f9c
+		 * FIXME: which are the error code returned by this ioctl ?
Josef Bacik 0b8f9c
+		 * it seems that is impossible to understand if there no is
Josef Bacik 0b8f9c
+		 * a btrfs filesystem from an I/O error !!!
Josef Bacik 0b8f9c
+		 */
Josef Bacik 0b8f9c
+		ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if( ret < 0 ){
Josef Bacik 0b8f9c
+			close(fd);
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: unable to scan the device '%s'\n", argv[i]);
Josef Bacik 0b8f9c
+			return 11;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	close(fd);
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_resize(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_vol_args	args;
Josef Bacik 0b8f9c
+	int	fd, res, len;
Josef Bacik 0b8f9c
+	char	*amount=argv[1], *path=argv[2];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(path);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	len = strlen(amount);
Josef Bacik 0b8f9c
+	if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: size value too long ('%s)\n",
Josef Bacik 0b8f9c
+			amount);
Josef Bacik 0b8f9c
+		return 14;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	printf("Resize '%s' of '%s'\n", path, amount);
Josef Bacik 0b8f9c
+	strcpy(args.name, amount);
Josef Bacik 0b8f9c
+	res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
Josef Bacik 0b8f9c
+	close(fd);
Josef Bacik 0b8f9c
+	if( res < 0 ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: unable to resize '%s'\n", path);
Josef Bacik 0b8f9c
+		return 30;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct list_head *cur;
Josef Bacik 0b8f9c
+	struct btrfs_device *device;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	list_for_each(cur, &fs_devices->devices) {
Josef Bacik 0b8f9c
+		device = list_entry(cur, struct btrfs_device, dev_list);
Josef Bacik 0b8f9c
+		if ((device->label && strcmp(device->label, search) == 0) ||
Josef Bacik 0b8f9c
+		    strcmp(device->name, search) == 0)
Josef Bacik 0b8f9c
+			return 1;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	char uuidbuf[37];
Josef Bacik 0b8f9c
+	struct list_head *cur;
Josef Bacik 0b8f9c
+	struct btrfs_device *device;
Josef Bacik 0b8f9c
+	char *super_bytes_used;
Josef Bacik 0b8f9c
+	u64 devs_found = 0;
Josef Bacik 0b8f9c
+	u64 total;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	uuid_unparse(fs_devices->fsid, uuidbuf);
Josef Bacik 0b8f9c
+	device = list_entry(fs_devices->devices.next, struct btrfs_device,
Josef Bacik 0b8f9c
+			    dev_list);
Josef Bacik 0b8f9c
+	if (device->label && device->label[0])
Josef Bacik 0b8f9c
+		printf("Label: '%s' ", device->label);
Josef Bacik 0b8f9c
+	else
Josef Bacik 0b8f9c
+		printf("Label: none ");
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	super_bytes_used = pretty_sizes(device->super_bytes_used);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	total = device->total_devs;
Josef Bacik 0b8f9c
+	printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
Josef Bacik 0b8f9c
+	       (unsigned long long)total, super_bytes_used);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	free(super_bytes_used);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	list_for_each(cur, &fs_devices->devices) {
Josef Bacik 0b8f9c
+		char *total_bytes;
Josef Bacik 0b8f9c
+		char *bytes_used;
Josef Bacik 0b8f9c
+		device = list_entry(cur, struct btrfs_device, dev_list);
Josef Bacik 0b8f9c
+		total_bytes = pretty_sizes(device->total_bytes);
Josef Bacik 0b8f9c
+		bytes_used = pretty_sizes(device->bytes_used);
Josef Bacik 0b8f9c
+		printf("\tdevid %4llu size %s used %s path %s\n",
Josef Bacik 0b8f9c
+		       (unsigned long long)device->devid,
Josef Bacik 0b8f9c
+		       total_bytes, bytes_used, device->name);
Josef Bacik 0b8f9c
+		free(total_bytes);
Josef Bacik 0b8f9c
+		free(bytes_used);
Josef Bacik 0b8f9c
+		devs_found++;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (devs_found < total) {
Josef Bacik 0b8f9c
+		printf("\t*** Some devices missing\n");
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	printf("\n");
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_show_filesystem(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct list_head *all_uuids;
Josef Bacik 0b8f9c
+	struct btrfs_fs_devices *fs_devices;
Josef Bacik 0b8f9c
+	struct list_head *cur_uuid;
Josef Bacik 0b8f9c
+	char *search = argv[1];
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = btrfs_scan_one_dir("/dev", 0);
Josef Bacik 0b8f9c
+	if (ret){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: error %d while scanning\n", ret);
Josef Bacik 0b8f9c
+		return 18;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	all_uuids = btrfs_scanned_uuids();
Josef Bacik 0b8f9c
+	list_for_each(cur_uuid, all_uuids) {
Josef Bacik 0b8f9c
+		fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
Josef Bacik 0b8f9c
+					list);
Josef Bacik 0b8f9c
+		if (search && uuid_search(fs_devices, search) == 0)
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		print_one_uuid(fs_devices);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	printf("%s\n", BTRFS_BUILD_VERSION);
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_add_volume(int nargs, char **args)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	char	*mntpnt = args[nargs-1];
Josef Bacik 0b8f9c
+	int	i, fdmnt, ret=0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fdmnt = open_file_or_dir(mntpnt);
Josef Bacik 0b8f9c
+	if (fdmnt < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for(i=1 ; i < (nargs-1) ; i++ ){
Josef Bacik 0b8f9c
+		struct btrfs_ioctl_vol_args ioctl_args;
Josef Bacik 0b8f9c
+		int	devfd, res;
Josef Bacik 0b8f9c
+		u64 dev_block_count = 0;
Josef Bacik 0b8f9c
+		struct stat st;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		devfd = open(args[i], O_RDWR);
Josef Bacik 0b8f9c
+		if (!devfd) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
Josef Bacik 0b8f9c
+			close(devfd);
Josef Bacik 0b8f9c
+			ret++;
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		ret = fstat(devfd, &st);
Josef Bacik 0b8f9c
+		if (ret) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
Josef Bacik 0b8f9c
+			close(devfd);
Josef Bacik 0b8f9c
+			ret++;
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		if (!S_ISBLK(st.st_mode)) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
Josef Bacik 0b8f9c
+			close(devfd);
Josef Bacik 0b8f9c
+			ret++;
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count);
Josef Bacik 0b8f9c
+		if (res) {
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
Josef Bacik 0b8f9c
+			close(devfd);
Josef Bacik 0b8f9c
+			ret++;
Josef Bacik 0b8f9c
+			continue;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+		close(devfd);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		strcpy(ioctl_args.name, args[i]);
Josef Bacik 0b8f9c
+		res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
Josef Bacik 0b8f9c
+		if(res<0){
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: error adding the device '%s'\n", args[i]);
Josef Bacik 0b8f9c
+			ret++;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	close(fdmnt);
Josef Bacik 0b8f9c
+	if( ret)
Josef Bacik 0b8f9c
+		return ret+20;
Josef Bacik 0b8f9c
+	else
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_balance(int argc, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	int	fdmnt, ret=0;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_vol_args args;
Josef Bacik 0b8f9c
+	char	*path = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fdmnt = open_file_or_dir(path);
Josef Bacik 0b8f9c
+	if (fdmnt < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	memset(&args, 0, sizeof(args));
Josef Bacik 0b8f9c
+	ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
Josef Bacik 0b8f9c
+	close(fdmnt);
Josef Bacik 0b8f9c
+	if(ret<0){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: balancing '%s'\n", path);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		return 19;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+int do_remove_volume(int nargs, char **args)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	char	*mntpnt = args[nargs-1];
Josef Bacik 0b8f9c
+	int	i, fdmnt, ret=0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fdmnt = open_file_or_dir(mntpnt);
Josef Bacik 0b8f9c
+	if (fdmnt < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for(i=1 ; i < (nargs-1) ; i++ ){
Josef Bacik 0b8f9c
+		struct	btrfs_ioctl_vol_args arg;
Josef Bacik 0b8f9c
+		int	res;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		strcpy(arg.name, args[i]);
Josef Bacik 0b8f9c
+		res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg;;
Josef Bacik 0b8f9c
+		if(res<0){
Josef Bacik 0b8f9c
+			fprintf(stderr, "ERROR: error removing the device '%s'\n", args[i]);
Josef Bacik 0b8f9c
+			ret++;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	close(fdmnt);
Josef Bacik 0b8f9c
+	if( ret)
Josef Bacik 0b8f9c
+		return ret+20;
Josef Bacik 0b8f9c
+	else
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_set_default_subvol(int nargs, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int	ret=0, fd;
Josef Bacik 0b8f9c
+	u64	objectid;
Josef Bacik 0b8f9c
+	char	*path = argv[2];
Josef Bacik 0b8f9c
+	char	*subvolid = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(path);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
Josef Bacik 0b8f9c
+	if (errno == ERANGE) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
Josef Bacik 0b8f9c
+		return 30;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
Josef Bacik 0b8f9c
+	close(fd);
Josef Bacik 0b8f9c
+	if( ret < 0 ){
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: unable to set a new default subvolume\n");
Josef Bacik 0b8f9c
+		return 30;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int do_df_filesystem(int nargs, char **argv)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_space_args *sargs;
Josef Bacik 0b8f9c
+	u64 count = 0, i;
Josef Bacik 0b8f9c
+	int ret;
Josef Bacik 0b8f9c
+	int fd;
Josef Bacik 0b8f9c
+	char *path = argv[1];
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	fd = open_file_or_dir(path);
Josef Bacik 0b8f9c
+	if (fd < 0) {
Josef Bacik 0b8f9c
+		fprintf(stderr, "ERROR: can't access to '%s'\n", path);
Josef Bacik 0b8f9c
+		return 12;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
Josef Bacik 0b8f9c
+	if (!sargs)
Josef Bacik 0b8f9c
+		return -ENOMEM;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sargs->space_slots = 0;
Josef Bacik 0b8f9c
+	sargs->total_spaces = 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
Josef Bacik 0b8f9c
+	if (ret) {
Josef Bacik 0b8f9c
+		free(sargs);
Josef Bacik 0b8f9c
+		return ret;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	if (!sargs->total_spaces)
Josef Bacik 0b8f9c
+		return 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	count = sargs->total_spaces;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
Josef Bacik 0b8f9c
+			(count * sizeof(struct btrfs_ioctl_space_info)));
Josef Bacik 0b8f9c
+	if (!sargs)
Josef Bacik 0b8f9c
+		return -ENOMEM;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	sargs->space_slots = count;
Josef Bacik 0b8f9c
+	sargs->total_spaces = 0;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
Josef Bacik 0b8f9c
+	if (ret) {
Josef Bacik 0b8f9c
+		free(sargs);
Josef Bacik 0b8f9c
+		return ret;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	for (i = 0; i < sargs->total_spaces; i++) {
Josef Bacik 0b8f9c
+		char description[80];
Josef Bacik 0b8f9c
+		char *total_bytes;
Josef Bacik 0b8f9c
+		char *used_bytes;
Josef Bacik 0b8f9c
+		int written = 0;
Josef Bacik 0b8f9c
+		u64 flags = sargs->spaces[i].flags;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		memset(description, 0, 80);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if (flags & BTRFS_BLOCK_GROUP_DATA) {
Josef Bacik 0b8f9c
+			snprintf(description, 5, "%s", "Data");
Josef Bacik 0b8f9c
+			written += 4;
Josef Bacik 0b8f9c
+		} else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Josef Bacik 0b8f9c
+			snprintf(description, 7, "%s", "System");
Josef Bacik 0b8f9c
+			written += 6;
Josef Bacik 0b8f9c
+		} else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik 0b8f9c
+			snprintf(description, 9, "%s", "Metadata");
Josef Bacik 0b8f9c
+			written += 8;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		if (flags & BTRFS_BLOCK_GROUP_RAID0) {
Josef Bacik 0b8f9c
+			snprintf(description+written, 8, "%s", ", RAID0");
Josef Bacik 0b8f9c
+			written += 7;
Josef Bacik 0b8f9c
+		} else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
Josef Bacik 0b8f9c
+			snprintf(description+written, 8, "%s", ", RAID1");
Josef Bacik 0b8f9c
+			written += 7;
Josef Bacik 0b8f9c
+		} else if (flags & BTRFS_BLOCK_GROUP_DUP) {
Josef Bacik 0b8f9c
+			snprintf(description+written, 6, "%s", ", DUP");
Josef Bacik 0b8f9c
+			written += 5;
Josef Bacik 0b8f9c
+		} else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
Josef Bacik 0b8f9c
+			snprintf(description+written, 9, "%s", ", RAID10");
Josef Bacik 0b8f9c
+			written += 8;
Josef Bacik 0b8f9c
+		}
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+		total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
Josef Bacik 0b8f9c
+		used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
Josef Bacik 0b8f9c
+		printf("%s: total=%s, used=%s\n", description, total_bytes,
Josef Bacik 0b8f9c
+		       used_bytes);
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	free(sargs);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
diff --git a/btrfs_cmds.h b/btrfs_cmds.h
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..7bde191
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/btrfs_cmds.h
Josef Bacik 0b8f9c
@@ -0,0 +1,34 @@
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * This program is free software; you can redistribute it and/or
Josef Bacik 0b8f9c
+ * modify it under the terms of the GNU General Public
Josef Bacik 0b8f9c
+ * License v2 as published by the Free Software Foundation.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * This program is distributed in the hope that it will be useful,
Josef Bacik 0b8f9c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Josef Bacik 0b8f9c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Josef Bacik 0b8f9c
+ * General Public License for more details.
Josef Bacik 0b8f9c
+ *
Josef Bacik 0b8f9c
+ * You should have received a copy of the GNU General Public
Josef Bacik 0b8f9c
+ * License along with this program; if not, write to the
Josef Bacik 0b8f9c
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Josef Bacik 0b8f9c
+ * Boston, MA 021110-1307, USA.
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/* btrfs_cmds.c*/
Josef Bacik 0b8f9c
+int do_clone(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_delete_subvolume(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_create_subvol(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_fssync(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_defrag(int argc, char **argv);
Josef Bacik 0b8f9c
+int do_show_filesystem(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_add_volume(int nargs, char **args);
Josef Bacik 0b8f9c
+int do_balance(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_remove_volume(int nargs, char **args);
Josef Bacik 0b8f9c
+int do_scan(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_resize(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_subvol_list(int nargs, char **argv);
Josef Bacik 0b8f9c
+int do_set_default_subvol(int nargs, char **argv);
Josef Bacik 0b8f9c
+int list_subvols(int fd);
Josef Bacik 0b8f9c
+int do_df_filesystem(int nargs, char **argv);
Josef Bacik 0b8f9c
+int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
Josef Bacik 0b8f9c
+int do_find_newer(int argc, char **argv);
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 0b8f9c
index b323818..be6bf25 100644
Josef Bacik 8a3df8
--- a/btrfsctl.c
Josef Bacik 8a3df8
+++ b/btrfsctl.c
Josef Bacik 0b8f9c
@@ -29,6 +29,7 @@
Josef Bacik 0b8f9c
 #include <unistd.h>
Josef Bacik 0b8f9c
 #include <dirent.h>
Josef Bacik 0b8f9c
 #include <libgen.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
 #include "kerncompat.h"
Josef Bacik 0b8f9c
 #include "ctree.h"
Josef Bacik 0b8f9c
 #include "transaction.h"
Josef Bacik 0b8f9c
@@ -46,7 +47,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 0b8f9c
@@ -55,6 +56,9 @@ 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 0b8f9c
+	printf("\t-m [tree id] directory: set the default mounted subvolume"
Josef Bacik 0b8f9c
+	       " to the [tree id] or the directory\n");
Josef Bacik 8a3df8
 	printf("%s\n", BTRFS_BUILD_VERSION);
Josef Bacik 8a3df8
 	exit(1);
Josef Bacik 8a3df8
 }
Josef Bacik 0b8f9c
@@ -100,6 +104,7 @@ int main(int ac, char **av)
Josef Bacik 0b8f9c
 	unsigned long command = 0;
Josef Bacik 0b8f9c
 	int len;
Josef Bacik 0b8f9c
 	char *fullpath;
Josef Bacik 0b8f9c
+	u64 objectid = 0;
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 	if (ac == 2 && strcmp(av[1], "-a") == 0) {
Josef Bacik 0b8f9c
 		fprintf(stderr, "Scanning for Btrfs filesystems\n");
Josef Bacik 0b8f9c
@@ -158,6 +163,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 0b8f9c
@@ -178,6 +195,16 @@ int main(int ac, char **av)
Josef Bacik 0b8f9c
 			command = BTRFS_IOC_RESIZE;
Josef Bacik 0b8f9c
 		} else if (strcmp(av[i], "-c") == 0) {
Josef Bacik 0b8f9c
 			command = BTRFS_IOC_SYNC;
Josef Bacik 0b8f9c
+		} else if (strcmp(av[i], "-m") == 0) {
Josef Bacik 0b8f9c
+			command = BTRFS_IOC_DEFAULT_SUBVOL;
Josef Bacik 0b8f9c
+			if (i == ac - 3) {
Josef Bacik 0b8f9c
+				objectid = (unsigned long long)
Josef Bacik 0b8f9c
+					    strtoll(av[i + 1], NULL, 0);
Josef Bacik 0b8f9c
+				if (errno == ERANGE) {
Josef Bacik 0b8f9c
+					fprintf(stderr, "invalid tree id\n");
Josef Bacik 0b8f9c
+					exit(1);
Josef Bacik 0b8f9c
+				}
Josef Bacik 0b8f9c
+			}
Josef Bacik 0b8f9c
 		}
Josef Bacik 0b8f9c
 	}
Josef Bacik 0b8f9c
 	if (command == 0) {
Josef Bacik 0b8f9c
@@ -206,6 +233,9 @@ int main(int ac, char **av)
Josef Bacik 0b8f9c
 	if (command == BTRFS_IOC_SNAP_CREATE) {
Josef Bacik 0b8f9c
 		args.fd = fd;
Josef Bacik 0b8f9c
 		ret = ioctl(snap_fd, command, &args);
Josef Bacik 0b8f9c
+	} else if (command == BTRFS_IOC_DEFAULT_SUBVOL) {
Josef Bacik 0b8f9c
+		printf("objectid is %llu\n", objectid);
Josef Bacik 0b8f9c
+		ret = ioctl(fd, command, &objectid);
Josef Bacik 0b8f9c
 	} else
Josef Bacik 0b8f9c
 		ret = ioctl(fd, command, &args);
Josef Bacik 0b8f9c
 	if (ret < 0) {
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 0b8f9c
diff --git a/ctree.h b/ctree.h
Josef Bacik 0b8f9c
index a9062ea..64ecf12 100644
Josef Bacik 0b8f9c
--- a/ctree.h
Josef Bacik 0b8f9c
+++ b/ctree.h
Josef Bacik 0b8f9c
@@ -1047,6 +1047,7 @@ BTRFS_SETGET_STACK_FUNCS(block_group_flags,
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 /* struct btrfs_inode_ref */
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 /* struct btrfs_inode_item */
Josef Bacik 0b8f9c
@@ -1325,6 +1326,10 @@ BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
 /* struct btrfs_dir_item */
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
Josef Bacik 0b8f9c
@@ -1572,6 +1577,7 @@ static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 /* struct btrfs_file_extent_item */
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 static inline unsigned long btrfs_file_extent_inline_start(struct
Josef Bacik 0b8f9c
 						   btrfs_file_extent_item *e)
Josef Bacik 0b8f9c
@@ -1588,18 +1594,30 @@ static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   disk_bytenr, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
+		   disk_bytenr, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   generation, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
+		   generation, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   disk_num_bytes, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		  offset, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
+		  offset, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   num_bytes, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
+		   num_bytes, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   ram_bytes, 64);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
+		   ram_bytes, 64);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   compression, 8);
Josef Bacik 0b8f9c
+BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
+		   compression, 8);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
 		   encryption, 8);
Josef Bacik 0b8f9c
 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
Josef Bacik 0b8f9c
diff --git a/ioctl-test.c b/ioctl-test.c
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..7cf3bc2
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/ioctl-test.c
Josef Bacik 0b8f9c
@@ -0,0 +1,36 @@
Josef Bacik 0b8f9c
+#include <stdio.h>
Josef Bacik 0b8f9c
+#include <stdlib.h>
Josef Bacik 0b8f9c
+#include "kerncompat.h"
Josef Bacik 0b8f9c
+#include "ioctl.h"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+unsigned long ioctls[] = {
Josef Bacik 0b8f9c
+	BTRFS_IOC_SNAP_CREATE,
Josef Bacik 0b8f9c
+	BTRFS_IOC_DEFRAG,
Josef Bacik 0b8f9c
+	BTRFS_IOC_RESIZE,
Josef Bacik 0b8f9c
+	BTRFS_IOC_SCAN_DEV,
Josef Bacik 0b8f9c
+	BTRFS_IOC_TRANS_START,
Josef Bacik 0b8f9c
+	BTRFS_IOC_TRANS_END,
Josef Bacik 0b8f9c
+	BTRFS_IOC_SYNC,
Josef Bacik 0b8f9c
+	BTRFS_IOC_CLONE,
Josef Bacik 0b8f9c
+	BTRFS_IOC_ADD_DEV,
Josef Bacik 0b8f9c
+	BTRFS_IOC_RM_DEV,
Josef Bacik 0b8f9c
+	BTRFS_IOC_BALANCE,
Josef Bacik 0b8f9c
+	BTRFS_IOC_SUBVOL_CREATE,
Josef Bacik 0b8f9c
+	BTRFS_IOC_SNAP_DESTROY,
Josef Bacik 0b8f9c
+	BTRFS_IOC_DEFRAG_RANGE,
Josef Bacik 0b8f9c
+	BTRFS_IOC_TREE_SEARCH,
Josef Bacik 0b8f9c
+	BTRFS_IOC_INO_LOOKUP,
Josef Bacik 0b8f9c
+	BTRFS_IOC_DEFAULT_SUBVOL,
Josef Bacik 0b8f9c
+	BTRFS_IOC_SPACE_INFO,
Josef Bacik 0b8f9c
+	0 };
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+int main(int ac, char **av)
Josef Bacik 0b8f9c
+{
Josef Bacik 0b8f9c
+	int i = 0;
Josef Bacik 0b8f9c
+	while(ioctls[i]) {
Josef Bacik 0b8f9c
+		printf("%lu\n" ,ioctls[i]);
Josef Bacik 0b8f9c
+		i++;
Josef Bacik 0b8f9c
+	}
Josef Bacik 0b8f9c
+	return 0;
Josef Bacik 0b8f9c
+}
Josef Bacik 0b8f9c
+
Josef Bacik 8a3df8
diff --git a/ioctl.h b/ioctl.h
Josef Bacik 0b8f9c
index a084f33..776d7a9 100644
Josef Bacik 8a3df8
--- a/ioctl.h
Josef Bacik 8a3df8
+++ b/ioctl.h
Josef Bacik 0b8f9c
@@ -30,6 +30,108 @@ struct btrfs_ioctl_vol_args {
Josef Bacik 0b8f9c
 	char name[BTRFS_PATH_NAME_MAX + 1];
Josef Bacik 0b8f9c
 };
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
+struct btrfs_ioctl_search_key {
Josef Bacik 0b8f9c
+	/* which root are we searching.  0 is the tree of tree roots */
Josef Bacik 0b8f9c
+	__u64 tree_id;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* keys returned will be >= min and <= max */
Josef Bacik 0b8f9c
+	__u64 min_objectid;
Josef Bacik 0b8f9c
+	__u64 max_objectid;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* keys returned will be >= min and <= max */
Josef Bacik 0b8f9c
+	__u64 min_offset;
Josef Bacik 0b8f9c
+	__u64 max_offset;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* max and min transids to search for */
Josef Bacik 0b8f9c
+	__u64 min_transid;
Josef Bacik 0b8f9c
+	__u64 max_transid;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* keys returned will be >= min and <= max */
Josef Bacik 0b8f9c
+	__u32 min_type;
Josef Bacik 0b8f9c
+	__u32 max_type;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * how many items did userland ask for, and how many are we
Josef Bacik 0b8f9c
+	 * returning
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	__u32 nr_items;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* align to 64 bits */
Josef Bacik 0b8f9c
+	__u32 unused;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* some extra for later */
Josef Bacik 0b8f9c
+	__u64 unused1;
Josef Bacik 0b8f9c
+	__u64 unused2;
Josef Bacik 0b8f9c
+	__u64 unused3;
Josef Bacik 0b8f9c
+	__u64 unused4;
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+struct btrfs_ioctl_search_header {
Josef Bacik 0b8f9c
+	__u64 transid;
Josef Bacik 0b8f9c
+	__u64 objectid;
Josef Bacik 0b8f9c
+	__u64 offset;
Josef Bacik 0b8f9c
+	__u32 type;
Josef Bacik 0b8f9c
+	__u32 len;
Josef Bacik 0b8f9c
+} __attribute__((may_alias));
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#define BTRFS_SEARCH_ARGS_BUFSIZE (4096 - sizeof(struct btrfs_ioctl_search_key))
Josef Bacik 0b8f9c
+/*
Josef Bacik 0b8f9c
+ * the buf is an array of search headers where
Josef Bacik 0b8f9c
+ * each header is followed by the actual item
Josef Bacik 0b8f9c
+ * the type field is expanded to 32 bits for alignment
Josef Bacik 0b8f9c
+ */
Josef Bacik 0b8f9c
+struct btrfs_ioctl_search_args {
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_search_key key;
Josef Bacik 0b8f9c
+	char buf[BTRFS_SEARCH_ARGS_BUFSIZE];
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+#define BTRFS_INO_LOOKUP_PATH_MAX 4080
Josef Bacik 0b8f9c
+struct btrfs_ioctl_ino_lookup_args {
Josef Bacik 0b8f9c
+	__u64 treeid;
Josef Bacik 0b8f9c
+	__u64 objectid;
Josef Bacik 0b8f9c
+	char name[BTRFS_INO_LOOKUP_PATH_MAX];
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+/* flags for the defrag range ioctl */
Josef Bacik 0b8f9c
+#define BTRFS_DEFRAG_RANGE_COMPRESS 1
Josef Bacik 0b8f9c
+#define BTRFS_DEFRAG_RANGE_START_IO 2
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+struct btrfs_ioctl_defrag_range_args {
Josef Bacik 0b8f9c
+	/* start of the defrag operation */
Josef Bacik 0b8f9c
+	__u64 start;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* number of bytes to defrag, use (u64)-1 to say all */
Josef Bacik 0b8f9c
+	__u64 len;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * flags for the operation, which can include turning
Josef Bacik 0b8f9c
+	 * on compression for this one defrag
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	__u64 flags;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/*
Josef Bacik 0b8f9c
+	 * any extent bigger than this will be considered
Josef Bacik 0b8f9c
+	 * already defragged.  Use 0 to take the kernel default
Josef Bacik 0b8f9c
+	 * Use 1 to say every single extent must be rewritten
Josef Bacik 0b8f9c
+	 */
Josef Bacik 0b8f9c
+	__u32 extent_thresh;
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+	/* spare for later */
Josef Bacik 0b8f9c
+	__u32 unused[5];
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+struct btrfs_ioctl_space_info {
Josef Bacik 0b8f9c
+	__u64 flags;
Josef Bacik 0b8f9c
+	__u64 total_bytes;
Josef Bacik 0b8f9c
+	__u64 used_bytes;
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+struct btrfs_ioctl_space_args {
Josef Bacik 0b8f9c
+	__u64 space_slots;
Josef Bacik 0b8f9c
+	__u64 total_spaces;
Josef Bacik 0b8f9c
+	struct btrfs_ioctl_space_info spaces[0];
Josef Bacik 0b8f9c
+};
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
Josef Bacik 0b8f9c
 				   struct btrfs_ioctl_vol_args)
Josef Bacik 0b8f9c
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
Josef Bacik 0b8f9c
@@ -56,4 +158,15 @@ 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
+#define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
Josef Bacik 8a3df8
+				   struct btrfs_ioctl_vol_args)
Josef Bacik 0b8f9c
+#define BTRFS_IOC_DEFRAG_RANGE _IOW(BTRFS_IOCTL_MAGIC, 16, \
Josef Bacik 0b8f9c
+				struct btrfs_ioctl_defrag_range_args)
Josef Bacik 0b8f9c
+#define BTRFS_IOC_TREE_SEARCH _IOWR(BTRFS_IOCTL_MAGIC, 17, \
Josef Bacik 0b8f9c
+				   struct btrfs_ioctl_search_args)
Josef Bacik 0b8f9c
+#define BTRFS_IOC_INO_LOOKUP _IOWR(BTRFS_IOCTL_MAGIC, 18, \
Josef Bacik 0b8f9c
+				   struct btrfs_ioctl_ino_lookup_args)
Josef Bacik 0b8f9c
+#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)
Josef Bacik 0b8f9c
+#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
Josef Bacik 0b8f9c
+				    struct btrfs_ioctl_space_args)
Josef Bacik 8a3df8
 #endif
Josef Bacik 0b8f9c
diff --git a/man/Makefile b/man/Makefile
Josef Bacik 0b8f9c
index 4e8893b..4a90b75 100644
Josef Bacik 0b8f9c
--- a/man/Makefile
Josef Bacik 0b8f9c
+++ b/man/Makefile
Josef Bacik 0b8f9c
@@ -7,13 +7,16 @@ mandir = $(prefix)/man
Josef Bacik 0b8f9c
 man8dir = $(mandir)/man8
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 MANPAGES = mkfs.btrfs.8.gz btrfsctl.8.gz btrfsck.8.gz btrfs-image.8.gz \
Josef Bacik 0b8f9c
-	   btrfs-show.8.gz
Josef Bacik 0b8f9c
+	   btrfs-show.8.gz btrfs.8.gz
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 all: $(MANPAGES)
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
 mkfs.btrfs.8.gz: mkfs.btrfs.8.in
Josef Bacik 0b8f9c
 	$(GZIP) -n -c mkfs.btrfs.8.in > mkfs.btrfs.8.gz
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
+btrfs.8.gz: btrfs.8.in
Josef Bacik 0b8f9c
+	$(GZIP) -n -c btrfs.8.in > btrfs.8.gz
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
 btrfsctl.8.gz: btrfsctl.8.in
Josef Bacik 0b8f9c
 	$(GZIP) -n -c btrfsctl.8.in > btrfsctl.8.gz
Josef Bacik 0b8f9c
 
Josef Bacik 0b8f9c
diff --git a/man/btrfs.8.in b/man/btrfs.8.in
Josef Bacik 0b8f9c
new file mode 100644
Josef Bacik 0b8f9c
index 0000000..26ef982
Josef Bacik 0b8f9c
--- /dev/null
Josef Bacik 0b8f9c
+++ b/man/btrfs.8.in
Josef Bacik 0b8f9c
@@ -0,0 +1,170 @@
Josef Bacik 0b8f9c
+.TH BTRFS 8 "" "btrfs" "btrfs"
Josef Bacik 0b8f9c
+.\"
Josef Bacik 0b8f9c
+.\" Man page written by Goffredo Baroncelli <kreijack@inwind.it> (Feb 2010)
Josef Bacik 0b8f9c
+.\"
Josef Bacik 0b8f9c
+.SH NAME
Josef Bacik 0b8f9c
+btrfs \- control a btrfs filesystem
Josef Bacik 0b8f9c
+.SH SYNOPSIS
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBsubvolume snapshot\fP\fI <source> [<dest>/]<name>\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBsubvolume delete\fP\fI <subvolume>\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBsubvolume create\fP\fI [<dest>/]<name>\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBsubvolume list\fP\fI <path>\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBsubvolume set-default\fP\fI <id> <path>\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBfilesystem defrag\fP\fI <file>|<dir> [<file>|<dir>...]\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBfilesystem sync\fP\fI <path> \fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBfilesystem resize\fP\fI [+/\-]<size>[gkm]|max <filesystem>\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBdevice scan\fP\fI [<device> [<device>..]]\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBdevice balance\fP\fI <path> \fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBdevice add\fP\fI <dev> [<dev>..] <path> \fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBdevice delete\fP\fI <dev> [<dev>..] <path> \fP]
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+\fBbtrfs\fP \fBhelp|\-\-help|\-h \fP\fI\fP
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+.SH DESCRIPTION
Josef Bacik 0b8f9c
+.B btrfs
Josef Bacik 0b8f9c
+is used to control the filesystem and the files and directories stored. It is
Josef Bacik 0b8f9c
+the tool to create or destroy a snapshot or a subvolume for the
Josef Bacik 0b8f9c
+filesystem, to defrag a file or a directory, flush the data to the disk,
Josef Bacik 0b8f9c
+to resize the filesystem, to scan the device.
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+It is possible to abbreviate the commands unless the commands  are ambiguous.
Josef Bacik 0b8f9c
+For example: it is possible to run
Josef Bacik 0b8f9c
+.I btrfs sub snaps
Josef Bacik 0b8f9c
+instead of
Josef Bacik 0b8f9c
+.I btrfs subvolume snapshot.
Josef Bacik 0b8f9c
+But
Josef Bacik 0b8f9c
+.I btrfs dev s
Josef Bacik 0b8f9c
+is not allowed, because
Josef Bacik 0b8f9c
+.I dev s
Josef Bacik 0b8f9c
+may be interpreted both as
Josef Bacik 0b8f9c
+.I device show
Josef Bacik 0b8f9c
+and as
Josef Bacik 0b8f9c
+.I device scan.
Josef Bacik 0b8f9c
+In this case
Josef Bacik 0b8f9c
+.I btrfs
Josef Bacik 0b8f9c
+returns an error.
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+If a command is terminated by
Josef Bacik 0b8f9c
+.I --help
Josef Bacik 0b8f9c
+, the relevant help is showed. If the passed command matches more commands,
Josef Bacik 0b8f9c
+the help of all the matched commands are showed. For example
Josef Bacik 0b8f9c
+.I btrfs dev --help
Josef Bacik 0b8f9c
+shows the help of all
Josef Bacik 0b8f9c
+.I device*
Josef Bacik 0b8f9c
+command.
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+.SH COMMANDS
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBsubvolume snapshot\fR\fI <source> [<dest>/]<name>\fR
Josef Bacik 0b8f9c
+Create a writable snapshot of the subvolume \fI<source>\fR with the name
Josef Bacik 0b8f9c
+\fI<name>\fR in the \fI<dest>\fR directory. If \fI<source>\fR is not a
Josef Bacik 0b8f9c
+subvolume, \fBbtrfs\fR returns an error.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBsubvolume delete\fR\fI <subvolume>\fR
Josef Bacik 0b8f9c
+Delete the subvolume \fI<subvolume>\fR. If \fI<subvolume>\fR is not a
Josef Bacik 0b8f9c
+subvolume, \fBbtrfs\fR returns an error.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBsubvolume create\fR\fI [<dest>/]<name>\fR
Josef Bacik 0b8f9c
+Create a subvolume in \fI<dest>\fR (or in the current directory if
Josef Bacik 0b8f9c
+\fI<dest>\fR is omitted).
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBsubvolume list\fR\fI <path>\fR
Josef Bacik 0b8f9c
+List the subvolumes present in the filesystem \fI<path>\fR. For every
Josef Bacik 0b8f9c
+subvolume is showed the subvolume ID (second column), 
Josef Bacik 0b8f9c
+the ID of the \fItop level\fR 
Josef Bacik 0b8f9c
+subvolume (fifth column), and the path (seventh column) relative to the
Josef Bacik 0b8f9c
+\fItop level\fR subvolume.
Josef Bacik 0b8f9c
+These <ID> may be used by the \fBsubvolume set-default\fR command, or at
Josef Bacik 0b8f9c
+mount time via the \fIsubvol=\fR option.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBsubvolume set-default\fR\fI <id> <path>\fR
Josef Bacik 0b8f9c
+Set the subvolume of the filesystem \fI<path>\fR which is mounted as 
Josef Bacik 0b8f9c
+\fIdefault\fR. The subvolume is identified by \fB<id>\fR, which 
Josef Bacik 0b8f9c
+is returned by the \fBsubvolume list\fR command.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBfilesystem defragment\fP\fI <file>|<dir> [<file>|<dir>...]\fR
Josef Bacik 0b8f9c
+Defragment files and/or directories.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBdevice scan\fR \fI[<device> [<device>..]]\fR
Josef Bacik 0b8f9c
+Scan devices for a btrfs filesystem. If no devices are passed, \fBbtrfs\fR scans
Josef Bacik 0b8f9c
+all the block devices.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBfilesystem sync\fR\fI <path> \fR
Josef Bacik 0b8f9c
+Force a sync for the filesystem identified by \fI<path>\fR.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+.\"
Josef Bacik 0b8f9c
+.\" Some wording are extracted by the resize2fs man page
Josef Bacik 0b8f9c
+.\"
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBfilesystem resize\fR\fI [+/\-]<size>[gkm]|max <path>\fR
Josef Bacik 0b8f9c
+Resize a filesystem identified by \fI<path>\fR.
Josef Bacik 0b8f9c
+The \fI<size>\fR parameter specifies the new size of the filesystem.
Josef Bacik 0b8f9c
+If the prefix \fI+\fR or \fI\-\fR is present the size is increased or decreased
Josef Bacik 0b8f9c
+by the quantity \fI<size>\fR.
Josef Bacik 0b8f9c
+If no units are specified, the unit of the \fI<size>\fR parameter defaults to
Josef Bacik 0b8f9c
+bytes. Optionally, the size parameter may be suffixed by one of the following
Josef Bacik 0b8f9c
+the units designators: 'K', 'M', or 'G', kilobytes, megabytes, or gigabytes,
Josef Bacik 0b8f9c
+respectively.
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+If 'max' is passed, the filesystem will occupy all available space on the
Josef Bacik 0b8f9c
+volume(s).
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+The \fBresize\fR command \fBdoes not\fR manipulate the size of underlying
Josef Bacik 0b8f9c
+partition.  If you wish to enlarge/reduce a filesystem, you must make sure you
Josef Bacik 0b8f9c
+can expand the partition before enlarging the filesystem and shrink the
Josef Bacik 0b8f9c
+partition after reducing the size of the filesystem.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBfilesystem show\fR [<uuid>|<label>]\fR
Josef Bacik 0b8f9c
+Show the btrfs filesystem with some additional info. If no UUID or label is
Josef Bacik 0b8f9c
+passed, \fBbtrfs\fR show info of all the btrfs filesystem.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBdevice balance\fR \fI<path>\fR
Josef Bacik 0b8f9c
+Balance the chunks of the filesystem identified by \fI<path>\fR
Josef Bacik 0b8f9c
+across the devices.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBdevice add\fR\fI <dev> [<dev>..] <path>\fR
Josef Bacik 0b8f9c
+Add device(s) to the filesystem identified by \fI<path>\fR.
Josef Bacik 0b8f9c
+.TP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+\fBdevice delete\fR\fI <dev> [<dev>..] <path>\fR
Josef Bacik 0b8f9c
+Remove device(s) from a filesystem identified by \fI<path>\fR.
Josef Bacik 0b8f9c
+.PP
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+.SH EXIT STATUS
Josef Bacik 0b8f9c
+\fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in
Josef Bacik 0b8f9c
+case of failure.
Josef Bacik 0b8f9c
+
Josef Bacik 0b8f9c
+.SH AVAILABILITY
Josef Bacik 0b8f9c
+.B btrfs
Josef Bacik 0b8f9c
+is part of btrfs-progs. Btrfs filesystem is currently under heavy development,
Josef Bacik 0b8f9c
+and not suitable for any uses other than benchmarking and review.
Josef Bacik 0b8f9c
+Please refer to the btrfs wiki http://btrfs.wiki.kernel.org for
Josef Bacik 0b8f9c
+further details.
Josef Bacik 0b8f9c
+.SH SEE ALSO
Josef Bacik 0b8f9c
+.BR mkfs.btrfs (8)