Btrfs: dirindex optimizations

Signed-off-by: Chris Mason <chris.mason@oracle.com>
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 794f7e4..6d40758 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -239,6 +239,7 @@
 	struct radix_tree_root pinned_radix;
 	u64 last_inode_alloc;
 	u64 generation;
+	u64 highest_inode;
 	struct btrfs_transaction *running_transaction;
 	struct btrfs_super_block *disk_super;
 	struct buffer_head *sb_buffer;
@@ -970,6 +971,8 @@
 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
 			   struct btrfs_root *root, struct btrfs_path *path,
 			   u64 objectid, int mod);
+int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
+
 /* inode-item.c */
 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
 		       *root, u64 objectid, struct btrfs_inode_item
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index b1629a5..0ee9945 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -92,6 +92,7 @@
 	int cow = mod != 0;
 	struct btrfs_disk_key *found_key;
 	struct btrfs_leaf *leaf;
+	u32 overflow;
 
 	key.objectid = dir;
 	key.flags = 0;
@@ -119,8 +120,10 @@
 		if (btrfs_match_dir_item_name(root, path, name, name_len))
 			return 0;
 
-		if (btrfs_disk_key_overflow(found_key) == 0)
+		overflow = btrfs_disk_key_overflow(found_key);
+		if (overflow == 0)
 			return 1;
+		btrfs_set_key_overflow(&key, overflow - 1);
 		btrfs_release_path(root, path);
 	}
 	return 1;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index de9ee3aa..5230554 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -311,6 +311,7 @@
 	fs_info->extent_root = extent_root;
 	fs_info->inode_root = inode_root;
 	fs_info->last_inode_alloc = 0;
+	fs_info->highest_inode = 0;
 	fs_info->sb = sb;
 	fs_info->btree_inode = new_inode(sb);
 	fs_info->btree_inode->i_ino = 1;
@@ -360,12 +361,15 @@
 
 	ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info,
 				  BTRFS_FS_TREE_OBJECTID, root);
-	mutex_unlock(&fs_info->fs_mutex);
 	BUG_ON(ret);
 	root->commit_root = root->node;
 	get_bh(root->node);
 	root->ref_cows = 1;
 	root->fs_info->generation = root->root_key.offset + 1;
+	ret = btrfs_find_highest_inode(root, &root->fs_info->last_inode_alloc);
+	if (ret == 0)
+		fs_info->highest_inode = fs_info->last_inode_alloc;
+	mutex_unlock(&fs_info->fs_mutex);
 	return root;
 }
 
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index 329edb4..f665221 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -3,6 +3,37 @@
 #include "disk-io.h"
 #include "transaction.h"
 
+int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid)
+{
+	struct btrfs_path *path;
+	int ret;
+	struct btrfs_leaf *l;
+	struct btrfs_root *root = fs_root->fs_info->inode_root;
+	struct btrfs_key search_key;
+	int slot;
+
+	path = btrfs_alloc_path();
+	BUG_ON(!path);
+
+	search_key.objectid = (u64)-1;
+	search_key.offset = (u64)-1;
+	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
+	if (ret < 0)
+		goto error;
+	BUG_ON(ret == 0);
+	if (path->slots[0] > 0) {
+		slot = path->slots[0] - 1;
+		l = btrfs_buffer_leaf(path->nodes[0]);
+		*objectid = btrfs_disk_key_objectid(&l->items[slot].key);
+	} else {
+		*objectid = BTRFS_FIRST_FREE_OBJECTID;
+	}
+	ret = 0;
+error:
+	btrfs_free_path(path);
+	return ret;
+}
+
 /*
  * walks the btree of allocated inodes and find a hole.
  */
@@ -28,21 +59,6 @@
 	btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
 
 	search_start = fs_root->fs_info->last_inode_alloc;
-	if (search_start == 0) {
-		struct btrfs_disk_key *last_key;
-		btrfs_init_path(path);
-		search_key.objectid = (u64)-1;
-		search_key.offset = (u64)-1;
-		ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
-		if (ret < 0)
-			goto error;
-		BUG_ON(ret == 0);
-		if (path->slots[0] > 0)
-			path->slots[0]--;
-		l = btrfs_buffer_leaf(path->nodes[0]);
-		last_key = &l->items[path->slots[0]].key;
-		search_start = btrfs_disk_key_objectid(last_key);
-	}
 	search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
 	search_key.objectid = search_start;
 	search_key.offset = 0;
@@ -129,6 +145,8 @@
 				    path->slots[0], struct btrfs_inode_map_item);
 	btrfs_cpu_key_to_disk(&inode_item->key, location);
 	btrfs_mark_buffer_dirty(path->nodes[0]);
+	if (objectid > fs_root->fs_info->highest_inode)
+		fs_root->fs_info->highest_inode = objectid;
 out:
 	btrfs_release_path(inode_root, path);
 	btrfs_free_path(path);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 4fd2b16..d4ee780 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -482,6 +482,11 @@
 		item = leaf->items + slot;
 		if (btrfs_disk_key_objectid(&item->key) != key.objectid)
 			break;
+		if (btrfs_disk_key_offset(&item->key) >
+		    root->fs_info->highest_inode) {
+printk("stopping at highest inode %Lu\n", root->fs_info->highest_inode);
+			break;
+		}
 		if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_INDEX_KEY)
 			continue;
 		if (btrfs_disk_key_offset(&item->key) < filp->f_pos)