blob: 4054708662548c2c8cc307dff82b011a4b87f6de [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;
27 struct btrfs_leaf *l;
Chris Mason5be6f7f2007-04-05 13:35:25 -040028 struct btrfs_key search_key;
29 int slot;
30
31 path = btrfs_alloc_path();
32 BUG_ON(!path);
33
34 search_key.objectid = (u64)-1;
35 search_key.offset = (u64)-1;
36 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
37 if (ret < 0)
38 goto error;
39 BUG_ON(ret == 0);
40 if (path->slots[0] > 0) {
41 slot = path->slots[0] - 1;
42 l = btrfs_buffer_leaf(path->nodes[0]);
43 *objectid = btrfs_disk_key_objectid(&l->items[slot].key);
44 } else {
45 *objectid = BTRFS_FIRST_FREE_OBJECTID;
46 }
47 ret = 0;
48error:
49 btrfs_free_path(path);
50 return ret;
51}
52
Chris Mason9f5fae22007-03-20 14:38:32 -040053/*
54 * walks the btree of allocated inodes and find a hole.
55 */
56int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
Chris Mason1b05da22007-04-10 12:13:09 -040057 struct btrfs_root *root,
Chris Mason9f5fae22007-03-20 14:38:32 -040058 u64 dirid, u64 *objectid)
59{
Chris Mason7cfcc172007-04-02 14:53:59 -040060 struct btrfs_path *path;
Chris Mason9f5fae22007-03-20 14:38:32 -040061 struct btrfs_key key;
62 int ret;
63 u64 hole_size = 0;
64 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -040065 u64 last_ino = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -040066 int start_found;
67 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -040068 struct btrfs_key search_key;
69 u64 search_start = dirid;
70
Chris Mason7cfcc172007-04-02 14:53:59 -040071 path = btrfs_alloc_path();
72 BUG_ON(!path);
Chris Masonb1a4d962007-04-04 15:27:52 -040073 search_key.flags = 0;
Chris Mason1b05da22007-04-10 12:13:09 -040074 search_start = root->last_inode_alloc;
Chris Masonb1a4d962007-04-04 15:27:52 -040075 search_start = max(search_start, BTRFS_FIRST_FREE_OBJECTID);
76 search_key.objectid = search_start;
77 search_key.offset = 0;
78
Chris Mason7cfcc172007-04-02 14:53:59 -040079 btrfs_init_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040080 start_found = 0;
Chris Mason7cfcc172007-04-02 14:53:59 -040081 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
Chris Mason9f5fae22007-03-20 14:38:32 -040082 if (ret < 0)
83 goto error;
84
Chris Mason7cfcc172007-04-02 14:53:59 -040085 if (path->slots[0] > 0)
86 path->slots[0]--;
Chris Mason9f5fae22007-03-20 14:38:32 -040087
88 while (1) {
Chris Mason7cfcc172007-04-02 14:53:59 -040089 l = btrfs_buffer_leaf(path->nodes[0]);
90 slot = path->slots[0];
Chris Mason9f5fae22007-03-20 14:38:32 -040091 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Mason7cfcc172007-04-02 14:53:59 -040092 ret = btrfs_next_leaf(root, path);
Chris Mason9f5fae22007-03-20 14:38:32 -040093 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 }
106 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
107 if (key.objectid >= search_start) {
108 if (start_found) {
109 if (last_ino < search_start)
110 last_ino = search_start;
111 hole_size = key.objectid - last_ino;
112 if (hole_size > 0) {
113 *objectid = last_ino;
114 goto found;
115 }
116 }
117 }
118 start_found = 1;
119 last_ino = key.objectid + 1;
Chris Mason7cfcc172007-04-02 14:53:59 -0400120 path->slots[0]++;
Chris Mason9f5fae22007-03-20 14:38:32 -0400121 }
122 // FIXME -ENOSPC
123found:
Chris Mason1b05da22007-04-10 12:13:09 -0400124 root->last_inode_alloc = *objectid;
Chris Mason7cfcc172007-04-02 14:53:59 -0400125 btrfs_release_path(root, path);
126 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400127 BUG_ON(*objectid < search_start);
128 return 0;
129error:
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 return ret;
133}