blob: cd6171c2da4296fe213a07491ff6c1f20ec0220d [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
Zheng Yan6527cdb2008-09-05 16:43:53 -040035 search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
36 search_key.type = -1;
Chris Mason5be6f7f2007-04-05 13:35:25 -040037 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;
Chris Mason5f39d392007-10-15 16:14:19 -040044 l = path->nodes[0];
45 btrfs_item_key_to_cpu(l, &found_key, slot);
46 *objectid = found_key.objectid;
Chris Mason5be6f7f2007-04-05 13:35:25 -040047 } else {
48 *objectid = BTRFS_FIRST_FREE_OBJECTID;
49 }
50 ret = 0;
51error:
52 btrfs_free_path(path);
53 return ret;
54}
55
Chris Mason9f5fae22007-03-20 14:38:32 -040056/*
57 * walks the btree of allocated inodes and find a hole.
58 */
59int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
Chris Mason1b05da22007-04-10 12:13:09 -040060 struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -040061 u64 dirid, u64 *objectid)
62{
Chris Mason7cfcc172007-04-02 14:53:59 -040063 struct btrfs_path *path;
Chris Mason9f5fae22007-03-20 14:38:32 -040064 struct btrfs_key key;
65 int ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040066 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -040067 u64 last_ino = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040068 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -040069 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -040070 struct btrfs_key search_key;
71 u64 search_start = dirid;
72
Chris Masona2135012008-06-25 16:01:30 -040073 mutex_lock(&root->objectid_mutex);
Zheng Yan6527cdb2008-09-05 16:43:53 -040074 if (root->last_inode_alloc >= BTRFS_FIRST_FREE_OBJECTID &&
75 root->last_inode_alloc < BTRFS_LAST_FREE_OBJECTID) {
Chris Masona2135012008-06-25 16:01:30 -040076 *objectid = ++root->last_inode_alloc;
77 mutex_unlock(&root->objectid_mutex);
78 return 0;
79 }
Chris Mason7cfcc172007-04-02 14:53:59 -040080 path = btrfs_alloc_path();
81 BUG_ON(!path);
Chris Masonb1a4d962007-04-04 15:27:52 -040082 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
83 search_key.objectid = search_start;
Zheng Yan6527cdb2008-09-05 16:43:53 -040084 search_key.type = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -040085 search_key.offset = 0;
86
Chris Mason7cfcc172007-04-02 14:53:59 -040087 btrfs_init_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040088 start_found = 0;
Chris Mason7cfcc172007-04-02 14:53:59 -040089 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
Chris Mason9f5fae22007-03-20 14:38:32 -040090 if (ret < 0)
91 goto error;
92
Chris Mason9f5fae22007-03-20 14:38:32 -040093 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -040094 l = path->nodes[0];
Chris Mason7cfcc172007-04-02 14:53:59 -040095 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040096 if (slot >= btrfs_header_nritems(l)) {
Chris Mason7cfcc172007-04-02 14:53:59 -040097 ret = btrfs_next_leaf(root, path);
Chris Mason9f5fae22007-03-20 14:38:32 -040098 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 }
Chris Mason5f39d392007-10-15 16:14:19 -0400111 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason9f5fae22007-03-20 14:38:32 -0400112 if (key.objectid >= search_start) {
113 if (start_found) {
114 if (last_ino < search_start)
115 last_ino = search_start;
Yanb1785422008-01-22 12:46:56 -0500116 if (key.objectid > last_ino) {
Chris Mason9f5fae22007-03-20 14:38:32 -0400117 *objectid = last_ino;
118 goto found;
119 }
120 }
121 }
Zheng Yan6527cdb2008-09-05 16:43:53 -0400122 if (key.objectid >= BTRFS_LAST_FREE_OBJECTID)
123 break;
Chris Mason9f5fae22007-03-20 14:38:32 -0400124 start_found = 1;
125 last_ino = key.objectid + 1;
Chris Mason7cfcc172007-04-02 14:53:59 -0400126 path->slots[0]++;
Chris Mason9f5fae22007-03-20 14:38:32 -0400127 }
128 // FIXME -ENOSPC
Zheng Yan6527cdb2008-09-05 16:43:53 -0400129 BUG_ON(1);
Chris Mason9f5fae22007-03-20 14:38:32 -0400130found:
Chris Mason7cfcc172007-04-02 14:53:59 -0400131 btrfs_release_path(root, path);
132 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400133 BUG_ON(*objectid < search_start);
Chris Masona2135012008-06-25 16:01:30 -0400134 mutex_unlock(&root->objectid_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400135 return 0;
136error:
Chris Mason7cfcc172007-04-02 14:53:59 -0400137 btrfs_release_path(root, path);
138 btrfs_free_path(path);
Chris Masona2135012008-06-25 16:01:30 -0400139 mutex_unlock(&root->objectid_mutex);
Chris Mason9f5fae22007-03-20 14:38:32 -0400140 return ret;
141}