Commit | Line | Data |
---|---|---|
2e635a27 | 1 | #include <linux/module.h> |
9f5fae2f CM |
2 | #include "ctree.h" |
3 | #include "disk-io.h" | |
4 | #include "transaction.h" | |
5 | ||
1b05da2e | 6 | int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) |
5be6f7f1 CM |
7 | { |
8 | struct btrfs_path *path; | |
9 | int ret; | |
10 | struct btrfs_leaf *l; | |
5be6f7f1 CM |
11 | struct btrfs_key search_key; |
12 | int slot; | |
13 | ||
14 | path = btrfs_alloc_path(); | |
15 | BUG_ON(!path); | |
16 | ||
17 | search_key.objectid = (u64)-1; | |
18 | search_key.offset = (u64)-1; | |
19 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
20 | if (ret < 0) | |
21 | goto error; | |
22 | BUG_ON(ret == 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); | |
27 | } else { | |
28 | *objectid = BTRFS_FIRST_FREE_OBJECTID; | |
29 | } | |
30 | ret = 0; | |
31 | error: | |
32 | btrfs_free_path(path); | |
33 | return ret; | |
34 | } | |
35 | ||
9f5fae2f CM |
36 | /* |
37 | * walks the btree of allocated inodes and find a hole. | |
38 | */ | |
39 | int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, | |
1b05da2e | 40 | struct btrfs_root *root, |
9f5fae2f CM |
41 | u64 dirid, u64 *objectid) |
42 | { | |
7cfcc17e | 43 | struct btrfs_path *path; |
9f5fae2f CM |
44 | struct btrfs_key key; |
45 | int ret; | |
46 | u64 hole_size = 0; | |
47 | int slot = 0; | |
e20d96d6 | 48 | u64 last_ino = 0; |
9f5fae2f CM |
49 | int start_found; |
50 | struct btrfs_leaf *l; | |
9f5fae2f CM |
51 | struct btrfs_key search_key; |
52 | u64 search_start = dirid; | |
53 | ||
b1a4d965 CM |
54 | path = btrfs_alloc_path(); |
55 | BUG_ON(!path); | |
56 | search_key.flags = 0; | |
1b05da2e | 57 | search_start = root->last_inode_alloc; |
6407bf6d | 58 | search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID); |
9f5fae2f | 59 | search_key.objectid = search_start; |
9f5fae2f CM |
60 | search_key.offset = 0; |
61 | ||
7cfcc17e | 62 | btrfs_init_path(path); |
9f5fae2f | 63 | start_found = 0; |
7cfcc17e | 64 | ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0); |
9f5fae2f CM |
65 | if (ret < 0) |
66 | goto error; | |
67 | ||
7cfcc17e CM |
68 | if (path->slots[0] > 0) |
69 | path->slots[0]--; | |
9f5fae2f CM |
70 | |
71 | while (1) { | |
7cfcc17e CM |
72 | l = btrfs_buffer_leaf(path->nodes[0]); |
73 | slot = path->slots[0]; | |
9f5fae2f | 74 | if (slot >= btrfs_header_nritems(&l->header)) { |
7cfcc17e | 75 | ret = btrfs_next_leaf(root, path); |
9f5fae2f CM |
76 | if (ret == 0) |
77 | continue; | |
78 | if (ret < 0) | |
79 | goto error; | |
80 | if (!start_found) { | |
81 | *objectid = search_start; | |
82 | start_found = 1; | |
83 | goto found; | |
84 | } | |
85 | *objectid = last_ino > search_start ? | |
86 | last_ino : search_start; | |
87 | goto found; | |
88 | } | |
89 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); | |
90 | if (key.objectid >= search_start) { | |
91 | if (start_found) { | |
92 | if (last_ino < search_start) | |
93 | last_ino = search_start; | |
94 | hole_size = key.objectid - last_ino; | |
95 | if (hole_size > 0) { | |
96 | *objectid = last_ino; | |
97 | goto found; | |
98 | } | |
99 | } | |
100 | } | |
101 | start_found = 1; | |
102 | last_ino = key.objectid + 1; | |
7cfcc17e | 103 | path->slots[0]++; |
9f5fae2f CM |
104 | } |
105 | // FIXME -ENOSPC | |
106 | found: | |
1b05da2e | 107 | root->last_inode_alloc = *objectid; |
7cfcc17e CM |
108 | btrfs_release_path(root, path); |
109 | btrfs_free_path(path); | |
9f5fae2f CM |
110 | BUG_ON(*objectid < search_start); |
111 | return 0; | |
112 | error: | |
7cfcc17e CM |
113 | btrfs_release_path(root, path); |
114 | btrfs_free_path(path); | |
9f5fae2f CM |
115 | return ret; |
116 | } |