2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
21 #include "transaction.h"
24 * walks the btree of allocated inodes and find a hole.
26 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
27 struct btrfs_root *root,
28 u64 dirid, u64 *objectid)
30 struct btrfs_path *path;
36 struct extent_buffer *l;
37 struct btrfs_key search_key;
38 u64 search_start = dirid;
40 path = btrfs_alloc_path();
42 search_start = root->last_inode_alloc;
43 search_start = max((unsigned long long)search_start,
44 BTRFS_FIRST_FREE_OBJECTID);
45 search_key.objectid = search_start;
46 search_key.offset = 0;
48 btrfs_init_path(path);
50 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
54 if (path->slots[0] > 0)
59 slot = path->slots[0];
60 if (slot >= btrfs_header_nritems(l)) {
61 ret = btrfs_next_leaf(root, path);
67 *objectid = search_start;
71 *objectid = last_ino > search_start ?
72 last_ino : search_start;
75 btrfs_item_key_to_cpu(l, &key, slot);
76 if (key.objectid >= search_start) {
78 if (last_ino < search_start)
79 last_ino = search_start;
80 if (key.objectid > last_ino) {
87 last_ino = key.objectid + 1;
92 root->last_inode_alloc = *objectid;
93 btrfs_free_path(path);
94 BUG_ON(*objectid < search_start);
97 btrfs_free_path(path);