Fix for btrfs_find_free_objectid
[linux-block.git] / fs / btrfs / inode-map.c
CommitLineData
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 23int 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
35 search_key.objectid = (u64)-1;
36 search_key.offset = (u64)-1;
37 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
38 if (ret < 0)
39 goto error;
40 BUG_ON(ret == 0);
41 if (path->slots[0] > 0) {
42 slot = path->slots[0] - 1;
5f39d397
CM
43 l = path->nodes[0];
44 btrfs_item_key_to_cpu(l, &found_key, slot);
45 *objectid = found_key.objectid;
5be6f7f1
CM
46 } else {
47 *objectid = BTRFS_FIRST_FREE_OBJECTID;
48 }
49 ret = 0;
50error:
51 btrfs_free_path(path);
52 return ret;
53}
54
9f5fae2f
CM
55/*
56 * walks the btree of allocated inodes and find a hole.
57 */
58int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1b05da2e 59 struct btrfs_root *root,
9f5fae2f
CM
60 u64 dirid, u64 *objectid)
61{
7cfcc17e 62 struct btrfs_path *path;
9f5fae2f
CM
63 struct btrfs_key key;
64 int ret;
9f5fae2f 65 int slot = 0;
e20d96d6 66 u64 last_ino = 0;
9f5fae2f 67 int start_found;
5f39d397 68 struct extent_buffer *l;
9f5fae2f
CM
69 struct btrfs_key search_key;
70 u64 search_start = dirid;
71
b1a4d965
CM
72 path = btrfs_alloc_path();
73 BUG_ON(!path);
1b05da2e 74 search_start = root->last_inode_alloc;
6407bf6d 75 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
9f5fae2f 76 search_key.objectid = search_start;
9f5fae2f
CM
77 search_key.offset = 0;
78
7cfcc17e 79 btrfs_init_path(path);
9f5fae2f 80 start_found = 0;
7cfcc17e 81 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
9f5fae2f
CM
82 if (ret < 0)
83 goto error;
84
7cfcc17e
CM
85 if (path->slots[0] > 0)
86 path->slots[0]--;
9f5fae2f
CM
87
88 while (1) {
5f39d397 89 l = path->nodes[0];
7cfcc17e 90 slot = path->slots[0];
5f39d397 91 if (slot >= btrfs_header_nritems(l)) {
7cfcc17e 92 ret = btrfs_next_leaf(root, path);
9f5fae2f
CM
93 if (ret == 0)
94 continue;
95 if (ret < 0)
96 goto error;
97 if (!start_found) {
98 *objectid = search_start;
99 start_found = 1;
100 goto found;
101 }
102 *objectid = last_ino > search_start ?
103 last_ino : search_start;
104 goto found;
105 }
5f39d397 106 btrfs_item_key_to_cpu(l, &key, slot);
9f5fae2f
CM
107 if (key.objectid >= search_start) {
108 if (start_found) {
109 if (last_ino < search_start)
110 last_ino = search_start;
b1785427 111 if (key.objectid > last_ino) {
9f5fae2f
CM
112 *objectid = last_ino;
113 goto found;
114 }
115 }
116 }
117 start_found = 1;
118 last_ino = key.objectid + 1;
7cfcc17e 119 path->slots[0]++;
9f5fae2f
CM
120 }
121 // FIXME -ENOSPC
122found:
1b05da2e 123 root->last_inode_alloc = *objectid;
7cfcc17e
CM
124 btrfs_release_path(root, path);
125 btrfs_free_path(path);
9f5fae2f
CM
126 BUG_ON(*objectid < search_start);
127 return 0;
128error:
7cfcc17e
CM
129 btrfs_release_path(root, path);
130 btrfs_free_path(path);
9f5fae2f
CM
131 return ret;
132}