blob: 298346ae1481e4f1ef64515887a259bc4818e3db [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason9f5fae22007-03-20 14:38:32 -040019#include "ctree.h"
20#include "disk-io.h"
21#include "transaction.h"
22
Chris Mason1b05da22007-04-10 12:13:09 -040023int btrfs_find_highest_inode(struct btrfs_root *root, u64 *objectid)
Chris Mason5be6f7f2007-04-05 13:35:25 -040024{
25 struct btrfs_path *path;
26 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -040027 struct extent_buffer *l;
Chris Mason5be6f7f2007-04-05 13:35:25 -040028 struct btrfs_key search_key;
Chris Mason5f39d392007-10-15 16:14:19 -040029 struct btrfs_key found_key;
Chris Mason5be6f7f2007-04-05 13:35:25 -040030 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;
Chris Mason5f39d392007-10-15 16:14:19 -040043 l = path->nodes[0];
44 btrfs_item_key_to_cpu(l, &found_key, slot);
45 *objectid = found_key.objectid;
Chris Mason5be6f7f2007-04-05 13:35:25 -040046 } else {
47 *objectid = BTRFS_FIRST_FREE_OBJECTID;
48 }
49 ret = 0;
50error:
51 btrfs_free_path(path);
52 return ret;
53}
54
Chris Mason9f5fae22007-03-20 14:38:32 -040055/*
56 * walks the btree of allocated inodes and find a hole.
57 */
58int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
Chris Mason1b05da22007-04-10 12:13:09 -040059 struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -040060 u64 dirid, u64 *objectid)
61{
Chris Mason7cfcc172007-04-02 14:53:59 -040062 struct btrfs_path *path;
Chris Mason9f5fae22007-03-20 14:38:32 -040063 struct btrfs_key key;
64 int ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040065 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -040066 u64 last_ino = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040067 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -040068 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -040069 struct btrfs_key search_key;
70 u64 search_start = dirid;
71
Chris Masona2135012008-06-25 16:01:30 -040072 mutex_lock(&root->objectid_mutex);
73 if (root->last_inode_alloc) {
74 *objectid = ++root->last_inode_alloc;
75 mutex_unlock(&root->objectid_mutex);
76 return 0;
77 }
Chris Mason7cfcc172007-04-02 14:53:59 -040078 path = btrfs_alloc_path();
79 BUG_ON(!path);
Chris Mason1b05da22007-04-10 12:13:09 -040080 search_start = root->last_inode_alloc;
Chris Masonb1a4d962007-04-04 15:27:52 -040081 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
82 search_key.objectid = search_start;
83 search_key.offset = 0;
84
Chris Mason7cfcc172007-04-02 14:53:59 -040085 btrfs_init_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040086 start_found = 0;
Chris Mason7cfcc172007-04-02 14:53:59 -040087 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
Chris Mason9f5fae22007-03-20 14:38:32 -040088 if (ret < 0)
89 goto error;
90
Chris Mason7cfcc172007-04-02 14:53:59 -040091 if (path->slots[0] > 0)
92 path->slots[0]--;
Chris Mason9f5fae22007-03-20 14:38:32 -040093
94 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -040095 l = path->nodes[0];
Chris Mason7cfcc172007-04-02 14:53:59 -040096 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040097 if (slot >= btrfs_header_nritems(l)) {
Chris Mason7cfcc172007-04-02 14:53:59 -040098 ret = btrfs_next_leaf(root, path);
Chris Mason9f5fae22007-03-20 14:38:32 -040099 if (ret == 0)
100 continue;
101 if (ret < 0)
102 goto error;
103 if (!start_found) {
104 *objectid = search_start;
105 start_found = 1;
106 goto found;
107 }
108 *objectid = last_ino > search_start ?
109 last_ino : search_start;
110 goto found;
111 }
Chris Mason5f39d392007-10-15 16:14:19 -0400112 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason9f5fae22007-03-20 14:38:32 -0400113 if (key.objectid >= search_start) {
114 if (start_found) {
115 if (last_ino < search_start)
116 last_ino = search_start;
Yanb1785422008-01-22 12:46:56 -0500117 if (key.objectid > last_ino) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400118 *objectid = last_ino;
119 goto found;
120 }
121 }
122 }
123 start_found = 1;
124 last_ino = key.objectid + 1;
Chris Mason7cfcc172007-04-02 14:53:59 -0400125 path->slots[0]++;
Chris Mason9f5fae22007-03-20 14:38:32 -0400126 }
127 // FIXME -ENOSPC
128found:
Chris Mason1b05da22007-04-10 12:13:09 -0400129 root->last_inode_alloc = *objectid;
Chris Mason7cfcc172007-04-02 14:53:59 -0400130 btrfs_release_path(root, path);
131 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400132 BUG_ON(*objectid < search_start);
Chris Masona2135012008-06-25 16:01:30 -0400133 mutex_unlock(&root->objectid_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400134 return 0;
135error:
Chris Mason7cfcc172007-04-02 14:53:59 -0400136 btrfs_release_path(root, path);
137 btrfs_free_path(path);
Chris Masona2135012008-06-25 16:01:30 -0400138 mutex_unlock(&root->objectid_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400139 return ret;
140}