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"
23 int btrfs_find_highest_inode(struct btrfs_root
*root
, u64
*objectid
)
25 struct btrfs_path
*path
;
27 struct extent_buffer
*l
;
28 struct btrfs_key search_key
;
29 struct btrfs_key found_key
;
32 path
= btrfs_alloc_path();
35 search_key
.objectid
= BTRFS_LAST_FREE_OBJECTID
;
37 search_key
.offset
= (u64
)-1;
38 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
42 if (path
->slots
[0] > 0) {
43 slot
= path
->slots
[0] - 1;
45 btrfs_item_key_to_cpu(l
, &found_key
, slot
);
46 *objectid
= found_key
.objectid
;
48 *objectid
= BTRFS_FIRST_FREE_OBJECTID
;
52 btrfs_free_path(path
);
57 * walks the btree of allocated inodes and find a hole.
59 int btrfs_find_free_objectid(struct btrfs_trans_handle
*trans
,
60 struct btrfs_root
*root
,
61 u64 dirid
, u64
*objectid
)
63 struct btrfs_path
*path
;
69 struct extent_buffer
*l
;
70 struct btrfs_key search_key
;
71 u64 search_start
= dirid
;
73 mutex_lock(&root
->objectid_mutex
);
74 if (root
->last_inode_alloc
>= BTRFS_FIRST_FREE_OBJECTID
&&
75 root
->last_inode_alloc
< BTRFS_LAST_FREE_OBJECTID
) {
76 *objectid
= ++root
->last_inode_alloc
;
77 mutex_unlock(&root
->objectid_mutex
);
80 path
= btrfs_alloc_path();
82 search_start
= max(search_start
, (u64
)BTRFS_FIRST_FREE_OBJECTID
);
83 search_key
.objectid
= search_start
;
85 search_key
.offset
= 0;
88 ret
= btrfs_search_slot(trans
, root
, &search_key
, path
, 0, 0);
94 slot
= path
->slots
[0];
95 if (slot
>= btrfs_header_nritems(l
)) {
96 ret
= btrfs_next_leaf(root
, path
);
102 *objectid
= search_start
;
106 *objectid
= last_ino
> search_start
?
107 last_ino
: search_start
;
110 btrfs_item_key_to_cpu(l
, &key
, slot
);
111 if (key
.objectid
>= search_start
) {
113 if (last_ino
< search_start
)
114 last_ino
= search_start
;
115 if (key
.objectid
> last_ino
) {
116 *objectid
= last_ino
;
119 } else if (key
.objectid
> search_start
) {
120 *objectid
= search_start
;
124 if (key
.objectid
>= BTRFS_LAST_FREE_OBJECTID
)
128 last_ino
= key
.objectid
+ 1;
133 btrfs_release_path(root
, path
);
134 btrfs_free_path(path
);
135 BUG_ON(*objectid
< search_start
);
136 mutex_unlock(&root
->objectid_mutex
);
139 btrfs_release_path(root
, path
);
140 btrfs_free_path(path
);
141 mutex_unlock(&root
->objectid_mutex
);