1 #include <linux/module.h>
4 #include "transaction.h"
6 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid)
8 struct btrfs_path *path;
11 struct btrfs_root *root = fs_root->fs_info->inode_root;
12 struct btrfs_key search_key;
15 path = btrfs_alloc_path();
18 search_key.objectid = (u64)-1;
19 search_key.offset = (u64)-1;
20 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
24 if (path->slots[0] > 0) {
25 slot = path->slots[0] - 1;
26 l = btrfs_buffer_leaf(path->nodes[0]);
27 *objectid = btrfs_disk_key_objectid(&l->items[slot].key);
29 *objectid = BTRFS_FIRST_FREE_OBJECTID;
33 btrfs_free_path(path);
38 * walks the btree of allocated inodes and find a hole.
40 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
41 struct btrfs_root *fs_root,
42 u64 dirid, u64 *objectid)
44 struct btrfs_path *path;
52 struct btrfs_root *root = fs_root->fs_info->inode_root;
53 struct btrfs_key search_key;
54 u64 search_start = dirid;
56 path = btrfs_alloc_path();
59 btrfs_set_key_type(&search_key, BTRFS_INODE_MAP_ITEM_KEY);
61 search_start = fs_root->fs_info->last_inode_alloc;
62 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
63 search_key.objectid = search_start;
64 search_key.offset = 0;
66 btrfs_init_path(path);
68 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
72 if (path->slots[0] > 0)
76 l = btrfs_buffer_leaf(path->nodes[0]);
77 slot = path->slots[0];
78 if (slot >= btrfs_header_nritems(&l->header)) {
79 ret = btrfs_next_leaf(root, path);
85 *objectid = search_start;
89 *objectid = last_ino > search_start ?
90 last_ino : search_start;
93 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
94 if (key.objectid >= search_start) {
96 if (last_ino < search_start)
97 last_ino = search_start;
98 hole_size = key.objectid - last_ino;
100 *objectid = last_ino;
106 last_ino = key.objectid + 1;
111 root->fs_info->last_inode_alloc = *objectid;
112 btrfs_release_path(root, path);
113 btrfs_free_path(path);
114 BUG_ON(*objectid < search_start);
117 btrfs_release_path(root, path);
118 btrfs_free_path(path);
122 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
123 struct btrfs_root *fs_root,
124 u64 objectid, struct btrfs_key *location)
127 struct btrfs_path *path;
128 struct btrfs_inode_map_item *inode_item;
129 struct btrfs_key key;
130 struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
132 key.objectid = objectid;
134 btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
136 path = btrfs_alloc_path();
138 btrfs_init_path(path);
139 ret = btrfs_insert_empty_item(trans, inode_root, path, &key,
140 sizeof(struct btrfs_inode_map_item));
144 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
145 path->slots[0], struct btrfs_inode_map_item);
146 btrfs_cpu_key_to_disk(&inode_item->key, location);
147 btrfs_mark_buffer_dirty(path->nodes[0]);
148 if (objectid > fs_root->fs_info->highest_inode)
149 fs_root->fs_info->highest_inode = objectid;
151 btrfs_release_path(inode_root, path);
152 btrfs_free_path(path);
156 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
157 struct btrfs_root *fs_root, struct btrfs_path *path,
158 u64 objectid, int mod)
161 struct btrfs_key key;
162 int ins_len = mod < 0 ? -1 : 0;
164 struct btrfs_root *inode_root = fs_root->fs_info->inode_root;
166 key.objectid = objectid;
169 btrfs_set_key_type(&key, BTRFS_INODE_MAP_ITEM_KEY);
170 ret = btrfs_search_slot(trans, inode_root, &key, path, ins_len, cow);