1 #include <linux/module.h>
4 #include "transaction.h"
6 int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
8 struct btrfs_path *path;
11 struct btrfs_key search_key;
14 path = btrfs_alloc_path();
17 search_key.objectid = (u64)-1;
18 search_key.offset = (u64)-1;
19 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
23 if (path->slots[0] > 0) {
24 slot = path->slots[0] - 1;
25 l = btrfs_buffer_leaf(path->nodes[0]);
26 *objectid = btrfs_disk_key_objectid(&l->items[slot].key);
28 *objectid = BTRFS_FIRST_FREE_OBJECTID;
32 btrfs_free_path(path);
37 * walks the btree of allocated inodes and find a hole.
39 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
40 struct btrfs_root *root,
41 u64 dirid, u64 *objectid)
43 struct btrfs_path *path;
51 struct btrfs_key search_key;
52 u64 search_start = dirid;
54 path = btrfs_alloc_path();
57 search_start = root->last_inode_alloc;
58 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
59 search_key.objectid = search_start;
60 search_key.offset = 0;
62 btrfs_init_path(path);
64 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
68 if (path->slots[0] > 0)
72 l = btrfs_buffer_leaf(path->nodes[0]);
73 slot = path->slots[0];
74 if (slot >= btrfs_header_nritems(&l->header)) {
75 ret = btrfs_next_leaf(root, path);
81 *objectid = search_start;
85 *objectid = last_ino > search_start ?
86 last_ino : search_start;
89 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
90 if (key.objectid >= search_start) {
92 if (last_ino < search_start)
93 last_ino = search_start;
94 hole_size = key.objectid - last_ino;
102 last_ino = key.objectid + 1;
107 root->last_inode_alloc = *objectid;
108 btrfs_release_path(root, path);
109 btrfs_free_path(path);
110 BUG_ON(*objectid < search_start);
113 btrfs_release_path(root, path);
114 btrfs_free_path(path);