Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
9f5fae2f CM |
19 | #include "ctree.h" |
20 | #include "disk-io.h" | |
21 | #include "transaction.h" | |
22 | ||
1b05da2e | 23 | int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid) |
5be6f7f1 CM |
24 | { |
25 | struct btrfs_path *path; | |
26 | int ret; | |
5f39d397 | 27 | struct extent_buffer *l; |
5be6f7f1 | 28 | struct btrfs_key search_key; |
5f39d397 | 29 | struct btrfs_key found_key; |
5be6f7f1 CM |
30 | int slot; |
31 | ||
32 | path = btrfs_alloc_path(); | |
33 | BUG_ON(!path); | |
34 | ||
6527cdbe ZY |
35 | search_key.objectid = BTRFS_LAST_FREE_OBJECTID; |
36 | search_key.type = -1; | |
5be6f7f1 CM |
37 | search_key.offset = (u64)-1; |
38 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | |
39 | if (ret < 0) | |
40 | goto error; | |
41 | BUG_ON(ret == 0); | |
42 | if (path->slots[0] > 0) { | |
43 | slot = path->slots[0] - 1; | |
5f39d397 CM |
44 | l = path->nodes[0]; |
45 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
46 | *objectid = found_key.objectid; | |
5be6f7f1 CM |
47 | } else { |
48 | *objectid = BTRFS_FIRST_FREE_OBJECTID; | |
49 | } | |
50 | ret = 0; | |
51 | error: | |
52 | btrfs_free_path(path); | |
53 | return ret; | |
54 | } | |
55 | ||
9f5fae2f CM |
56 | /* |
57 | * walks the btree of allocated inodes and find a hole. | |
58 | */ | |
59 | int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, | |
1b05da2e | 60 | struct btrfs_root *root, |
9f5fae2f CM |
61 | u64 dirid, u64 *objectid) |
62 | { | |
7cfcc17e | 63 | struct btrfs_path *path; |
9f5fae2f CM |
64 | struct btrfs_key key; |
65 | int ret; | |
9f5fae2f | 66 | int slot = 0; |
e20d96d6 | 67 | u64 last_ino = 0; |
9f5fae2f | 68 | int start_found; |
5f39d397 | 69 | struct extent_buffer *l; |
9f5fae2f CM |
70 | struct btrfs_key search_key; |
71 | u64 search_start = dirid; | |
72 | ||
a2135011 | 73 | mutex_lock(&root->objectid_mutex); |
6527cdbe ZY |
74 | if (root->last_inode_alloc >= BTRFS_FIRST_FREE_OBJECTID && |
75 | root->last_inode_alloc < BTRFS_LAST_FREE_OBJECTID) { | |
a2135011 CM |
76 | *objectid = ++root->last_inode_alloc; |
77 | mutex_unlock(&root->objectid_mutex); | |
78 | return 0; | |
79 | } | |
b1a4d965 CM |
80 | path = btrfs_alloc_path(); |
81 | BUG_ON(!path); | |
6407bf6d | 82 | search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID); |
9f5fae2f | 83 | search_key.objectid = search_start; |
6527cdbe | 84 | search_key.type = 0; |
9f5fae2f CM |
85 | search_key.offset = 0; |
86 | ||
7cfcc17e | 87 | btrfs_init_path(path); |
9f5fae2f | 88 | start_found = 0; |
7cfcc17e | 89 | ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0); |
9f5fae2f CM |
90 | if (ret < 0) |
91 | goto error; | |
92 | ||
9f5fae2f | 93 | while (1) { |
5f39d397 | 94 | l = path->nodes[0]; |
7cfcc17e | 95 | slot = path->slots[0]; |
5f39d397 | 96 | if (slot >= btrfs_header_nritems(l)) { |
7cfcc17e | 97 | ret = btrfs_next_leaf(root, path); |
9f5fae2f CM |
98 | if (ret == 0) |
99 | continue; | |
100 | if (ret < 0) | |
101 | goto error; | |
102 | if (!start_found) { | |
103 | *objectid = search_start; | |
104 | start_found = 1; | |
105 | goto found; | |
106 | } | |
107 | *objectid = last_ino > search_start ? | |
108 | last_ino : search_start; | |
109 | goto found; | |
110 | } | |
5f39d397 | 111 | btrfs_item_key_to_cpu(l, &key, slot); |
9f5fae2f CM |
112 | if (key.objectid >= search_start) { |
113 | if (start_found) { | |
114 | if (last_ino < search_start) | |
115 | last_ino = search_start; | |
b1785427 | 116 | if (key.objectid > last_ino) { |
9f5fae2f CM |
117 | *objectid = last_ino; |
118 | goto found; | |
119 | } | |
5b21f2ed ZY |
120 | } else if (key.objectid > search_start) { |
121 | *objectid = search_start; | |
122 | goto found; | |
9f5fae2f CM |
123 | } |
124 | } | |
6527cdbe ZY |
125 | if (key.objectid >= BTRFS_LAST_FREE_OBJECTID) |
126 | break; | |
5b21f2ed | 127 | |
9f5fae2f CM |
128 | start_found = 1; |
129 | last_ino = key.objectid + 1; | |
7cfcc17e | 130 | path->slots[0]++; |
9f5fae2f | 131 | } |
6527cdbe | 132 | BUG_ON(1); |
9f5fae2f | 133 | found: |
7cfcc17e CM |
134 | btrfs_release_path(root, path); |
135 | btrfs_free_path(path); | |
9f5fae2f | 136 | BUG_ON(*objectid < search_start); |
a2135011 | 137 | mutex_unlock(&root->objectid_mutex); |
9f5fae2f CM |
138 | return 0; |
139 | error: | |
7cfcc17e CM |
140 | btrfs_release_path(root, path); |
141 | btrfs_free_path(path); | |
a2135011 | 142 | mutex_unlock(&root->objectid_mutex); |
9f5fae2f CM |
143 | return ret; |
144 | } |